Repository navigation
Description
Context
The software renderer (gpu.NewSoftwareRenderer, and the device nsgotest uses) does no clipping in clip space. softDrawCtx.rasterTriangle (internal/driver/gpu_software.go:1766) instead:
- drops the whole triangle when any vertex has
w <= 1e-6(gpu_software.go:1767-1770); - divides by
w, maps NDC z through the viewport depth range (toScreen,gpu_software.go:1775-1781), then interpolates screen-space depth per sample asz := w0*z0 + w1*z1 + w2*z2withw2 = 1 - w0 - w1(gpu_software.go:1879-1883,:1903); - rejects the sample when that interpolated, viewport-mapped depth is outside
[0, 1](gpu_software.go:1907-1910).
WebGPU (and Metal, D3D, GL) clip primitives against the clip volume (-w <= x, y <= w, 0 <= z <= w) per vertex before the divide, then map to the viewport depth range. The software renderer differs in three ways:
- Far plane. A triangle whose vertices all sit exactly at the far plane (
z == w, the usualpos.xywwskybox drawn withCompareLessEqualinto a depth buffer cleared to 1.0) should cover every pixel it touches. Here the interpolated depth is computed in float32 from barycentrics that do not sum to exactly 1, so it can come out as1.0000001and the sample is discarded. On amd64 (no FMA contraction ofw0*z0 + w1*z1 + w2*z2) a simulation of that exact expression over random triangles withz0 = z1 = z2 = 1puts about 0.9% of covered samples above 1; on arm64 it is rarer but not zero. The result is scattered clear-color pixels in a skybox, only on the software renderer. - Near plane. A triangle with one vertex behind the eye (
w <= 0) is dropped entirely instead of being clipped, so a large ground plane, road or wall that extends past the camera disappears on the software renderer while every native backend draws its visible part. - Viewport depth range. The range check runs after the viewport mapping, so with
SetViewport(..., minDepth, maxDepth)other than0, 1, it tests the wrong range: withmaxDepth = 0.5, geometry with NDC z in(1, 2]maps into(0.5, 1]and is drawn instead of clipped.
docs/architecture/gpu.md documents WebGPU clip-space conventions (NDC depth in [0, 1]) for every target, the software device included, and does not mention any clipping difference.
Why it matters
The software renderer is what headless tests and CI read pixels from, so a difference here either breaks a correct scene in tests or forces the app to bend its shaders to the software device. A game layer built on NSGo (nsgo-dev/game) hit item 1 with a textbook skybox: its vertex shader now emits z = 1 - 2^-24 instead of w so the software renderer keeps the far-plane fragments, which the native backends do not need. Item 2 affects any first- or third-person scene whose floor or terrain is a few large triangles; a test that reads the floor's pixels sees the clear color.
Proposal
- Clip each triangle in homogeneous clip space against the near plane (
z >= 0, which also removesw <= 0) before the perspective divide, splitting it into up to two triangles with interpolated varyings, the way hardware does. Guard-band x/y and the far plane can stay per-fragment. - Do the far-plane / depth-range test on NDC depth (or against the viewport's
[minDepth, maxDepth]), not on the viewport-mapped value against[0, 1], and tolerate a few ulps of interpolation error: clamp the interpolated depth into the viewport range when every vertex is inside the clip volume, or interpolate depth with a form whose weights sum to exactly 1 (for examplez0 + w1*(z1-z0) + w2*(z2-z0)). - Clamp the final fragment depth to
[minDepth, maxDepth]as WebGPU specifies, which also replaces theclamp01after depth bias (gpu_software.go:1904-1906,:1942-1944).
Acceptance
- A full-target triangle (or quad) whose vertices are all at
z == w, drawn withCompareLessEqualinto a depth buffer cleared to 1.0, covers every pixel on the software renderer, at several sizes and on amd64 and arm64. - A triangle with one vertex behind the eye (
w < 0) and its visible part in front of the near plane draws that visible part, with the same pixels (within tolerance) as a reference made of the pre-clipped triangle. - With a viewport depth range of
[0, 0.5], geometry beyond the far plane is clipped and visible geometry writes depth in[0, 0.5]. - Existing software renderer pixel tests (depth-tested triangles, depth bias, MSAA) still pass.
Activity