gpu: cast a shadow with a translated depth pass and SampleCmpLevelZero #767

Merged
roansystemlabs merged 1 commit from upstream-pr/773 into main 2026-11-23 02:24:13 +00:00
roansystemlabs commented 2026-11-23 02:08:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by roansystemlabs on 2026-11-23T02:08:45Z.


Change

Step 2 of #730: the standalone shadow fixture, outside the EXE path. Refs #730.

It builds on the lit fixture from #749, now in main, and reuses its N·L pixel program and reference helpers.

tests/mesh_shadow_check.py draws a small scene in two passes on a 64×64 target, through a new offscreen_check --mesh-shadow operation (tests/mesh_shadow_draw.cpp):

  • Scene: an orthographic camera with depth z, and a directional light that projects (x, y, z) to (x − z/2, y + z/2) with depth z. A triangle caster stands at depth 1/4 in front of a receiver plane at depth 3/4. A second triangle at depth 7/8, behind the plane, is drawn only into the light pass. Every vertex lies on the pixel grid in both views, so each receiver pixel center samples a texel center of the map.
  • Light pass: the indexed PARAM0 mesh program from #546, with the export order of the textured fixture, writes light-space depth into an R32_TYPELESS texture through a D32_FLOAT view (LESS, cleared to 1). The pipeline carries the caster's pixel program with color writes masked. On WARP (10.0.26100.9818), the same light pass without a pixel shader removed the device (DXGI_ERROR_DEVICE_REMOVED at the first readback map, every run) with no debug-layer message, both with the color target bound and with no render target, and also with the mesh trace writes compiled out. The RTX 3070 drew both variants with zero mismatches. Keeping the pixel program with color writes masked avoids that and keeps the debug layer quiet; it does not change the depth the pass writes. I did not reduce it to a repro outside this fixture.
  • Camera pass: the plane's source-built gfx1013 pixel program interpolates the map coordinate from attr0.xy, loads its light-space depth from b0 at byte 0x90 and runs image_sample_c_lz (point, LESS, CLAMP_LAST_TEXEL, TRUNC_COORD 1). That lowers through the #718 path to SampleCmpLevelZero on an R32_FLOAT view. The host's static comparison sampler is built from the decoded descriptor words. The plane exports 1/4 + result/2 in RG and half of that in B. Then the caster is drawn at depth 1/4 with the #749 N·L program, which reads its normal from attr0.xyz. Both camera draws are depth tested.
  • Reference: Python rasterizes both views exactly with Fraction and asserts that no pixel or texel center lies on an edge. It builds the shadow map with the depth test, applies the comparison and edge clamping at each plane pixel, evaluates N·L at each caster pixel, then applies the binary16 export and the 8-bit target. No D3D12 code is involved. Every RGBA byte, every texel of the current map and the mesh export trace of all five draws are compared.

Values are kept away from every boundary by construction, following #749. Map coordinates are texel centers, half a texel from any edge. The plane depth 3/4 is never equal to a stored depth (1/4, 7/8 or 1), and the comparison result is exactly 0 or 1, so the plane colors are exact binary16 values. For the caster, the normal's z is 17/32, so N·L is an odd multiple of 1/128 at every pixel center and no result exports as 1/2, where the 8-bit conversion would round a half. The ambient term is 1/4 + 2^-17, as in #749, so each caster value sits at least 2^-17 above a binary16 value, far beyond the 2^-23 interpolation error seen on hardware. At every pixel the reference asserts that no value is a binary16 value and no 8-bit conversion rounds a half.

C++: one new file and one row in the offscreen_check operation table. The existing mesh renderer, the translators (pixel_aot.py, mesh_entry_aot.py) and the compute files are untouched. Also included: one CMake test (mesh_shadow_draw_{hardware,warp}), one line in the Ubuntu LLVM 15 job, a subsection in docs/experiments.md after the lit fixture, and a reference image (scene and light-space depth).

Verification

Commit 2192cbe on main 761e323. Windows 11 build 26200.9974, D3D12 debug layer enabled, NVIDIA GeForce RTX 3070 (driver 621.01, 32.0.16.2101) and WARP (10.0.26100.9818).

ctest --preset dev --no-tests=error -R "^(mesh_|compute_mesh_texture_|offscreen_warp|pixel_aot_compare_)" -V
100% tests passed, 0 tests failed out of 25
ctest --preset release --no-tests=error -R "^mesh_(shadow|lit)_draw_" -V
100% tests passed, 0 tests failed out of 4

On both adapters, mesh_shadow_draw gives pixels=4096 mismatches=0 and shadow_map_texels=4096 shadow_map_mismatches=0, with 342 plane pixels in shadow. The current map holds 576 texels at 1/4, 176 at 7/8 and 3,344 at 1. The readback is byte-identical between the two adapters. All 323 debug-layer lines in the dev run report debug_errors=0 debug_warnings=0. No capability skip on this machine. On an adapter without mesh shaders the draw returns 77 like the other mesh_* tests. I couldn't test that here.

Negative controls, each against the base reference unless stated:

Control What changes Result on RTX 3070 and WARP
GREATER for LESS Program translated from GREATER descriptor words, host sampler from the same words mismatches=3520: every plane pixel flips, the 576 caster pixels match. The draw matches its own GREATER reference.
Stale map The camera pass binds the map drawn, in the same command list, for a previous light (x + z/2, y + z/2) mismatches=356. It matches its own reference for that map.
Ignored map The plane program exports the lit color without sampling mismatches=342, the shadowed pixels. It matches its own unshadowed reference.
Coverage instead of depth A reference where any texel a caster wrote shadows, whatever its depth mismatches=52: the plane pixels that sample the triangle behind the plane.
Map without the second triangle The expected map lacks the triangle behind the plane shadow_map_mismatches=176

The test checks each count, the matching message on stderr and a clean debug layer.

Binary check (LLVM 15). python3 -B tests/mesh_shadow_check.py binary ... assembles the mesh program and both pixel programs, wraps each pixel program in a container, decodes it through native_program and disassemble, and requires the same translation as the source text. The fork's Actions were unavailable (HTTP 422 on workflow run), so I ran it on Windows with the LLVM 15.0.7 release, as for #749: the clang 15 integrated assembler stood in for the missing llvm-mc, with llvm-objcopy and LLVM-C.dll from the same release. It passed (receiver 132 bytes, caster 112 bytes), as did mesh_lit_check.py binary and mesh_textured_check.py binary. The Ubuntu job in this PR runs the same check with llvm-mc-15.

Mutation check. I applied 16 single-edit mutants to mesh_shadow_check.py and ran each on both adapters. 14 were killed:

  • by a readback mismatch: u and v swapped in the plane program, the plane depth loaded 16 bytes off, the plane depth not uploaded, the reference using the previous light for the plane's map coordinate, the reference rounding instead of flooring the texel index, the caster program keeping #749's attribute order, the uploaded normal z halved, the 1/4 dropped from the plane export, a GREATER host sampler with LESS words;
  • by the map readback: light-pass counts exchanged in the header, the current map drawn without the triangle behind the plane;
  • by the reference's own assertions: the map keeping the farthest depth, the caster at the plane's depth, the ambient term without 2^-17.

