diff --git a/GPU/Common/FragmentShaderGenerator.cpp b/GPU/Common/FragmentShaderGenerator.cpp index 1f89aa2261..fd90240a62 100644 --- a/GPU/Common/FragmentShaderGenerator.cpp +++ b/GPU/Common/FragmentShaderGenerator.cpp @@ -1051,8 +1051,10 @@ bool GenerateFragmentShader(const FShaderID &id, char *buffer, const ShaderLangu } if (colorToDepth) { + DepthScaleFactors factors = GetDepthScaleFactors(); + WRITE(p, " highp float depthValue = float(uint(%s.x * 32.0) | (uint(%s.y * 64.0) << 5) | (uint(%s.z * 32.0) << 11)) / 65535.0;\n", "v", "v", "v"); // compat.fragColor0, compat.fragColor0, compat.fragColor0); - WRITE(p, " gl_FragDepth = depthValue;\n"); // TODO: Don't forget to apply accurate-depth kind of stuff + WRITE(p, " gl_FragDepth = (depthValue / %f) - %f;\n", factors.scale / 65535.0f, factors.offset); } if (gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { diff --git a/GPU/Common/GPUStateUtils.h b/GPU/Common/GPUStateUtils.h index 14edc21638..956a71e7f6 100644 --- a/GPU/Common/GPUStateUtils.h +++ b/GPU/Common/GPUStateUtils.h @@ -81,7 +81,6 @@ struct ViewportAndScissor { void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, float renderHeight, int bufferWidth, int bufferHeight, ViewportAndScissor &out); float ToScaledDepthFromIntegerScale(float z); -// Use like this: (z - offset) * scale struct DepthScaleFactors { float offset; float scale; @@ -89,6 +88,10 @@ struct DepthScaleFactors { float Apply(float z) const { return (z - offset) * scale; } + + float ApplyInverse(float z) const { + return (z / scale) + offset; + } }; DepthScaleFactors GetDepthScaleFactors(); diff --git a/GPU/GLES/StateMappingGLES.cpp b/GPU/GLES/StateMappingGLES.cpp index 91e77594aa..08e61ee56b 100644 --- a/GPU/GLES/StateMappingGLES.cpp +++ b/GPU/GLES/StateMappingGLES.cpp @@ -250,7 +250,11 @@ void DrawEngineGLES::ApplyDrawState(int prim) { GenericStencilFuncState stencilState; ConvertStencilFuncState(stencilState); - if (gstate.isModeClear()) { + if (gstate_c.renderMode == FB_MODE_COLOR_TO_DEPTH) { + // Enforce plain depth writing. + renderManager->SetStencilDisabled(); + renderManager->SetDepth(true, true, GL_ALWAYS); + } else if (gstate.isModeClear()) { // Depth Test if (gstate.isClearModeDepthMask()) { framebufferManager_->SetDepthUpdated();