2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define M64P_PLUGIN_PROTOTYPES 1
21 #include "osal_preproc.h"
22 #include "m64p_plugin.h"
24 #include "ConvertImage.h"
25 #include "DeviceBuilder.h"
26 #include "FrameBuffer.h"
29 #include "liblinux/BMGLibPNG.h"
33 extern FiddledVtx * g_pVtxBase;
34 CRender * CRender::g_pRender=NULL;
35 int CRender::gRenderReferenceCount=0;
37 XMATRIX reverseXY(-1,0,0,0,0,-1,0,0,0,0,1,0,0,0,0,1);
38 XMATRIX reverseY(1,0,0,0,0,-1,0,0,0,0,1,0,0,0,0,1);
39 extern char* right (const char * src, int nchars);
42 #define strcasecmp _stricmp
52 //========================================================================
53 CRender * CRender::GetRender(void)
55 if( CRender::g_pRender == NULL )
57 DebugMessage(M64MSG_ERROR, "g_pRender is NULL");
61 return CRender::g_pRender;
63 bool CRender::IsAvailable()
65 return CRender::g_pRender != NULL;
69 m_fScreenViewportMultX(2.0f),
70 m_fScreenViewportMultY(2.0f),
72 m_dwTexturePerspective(FALSE),
73 m_bAlphaTestEnable(FALSE),
78 m_dwMinFilter(FILTER_POINT),
79 m_dwMagFilter(FILTER_POINT),
82 m_bBlendModeValid(FALSE)
87 for( i=0; i<MAX_TEXTURES; i++ )
89 g_textures[i].m_lpsTexturePtr = NULL;
90 g_textures[i].m_pCTexture = NULL;
92 g_textures[i].m_dwTileWidth = 64; // Value doesn't really matter, as tex not set
93 g_textures[i].m_dwTileHeight = 64;
94 g_textures[i].m_fTexWidth = 64.0f; // Value doesn't really matter, as tex not set
95 g_textures[i].m_fTexHeight = 64.0f;
96 g_textures[i].pTextureEntry = NULL;
98 TileUFlags[i] = TileVFlags[i] = TEXTURE_UV_FLAG_CLAMP;
102 //for( i=0; i<MAX_VERTS; i++)
104 // g_dwVtxFlags[i] = 0;
107 m_pColorCombiner = CDeviceBuilder::GetBuilder()->CreateColorCombiner(this);
108 m_pColorCombiner->Initialize();
110 m_pAlphaBlender = CDeviceBuilder::GetBuilder()->CreateAlphaBlender(this);
116 if( m_pColorCombiner != NULL )
118 CDeviceBuilder::GetBuilder()->DeleteColorCombiner();
119 m_pColorCombiner = NULL;
122 if( m_pAlphaBlender != NULL )
124 CDeviceBuilder::GetBuilder()->DeleteAlphaBlender();
125 m_pAlphaBlender = NULL;
129 void CRender::ResetMatrices()
133 mat.m[0][1] = mat.m[0][2] = mat.m[0][3] =
134 mat.m[1][0] = mat.m[1][2] = mat.m[1][3] =
135 mat.m[2][0] = mat.m[2][1] = mat.m[2][3] =
136 mat.m[3][0] = mat.m[3][1] = mat.m[3][2] = 0.0f;
138 mat.m[0][0] = mat.m[1][1] = mat.m[2][2] = mat.m[3][3] = 1.0f;
140 gRSP.projectionMtxTop = 0;
141 gRSP.modelViewMtxTop = 0;
142 gRSP.projectionMtxs[0] = mat;
143 gRSP.modelviewMtxs[0] = mat;
145 gRSP.bMatrixIsUpdated = true;
146 gRSP.bWorldMatrixIsUpdated = true;
147 UpdateCombinedMatrix();
150 void CRender::SetProjection(const Matrix & mat, bool bPush, bool bReplace)
154 if (gRSP.projectionMtxTop >= (RICE_MATRIX_STACK-1))
156 TRACE0("Pushing past proj stack limits!");
159 gRSP.projectionMtxTop++;
163 // Load projection matrix
164 gRSP.projectionMtxs[gRSP.projectionMtxTop] = mat;
168 gRSP.projectionMtxs[gRSP.projectionMtxTop] = mat * gRSP.projectionMtxs[gRSP.projectionMtxTop-1];
176 // Load projection matrix
177 gRSP.projectionMtxs[gRSP.projectionMtxTop] = mat;
181 gRSP.projectionMtxs[gRSP.projectionMtxTop] = mat * gRSP.projectionMtxs[gRSP.projectionMtxTop];
185 gRSP.bMatrixIsUpdated = true;
187 DumpMatrix(mat,"Set Projection Matrix");
190 bool mtxPopUpError = false;
191 void CRender::SetWorldView(const Matrix & mat, bool bPush, bool bReplace)
195 if (gRSP.modelViewMtxTop >= (RICE_MATRIX_STACK-1))
196 DebuggerAppendMsg("Pushing past modelview stack limits! %s", bReplace?"Load":"Mul");
198 gRSP.modelViewMtxTop++;
200 // We should store the current projection matrix...
203 // Load projection matrix
204 gRSP.modelviewMtxs[gRSP.modelViewMtxTop] = mat;
206 else // Multiply projection matrix
208 gRSP.modelviewMtxs[gRSP.modelViewMtxTop] = mat * gRSP.modelviewMtxs[gRSP.modelViewMtxTop-1];
215 // Load projection matrix
216 gRSP.modelviewMtxs[gRSP.modelViewMtxTop] = mat;
218 //GSI: Hack needed to show heart in OOT & MM
219 //it renders at Z cordinate = 0.0f that gets clipped away.
220 //so we translate them a bit along Z to make them stick
221 if( options.enableHackForGames == HACK_FOR_ZELDA || options.enableHackForGames == HACK_FOR_ZELDA_MM)
223 if(gRSP.modelviewMtxs[gRSP.modelViewMtxTop]._43 == 0.0f
224 && gRSP.modelviewMtxs[gRSP.modelViewMtxTop]._42 != 0.0f
225 && gRSP.modelviewMtxs[gRSP.modelViewMtxTop]._42 <= 94.5f
226 && gRSP.modelviewMtxs[gRSP.modelViewMtxTop]._42 >= -94.5f)
228 gRSP.modelviewMtxs[gRSP.modelViewMtxTop]._43 -= 10.1f;
234 // Multiply projection matrix
235 gRSP.modelviewMtxs[gRSP.modelViewMtxTop] = mat * gRSP.modelviewMtxs[gRSP.modelViewMtxTop];
239 gRSPmodelViewTop = gRSP.modelviewMtxs[gRSP.modelViewMtxTop];
240 if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
242 gRSPmodelViewTop = gRSPmodelViewTop * reverseXY;
244 if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
246 gRSPmodelViewTop = gRSPmodelViewTop * reverseY;
248 MatrixTranspose(&gRSPmodelViewTopTranspose, &gRSPmodelViewTop);
250 gRSP.bMatrixIsUpdated = true;
251 gRSP.bWorldMatrixIsUpdated = true;
253 DumpMatrix(mat,"Set WorldView Matrix");
257 void CRender::PopWorldView()
259 if (gRSP.modelViewMtxTop > 0)
261 gRSP.modelViewMtxTop--;
262 gRSPmodelViewTop = gRSP.modelviewMtxs[gRSP.modelViewMtxTop];
263 if( options.enableHackForGames == HACK_REVERSE_XY_COOR )
265 gRSPmodelViewTop = gRSPmodelViewTop * reverseXY;
267 if( options.enableHackForGames == HACK_REVERSE_Y_COOR )
269 gRSPmodelViewTop = gRSPmodelViewTop * reverseY;
271 MatrixTranspose(&gRSPmodelViewTopTranspose, &gRSPmodelViewTop);
272 gRSP.bMatrixIsUpdated = true;
273 gRSP.bWorldMatrixIsUpdated = true;
279 TRACE0("Popping past worldview stack limits");
281 mtxPopUpError = true;
286 Matrix & CRender::GetWorldProjectMatrix(void)
288 return gRSPworldProject;
291 void CRender::SetWorldProjectMatrix(Matrix &mtx)
294 if( pauseAtNext && (eventToPause == NEXT_TRIANGLE || eventToPause == NEXT_FLUSH_TRI || eventToPause == NEXT_MATRIX_CMD ) )
296 uint32 dwPC = gDlistStack[gDlistStackPointer].pc-8;
297 DebuggerAppendMsg("Force Matrix: pc=%08X", dwPC);
298 DumpMatrix(mtx, "Force Matrix, loading new world-project matrix");
301 gRSPworldProject = mtx;
303 gRSP.bMatrixIsUpdated = false;
304 gRSP.bCombinedMatrixIsUpdated = true;
307 void CRender::SetMux(uint32 dwMux0, uint32 dwMux1)
309 uint64 tempmux = (((uint64)dwMux0) << 32) | (uint64)dwMux1;
310 if( m_Mux != tempmux )
313 m_bBlendModeValid = FALSE;
314 m_pColorCombiner->UpdateCombiner(dwMux0, dwMux1);
319 void CRender::SetCombinerAndBlender()
323 if( g_curRomInfo.bDisableBlender )
324 m_pAlphaBlender->DisableAlphaBlender();
325 else if( currentRomOptions.bNormalBlender )
326 m_pAlphaBlender->NormalAlphaBlender();
328 m_pAlphaBlender->InitBlenderMode();
330 m_pColorCombiner->InitCombinerMode();
333 void CRender::RenderReset()
335 UpdateClipRectangle();
338 gRSP.numVertices = 0;
339 gRSP.maxVertexID = 0;
341 gRSP.fTexScaleX = 1/32.0f;;
342 gRSP.fTexScaleY = 1/32.0f;
345 bool CRender::FillRect(int nX0, int nY0, int nX1, int nY1, uint32 dwColor)
347 LOG_UCODE("FillRect: X0=%d, Y0=%d, X1=%d, Y1=%d, Color=0x%8X", nX0, nY0, nX1, nY1, dwColor);
349 if (g_CI.dwSize != TXT_SIZE_16b && frameBufferOptions.bIgnore)
352 if (status.bHandleN64RenderTexture && !status.bDirectWriteIntoRDRAM)
353 status.bFrameBufferIsDrawn = true;
355 if(status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_AT_1ST_PRIMITIVE)
357 status.bVIOriginIsUpdated=false;
358 CGraphicsContext::Get()->UpdateFrame();
359 DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG, {DebuggerAppendMsg("Screen Update at 1st FillRectangle");});
362 if (status.bCIBufferIsRendered && status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_BEFORE_SCREEN_CLEAR )
364 if ((nX0==0 && nY0 == 0 && (nX1 == (int) g_CI.dwWidth || nX1 == (int) g_CI.dwWidth-1)) ||
365 (nX0==gRDP.scissor.left && nY0 == gRDP.scissor.top && (nX1 == gRDP.scissor.right || nX1 == gRDP.scissor.right-1)) ||
366 ((nX0+nX1 == (int)g_CI.dwWidth || nX0+nX1 == (int)g_CI.dwWidth-1 || nX0+nX1 == gRDP.scissor.left+gRDP.scissor.right || nX0+nX1 == gRDP.scissor.left+gRDP.scissor.right-1) && (nY0 == gRDP.scissor.top || nY0 == 0 || nY0+nY1 == gRDP.scissor.top+gRDP.scissor.bottom || nY0+nY1 == gRDP.scissor.top+gRDP.scissor.bottom-1)))
368 status.bVIOriginIsUpdated=false;
369 CGraphicsContext::Get()->UpdateFrame();
370 DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG,{DebuggerAppendMsg("Screen Update Before Screen Clear");});
375 SetFillMode(RICE_FILLMODE_SOLID);
380 // I don't know why this does not work for OpenGL
381 if( gRDP.otherMode.cycle_type == CYCLE_TYPE_FILL && nX0 == 0 && nY0 == 0 && ((nX1==windowSetting.uViWidth && nY1==windowSetting.uViHeight)||(nX1==windowSetting.uViWidth-1 && nY1==windowSetting.uViHeight-1)) )
383 CGraphicsContext::g_pGraphicsContext->Clear(CLEAR_COLOR_BUFFER,dwColor);
388 //BOOL m_savedZBufferFlag = gRSP.bZBufferEnabled; // Save ZBuffer state
389 ZBufferEnable( FALSE );
391 m_fillRectVtx[0].x = ViewPortTranslatei_x(nX0);
392 m_fillRectVtx[0].y = ViewPortTranslatei_y(nY0);
393 m_fillRectVtx[1].x = ViewPortTranslatei_x(nX1);
394 m_fillRectVtx[1].y = ViewPortTranslatei_y(nY1);
396 SetCombinerAndBlender();
398 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY )
400 ZBufferEnable(FALSE);
404 //dwColor = PostProcessDiffuseColor(0);
405 dwColor = PostProcessDiffuseColor(gRDP.primitiveColor);
408 float depth = (gRDP.otherMode.depth_source == 1 ? gRDP.fPrimitiveDepth : 0 );
412 res = RenderFillRect(dwColor, depth);
413 TurnFogOnOff(gRSP.bFogEnabled);
415 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY )
417 ZBufferEnable(gRSP.bZBufferEnabled);
421 if( options.bWinFrameMode ) SetFillMode(RICE_FILLMODE_WINFRAME );
423 DEBUGGER_PAUSE_AND_DUMP_COUNT_N( NEXT_FILLRECT, {DebuggerAppendMsg("FillRect: X0=%d, Y0=%d, X1=%d, Y1=%d, Color=0x%08X", nX0, nY0, nX1, nY1, dwColor);
424 DebuggerAppendMsg("Pause after FillRect: Color=%08X\n", dwColor);if( logCombiners ) m_pColorCombiner->DisplayMuxString();});
425 DEBUGGER_PAUSE_AND_DUMP_COUNT_N( NEXT_FLUSH_TRI, {DebuggerAppendMsg("FillRect: X0=%d, Y0=%d, X1=%d, Y1=%d, Color=0x%08X", nX0, nY0, nX1, nY1, dwColor);
426 DebuggerAppendMsg("Pause after FillRect: Color=%08X\n", dwColor);if( logCombiners ) m_pColorCombiner->DisplayMuxString();});
432 bool CRender::Line3D(uint32 dwV0, uint32 dwV1, uint32 dwWidth)
434 LOG_UCODE("Line3D: Vtx0=%d, Vtx1=%d, Width=%d", dwV0, dwV1, dwWidth);
435 if( !status.bCIBufferIsRendered ) g_pFrameBufferManager->ActiveTextureBuffer();
437 m_line3DVtx[0].z = (g_vecProjected[dwV0].z + 1.0f) * 0.5f;
438 m_line3DVtx[1].z = (g_vecProjected[dwV1].z + 1.0f) * 0.5f;
440 if( m_line3DVtx[0].z != m_line3DVtx[1].z )
443 if( status.bHandleN64RenderTexture && !status.bDirectWriteIntoRDRAM ) status.bFrameBufferIsDrawn = true;
444 if( status.bHandleN64RenderTexture )
446 g_pRenderTextureInfo->maxUsedHeight = g_pRenderTextureInfo->N64Height;
447 if( status.bHandleN64RenderTexture && !status.bDirectWriteIntoRDRAM )
449 status.bFrameBufferIsDrawn = true;
450 status.bFrameBufferDrawnByTriangles = true;
454 m_line3DVtx[0].x = ViewPortTranslatef_x(g_vecProjected[dwV0].x);
455 m_line3DVtx[0].y = ViewPortTranslatef_y(g_vecProjected[dwV0].y);
456 m_line3DVtx[0].rhw = g_vecProjected[dwV0].w;
457 m_line3DVtx[0].dcDiffuse = PostProcessDiffuseColor(g_dwVtxDifColor[dwV0]);
458 m_line3DVtx[0].dcSpecular = PostProcessSpecularColor();
460 m_line3DVtx[1].x = ViewPortTranslatef_x(g_vecProjected[dwV1].x);
461 m_line3DVtx[1].y = ViewPortTranslatef_y(g_vecProjected[dwV1].y);
462 m_line3DVtx[1].rhw = g_vecProjected[dwV1].w;
463 m_line3DVtx[1].dcDiffuse = PostProcessDiffuseColor(g_dwVtxDifColor[dwV1]);
464 m_line3DVtx[1].dcSpecular = m_line3DVtx[0].dcSpecular;
466 float width = dwWidth*0.5f+1.5f;
468 if( m_line3DVtx[0].y == m_line3DVtx[1].y )
470 m_line3DVector[0].x = m_line3DVector[1].x = m_line3DVtx[0].x;
471 m_line3DVector[2].x = m_line3DVector[3].x = m_line3DVtx[1].x;
473 m_line3DVector[0].y = m_line3DVector[2].y = m_line3DVtx[0].y-width/2*windowSetting.fMultY;
474 m_line3DVector[1].y = m_line3DVector[3].y = m_line3DVtx[0].y+width/2*windowSetting.fMultY;
478 m_line3DVector[0].y = m_line3DVector[1].y = m_line3DVtx[0].y;
479 m_line3DVector[2].y = m_line3DVector[3].y = m_line3DVtx[1].y;
481 m_line3DVector[0].x = m_line3DVector[2].x = m_line3DVtx[0].x-width/2*windowSetting.fMultX;
482 m_line3DVector[1].x = m_line3DVector[3].x = m_line3DVtx[0].x+width/2*windowSetting.fMultX;
485 SetCombinerAndBlender();
487 bool res=RenderLine3D();
489 DEBUGGER_PAUSE_AND_DUMP_COUNT_N(NEXT_FLUSH_TRI, {
490 DebuggerAppendMsg("Pause after Line3D: v%d(%f,%f,%f), v%d(%f,%f,%f), Width=%d\n", dwV0, m_line3DVtx[0].x, m_line3DVtx[0].y, m_line3DVtx[0].z,
491 dwV1, m_line3DVtx[1].x, m_line3DVtx[1].y, m_line3DVtx[1].z, dwWidth);
494 DEBUGGER_PAUSE_AND_DUMP_COUNT_N(NEXT_OBJ_TXT_CMD, {
495 DebuggerAppendMsg("Pause after Line3D: v%d(%f,%f,%f), v%d(%f,%f,%f), Width=%d\n", dwV0, m_line3DVtx[0].x, m_line3DVtx[0].y, m_line3DVtx[0].z,
496 dwV1, m_line3DVtx[1].x, m_line3DVtx[1].y, m_line3DVtx[1].z, dwWidth);
502 bool CRender::RemapTextureCoordinate
503 (float t0, float t1, uint32 tileWidth, uint32 mask, float textureWidth, float &u0, float &u1)
507 int width = mask>0 ? (1<<mask) : tileWidth;
508 if( width == 0 ) return false;
510 int divs0 = s0/width; if( divs0*width > s0 ) divs0--;
511 int divs1 = s1/width; if( divs1*width > s1 ) divs1--;
521 u0 = s0/textureWidth;
522 u1 = s1/textureWidth;
526 else if( divs0+1 == divs1 && s0%width==0 && s1%width == 0 )
529 u1 = tileWidth/textureWidth;
532 else if( divs0 == divs1+1 && s0%width==0 && s1%width == 0 )
535 u0 = tileWidth/textureWidth;
543 // u0 = s0/textureWidth;
548 // u1 = s1/textureWidth;
555 bool CRender::TexRect(int nX0, int nY0, int nX1, int nY1, float fS0, float fT0, float fScaleS, float fScaleT, bool colorFlag, uint32 diffuseColor)
557 if( options.enableHackForGames == HACK_FOR_DUKE_NUKEM )
563 if( status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_AT_1ST_PRIMITIVE )
565 status.bVIOriginIsUpdated=false;
566 CGraphicsContext::Get()->UpdateFrame();
567 DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG,{DebuggerAppendMsg("Screen Update at 1st textRect");});
570 if( options.enableHackForGames == HACK_FOR_BANJO_TOOIE )
572 // Hack for Banjo shadow in Banjo Tooie
573 if( g_TI.dwWidth == g_CI.dwWidth && g_TI.dwFormat == TXT_FMT_CI && g_TI.dwSize == TXT_SIZE_8b )
575 if( nX0 == fS0 && nY0 == fT0 )//&& nX0 > 90 && nY0 > 130 && nX1-nX0 > 80 && nY1-nY0 > 20 )
577 // Skip the text rect
583 if( status.bN64IsDrawingTextureBuffer )
585 if( frameBufferOptions.bIgnore || ( frameBufferOptions.bIgnoreRenderTextureIfHeightUnknown && newRenderTextureInfo.knownHeight == 0 ) )
593 if( status.bHandleN64RenderTexture && g_pRenderTextureInfo->CI_Info.dwSize == TXT_SIZE_8b )
598 if( !IsTextureEnabled() && gRDP.otherMode.cycle_type != CYCLE_TYPE_COPY )
600 FillRect(nX0, nY0, nX1, nY1, gRDP.primitiveColor);
605 if( IsUsedAsDI(g_CI.dwAddr) && !status.bHandleN64RenderTexture )
607 status.bFrameBufferIsDrawn = true;
610 if( status.bHandleN64RenderTexture && !status.bDirectWriteIntoRDRAM ) status.bFrameBufferIsDrawn = true;
612 LOG_UCODE("TexRect: X0=%d, Y0=%d, X1=%d, Y1=%d,\n\t\tfS0=%f, fT0=%f, ScaleS=%f, ScaleT=%f ",
613 nX0, nY0, nX1, nY1, fS0, fT0, fScaleS, fScaleT);
615 if( options.bEnableHacks )
622 else if( gRDP.otherMode.cycle_type == CYCLE_TYPE_1 && fScaleS == 1 && fScaleT == 1 &&
623 (int)g_textures[gRSP.curTile].m_dwTileWidth == nX1-nX0+1 && (int)g_textures[gRSP.curTile].m_dwTileHeight == nY1-nY0+1 &&
624 g_textures[gRSP.curTile].m_dwTileWidth%2 == 0 && g_textures[gRSP.curTile].m_dwTileHeight%2 == 0 )
629 else if( g_curRomInfo.bIncTexRectEdge )
637 // Scale to Actual texture coords
638 // The two cases are to handle the oversized textures hack on voodoos
640 SetCombinerAndBlender();
643 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY || !gRDP.otherMode.z_cmp )
645 ZBufferEnable(FALSE);
648 BOOL accurate = currentRomOptions.bAccurateTextureMapping;
650 RenderTexture &tex0 = g_textures[gRSP.curTile];
651 Tile &tile0 = gRDP.tiles[gRSP.curTile];
653 float widthDiv = tex0.m_fTexWidth;
654 float heightDiv = tex0.m_fTexHeight;
656 if( options.enableHackForGames == HACK_FOR_ALL_STAR_BASEBALL || options.enableHackForGames == HACK_FOR_MLB )
658 t0u0 = (fS0 -tile0.fhilite_sl);
662 t0u0 = (fS0 * tile0.fShiftScaleS -tile0.fhilite_sl);
666 if( accurate && gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY )
668 t0u1 = t0u0 + (fScaleS * (nX1 - nX0 - 1))*tile0.fShiftScaleS;
672 t0u1 = t0u0 + (fScaleS * (nX1 - nX0))*tile0.fShiftScaleS;
676 if( status.UseLargerTile[0] )
678 m_texRectTex1UV[0].u = (t0u0+status.LargerTileRealLeft[0])/widthDiv;
679 m_texRectTex1UV[1].u = (t0u1+status.LargerTileRealLeft[0])/widthDiv;
683 m_texRectTex1UV[0].u = t0u0/widthDiv;
684 m_texRectTex1UV[1].u = t0u1/widthDiv;
685 if( accurate && !tile0.bMirrorS && RemapTextureCoordinate(t0u0, t0u1, tex0.m_dwTileWidth, tile0.dwMaskS, widthDiv, m_texRectTex1UV[0].u, m_texRectTex1UV[1].u) )
686 SetTextureUFlag(TEXTURE_UV_FLAG_CLAMP, gRSP.curTile);
690 if( options.enableHackForGames == HACK_FOR_ALL_STAR_BASEBALL || options.enableHackForGames == HACK_FOR_MLB )
692 t0v0 = (fT0 -tile0.fhilite_tl);
696 t0v0 = (fT0 * tile0.fShiftScaleT -tile0.fhilite_tl);
700 if ( accurate && gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY)
702 t0v1 = t0v0 + (fScaleT * (nY1 - nY0-1))*tile0.fShiftScaleT;
706 t0v1 = t0v0 + (fScaleT * (nY1 - nY0))*tile0.fShiftScaleT;
709 m_texRectTex1UV[0].v = t0v0/heightDiv;
710 m_texRectTex1UV[1].v = t0v1/heightDiv;
711 if( accurate && !tile0.bMirrorT && RemapTextureCoordinate(t0v0, t0v1, tex0.m_dwTileHeight, tile0.dwMaskT, heightDiv, m_texRectTex1UV[0].v, m_texRectTex1UV[1].v) )
712 SetTextureVFlag(TEXTURE_UV_FLAG_CLAMP, gRSP.curTile);
714 COLOR speColor = PostProcessSpecularColor();
717 difColor = PostProcessDiffuseColor(diffuseColor);
719 //difColor = PostProcessDiffuseColor(0);
720 difColor = PostProcessDiffuseColor(gRDP.primitiveColor);
722 g_texRectTVtx[0].x = ViewPortTranslatei_x(nX0);
723 g_texRectTVtx[0].y = ViewPortTranslatei_y(nY0);
724 g_texRectTVtx[0].dcDiffuse = difColor;
725 g_texRectTVtx[0].dcSpecular = speColor;
727 g_texRectTVtx[1].x = ViewPortTranslatei_x(nX1);
728 g_texRectTVtx[1].y = ViewPortTranslatei_y(nY0);
729 g_texRectTVtx[1].dcDiffuse = difColor;
730 g_texRectTVtx[1].dcSpecular = speColor;
732 g_texRectTVtx[2].x = ViewPortTranslatei_x(nX1);
733 g_texRectTVtx[2].y = ViewPortTranslatei_y(nY1);
734 g_texRectTVtx[2].dcDiffuse = difColor;
735 g_texRectTVtx[2].dcSpecular = speColor;
737 g_texRectTVtx[3].x = ViewPortTranslatei_x(nX0);
738 g_texRectTVtx[3].y = ViewPortTranslatei_y(nY1);
739 g_texRectTVtx[3].dcDiffuse = difColor;
740 g_texRectTVtx[3].dcSpecular = speColor;
742 float depth = (gRDP.otherMode.depth_source == 1 ? gRDP.fPrimitiveDepth : 0 );
744 g_texRectTVtx[0].z = g_texRectTVtx[1].z = g_texRectTVtx[2].z = g_texRectTVtx[3].z = depth;
745 g_texRectTVtx[0].rhw = g_texRectTVtx[1].rhw = g_texRectTVtx[2].rhw = g_texRectTVtx[3].rhw = 1;
747 if( IsTexel1Enable() )
749 RenderTexture &tex1 = g_textures[(gRSP.curTile+1)&7];
750 Tile &tile1 = gRDP.tiles[(gRSP.curTile+1)&7];
752 widthDiv = tex1.m_fTexWidth;
753 heightDiv = tex1.m_fTexHeight;
754 //if( tile1.dwMaskS == 0 ) widthDiv = tile1.dwWidth;
755 //if( tile1.dwMaskT == 0 ) heightDiv = tile1.dwHeight;
757 float t0u0 = fS0 * tile1.fShiftScaleS -tile1.fhilite_sl;
758 float t0v0 = fT0 * tile1.fShiftScaleT -tile1.fhilite_tl;
761 if ( accurate && gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY)
763 t0u1 = t0u0 + (fScaleS * (nX1 - nX0 - 1))*tile1.fShiftScaleS;
764 t0v1 = t0v0 + (fScaleT * (nY1 - nY0 - 1))*tile1.fShiftScaleT;
768 t0u1 = t0u0 + (fScaleS * (nX1 - nX0))*tile1.fShiftScaleS;
769 t0v1 = t0v0 + (fScaleT * (nY1 - nY0))*tile1.fShiftScaleT;
772 if( status.UseLargerTile[1] )
774 m_texRectTex2UV[0].u = (t0u0+status.LargerTileRealLeft[1])/widthDiv;
775 m_texRectTex2UV[1].u = (t0u1+status.LargerTileRealLeft[1])/widthDiv;
779 m_texRectTex2UV[0].u = t0u0/widthDiv;
780 m_texRectTex2UV[1].u = t0u1/widthDiv;
781 if( accurate && !tile1.bMirrorS && RemapTextureCoordinate(t0u0, t0u1, tex1.m_dwTileWidth, tile1.dwMaskS, widthDiv, m_texRectTex2UV[0].u, m_texRectTex2UV[1].u) )
782 SetTextureUFlag(TEXTURE_UV_FLAG_CLAMP, (gRSP.curTile+1)&7);
785 m_texRectTex2UV[0].v = t0v0/heightDiv;
786 m_texRectTex2UV[1].v = t0v1/heightDiv;
788 if( accurate && !tile1.bMirrorT && RemapTextureCoordinate(t0v0, t0v1, tex1.m_dwTileHeight, tile1.dwMaskT, heightDiv, m_texRectTex2UV[0].v, m_texRectTex2UV[1].v) )
789 SetTextureVFlag(TEXTURE_UV_FLAG_CLAMP, (gRSP.curTile+1)&7);
791 SetVertexTextureUVCoord(g_texRectTVtx[0], m_texRectTex1UV[0].u, m_texRectTex1UV[0].v, m_texRectTex2UV[0].u, m_texRectTex2UV[0].v);
792 SetVertexTextureUVCoord(g_texRectTVtx[1], m_texRectTex1UV[1].u, m_texRectTex1UV[0].v, m_texRectTex2UV[1].u, m_texRectTex2UV[0].v);
793 SetVertexTextureUVCoord(g_texRectTVtx[2], m_texRectTex1UV[1].u, m_texRectTex1UV[1].v, m_texRectTex2UV[1].u, m_texRectTex2UV[1].v);
794 SetVertexTextureUVCoord(g_texRectTVtx[3], m_texRectTex1UV[0].u, m_texRectTex1UV[1].v, m_texRectTex2UV[0].u, m_texRectTex2UV[1].v);
798 SetVertexTextureUVCoord(g_texRectTVtx[0], m_texRectTex1UV[0].u, m_texRectTex1UV[0].v);
799 SetVertexTextureUVCoord(g_texRectTVtx[1], m_texRectTex1UV[1].u, m_texRectTex1UV[0].v);
800 SetVertexTextureUVCoord(g_texRectTVtx[2], m_texRectTex1UV[1].u, m_texRectTex1UV[1].v);
801 SetVertexTextureUVCoord(g_texRectTVtx[3], m_texRectTex1UV[0].u, m_texRectTex1UV[1].v);
807 if( TileUFlags[gRSP.curTile]==TEXTURE_UV_FLAG_CLAMP && TileVFlags[gRSP.curTile]==TEXTURE_UV_FLAG_CLAMP && options.forceTextureFilter == FORCE_DEFAULT_FILTER )
809 TextureFilter dwFilter = m_dwMagFilter;
810 m_dwMagFilter = m_dwMinFilter = FILTER_LINEAR;
811 ApplyTextureFilter();
813 res = RenderTexRect();
814 m_dwMagFilter = m_dwMinFilter = dwFilter;
815 ApplyTextureFilter();
817 else if( fScaleS >= 1 && fScaleT >= 1 && options.forceTextureFilter == FORCE_DEFAULT_FILTER )
819 TextureFilter dwFilter = m_dwMagFilter;
820 m_dwMagFilter = m_dwMinFilter = FILTER_POINT;
821 ApplyTextureFilter();
823 res = RenderTexRect();
824 m_dwMagFilter = m_dwMinFilter = dwFilter;
825 ApplyTextureFilter();
830 res = RenderTexRect();
832 TurnFogOnOff(gRSP.bFogEnabled);
834 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY || !gRDP.otherMode.z_cmp )
836 ZBufferEnable(gRSP.bZBufferEnabled);
839 DEBUGGER_PAUSE_AT_COND_AND_DUMP_COUNT_N((eventToPause == NEXT_FLUSH_TRI || eventToPause == NEXT_TEXTRECT), {
840 DebuggerAppendMsg("TexRect: tile=%d, X0=%d, Y0=%d, X1=%d, Y1=%d,\nfS0=%f, fT0=%f, ScaleS=%f, ScaleT=%f\n",
841 gRSP.curTile, nX0, nY0, nX1, nY1, fS0, fT0, fScaleS, fScaleT);
842 DebuggerAppendMsg(" : x0=%f, y0=%f, x1=%f, y1=%f\n", g_texRectTVtx[0].x, g_texRectTVtx[0].y, g_texRectTVtx[2].x, g_texRectTVtx[2].y);
843 DebuggerAppendMsg(" Tex0: u0=%f, v0=%f, u1=%f, v1=%f\n", m_texRectTex1UV[0].u, m_texRectTex1UV[0].v, m_texRectTex1UV[1].u, m_texRectTex1UV[1].v);
844 if( IsTexel1Enable() )
846 DebuggerAppendMsg(" Tex1: u0=%f, v0=%f, u1=%f, v1=%f\n", m_texRectTex2UV[0].u, m_texRectTex2UV[0].v, m_texRectTex2UV[1].u, m_texRectTex2UV[1].v);
848 DebuggerAppendMsg("color=%08X, %08X\n", g_texRectTVtx[0].dcDiffuse, g_texRectTVtx[0].dcSpecular);
849 DebuggerAppendMsg("Pause after TexRect\n");
850 if( logCombiners ) m_pColorCombiner->DisplayMuxString();
857 bool CRender::TexRectFlip(int nX0, int nY0, int nX1, int nY1, float fS0, float fT0, float fS1, float fT1)
859 LOG_UCODE("TexRectFlip: X0=%d, Y0=%d, X1=%d, Y1=%d,\n\t\tfS0=%f, fT0=%f, fS1=%f, fT1=%f ",
860 nX0, nY0, nX1, nY1, fS0, fT0, fS1, fT1);
862 if( status.bHandleN64RenderTexture && !status.bDirectWriteIntoRDRAM )
864 status.bFrameBufferIsDrawn = true;
865 status.bFrameBufferDrawnByTriangles = true;
870 // Save ZBuffer state
871 m_savedZBufferFlag = gRSP.bZBufferEnabled;
872 if( gRDP.otherMode.depth_source == 0 ) ZBufferEnable( FALSE );
874 float widthDiv = g_textures[gRSP.curTile].m_fTexWidth;
875 float heightDiv = g_textures[gRSP.curTile].m_fTexHeight;
877 //Tile &tile0 = gRDP.tiles[gRSP.curTile];
879 float t0u0 = fS0 / widthDiv;
880 float t0v0 = fT0 / heightDiv;
882 float t0u1 = (fS1 - fS0)/ widthDiv + t0u0;
883 float t0v1 = (fT1 - fT0)/ heightDiv + t0v0;
885 float depth = (gRDP.otherMode.depth_source == 1 ? gRDP.fPrimitiveDepth : 0 );
887 if( t0u0 >= 0 && t0u1 <= 1 && t0u1 >= t0u0 ) SetTextureUFlag(TEXTURE_UV_FLAG_CLAMP, gRSP.curTile);
888 if( t0v0 >= 0 && t0v1 <= 1 && t0v1 >= t0v0 ) SetTextureVFlag(TEXTURE_UV_FLAG_CLAMP, gRSP.curTile);
890 SetCombinerAndBlender();
892 COLOR speColor = PostProcessSpecularColor();
893 COLOR difColor = PostProcessDiffuseColor(gRDP.primitiveColor);
895 // Same as TexRect, but with texcoords 0,2 swapped
896 g_texRectTVtx[0].x = ViewPortTranslatei_x(nX0);
897 g_texRectTVtx[0].y = ViewPortTranslatei_y(nY0);
898 g_texRectTVtx[0].dcDiffuse = difColor;
899 g_texRectTVtx[0].dcSpecular = speColor;
901 g_texRectTVtx[1].x = ViewPortTranslatei_x(nX1);
902 g_texRectTVtx[1].y = ViewPortTranslatei_y(nY0);
903 g_texRectTVtx[1].dcDiffuse = difColor;
904 g_texRectTVtx[1].dcSpecular = speColor;
906 g_texRectTVtx[2].x = ViewPortTranslatei_x(nX1);
907 g_texRectTVtx[2].y = ViewPortTranslatei_y(nY1);
908 g_texRectTVtx[2].dcDiffuse = difColor;
909 g_texRectTVtx[2].dcSpecular = speColor;
911 g_texRectTVtx[3].x = ViewPortTranslatei_x(nX0);
912 g_texRectTVtx[3].y = ViewPortTranslatei_y(nY1);
913 g_texRectTVtx[3].dcDiffuse = difColor;
914 g_texRectTVtx[3].dcSpecular = speColor;
916 g_texRectTVtx[0].z = g_texRectTVtx[1].z = g_texRectTVtx[2].z = g_texRectTVtx[3].z = depth;
917 g_texRectTVtx[0].rhw = g_texRectTVtx[1].rhw = g_texRectTVtx[2].rhw = g_texRectTVtx[3].rhw = 1.0f;
919 SetVertexTextureUVCoord(g_texRectTVtx[0], t0u0, t0v0);
920 SetVertexTextureUVCoord(g_texRectTVtx[1], t0u0, t0v1);
921 SetVertexTextureUVCoord(g_texRectTVtx[2], t0u1, t0v1);
922 SetVertexTextureUVCoord(g_texRectTVtx[3], t0u1, t0v0);
926 bool res = RenderTexRect();
928 TurnFogOnOff(gRSP.bFogEnabled);
931 ZBufferEnable( m_savedZBufferFlag );
933 DEBUGGER_PAUSE_AT_COND_AND_DUMP_COUNT_N((eventToPause == NEXT_FLUSH_TRI || eventToPause == NEXT_TEXTRECT), {
934 DebuggerAppendMsg("TexRectFlip: tile=%d, X0=%d, Y0=%d, X1=%d, Y1=%d,\nfS0=%f, fT0=%f, nfS1=%f, fT1=%f\n",
935 gRSP.curTile, nX0, nY0, nX1, nY1, fS0, fT0, fS1, fT1);
936 DebuggerAppendMsg(" : x0=%f, y0=%f, x1=%f, y1=%f\n", g_texRectTVtx[0].x, g_texRectTVtx[0].y, g_texRectTVtx[2].x, g_texRectTVtx[2].y);
937 DebuggerAppendMsg(" Tex0: u0=%f, v0=%f, u1=%f, v1=%f\n", g_texRectTVtx[0].tcord[0].u, g_texRectTVtx[0].tcord[0].v, g_texRectTVtx[2].tcord[0].u, g_texRectTVtx[2].tcord[0].v);
938 TRACE0("Pause after TexRectFlip\n");
939 if( logCombiners ) m_pColorCombiner->DisplayMuxString();
946 void CRender::StartDrawSimple2DTexture(float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, COLOR dif, COLOR spe, float z, float rhw)
948 g_texRectTVtx[0].x = ViewPortTranslatei_x(x0); // << Error here, shouldn't divid by 4
949 g_texRectTVtx[0].y = ViewPortTranslatei_y(y0);
950 g_texRectTVtx[0].dcDiffuse = dif;
951 g_texRectTVtx[0].dcSpecular = spe;
952 g_texRectTVtx[0].tcord[0].u = u0;
953 g_texRectTVtx[0].tcord[0].v = v0;
956 g_texRectTVtx[1].x = ViewPortTranslatei_x(x1);
957 g_texRectTVtx[1].y = ViewPortTranslatei_y(y0);
958 g_texRectTVtx[1].dcDiffuse = dif;
959 g_texRectTVtx[1].dcSpecular = spe;
960 g_texRectTVtx[1].tcord[0].u = u1;
961 g_texRectTVtx[1].tcord[0].v = v0;
963 g_texRectTVtx[2].x = ViewPortTranslatei_x(x1);
964 g_texRectTVtx[2].y = ViewPortTranslatei_y(y1);
965 g_texRectTVtx[2].dcDiffuse = dif;
966 g_texRectTVtx[2].dcSpecular = spe;
967 g_texRectTVtx[2].tcord[0].u = u1;
968 g_texRectTVtx[2].tcord[0].v = v1;
970 g_texRectTVtx[3].x = ViewPortTranslatei_x(x0);
971 g_texRectTVtx[3].y = ViewPortTranslatei_y(y1);
972 g_texRectTVtx[3].dcDiffuse = dif;
973 g_texRectTVtx[3].dcSpecular = spe;
974 g_texRectTVtx[3].tcord[0].u = u0;
975 g_texRectTVtx[3].tcord[0].v = v1;
977 RenderTexture &txtr = g_textures[0];
978 if( txtr.pTextureEntry && txtr.pTextureEntry->txtrBufIdx > 0 )
980 RenderTextureInfo &info = gRenderTextureInfos[txtr.pTextureEntry->txtrBufIdx-1];
981 g_texRectTVtx[0].tcord[0].u *= info.scaleX;
982 g_texRectTVtx[0].tcord[0].v *= info.scaleY;
983 g_texRectTVtx[1].tcord[0].u *= info.scaleX;
984 g_texRectTVtx[1].tcord[0].v *= info.scaleY;
985 g_texRectTVtx[2].tcord[0].u *= info.scaleX;
986 g_texRectTVtx[2].tcord[0].v *= info.scaleY;
987 g_texRectTVtx[3].tcord[0].u *= info.scaleX;
988 g_texRectTVtx[3].tcord[0].v *= info.scaleY;
991 g_texRectTVtx[0].z = g_texRectTVtx[1].z = g_texRectTVtx[2].z = g_texRectTVtx[3].z = z;
992 g_texRectTVtx[0].rhw = g_texRectTVtx[1].rhw = g_texRectTVtx[2].rhw = g_texRectTVtx[3].rhw = rhw;
995 void CRender::StartDrawSimpleRect(int nX0, int nY0, int nX1, int nY1, uint32 dwColor, float depth, float rhw)
997 m_simpleRectVtx[0].x = ViewPortTranslatei_x(nX0);
998 m_simpleRectVtx[1].x = ViewPortTranslatei_x(nX1);
999 m_simpleRectVtx[0].y = ViewPortTranslatei_y(nY0);
1000 m_simpleRectVtx[1].y = ViewPortTranslatei_y(nY1);
1003 void CRender::SetAddressUAllStages(uint32 dwTile, TextureUVFlag dwFlag)
1007 void CRender::SetAddressVAllStages(uint32 dwTile, TextureUVFlag dwFlag)
1011 void CRender::SetAllTexelRepeatFlag()
1013 if( IsTextureEnabled() )
1015 if( IsTexel0Enable() || gRDP.otherMode.cycle_type == CYCLE_TYPE_COPY )
1016 SetTexelRepeatFlags(gRSP.curTile);
1017 if( IsTexel1Enable() )
1018 SetTexelRepeatFlags((gRSP.curTile+1)&7);
1023 void CRender::SetTexelRepeatFlags(uint32 dwTile)
1025 Tile &tile = gRDP.tiles[dwTile];
1027 if( tile.bForceClampS )
1028 SetTextureUFlag(TEXTURE_UV_FLAG_CLAMP, dwTile);
1029 else if( tile.bForceWrapS )
1030 SetTextureUFlag(TEXTURE_UV_FLAG_WRAP, dwTile);
1031 else if( tile.dwMaskS == 0 || tile.bClampS )
1033 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY )
1034 SetTextureUFlag(TEXTURE_UV_FLAG_WRAP, dwTile); // Can not clamp in COPY/FILL mode
1036 SetTextureUFlag(TEXTURE_UV_FLAG_CLAMP, dwTile);
1038 else if (tile.bMirrorS )
1039 SetTextureUFlag(TEXTURE_UV_FLAG_MIRROR, dwTile);
1041 SetTextureUFlag(TEXTURE_UV_FLAG_WRAP, dwTile);
1043 if( tile.bForceClampT )
1044 SetTextureVFlag(TEXTURE_UV_FLAG_CLAMP, dwTile);
1045 else if( tile.bForceWrapT )
1046 SetTextureVFlag(TEXTURE_UV_FLAG_WRAP, dwTile);
1047 else if( tile.dwMaskT == 0 || tile.bClampT)
1049 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY )
1050 SetTextureVFlag(TEXTURE_UV_FLAG_WRAP, dwTile); // Can not clamp in COPY/FILL mode
1052 SetTextureVFlag(TEXTURE_UV_FLAG_CLAMP, dwTile);
1054 else if (tile.bMirrorT )
1055 SetTextureVFlag(TEXTURE_UV_FLAG_MIRROR, dwTile);
1057 SetTextureVFlag(TEXTURE_UV_FLAG_WRAP, dwTile);
1060 void CRender::Initialize(void)
1062 ClearDeviceObjects();
1063 InitDeviceObjects();
1066 void CRender::CleanUp(void)
1068 m_pColorCombiner->CleanUp();
1069 ClearDeviceObjects();
1072 void myVec3Transform(float *vecout, float *vecin, float* m)
1074 float w = m[3]*vecin[0]+m[7]*vecin[1]+m[11]*vecin[2]+m[15];
1075 vecout[0] = (m[0]*vecin[0]+m[4]*vecin[1]+m[8]*vecin[2]+m[12])/w;
1076 vecout[1] = (m[1]*vecin[0]+m[5]*vecin[1]+m[9]*vecin[2]+m[13])/w;
1077 vecout[2] = (m[2]*vecin[0]+m[6]*vecin[1]+m[10]*vecin[2]+m[14])/w;
1080 void CRender::SetTextureEnableAndScale(int dwTile, bool bEnable, float fScaleX, float fScaleY)
1082 gRSP.bTextureEnabled = bEnable;
1086 if( gRSP.curTile != (unsigned int)dwTile )
1087 gRDP.textureIsChanged = true;
1089 gRSP.curTile = dwTile;
1091 gRSP.fTexScaleX = fScaleX;
1092 gRSP.fTexScaleY = fScaleY;
1094 if( fScaleX == 0 || fScaleY == 0 )
1096 gRSP.fTexScaleX = 1/32.0f;
1097 gRSP.fTexScaleY = 1/32.0f;
1102 void CRender::SetFogFlagForNegativeW()
1104 if( !gRSP.bFogEnabled ) return;
1106 m_bFogStateSave = gRSP.bFogEnabled;
1108 bool flag=gRSP.bFogEnabled;
1110 for (uint32 i = 0; i < gRSP.numVertices; i++)
1112 if( g_vtxBuffer[i].rhw < 0 )
1119 void CRender::RestoreFogFlag()
1121 if( !gRSP.bFogEnabled ) return;
1122 TurnFogOnOff(m_bFogStateSave);
1125 void CRender::SetViewport(int nLeft, int nTop, int nRight, int nBottom, int maxZ)
1127 if( status.bHandleN64RenderTexture )
1130 static float MultX=0, MultY=0;
1132 if( gRSP.nVPLeftN == nLeft && gRSP.nVPTopN == nTop &&
1133 gRSP.nVPRightN == nRight && gRSP.nVPBottomN == nBottom &&
1134 MultX==windowSetting.fMultX && MultY==windowSetting.fMultY)
1140 MultX=windowSetting.fMultX;
1141 MultY=windowSetting.fMultY;
1144 gRSP.nVPLeftN = nLeft;
1145 gRSP.nVPTopN = nTop;
1146 gRSP.nVPRightN = nRight;
1147 gRSP.nVPBottomN = nBottom;
1148 gRSP.nVPWidthN = nRight - nLeft + 1;
1149 gRSP.nVPHeightN = nBottom - nTop + 1;
1151 UpdateClipRectangle();
1152 SetViewportRender();
1154 LOG_UCODE("SetViewport (%d,%d - %d,%d)",gRSP.nVPLeftN, gRSP.nVPTopN, gRSP.nVPRightN, gRSP.nVPBottomN);
1157 extern bool bHalfTxtScale;
1159 bool CRender::DrawTriangles()
1161 if( !status.bCIBufferIsRendered ) g_pFrameBufferManager->ActiveTextureBuffer();
1163 DEBUGGER_ONLY_IF( (!debuggerEnableZBuffer), {ZBufferEnable( FALSE );} );
1165 if( status.bVIOriginIsUpdated == true && currentRomOptions.screenUpdateSetting==SCREEN_UPDATE_AT_1ST_PRIMITIVE )
1167 status.bVIOriginIsUpdated=false;
1168 CGraphicsContext::Get()->UpdateFrame();
1169 DEBUGGER_PAUSE_AND_DUMP_NO_UPDATE(NEXT_SET_CIMG,{DebuggerAppendMsg("Screen Update at 1st triangle");});
1172 // Hack for Pilotwings 64 (U) [!].v64
1173 static bool skipNext=false;
1174 if( options.enableHackForGames == HACK_FOR_PILOT_WINGS )
1176 if( IsUsedAsDI(g_CI.dwAddr) && gRDP.otherMode.z_cmp+gRDP.otherMode.z_upd > 0 )
1178 TRACE0("Warning: using Flushtris to write Zbuffer" );
1179 gRSP.numVertices = 0;
1180 gRSP.maxVertexID = 0;
1187 gRSP.numVertices = 0;
1188 gRSP.maxVertexID = 0;
1193 if( status.bN64IsDrawingTextureBuffer && frameBufferOptions.bIgnore )
1195 gRSP.numVertices = 0;
1196 gRSP.maxVertexID = 0;
1200 extern bool bConkerHideShadow;
1201 if( options.enableHackForGames == HACK_FOR_CONKER && bConkerHideShadow )
1203 gRSP.numVertices = 0;
1204 gRSP.maxVertexID = 0;
1208 if( IsUsedAsDI(g_CI.dwAddr) && !status.bHandleN64RenderTexture )
1210 status.bFrameBufferIsDrawn = true;
1214 if( status.bHandleN64RenderTexture && g_pRenderTextureInfo->CI_Info.dwSize == TXT_SIZE_8b )
1216 gRSP.numVertices = 0;
1217 gRSP.maxVertexID = 0;
1222 if (gRSP.numVertices == 0) return true;
1223 if( status.bHandleN64RenderTexture )
1225 g_pRenderTextureInfo->maxUsedHeight = g_pRenderTextureInfo->N64Height;
1226 if( !status.bDirectWriteIntoRDRAM )
1228 status.bFrameBufferIsDrawn = true;
1229 status.bFrameBufferDrawnByTriangles = true;
1233 if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
1235 TurnFogOnOff(false);
1238 for( int t=0; t<2; t++ )
1240 float halfscaleS = 1;
1242 // This will get rid of the thin black lines
1243 if( t==0 && !(m_pColorCombiner->m_bTex0Enabled) )
1247 if( ( gRDP.tiles[gRSP.curTile].dwSize == TXT_SIZE_32b && options.enableHackForGames == HACK_FOR_RUMBLE ) ||
1248 (bHalfTxtScale && g_curRomInfo.bTextureScaleHack ) ||
1249 (options.enableHackForGames == HACK_FOR_POLARISSNOCROSS && gRDP.tiles[7].dwFormat == TXT_FMT_CI && gRDP.tiles[7].dwSize == TXT_SIZE_8b
1250 && gRDP.tiles[0].dwFormat == TXT_FMT_CI && gRDP.tiles[0].dwSize == TXT_SIZE_8b && gRSP.curTile == 0 ))
1256 if( t==1 && !(m_pColorCombiner->m_bTex1Enabled) )
1259 if( halfscaleS < 1 )
1261 for( uint32 i=0; i<gRSP.numVertices; i++ )
1265 g_vtxBuffer[i].tcord[t].u += gRSP.tex0OffsetX;
1266 g_vtxBuffer[i].tcord[t].u /= 2;
1267 g_vtxBuffer[i].tcord[t].u -= gRSP.tex0OffsetX;
1268 g_vtxBuffer[i].tcord[t].v += gRSP.tex0OffsetY;
1269 g_vtxBuffer[i].tcord[t].v /= 2;
1270 g_vtxBuffer[i].tcord[t].v -= gRSP.tex0OffsetY;
1274 g_vtxBuffer[i].tcord[t].u += gRSP.tex1OffsetX;
1275 g_vtxBuffer[i].tcord[t].u /= 2;
1276 g_vtxBuffer[i].tcord[t].u -= gRSP.tex1OffsetX;
1277 g_vtxBuffer[i].tcord[t].v += gRSP.tex1OffsetY;
1278 g_vtxBuffer[i].tcord[t].v /= 2;
1279 g_vtxBuffer[i].tcord[t].v -= gRSP.tex1OffsetY;
1285 // The code here is disabled because it could cause incorrect texture repeating flag
1286 // for later DrawTriangles
1290 for( uint32 i=0; i<gRSP.numVertices; i++ )
1292 float w = CDeviceBuilder::GetGeneralDeviceType() == OGL_DEVICE ? g_vtxProjected5[i][3] : g_vtxBuffer[i].rhw;
1293 if( w < 0 || g_vtxBuffer[i].tcord[t].u > 1.0 || g_vtxBuffer[i].tcord[t].u < 0.0 )
1300 for( uint32 i=0; i<gRSP.numVertices; i++ )
1302 float w = CDeviceBuilder::GetGeneralDeviceType() == OGL_DEVICE ? g_vtxProjected5[i][3] : g_vtxBuffer[i].rhw;
1303 if( w < 0 || g_vtxBuffer[i].tcord[t].v > 1.0 || g_vtxBuffer[i].tcord[t].v < 0.0 )
1312 SetTextureUFlag(TEXTURE_UV_FLAG_CLAMP, gRSP.curTile+t);
1316 SetTextureVFlag(TEXTURE_UV_FLAG_CLAMP, gRSP.curTile+t);
1321 if( status.bHandleN64RenderTexture && g_pRenderTextureInfo->CI_Info.dwSize == TXT_SIZE_8b )
1323 ZBufferEnable(FALSE);
1326 ApplyScissorWithClipRatio();
1328 if( g_curRomInfo.bZHack )
1330 extern void HackZAll();
1334 bool res = RenderFlushTris();
1335 g_clippedVtxCount = 0;
1337 LOG_UCODE("DrawTriangles: Draw %d Triangles", gRSP.numVertices/3);
1339 gRSP.numVertices = 0; // Reset index
1340 gRSP.maxVertexID = 0;
1342 DEBUGGER_PAUSE_AND_DUMP_COUNT_N(NEXT_FLUSH_TRI, {
1343 TRACE0("Pause after DrawTriangles\n");
1344 if( logCombiners ) m_pColorCombiner->DisplayMuxString();
1347 if( !gRDP.bFogEnableInBlender && gRSP.bFogEnabled )
1355 inline int ReverseCITableLookup(uint32 *pTable, int size, uint32 val)
1357 for( int i=0; i<size; i++)
1359 if( pTable[i] == val )
1363 TRACE0("Cannot find value in CI table");
1367 bool SaveCITextureToFile(TxtrCacheEntry &entry, char *filename, bool bShow, bool bWholeTexture )
1369 if( !( (gRDP.otherMode.text_tlut>=2 || entry.ti.Format == TXT_FMT_CI || entry.ti.Format == TXT_FMT_RGBA) && entry.ti.Size <= TXT_SIZE_8b ) )
1374 if ( entry.ti.TLutFmt != TLUT_FMT_RGBA16 && entry.ti.TLutFmt != TLUT_FMT_IA16 )
1376 TRACE0("Invalid Tlut format");
1380 if( !entry.pTexture )
1382 TRACE0("Null texture");
1386 uint32 *pTable = NULL;
1388 uint16 * pPal = (uint16 *)entry.ti.PalAddress;
1390 // Create the pallette table
1391 if( entry.ti.Size == TXT_SIZE_4b )
1395 pTable = new uint32[16];
1397 for( int i=0; i<16; i++ )
1399 pTable[i] = entry.ti.TLutFmt == TLUT_FMT_RGBA16 ? Convert555ToRGBA(pPal[i^1]) : ConvertIA16ToRGBA(pPal[i^1]);
1406 pTable = new uint32[256];
1408 for( int i=0; i<256; i++ )
1410 pTable[i] = entry.ti.TLutFmt == TLUT_FMT_RGBA16 ? Convert555ToRGBA(pPal[i^1]) : ConvertIA16ToRGBA(pPal[i^1]);
1414 // Reversely convert current texture to indexed textures
1415 CTexture &texture = *entry.pTexture;
1416 //int width = bWholeTexture ? texture.m_dwCreatedTextureWidth : texture.m_dwWidth;
1417 //int height = bWholeTexture ? texture.m_dwCreatedTextureHeight : texture.m_dwHeight;
1418 int width = bWholeTexture ? texture.m_dwCreatedTextureWidth : entry.ti.WidthToLoad;
1419 int height = bWholeTexture ? texture.m_dwCreatedTextureHeight : entry.ti.HeightToLoad;
1420 int bufSizePerLine = (((((width << entry.ti.Size) + 1 ) >> 1)+3) >> 2)*4; // pad to 32bit boundary
1421 int bufSize = bufSizePerLine*height;
1422 unsigned char *pbuf = new unsigned char[bufSize];
1425 if( texture.StartUpdate(&srcInfo) )
1428 for( int i=height-1; i>=0; i--)
1430 uint32 *pSrc = (uint32*)((unsigned char*)srcInfo.lpSurface+srcInfo.lPitch * i);
1431 for( int j=0; j<width; j++)
1433 int val = ReverseCITableLookup(pTable, tableSize, *pSrc);
1436 if( entry.ti.Size == TXT_SIZE_4b )
1442 pbuf[idx>>1] = (pbuf[idx>>1]<<4) | val;
1448 pbuf[idx>>1] = (unsigned char)val;
1455 pbuf[idx++] = (unsigned char)val;
1458 if( entry.ti.Size == TXT_SIZE_4b )
1460 if( idx%8 ) idx = (idx/8+1)*8;
1464 if( idx%4 ) idx = (idx/4+1)*4;
1469 texture.EndUpdate(&srcInfo);
1472 // Create BMP color indexed file
1473 if( strcasecmp(right(filename,4),".bmp") != 0 )
1474 strcat(filename,".bmp");
1476 BITMAPFILEHEADER fileHeader;
1477 BITMAPINFOHEADER infoHeader;
1479 infoHeader.biSize = sizeof( BITMAPINFOHEADER );
1480 infoHeader.biWidth = width;
1481 infoHeader.biHeight = height;
1482 infoHeader.biPlanes = 1;
1483 infoHeader.biBitCount = entry.ti.Size == TXT_SIZE_4b ? 4 : 8;
1484 infoHeader.biCompression = BI_RGB;
1485 infoHeader.biSizeImage = bufSize;
1486 infoHeader.biXPelsPerMeter = 0;
1487 infoHeader.biYPelsPerMeter = 0;
1488 infoHeader.biClrUsed = 0;
1489 infoHeader.biClrImportant = 0;
1491 fileHeader.bfType = 19778;
1492 fileHeader.bfSize = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + infoHeader.biSizeImage + tableSize*4;
1493 fileHeader.bfReserved1 = fileHeader.bfReserved2 = 0;
1494 fileHeader.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + tableSize*4;
1498 f = fopen(filename, "wb");
1501 if (fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, f) != 1 ||
1502 fwrite(&infoHeader, sizeof(BITMAPINFOHEADER), 1, f) != 1 ||
1503 fwrite(pTable, tableSize*4, 1, f) != 1 ||
1504 fwrite(pbuf, infoHeader.biSizeImage, 1, f) != 1)
1505 printf("failed to write out texture data to image file '%s'", filename);
1512 TRACE1("Fail to create file %s", filename);
1522 void CRender::SaveTextureToFile(CTexture &texture, char *filename, TextureChannel channel, bool bShow, bool bWholeTexture, int width, int height)
1524 if( width < 0 || height < 0 )
1526 width = bWholeTexture ? texture.m_dwCreatedTextureWidth : texture.m_dwWidth;
1527 height = bWholeTexture ? texture.m_dwCreatedTextureHeight : texture.m_dwHeight;
1530 unsigned char *pbuf = new unsigned char[width*height* (channel == TXT_RGBA ? 4 : 3)];
1534 if( texture.StartUpdate(&srcInfo) )
1536 if( channel == TXT_RGBA )
1538 uint32 *pbuf2 = (uint32*)pbuf;
1539 for( int i=height-1; i>=0; i--)
1541 uint32 *pSrc = (uint32*)((unsigned char*)srcInfo.lpSurface+srcInfo.lPitch * i);
1542 for( int j=0; j<width; j++)
1548 if( SaveRGBABufferToPNGFile(filename, (unsigned char*)pbuf, width, height ) )
1549 //if( SaveRGBABufferToPNGFile(filename, (unsigned char*)srcInfo.lpSurface, width, height, srcInfo.lPitch ) )
1555 unsigned char *pbuf2 = pbuf;
1556 for( int i=height-1; i>=0; i--)
1558 unsigned char *pSrc = (unsigned char*)srcInfo.lpSurface+srcInfo.lPitch * i;
1559 for( int j=0; j<width; j++)
1561 if( channel == TXT_ALPHA )
1563 pbuf2[0] = pbuf2[1] = pbuf2[2] = pSrc[3];
1577 if( SaveRGBBufferToFile(filename, pbuf, width, height ) )
1581 texture.EndUpdate(&srcInfo);
1585 TRACE0("Cannot lock texture");
1591 TRACE0("Out of memory");
1597 bool CRender::DrawTexture(int tex, TextureChannel channel)
1599 if( g_textures[tex].m_pCTexture == NULL )
1601 TRACE0("Can't draw null texture");
1605 SaveTextureToFile(tex, channel, true); // Save to file instead of draw to screen
1606 DebuggerAppendMsg("Texture %d (CurTile:%d): W=%f, H=%f, Real W=%d, H=%d", tex, gRSP.curTile,
1607 g_textures[tex].m_fTexWidth, g_textures[tex].m_fTexHeight, g_textures[tex].m_dwTileWidth, g_textures[tex].m_dwTileHeight);
1608 DebuggerAppendMsg("X scale: %f, Y scale: %f, %s", gRSP.fTexScaleX, gRSP.fTexScaleY, gRSP.bTextureEnabled?"Enabled":"Disabled");
1613 void CRender::SaveTextureToFile(int tex, TextureChannel channel, bool bShow)
1615 TxtrCacheEntry &entry = *(g_textures[tex].pTextureEntry);
1617 CTexture *pBaseTexture = entry.pTexture;
1618 if( pBaseTexture == NULL )
1620 TRACE0("Can't dump null texture");
1623 CTexture *pEnhancedTexture = entry.pEnhancedTexture ? entry.pEnhancedTexture : entry.pTexture;
1625 bool bInWhole = false;
1626 if( entry.ti.HeightToCreate == entry.ti.HeightToLoad && entry.ti.WidthToCreate == entry.ti.WidthToLoad
1627 && pEnhancedTexture == pBaseTexture )
1631 if( (gRDP.otherMode.text_tlut>=2 || g_textures[tex].pTextureEntry->ti.Format == TXT_FMT_CI || g_textures[tex].pTextureEntry->ti.Format == TXT_FMT_RGBA) && g_textures[tex].pTextureEntry->ti.Size <= TXT_SIZE_8b )
1633 sprintf(filename, "\\%s#%08X#%d#%d_ci", g_curRomInfo.szGameName, g_textures[tex].pTextureEntry->dwCRC,
1634 g_textures[tex].pTextureEntry->ti.Format,
1635 g_textures[tex].pTextureEntry->ti.Size);
1636 SaveCITextureToFile(*(g_textures[tex].pTextureEntry), filename, bShow && bInWhole, false);
1638 sprintf(filename, "\\%s#%08X#%d#%d#%08X_ci", g_curRomInfo.szGameName, g_textures[tex].pTextureEntry->dwCRC,
1639 g_textures[tex].pTextureEntry->ti.Format,
1640 g_textures[tex].pTextureEntry->ti.Size, g_textures[tex].pTextureEntry->dwPalCRC);
1641 SaveCITextureToFile(*(g_textures[tex].pTextureEntry), filename, false, false);
1643 sprintf(filename, "\\%s#%08X#%d#%d#%08X_ciByRGBA", g_curRomInfo.szGameName, g_textures[tex].pTextureEntry->dwCRC,
1644 g_textures[tex].pTextureEntry->ti.Format,
1645 g_textures[tex].pTextureEntry->ti.Size, g_textures[tex].pTextureEntry->dwPalCRC);
1646 SaveTextureToFile(*pBaseTexture, filename, TXT_RGBA, false, false, g_textures[tex].pTextureEntry->ti.WidthToLoad, g_textures[tex].pTextureEntry->ti.HeightToLoad);
1648 DebuggerAppendMsg("Base texture is stored at: %s", filename);
1650 if( !bInWhole && bShow )
1652 sprintf(filename, "\\%s#%08X#%d#%d_ci_%s_debugger", g_curRomInfo.szGameName, g_textures[tex].pTextureEntry->dwCRC,
1653 g_textures[tex].pTextureEntry->ti.Format,
1654 g_textures[tex].pTextureEntry->ti.Size, channel == TXT_ALPHA ? "a" : channel == TXT_RGBA ? "all" : "rgb");
1655 SaveTextureToFile(*pEnhancedTexture, filename, channel, true, true);
1656 DebuggerAppendMsg("Whole texture is stored at: %s", filename);
1661 sprintf(filename, "\\%s#%08X#%d#%d_%s", g_curRomInfo.szGameName, g_textures[tex].pTextureEntry->dwCRC,
1662 g_textures[tex].pTextureEntry->ti.Format,
1663 g_textures[tex].pTextureEntry->ti.Size, channel == TXT_ALPHA ? "a" : channel == TXT_RGBA ? "all" : "rgb");
1664 SaveTextureToFile(*pBaseTexture, filename, channel, bShow && bInWhole, false,g_textures[tex].pTextureEntry->ti.WidthToLoad, g_textures[tex].pTextureEntry->ti.HeightToLoad);
1665 DebuggerAppendMsg("Base texture is stored at: %s", filename);
1667 if( !bInWhole && bShow )
1669 sprintf(filename, "\\%s#%08X#%d#%d_%s_debugger", g_curRomInfo.szGameName, g_textures[tex].pTextureEntry->dwCRC,
1670 g_textures[tex].pTextureEntry->ti.Format,
1671 g_textures[tex].pTextureEntry->ti.Size, channel == TXT_ALPHA ? "a" : channel == TXT_RGBA ? "all" : "rgb");
1672 SaveTextureToFile(*pEnhancedTexture, filename, channel, true, true);
1673 DebuggerAppendMsg("Whole texture is stored at: %s", filename);
1679 extern RenderTextureInfo gRenderTextureInfos[];
1680 void SetVertexTextureUVCoord(TexCord &dst, const TexCord &src, int tile, TxtrCacheEntry *pEntry)
1682 RenderTexture &txtr = g_textures[tile];
1683 RenderTextureInfo &info = gRenderTextureInfos[pEntry->txtrBufIdx-1];
1687 uint32 addrOffset = g_TI.dwAddr-info.CI_Info.dwAddr;
1688 uint32 extraTop = (addrOffset>>(info.CI_Info.dwSize-1)) /info.CI_Info.dwWidth;
1689 uint32 extraLeft = (addrOffset>>(info.CI_Info.dwSize-1))%info.CI_Info.dwWidth;
1691 if( pEntry->txtrBufIdx > 0 )
1693 // Loading from render_texture or back buffer
1694 s += (extraLeft+pEntry->ti.LeftToLoad)/txtr.m_fTexWidth;
1695 t += (extraTop+pEntry->ti.TopToLoad)/txtr.m_fTexHeight;
1705 void CRender::SetVertexTextureUVCoord(TLITVERTEX &v, const TexCord &fTex0)
1707 RenderTexture &txtr = g_textures[0];
1708 if( txtr.pTextureEntry && txtr.pTextureEntry->txtrBufIdx > 0 )
1710 ::SetVertexTextureUVCoord(v.tcord[0], fTex0, 0, txtr.pTextureEntry);
1718 void CRender::SetVertexTextureUVCoord(TLITVERTEX &v, float fTex0S, float fTex0T)
1720 TexCord t = { fTex0S, fTex0T };
1721 SetVertexTextureUVCoord(v, t);
1724 void CRender::SetVertexTextureUVCoord(TLITVERTEX &v, const TexCord &fTex0_, const TexCord &fTex1_)
1726 TexCord fTex0 = fTex0_;
1727 TexCord fTex1 = fTex1_;
1729 if( (options.enableHackForGames == HACK_FOR_ZELDA||options.enableHackForGames == HACK_FOR_ZELDA_MM) && m_Mux == 0x00262a60150c937fLL && gRSP.curTile == 0 )
1731 // Hack for Zelda Sun
1732 Tile &t0 = gRDP.tiles[0];
1733 Tile &t1 = gRDP.tiles[1];
1734 if( t0.dwFormat == TXT_FMT_I && t0.dwSize == TXT_SIZE_8b && t0.dwWidth == 64 &&
1735 t1.dwFormat == TXT_FMT_I && t1.dwSize == TXT_SIZE_8b && t1.dwWidth == 64 &&
1736 t0.dwHeight == t1.dwHeight )
1745 RenderTexture &txtr0 = g_textures[0];
1746 if( txtr0.pTextureEntry && txtr0.pTextureEntry->txtrBufIdx > 0 )
1748 ::SetVertexTextureUVCoord(v.tcord[0], fTex0, 0, txtr0.pTextureEntry);
1755 RenderTexture &txtr1 = g_textures[1];
1756 if( txtr1.pTextureEntry && txtr1.pTextureEntry->txtrBufIdx > 0 )
1758 ::SetVertexTextureUVCoord(v.tcord[1], fTex1, 1, txtr1.pTextureEntry);
1766 void CRender::SetVertexTextureUVCoord(TLITVERTEX &v, float fTex0S, float fTex0T, float fTex1S, float fTex1T)
1768 TexCord t0 = { fTex0S, fTex0T };
1769 TexCord t1 = { fTex1S, fTex1T };
1770 SetVertexTextureUVCoord(v, t0, t1);
1773 void CRender::SetClipRatio(uint32 type, uint32 w1)
1775 bool modified = false;
1778 case RSP_MV_WORD_OFFSET_CLIP_RNX:
1779 LOG_UCODE(" RSP_MOVE_WORD_CLIP NegX: %d", (int)(short)w1);
1780 if( gRSP.clip_ratio_negx != (short)w1 )
1782 gRSP.clip_ratio_negx = (short)w1;
1786 case RSP_MV_WORD_OFFSET_CLIP_RNY:
1787 LOG_UCODE(" RSP_MOVE_WORD_CLIP NegY: %d", (int)(short)w1);
1788 if( gRSP.clip_ratio_negy != (short)w1 )
1790 gRSP.clip_ratio_negy = (short)w1;
1794 case RSP_MV_WORD_OFFSET_CLIP_RPX:
1795 LOG_UCODE(" RSP_MOVE_WORD_CLIP PosX: %d", (int)(short)w1);
1796 if( gRSP.clip_ratio_posx != -(short)w1 )
1798 gRSP.clip_ratio_posx = -(short)w1;
1802 case RSP_MV_WORD_OFFSET_CLIP_RPY:
1803 LOG_UCODE(" RSP_MOVE_WORD_CLIP PosY: %d", (int)(short)w1);
1804 if( gRSP.clip_ratio_posy != -(short)w1 )
1806 gRSP.clip_ratio_posy = -(short)w1;
1814 UpdateClipRectangle();
1818 void CRender::UpdateClipRectangle()
1820 if( status.bHandleN64RenderTexture )
1822 //windowSetting.fMultX = windowSetting.fMultY = 1;
1823 windowSetting.vpLeftW = 0;
1824 windowSetting.vpTopW = 0;
1825 windowSetting.vpRightW = newRenderTextureInfo.bufferWidth;
1826 windowSetting.vpBottomW = newRenderTextureInfo.bufferHeight;
1827 windowSetting.vpWidthW = newRenderTextureInfo.bufferWidth;
1828 windowSetting.vpHeightW = newRenderTextureInfo.bufferHeight;
1830 gRSP.vtxXMul = windowSetting.vpWidthW/2.0f;
1831 gRSP.vtxXAdd = gRSP.vtxXMul + windowSetting.vpLeftW;
1832 gRSP.vtxYMul = -windowSetting.vpHeightW/2.0f;
1833 gRSP.vtxYAdd = windowSetting.vpHeightW/2.0f + windowSetting.vpTopW+windowSetting.toolbarHeightToUse;
1835 // Update clip rectangle by setting scissor
1837 int halfx = newRenderTextureInfo.bufferWidth/2;
1838 int halfy = newRenderTextureInfo.bufferHeight/2;
1839 int centerx = halfx;
1840 int centery = halfy;
1842 gRSP.clip_ratio_left = centerx - halfx * gRSP.clip_ratio_negx;
1843 gRSP.clip_ratio_top = centery - halfy * gRSP.clip_ratio_negy;
1844 gRSP.clip_ratio_right = centerx + halfx * gRSP.clip_ratio_posx;
1845 gRSP.clip_ratio_bottom = centery + halfy * gRSP.clip_ratio_posy;
1849 windowSetting.vpLeftW = int(gRSP.nVPLeftN * windowSetting.fMultX);
1850 windowSetting.vpTopW = int(gRSP.nVPTopN * windowSetting.fMultY);
1851 windowSetting.vpRightW = int(gRSP.nVPRightN* windowSetting.fMultX);
1852 windowSetting.vpBottomW = int(gRSP.nVPBottomN* windowSetting.fMultY);
1853 windowSetting.vpWidthW = int((gRSP.nVPRightN - gRSP.nVPLeftN + 1) * windowSetting.fMultX);
1854 windowSetting.vpHeightW = int((gRSP.nVPBottomN - gRSP.nVPTopN + 1) * windowSetting.fMultY);
1856 gRSP.vtxXMul = windowSetting.vpWidthW/2.0f;
1857 gRSP.vtxXAdd = gRSP.vtxXMul + windowSetting.vpLeftW;
1858 gRSP.vtxYMul = -windowSetting.vpHeightW/2.0f;
1859 gRSP.vtxYAdd = windowSetting.vpHeightW/2.0f + windowSetting.vpTopW+windowSetting.toolbarHeightToUse;
1861 // Update clip rectangle by setting scissor
1863 int halfx = gRSP.nVPWidthN/2;
1864 int halfy = gRSP.nVPHeightN/2;
1865 int centerx = gRSP.nVPLeftN+halfx;
1866 int centery = gRSP.nVPTopN+halfy;
1868 gRSP.clip_ratio_left = centerx - halfx * gRSP.clip_ratio_negx;
1869 gRSP.clip_ratio_top = centery - halfy * gRSP.clip_ratio_negy;
1870 gRSP.clip_ratio_right = centerx + halfx * gRSP.clip_ratio_posx;
1871 gRSP.clip_ratio_bottom = centery + halfy * gRSP.clip_ratio_posy;
1874 UpdateScissorWithClipRatio();
1877 void CRender::UpdateScissorWithClipRatio()
1879 gRSP.real_clip_scissor_left = std::max(gRDP.scissor.left, gRSP.clip_ratio_left);
1880 gRSP.real_clip_scissor_top = std::max(gRDP.scissor.top, gRSP.clip_ratio_top);
1881 gRSP.real_clip_scissor_right = std::min(gRDP.scissor.right,gRSP.clip_ratio_right);
1882 gRSP.real_clip_scissor_bottom = std::min(gRDP.scissor.bottom, gRSP.clip_ratio_bottom);
1884 gRSP.real_clip_scissor_left = std::max(gRSP.real_clip_scissor_left, 0);
1885 gRSP.real_clip_scissor_top = std::max(gRSP.real_clip_scissor_top, 0);
1886 gRSP.real_clip_scissor_right = std::min(gRSP.real_clip_scissor_right,windowSetting.uViWidth-1);
1887 gRSP.real_clip_scissor_bottom = std::min(gRSP.real_clip_scissor_bottom, windowSetting.uViHeight-1);
1889 WindowSettingStruct &w = windowSetting;
1890 w.clipping.left = (uint32)(gRSP.real_clip_scissor_left*windowSetting.fMultX);
1891 w.clipping.top = (uint32)(gRSP.real_clip_scissor_top*windowSetting.fMultY);
1892 w.clipping.bottom = (uint32)(gRSP.real_clip_scissor_bottom*windowSetting.fMultY);
1893 w.clipping.right = (uint32)(gRSP.real_clip_scissor_right*windowSetting.fMultX);
1894 if( w.clipping.left > 0 || w.clipping.top > 0 || w.clipping.right < (uint32)windowSetting.uDisplayWidth-1 ||
1895 w.clipping.bottom < (uint32)windowSetting.uDisplayHeight-1 )
1897 w.clipping.needToClip = true;
1901 w.clipping.needToClip = false;
1903 w.clipping.width = (uint32)((gRSP.real_clip_scissor_right-gRSP.real_clip_scissor_left+1)*windowSetting.fMultX);
1904 w.clipping.height = (uint32)((gRSP.real_clip_scissor_bottom-gRSP.real_clip_scissor_top+1)*windowSetting.fMultY);
1906 float halfx = gRSP.nVPWidthN/2.0f;
1907 float halfy = gRSP.nVPHeightN/2.0f;
1908 float centerx = gRSP.nVPLeftN+halfx;
1909 float centery = gRSP.nVPTopN+halfy;
1911 gRSP.real_clip_ratio_negx = (gRSP.real_clip_scissor_left - centerx)/halfx;
1912 gRSP.real_clip_ratio_negy = (gRSP.real_clip_scissor_top - centery)/halfy;
1913 gRSP.real_clip_ratio_posx = (gRSP.real_clip_scissor_right - centerx)/halfx;
1914 gRSP.real_clip_ratio_posy = (gRSP.real_clip_scissor_bottom - centery)/halfy;
1916 ApplyScissorWithClipRatio(true);
1920 // Set other modes not covered by color combiner or alpha blender
1921 void CRender::InitOtherModes(void)
1923 ApplyTextureFilter();
1926 // I can't think why the hand in mario's menu screen is rendered with an opaque rendermode,
1927 // and no alpha threshold. We set the alpha reference to 1 to ensure that the transparent pixels
1928 // don't get rendered. I hope this doesn't fuck anything else up.
1930 if ( gRDP.otherMode.alpha_compare == 0 )
1932 if ( gRDP.otherMode.cvg_x_alpha && (gRDP.otherMode.alpha_cvg_sel || gRDP.otherMode.aa_en ) )
1934 ForceAlphaRef(128); // Strange, I have to use value=2 for pixel shader combiner for Nvidia FX5200
1935 // for other video cards, value=1 is good enough.
1936 SetAlphaTestEnable(TRUE);
1940 SetAlphaTestEnable(FALSE);
1943 else if ( gRDP.otherMode.alpha_compare == 3 )
1945 //RDP_ALPHA_COMPARE_DITHER
1946 SetAlphaTestEnable(FALSE);
1950 if( (gRDP.otherMode.alpha_cvg_sel ) && !gRDP.otherMode.cvg_x_alpha )
1952 // Use CVG for pixel alpha
1953 SetAlphaTestEnable(FALSE);
1957 // RDP_ALPHA_COMPARE_THRESHOLD || RDP_ALPHA_COMPARE_DITHER
1961 ForceAlphaRef(m_dwAlpha);
1962 SetAlphaTestEnable(TRUE);
1966 if( options.enableHackForGames == HACK_FOR_SOUTH_PARK_RALLY && m_Mux == 0x00121824ff33ffffLL &&
1967 gRSP.bCullFront && gRDP.otherMode.aa_en && gRDP.otherMode.z_cmp && gRDP.otherMode.z_upd )
1973 if( gRDP.otherMode.cycle_type >= CYCLE_TYPE_COPY )
1975 // Disable zbuffer for COPY and FILL mode
1980 SetZCompare(gRDP.otherMode.z_cmp);
1981 SetZUpdate(gRDP.otherMode.z_upd);
1985 if( options.enableHackForGames == HACK_FOR_SOUTH_PARK_RALLY && m_Mux == 0x00121824ff33ffff &&
1986 gRSP.bCullFront && gRDP.otherMode.z_cmp && gRDP.otherMode.z_upd )//&& gRDP.otherMode.aa_en )
1995 void CRender::SetTextureFilter(uint32 dwFilter)
1997 if( options.forceTextureFilter == FORCE_DEFAULT_FILTER )
2001 case RDP_TFILTER_AVERAGE: //?
2002 case RDP_TFILTER_BILERP:
2003 m_dwMinFilter = m_dwMagFilter = FILTER_LINEAR;
2006 m_dwMinFilter = m_dwMagFilter = FILTER_POINT;
2012 switch( options.forceTextureFilter )
2014 case FORCE_POINT_FILTER:
2015 m_dwMinFilter = m_dwMagFilter = FILTER_POINT;
2017 case FORCE_LINEAR_FILTER:
2018 m_dwMinFilter = m_dwMagFilter = FILTER_LINEAR;
2023 ApplyTextureFilter();
2026 bool SaveRGBBufferToFile(char *filename, unsigned char *buf, int width, int height, int pitch)
2031 if( strcasecmp(right(filename,3),"bmp") == 0 )
2033 BITMAPFILEHEADER fileHeader;
2034 BITMAPINFOHEADER infoHeader;
2036 infoHeader.biSize = sizeof( BITMAPINFOHEADER );
2037 infoHeader.biWidth = width;
2038 infoHeader.biHeight = height;
2039 infoHeader.biPlanes = 1;
2040 infoHeader.biBitCount = 24;
2041 infoHeader.biCompression = BI_RGB;
2042 infoHeader.biSizeImage = width * height * 3;
2043 infoHeader.biXPelsPerMeter = 0;
2044 infoHeader.biYPelsPerMeter = 0;
2045 infoHeader.biClrUsed = 0;
2046 infoHeader.biClrImportant = 0;
2048 fileHeader.bfType = 19778;
2049 fileHeader.bfSize = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + infoHeader.biSizeImage;
2050 fileHeader.bfReserved1 = fileHeader.bfReserved2 = 0;
2051 fileHeader.bfOffBits = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER );
2055 f = fopen(filename, "wb");
2058 if (fwrite(&fileHeader, sizeof(BITMAPFILEHEADER), 1, f) != 1 ||
2059 fwrite(&infoHeader, sizeof(BITMAPINFOHEADER), 1, f) != 1 ||
2060 fwrite(buf, infoHeader.biSizeImage, 1, f) != 1)
2061 printf("failed to write out texture data to image file '%s'", filename);
2069 TRACE1("Fail to create file %s", filename);
2075 if( strcasecmp(right(filename,4),".png") != 0 ) strcat(filename,".png");
2077 struct BMGImageStruct img;
2078 memset(&img, 0, sizeof(BMGImageStruct));
2081 img.bits_per_pixel = 24;
2082 img.height = height;
2084 img.scan_width = pitch;
2085 BMG_Error code = WritePNG(filename, img);
2087 if( code == BMG_OK )
2094 bool SaveRGBABufferToPNGFile(char *filename, unsigned char *buf, int width, int height, int pitch)
2099 if( strcasecmp(right(filename,4),".png") != 0 ) strcat(filename,".png");
2101 struct BMGImageStruct img;
2102 memset(&img, 0, sizeof(BMGImageStruct));
2105 img.bits_per_pixel = 32;
2106 img.height = height;
2108 img.scan_width = pitch;
2109 BMG_Error code = WritePNG(filename, img);
2111 if( code == BMG_OK )