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"
27 static const short dispWidths[8] = {256,320,512,640,368,384,512,640};
31 short ly0,lx0,ly1,lx1,ly2,lx2,ly3,lx3; // global psx vertex coords
32 int GlobalTextAddrX,GlobalTextAddrY,GlobalTextTP;
33 int GlobalTextREST,GlobalTextABR,GlobalTextPAGE;
35 unsigned int dwGPUVersion;
37 int iGPUHeightMask=511;
40 unsigned char *psxVub;
41 unsigned short *psxVuw;
44 BOOL bNeedInterlaceUpdate;
45 BOOL bNeedRGB24Update;
48 unsigned int ulGPUInfoVals[16];
55 int lClearOnSwapColor;
58 PSXDisplay_t PSXDisplay;
59 PSXDisplay_t PreviousPSXDisplay;
62 BOOL bNeedWriteUpload;
65 // don't do GL vram read
66 void CheckVRamRead(int x, int y, int dx, int dy, bool bFront)
70 void CheckVRamReadEx(int x, int y, int dx, int dy)
78 static void PaintBlackBorders(void)
81 glDisable(GL_SCISSOR_TEST); glError();
82 if(bTexEnabled) {glDisable(GL_TEXTURE_2D);bTexEnabled=FALSE;} glError();
83 if(bOldSmoothShaded) {glShadeModel(GL_FLAT);bOldSmoothShaded=FALSE;} glError();
84 if(bBlendEnable) {glDisable(GL_BLEND);bBlendEnable=FALSE;} glError();
85 glDisable(GL_ALPHA_TEST); glError();
87 glEnable(GL_ALPHA_TEST); glError();
88 glEnable(GL_SCISSOR_TEST); glError();
91 static void fps_update(void);
93 void updateDisplay(void)
95 bFakeFrontBuffer=FALSE;
96 bRenderFrontBuffer=FALSE;
98 if(PSXDisplay.RGB24)// && !bNeedUploadAfter) // (mdec) upload wanted?
100 PrepareFullScreenUpload(-1);
101 UploadScreen(PSXDisplay.Interlaced); // -> upload whole screen from psx vram
102 bNeedUploadTest=FALSE;
103 bNeedInterlaceUpdate=FALSE;
104 bNeedUploadAfter=FALSE;
105 bNeedRGB24Update=FALSE;
108 if(bNeedInterlaceUpdate) // smaller upload?
110 bNeedInterlaceUpdate=FALSE;
111 xrUploadArea=xrUploadAreaIL; // -> upload this rect
115 if(dwActFixes&512) bCheckFF9G4(NULL); // special game fix for FF9
117 if(PSXDisplay.Disabled) // display disabled?
120 glDisable(GL_SCISSOR_TEST); glError();
121 glClearColor(0,0,0,128); glError(); // -> clear whole backbuffer
122 glClear(uiBufferBits); glError();
123 glEnable(GL_SCISSOR_TEST); glError();
125 bDisplayNotSet = TRUE;
131 eglSwapBuffers(display, surface);
135 if(lClearOnSwap) // clear buffer after swap?
139 if(bDisplayNotSet) // -> set new vals
140 SetOGLDisplaySettings(1);
142 g=((GLclampf)GREEN(lClearOnSwapColor))/255.0f; // -> get col
143 b=((GLclampf)BLUE(lClearOnSwapColor))/255.0f;
144 r=((GLclampf)RED(lClearOnSwapColor))/255.0f;
145 glDisable(GL_SCISSOR_TEST); glError();
146 glClearColor(r,g,b,128); glError(); // -> clear
147 glClear(uiBufferBits); glError();
148 glEnable(GL_SCISSOR_TEST); glError();
149 lClearOnSwap=0; // -> done
153 if(iZBufferDepth) // clear zbuffer as well (if activated)
155 glDisable(GL_SCISSOR_TEST); glError();
156 glClear(GL_DEPTH_BUFFER_BIT); glError();
157 glEnable(GL_SCISSOR_TEST); glError();
162 // additional uploads immediatly after swapping
163 if(bNeedUploadAfter) // upload wanted?
165 bNeedUploadAfter=FALSE;
166 bNeedUploadTest=FALSE;
167 UploadScreen(-1); // -> upload
172 bNeedUploadTest=FALSE;
173 if(PSXDisplay.InterlacedTest &&
174 //iOffscreenDrawing>2 &&
175 PreviousPSXDisplay.DisplayPosition.x==PSXDisplay.DisplayPosition.x &&
176 PreviousPSXDisplay.DisplayEnd.x==PSXDisplay.DisplayEnd.x &&
177 PreviousPSXDisplay.DisplayPosition.y==PSXDisplay.DisplayPosition.y &&
178 PreviousPSXDisplay.DisplayEnd.y==PSXDisplay.DisplayEnd.y)
180 PrepareFullScreenUpload(TRUE);
186 void updateFrontDisplay(void)
188 if(PreviousPSXDisplay.Range.x0||
189 PreviousPSXDisplay.Range.y0)
192 bFakeFrontBuffer=FALSE;
193 bRenderFrontBuffer=FALSE;
195 if(iDrawnSomething) // linux:
196 eglSwapBuffers(display, surface);
199 static void ChangeDispOffsetsX(void) // CENTER X
203 if(!PSXDisplay.Range.x1) return; // some range given?
205 l=PSXDisplay.DisplayMode.x;
207 l*=(int)PSXDisplay.Range.x1; // some funky calculation
208 l/=2560;lx=l;l&=0xfffffff8;
210 if(l==PreviousPSXDisplay.Range.x1) return; // some change?
212 sO=PreviousPSXDisplay.Range.x0; // store old
214 if(lx>=PSXDisplay.DisplayMode.x) // range bigger?
216 PreviousPSXDisplay.Range.x1= // -> take display width
217 PSXDisplay.DisplayMode.x;
218 PreviousPSXDisplay.Range.x0=0; // -> start pos is 0
220 else // range smaller? center it
222 PreviousPSXDisplay.Range.x1=l; // -> store width (8 pixel aligned)
223 PreviousPSXDisplay.Range.x0= // -> calc start pos
224 (PSXDisplay.Range.x0-500)/8;
225 if(PreviousPSXDisplay.Range.x0<0) // -> we don't support neg. values yet
226 PreviousPSXDisplay.Range.x0=0;
228 if((PreviousPSXDisplay.Range.x0+lx)> // -> uhuu... that's too much
229 PSXDisplay.DisplayMode.x)
231 PreviousPSXDisplay.Range.x0= // -> adjust start
232 PSXDisplay.DisplayMode.x-lx;
233 PreviousPSXDisplay.Range.x1+=lx-l; // -> adjust width
237 if(sO!=PreviousPSXDisplay.Range.x0) // something changed?
239 bDisplayNotSet=TRUE; // -> recalc display stuff
243 ////////////////////////////////////////////////////////////////////////
245 static void ChangeDispOffsetsY(void) // CENTER Y
247 int iT;short sO; // store previous y size
249 if(PSXDisplay.PAL) iT=48; else iT=28; // different offsets on PAL/NTSC
251 if(PSXDisplay.Range.y0>=iT) // crossed the security line? :)
253 PreviousPSXDisplay.Range.y1= // -> store width
254 PSXDisplay.DisplayModeNew.y;
256 sO=(PSXDisplay.Range.y0-iT-4)*PSXDisplay.Double; // -> calc offset
259 PSXDisplay.DisplayModeNew.y+=sO; // -> add offset to y size, too
261 else sO=0; // else no offset
263 if(sO!=PreviousPSXDisplay.Range.y0) // something changed?
265 PreviousPSXDisplay.Range.y0=sO;
266 bDisplayNotSet=TRUE; // -> recalc display stuff
270 static void updateDisplayIfChanged(void)
274 if ((PSXDisplay.DisplayMode.y == PSXDisplay.DisplayModeNew.y) &&
275 (PSXDisplay.DisplayMode.x == PSXDisplay.DisplayModeNew.x))
277 if((PSXDisplay.RGB24 == PSXDisplay.RGB24New) &&
278 (PSXDisplay.Interlaced == PSXDisplay.InterlacedNew))
279 return; // nothing has changed? fine, no swap buffer needed
281 else // some res change?
283 glLoadIdentity(); glError();
284 glOrtho(0,PSXDisplay.DisplayModeNew.x, // -> new psx resolution
285 PSXDisplay.DisplayModeNew.y, 0, -1, 1); glError();
286 if(bKeepRatio) SetAspectRatio();
289 bDisplayNotSet = TRUE; // re-calc offsets/display area
292 if(PSXDisplay.RGB24!=PSXDisplay.RGB24New) // clean up textures, if rgb mode change (usually mdec on/off)
294 PreviousPSXDisplay.RGB24=0; // no full 24 frame uploaded yet
295 ResetTextureArea(FALSE);
299 PSXDisplay.RGB24 = PSXDisplay.RGB24New; // get new infos
300 PSXDisplay.DisplayMode.y = PSXDisplay.DisplayModeNew.y;
301 PSXDisplay.DisplayMode.x = PSXDisplay.DisplayModeNew.x;
302 PSXDisplay.Interlaced = PSXDisplay.InterlacedNew;
304 PSXDisplay.DisplayEnd.x= // calc new ends
305 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
306 PSXDisplay.DisplayEnd.y=
307 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
308 PreviousPSXDisplay.DisplayEnd.x=
309 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
310 PreviousPSXDisplay.DisplayEnd.y=
311 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
313 ChangeDispOffsetsX();
314 if(bUp) updateDisplay(); // yeah, real update (swap buffer)
317 #define GPUwriteStatus_ext GPUwriteStatus_ext // for gpulib to see this
318 void GPUwriteStatus_ext(unsigned int gdata)
323 switch((gdata>>24)&0xff)
326 PSXDisplay.Disabled=1;
327 PSXDisplay.DrawOffset.x=PSXDisplay.DrawOffset.y=0;
328 drawX=drawY=0;drawW=drawH=0;
329 sSetMask=0;lSetMask=0;bCheckMask=FALSE;iSetMask=0;
331 GlobalTextAddrX=0;GlobalTextAddrY=0;
332 GlobalTextTP=0;GlobalTextABR=0;
333 PSXDisplay.RGB24=FALSE;
334 PSXDisplay.Interlaced=FALSE;
339 PreviousPSXDisplay.Disabled = PSXDisplay.Disabled;
340 PSXDisplay.Disabled = (gdata & 1);
342 if (iOffscreenDrawing==4 &&
343 PreviousPSXDisplay.Disabled &&
344 !(PSXDisplay.Disabled))
347 if(!PSXDisplay.RGB24)
349 PrepareFullScreenUpload(TRUE);
358 short sx=(short)(gdata & 0x3ff);
361 sy = (short)((gdata>>10)&0x3ff); // really: 0x1ff, but we adjust it later
365 PreviousPSXDisplay.DisplayModeNew.y=sy/PSXDisplay.Double;
368 else PreviousPSXDisplay.DisplayModeNew.y=0;
374 if((!PSXDisplay.Interlaced) &&
375 PreviousPSXDisplay.DisplayPosition.x == sx &&
376 PreviousPSXDisplay.DisplayPosition.y == sy)
379 PSXDisplay.DisplayPosition.x = PreviousPSXDisplay.DisplayPosition.x;
380 PSXDisplay.DisplayPosition.y = PreviousPSXDisplay.DisplayPosition.y;
381 PreviousPSXDisplay.DisplayPosition.x = sx;
382 PreviousPSXDisplay.DisplayPosition.y = sy;
386 if((!PSXDisplay.Interlaced) &&
387 PSXDisplay.DisplayPosition.x == sx &&
388 PSXDisplay.DisplayPosition.y == sy)
390 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
391 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
392 PSXDisplay.DisplayPosition.x = sx;
393 PSXDisplay.DisplayPosition.y = sy;
396 PSXDisplay.DisplayEnd.x=
397 PSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
398 PSXDisplay.DisplayEnd.y=
399 PSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
401 PreviousPSXDisplay.DisplayEnd.x=
402 PreviousPSXDisplay.DisplayPosition.x+ PSXDisplay.DisplayMode.x;
403 PreviousPSXDisplay.DisplayEnd.y=
404 PreviousPSXDisplay.DisplayPosition.y+ PSXDisplay.DisplayMode.y+PreviousPSXDisplay.DisplayModeNew.y;
406 bDisplayNotSet = TRUE;
408 if (!(PSXDisplay.Interlaced))
413 if(PSXDisplay.InterlacedTest &&
414 ((PreviousPSXDisplay.DisplayPosition.x != PSXDisplay.DisplayPosition.x)||
415 (PreviousPSXDisplay.DisplayPosition.y != PSXDisplay.DisplayPosition.y)))
416 PSXDisplay.InterlacedTest--;
421 PSXDisplay.Range.x0=gdata & 0x7ff; //0x3ff;
422 PSXDisplay.Range.x1=(gdata>>12) & 0xfff;//0x7ff;
424 PSXDisplay.Range.x1-=PSXDisplay.Range.x0;
426 ChangeDispOffsetsX();
430 PreviousPSXDisplay.Height = PSXDisplay.Height;
432 PSXDisplay.Range.y0=gdata & 0x3ff;
433 PSXDisplay.Range.y1=(gdata>>10) & 0x3ff;
435 PSXDisplay.Height = PSXDisplay.Range.y1 -
436 PSXDisplay.Range.y0 +
437 PreviousPSXDisplay.DisplayModeNew.y;
439 if (PreviousPSXDisplay.Height != PSXDisplay.Height)
441 PSXDisplay.DisplayModeNew.y=PSXDisplay.Height*PSXDisplay.Double;
442 ChangeDispOffsetsY();
443 updateDisplayIfChanged();
448 PSXDisplay.DisplayModeNew.x = dispWidths[(gdata & 0x03) | ((gdata & 0x40) >> 4)];
450 if (gdata&0x04) PSXDisplay.Double=2;
451 else PSXDisplay.Double=1;
452 PSXDisplay.DisplayModeNew.y = PSXDisplay.Height*PSXDisplay.Double;
454 ChangeDispOffsetsY();
456 PSXDisplay.PAL = (gdata & 0x08)?TRUE:FALSE; // if 1 - PAL mode, else NTSC
457 PSXDisplay.RGB24New = (gdata & 0x10)?TRUE:FALSE; // if 1 - TrueColor
458 PSXDisplay.InterlacedNew = (gdata & 0x20)?TRUE:FALSE; // if 1 - Interlace
460 PreviousPSXDisplay.InterlacedNew=FALSE;
461 if (PSXDisplay.InterlacedNew)
463 if(!PSXDisplay.Interlaced)
465 PSXDisplay.InterlacedTest=2;
466 PreviousPSXDisplay.DisplayPosition.x = PSXDisplay.DisplayPosition.x;
467 PreviousPSXDisplay.DisplayPosition.y = PSXDisplay.DisplayPosition.y;
468 PreviousPSXDisplay.InterlacedNew=TRUE;
473 PSXDisplay.InterlacedTest=0;
475 updateDisplayIfChanged();
480 /////////////////////////////////////////////////////////////////////////////
484 #include "../gpulib/gpu.c"
486 static void set_vram(void *vram)
489 psxVuw=(unsigned short *)psxVub;
492 int renderer_init(void)
496 PSXDisplay.RGB24 = FALSE; // init some stuff
497 PSXDisplay.Interlaced = FALSE;
498 PSXDisplay.DrawOffset.x = 0;
499 PSXDisplay.DrawOffset.y = 0;
500 PSXDisplay.DisplayMode.x= 320;
501 PSXDisplay.DisplayMode.y= 240;
502 PSXDisplay.Disabled = FALSE;
503 PSXDisplay.Range.x0=0;
504 PSXDisplay.Range.x1=0;
505 PSXDisplay.Double = 1;
507 lGPUstatusRet = 0x14802000;
512 static void clear_gl_state_for_menu(void)
514 static const GLenum caps[] = {
515 GL_ALPHA_TEST, GL_BLEND, GL_COLOR_LOGIC_OP, GL_COLOR_MATERIAL,
516 GL_CULL_FACE, GL_DEPTH_TEST, GL_FOG, GL_LIGHTING, GL_NORMALIZE,
517 GL_POLYGON_OFFSET_FILL, GL_RESCALE_NORMAL, GL_SAMPLE_ALPHA_TO_COVERAGE,
518 GL_SAMPLE_ALPHA_TO_ONE, GL_SAMPLE_COVERAGE, GL_SCISSOR_TEST, GL_STENCIL_TEST
520 static const GLenum cstates[] = {
521 GL_COLOR_ARRAY, GL_NORMAL_ARRAY, GL_POINT_SIZE_ARRAY_OES
524 for (i = 0; i < sizeof(caps) / sizeof(caps[0]); i++)
526 for (i = 0; i < 6; i++)
527 glDisable(GL_CLIP_PLANE0 + i);
528 for (i = 0; i < 8; i++)
529 glDisable(GL_LIGHT0 + i);
530 for (i = 0; i < sizeof(cstates) / sizeof(cstates[0]); i++)
531 glDisableClientState(cstates[i]);
533 glColor4ub(255, 255, 255, 255);
535 glEnable(GL_TEXTURE_2D);
536 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
537 glEnableClientState(GL_VERTEX_ARRAY);
540 void renderer_finish(void)
544 void renderer_notify_res_change(void)
548 void renderer_notify_scanout_change(int x, int y)
552 extern const unsigned char cmd_lengths[256];
554 // XXX: mostly dupe code from soft peops
555 int do_cmd_list(uint32_t *list, int list_len,
556 int *cycles_sum_out, int *cycles_last, int *last_cmd)
558 unsigned int cmd, len;
559 unsigned int *list_start = list;
560 unsigned int *list_end = list + list_len;
562 for (; list < list_end; list += 1 + len)
565 len = cmd_lengths[cmd];
566 if (list + 1 + len > list_end) {
572 if (cmd == 0xa0 || cmd == 0xc0)
573 break; // image i/o, forward to upper layer
574 else if ((cmd & 0xf8) == 0xe0)
575 gpu.ex_regs[cmd & 7] = list[0];
578 primTableJ[cmd]((void *)list);
584 uint32_t num_vertexes = 2;
585 uint32_t *list_position = &(list[3]);
589 if(list_position >= list_end) {
594 if((*list_position & 0xf000f000) == 0x50005000)
601 len += (num_vertexes - 2);
607 uint32_t num_vertexes = 2;
608 uint32_t *list_position = &(list[4]);
612 if(list_position >= list_end) {
617 if((*list_position & 0xf000f000) == 0x50005000)
624 len += (num_vertexes - 2) * 2;
629 case 0xA0: // sys -> vid
631 short *slist = (void *)list;
632 uint32_t load_width = slist[4];
633 uint32_t load_height = slist[5];
634 uint32_t load_size = load_width * load_height;
636 len += load_size / 2;
644 gpu.ex_regs[1] &= ~0x1ff;
645 gpu.ex_regs[1] |= lGPUstatusRet & 0x1ff;
648 return list - list_start;
651 void renderer_sync_ecmds(uint32_t *ecmds)
653 cmdTexturePage((unsigned char *)&ecmds[1]);
654 cmdTextureWindow((unsigned char *)&ecmds[2]);
655 cmdDrawAreaStart((unsigned char *)&ecmds[3]);
656 cmdDrawAreaEnd((unsigned char *)&ecmds[4]);
657 cmdDrawOffset((unsigned char *)&ecmds[5]);
658 cmdSTP((unsigned char *)&ecmds[6]);
661 void renderer_update_caches(int x, int y, int w, int h, int state_changed)
666 VRAMWrite.Height = h;
671 void renderer_flush_queues(void)
675 void renderer_set_interlace(int enable, int is_odd)
684 int vout_finish(void)
689 int vout_update(void)
691 if(PSXDisplay.Interlaced) // interlaced mode?
693 if(PSXDisplay.DisplayMode.x>0 && PSXDisplay.DisplayMode.y>0)
695 updateDisplay(); // -> swap buffers (new frame)
699 else if(bRenderFrontBuffer) // no interlace mode? and some stuff in front has changed?
701 updateFrontDisplay(); // -> update front buffer
707 void vout_blank(void)
711 void vout_set_config(const struct rearmed_cbs *cbs)
715 static struct rearmed_cbs *cbs;
717 long GPUopen(unsigned long *disp, char *cap, char *cfg)
722 fprintf(stderr, "double GPUopen\n");
725 iResX = cbs->screen_w;
726 iResY = cbs->screen_h;
727 rRatioRect.left = rRatioRect.top=0;
728 rRatioRect.right = iResX;
729 rRatioRect.bottom = iResY;
731 bDisplayNotSet = TRUE;
733 CSTEXTURE = CSVERTEX = CSCOLOR = 0;
735 InitializeTextureStore(); // init texture mem
737 ret = GLinitialize(cbs->gles_display, cbs->gles_surface);
751 clear_gl_state_for_menu();
752 GLcleanup(); // close OGL
756 /* acting as both renderer and vout handler here .. */
757 void renderer_set_config(const struct rearmed_cbs *cbs_)
759 cbs = (void *)cbs_; // ugh..
761 iOffscreenDrawing = 0;
766 dwActFixes = cbs->gpu_peopsgl.dwActFixes;
767 bDrawDither = cbs->gpu_peopsgl.bDrawDither;
768 iFilterType = cbs->gpu_peopsgl.iFilterType;
769 iFrameTexType = cbs->gpu_peopsgl.iFrameTexType;
770 iUseMask = cbs->gpu_peopsgl.iUseMask;
771 bOpaquePass = cbs->gpu_peopsgl.bOpaquePass;
772 bAdvancedBlend = cbs->gpu_peopsgl.bAdvancedBlend;
773 bUseFastMdec = cbs->gpu_peopsgl.bUseFastMdec;
774 iTexGarbageCollection = cbs->gpu_peopsgl.iTexGarbageCollection;
775 iVRamSize = cbs->gpu_peopsgl.iVRamSize;
777 if (cbs->pl_set_gpu_caps)
778 cbs->pl_set_gpu_caps(GPU_CAP_OWNS_DISPLAY);
780 if (is_opened && cbs->gles_display != NULL && cbs->gles_surface != NULL) {
781 if (cbs->gles_display != display || cbs->gles_surface != surface) {
783 fprintf(stderr, "gles reinit hack\n");
785 GPUopen(NULL, NULL, NULL);
792 void SetAspectRatio(void)
794 if (cbs->pl_get_layer_pos)
795 cbs->pl_get_layer_pos(&rRatioRect.left, &rRatioRect.top, &rRatioRect.right, &rRatioRect.bottom);
797 glScissor(rRatioRect.left,
798 iResY-(rRatioRect.top+rRatioRect.bottom),
799 rRatioRect.right,rRatioRect.bottom);
800 glViewport(rRatioRect.left,
801 iResY-(rRatioRect.top+rRatioRect.bottom),
802 rRatioRect.right,rRatioRect.bottom);
806 static void fps_update(void)
811 if(cbs->flips_per_sec != 0)
813 snprintf(buf,sizeof(buf),"%2d %4.1f",cbs->flips_per_sec,cbs->vsps_cur);
816 if(cbs->cpu_usage != 0)
818 snprintf(buf,sizeof(buf),"%3d",cbs->cpu_usage);
823 void renderer_sync(void)
827 void renderer_notify_update_lace(int updated)