From 2192cbe647b2479443e79d5170a10a7469f5fe54 Mon Sep 17 00:00:00 2001 From: Roan System Labs Date: Mon, 23 Nov 2026 02:02:49 +0100 Subject: [PATCH] gpu: cast a shadow with a translated depth pass and SampleCmpLevelZero Step 2 of #730, on top of the lit fixture (#749). A light pass writes light-space depth from the translated mesh program into a depth texture. The camera pass draws a receiver plane whose translated pixel program compares against that map with image_sample_c_lz (the #718 path), then the caster lit with the #749 N.L program. An exact CPU reference checks every byte, the shadow map and the mesh traces. Negative controls: GREATER for LESS, stale map, ignored map, coverage instead of depth. Refs #730 --- .github/workflows/windows.yml | 1 + CMakeLists.txt | 6 +- docs/experiments.md | 54 ++++++ docs/mesh-shadow-reference.png | Bin 0 -> 911 bytes tests/desktop_gpu.h | 2 + tests/mesh_shadow_check.py | 331 +++++++++++++++++++++++++++++++++ tests/mesh_shadow_draw.cpp | 302 ++++++++++++++++++++++++++++++ tests/offscreen_check.cpp | 12 ++ 8 files changed, 707 insertions(+), 1 deletion(-) create mode 100644 docs/mesh-shadow-reference.png create mode 100644 tests/mesh_shadow_check.py create mode 100644 tests/mesh_shadow_draw.cpp diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index e752af345..f58b244e3 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -38,6 +38,7 @@ jobs: python3 -B tests/mesh_entry_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 python3 -B tests/mesh_descriptor_swap_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 python3 -B tests/mesh_lit_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 + python3 -B tests/mesh_shadow_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 python3 -B tests/ffx_dispatch_aot_check.py llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 LLVM_LIB=libLLVM-15.so.1 LLVM_MC=llvm-mc-15 LLVM_OBJCOPY=llvm-objcopy-15 python3 -B tests/rdna2_tools_check.py python-linux: diff --git a/CMakeLists.txt b/CMakeLists.txt index e30f4c683..38031eb3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -134,7 +134,7 @@ add_custom_command(OUTPUT "${pixel_vs}" COMMAND "${DXC_EXECUTABLE}" -T vs_6_0 -E -Fh "${pixel_vs}" -Vn pixel_aot_vs "${CMAKE_CURRENT_SOURCE_DIR}/tests/pixel_aot_vs.hlsl" DEPENDS tests/pixel_aot_vs.hlsl VERBATIM) target_sources(offscreen_check PRIVATE tests/pixel_aot_render.cpp tests/pixel_compare_render.cpp "${pixel_vs}") -target_sources(offscreen_check PRIVATE tests/mesh_entry_draw.cpp) +target_sources(offscreen_check PRIVATE tests/mesh_entry_draw.cpp tests/mesh_shadow_draw.cpp) target_sources(offscreen_check PRIVATE tests/binding_mips.cpp tests/ffx_recurrent_views.cpp) target_sources(offscreen_check PRIVATE tests/ffx_rcas_views.cpp) foreach(storage IN ITEMS Native Packed) @@ -2479,6 +2479,10 @@ if(EXISTS "${INTAKE_PYTHON}") "${CMAKE_SOURCE_DIR}/tests/mesh_lit_check.py" "${DXC_EXECUTABLE}" "$" ${adapter}) set_tests_properties(mesh_lit_draw_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) + add_test(NAME mesh_shadow_draw_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_SOURCE_DIR}/tests/mesh_shadow_check.py" "${DXC_EXECUTABLE}" + "$" ${adapter}) + set_tests_properties(mesh_shadow_draw_${adapter} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77) add_test(NAME mesh_entry_draw_${adapter} COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/mesh_entry_check.py" "${DXC_EXECUTABLE}" "$" ${adapter}) diff --git a/docs/experiments.md b/docs/experiments.md index 8ccaf83e4..47b85909b 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -1725,6 +1725,60 @@ python3 -B tests/mesh_lit_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15. ctest --preset dev --no-tests=error -R "^mesh_lit_draw_" -V ``` +The shadow fixture (`tests/mesh_shadow_check.py`, `offscreen_check +--mesh-shadow`) adds a depth pass and a comparison sample to the lit fixture. +All draws use the PARAM0 mesh program of the textured fixture on a 64×64 target. +The scene is orthographic with depth z. A directional light projects (x, y, z) +to (x − z/2, y + z/2) with depth z. 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 caster triangle at depth 1/4 and a second triangle at + depth 7/8 go into an `R32_TYPELESS` texture through a `D32_FLOAT` view. The + depth test is `LESS` and the map is cleared to 1. The pixel program is the + caster's, with color writes masked. On WARP (10.0.26100.9818), a mesh + pipeline without a pixel shader removed the device in this fixture. +- **Camera pass:** a receiver plane at depth 3/4 covers the target. Its + source-built pixel program interpolates the map coordinate from attr0.xy and + loads its light-space depth from b0 at byte 0x90. It compares that depth + against the map with `image_sample_c_lz` (point, `LESS`, through the #718 + path, `SampleCmpLevelZero` on an `R32_FLOAT` view with a static comparison + sampler from the decoded words). It exports 1/4 + result/2 as the plane color. + The caster is then drawn at depth 1/4 with the #749 N·L program, reading its + normal from attr0.xyz. The normal's z is 17/32, so N·L at every pixel center + is an odd multiple of 1/128 and no result exports as 1/2. The ambient term is + 1/4 + 2^-17, as in the lit fixture. + +The CPU reference rasterizes both views exactly with `Fraction` and asserts that +no pixel or texel center lies on an edge. It evaluates N·L at each caster pixel +and applies the comparison, `CLAMP` addressing and the 8-bit target. It does not +use D3D12. Every RGBA byte, every texel of the current map (576 at 1/4, 176 at +7/8, the rest 1) and the mesh trace of each of the five draws are compared. On +RTX 3070 and WARP with the debug layer, the base scene matches, with 342 plane +pixels in shadow. The second triangle is behind the plane: its 176 texels are in +the map, and the plane pixels that sample them stay lit. The scene does not tell +`CLAMP` from `WRAP`: the texels a wrapped coordinate would reach are clear or +behind the plane. Negative controls: + +- **`GREATER` for `LESS`** (translated program and host sampler): every one of + the 3,520 plane pixels flips. 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). 356 pixels differ. The draw matches + its own reference for that map. +- **Ignored map:** a receiver program that exports the lit color without + sampling gives 342 mismatches. It matches its own unshadowed reference. +- **Coverage instead of depth:** a reference where any written texel shadows + differs at 52 pixels, the plane pixels that sample the triangle behind the + plane. +- **Map without the second triangle:** the map readback differs at its 176 + texels. + +![Shadow scene reference and the light-space depth map](mesh-shadow-reference.png) + +```sh +python3 -B tests/mesh_shadow_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 +ctest --preset dev --no-tests=error -R "^mesh_shadow_draw_" -V +``` + A separate draw binds two immutable constant buffers through a shader-visible D3D12 table. The translated mesh reads depth from b0 and a horizontal offset from b1. The translated pixel stage reads a packed color from b0. Both draws diff --git a/docs/mesh-shadow-reference.png b/docs/mesh-shadow-reference.png new file mode 100644 index 0000000000000000000000000000000000000000..49b70c4a7d6565ec04fde2c5ee8b817ba1ba2097 GIT binary patch literal 911 zcmeAS@N?(olHy`uVBq!ia0y~yVB`R@8#tJNWCcUQN(KhzFi#i9kcwMxZ!GK!4iss- zSScd+G>wI+(V@p-0w<7C5a193ae*|1TH>hScEYbOS^+%I~0ul)vJO(Um5=_iIjf`yv7@QL%B(DFuYhrMKftknP1Bd{! z_#xaaHbN-k$(_r#{&HhtlSpV_bn7>G_J9%0JZ9+8a{!{T=U$==Hz!a&Q2tnIiC`n+ zu^Jnoc|E@j{!RKJ+IVrA%Y88al)*L6KRtCW2@QtqAUhTt_4o?iS2nmc>BUrbkao#q zYMuFl5(#G-S%5BD!I>=Gx&GpGj-G8QzaE2Zz0$IA_L1j6zx163*}Q@+Szc)VqJBY% zT+c5@pd$4`@{7*K&sY}ypAF{Zf76freh~}wKhOuqJPh*O9_4QFT*TFH=ket@NY@Fj z#{xq6i>?E0ZU9=d_prsBBdbBSRy_jQn$lWm(K-F1nZ(RWkaR-BMx9Rai=V;H0td#E z=E4}E^NXU`MIi?0df4>Nb4l2u8aNl^t%;n+YEB!#~I>5LB1(8(GQ_nlyZf%CD+#r7PeqH}Zc~gE0%Yb;y zJu)iqT(&R1nBddN3{rn%-!>J$#dBZ|+3B(7x_!?|53skcPwzaUC$Y8y;)jV7U5+(# zK@7QC878!oZ98uBcg8@>ICjH8Yr+fOV<9#m%R?kYJyL$M?7w8OTm@EwZ4%0VlqOgLO^dKkaI$VqQL?5= 0 or max(edges) <= 0)), "pixel centre on a triangle edge" + return min(edges) > 0 or max(edges) < 0 + + +def shadow_map(casters): + """Light-space depth after a LESS depth test from a cleared 1.0, as D32_FLOAT texels.""" + texels = [] + for y in range(SIDE): + for x in range(SIDE): + texels.append(min([depth for triangle, depth in casters if inside(triangle, x, y)] + [Fraction(1)])) + return texels + + +def caster_light(shear): + return [project(ndc(corner), shear, CASTER_DEPTH) for corner in CASTER] + + +def light_casters(shear, hidden): + casters = [(caster_light(shear), CASTER_DEPTH)] + if hidden: + casters.append(([ndc(corner) for corner in HIDDEN], HIDDEN_DEPTH)) + return casters + + +def exact16(value): + """A receiver result: compare result times 1/2 plus 1/4, exact in binary32 and in binary16.""" + assert Fraction(struct.unpack("