From a15555fe0304e1ba560f91f8bacc8b121d226d1c Mon Sep 17 00:00:00 2001 From: Jules Blok Date: Fri, 24 Feb 2017 15:16:28 +0100 Subject: [PATCH] VideoBackends: Use vertex shader depth range if ztexture is used. --- Source/Core/VideoBackends/D3D/Render.cpp | 3 +- Source/Core/VideoBackends/D3D12/Render.cpp | 3 +- Source/Core/VideoBackends/OGL/Render.cpp | 47 +++++++++---------- Source/Core/VideoBackends/Vulkan/Renderer.cpp | 4 +- Source/Core/VideoCommon/BPStructs.cpp | 2 + Source/Core/VideoCommon/RenderBase.cpp | 27 +++++++++++ Source/Core/VideoCommon/RenderBase.h | 2 + .../Core/VideoCommon/VertexShaderManager.cpp | 41 ++++++---------- 8 files changed, 69 insertions(+), 60 deletions(-) diff --git a/Source/Core/VideoBackends/D3D/Render.cpp b/Source/Core/VideoBackends/D3D/Render.cpp index a100fb5616..15273ed719 100644 --- a/Source/Core/VideoBackends/D3D/Render.cpp +++ b/Source/Core/VideoBackends/D3D/Render.cpp @@ -576,8 +576,7 @@ void Renderer::SetViewport() // If an inverted or oversized depth range is used, we need to calculate the depth range in the // vertex shader. - if (xfmem.viewport.zRange < 0.0f || fabs(xfmem.viewport.zRange) > 16777215.0f || - fabs(xfmem.viewport.farZ) > 16777215.0f) + if (UseVertexDepthRange()) { // We need to ensure depth values are clamped the maximum value supported by the console GPU. min_depth = 0.0f; diff --git a/Source/Core/VideoBackends/D3D12/Render.cpp b/Source/Core/VideoBackends/D3D12/Render.cpp index d5bdaeb07a..4d2badc2e2 100644 --- a/Source/Core/VideoBackends/D3D12/Render.cpp +++ b/Source/Core/VideoBackends/D3D12/Render.cpp @@ -481,8 +481,7 @@ void Renderer::SetViewport() // If an inverted or oversized depth range is used, we need to calculate the depth range in the // vertex shader. - if (xfmem.viewport.zRange < 0.0f || fabs(xfmem.viewport.zRange) > 16777215.0f || - fabs(xfmem.viewport.farZ) > 16777215.0f) + if (UseVertexDepthRange()) { // We need to ensure depth values are clamped the maximum value supported by the console GPU. min_depth = 0.0f; diff --git a/Source/Core/VideoBackends/OGL/Render.cpp b/Source/Core/VideoBackends/OGL/Render.cpp index f8405250d7..ee7d37bb67 100644 --- a/Source/Core/VideoBackends/OGL/Render.cpp +++ b/Source/Core/VideoBackends/OGL/Render.cpp @@ -1135,41 +1135,36 @@ void Renderer::SetViewport() glViewport(iceilf(X), iceilf(Y), iceilf(Width), iceilf(Height)); } - // Set the reversed depth range. - if (g_ActiveConfig.backend_info.bSupportsOversizedDepthRanges) + if (!g_ActiveConfig.backend_info.bSupportsOversizedDepthRanges && + !g_ActiveConfig.backend_info.bSupportsDepthClamp) { - glDepthRangedNV(max_depth, min_depth); + // There's no way to support oversized depth ranges in this situation. Let's just clamp the + // range to the maximum value supported by the console GPU and hope for the best. + min_depth = MathUtil::Clamp(min_depth, 0.0f, GX_MAX_DEPTH); + max_depth = MathUtil::Clamp(max_depth, 0.0f, GX_MAX_DEPTH); } - else + + if (UseVertexDepthRange()) { - // If an oversized depth range is used, we need to calculate the depth range in the - // vertex shader. - if (g_ActiveConfig.backend_info.bSupportsDepthClamp && - (fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f)) + // We need to ensure depth values are clamped the maximum value supported by the console GPU. + // Taking into account whether the depth range is inverted or not. + if (xfmem.viewport.zRange < 0.0f) { - // We need to ensure depth values are clamped the maximum value supported by the console GPU. - // Taking into account whether the depth range is inverted or not. - if (xfmem.viewport.zRange < 0.0f) - { - min_depth = GX_MAX_DEPTH; - max_depth = 0.0f; - } - else - { - min_depth = 0.0f; - max_depth = GX_MAX_DEPTH; - } + min_depth = GX_MAX_DEPTH; + max_depth = 0.0f; } else { - // There's no way to support oversized depth ranges in this situation. Let's just clamp the - // range to the maximum value supported by the console GPU and hope for the best. - min_depth = MathUtil::Clamp(min_depth, 0.0f, GX_MAX_DEPTH); - max_depth = MathUtil::Clamp(max_depth, 0.0f, GX_MAX_DEPTH); + min_depth = 0.0f; + max_depth = GX_MAX_DEPTH; } - - glDepthRangef(max_depth, min_depth); } + + // Set the reversed depth range. + if (g_ActiveConfig.backend_info.bSupportsOversizedDepthRanges) + glDepthRangedNV(max_depth, min_depth); + else + glDepthRangef(max_depth, min_depth); } void Renderer::ClearScreen(const EFBRectangle& rc, bool colorEnable, bool alphaEnable, bool zEnable, diff --git a/Source/Core/VideoBackends/Vulkan/Renderer.cpp b/Source/Core/VideoBackends/Vulkan/Renderer.cpp index ebfc755327..3cf8919042 100644 --- a/Source/Core/VideoBackends/Vulkan/Renderer.cpp +++ b/Source/Core/VideoBackends/Vulkan/Renderer.cpp @@ -4,7 +4,6 @@ #include "VideoBackends/Vulkan/Renderer.h" -#include #include #include #include @@ -1657,8 +1656,7 @@ void Renderer::SetViewport() // If an oversized or inverted depth range is used, we need to calculate the depth range in the // vertex shader. // TODO: Inverted depth ranges are bugged in all drivers, which should be added to DriverDetails. - if (xfmem.viewport.zRange < 0.0f || fabs(xfmem.viewport.zRange) > 16777215.0f || - fabs(xfmem.viewport.farZ) > 16777215.0f) + if (UseVertexDepthRange()) { // We need to ensure depth values are clamped the maximum value supported by the console GPU. min_depth = 0.0f; diff --git a/Source/Core/VideoCommon/BPStructs.cpp b/Source/Core/VideoCommon/BPStructs.cpp index 128969af52..e42f36f471 100644 --- a/Source/Core/VideoCommon/BPStructs.cpp +++ b/Source/Core/VideoCommon/BPStructs.cpp @@ -332,6 +332,8 @@ static void BPWritten(const BPCmd& bp) { if (bp.changes & 3) PixelShaderManager::SetZTextureTypeChanged(); + if (bp.changes & 12) + VertexShaderManager::SetViewportChanged(); #if defined(_DEBUG) || defined(DEBUGFAST) const char* pzop[] = {"DISABLE", "ADD", "REPLACE", "?"}; const char* pztype[] = {"Z8", "Z16", "Z24", "?"}; diff --git a/Source/Core/VideoCommon/RenderBase.cpp b/Source/Core/VideoCommon/RenderBase.cpp index f47288ea4e..19ec2abed2 100644 --- a/Source/Core/VideoCommon/RenderBase.cpp +++ b/Source/Core/VideoCommon/RenderBase.cpp @@ -940,3 +940,30 @@ void Renderer::DumpFrameToImage(const FrameDumpConfig& config) TextureToPng(config.data, config.stride, filename, config.width, config.height, false); m_frame_dump_image_counter++; } + +bool Renderer::UseVertexDepthRange() const +{ + // We can't compute the depth range in the vertex shader if we don't support depth clamp. + if (!g_ActiveConfig.backend_info.bSupportsDepthClamp) + return false; + + if (g_ActiveConfig.backend_info.bSupportsOversizedDepthRanges) + { + // We support oversized depth ranges, but we need a full depth range if a ztexture is used. + return bpmem.ztex2.type != ZTEXTURE_DISABLE; + } + else + { + // We need a full depth range if a ztexture is used. + if (bpmem.ztex2.type != ZTEXTURE_DISABLE) + return true; + + // If an inverted depth range is unsupported, we also need to check if the range is inverted. + if (!g_ActiveConfig.backend_info.bSupportsReversedDepthRange && xfmem.viewport.zRange < 0.0f) + return true; + + // If an oversized depth range or a ztexture is used, we need to calculate the depth range + // in the vertex shader. + return fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f; + } +} diff --git a/Source/Core/VideoCommon/RenderBase.h b/Source/Core/VideoCommon/RenderBase.h index 46c01afb78..2be470fe38 100644 --- a/Source/Core/VideoCommon/RenderBase.h +++ b/Source/Core/VideoCommon/RenderBase.h @@ -145,6 +145,8 @@ public: // Final surface changing // This is called when the surface is resized (WX) or the window changes (Android). virtual void ChangeSurface(void* new_surface_handle) {} + bool UseVertexDepthRange() const; + protected: static void CalculateTargetScale(int x, int y, int* scaledX, int* scaledY); bool CalculateTargetSize(); diff --git a/Source/Core/VideoCommon/VertexShaderManager.cpp b/Source/Core/VideoCommon/VertexShaderManager.cpp index ed8d52efb7..5998fa2112 100644 --- a/Source/Core/VideoCommon/VertexShaderManager.cpp +++ b/Source/Core/VideoCommon/VertexShaderManager.cpp @@ -391,40 +391,27 @@ void VertexShaderManager::SetConstants() constants.pixelcentercorrection[2] = 1.0f; constants.pixelcentercorrection[3] = 0.0f; - if (g_ActiveConfig.backend_info.bSupportsDepthClamp && - !g_ActiveConfig.backend_info.bSupportsOversizedDepthRanges) + if (g_renderer->UseVertexDepthRange()) { // Oversized depth ranges are handled in the vertex shader. We need to reverse - // the far value to get a reversed depth range mapping. This is necessary - // because the standard depth range equation pushes all depth values towards - // the back of the depth buffer where conventionally depth buffers have the - // least precision. + // the far value to use the reversed-Z trick. if (g_ActiveConfig.backend_info.bSupportsReversedDepthRange) { - if (fabs(xfmem.viewport.zRange) > 16777215.0f || fabs(xfmem.viewport.farZ) > 16777215.0f) - { - // For backends that support reversing the depth range we also support cases - // where the console also uses reversed depth with the same accuracy. We need - // to make sure the depth range is positive here and then reverse the depth in - // the backend viewport. - constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f; - if (xfmem.viewport.zRange < 0.0f) - constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f; - else - constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; - } + // Sometimes the console also tries to use the reversed-Z trick. We can only do + // that with the expected accuracy if the backend can reverse the depth range. + constants.pixelcentercorrection[2] = fabs(xfmem.viewport.zRange) / 16777215.0f; + if (xfmem.viewport.zRange < 0.0f) + constants.pixelcentercorrection[3] = xfmem.viewport.farZ / 16777215.0f; + else + constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; } else { - if (xfmem.viewport.zRange < 0.0f || xfmem.viewport.zRange > 16777215.0f || - fabs(xfmem.viewport.farZ) > 16777215.0f) - { - // For backends that don't support reversing the depth range we can still render - // cases where the console uses reversed depth correctly. But we simply can't - // provide the same accuracy as the console. - constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f; - constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; - } + // For backends that don't support reversing the depth range we can still render + // cases where the console uses the reversed-Z trick. But we simply can't provide + // the expected accuracy, which might result in z-fighting. + constants.pixelcentercorrection[2] = xfmem.viewport.zRange / 16777215.0f; + constants.pixelcentercorrection[3] = 1.0f - xfmem.viewport.farZ / 16777215.0f; } }