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"
24 static const short dispWidths[8] = {256,320,512,640,368,384,512,640};
28 short ly0,lx0,ly1,lx1,ly2,lx2,ly3,lx3; // global psx vertex coords
29 long GlobalTextAddrX,GlobalTextAddrY,GlobalTextTP;
30 long GlobalTextREST,GlobalTextABR,GlobalTextPAGE;
32 unsigned long dwGPUVersion;
34 int iGPUHeightMask=511;
37 unsigned char *psxVub;
38 unsigned short *psxVuw;
41 BOOL bNeedInterlaceUpdate;
42 BOOL bNeedRGB24Update;
45 unsigned long ulGPUInfoVals[16];
52 long lClearOnSwapColor;
55 PSXDisplay_t PSXDisplay;
56 PSXDisplay_t PreviousPSXDisplay;
59 BOOL bNeedWriteUpload;
62 // don't do GL vram read
63 void CheckVRamRead(int x, int y, int dx, int dy, bool bFront)
67 void CheckVRamReadEx(int x, int y, int dx, int dy)
75 static void PaintBlackBorders(void)
78 glDisable(GL_SCISSOR_TEST); glError();
79 if(bTexEnabled) {glDisable(GL_TEXTURE_2D);bTexEnabled=FALSE;} glError();
80 if(bOldSmoothShaded) {glShadeModel(GL_FLAT);bOldSmoothShaded=FALSE;} glError();
81 if(bBlendEnable) {glDisable(GL_BLEND);bBlendEnable=FALSE;} glError();
82 glDisable(GL_ALPHA_TEST); glError();
84 glEnable(GL_ALPHA_TEST); glError();
85 glEnable(GL_SCISSOR_TEST); glError();
88 static void fps_update(void);
90 void updateDisplay(void)
92 bFakeFrontBuffer=FALSE;
93 bRenderFrontBuffer=FALSE;
95 if(PSXDisplay.RGB24)// && !bNeedUploadAfter) // (mdec) upload wanted?
97 PrepareFullScreenUpload(-1);
98 UploadScreen(PSXDisplay.Interlaced); // -> upload whole screen from psx vram
99 bNeedUploadTest=FALSE;
100 bNeedInterlaceUpdate=FALSE;
101 bNeedUploadAfter=FALSE;
102 bNeedRGB24Update=FALSE;
105 if(bNeedInterlaceUpdate) // smaller upload?
107 bNeedInterlaceUpdate=FALSE;
108 xrUploadArea=xrUploadAreaIL; // -> upload this rect
112 if(dwActFixes&512) bCheckFF9G4(NULL); // special game fix for FF9
114 if(PSXDisplay.Disabled) // display disabled?
117 glDisable(GL_SCISSOR_TEST); glError();
118 glClearColor(0,0,0,128); glError(); // -> clear whole backbuffer
119 glClear(uiBufferBits); glError();
120 glEnable(GL_SCISSOR_TEST); glError();
122 bDisplayNotSet = TRUE;
128 eglSwapBuffers(display, surface);
132 if(lClearOnSwap) // clear buffer after swap?
136 if(bDisplayNotSet) // -> set new vals
137 SetOGLDisplaySettings(1);
139 g=((GLclampf)GREEN(lClearOnSwapColor))/255.0f; // -> get col
140 b=((GLclampf)BLUE(lClearOnSwapColor))/255.0f;
141 r=((GLclampf)RED(lClearOnSwapColor))/255.0f;
142 glDisable(GL_SCISSOR_TEST); glError();
143 glClearColor(r,g,b,128); glError(); // -> clear
144 glClear(uiBufferBits); glError();
145 glEnable(GL_SCISSOR_TEST); glError();
146 lClearOnSwap=0; // -> done
150 if(iZBufferDepth) // clear zbuffer as well (if activated)
152 glDisable(GL_SCISSOR_TEST); glError();
153 glClear(GL_DEPTH_BUFFER_BIT); glError();
154 glEnable(GL_SCISSOR_TEST); glError();
159 // additional uploads immediatly after swapping
160 if(bNeedUploadAfter) // upload wanted?
162 bNeedUploadAfter=FALSE;
163 bNeedUploadTest=FALSE;
164 UploadScreen(-1); // -> upload
169 bNeedUploadTest=FALSE;
170 if(PSXDisplay.InterlacedTest &&
171 //iOffscreenDrawing>2 &&
172 PreviousPSXDisplay.DisplayPosition.x==PSXDisplay.DisplayPosition.x &&
173 PreviousPSXDisplay.DisplayEnd.x==PSXDisplay.DisplayEnd.x &&
174 PreviousPSXDisplay.DisplayPosition.y==PSXDisplay.DisplayPosition.y &&
175 PreviousPSXDisplay.DisplayEnd.y==PSXDisplay.DisplayEnd.y)
177 PrepareFullScreenUpload(TRUE);
183 void updateFrontDisplay(void)
185 if(PreviousPSXDisplay.Range.x0||
186 PreviousPSXDisplay.Range.y0)
189 bFakeFrontBuffer=FALSE;
190 bRenderFrontBuffer=FALSE;
192 if(iDrawnSomething) // linux:
193 eglSwapBuffers(display, surface);
196 static void ChangeDispOffsetsX(void) // CENTER X
200 if(!PSXDisplay.Range.x1) return; // some range given?
202 l=PSXDisplay.DisplayMode.x;
204 l*=(long)PSXDisplay.Range.x1; // some funky calculation
205 l/=2560;lx=l;l&=0xfffffff8;
207 if(l==PreviousPSXDisplay.Range.x1) return; // some change?
209 sO=PreviousPSXDisplay.Range.x0; // store old
211 if(lx>=PSXDisplay.DisplayMode.x) // range bigger?
213 PreviousPSXDisplay.Range.x1= // -> take display width
214 PSXDisplay.DisplayMode.x;
215 PreviousPSXDisplay.Range.x0=0; // -> start pos is 0
217 else // range smaller? center it
219 PreviousPSXDisplay.Range.x1=l; // -> store width (8 pixel aligned)
220 PreviousPSXDisplay.Range.x0= // -> calc start pos
221 (PSXDisplay.Range.x0-500)/8;
222 if(PreviousPSXDisplay.Range.x0<0) // -> we don't support neg. values yet
223 PreviousPSXDisplay.Range.x0=0;
225 if((PreviousPSXDisplay.Range.x0+lx)> // -> uhuu... that's too much
226 PSXDisplay.DisplayMode.x)
228 PreviousPSXDisplay.Range.x0= // -> adjust start
229 PSXDisplay.DisplayMode.x-lx;
230 PreviousPSXDisplay.Range.x1+=lx-l; // -> adjust width
234 if(sO!=PreviousPSXDisplay.Range.x0) // something changed?
236 bDisplayNotSet=TRUE; // -> recalc display stuff
240 ////////////////////////////////////////////////////////////////////////
242 static void ChangeDispOffsetsY(void) // CENTER Y
244 int iT;short sO; // store previous y size
246 if(PSXDisplay.PAL) iT=48; else iT=28; // different offsets on PAL/NTSC
248 if(PSXDisplay.Range.y0>=iT) // crossed the security line? :)
250 PreviousPSXDisplay.Range.y1= // -> store width
251 PSXDisplay.DisplayModeNew.y;
253 sO=(PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double; // -> calc offset
256 PSXDisplay.DisplayModeNew.y+=sO; // -> add offset to y size, too
258 else sO=0; // else no offset
260 if(sO!=PreviousPSXDisplay.Range.y0) // something changed?
262 PreviousPSXDisplay.Range.y0=sO;
263 bDisplayNotSet=TRUE; // -> recalc display stuff
267 static void updateDisplayIfChanged(void)
271 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
272 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
274 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
275 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew))
276 return; // nothing has changed? fine, no swap buffer needed
278 else // some res change?
280 glLoadIdentity(); glError();
281 glOrtho(0,PSXDisplay.DisplayModeNew.x, // -> new psx resolution
282 PSXDisplay.DisplayModeNew.y, 0, -1, 1); glError();
283 if(bKeepRatio) SetAspectRatio();
286 bDisplayNotSet = TRUE; // re-calc offsets/display area
289 if(PSXDisplay.RGB24!=PSXDisplay.RGB24New) // clean up textures, if rgb mode change (usually mdec on/off)
291 PreviousPSXDisplay.RGB24=0; // no full 24 frame uploaded yet
292 ResetTextureArea(FALSE);
296 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
297 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
298 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
299 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
301 PSXDisplay.DisplayEnd.x= // calc new ends
302 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
303 PSXDisplay.DisplayEnd.y=
304 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
305 PreviousPSXDisplay.DisplayEnd.x=
306 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
307 PreviousPSXDisplay.DisplayEnd.y=
308 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
310 ChangeDispOffsetsX();
311 if(bUp) updateDisplay(); // yeah, real update (swap buffer)
314 #define GPUwriteStatus_ext GPUwriteStatus_ext // for gpulib to see this
315 void GPUwriteStatus_ext(unsigned long gdata)
317 switch((gdata>>24)&0xff)
320 PSXDisplay.Disabled=1;
321 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
322 drawX=drawY=0;drawW=drawH=0;
323 sSetMask=0;lSetMask=0;bCheckMask=FALSE;iSetMask=0;
325 GlobalTextAddrX=0;GlobalTextAddrY=0;
326 GlobalTextTP=0;GlobalTextABR=0;
327 PSXDisplay.RGB24=FALSE;
328 PSXDisplay.Interlaced=FALSE;
333 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
334 PSXDisplay.Disabled = (gdata & 1);
336 if (iOffscreenDrawing==4 &&
337 PreviousPSXDisplay.Disabled &&
338 !(PSXDisplay.Disabled))
341 if(!PSXDisplay.RGB24)
343 PrepareFullScreenUpload(TRUE);
352 short sx=(short)(gdata & 0x3ff);
355 sy = (short)((gdata>>10)&0x3ff); // really: 0x1ff, but we adjust it later
359 PreviousPSXDisplay.DisplayModeNew.y=sy/PSXDisplay.Double;
362 else PreviousPSXDisplay.DisplayModeNew.y=0;
368 if((!PSXDisplay.Interlaced) &&
369 PreviousPSXDisplay.DisplayPosition.x == sx &&
370 PreviousPSXDisplay.DisplayPosition.y == sy)
373 PSXDisplay.DisplayPosition.x = PreviousPSXDisplay.DisplayPosition.x;
374 PSXDisplay.DisplayPosition.y = PreviousPSXDisplay.DisplayPosition.y;
375 PreviousPSXDisplay.DisplayPosition.x = sx;
376 PreviousPSXDisplay.DisplayPosition.y = sy;
380 if((!PSXDisplay.Interlaced) &&
381 PSXDisplay.DisplayPosition.x == sx &&
382 PSXDisplay.DisplayPosition.y == sy)
384 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
385 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
386 PSXDisplay.DisplayPosition.x = sx;
387 PSXDisplay.DisplayPosition.y = sy;
390 PSXDisplay.DisplayEnd.x=
391 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
392 PSXDisplay.DisplayEnd.y=
393 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
395 PreviousPSXDisplay.DisplayEnd.x=
396 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
397 PreviousPSXDisplay.DisplayEnd.y=
398 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
400 bDisplayNotSet = TRUE;
402 if (!(PSXDisplay.Interlaced))
407 if(PSXDisplay.InterlacedTest &&
408 ((PreviousPSXDisplay.DisplayPosition.x != PSXDisplay.DisplayPosition.x)||
409 (PreviousPSXDisplay.DisplayPosition.y != PSXDisplay.DisplayPosition.y)))
410 PSXDisplay.InterlacedTest--;
415 PSXDisplay.Range.x0=gdata & 0x7ff; //0x3ff;
416 PSXDisplay.Range.x1=(gdata>>12) & 0xfff;//0x7ff;
418 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
420 ChangeDispOffsetsX();
424 PreviousPSXDisplay.Height = PSXDisplay.Height;
426 PSXDisplay.Range.y0=gdata & 0x3ff;
427 PSXDisplay.Range.y1=(gdata>>10) & 0x3ff;
429 PSXDisplay.Height = PSXDisplay.Range.y1 -
430 PSXDisplay.Range.y0 +
431 PreviousPSXDisplay.DisplayModeNew.y;
433 if (PreviousPSXDisplay.Height != PSXDisplay.Height)
435 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
436 ChangeDispOffsetsY();
437 updateDisplayIfChanged();
442 PSXDisplay.DisplayModeNew.x = dispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
444 if (gdata&0x04) PSXDisplay.Double=2;
445 else PSXDisplay.Double=1;
446 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
448 ChangeDispOffsetsY();
450 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
451 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
452 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
454 PreviousPSXDisplay.InterlacedNew=FALSE;
455 if (PSXDisplay.InterlacedNew)
457 if(!PSXDisplay.Interlaced)
459 PSXDisplay.InterlacedTest=2;
460 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
461 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
462 PreviousPSXDisplay.InterlacedNew=TRUE;
467 PSXDisplay.InterlacedTest=0;
469 updateDisplayIfChanged();
474 /////////////////////////////////////////////////////////////////////////////
478 #include "../gpulib/gpu.c"
480 static int is_opened;
482 static void set_vram(void *vram)
485 psxVuw=(unsigned short *)psxVub;
488 int renderer_init(void)
492 PSXDisplay.RGB24 = FALSE; // init some stuff
493 PSXDisplay.Interlaced = FALSE;
494 PSXDisplay.DrawOffset.x = 0;
495 PSXDisplay.DrawOffset.y = 0;
496 PSXDisplay.DisplayMode.x= 320;
497 PSXDisplay.DisplayMode.y= 240;
498 PSXDisplay.Disabled = FALSE;
499 PSXDisplay.Range.x0=0;
500 PSXDisplay.Range.x1=0;
501 PSXDisplay.Double = 1;
503 lGPUstatusRet = 0x14802000;
508 void renderer_finish(void)
512 void renderer_notify_res_change(void)
516 extern const unsigned char cmd_lengths[256];
518 // XXX: mostly dupe code from soft peops
519 int do_cmd_list(unsigned int *list, int list_len, int *last_cmd)
521 unsigned int cmd, len;
522 unsigned int *list_start = list;
523 unsigned int *list_end = list + list_len;
525 for (; list < list_end; list += 1 + len)
528 len = cmd_lengths[cmd];
529 if (list + 1 + len > list_end) {
535 if (cmd == 0xa0 || cmd == 0xc0)
536 break; // image i/o, forward to upper layer
537 else if ((cmd & 0xf8) == 0xe0)
538 gpu.ex_regs[cmd & 7] = list[0];
541 primTableJ[cmd]((void *)list);
547 uint32_t num_vertexes = 2;
548 uint32_t *list_position = &(list[3]);
552 if(list_position >= list_end) {
557 if((*list_position & 0xf000f000) == 0x50005000)
564 len += (num_vertexes - 2);
570 uint32_t num_vertexes = 2;
571 uint32_t *list_position = &(list[4]);
575 if(list_position >= list_end) {
580 if((*list_position & 0xf000f000) == 0x50005000)
587 len += (num_vertexes - 2) * 2;
592 case 0xA0: // sys -> vid
594 short *slist = (void *)list;
595 uint32_t load_width = slist[4];
596 uint32_t load_height = slist[5];
597 uint32_t load_size = load_width * load_height;
599 len += load_size / 2;
607 gpu.ex_regs[1] &= ~0x1ff;
608 gpu.ex_regs[1] |= lGPUstatusRet & 0x1ff;
611 return list - list_start;
614 void renderer_sync_ecmds(uint32_t *ecmds)
616 cmdTexturePage((unsigned char *)&ecmds[1]);
617 cmdTextureWindow((unsigned char *)&ecmds[2]);
618 cmdDrawAreaStart((unsigned char *)&ecmds[3]);
619 cmdDrawAreaEnd((unsigned char *)&ecmds[4]);
620 cmdDrawOffset((unsigned char *)&ecmds[5]);
621 cmdSTP((unsigned char *)&ecmds[6]);
624 void renderer_update_caches(int x, int y, int w, int h)
629 VRAMWrite.Height = h;
634 void renderer_flush_queues(void)
638 void renderer_set_interlace(int enable, int is_odd)
647 int vout_finish(void)
652 void vout_update(void)
654 if(PSXDisplay.Interlaced) // interlaced mode?
656 if(PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
658 updateDisplay(); // -> swap buffers (new frame)
661 else if(bRenderFrontBuffer) // no interlace mode? and some stuff in front has changed?
663 updateFrontDisplay(); // -> update front buffer
667 void vout_blank(void)
671 void vout_set_config(const struct rearmed_cbs *cbs)
675 static struct rearmed_cbs *cbs;
677 long GPUopen(void **dpy)
681 iResX = cbs->screen_w;
682 iResY = cbs->screen_h;
683 rRatioRect.left = rRatioRect.top=0;
684 rRatioRect.right = iResX;
685 rRatioRect.bottom = iResY;
687 bDisplayNotSet = TRUE;
689 CSTEXTURE = CSVERTEX = CSCOLOR = 0;
691 InitializeTextureStore(); // init texture mem
693 ret = GLinitialize(cbs->gles_display, cbs->gles_surface);
705 GLcleanup(); // close OGL
709 /* acting as both renderer and vout handler here .. */
710 void renderer_set_config(const struct rearmed_cbs *cbs_)
712 cbs = (void *)cbs_; // ugh..
714 iOffscreenDrawing = 0;
719 dwActFixes = cbs->gpu_peopsgl.dwActFixes;
720 bDrawDither = cbs->gpu_peopsgl.bDrawDither;
721 iFilterType = cbs->gpu_peopsgl.iFilterType;
722 iFrameTexType = cbs->gpu_peopsgl.iFrameTexType;
723 iUseMask = cbs->gpu_peopsgl.iUseMask;
724 bOpaquePass = cbs->gpu_peopsgl.bOpaquePass;
725 bAdvancedBlend = cbs->gpu_peopsgl.bAdvancedBlend;
726 bUseFastMdec = cbs->gpu_peopsgl.bUseFastMdec;
727 iTexGarbageCollection = cbs->gpu_peopsgl.iTexGarbageCollection;
728 iVRamSize = cbs->gpu_peopsgl.iVRamSize;
730 if (cbs->pl_set_gpu_caps)
731 cbs->pl_set_gpu_caps(GPU_CAP_OWNS_DISPLAY);
733 if (is_opened && cbs->gles_display != NULL && cbs->gles_surface != NULL) {
742 void SetAspectRatio(void)
744 if (cbs->pl_get_layer_pos)
745 cbs->pl_get_layer_pos(&rRatioRect.left, &rRatioRect.top, &rRatioRect.right, &rRatioRect.bottom);
747 glScissor(rRatioRect.left,
748 iResY-(rRatioRect.top+rRatioRect.bottom),
749 rRatioRect.right,rRatioRect.bottom);
750 glViewport(rRatioRect.left,
751 iResY-(rRatioRect.top+rRatioRect.bottom),
752 rRatioRect.right,rRatioRect.bottom);
756 static void fps_update(void)
761 if(cbs->flips_per_sec != 0)
763 snprintf(buf,sizeof(buf),"%2d %4.1f",cbs->flips_per_sec,cbs->vsps_cur);
766 if(cbs->cpu_usage != 0)
768 snprintf(buf,sizeof(buf),"%3d",cbs->cpu_usage);
773 void renderer_sync(void)
777 void renderer_notify_update_lace(int updated)