js-logarithmic-depth-buffer-z-precision
Done. Solution is implemented, verified end-to-end on Node 20, and documented in ~/SOLUTION.md.
Root cause: The standard perspective depth mapping d_std(z) = far·(z−near)/(z·(far−near)) has derivative ∝ 1/z², concentrating all depth-buffer precision next to the near plane. At distance the value asymptotes to 1, so nearly-identical distant depths quantize into the same depth-buffer bin → z-fighting. With near = 0 it collapses entirely (d_std(z) ≡ 1 for all z).
Fix (~/logdepth.js): replace the algebraic mapping with a logarithmic one:
- near > 0: d_log(z) = log₂(z/near) / log₂(far/near) — derivative ∝ 1/z, i.e. constant relative precision across the whole frustum, mapping [near, far] → [0, 1] bijectively.
- near = 0 (degenerate): pivot to the +1-offset form log₂(z+1)/log₂(far+1), which is finite at z = 0 (→ 0) and still maps z = far → 1 while retaining the log precision profile.
Verification (node ~/test-logdepth.js, all assertions pass):
- z=950000 vs z=950010 (near=0.1, far=1e6): standard mapping → same 24-bit bin 16777214 (collapsed, z-fighting); log mapping → distinct bins 16723824 vs 16723835. Same result in float32.
- near=0: endpoints exact (z=0→0, z=far→1), monotone over 4096 samples, z=990 vs z=991 still distinct — while standard collapses to constant 1.
- Resolution at z=950000: 1.86e-6 std bins/unit vs 1.10e+0 log bins/unit — ~6 orders of magnitude more resolvable steps at distance (a 10-unit step spans 0.000019 std bins vs 10.96 log bins).
The markdown is self-contained: title, root-cause analysis with the math, the exact code, usage (including NDC [−1,1] scaling), and the captured test output.
# Evidence - Problem class: js-logarithmic-depth-buffer-z-precision - Model: openrouter/deepseek/deepseek-v4-flash-0731 - Solved: 2026-08-31T16:47:19.447Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a logarithmic depth-buffer remapping for a rasterizer to eliminate z-fighting: encode view-space depth into a logarithmic clip-space value that preserves precision at large distances, replacing standard near/far perspective mapping. Handle the degenerate near=0 case and verify that two nearly-identical distant depths map to distinct encodings. Return the encoded depth for a given view-space z and near/far planes.", "environment": "node20", "language": "js", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "js-logarithmic-depth-buffer-z-precision", "provider": "openrouter", "solved_at": "2026-08-31T16:47:19.447Z", "version": "20"}Done. Solution is implemented, verified end-to-end on Node 20, and documented in ~/SOLUTION.md.
Root cause: The standard perspective depth mapping d_std(z) = far·(z−near)/(z·(far−near)) has derivative ∝ 1/z², concentrating all depth-buffer precision next to the near plane. At distance the value asymptotes to 1, so nearly-identical distant depths quantize into the same depth-buffer bin → z-fighting. With near = 0 it collapses entirely (d_std(z) ≡ 1 for all z).
Fix (~/logdepth.js): replace the algebraic mapping with a logarithmic one:
- near > 0: d_log(z) = log₂(z/near) / log₂(far/near) — derivative ∝ 1/z, i.e. constant relative precision across the whole frustum, mapping [near, far] → [0, 1] bijectively.
- near = 0 (degenerate): pivot to the +1-offset form log₂(z+1)/log₂(far+1), which is finite at z = 0 (→ 0) and still maps z = far → 1 while retaining the log precision profile.
Verification (node ~/test-logdepth.js, all assertions pass):
- z=950000 vs z=950010 (near=0.1, far=1e6): standard mapping → same 24-bit bin 16777214 (collapsed, z-fighting); log mapping → distinct bins 16723824 vs 16723835. Same result in float32.
- near=0: endpoints exact (z=0→0, z=far→1), monotone over 4096 samples, z=990 vs z=991 still distinct — while standard collapses to constant 1.
- Resolution at z=950000: 1.86e-6 std bins/unit vs 1.10e+0 log bins/unit — ~6 orders of magnitude more resolvable steps at distance (a 10-unit step spans 0.000019 std bins vs 10.96 log bins).
The markdown is self-contained: title, root-cause analysis with the math, the exact code, usage (including NDC [−1,1] scaling), and the captured test output.
# Evidence - Problem class: js-logarithmic-depth-buffer-z-precision - Model: openrouter/deepseek/deepseek-v4-flash-0731 - Solved: 2026-08-31T16:47:19.447Z - Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement a logarithmic depth-buffer remapping for a rasterizer to eliminate z-fighting: encode view-space depth into a logarithmic clip-space value that preserves precision at large distances, replacing standard near/far perspective mapping. Handle the degenerate near=0 case and verify that two nearly-identical distant depths map to distinct encodings. Return the encoded depth for a given view-space z and near/far planes.", "environment": "node20", "language": "js", "model": "openrouter/deepseek/deepseek-v4-flash-0731", "problem_class": "js-logarithmic-depth-buffer-z-precision", "provider": "openrouter", "solved_at": "2026-08-31T16:47:19.447Z", "version": "20"}