Skip to content

Rasterize the visibility of the view and draw its material once a pixel - #114

Open
FramaLuna wants to merge 4 commits into
mainfrom
raster-visibility
Open

FramaLuna wants to merge 4 commits into
mainfrom
raster-visibility

Conversation

@FramaLuna

@FramaLuna FramaLuna commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

The G-buffer rasterizes the visibility of the view and draws its material once a pixel. A forest of triangles smaller than a pixel costs a quarter of what it did; scenes of large triangles keep their cost.

Why

Measured on a GTX 1070 with a forest of 12.6M visible triangles, splitting the G-buffer pass by shader variants:

G-buffer of the trees, ms
As it was (material, every attribute) 47.8
No material, same vertices 30.1
Fragment that reads only the instance 7.7
Rasterizing and writing the pixels alone ~1

The rasterizer costs little. The cost was the vertex shader running three times a triangle (non-indexed clusters) and computing and passing every attribute (normal, tangent, UVs, colors, previous position, material), then the material per fragment. When the fragment reads only ids, the driver drops the rest.

Changes

  • Visibility pass (visibility_pass, visibility.vert/.frag): each phase draws the visible clusters with vertices that hand the fragment only the instance, the cluster and the triangle. Each pixel keeps its nearest triangle (RG32: instance, primitive) and its linear depth, which the depth pyramid between the phases reads. Fades and masks are decided there, the mask sampled as the rasterizer would.
  • Material once a pixel (gbuffer_visible.frag): after both phases, it rebuilds what the rasterizer would have handed gbuffer.frag at each pixel (visible_input.slang): each attribute of gbuffer.vert interpolated where the ray of the pixel crosses the triangle, with the derivatives taken from the triangle at the next pixels (MATERIAL_GRADIENTS: SampleGrad, footprint, specular antialiasing). Then it runs the same body as gbuffer.frag (gbuffer_fragment.slang).
  • Visible list entries (visible_entry): they carry where the triangles and the posed vertices of a cluster are, so a vertex reads its corner without going through the mesh and its pages.
  • Large triangles stay rasterized: a cluster whose triangles are not small (VISIBILITY_TRIANGLE_PIXELS over the pixels across its box at rest) goes to bin 0 of the program list, like Surface programs. Before the visibility, these clusters leave their depth with the vertices of their G-buffer and its fade and mask (gbuffer_depth.frag, one per program). After it, they draw gbuffer.frag with the depth test ahead of the stage (after variants, [earlydepthstencil]; the texture feedback writes had kept early-Z off). Each pixel is shaded once.
  • A view with a clip plane keeps gbuffer_pass.
  • The demo has the forest it was measured in, foliage_extreme (842 trees, no impostors), with the foliage plugin on.
  • frame-graph.md and scene.md describe the passes.

Measurements

mirai bench compare, 5 alternating pairs, 1440x900, dev, GTX 1070, against main at 623c6a8. The forest is foliage_extreme (842 trees, 27M triangles at level 0, no impostors), which this PR adds to the demo; for the base it was copied into a checkout of main with the foliage plugin on.

View main this branch Δ
Forest, ground (frame p50) 82.42 ms 20.52 ms -75.1 %
Forest, above the canopy 80.97 ms 22.86 ms -71.8 %
Gallery main_vitral 6.43 ms 6.43 ms -0.1 %

On the forest, the G-buffer goes from 79.2 to 4.6 ms, plus 13.0 ms of visibility. On the gallery, it goes from 0.72 to 0.47 ms, plus 0.09 ms of visibility. Captures against main: SSIM 0.99988 to 0.99998 on the forest, 0.9999 on the gallery, 1.0 on control.

mirai render check demo --base main (dev, GTX 1070, 1440x900): No regressions. Frame p50, ms:

View main this branch Stability (luma MAE)
night 3.73 3.74 0.003 → 0.003
night_wall 3.33 3.43 0.032 → 0.032
night_lights 4.49 4.36 0.018 → 0.018
control 3.74 3.67 0.020 → 0.020
main_vitral 9.80 9.66 0.043 → 0.041
main_wall 8.00 7.60 0.017 → 0.017
church 12.59 12.02 0.081 → 0.068
church_push 12.59 12.00 0.053 → 0.057
portal_chamber 11.52 11.46 0.026 → 0.027
showcase 3.14 3.10 0.011 → 0.010
portal_lab 9.52 9.44 0.042 → 0.035
sky_sunset / away / high 2.26 / 2.53 / 2.85 2.32 / 2.57 / 2.91 unchanged
raft (4 views) 11.04–11.07 11.19–11.32 unchanged

The luminance of every view is unchanged. The scenes of large triangles pay up to ~0.3 ms for the passes of the visibility (raft, sky); the ones with more geometry gain up to 5 %. 18 of the base measures came from the saved base of 623c6a8.

Tests

  • mirai check render; mirai test cook/shaders: 83/83 (the per-program stages of the new gbuffer and gbuffer_depth variants).
  • mirai test render with validation: 284/284. gpu_skins_with_influences_of_every_level… now finds the chosen level in the list it went to: its cluster of two large triangles is listed in bin 0.
  • check_docs.py did not run: the uv of this machine is too old for it. I checked the lines of the docs by hand.
  • Not run: Metal ([earlydepthstencil], the static gradients of MATERIAL_GRADIENTS). On a Mac: mirai test render and a capture of gallery/main.scene against main.

Known differences

  • Specular antialiasing: a pixel drawn from the visibility gets the spread of the normal of its surface, without that of its normal map, which ddx(n) of the mapped normal had.

Known issue in the forest (of main, left for a separate fix)

Opening foliage_extreme with Light probes on Medium or High resets the GPU driver on a GTX 1070 (Windows TDR; the editor reports device lost … the GPU stopped in ddgi, with the pipeline Ddgi_Trace). On Low it works. It does not come from this PR: the probe volume and its rays are the code of main.

  • The frame that shows a world anew updates the probe volume GI_WARM_UPDATES (64) times, up to PROBE_WARM_BUDGET (2048) probes each, all in one submission (pass_world_lighting). In this forest that frame took 2.05 s of GPU on Low (ddgi 2047 ms, profile.capture from [30,4,70] toward [0,7,20]), and that with the probe rays already through coarser cuts of the trees (a trial not in this PR); Windows resets a GPU that runs one submission past 2 s. Medium and High trace 1.5x and 4x the rays.
  • Its rays are expensive: the tree meshes are of whole levels (pruned foliage), which mesh_ray_errors gives no ray cuts, and batches trace the cut they always have (ray_cut_always, level 0), so every probe ray meets ~32k triangles a tree.
  • Once the volume is warm, a frame on Medium costs 7.8 ms of ddgi there.
  • To bump IMPORTER_VERSION while trying a fix, use a number no other branch uses: the mesh cache is shared by every checkout of the clone, and foliage-perf and vis-buffer already use 12, so an import there reads their meshes.

🤖 Generated with Claude Code

FramaLuna and others added 4 commits November 18, 2026 02:15
Each phase of the G-buffer now draws the visibility of the view: the
vertices of a visible cluster hand the fragment only the instance, the
cluster and the triangle, and each pixel keeps the nearest one. After both
phases, gbuffer_visible.frag rebuilds what the rasterizer would have handed
gbuffer.frag at each pixel from its triangle, with the derivatives taken from
the triangle at the next pixels (MATERIAL_GRADIENTS), and runs the same
material and targets once. A forest of triangles smaller than a pixel spent
its time computing and passing every attribute of three vertices a triangle
and shading every fragment; the rasterizer itself costs little.

The entries of the visible lists carry where the triangles and the posed
vertices of a cluster are (visible_entry), which shortens the reads of the
vertex. Clusters whose triangles are not small go to bin 0 of the program
list, as those of Surface programs do: they leave their depth before the
visibility and draw their G-buffer after it with the depth test ahead of the
stage, so each pixel is shaded once and scenes of large triangles keep their
cost. The visibility writes the linear depth the pyramid between the phases
reads, and decides masks as the rasterizer samples them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A cluster of large triangles is listed in bin 0 of the program list, one of
small ones in the visible list: the test of the posed levels reads both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The clusters of the program list leave their depth before the visibility
with gbuffer.vert and a stage that decides their fade and their mask as
gbuffer.frag does (gbuffer_depth.frag, one per Surface program), so the
depth their G-buffer tests for equality after it is the same, whatever the
device computes, and the mask of a program is its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
foliage_extreme: 842 trees of three kinds on a forest floor, without
impostors, 27M triangles at level 0, with the foliage plugin on in the demo.
With light probes on Medium or High the frame that shows it anew runs the
warm-up of the probe volume past what Windows lets one submission run, and
the driver resets; the pull request describes it for a separate fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@5hif7y

5hif7y commented Nov 18, 2026

Copy link
Copy Markdown
Member

Looks really heavy but it is only

Odin: +135-20
Slang: +425-96

diff lines at 4a70663

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants