VideoBackends: Use vertex shader depth range if ztexture is used.

This commit is contained in:
Jules Blok 2017-02-24 15:16:28 +01:00
parent bde8126913
commit a15555fe03
8 changed files with 69 additions and 60 deletions

View File

@ -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;

View File

@ -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;

View File

@ -1135,17 +1135,16 @@ 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 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))
if (UseVertexDepthRange())
{
// 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.
@ -1160,17 +1159,13 @@ void Renderer::SetViewport()
max_depth = GX_MAX_DEPTH;
}
}
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);
}
// 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,
u32 color, u32 z)

View File

@ -4,7 +4,6 @@
#include "VideoBackends/Vulkan/Renderer.h"
#include <cmath>
#include <cstddef>
#include <cstdio>
#include <limits>
@ -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;

View File

@ -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", "?"};

View File

@ -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;
}
}

View File

@ -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();

View File

@ -391,42 +391,29 @@ 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.
// 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.
// 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;
}
}
}
dirty = true;
// This is so implementation-dependent that we can't have it here.