Skip to content

gpu: the software renderer does not clip in clip space, so far-plane fragments are lost to rounding and triangles crossing the near plane vanish #2344

Backlog

Description

@houko

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 as z := w0*z0 + w1*z1 + w2*z2 with w2 = 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:

  1. Far plane. A triangle whose vertices all sit exactly at the far plane (z == w, the usual pos.xyww skybox drawn with CompareLessEqual into 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 as 1.0000001 and the sample is discarded. On amd64 (no FMA contraction of w0*z0 + w1*z1 + w2*z2) a simulation of that exact expression over random triangles with z0 = z1 = z2 = 1 puts 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.
  2. 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.
  3. Viewport depth range. The range check runs after the viewport mapping, so with SetViewport(..., minDepth, maxDepth) other than 0, 1, it tests the wrong range: with maxDepth = 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 removes w <= 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 example z0 + w1*(z1-z0) + w2*(z2-z0)).
  • Clamp the final fragment depth to [minDepth, maxDepth] as WebGPU specifies, which also replaces the clamp01 after depth bias (gpu_software.go:1904-1906, :1942-1944).

Acceptance

  • A full-target triangle (or quad) whose vertices are all at z == w, drawn with CompareLessEqual into 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions