Correct pixel depth rounding.

This commit is contained in:
Unknown W. Brackets 2016-01-19 20:19:50 -08:00
parent 5de7d2cf8f
commit e00c9940e8
3 changed files with 14 additions and 1 deletions

View File

@ -515,6 +515,10 @@ float FromScaledDepth(float z) {
return (z - offset) * depthSliceFactor * 65535.0f;
}
float DepthSliceFactor() {
return depthSliceFactor;
}
void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, float renderHeight, int bufferWidth, int bufferHeight, ViewportAndScissor &out) {
bool throughmode = gstate.isModeThrough();
out.dirtyProj = false;

View File

@ -68,6 +68,7 @@ struct ViewportAndScissor {
void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, float renderHeight, int bufferWidth, int bufferHeight, ViewportAndScissor &out);
float ToScaledDepth(u16 z);
float FromScaledDepth(float z);
float DepthSliceFactor();
// These are common to all modern APIs and can be easily converted with a lookup table.
enum class BlendFactor : uint8_t {

View File

@ -676,8 +676,16 @@ bool GenerateFragmentShader(const ShaderID &id, char *buffer) {
#endif
if (gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) {
const double scale = DepthSliceFactor() * 65535.0;
WRITE(p, " highp float z = gl_FragCoord.z;\n");
WRITE(p, " z = (1.0/65535.0) * floor(z * 65535.0);\n");
// We center the depth with an offset, but only its fraction matters.
// When (DepthSliceFactor() - 1) is odd, it will be 0.5, otherwise 0.
if (((int)(DepthSliceFactor() - 1.0f) & 1) == 1) {
WRITE(p, " z = (floor((z * %f) - (1.0 / 2.0)) + (1.0 / 2.0)) * (1.0 / %f);\n", scale, scale);
} else {
WRITE(p, " z = floor(z * %f) * (1.0 / %f);\n", scale, scale);
}
WRITE(p, " gl_FragDepth = z;\n");
}