Commit 3cbeac56 by Nicolas Capens Committed by Nicolas Capens

Fix polygon offset depth clamp and units resolution.

OpenGL requires depth values to be clamped to the [0, 1] range. Due to frustum clipping already limiting the range, this can only happen when non-zero polygon offset parameters are active. Also fix the 'minimum resolvable difference' for the 32-bit floating- point internal depth format that we use. Bug swiftshader:82 Change-Id: Ic9ebcac182a2bc81ab51d79cfe0bb451d340bd1e Reviewed-on: https://swiftshader-review.googlesource.com/12108Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <nicolascapens@google.com>
parent 66954c9a
...@@ -2180,7 +2180,8 @@ template<typename T> bool Context::getIntegerv(GLenum pname, T *params) const ...@@ -2180,7 +2180,8 @@ template<typename T> bool Context::getIntegerv(GLenum pname, T *params) const
case GL_MAX_PROGRAM_TEXEL_OFFSET: case GL_MAX_PROGRAM_TEXEL_OFFSET:
// Note: SwiftShader has no actual texel offset limit, so this limit can be modified if required. // Note: SwiftShader has no actual texel offset limit, so this limit can be modified if required.
// In any case, any behavior outside the specified range is valid since the spec mentions: // In any case, any behavior outside the specified range is valid since the spec mentions:
// (see OpenGL ES 3.0.5, 3.8.10.1 Scale Factor and Level of Detail, p.153) // "If any of the offset values are outside the range of the implementation-defined values // (see OpenGL ES 3.0.5, 3.8.10.1 Scale Factor and Level of Detail, p.153)
// "If any of the offset values are outside the range of the implementation-defined values
// MIN_PROGRAM_TEXEL_OFFSET and MAX_PROGRAM_TEXEL_OFFSET, results of the texture lookup are // MIN_PROGRAM_TEXEL_OFFSET and MAX_PROGRAM_TEXEL_OFFSET, results of the texture lookup are
// undefined." // undefined."
*params = MAX_PROGRAM_TEXEL_OFFSET; *params = MAX_PROGRAM_TEXEL_OFFSET;
...@@ -2221,7 +2222,8 @@ template<typename T> bool Context::getIntegerv(GLenum pname, T *params) const ...@@ -2221,7 +2222,8 @@ template<typename T> bool Context::getIntegerv(GLenum pname, T *params) const
case GL_MIN_PROGRAM_TEXEL_OFFSET: case GL_MIN_PROGRAM_TEXEL_OFFSET:
// Note: SwiftShader has no actual texel offset limit, so this limit can be modified if required. // Note: SwiftShader has no actual texel offset limit, so this limit can be modified if required.
// In any case, any behavior outside the specified range is valid since the spec mentions: // In any case, any behavior outside the specified range is valid since the spec mentions:
// (see OpenGL ES 3.0.5, 3.8.10.1 Scale Factor and Level of Detail, p.153) // "If any of the offset values are outside the range of the implementation-defined values // (see OpenGL ES 3.0.5, 3.8.10.1 Scale Factor and Level of Detail, p.153)
// "If any of the offset values are outside the range of the implementation-defined values
// MIN_PROGRAM_TEXEL_OFFSET and MAX_PROGRAM_TEXEL_OFFSET, results of the texture lookup are // MIN_PROGRAM_TEXEL_OFFSET and MAX_PROGRAM_TEXEL_OFFSET, results of the texture lookup are
// undefined." // undefined."
*params = MIN_PROGRAM_TEXEL_OFFSET; *params = MIN_PROGRAM_TEXEL_OFFSET;
...@@ -2855,7 +2857,7 @@ void Context::applyState(GLenum drawMode) ...@@ -2855,7 +2857,7 @@ void Context::applyState(GLenum drawMode)
if(depthbuffer) if(depthbuffer)
{ {
device->setSlopeDepthBias(mState.polygonOffsetFactor); device->setSlopeDepthBias(mState.polygonOffsetFactor);
float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize())); float depthBias = ldexp(mState.polygonOffsetUnits, -23); // We use 32-bit floating-point for all depth formats, with 23 mantissa bits.
device->setDepthBias(depthBias); device->setDepthBias(depthBias);
} }
} }
......
...@@ -1607,6 +1607,7 @@ namespace sw2es ...@@ -1607,6 +1607,7 @@ namespace sw2es
// case sw::FORMAT_D32_LOCKABLE: return 0; // case sw::FORMAT_D32_LOCKABLE: return 0;
// case sw::FORMAT_S8_LOCKABLE: return 8; // case sw::FORMAT_S8_LOCKABLE: return 8;
default: default:
UNREACHABLE(stencilFormat);
return 0; return 0;
} }
} }
...@@ -1642,6 +1643,7 @@ namespace sw2es ...@@ -1642,6 +1643,7 @@ namespace sw2es
case sw::FORMAT_R5G6B5: case sw::FORMAT_R5G6B5:
return 0; return 0;
default: default:
UNREACHABLE(colorFormat);
return 0; return 0;
} }
} }
...@@ -1705,6 +1707,7 @@ namespace sw2es ...@@ -1705,6 +1707,7 @@ namespace sw2es
case sw::FORMAT_R5G6B5: case sw::FORMAT_R5G6B5:
return 5; return 5;
default: default:
UNREACHABLE(colorFormat);
return 0; return 0;
} }
} }
...@@ -1759,6 +1762,7 @@ namespace sw2es ...@@ -1759,6 +1762,7 @@ namespace sw2es
case sw::FORMAT_R5G6B5: case sw::FORMAT_R5G6B5:
return 6; return 6;
default: default:
UNREACHABLE(colorFormat);
return 0; return 0;
} }
} }
...@@ -1802,6 +1806,7 @@ namespace sw2es ...@@ -1802,6 +1806,7 @@ namespace sw2es
case sw::FORMAT_R5G6B5: case sw::FORMAT_R5G6B5:
return 5; return 5;
default: default:
UNREACHABLE(colorFormat);
return 0; return 0;
} }
} }
...@@ -1827,7 +1832,9 @@ namespace sw2es ...@@ -1827,7 +1832,9 @@ namespace sw2es
// case sw::FORMAT_S8_LOCKABLE: return 0; // case sw::FORMAT_S8_LOCKABLE: return 0;
case sw::FORMAT_D32FS8_SHADOW: case sw::FORMAT_D32FS8_SHADOW:
case sw::FORMAT_D32FS8_TEXTURE: return 32; case sw::FORMAT_D32FS8_TEXTURE: return 32;
default: return 0; default:
UNREACHABLE(depthFormat);
return 0;
} }
} }
......
...@@ -276,6 +276,9 @@ namespace sw ...@@ -276,6 +276,9 @@ namespace sw
cullMode = CULL_CLOCKWISE; cullMode = CULL_CLOCKWISE;
alphaReference = 0.0f; alphaReference = 0.0f;
depthBias = 0.0f;
slopeDepthBias = 0.0f;
for(int i = 0; i < RENDERTARGETS; i++) for(int i = 0; i < RENDERTARGETS; i++)
{ {
colorWriteMask[i] = 0x0000000F; colorWriteMask[i] = 0x0000000F;
......
...@@ -434,6 +434,9 @@ namespace sw ...@@ -434,6 +434,9 @@ namespace sw
CullMode cullMode; CullMode cullMode;
float alphaReference; float alphaReference;
float depthBias;
float slopeDepthBias;
TextureStage textureStage[8]; TextureStage textureStage[8];
Sampler sampler[TOTAL_IMAGE_UNITS]; Sampler sampler[TOTAL_IMAGE_UNITS];
......
...@@ -990,6 +990,7 @@ namespace sw ...@@ -990,6 +990,7 @@ namespace sw
state.pixelFogMode = context->pixelFogActive(); state.pixelFogMode = context->pixelFogActive();
state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE; state.wBasedFog = context->wBasedFog && context->pixelFogActive() != FOG_NONE;
state.perspective = context->perspectiveActive(); state.perspective = context->perspectiveActive();
state.depthClamp = (context->depthBias != 0.0f) || (context->slopeDepthBias != 0.0f);
if(context->alphaBlendActive()) if(context->alphaBlendActive())
{ {
......
...@@ -66,6 +66,7 @@ namespace sw ...@@ -66,6 +66,7 @@ namespace sw
bool occlusionEnabled : 1; bool occlusionEnabled : 1;
bool wBasedFog : 1; bool wBasedFog : 1;
bool perspective : 1; bool perspective : 1;
bool depthClamp : 1;
bool alphaBlendActive : 1; bool alphaBlendActive : 1;
BlendFactor sourceBlendFactor : BITS(BLEND_LAST); BlendFactor sourceBlendFactor : BITS(BLEND_LAST);
......
...@@ -545,7 +545,7 @@ namespace sw ...@@ -545,7 +545,7 @@ namespace sw
if(context->isDrawTriangle(false)) if(context->isDrawTriangle(false))
{ {
N += depthBias; N += context->depthBias;
} }
if(complementaryDepthBuffer) if(complementaryDepthBuffer)
...@@ -583,7 +583,7 @@ namespace sw ...@@ -583,7 +583,7 @@ namespace sw
data->halfPixelX = replicate(0.5f / W); data->halfPixelX = replicate(0.5f / W);
data->halfPixelY = replicate(0.5f / H); data->halfPixelY = replicate(0.5f / H);
data->viewportHeight = abs(viewport.height); data->viewportHeight = abs(viewport.height);
data->slopeDepthBias = slopeDepthBias; data->slopeDepthBias = context->slopeDepthBias;
data->depthRange = Z; data->depthRange = Z;
data->depthNear = N; data->depthNear = N;
draw->clipFlags = clipFlags; draw->clipFlags = clipFlags;
...@@ -2447,12 +2447,12 @@ namespace sw ...@@ -2447,12 +2447,12 @@ namespace sw
void Renderer::setDepthBias(float bias) void Renderer::setDepthBias(float bias)
{ {
depthBias = bias; context->depthBias = bias;
} }
void Renderer::setSlopeDepthBias(float slopeBias) void Renderer::setSlopeDepthBias(float slopeBias)
{ {
slopeDepthBias = slopeBias; context->slopeDepthBias = slopeBias;
} }
void Renderer::setRasterizerDiscard(bool rasterizerDiscard) void Renderer::setRasterizerDiscard(bool rasterizerDiscard)
......
...@@ -85,7 +85,7 @@ namespace sw ...@@ -85,7 +85,7 @@ namespace sw
state.pointSprite = context->pointSpriteActive(); state.pointSprite = context->pointSpriteActive();
state.cullMode = context->cullMode; state.cullMode = context->cullMode;
state.twoSidedStencil = context->stencilActive() && context->twoSidedStencil; state.twoSidedStencil = context->stencilActive() && context->twoSidedStencil;
state.slopeDepthBias = slopeDepthBias != 0.0f; state.slopeDepthBias = context->slopeDepthBias != 0.0f;
state.vFace = context->pixelShader && context->pixelShader->isVFaceDeclared(); state.vFace = context->pixelShader && context->pixelShader->isVFaceDeclared();
state.positionRegister = Pos; state.positionRegister = Pos;
......
...@@ -95,9 +95,6 @@ namespace sw ...@@ -95,9 +95,6 @@ namespace sw
void setRoutineCacheSize(int cacheSize); void setRoutineCacheSize(int cacheSize);
float depthBias;
float slopeDepthBias;
private: private:
Context *const context; Context *const context;
......
...@@ -95,6 +95,11 @@ namespace sw ...@@ -95,6 +95,11 @@ namespace sw
} }
z[q] = interpolate(x, Dz[q], z[q], primitive + OFFSET(Primitive,z), false, false); z[q] = interpolate(x, Dz[q], z[q], primitive + OFFSET(Primitive,z), false, false);
if(state.depthClamp)
{
z[q] = Min(Max(z[q], Float4(0.0f)), Float4(1.0f));
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment