Vulkan: Add support for unrestricted depth range.

This commit is contained in:
CrossVR
2025-07-21 05:48:04 +09:00
parent dccd8709d8
commit 0ace5f5d3d
6 changed files with 35 additions and 10 deletions

View File

@ -216,17 +216,12 @@ void SetScissorAndViewport()
height *= -1;
}
// The maximum depth that is written to the depth buffer should never exceed this value.
// This is necessary because we use a 2^24 divisor for all our depth values to prevent
// floating-point round-trip errors. However the console GPU doesn't ever write a value
// to the depth buffer that exceeds 2^24 - 1.
constexpr float GX_MAX_DEPTH = 16777215.0f / 16777216.0f;
if (!g_backend_info.bSupportsDepthClamp)
{
// 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 = std::clamp(min_depth, 0.0f, GX_MAX_DEPTH);
max_depth = std::clamp(max_depth, 0.0f, GX_MAX_DEPTH);
min_depth = std::clamp(min_depth, 0.0f, MAX_EFB_DEPTH);
max_depth = std::clamp(max_depth, 0.0f, MAX_EFB_DEPTH);
}
if (VertexShaderManager::UseVertexDepthRange())
@ -235,13 +230,13 @@ void SetScissorAndViewport()
// Taking into account whether the depth range is inverted or not.
if (xfmem.viewport.zRange < 0.0f && g_backend_info.bSupportsReversedDepthRange)
{
min_depth = GX_MAX_DEPTH;
min_depth = MAX_EFB_DEPTH;
max_depth = 0.0f;
}
else
{
min_depth = 0.0f;
max_depth = GX_MAX_DEPTH;
max_depth = MAX_EFB_DEPTH;
}
}

View File

@ -131,6 +131,11 @@ void VertexShaderManager::SetProjectionMatrix(XFStateManager& xf_state_manager)
bool VertexShaderManager::UseVertexDepthRange()
{
// Backend has full support for unrestricted depth ranges including the ability to clamp the
// final depth value to MAX_EFB_DEPTH.
if (g_backend_info.bSupportsUnrestrictedDepthRange)
return false;
// We can't compute the depth range in the vertex shader if we don't support depth clamp.
if (!g_backend_info.bSupportsDepthClamp)
return false;

View File

@ -15,6 +15,12 @@
constexpr u32 EFB_WIDTH = 640u;
constexpr u32 EFB_HEIGHT = 528u;
// The maximum depth that is written to the depth buffer should never exceed this value.
// This is necessary because we use a 2^24 divisor for all our depth values to prevent
// floating-point round-trip errors. However the console GPU doesn't ever write a value
// to the depth buffer that exceeds 2^24 - 1.
constexpr float MAX_EFB_DEPTH = 16777215.0f / 16777216.0f;
// Max XFB width is 720. You can only copy out 640 wide areas of efb to XFB
// so you need multiple copies to do the full width.
// The VI can do horizontal scaling (TODO: emulate).

View File

@ -180,6 +180,7 @@ struct BackendInfo
bool bSupportsVSLinePointExpand = false;
bool bSupportsGLLayerInFS = true;
bool bSupportsHDROutput = false;
bool bSupportsUnrestrictedDepthRange = false;
};
extern BackendInfo g_backend_info;