summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon
diff options
context:
space:
mode:
authorMarkus Wick <degasus@users.noreply.github.com>2017-03-10 18:05:52 +0100
committerGitHub <noreply@github.com>2017-03-10 18:05:52 +0100
commite99cd57eb3827b246ea712e031bac3ffe92fe109 (patch)
treef12d83d565f2e36d785046ff5917814be50fe62b /Source/Core/VideoCommon
parenta3f5e3e27dacc40082346bbb00d41eddd61b16b9 (diff)
parent3f41e6d4cf9f1a318c6d32fea4d2b422684961a3 (diff)
Merge pull request #4935 from Armada651/depth-range-fix
VideoBackends: Set the maximum range when the depth range is oversized.
Diffstat (limited to 'Source/Core/VideoCommon')
-rw-r--r--Source/Core/VideoCommon/BPStructs.cpp2
-rw-r--r--Source/Core/VideoCommon/RenderBase.cpp29
-rw-r--r--Source/Core/VideoCommon/RenderBase.h2
-rw-r--r--Source/Core/VideoCommon/VertexShaderManager.cpp40
-rw-r--r--Source/Core/VideoCommon/VideoConfig.h1
5 files changed, 48 insertions, 26 deletions
diff --git a/Source/Core/VideoCommon/BPStructs.cpp b/Source/Core/VideoCommon/BPStructs.cpp
index 8384080099..774754839b 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 1c99e51950..87b3a5314a 100644
--- a/Source/Core/VideoCommon/RenderBase.cpp
+++ b/Source/Core/VideoCommon/RenderBase.cpp
@@ -926,3 +926,32 @@ 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;
+
+ const bool ztexture_enabled = bpmem.ztex2.type != ZTEXTURE_DISABLE && !bpmem.zcontrol.early_ztest;
+
+ if (g_ActiveConfig.backend_info.bSupportsOversizedDepthRanges)
+ {
+ // We support oversized depth ranges, but we need a full depth range if a ztexture is used.
+ return ztexture_enabled;
+ }
+ else
+ {
+ // We need a full depth range if a ztexture is used.
+ if (ztexture_enabled)
+ 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 b1291adf0d..93c22f6202 100644
--- a/Source/Core/VideoCommon/RenderBase.h
+++ b/Source/Core/VideoCommon/RenderBase.h
@@ -140,6 +140,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:
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 4c3959d92d..36bace7426 100644
--- a/Source/Core/VideoCommon/VertexShaderManager.cpp
+++ b/Source/Core/VideoCommon/VertexShaderManager.cpp
@@ -391,39 +391,27 @@ void VertexShaderManager::SetConstants()
constants.pixelcentercorrection[2] = 1.0f;
constants.pixelcentercorrection[3] = 0.0f;
- if (g_ActiveConfig.backend_info.bSupportsDepthClamp)
+ 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;
}
}
diff --git a/Source/Core/VideoCommon/VideoConfig.h b/Source/Core/VideoCommon/VideoConfig.h
index ec79e6de48..d62334df8e 100644
--- a/Source/Core/VideoCommon/VideoConfig.h
+++ b/Source/Core/VideoCommon/VideoConfig.h
@@ -179,6 +179,7 @@ struct VideoConfig final
bool bSupportsDualSourceBlend;
bool bSupportsPrimitiveRestart;
bool bSupportsOversizedViewports;
+ bool bSupportsOversizedDepthRanges;
bool bSupportsGeometryShaders;
bool bSupports3DVision;
bool bSupportsEarlyZ; // needed by PixelShaderGen, so must stay in VideoCommon