Two survive, and both are equivalent here:

  • LESS_EQUAL in the reference: the plane depth never equals a stored depth. The strict and inclusive split is covered by pixel_aot_compare (#718).
  • A wrapping texel index in the reference instead of the clamp: the only texels a wrapped coordinate reaches are clear or behind the plane, so they leave the plane lit. This fixture does not distinguish CLAMP from WRAP.

Pure-Python checks from AGENTS.md (22), plus pixel_aot_check.py, pixel_compare_check.py and mesh_shadow_check.py without arguments, and gpu_report_format_check.py: all exit 0.

GPU load during the dev run (nvidia-smi): 4 % before and 10 % after, about 5.7 GB of 8 GB already in use by other processes. Each draw is a 64×64 target.

Overlap. git merge-tree against each of the 62 open PRs: this branch adds no conflicted file that main does not already have. No open PR or issue other than #730 works on shadows.

Evidence and follow-up

  • Fixtures: two original pixel programs written for this test and the N·L program from #749, plus the mesh program from #546. All are gfx1013 assembly assembled with LLVM 15, MIT. No game files.
  • Assumptions: the receiver's light-space depth comes from b0, which is enough for a plane facing the light's depth axis; a per-pixel reference from an attribute is not exercised because pixel_aot interpolates only attr0.xy in sampled programs. Casters write the map; the receiver does not, so no depth bias is needed. Native shadow-map layouts and formats are not modelled.
  • Not covered: bilinear comparison filtering (PCF) in the scene, CLAMP against WRAP (above), and shadow acne or bias.
  • Next, as you laid out on #730: this fixture is the combined lit and shadowed scene, standalone. Once its comparisons are accepted here, the EXE hookup on the current draw_request/draw_frame ABI with the three-runner output and stale-input checks. The swapchain stays out.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: yes. Code, tests and this description were prepared with an AI coding agent and checked by running the commands above.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6550812112 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/773). Originally posted by [roansystemlabs](https://github.com/roansystemlabs) on 2026-11-23T02:08:45Z. --- ## Change Step 2 of #730: the standalone shadow fixture, outside the EXE path. Refs #730. It builds on the lit fixture from #749, now in `main`, and reuses its N·L pixel program and reference helpers. `tests/mesh_shadow_check.py` draws a small scene in two passes on a 64×64 target, through a new `offscreen_check --mesh-shadow` operation (`tests/mesh_shadow_draw.cpp`): - **Scene:** an orthographic camera with depth z, and a directional light that projects (x, y, z) to (x − z/2, y + z/2) with depth z. A triangle caster stands at depth 1/4 in front of a receiver plane at depth 3/4. A second triangle at depth 7/8, behind the plane, is drawn only into the light pass. Every vertex lies on the pixel grid in both views, so each receiver pixel center samples a texel center of the map. - **Light pass:** the indexed PARAM0 mesh program from #546, with the export order of the textured fixture, writes light-space depth into an `R32_TYPELESS` texture through a `D32_FLOAT` view (`LESS`, cleared to 1). The pipeline carries the caster's pixel program with color writes masked. On WARP (10.0.26100.9818), the same light pass without a pixel shader removed the device (`DXGI_ERROR_DEVICE_REMOVED` at the first readback map, every run) with no debug-layer message, both with the color target bound and with no render target, and also with the mesh trace writes compiled out. The RTX 3070 drew both variants with zero mismatches. Keeping the pixel program with color writes masked avoids that and keeps the debug layer quiet; it does not change the depth the pass writes. I did not reduce it to a repro outside this fixture. - **Camera pass:** the plane's source-built gfx1013 pixel program interpolates the map coordinate from attr0.xy, loads its light-space depth from b0 at byte 0x90 and runs `image_sample_c_lz` (point, `LESS`, `CLAMP_LAST_TEXEL`, `TRUNC_COORD` 1). That lowers through the #718 path to `SampleCmpLevelZero` on an `R32_FLOAT` view. The host's static comparison sampler is built from the decoded descriptor words. The plane exports 1/4 + result/2 in RG and half of that in B. Then the caster is drawn at depth 1/4 with the #749 N·L program, which reads its normal from attr0.xyz. Both camera draws are depth tested. - **Reference:** Python rasterizes both views exactly with `Fraction` and asserts that no pixel or texel center lies on an edge. It builds the shadow map with the depth test, applies the comparison and edge clamping at each plane pixel, evaluates N·L at each caster pixel, then applies the binary16 export and the 8-bit target. No D3D12 code is involved. Every RGBA byte, every texel of the current map and the mesh export trace of all five draws are compared. Values are kept away from every boundary by construction, following #749. Map coordinates are texel centers, half a texel from any edge. The plane depth 3/4 is never equal to a stored depth (1/4, 7/8 or 1), and the comparison result is exactly 0 or 1, so the plane colors are exact binary16 values. For the caster, the normal's z is 17/32, so N·L is an odd multiple of 1/128 at every pixel center and no result exports as 1/2, where the 8-bit conversion would round a half. The ambient term is 1/4 + 2^-17, as in #749, so each caster value sits at least 2^-17 above a binary16 value, far beyond the 2^-23 interpolation error seen on hardware. At every pixel the reference asserts that no value is a binary16 value and no 8-bit conversion rounds a half. C++: one new file and one row in the `offscreen_check` operation table. The existing mesh renderer, the translators (`pixel_aot.py`, `mesh_entry_aot.py`) and the compute files are untouched. Also included: one CMake test (`mesh_shadow_draw_{hardware,warp}`), one line in the Ubuntu LLVM 15 job, a subsection in `docs/experiments.md` after the lit fixture, and a reference image (scene and light-space depth). ## Verification Commit `2192cbe` on `main` `761e323`. Windows 11 build 26200.9974, D3D12 debug layer enabled, NVIDIA GeForce RTX 3070 (driver 621.01, 32.0.16.2101) and WARP (10.0.26100.9818). ``` ctest --preset dev --no-tests=error -R "^(mesh_|compute_mesh_texture_|offscreen_warp|pixel_aot_compare_)" -V 100% tests passed, 0 tests failed out of 25 ctest --preset release --no-tests=error -R "^mesh_(shadow|lit)_draw_" -V 100% tests passed, 0 tests failed out of 4 ``` On both adapters, `mesh_shadow_draw` gives `pixels=4096 mismatches=0` and `shadow_map_texels=4096 shadow_map_mismatches=0`, with 342 plane pixels in shadow. The current map holds 576 texels at 1/4, 176 at 7/8 and 3,344 at 1. The readback is byte-identical between the two adapters. All 323 debug-layer lines in the dev run report `debug_errors=0 debug_warnings=0`. No capability skip on this machine. On an adapter without mesh shaders the draw returns 77 like the other `mesh_*` tests. I couldn't test that here. **Negative controls**, each against the base reference unless stated: | Control | What changes | Result on RTX 3070 and WARP | |---|---|---| | `GREATER` for `LESS` | Program translated from `GREATER` descriptor words, host sampler from the same words | `mismatches=3520`: every plane pixel flips, the 576 caster pixels match. The draw matches its own `GREATER` reference. | | Stale map | The camera pass binds the map drawn, in the same command list, for a previous light (x + z/2, y + z/2) | `mismatches=356`. It matches its own reference for that map. | | Ignored map | The plane program exports the lit color without sampling | `mismatches=342`, the shadowed pixels. It matches its own unshadowed reference. | | Coverage instead of depth | A reference where any texel a caster wrote shadows, whatever its depth | `mismatches=52`: the plane pixels that sample the triangle behind the plane. | | Map without the second triangle | The expected map lacks the triangle behind the plane | `shadow_map_mismatches=176` | The test checks each count, the matching message on stderr and a clean debug layer. **Binary check (LLVM 15).** `python3 -B tests/mesh_shadow_check.py binary ...` assembles the mesh program and both pixel programs, wraps each pixel program in a container, decodes it through `native_program` and `disassemble`, and requires the same translation as the source text. The fork's Actions were unavailable (HTTP 422 on `workflow run`), so I ran it on Windows with the LLVM 15.0.7 release, as for #749: the clang 15 integrated assembler stood in for the missing `llvm-mc`, with `llvm-objcopy` and `LLVM-C.dll` from the same release. It passed (receiver 132 bytes, caster 112 bytes), as did `mesh_lit_check.py binary` and `mesh_textured_check.py binary`. The Ubuntu job in this PR runs the same check with `llvm-mc-15`. **Mutation check.** I applied 16 single-edit mutants to `mesh_shadow_check.py` and ran each on both adapters. 14 were killed: - by a readback mismatch: u and v swapped in the plane program, the plane depth loaded 16 bytes off, the plane depth not uploaded, the reference using the previous light for the plane's map coordinate, the reference rounding instead of flooring the texel index, the caster program keeping #749's attribute order, the uploaded normal z halved, the 1/4 dropped from the plane export, a `GREATER` host sampler with `LESS` words; - by the map readback: light-pass counts exchanged in the header, the current map drawn without the triangle behind the plane; - by the reference's own assertions: the map keeping the farthest depth, the caster at the plane's depth, the ambient term without 2^-17. Two survive, and both are equivalent here: - `LESS_EQUAL` in the reference: the plane depth never equals a stored depth. The strict and inclusive split is covered by `pixel_aot_compare` (#718). - A wrapping texel index in the reference instead of the clamp: the only texels a wrapped coordinate reaches are clear or behind the plane, so they leave the plane lit. This fixture does not distinguish `CLAMP` from `WRAP`. Pure-Python checks from `AGENTS.md` (22), plus `pixel_aot_check.py`, `pixel_compare_check.py` and `mesh_shadow_check.py` without arguments, and `gpu_report_format_check.py`: all exit 0. GPU load during the dev run (`nvidia-smi`): 4 % before and 10 % after, about 5.7 GB of 8 GB already in use by other processes. Each draw is a 64×64 target. **Overlap.** `git merge-tree` against each of the 62 open PRs: this branch adds no conflicted file that `main` does not already have. No open PR or issue other than #730 works on shadows. ## Evidence and follow-up - **Fixtures:** two original pixel programs written for this test and the N·L program from #749, plus the mesh program from #546. All are gfx1013 assembly assembled with LLVM 15, MIT. No game files. - **Assumptions:** the receiver's light-space depth comes from b0, which is enough for a plane facing the light's depth axis; a per-pixel reference from an attribute is not exercised because `pixel_aot` interpolates only attr0.xy in sampled programs. Casters write the map; the receiver does not, so no depth bias is needed. Native shadow-map layouts and formats are not modelled. - **Not covered:** bilinear comparison filtering (PCF) in the scene, `CLAMP` against `WRAP` (above), and shadow acne or bias. - **Next, as you laid out on #730:** this fixture is the combined lit and shadowed scene, standalone. Once its comparisons are accepted here, the EXE hookup on the current `draw_request`/`draw_frame` ABI with the three-runner output and stale-input checks. The swapchain stays out. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: yes. Code, tests and this description were prepared with an AI coding agent and checked by running the commands above.
upstream-sync closed this pull request 2026-11-24 06:44:43 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!767
No description provided.