Merge pull request #2005 from CookiePLMonster/fix-vs-precision

Improve reverse screenspace transformation precision
This commit is contained in:
PatrickvL 2020-10-24 23:21:48 +02:00 committed by GitHub
commit 23a3bc4b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -261,9 +261,13 @@ float4 reverseScreenspaceTransform(float4 oPos)
// mad oPos.xyz, r12, r1.x, c-37 // mad oPos.xyz, r12, r1.x, c-37
// where c-37 and c-38 are reserved transform values // where c-37 and c-38 are reserved transform values
// oPos.w and xboxViewportScale.z might be VERY big when a D24 depth buffer is used
// and multiplying oPos.xyz by oPos.w may cause precision issues.
// Pre-divide them to help keep the values reasonably small.
// Test case: Burnout 3
float3 divisor = xboxViewportScale.xyz / oPos.w;
oPos.xyz -= xboxViewportOffset.xyz; // reverse offset oPos.xyz -= xboxViewportOffset.xyz; // reverse offset
oPos.xyz *= oPos.w; // reverse perspective divide oPos.xyz /= divisor; // reverse scale and perspective divide
oPos.xyz /= xboxViewportScale.xyz; // reverse scale
return oPos; return oPos;
} }

View File

@ -4032,11 +4032,10 @@ void GetViewPortOffsetAndScale(float (&vOffset)[4], float(&vScale)[4])
void UpdateViewPortOffsetAndScaleConstants() void UpdateViewPortOffsetAndScaleConstants()
{ {
float vOffset[4], vScale[4]; float vScaleOffset[2][4]; // 0 - scale 1 - offset
GetViewPortOffsetAndScale(vOffset, vScale); GetViewPortOffsetAndScale(vScaleOffset[1], vScaleOffset[0]);
g_pD3DDevice->SetVertexShaderConstantF(CXBX_D3DVS_VIEWPORT_SCALE_MIRROR, vScale, 1); g_pD3DDevice->SetVertexShaderConstantF(CXBX_D3DVS_VIEWPORT_SCALE_MIRROR, reinterpret_cast<float*>(vScaleOffset), 2);
g_pD3DDevice->SetVertexShaderConstantF(CXBX_D3DVS_VIEWPORT_OFFSET_MIRROR, vOffset, 1);
// Store viewport offset and scale in constant registers 58 (c-38) and // Store viewport offset and scale in constant registers 58 (c-38) and
// 59 (c-37) used for screen space transformation. // 59 (c-37) used for screen space transformation.
@ -4044,8 +4043,7 @@ void UpdateViewPortOffsetAndScaleConstants()
// Treat this as a flag // Treat this as a flag
// Test Case: GTA III, Soldier of Fortune II // Test Case: GTA III, Soldier of Fortune II
if (!(g_Xbox_VertexShaderConstantMode & X_D3DSCM_NORESERVEDCONSTANTS)) { if (!(g_Xbox_VertexShaderConstantMode & X_D3DSCM_NORESERVEDCONSTANTS)) {
g_pD3DDevice->SetVertexShaderConstantF(X_D3DSCM_RESERVED_CONSTANT_SCALE + X_D3DSCM_CORRECTION, vScale, 1); g_pD3DDevice->SetVertexShaderConstantF(X_D3DSCM_RESERVED_CONSTANT_SCALE + X_D3DSCM_CORRECTION, reinterpret_cast<float*>(vScaleOffset), 2);
g_pD3DDevice->SetVertexShaderConstantF(X_D3DSCM_RESERVED_CONSTANT_OFFSET + X_D3DSCM_CORRECTION, vOffset, 1);
} }
} }