Simplify depth calculation a bit.

This way we keep the original range better, so we should lose less
precision.
This commit is contained in:
Unknown W. Brackets 2016-01-17 18:30:58 -08:00
parent 14fb4f2c22
commit 0828721bc2
2 changed files with 17 additions and 23 deletions

View File

@ -634,32 +634,27 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
float vpZScale = gstate.getViewportZScale();
float vpZCenter = gstate.getViewportZCenter();
float depthRangeMin = vpZCenter - vpZScale;
float depthRangeMax = vpZCenter + vpZScale;
// Near/far can be inverted. Let's reverse while dealing with clamping, though.
bool inverted = vpZScale < 0.0f;
float near = (inverted ? depthRangeMax : depthRangeMin) * (1.0f / 65535.0f);
float far = (inverted ? depthRangeMin : depthRangeMax) * (1.0f / 65535.0f);
// Near/far can be inverted. We deal with that in the projection/scale.
float near = vpZCenter - fabsf(vpZScale);
float far = vpZCenter + fabsf(vpZScale);
if (near < 0.0f || far > 1.0f) {
if (near < 0.0f || far > 65535.0f) {
float overageNear = std::max(-near, 0.0f);
float overageFar = std::max(far - 1.0f, 0.0f);
float overageFar = std::max(far - 65535.0f, 0.0f);
float drift = overageFar - overageNear;
near += overageNear;
far -= overageFar;
zScale = fabsf(vpZScale * (2.0f / 65535.0f)) / (far - near);
zScale = (vpZScale * 2.0f) / (far - near);
zOffset = drift / (far - near);
}
if (inverted) {
} else if (vpZScale < 0.0f) {
// This flips to match our near/far.
zScale = -zScale;
inverted = false;
}
out.depthRangeMin = inverted ? far : near;
out.depthRangeMax = inverted ? near : far;
out.depthRangeMin = near * (1.0f / 65535.0f);
out.depthRangeMax = far * (1.0f / 65535.0f);
bool scaleChanged = gstate_c.vpWidthScale != wScale || gstate_c.vpHeightScale != hScale;
bool offsetChanged = gstate_c.vpXOffset != xOffset || gstate_c.vpYOffset != yOffset;
@ -678,12 +673,11 @@ void ConvertViewportAndScissor(bool useBufferedRendering, float renderWidth, flo
#ifndef MOBILE_DEVICE
float minz = gstate.getDepthRangeMin();
float maxz = gstate.getDepthRangeMax();
if ((minz > depthRangeMin && minz > depthRangeMax) || (maxz < depthRangeMin && maxz < depthRangeMax)) {
WARN_LOG_REPORT_ONCE(minmaxz, G3D, "Unsupported depth range in test - depth range: %f-%f, test: %f-%f", depthRangeMin, depthRangeMax, minz, maxz);
} else if ((gstate.clipEnable & 1) == 0) {
// TODO: Need to test whether clipEnable should even affect depth or not.
if ((minz < depthRangeMin && minz < depthRangeMax) || (maxz > depthRangeMin && maxz > depthRangeMax)) {
WARN_LOG_REPORT_ONCE(znoclip, G3D, "Unsupported depth range in test without clipping - depth range: %f-%f, test: %f-%f", depthRangeMin, depthRangeMax, minz, maxz);
if (minz > near || maxz < far) {
if ((gstate.clipEnable & 1) == 0) {
WARN_LOG_REPORT_ONCE(minmaxznoclip, G3D, "Unsupported depth range test without clipping - clip: %f-%f, test: %f-%f", near, far, minz, maxz);
} else {
WARN_LOG_REPORT_ONCE(minmaxz, G3D, "Unsupported depth range test - clip: %f-%f, test: %f-%f", near, far, minz, maxz);
}
}
#endif

View File

@ -604,8 +604,8 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid) {
// The projection already accounts for those, so we need to reverse them.
//
// Additionally, OpenGL uses a range from [-1, 1]. So we multiply by scale and add the center.
viewZScale *= (1.0f / gstate_c.vpDepthScale);
viewZCenter -= 65535.0f * (gstate_c.vpZOffset);
viewZScale *= 1.0f / gstate_c.vpDepthScale;
viewZCenter -= 65535.0f * gstate_c.vpZOffset;
if (viewZScale != 0.0) {
viewZInvScale = 1.0f / viewZScale;