1 /***************************************************************************
2 begin : Sun Mar 08 2009
3 copyright : (C) 1999-2009 by Pete Bernert
4 email : BlackDove@addcom.de
6 PCSX rearmed rework (C) notaz, 2012
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. See also the license.txt file for *
15 * additional informations. *
17 ***************************************************************************/
20 #include "gpuTexture.c"
23 static const short dispWidths[8] = {256,320,512,640,368,384,512,640};
27 short ly0,lx0,ly1,lx1,ly2,lx2,ly3,lx3; // global psx vertex coords
28 long GlobalTextAddrX,GlobalTextAddrY,GlobalTextTP;
29 long GlobalTextREST,GlobalTextABR,GlobalTextPAGE;
31 unsigned long dwGPUVersion;
33 int iGPUHeightMask=511;
36 unsigned char *psxVub;
37 unsigned short *psxVuw;
40 BOOL bNeedInterlaceUpdate;
41 BOOL bNeedRGB24Update;
44 unsigned long ulGPUInfoVals[16];
51 long lClearOnSwapColor;
54 PSXDisplay_t PSXDisplay;
55 PSXDisplay_t PreviousPSXDisplay;
58 BOOL bNeedWriteUpload;
61 // don't do GL vram read
62 void CheckVRamRead(int x, int y, int dx, int dy, bool bFront)
66 void CheckVRamReadEx(int x, int y, int dx, int dy)
74 static void PaintBlackBorders(void)
77 glDisable(GL_SCISSOR_TEST); glError();
78 if(bTexEnabled) {glDisable(GL_TEXTURE_2D);bTexEnabled=FALSE;} glError();
79 if(bOldSmoothShaded) {glShadeModel(GL_FLAT);bOldSmoothShaded=FALSE;} glError();
80 if(bBlendEnable) {glDisable(GL_BLEND);bBlendEnable=FALSE;} glError();
81 glDisable(GL_ALPHA_TEST); glError();
83 glEnable(GL_ALPHA_TEST); glError();
84 glEnable(GL_SCISSOR_TEST); glError();
87 void updateDisplay(void)
89 bFakeFrontBuffer=FALSE;
90 bRenderFrontBuffer=FALSE;
92 if(PSXDisplay.RGB24)// && !bNeedUploadAfter) // (mdec) upload wanted?
94 PrepareFullScreenUpload(-1);
95 UploadScreen(PSXDisplay.Interlaced); // -> upload whole screen from psx vram
96 bNeedUploadTest=FALSE;
97 bNeedInterlaceUpdate=FALSE;
98 bNeedUploadAfter=FALSE;
99 bNeedRGB24Update=FALSE;
102 if(bNeedInterlaceUpdate) // smaller upload?
104 bNeedInterlaceUpdate=FALSE;
105 xrUploadArea=xrUploadAreaIL; // -> upload this rect
109 if(dwActFixes&512) bCheckFF9G4(NULL); // special game fix for FF9
111 if(PSXDisplay.Disabled) // display disabled?
114 glDisable(GL_SCISSOR_TEST); glError();
115 glClearColor(0,0,0,128); glError(); // -> clear whole backbuffer
116 glClear(uiBufferBits); glError();
117 glEnable(GL_SCISSOR_TEST); glError();
119 bDisplayNotSet = TRUE;
123 eglSwapBuffers(display, surface);
126 if(lClearOnSwap) // clear buffer after swap?
130 if(bDisplayNotSet) // -> set new vals
131 SetOGLDisplaySettings(1);
133 g=((GLclampf)GREEN(lClearOnSwapColor))/255.0f; // -> get col
134 b=((GLclampf)BLUE(lClearOnSwapColor))/255.0f;
135 r=((GLclampf)RED(lClearOnSwapColor))/255.0f;
136 glDisable(GL_SCISSOR_TEST); glError();
137 glClearColor(r,g,b,128); glError(); // -> clear
138 glClear(uiBufferBits); glError();
139 glEnable(GL_SCISSOR_TEST); glError();
140 lClearOnSwap=0; // -> done
144 if(iZBufferDepth) // clear zbuffer as well (if activated)
146 glDisable(GL_SCISSOR_TEST); glError();
147 glClear(GL_DEPTH_BUFFER_BIT); glError();
148 glEnable(GL_SCISSOR_TEST); glError();
153 // additional uploads immediatly after swapping
154 if(bNeedUploadAfter) // upload wanted?
156 bNeedUploadAfter=FALSE;
157 bNeedUploadTest=FALSE;
158 UploadScreen(-1); // -> upload
163 bNeedUploadTest=FALSE;
164 if(PSXDisplay.InterlacedTest &&
165 //iOffscreenDrawing>2 &&
166 PreviousPSXDisplay.DisplayPosition.x==PSXDisplay.DisplayPosition.x &&
167 PreviousPSXDisplay.DisplayEnd.x==PSXDisplay.DisplayEnd.x &&
168 PreviousPSXDisplay.DisplayPosition.y==PSXDisplay.DisplayPosition.y &&
169 PreviousPSXDisplay.DisplayEnd.y==PSXDisplay.DisplayEnd.y)
171 PrepareFullScreenUpload(TRUE);
177 void updateFrontDisplay(void)
179 if(PreviousPSXDisplay.Range.x0||
180 PreviousPSXDisplay.Range.y0)
183 bFakeFrontBuffer=FALSE;
184 bRenderFrontBuffer=FALSE;
186 if(iDrawnSomething) // linux:
187 eglSwapBuffers(display, surface);
190 static void ChangeDispOffsetsX(void) // CENTER X
194 if(!PSXDisplay.Range.x1) return; // some range given?
196 l=PSXDisplay.DisplayMode.x;
198 l*=(long)PSXDisplay.Range.x1; // some funky calculation
199 l/=2560;lx=l;l&=0xfffffff8;
201 if(l==PreviousPSXDisplay.Range.x1) return; // some change?
203 sO=PreviousPSXDisplay.Range.x0; // store old
205 if(lx>=PSXDisplay.DisplayMode.x) // range bigger?
207 PreviousPSXDisplay.Range.x1= // -> take display width
208 PSXDisplay.DisplayMode.x;
209 PreviousPSXDisplay.Range.x0=0; // -> start pos is 0
211 else // range smaller? center it
213 PreviousPSXDisplay.Range.x1=l; // -> store width (8 pixel aligned)
214 PreviousPSXDisplay.Range.x0= // -> calc start pos
215 (PSXDisplay.Range.x0-500)/8;
216 if(PreviousPSXDisplay.Range.x0<0) // -> we don't support neg. values yet
217 PreviousPSXDisplay.Range.x0=0;
219 if((PreviousPSXDisplay.Range.x0+lx)> // -> uhuu... that's too much
220 PSXDisplay.DisplayMode.x)
222 PreviousPSXDisplay.Range.x0= // -> adjust start
223 PSXDisplay.DisplayMode.x-lx;
224 PreviousPSXDisplay.Range.x1+=lx-l; // -> adjust width
228 if(sO!=PreviousPSXDisplay.Range.x0) // something changed?
230 bDisplayNotSet=TRUE; // -> recalc display stuff
234 ////////////////////////////////////////////////////////////////////////
236 static void ChangeDispOffsetsY(void) // CENTER Y
238 int iT;short sO; // store previous y size
240 if(PSXDisplay.PAL) iT=48; else iT=28; // different offsets on PAL/NTSC
242 if(PSXDisplay.Range.y0>=iT) // crossed the security line? :)
244 PreviousPSXDisplay.Range.y1= // -> store width
245 PSXDisplay.DisplayModeNew.y;
247 sO=(PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double; // -> calc offset
250 PSXDisplay.DisplayModeNew.y+=sO; // -> add offset to y size, too
252 else sO=0; // else no offset
254 if(sO!=PreviousPSXDisplay.Range.y0) // something changed?
256 PreviousPSXDisplay.Range.y0=sO;
257 bDisplayNotSet=TRUE; // -> recalc display stuff
261 static void updateDisplayIfChanged(void)
265 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
266 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
268 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
269 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew))
270 return; // nothing has changed? fine, no swap buffer needed
272 else // some res change?
274 glLoadIdentity(); glError();
275 glOrtho(0,PSXDisplay.DisplayModeNew.x, // -> new psx resolution
276 PSXDisplay.DisplayModeNew.y, 0, -1, 1); glError();
277 if(bKeepRatio) SetAspectRatio();
280 bDisplayNotSet = TRUE; // re-calc offsets/display area
283 if(PSXDisplay.RGB24!=PSXDisplay.RGB24New) // clean up textures, if rgb mode change (usually mdec on/off)
285 PreviousPSXDisplay.RGB24=0; // no full 24 frame uploaded yet
286 ResetTextureArea(FALSE);
290 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
291 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
292 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
293 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
295 PSXDisplay.DisplayEnd.x= // calc new ends
296 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
297 PSXDisplay.DisplayEnd.y=
298 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
299 PreviousPSXDisplay.DisplayEnd.x=
300 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
301 PreviousPSXDisplay.DisplayEnd.y=
302 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
304 ChangeDispOffsetsX();
305 if(bUp) updateDisplay(); // yeah, real update (swap buffer)
308 #define GPUwriteStatus_ext GPUwriteStatus_ext // for gpulib to see this
309 void GPUwriteStatus_ext(unsigned long gdata)
311 switch((gdata>>24)&0xff)
314 PSXDisplay.Disabled=1;
315 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
316 drawX=drawY=0;drawW=drawH=0;
317 sSetMask=0;lSetMask=0;bCheckMask=FALSE;iSetMask=0;
319 GlobalTextAddrX=0;GlobalTextAddrY=0;
320 GlobalTextTP=0;GlobalTextABR=0;
321 PSXDisplay.RGB24=FALSE;
322 PSXDisplay.Interlaced=FALSE;
327 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
328 PSXDisplay.Disabled = (gdata & 1);
330 if (iOffscreenDrawing==4 &&
331 PreviousPSXDisplay.Disabled &&
332 !(PSXDisplay.Disabled))
335 if(!PSXDisplay.RGB24)
337 PrepareFullScreenUpload(TRUE);
346 short sx=(short)(gdata & 0x3ff);
349 sy = (short)((gdata>>10)&0x3ff); // really: 0x1ff, but we adjust it later
353 PreviousPSXDisplay.DisplayModeNew.y=sy/PSXDisplay.Double;
356 else PreviousPSXDisplay.DisplayModeNew.y=0;
362 if((!PSXDisplay.Interlaced) &&
363 PreviousPSXDisplay.DisplayPosition.x == sx &&
364 PreviousPSXDisplay.DisplayPosition.y == sy)
367 PSXDisplay.DisplayPosition.x = PreviousPSXDisplay.DisplayPosition.x;
368 PSXDisplay.DisplayPosition.y = PreviousPSXDisplay.DisplayPosition.y;
369 PreviousPSXDisplay.DisplayPosition.x = sx;
370 PreviousPSXDisplay.DisplayPosition.y = sy;
374 if((!PSXDisplay.Interlaced) &&
375 PSXDisplay.DisplayPosition.x == sx &&
376 PSXDisplay.DisplayPosition.y == sy)
378 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
379 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
380 PSXDisplay.DisplayPosition.x = sx;
381 PSXDisplay.DisplayPosition.y = sy;
384 PSXDisplay.DisplayEnd.x=
385 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
386 PSXDisplay.DisplayEnd.y=
387 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
389 PreviousPSXDisplay.DisplayEnd.x=
390 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
391 PreviousPSXDisplay.DisplayEnd.y=
392 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
394 bDisplayNotSet = TRUE;
396 if (!(PSXDisplay.Interlaced))
401 if(PSXDisplay.InterlacedTest &&
402 ((PreviousPSXDisplay.DisplayPosition.x != PSXDisplay.DisplayPosition.x)||
403 (PreviousPSXDisplay.DisplayPosition.y != PSXDisplay.DisplayPosition.y)))
404 PSXDisplay.InterlacedTest--;
409 PSXDisplay.Range.x0=gdata & 0x7ff; //0x3ff;
410 PSXDisplay.Range.x1=(gdata>>12) & 0xfff;//0x7ff;
412 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
414 ChangeDispOffsetsX();
418 PreviousPSXDisplay.Height = PSXDisplay.Height;
420 PSXDisplay.Range.y0=gdata & 0x3ff;
421 PSXDisplay.Range.y1=(gdata>>10) & 0x3ff;
423 PSXDisplay.Height = PSXDisplay.Range.y1 -
424 PSXDisplay.Range.y0 +
425 PreviousPSXDisplay.DisplayModeNew.y;
427 if (PreviousPSXDisplay.Height != PSXDisplay.Height)
429 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
430 ChangeDispOffsetsY();
431 updateDisplayIfChanged();
436 PSXDisplay.DisplayModeNew.x = dispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
438 if (gdata&0x04) PSXDisplay.Double=2;
439 else PSXDisplay.Double=1;
440 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
442 ChangeDispOffsetsY();
444 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
445 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
446 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
448 PreviousPSXDisplay.InterlacedNew=FALSE;
449 if (PSXDisplay.InterlacedNew)
451 if(!PSXDisplay.Interlaced)
453 PSXDisplay.InterlacedTest=2;
454 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
455 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
456 PreviousPSXDisplay.InterlacedNew=TRUE;
461 PSXDisplay.InterlacedTest=0;
463 updateDisplayIfChanged();
468 /////////////////////////////////////////////////////////////////////////////
472 #include "../gpulib/gpu.c"
474 static int is_opened;
476 int renderer_init(void)
478 psxVub=(void *)gpu.vram;
479 psxVuw=(unsigned short *)psxVub;
481 PSXDisplay.RGB24 = FALSE; // init some stuff
482 PSXDisplay.Interlaced = FALSE;
483 PSXDisplay.DrawOffset.x = 0;
484 PSXDisplay.DrawOffset.y = 0;
485 PSXDisplay.DisplayMode.x= 320;
486 PSXDisplay.DisplayMode.y= 240;
487 PSXDisplay.Disabled = FALSE;
488 PSXDisplay.Range.x0=0;
489 PSXDisplay.Range.x1=0;
490 PSXDisplay.Double = 1;
492 lGPUstatusRet = 0x14802000;
497 extern const unsigned char cmd_lengths[256];
499 void do_cmd_list(unsigned int *list, int list_len)
501 unsigned int cmd, len;
503 unsigned int *list_end = list + list_len;
505 for (; list < list_end; list += 1 + len)
508 len = cmd_lengths[cmd];
510 primTableJ[cmd]((void *)list);
516 uint32_t num_vertexes = 1;
517 uint32_t *list_position = &(list[2]);
521 if((*list_position & 0xf000f000) == 0x50005000 || list_position >= list_end)
529 len += (num_vertexes - 2);
536 uint32_t num_vertexes = 1;
537 uint32_t *list_position = &(list[2]);
541 if((*list_position & 0xf000f000) == 0x50005000 || list_position >= list_end)
549 len += (num_vertexes - 2) * 2;
555 case 0xA0: // sys -> vid
557 short *slist = (void *)list;
558 uint32_t load_width = slist[4];
559 uint32_t load_height = slist[5];
560 uint32_t load_size = load_width * load_height;
562 len += load_size / 2;
570 void renderer_sync_ecmds(uint32_t *ecmds)
572 cmdTexturePage((unsigned char *)&ecmds[1]);
573 cmdTextureWindow((unsigned char *)&ecmds[2]);
574 cmdDrawAreaStart((unsigned char *)&ecmds[3]);
575 cmdDrawAreaEnd((unsigned char *)&ecmds[4]);
576 cmdDrawOffset((unsigned char *)&ecmds[5]);
577 cmdSTP((unsigned char *)&ecmds[6]);
580 void renderer_update_caches(int x, int y, int w, int h)
585 VRAMWrite.Height = h;
590 void renderer_flush_queues(void)
594 void renderer_set_interlace(int enable, int is_odd)
603 int vout_finish(void)
608 void vout_update(void)
610 if(PSXDisplay.Interlaced) // interlaced mode?
612 if(PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
614 updateDisplay(); // -> swap buffers (new frame)
617 else if(bRenderFrontBuffer) // no interlace mode? and some stuff in front has changed?
619 updateFrontDisplay(); // -> update front buffer
623 long GPUopen(void **dpy)
625 iResX = 800; iResY = 480;
626 rRatioRect.left = rRatioRect.top=0;
627 rRatioRect.right = iResX;
628 rRatioRect.bottom = iResY;
630 bDisplayNotSet = TRUE;
632 CSTEXTURE = CSVERTEX = CSCOLOR = 0;
634 InitializeTextureStore(); // init texture mem
637 return GLinitialize();
644 GLcleanup(); // close OGL
648 //#include "../../frontend/plugin_lib.h"
650 static const struct rearmed_cbs *cbs;
652 /* acting as both renderer and vout handler here .. */
653 void renderer_set_config(const struct rearmed_cbs *cbs_)
657 iOffscreenDrawing = 0;
662 dwActFixes = cbs->gpu_peopsgl.dwActFixes;
663 bDrawDither = cbs->gpu_peopsgl.bDrawDither;
664 iFilterType = cbs->gpu_peopsgl.iFilterType;
665 iFrameTexType = cbs->gpu_peopsgl.iFrameTexType;
666 iUseMask = cbs->gpu_peopsgl.iUseMask;
667 bOpaquePass = cbs->gpu_peopsgl.bOpaquePass;
668 bAdvancedBlend = cbs->gpu_peopsgl.bAdvancedBlend;
669 bUseFastMdec = cbs->gpu_peopsgl.bUseFastMdec;
670 iTexGarbageCollection = cbs->gpu_peopsgl.iTexGarbageCollection;
671 iVRamSize = cbs->gpu_peopsgl.iVRamSize;
674 void vout_set_config(const struct rearmed_cbs *cbs)
678 void SetAspectRatio(void)
680 cbs->pl_get_layer_pos(&rRatioRect.left, &rRatioRect.top, &rRatioRect.right, &rRatioRect.bottom);
682 glScissor(rRatioRect.left,
683 iResY-(rRatioRect.top+rRatioRect.bottom),
684 rRatioRect.right,rRatioRect.bottom);
685 glViewport(rRatioRect.left,
686 iResY-(rRatioRect.top+rRatioRect.bottom),
687 rRatioRect.right,rRatioRect.bottom);