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 000000000..49b70c4a7 Binary files /dev/null and b/docs/mesh-shadow-reference.png differ diff --git a/tests/desktop_gpu.h b/tests/desktop_gpu.h index 9c9583df4..9f20967f7 100644 --- a/tests/desktop_gpu.h +++ b/tests/desktop_gpu.h @@ -44,6 +44,8 @@ void check_compute_to_pixel(ID3D12Device* device, const char* compute, const cha const char* reference, const char* previous); void check_pixel_quad_lod(ID3D12Device* device, const char* shader); void check_pixel_compare(ID3D12Device* device, const char* shader, const char* reference); +bool check_mesh_shadow_draw(ID3D12Device*, const char* mesh, const char* receiver, const char* caster, + const char* fixture, bool stale); void check_bc7_pixel(ID3D12Device* device, const char* shader, const char* input, const char* capture, const char* mip1 = nullptr); void check_bc7_tail(ID3D12Device* device, const char* shader, const char* firstMip, int count, char** paths); diff --git a/tests/mesh_shadow_check.py b/tests/mesh_shadow_check.py new file mode 100644 index 000000000..9cc4a146a --- /dev/null +++ b/tests/mesh_shadow_check.py @@ -0,0 +1,331 @@ +"""Cast a shadow with translated programs: light-space depth, then a SampleCmpLevelZero receiver and a lit caster. + +Pass 1 draws the caster with the translated mesh program into a depth texture, from an oblique directional +light. Pass 2 draws a receiver plane whose translated pixel program compares its light-space depth against +that texture with image_sample_c_lz, then the caster lit with N.L from the #749 pixel program. The expected +image and the expected shadow map come from an exact CPU model of the scene; no D3D12 code is involved. +""" + +from fractions import Fraction +import math +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from mesh_entry_check import binary, enumerate_pcs, fixture +from mesh_indexed_check import HEADER, WORDS, inputs, lower +from mesh_lit_check import AMBIENT, LIGHT_OFFSET, export16, pixel_program, unorm8 +from mesh_textured_check import mesh_program +from pixel_aot import comparison_sampler_state, disassemble, lower as lower_pixel, native_program +from pixel_aot_check import envelope +from pixel_compare_check import passes, sampler_words + +SIDE = 64 +REFERENCE_OFFSET = 0x90 # params[9] of the receiver's b0: its light-space depth +# The scene in camera NDC, an orthographic view with depth z. A directional light projects a point +# (x, y, z) to (x + a z, y + b z) with depth z. Every vertex lies on the pixel grid in both views, so pixel +# centres of the receiver sample texel centres of the map, at least half a texel from any texel edge. +LIGHT_SHEAR = (Fraction(-1, 2), Fraction(1, 2)) +STALE_SHEAR = (Fraction(1, 2), Fraction(1, 2)) # the previous frame's light, for the stale-map control +CASTER_DEPTH = Fraction(1, 4) +RECEIVER_DEPTH = Fraction(3, 4) +HIDDEN_DEPTH = Fraction(7, 8) # behind the receiver: in the map, but it must not shadow +CASTER = ((12, 48), (44, 48), (28, 12)) # pixel corners: an upright triangle +HIDDEN = ((40, 4), (62, 4), (62, 20)) # light-space pixel corners of a caster behind the receiver +RECEIVER = ((-1, 1), (3, 1), (-1, -3)) # NDC: covers the whole target +# Caster shading: normal (x - cx, y - cy, 1/2) at NDC (x, y), lit from the upper left like the shadow. +CENTRE = (Fraction(0), Fraction(-1, 8)) +# N.L is an odd multiple of 1/128 at every pixel centre, so no exported value is 1/2, where the 8-bit +# conversion would round a half; the ambient's 2^-17 keeps each sum off a binary16 value, as in #749. +NORMAL_Z = Fraction(17, 32) +SHADE_LIGHT = (Fraction(-1, 2), Fraction(1, 2), Fraction(1, 4), AMBIENT) + + +def ndc(corner): + px, py = corner + return Fraction(px, SIDE // 2) - 1, 1 - Fraction(py, SIDE // 2) + + +def project(point, shear, depth): + return point[0] + shear[0] * depth, point[1] + shear[1] * depth + + +def centre(x, y): + return Fraction(2 * x + 1 - SIDE, SIDE), Fraction(SIDE - 2 * y - 1, SIDE) + + +def inside(triangle, x, y): + """Exact coverage of the pixel centre; no centre may lie on an edge, so the fill rule never decides.""" + cx, cy = centre(x, y) + edges = [(x1 - x0) * (cy - y0) - (y1 - y0) * (cx - x0) + for (x0, y0), (x1, y1) in zip(triangle, triangle[1:] + triangle[:1])] + assert not (0 in edges and (min(edges) >= 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("