diff --git a/GPU/Common/DrawEngineCommon.h b/GPU/Common/DrawEngineCommon.h index 4b62f48c14..8d6917fd6a 100644 --- a/GPU/Common/DrawEngineCommon.h +++ b/GPU/Common/DrawEngineCommon.h @@ -145,11 +145,28 @@ protected: return 1; } + inline void UpdateEverUsedEqualDepth(GEComparison comp) { + switch (comp) { + case GE_COMP_EQUAL: + case GE_COMP_NOTEQUAL: + case GE_COMP_LEQUAL: + case GE_COMP_GEQUAL: + everUsedEqualDepth_ = true; + break; + + default: + break; + } + } + bool useHWTransform_ = false; bool useHWTessellation_ = false; // Used to prevent unnecessary flushing in softgpu. bool flushOnParams_ = true; + // Set once a equal depth test is encountered. + bool everUsedEqualDepth_ = false; + // Vertex collector buffers u8 *decoded = nullptr; u16 *decIndex = nullptr; diff --git a/GPU/Common/GPUStateUtils.cpp b/GPU/Common/GPUStateUtils.cpp index c6fcfd7007..f168fa1920 100644 --- a/GPU/Common/GPUStateUtils.cpp +++ b/GPU/Common/GPUStateUtils.cpp @@ -136,6 +136,15 @@ bool IsColorTestTriviallyTrue() { } } +bool IsDepthTestEffectivelyDisabled() { + if (!gstate.isDepthTestEnabled()) + return true; + // We can ignore stencil, because ALWAYS and disabled choose the same stencil path. + if (gstate.getDepthTestFunction() != GE_COMP_ALWAYS) + return false; + return !gstate.isDepthWriteEnabled(); +} + const bool nonAlphaSrcFactors[16] = { true, // GE_SRCBLEND_DSTCOLOR, true, // GE_SRCBLEND_INVDSTCOLOR, diff --git a/GPU/Common/GPUStateUtils.h b/GPU/Common/GPUStateUtils.h index 038091f7d8..ab524f6c3f 100644 --- a/GPU/Common/GPUStateUtils.h +++ b/GPU/Common/GPUStateUtils.h @@ -56,6 +56,7 @@ bool IsColorTestAgainstZero(); bool IsColorTestTriviallyTrue(); bool IsAlphaTestAgainstZero(); bool NeedsTestDiscard(); +bool IsDepthTestEffectivelyDisabled(); bool IsStencilTestOutputDisabled(); StencilValueType ReplaceAlphaWithStencilType(); diff --git a/GPU/D3D11/DrawEngineD3D11.cpp b/GPU/D3D11/DrawEngineD3D11.cpp index e0c33e5d76..9ef139486f 100644 --- a/GPU/D3D11/DrawEngineD3D11.cpp +++ b/GPU/D3D11/DrawEngineD3D11.cpp @@ -643,6 +643,10 @@ rotateVBO: swTransform.BuildDrawingParams(prim, indexGen.VertexCount(), dec_->VertexType(), inds, maxIndex, &result); if (result.setSafeSize) framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight); + // Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values. + // Games sometimes expect exact matches (see #12626, for example) for equal comparisons. + if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f) + result.action = SW_DRAW_PRIMITIVES; ApplyDrawStateLate(result.setStencil, result.stencilValue); diff --git a/GPU/D3D11/GPU_D3D11.cpp b/GPU/D3D11/GPU_D3D11.cpp index d1c5edcce3..90fb480155 100644 --- a/GPU/D3D11/GPU_D3D11.cpp +++ b/GPU/D3D11/GPU_D3D11.cpp @@ -125,8 +125,7 @@ u32 GPU_D3D11::CheckGPUFeatures() const { if (!g_Config.bHighQualityDepth && (features & GPU_USE_ACCURATE_DEPTH) != 0) { features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; } else if (PSP_CoreParameter().compat.flags().PixelDepthRounding) { - // Use fragment rounding on desktop and GLES3, most accurate. - features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT; + features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; } else if (PSP_CoreParameter().compat.flags().VertexDepthRounding) { features |= GPU_ROUND_DEPTH_TO_16BIT; } diff --git a/GPU/D3D11/StateMappingD3D11.cpp b/GPU/D3D11/StateMappingD3D11.cpp index efbbcad31b..2174da2ce9 100644 --- a/GPU/D3D11/StateMappingD3D11.cpp +++ b/GPU/D3D11/StateMappingD3D11.cpp @@ -270,10 +270,11 @@ void DrawEngineD3D11::ApplyDrawState(int prim) { } else { keys_.depthStencil.value = 0; // Depth Test - if (gstate.isDepthTestEnabled()) { + if (!IsDepthTestEffectivelyDisabled()) { keys_.depthStencil.depthTestEnable = true; keys_.depthStencil.depthCompareOp = compareOps[gstate.getDepthTestFunction()]; keys_.depthStencil.depthWriteEnable = gstate.isDepthWriteEnabled(); + UpdateEverUsedEqualDepth(gstate.getDepthTestFunction()); } else { keys_.depthStencil.depthTestEnable = false; keys_.depthStencil.depthWriteEnable = false; diff --git a/GPU/Directx9/DrawEngineDX9.cpp b/GPU/Directx9/DrawEngineDX9.cpp index 33117be1c9..1e6bd62d61 100644 --- a/GPU/Directx9/DrawEngineDX9.cpp +++ b/GPU/Directx9/DrawEngineDX9.cpp @@ -611,6 +611,10 @@ rotateVBO: swTransform.BuildDrawingParams(prim, indexGen.VertexCount(), dec_->VertexType(), inds, maxIndex, &result); if (result.setSafeSize) framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight); + // Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values. + // Games sometimes expect exact matches (see #12626, for example) for equal comparisons. + if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f) + result.action = SW_DRAW_PRIMITIVES; ApplyDrawStateLate(); diff --git a/GPU/Directx9/StateMappingDX9.cpp b/GPU/Directx9/StateMappingDX9.cpp index 951a401189..4517150daf 100644 --- a/GPU/Directx9/StateMappingDX9.cpp +++ b/GPU/Directx9/StateMappingDX9.cpp @@ -228,10 +228,11 @@ void DrawEngineDX9::ApplyDrawState(int prim) { } } else { // Depth Test - if (gstate.isDepthTestEnabled()) { + if (!IsDepthTestEffectivelyDisabled()) { dxstate.depthTest.enable(); dxstate.depthFunc.set(ztests[gstate.getDepthTestFunction()]); dxstate.depthWrite.set(gstate.isDepthWriteEnabled()); + UpdateEverUsedEqualDepth(gstate.getDepthTestFunction()); } else { dxstate.depthTest.disable(); } diff --git a/GPU/GLES/DrawEngineGLES.cpp b/GPU/GLES/DrawEngineGLES.cpp index e95afe57ed..2c0c54304d 100644 --- a/GPU/GLES/DrawEngineGLES.cpp +++ b/GPU/GLES/DrawEngineGLES.cpp @@ -404,6 +404,10 @@ void DrawEngineGLES::DoFlush() { swTransform.BuildDrawingParams(prim, vertexCount, dec_->VertexType(), inds, maxIndex, &result); if (result.setSafeSize) framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight); + // Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values. + // Games sometimes expect exact matches (see #12626, for example) for equal comparisons. + if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f) + result.action = SW_DRAW_PRIMITIVES; ApplyDrawStateLate(result.setStencil, result.stencilValue); diff --git a/GPU/GLES/GPU_GLES.cpp b/GPU/GLES/GPU_GLES.cpp index 791fa22869..8b71b2dce1 100644 --- a/GPU/GLES/GPU_GLES.cpp +++ b/GPU/GLES/GPU_GLES.cpp @@ -181,17 +181,18 @@ u32 GPU_GLES::CheckGPUFeatures() const { // If we already have a 16-bit depth buffer, we don't need to round. bool prefer24 = draw_->GetDeviceCaps().preferredDepthBufferFormat == Draw::DataFormat::D24_S8; - if (prefer24) { + bool prefer16 = draw_->GetDeviceCaps().preferredDepthBufferFormat == Draw::DataFormat::D16; + if (!prefer16) { if (!g_Config.bHighQualityDepth && (features & GPU_USE_ACCURATE_DEPTH) != 0) { features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; } else if (PSP_CoreParameter().compat.flags().PixelDepthRounding) { - if (!gl_extensions.IsGLES || gl_extensions.GLES3) { - // Use fragment rounding on desktop and GLES3, most accurate. - features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT; - } else if (prefer24 && (features & GPU_USE_ACCURATE_DEPTH) != 0) { + if (prefer24 && (features & GPU_USE_ACCURATE_DEPTH) != 0) { // Here we can simulate a 16 bit depth buffer by scaling. // Note that the depth buffer is fixed point, not floating, so dividing by 256 is pretty good. features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; + } else if (!gl_extensions.IsGLES || gl_extensions.GLES3) { + // Use fragment rounding on desktop and GLES3, most accurate. + features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT; } else { // At least do vertex rounding if nothing else. features |= GPU_ROUND_DEPTH_TO_16BIT; diff --git a/GPU/GLES/StateMappingGLES.cpp b/GPU/GLES/StateMappingGLES.cpp index f80a3f5490..1d06ac01e4 100644 --- a/GPU/GLES/StateMappingGLES.cpp +++ b/GPU/GLES/StateMappingGLES.cpp @@ -253,7 +253,10 @@ void DrawEngineGLES::ApplyDrawState(int prim) { renderManager->SetDepth(true, gstate.isClearModeDepthMask() ? true : false, GL_ALWAYS); } else { // Depth Test - renderManager->SetDepth(gstate.isDepthTestEnabled(), gstate.isDepthWriteEnabled(), compareOps[gstate.getDepthTestFunction()]); + bool depthTestUsed = !IsDepthTestEffectivelyDisabled(); + renderManager->SetDepth(depthTestUsed, gstate.isDepthWriteEnabled(), compareOps[gstate.getDepthTestFunction()]); + if (depthTestUsed) + UpdateEverUsedEqualDepth(gstate.getDepthTestFunction()); // Stencil Test if (stencilState.enabled) { diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index de81929a32..c1d1871140 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -899,6 +899,10 @@ void DrawEngineVulkan::DoFlush() { if (result.setSafeSize) framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight); + // Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values. + // Games sometimes expect exact matches (see #12626, for example) for equal comparisons. + if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f) + result.action = SW_DRAW_PRIMITIVES; // Only here, where we know whether to clear or to draw primitives, should we actually set the current framebuffer! Because that gives use the opportunity // to use a "pre-clear" render pass, for high efficiency on tilers. diff --git a/GPU/Vulkan/GPU_Vulkan.cpp b/GPU/Vulkan/GPU_Vulkan.cpp index 754ef6ffce..17387c3e8b 100644 --- a/GPU/Vulkan/GPU_Vulkan.cpp +++ b/GPU/Vulkan/GPU_Vulkan.cpp @@ -257,15 +257,22 @@ u32 GPU_Vulkan::CheckGPUFeatures() const { INFO_LOG(G3D, "Deficient texture format support: 4444: %d 1555: %d 565: %d", fmt4444, fmt1555, fmt565); } - if (!g_Config.bHighQualityDepth && (features & GPU_USE_ACCURATE_DEPTH) != 0) { - features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; - } - else if (PSP_CoreParameter().compat.flags().PixelDepthRounding) { - // Use fragment rounding on desktop and GLES3, most accurate. - features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT; - } - else if (PSP_CoreParameter().compat.flags().VertexDepthRounding) { - features |= GPU_ROUND_DEPTH_TO_16BIT; + bool prefer24 = draw_->GetDeviceCaps().preferredDepthBufferFormat == Draw::DataFormat::D24_S8; + bool prefer16 = draw_->GetDeviceCaps().preferredDepthBufferFormat == Draw::DataFormat::D16; + if (!prefer16) { + if (!g_Config.bHighQualityDepth && (features & GPU_USE_ACCURATE_DEPTH) != 0) { + features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; + } else if (PSP_CoreParameter().compat.flags().PixelDepthRounding) { + if (prefer24 && (features & GPU_USE_ACCURATE_DEPTH) != 0) { + // Here we can simulate a 16 bit depth buffer by scaling. + // Note that the depth buffer is fixed point, not floating, so dividing by 256 is pretty good. + features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT; + } else { + features |= GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT; + } + } else if (PSP_CoreParameter().compat.flags().VertexDepthRounding) { + features |= GPU_ROUND_DEPTH_TO_16BIT; + } } if (g_Config.bStereoRendering && draw_->GetDeviceCaps().multiViewSupported) { diff --git a/GPU/Vulkan/StateMappingVulkan.cpp b/GPU/Vulkan/StateMappingVulkan.cpp index 9e2177b901..5f75ea2863 100644 --- a/GPU/Vulkan/StateMappingVulkan.cpp +++ b/GPU/Vulkan/StateMappingVulkan.cpp @@ -274,10 +274,11 @@ void DrawEngineVulkan::ConvertStateToVulkanKey(FramebufferManagerVulkan &fbManag } } else { // Depth Test - if (gstate.isDepthTestEnabled()) { + if (!IsDepthTestEffectivelyDisabled()) { key.depthTestEnable = true; key.depthCompareOp = compareOps[gstate.getDepthTestFunction()]; key.depthWriteEnable = gstate.isDepthWriteEnabled(); + UpdateEverUsedEqualDepth(gstate.getDepthTestFunction()); } else { key.depthTestEnable = false; key.depthWriteEnable = false;