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 ***************************************************************************/
19 #include "gpuStdafx.h"
21 #include "gpuTexture.c"
25 static const short dispWidths[8] = {256,320,512,640,368,384,512,640};
29 short ly0,lx0,ly1,lx1,ly2,lx2,ly3,lx3; // global psx vertex coords
30 int GlobalTextAddrX,GlobalTextAddrY,GlobalTextTP;
31 int GlobalTextREST,GlobalTextABR,GlobalTextPAGE;
33 unsigned int dwGPUVersion;
35 int iGPUHeightMask=511;
38 unsigned char *psxVub;
39 unsigned short *psxVuw;
42 BOOL bNeedInterlaceUpdate;
43 BOOL bNeedRGB24Update;
46 unsigned int ulGPUInfoVals[16];
53 int lClearOnSwapColor;
56 PSXDisplay_t PSXDisplay;
57 PSXDisplay_t PreviousPSXDisplay;
60 BOOL bNeedWriteUpload;
63 // don't do GL vram read
64 void CheckVRamRead(int x, int y, int dx, int dy, bool bFront)
68 void CheckVRamReadEx(int x, int y, int dx, int dy)
76 static void PaintBlackBorders(void)
79 glDisable(GL_SCISSOR_TEST); glError();
80 if(bTexEnabled) {glDisable(GL_TEXTURE_2D);bTexEnabled=FALSE;} glError();
81 if(bOldSmoothShaded) {glShadeModel(GL_FLAT);bOldSmoothShaded=FALSE;} glError();
82 if(bBlendEnable) {glDisable(GL_BLEND);bBlendEnable=FALSE;} glError();
83 glDisable(GL_ALPHA_TEST); glError();
85 glEnable(GL_ALPHA_TEST); glError();
86 glEnable(GL_SCISSOR_TEST); glError();
89 static void fps_update(void);
91 void updateDisplay(void)
93 bFakeFrontBuffer=FALSE;
94 bRenderFrontBuffer=FALSE;
96 if(PSXDisplay.RGB24)// && !bNeedUploadAfter) // (mdec) upload wanted?
98 PrepareFullScreenUpload(-1);
99 UploadScreen(PSXDisplay.Interlaced); // -> upload whole screen from psx vram
100 bNeedUploadTest=FALSE;
101 bNeedInterlaceUpdate=FALSE;
102 bNeedUploadAfter=FALSE;
103 bNeedRGB24Update=FALSE;
106 if(bNeedInterlaceUpdate) // smaller upload?
108 bNeedInterlaceUpdate=FALSE;
109 xrUploadArea=xrUploadAreaIL; // -> upload this rect
113 if(dwActFixes&512) bCheckFF9G4(NULL); // special game fix for FF9
115 if(PSXDisplay.Disabled) // display disabled?
118 glDisable(GL_SCISSOR_TEST); glError();
119 glClearColor(0,0,0,128); glError(); // -> clear whole backbuffer
120 glClear(uiBufferBits); glError();
121 glEnable(GL_SCISSOR_TEST); glError();
123 bDisplayNotSet = TRUE;
129 eglSwapBuffers(display, surface);
133 if(lClearOnSwap) // clear buffer after swap?
137 if(bDisplayNotSet) // -> set new vals
138 SetOGLDisplaySettings(1);
140 g=((GLclampf)GREEN(lClearOnSwapColor))/255.0f; // -> get col
141 b=((GLclampf)BLUE(lClearOnSwapColor))/255.0f;
142 r=((GLclampf)RED(lClearOnSwapColor))/255.0f;
143 glDisable(GL_SCISSOR_TEST); glError();
144 glClearColor(r,g,b,128); glError(); // -> clear
145 glClear(uiBufferBits); glError();
146 glEnable(GL_SCISSOR_TEST); glError();
147 lClearOnSwap=0; // -> done
151 if(iZBufferDepth) // clear zbuffer as well (if activated)
153 glDisable(GL_SCISSOR_TEST); glError();
154 glClear(GL_DEPTH_BUFFER_BIT); glError();
155 glEnable(GL_SCISSOR_TEST); glError();
160 // additional uploads immediatly after swapping
161 if(bNeedUploadAfter) // upload wanted?
163 bNeedUploadAfter=FALSE;
164 bNeedUploadTest=FALSE;
165 UploadScreen(-1); // -> upload
170 bNeedUploadTest=FALSE;
171 if(PSXDisplay.InterlacedTest &&
172 //iOffscreenDrawing>2 &&
173 PreviousPSXDisplay.DisplayPosition.x==PSXDisplay.DisplayPosition.x &&
174 PreviousPSXDisplay.DisplayEnd.x==PSXDisplay.DisplayEnd.x &&
175 PreviousPSXDisplay.DisplayPosition.y==PSXDisplay.DisplayPosition.y &&
176 PreviousPSXDisplay.DisplayEnd.y==PSXDisplay.DisplayEnd.y)
178 PrepareFullScreenUpload(TRUE);
184 void updateFrontDisplay(void)
186 if(PreviousPSXDisplay.Range.x0||
187 PreviousPSXDisplay.Range.y0)
190 bFakeFrontBuffer=FALSE;
191 bRenderFrontBuffer=FALSE;
193 if(iDrawnSomething) // linux:
194 eglSwapBuffers(display, surface);
197 static void ChangeDispOffsetsX(void) // CENTER X
201 if(!PSXDisplay.Range.x1) return; // some range given?
203 l=PSXDisplay.DisplayMode.x;
205 l*=(int)PSXDisplay.Range.x1; // some funky calculation
206 l/=2560;lx=l;l&=0xfffffff8;
208 if(l==PreviousPSXDisplay.Range.x1) return; // some change?
210 sO=PreviousPSXDisplay.Range.x0; // store old
212 if(lx>=PSXDisplay.DisplayMode.x) // range bigger?
214 PreviousPSXDisplay.Range.x1= // -> take display width
215 PSXDisplay.DisplayMode.x;
216 PreviousPSXDisplay.Range.x0=0; // -> start pos is 0
218 else // range smaller? center it
220 PreviousPSXDisplay.Range.x1=l; // -> store width (8 pixel aligned)
221 PreviousPSXDisplay.Range.x0= // -> calc start pos
222 (PSXDisplay.Range.x0-500)/8;
223 if(PreviousPSXDisplay.Range.x0<0) // -> we don't support neg. values yet
224 PreviousPSXDisplay.Range.x0=0;
226 if((PreviousPSXDisplay.Range.x0+lx)> // -> uhuu... that's too much
227 PSXDisplay.DisplayMode.x)
229 PreviousPSXDisplay.Range.x0= // -> adjust start
230 PSXDisplay.DisplayMode.x-lx;
231 PreviousPSXDisplay.Range.x1+=lx-l; // -> adjust width
235 if(sO!=PreviousPSXDisplay.Range.x0) // something changed?
237 bDisplayNotSet=TRUE; // -> recalc display stuff
241 ////////////////////////////////////////////////////////////////////////
243 static void ChangeDispOffsetsY(void) // CENTER Y
245 int iT;short sO; // store previous y size
247 if(PSXDisplay.PAL) iT=48; else iT=28; // different offsets on PAL/NTSC
249 if(PSXDisplay.Range.y0>=iT) // crossed the security line? :)
251 PreviousPSXDisplay.Range.y1= // -> store width
252 PSXDisplay.DisplayModeNew.y;
254 sO=(PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double; // -> calc offset
257 PSXDisplay.DisplayModeNew.y+=sO; // -> add offset to y size, too
259 else sO=0; // else no offset
261 if(sO!=PreviousPSXDisplay.Range.y0) // something changed?
263 PreviousPSXDisplay.Range.y0=sO;
264 bDisplayNotSet=TRUE; // -> recalc display stuff
268 static void updateDisplayIfChanged(void)
272 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
273 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
275 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
276 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew))
277 return; // nothing has changed? fine, no swap buffer needed
279 else // some res change?
281 glLoadIdentity(); glError();
282 glOrtho(0,PSXDisplay.DisplayModeNew.x, // -> new psx resolution
283 PSXDisplay.DisplayModeNew.y, 0, -1, 1); glError();
284 if(bKeepRatio) SetAspectRatio();
287 bDisplayNotSet = TRUE; // re-calc offsets/display area
290 if(PSXDisplay.RGB24!=PSXDisplay.RGB24New) // clean up textures, if rgb mode change (usually mdec on/off)
292 PreviousPSXDisplay.RGB24=0; // no full 24 frame uploaded yet
293 ResetTextureArea(FALSE);
297 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
298 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
299 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
300 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
302 PSXDisplay.DisplayEnd.x= // calc new ends
303 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
304 PSXDisplay.DisplayEnd.y=
305 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
306 PreviousPSXDisplay.DisplayEnd.x=
307 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
308 PreviousPSXDisplay.DisplayEnd.y=
309 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
311 ChangeDispOffsetsX();
312 if(bUp) updateDisplay(); // yeah, real update (swap buffer)
315 #define GPUwriteStatus_ext GPUwriteStatus_ext // for gpulib to see this
316 void GPUwriteStatus_ext(unsigned int gdata)
318 switch((gdata>>24)&0xff)
321 PSXDisplay.Disabled=1;
322 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
323 drawX=drawY=0;drawW=drawH=0;
324 sSetMask=0;lSetMask=0;bCheckMask=FALSE;iSetMask=0;
326 GlobalTextAddrX=0;GlobalTextAddrY=0;
327 GlobalTextTP=0;GlobalTextABR=0;
328 PSXDisplay.RGB24=FALSE;
329 PSXDisplay.Interlaced=FALSE;
334 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
335 PSXDisplay.Disabled = (gdata & 1);
337 if (iOffscreenDrawing==4 &&
338 PreviousPSXDisplay.Disabled &&
339 !(PSXDisplay.Disabled))
342 if(!PSXDisplay.RGB24)
344 PrepareFullScreenUpload(TRUE);
353 short sx=(short)(gdata & 0x3ff);
356 sy = (short)((gdata>>10)&0x3ff); // really: 0x1ff, but we adjust it later
360 PreviousPSXDisplay.DisplayModeNew.y=sy/PSXDisplay.Double;
363 else PreviousPSXDisplay.DisplayModeNew.y=0;
369 if((!PSXDisplay.Interlaced) &&
370 PreviousPSXDisplay.DisplayPosition.x == sx &&
371 PreviousPSXDisplay.DisplayPosition.y == sy)
374 PSXDisplay.DisplayPosition.x = PreviousPSXDisplay.DisplayPosition.x;
375 PSXDisplay.DisplayPosition.y = PreviousPSXDisplay.DisplayPosition.y;
376 PreviousPSXDisplay.DisplayPosition.x = sx;
377 PreviousPSXDisplay.DisplayPosition.y = sy;
381 if((!PSXDisplay.Interlaced) &&
382 PSXDisplay.DisplayPosition.x == sx &&
383 PSXDisplay.DisplayPosition.y == sy)
385 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
386 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
387 PSXDisplay.DisplayPosition.x = sx;
388 PSXDisplay.DisplayPosition.y = sy;
391 PSXDisplay.DisplayEnd.x=
392 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
393 PSXDisplay.DisplayEnd.y=
394 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
396 PreviousPSXDisplay.DisplayEnd.x=
397 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
398 PreviousPSXDisplay.DisplayEnd.y=
399 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
401 bDisplayNotSet = TRUE;
403 if (!(PSXDisplay.Interlaced))
408 if(PSXDisplay.InterlacedTest &&
409 ((PreviousPSXDisplay.DisplayPosition.x != PSXDisplay.DisplayPosition.x)||
410 (PreviousPSXDisplay.DisplayPosition.y != PSXDisplay.DisplayPosition.y)))
411 PSXDisplay.InterlacedTest--;
416 PSXDisplay.Range.x0=gdata & 0x7ff; //0x3ff;
417 PSXDisplay.Range.x1=(gdata>>12) & 0xfff;//0x7ff;
419 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
421 ChangeDispOffsetsX();
425 PreviousPSXDisplay.Height = PSXDisplay.Height;
427 PSXDisplay.Range.y0=gdata & 0x3ff;
428 PSXDisplay.Range.y1=(gdata>>10) & 0x3ff;
430 PSXDisplay.Height = PSXDisplay.Range.y1 -
431 PSXDisplay.Range.y0 +
432 PreviousPSXDisplay.DisplayModeNew.y;
434 if (PreviousPSXDisplay.Height != PSXDisplay.Height)
436 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
437 ChangeDispOffsetsY();
438 updateDisplayIfChanged();
443 PSXDisplay.DisplayModeNew.x = dispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
445 if (gdata&0x04) PSXDisplay.Double=2;
446 else PSXDisplay.Double=1;
447 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
449 ChangeDispOffsetsY();
451 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
452 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
453 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
455 PreviousPSXDisplay.InterlacedNew=FALSE;
456 if (PSXDisplay.InterlacedNew)
458 if(!PSXDisplay.Interlaced)
460 PSXDisplay.InterlacedTest=2;
461 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
462 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
463 PreviousPSXDisplay.InterlacedNew=TRUE;
468 PSXDisplay.InterlacedTest=0;
470 updateDisplayIfChanged();
475 /////////////////////////////////////////////////////////////////////////////
479 #include "../gpulib/gpu.c"
481 static int is_opened;
483 static void set_vram(void *vram)
486 psxVuw=(unsigned short *)psxVub;
489 int renderer_init(void)
493 PSXDisplay.RGB24 = FALSE; // init some stuff
494 PSXDisplay.Interlaced = FALSE;
495 PSXDisplay.DrawOffset.x = 0;
496 PSXDisplay.DrawOffset.y = 0;
497 PSXDisplay.DisplayMode.x= 320;
498 PSXDisplay.DisplayMode.y= 240;
499 PSXDisplay.Disabled = FALSE;
500 PSXDisplay.Range.x0=0;
501 PSXDisplay.Range.x1=0;
502 PSXDisplay.Double = 1;
504 lGPUstatusRet = 0x14802000;
509 void renderer_finish(void)
513 void renderer_notify_res_change(void)
517 void renderer_notify_scanout_change(int x, int y)
521 extern const unsigned char cmd_lengths[256];
523 // XXX: mostly dupe code from soft peops
524 int do_cmd_list(unsigned int *list, int list_len, int *last_cmd)
526 unsigned int cmd, len;
527 unsigned int *list_start = list;
528 unsigned int *list_end = list + list_len;
530 for (; list < list_end; list += 1 + len)
533 len = cmd_lengths[cmd];
534 if (list + 1 + len > list_end) {
540 if (cmd == 0xa0 || cmd == 0xc0)
541 break; // image i/o, forward to upper layer
542 else if ((cmd & 0xf8) == 0xe0)
543 gpu.ex_regs[cmd & 7] = list[0];
546 primTableJ[cmd]((void *)list);
552 uint32_t num_vertexes = 2;
553 uint32_t *list_position = &(list[3]);
557 if(list_position >= list_end) {
562 if((*list_position & 0xf000f000) == 0x50005000)
569 len += (num_vertexes - 2);
575 uint32_t num_vertexes = 2;
576 uint32_t *list_position = &(list[4]);
580 if(list_position >= list_end) {
585 if((*list_position & 0xf000f000) == 0x50005000)
592 len += (num_vertexes - 2) * 2;
597 case 0xA0: // sys -> vid
599 short *slist = (void *)list;
600 uint32_t load_width = slist[4];
601 uint32_t load_height = slist[5];
602 uint32_t load_size = load_width * load_height;
604 len += load_size / 2;
612 gpu.ex_regs[1] &= ~0x1ff;
613 gpu.ex_regs[1] |= lGPUstatusRet & 0x1ff;
616 return list - list_start;
619 void renderer_sync_ecmds(uint32_t *ecmds)
621 cmdTexturePage((unsigned char *)&ecmds[1]);
622 cmdTextureWindow((unsigned char *)&ecmds[2]);
623 cmdDrawAreaStart((unsigned char *)&ecmds[3]);
624 cmdDrawAreaEnd((unsigned char *)&ecmds[4]);
625 cmdDrawOffset((unsigned char *)&ecmds[5]);
626 cmdSTP((unsigned char *)&ecmds[6]);
629 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
634 VRAMWrite.Height = h;
639 void renderer_flush_queues(void)
643 void renderer_set_interlace(int enable, int is_odd)
652 int vout_finish(void)
657 void vout_update(void)
659 if(PSXDisplay.Interlaced) // interlaced mode?
661 if(PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
663 updateDisplay(); // -> swap buffers (new frame)
666 else if(bRenderFrontBuffer) // no interlace mode? and some stuff in front has changed?
668 updateFrontDisplay(); // -> update front buffer
672 void vout_blank(void)
676 void vout_set_config(const struct rearmed_cbs *cbs)
680 static struct rearmed_cbs *cbs;
682 long GPUopen(unsigned long *disp, char *cap, char *cfg)
686 iResX = cbs->screen_w;
687 iResY = cbs->screen_h;
688 rRatioRect.left = rRatioRect.top=0;
689 rRatioRect.right = iResX;
690 rRatioRect.bottom = iResY;
692 bDisplayNotSet = TRUE;
694 CSTEXTURE = CSVERTEX = CSCOLOR = 0;
696 InitializeTextureStore(); // init texture mem
698 ret = GLinitialize(cbs->gles_display, cbs->gles_surface);
710 GLcleanup(); // close OGL
714 /* acting as both renderer and vout handler here .. */
715 void renderer_set_config(const struct rearmed_cbs *cbs_)
717 cbs = (void *)cbs_; // ugh..
719 iOffscreenDrawing = 0;
724 dwActFixes = cbs->gpu_peopsgl.dwActFixes;
725 bDrawDither = cbs->gpu_peopsgl.bDrawDither;
726 iFilterType = cbs->gpu_peopsgl.iFilterType;
727 iFrameTexType = cbs->gpu_peopsgl.iFrameTexType;
728 iUseMask = cbs->gpu_peopsgl.iUseMask;
729 bOpaquePass = cbs->gpu_peopsgl.bOpaquePass;
730 bAdvancedBlend = cbs->gpu_peopsgl.bAdvancedBlend;
731 bUseFastMdec = cbs->gpu_peopsgl.bUseFastMdec;
732 iTexGarbageCollection = cbs->gpu_peopsgl.iTexGarbageCollection;
733 iVRamSize = cbs->gpu_peopsgl.iVRamSize;
735 if (cbs->pl_set_gpu_caps)
736 cbs->pl_set_gpu_caps(GPU_CAP_OWNS_DISPLAY);
738 if (is_opened && cbs->gles_display != NULL && cbs->gles_surface != NULL) {
741 GPUopen(NULL, NULL, NULL);
747 void SetAspectRatio(void)
749 if (cbs->pl_get_layer_pos)
750 cbs->pl_get_layer_pos(&rRatioRect.left, &rRatioRect.top, &rRatioRect.right, &rRatioRect.bottom);
752 glScissor(rRatioRect.left,
753 iResY-(rRatioRect.top+rRatioRect.bottom),
754 rRatioRect.right,rRatioRect.bottom);
755 glViewport(rRatioRect.left,
756 iResY-(rRatioRect.top+rRatioRect.bottom),
757 rRatioRect.right,rRatioRect.bottom);
761 static void fps_update(void)
766 if(cbs->flips_per_sec != 0)
768 snprintf(buf,sizeof(buf),"%2d %4.1f",cbs->flips_per_sec,cbs->vsps_cur);
771 if(cbs->cpu_usage != 0)
773 snprintf(buf,sizeof(buf),"%3d",cbs->cpu_usage);
778 void renderer_sync(void)
782 void renderer_notify_update_lace(int updated)