perf(render3d): a depth prepass for the near tree foliage

Needle-card crowns are many cut-out quads deep and a discarding shader turns early depth
rejection off, so every card behind the front one ran the full lighting shader. The near
tree LODs now write depth first (depth.frag, the same alpha coverage test), terrain under
them is rejected before shading, and the lit pass draws them with no discard and an equal
depth test (invariant gl_Position). R3D_NOPREPASS=1 restores the old path.

Dense forest on a Windows PC: 61 -> 78 fps at 3840x2160, 116 -> 164 fps at 1920x1080.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-13 04:18:38 +03:00
parent 21bc611455
commit d6ffdfa638
6 changed files with 123 additions and 1 deletions

View file

@ -0,0 +1,23 @@
// depth.frag — the foliage depth prepass (scatter_draw_depth, render.ludic).
//
// A crown of needle cards is many cut-out quads deep from any angle. Lit directly, every
// card behind the front one runs the whole lighting shader and is thrown away afterwards,
// because a shader that can discard switches early depth rejection off. So the near tree
// LODs first write depth with this: the same alpha coverage test the lit shader applies,
// and nothing else. The lit pass then draws them with no discard and an equal depth test
// (model.frag's EQ_PASS), and each pixel of foliage is lit once.
//
// The test must match model.frag's ALPHA_TEST exactly - a texel this pass keeps and that
// one would have cut is drawn solid - so it is the same formula, taken at coverage 0.5
// because without MSAA a fragment is either there or not.
in vec3 v_wpos;
in vec2 v_uv;
uniform sampler2D u_diff;
void main() {
if (v_wpos.y < u_clip_y) discard;
vec4 d = texture(u_diff, v_uv);
float lod = textureQueryLod(u_diff, v_uv).x;
float a = min(d.a * (1.0 + 0.45 * max(lod, 0.0)), 1.0);
float cov = (a - 0.4) / max(fwidth(a), 1e-4) + 0.5;
if (cov < 0.5) discard;
}

View file

@ -32,7 +32,12 @@ mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv) {
}
void main() {
#ifndef EQ_PASS
// (the EQ_PASS variant draws over the foliage prepass: any discard at all would switch
// early depth rejection off and bring the overdraw back, and the prepass has already
// cut both the clip plane and the alpha)
if (v_wpos.y < u_clip_y) discard;
#endif
vec3 N = normalize(v_nrm);
if (!gl_FrontFacing && v_hull >= 0.0) N = -N;
#ifdef CARD

View file

@ -40,6 +40,10 @@ float heightSmooth(sampler2D tex, vec2 uv) {
+ (texture(tex, vec2(o0.x, o1.y)).r * s0.x + texture(tex, vec2(o1.x, o1.y)).r * s1.x) * s1.y;
}
out vec3 v_wpos;
// The foliage prepass (depth.frag) and the lit pass after it compile this same source
// into two programs and compare depths for equality: the position must come out
// bit-identical in both, which is what `invariant` promises.
invariant gl_Position;
out vec3 v_nrm;
out vec2 v_uv;
out float v_seed;