Skip to content

Commit

Permalink
gpu: reverse z
Browse files Browse the repository at this point in the history
This changes the projection to (in row-major order):

2/w      0     0      0
0     -2/h     0      0
0        0     0     2n
0        0     1      0

Which changes the z calculation from (z-100)/z to 100/z.
  • Loading branch information
Adam- committed Dec 21, 2023
1 parent 6518013 commit 31b149c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public static float[] projection(float w, float h, float n)
{
2 / w, 0, 0, 0,
0, -2 / h, 0, 0,
0, 0, 1, 1,
0, 0, -2 * n, 0
0, 0, 0, 1,
0, 0, 2 * n, 0
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*/
#version 330

//#define FRAG_UVS
#define FRAG_UVS
//#define ZBUF

uniform sampler2DArray textures;
uniform float brightness;
Expand All @@ -38,12 +39,23 @@ noperspective centroid in float fHsl;
flat in int fTextureId;
in vec2 fUv;
in float fFogAmount;
#ifdef ZBUF
in float fDepth;
#endif

out vec4 FragColor;

#include "hsl_to_rgb.glsl"
#include "colorblind.glsl"

#ifdef ZBUF
float linear_depth(float depth) {
// depth is computed as 100/z, solve for z
float z = 100 / depth;
return 1 - z / 10000; // we don't have a far plane, but the client uses 10000
}
#endif

void main() {
vec4 c;

Expand All @@ -68,13 +80,24 @@ void main() {
}

vec3 mixedColor = mix(c.rgb, fogColor.rgb, fFogAmount);
FragColor = vec4(mixedColor, c.a);

#ifdef FRAG_UVS
if (fTextureId > 0) {
FragColor = vec4(fUv.x, 0, fUv.y, 1);
} else {
}
#endif
FragColor = vec4(mixedColor, c.a);
#ifdef FRAG_UVS

#ifdef ZBUF
float dc = linear_depth(fDepth);
if (dc > 1.0) {
FragColor = vec4(1, 0, 0, 1);
} else if (dc < -1.0) {
FragColor = vec4(0, 0, 1, 1);
} else if (dc < 0.0) {
FragColor = vec4(0, 1, 0, 1);
} else {
FragColor = vec4(dc, dc, dc, 1);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#version 330

//#define ZBUF

// smallest unit of the texture which can be moved per tick. textures are all
// 128x128px - so this is equivalent to +1px
#define TEXTURE_ANIM_UNIT (1.0f / 128.0f)
Expand Down Expand Up @@ -62,6 +64,9 @@ noperspective centroid out float fHsl;
flat out int fTextureId;
out vec2 fUv;
out float fFogAmount;
#ifdef ZBUF
out float fDepth;
#endif

void main() {
int textureId = gTextureId[0];
Expand All @@ -87,7 +92,13 @@ void main() {
fTextureId = gTextureId[i];
fUv = uv[i];
fFogAmount = gFogAmount[i];
gl_Position = projectionMatrix * vec4(gVertex[i], 1);

vec4 pos = projectionMatrix * vec4(gVertex[i], 1);
#ifdef ZBUF
fDepth = pos.z / pos.w;
#endif
gl_Position = pos;

EmitVertex();
}

Expand Down

0 comments on commit 31b149c

Please sign in to comment.