1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-07-04 10:03:36 +00:00

Fix depth calculation.

Problem:
N64 formula for screen z is screenZ = z/w*viewport.vscale[2]+viewport.vtrans[2]
OpenGL uses z/w, vscale and vtrans are set in glViewport and they always equal.
viewport.vscale[2] can be not equal to viewport.vtrans[2] and in that case
standard OpenGL z compare does not work correct.

Solution:
use N64 formula for screenZ in fragment shader.

Fixed Twine: wrong depth for the arm #115
This commit is contained in:
Sergey Lipskiy 2015-03-05 19:44:38 +06:00
parent c22de119e5
commit 33806e7170
4 changed files with 14 additions and 17 deletions

View File

@ -938,14 +938,12 @@ bool DepthBufferToRDRAM::CopyToRDRAM( u32 _address) {
for (u32 y = 0; y < VI.height; ++y) {
for (u32 x = 0; x < VI.width; ++x) {
f32 z = ptr_src[x + (VI.height - y - 1)*VI.width];
if (z == 1.0f)
ptr_dst[(x + y*VI.width) ^ 1] = zLUT[0x3FFFF];
else {
z = z*2.0f - 1.0f;
z = (z*scale + trans) * 8.0f;
const u32 idx = min(0x3FFFFU, u32(floorf(z + 0.5f)));
ptr_dst[(x + y*VI.width) ^ 1] = zLUT[idx];
u32 idx = 0x3FFFF;
if (z < 1.0f) {
z *= 262144.0f;
idx = min(0x3FFFFU, u32(floorf(z + 0.5f)));
}
ptr_dst[(x + y*VI.width) ^ 1] = zLUT[idx];
}
}

View File

@ -682,6 +682,7 @@ void ShaderCombiner::_locateUniforms() {
LocateUniform(uTexScale);
LocateUniform(uScreenScale);
LocateUniform(uDepthScale);
LocateUniform(uTexOffset[0]);
LocateUniform(uTexOffset[1]);
LocateUniform(uTexMask[0]);
@ -922,6 +923,8 @@ void ShaderCombiner::updateFBInfo(bool _bForce) {
}
void ShaderCombiner::updateDepthInfo(bool _bForce) {
_setFV2Uniform(m_uniforms.uDepthScale, gSP.viewport.vscale[2], gSP.viewport.vtrans[2], _bForce);
if (!video().getRender().isImageTexturesSupported())
return;
@ -991,9 +994,6 @@ void SetDepthFogCombiner()
int loc = glGetUniformLocation(g_draw_shadow_map_program, "uFogColor");
if (loc >= 0)
glUniform4fv(loc, 1, &gDP.fogColor.r);
loc = glGetUniformLocation(g_draw_shadow_map_program, "uDepthScale");
if (loc >= 0)
glUniform2f(loc, gSP.viewport.vscale[2] * 32768.0f, gSP.viewport.vtrans[2] * 32768.0f);
gDP.changed |= CHANGED_COMBINE;
}

View File

@ -47,7 +47,7 @@ private:
fv4Uniform uEnvColor, uPrimColor, uFogColor, uCenterColor, uScaleColor, uBlendColor;
fv2Uniform uTexScale, uScreenScale, uTexOffset[2], uTexMask[2],
fv2Uniform uTexScale, uScreenScale, uDepthScale, uTexOffset[2], uTexMask[2],
uCacheShiftScale[2], uCacheScale[2], uCacheOffset[2];
fv3Uniform uLightDirection[8], uLightColor[8];

View File

@ -138,6 +138,7 @@ static const char* fragment_shader_header_common_variables =
"uniform lowp int uFb8Bit; \n"
"uniform lowp int uFbFixedAlpha;\n"
"uniform lowp int uSpecialBlendMode;\n"
"uniform mediump vec2 uDepthScale; \n"
"in lowp vec4 vShadeColor; \n"
"in mediump vec2 vTexCoord0;\n"
"in mediump vec2 vTexCoord1;\n"
@ -275,6 +276,7 @@ static const char* fragment_shader_header_main =
" \n"
"void main() \n"
"{ \n"
" gl_FragDepth = clamp((gl_FragCoord.z * 2.0 - 1.0) * uDepthScale.s + uDepthScale.t, 0.0, 1.0); \n"
" if (uAlphaCompareMode == 3) {//dither \n"
" if (snoise() < 0.0) discard; \n"
" } \n"
@ -568,12 +570,12 @@ static const char* depth_compare_shader_float =
" ivec2 coord = ivec2(gl_FragCoord.xy); \n"
" highp vec4 depth = imageLoad(uDepthImage,coord); \n"
" highp float bufZ = depth.r; \n"
" highp float curZ = gl_FragCoord.z; \n"
" highp float curZ = gl_FragDepth; \n"
" highp float dz, dzMin; \n"
" if (uDepthSource == 1) { \n"
" dzMin = dz = uDeltaZ; \n"
" } else { \n"
" dz = 4.0*fwidth(gl_FragCoord.z); \n"
" dz = 4.0*fwidth(gl_FragDepth); \n"
" dzMin = min(dz, depth.g); \n"
" } \n"
" const bool bInfront = curZ < bufZ; \n"
@ -622,15 +624,12 @@ static const char* shadow_map_fragment_shader_float =
"layout(binding = 0, r16ui) uniform readonly uimage2D uZlutImage;\n"
"layout(binding = 1, r16ui) uniform readonly uimage1D uTlutImage;\n"
"uniform lowp vec4 uFogColor; \n"
"uniform mediump vec2 uDepthScale; \n"
"out lowp vec4 fragColor; \n"
"lowp float get_alpha() \n"
"{ \n"
" ivec2 coord = ivec2(gl_FragCoord.xy); \n"
" highp float bufZ = texelFetch(uDepthImage,coord, 0).r; \n"
" highp float fZ = 2.0*bufZ - 1.0; \n"
" fZ = (uDepthScale.x*fZ + uDepthScale.y)*8.0; \n"
" const highp int iZ = int(floor(fZ + 0.5)); \n"
" const highp int iZ = bufZ > 0.999 ? 262143 : int(floor(bufZ * 262143.0));\n"
" int y0 = clamp(iZ/512, 0, 511); \n"
" int x0 = iZ - 512*y0; \n"
" uint iN64z = imageLoad(uZlutImage,ivec2(x0,y0)).r; \n"