From 5f23fd28ebbaf222ba69fc11077b7f90c1f39a2f Mon Sep 17 00:00:00 2001 From: Royoo <3230853833@qq.com> Date: Mon, 23 Nov 2026 13:05:31 +0800 Subject: [PATCH] Compare two translated shadow frames from the EXE entry --- CMakeLists.txt | 11 ++- docs/experiments.md | 13 ++++ tests/compute_check.cpp | 43 ----------- tests/cpu/shim_chain_gpu.cpp | 33 ++++++--- tests/cpu/shim_draw_provider.cpp | 10 +++ tests/cpu/shim_shadow_check.py | 72 +++++++++++++++++++ tests/cpu/shim_startup.asm | 3 + tests/desktop_gpu.cpp | 82 +++++++++++++++++++++ tests/desktop_gpu.h | 3 +- tests/mesh_shadow_check.py | 16 ++--- tests/mesh_shadow_draw.cpp | 118 ++++++++++++++++++++++--------- tests/offscreen_check.cpp | 31 -------- 12 files changed, 309 insertions(+), 126 deletions(-) create mode 100644 tests/cpu/shim_shadow_check.py create mode 100644 tests/desktop_gpu.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ddaf11fa6..de5babd39 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 tests/mesh_shadow_draw.cpp) +target_sources(offscreen_check PRIVATE tests/desktop_gpu.cpp 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) @@ -2217,7 +2217,7 @@ if(EXISTS "${INTAKE_PYTHON}") target_compile_features(cpu_shim_compute PRIVATE cxx_std_17) target_link_libraries(cpu_shim_compute PRIVATE xmem_shim dxgi d3d12) add_library(cpu_shim_draw SHARED tests/cpu/shim_draw_provider.cpp tests/cpu/shim_draw_gpu.cpp - tests/cpu/shim_chain_gpu.cpp) + tests/cpu/shim_chain_gpu.cpp tests/desktop_gpu.cpp tests/mesh_shadow_draw.cpp) set(shim_draw_color_header "${CMAKE_CURRENT_BINARY_DIR}/shim_draw_color.h") add_custom_command(OUTPUT "${shim_draw_color_header}" COMMAND "${DXC_EXECUTABLE}" -T ps_6_0 -E main -Fh "${shim_draw_color_header}" -Vn shim_draw_color @@ -2304,6 +2304,13 @@ if(EXISTS "${INTAKE_PYTHON}") "$" "$") set_tests_properties(cpu_shim_chain_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77 ENVIRONMENT "X2W_SHIM_DRAW_ADAPTER=${adapter};X2W_SHIM_COMPUTE_CASE=") + add_test(NAME cpu_shim_shadow_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/cpu/shim_shadow_check.py" "${DXC_EXECUTABLE}" + "$" "$" "$" + "$" "$" "$" + "$" "$") + set_tests_properties(cpu_shim_shadow_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77 + ENVIRONMENT "X2W_SHIM_DRAW_ADAPTER=${adapter};X2W_SHIM_COMPUTE_CASE=") endforeach() add_test(NAME cpu_native_exe_chain COMMAND "${INTAKE_PYTHON}" -B diff --git a/docs/experiments.md b/docs/experiments.md index 236dd4aa0..ce09231e2 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -3793,3 +3793,16 @@ ctest --preset dev --no-tests=error -R '^pixel_aot_vop3_ternary_int_(warp|hardwa Python-only controls check explicit reference words and translator acceptance. Those controls do not establish GPU execution; the command above must pass on the named backend before recording readback evidence. + +### Two shadow frames from the EXE entry + +The native, mapped and AOT runners use the existing translated shadow scene. The +second frame shifts the caster four pixels while retaining the device, pipeline +states, descriptor heap, camera depth and color target. Two light depth maps +remain live so the second camera pass can deliberately select the first one. +The fixture compares all 4,096 color pixels and 4,096 light-depth texels of each +frame against the CPU reference, checks four owned surface releases after the +GPU fence, and verifies exit code 42. The stale binding changes only the +second color frame and is checked against its own reference. + +Run `ctest --preset release --no-tests=error -R '^cpu_shim_shadow_(warp|hardware)$' -V`. diff --git a/tests/compute_check.cpp b/tests/compute_check.cpp index bc3b7ed20..5e43daaf0 100644 --- a/tests/compute_check.cpp +++ b/tests/compute_check.cpp @@ -23,18 +23,6 @@ constexpr UINT sentinel = 0xc5c5c5c5u; constexpr std::array constants{count, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 23, 29}; } -ComPtr make_buffer(ID3D12Device* device, UINT64 length, D3D12_HEAP_TYPE heap, - D3D12_RESOURCE_STATES state, D3D12_RESOURCE_FLAGS flags) -{ - D3D12_RESOURCE_DESC desc{}; - desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; - desc.Width = length; - desc.Height = desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1; - desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; - desc.Flags = flags; - return resource(device, desc, heap, state); -} - static UINT input_word(UINT i) { return 0x8b02a7f1u + i * 0x413b1u; } static void fill_upload(ID3D12Resource* upload) @@ -54,15 +42,6 @@ static void fill_upload(ID3D12Resource* upload) upload->Unmap(0, nullptr); } -void transition(ID3D12GraphicsCommandList* list, ID3D12Resource* buffer, - D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after) -{ - D3D12_RESOURCE_BARRIER barrier{}; - barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; - barrier.Transition = {buffer, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, before, after}; - list->ResourceBarrier(1, &barrier); -} - static ComPtr bind_tables(ID3D12Device* device, ID3D12GraphicsCommandList* list, ID3D12Resource* input, ID3D12Resource* output) { @@ -248,25 +227,3 @@ void check_bvh_root_roundtrip(ID3D12Device* device) run_compute(device, false, shader, {roundtrip->GetBufferPointer(), roundtrip->GetBufferSize()}); std::puts("normalized_signature=matched_control root_creation=passed pipeline_creation=passed dispatch=passed"); } - -ComputeCommands::ComputeCommands(ID3D12Device* device, ID3D12PipelineState* pipeline, D3D12_COMMAND_LIST_TYPE type) -{ - D3D12_COMMAND_QUEUE_DESC desc{}; - desc.Type = type; - check(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue)), "Create compute queue"); - check(device->CreateCommandAllocator(desc.Type, IID_PPV_ARGS(&allocator)), "Create compute allocator"); - check(device->CreateCommandList(0, desc.Type, allocator.Get(), pipeline, IID_PPV_ARGS(&list)), - "Create compute command list"); -} - -void ComputeCommands::submit_and_wait(ID3D12Device* device) -{ - check(list->Close(), "Close compute command list"); - ComPtr fence; - check(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)), "Create compute fence"); - ID3D12CommandList* submitted[]{list.Get()}; - queue->ExecuteCommandLists(1, submitted); - check(queue->Signal(fence.Get(), 1), "Signal compute fence"); - check(fence->SetEventOnCompletion(1, nullptr), "Wait for compute"); - check(device->GetDeviceRemovedReason(), "Compute completion"); -} diff --git a/tests/cpu/shim_chain_gpu.cpp b/tests/cpu/shim_chain_gpu.cpp index 8afa83b9e..411105063 100644 --- a/tests/cpu/shim_chain_gpu.cpp +++ b/tests/cpu/shim_chain_gpu.cpp @@ -1,3 +1,6 @@ +#include "../desktop_gpu.h" +#include +#include // Run the translated compute -> mesh -> pixel chain for one EXE-requested frame. // // This repeats the sequence of `offscreen_check --compute-mesh-texture` @@ -131,15 +134,6 @@ ComPtr buffer(ID3D12Device* device, UINT64 bytes, D3D12_HEAP_TYP return committed(device, desc, heap, state); } -void transition(ID3D12GraphicsCommandList* list, ID3D12Resource* resource, - D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after) -{ - D3D12_RESOURCE_BARRIER barrier{}; - barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; - barrier.Transition = {resource, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, before, after}; - list->ResourceBarrier(1, &barrier); -} - template struct alignas(void*) Field { D3D12_PIPELINE_STATE_SUBOBJECT_TYPE kind = Kind; @@ -385,3 +379,24 @@ bool shim_chain_gpu(unsigned mode, std::vector& published, unsign return false; } } + +bool shim_shadow_gpu(bool stale, std::vector& output) +{ + try { + const auto* path = std::getenv("X2W_SHIM_CHAIN_DIR"); + if (!path || !*path) return false; + const auto directory = std::filesystem::path(path); + auto device = create_device(); + if (!check_mesh_shadow_draw(device.Get(), (directory / "mesh.dxil").string().c_str(), + (directory / "receiver.dxil").string().c_str(), + (directory / "caster.dxil").string().c_str(), + (directory / "frames.case").string().c_str(), stale, &output)) + ExitProcess(77); + return true; + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return false; + } catch (const Failure&) { + return false; + } +} diff --git a/tests/cpu/shim_draw_provider.cpp b/tests/cpu/shim_draw_provider.cpp index f2309ff52..f2d3ed7e0 100644 --- a/tests/cpu/shim_draw_provider.cpp +++ b/tests/cpu/shim_draw_provider.cpp @@ -10,6 +10,8 @@ bool shim_draw_gpu(unsigned mode, std::vector& rgba); bool shim_chain_gpu(unsigned mode, std::vector& published, unsigned& mismatches); +bool shim_shadow_gpu(bool stale, std::vector& output); + namespace { constexpr unsigned side = 64; @@ -50,6 +52,8 @@ extern "C" __declspec(dllexport) DWORD draw_request() if (!std::strcmp(mode, "two")) return 6; if (!std::strcmp(mode, "stale")) return 7; if (!std::strcmp(mode, "fail-second")) return 8; + if (!std::strcmp(mode, "shadow")) return 14; + if (!std::strcmp(mode, "shadow-stale")) return 15; if (!std::strcmp(mode, "texture")) return 10; if (!std::strcmp(mode, "texture-stale")) return 12; } @@ -65,6 +69,12 @@ extern "C" __declspec(dllexport) DWORD draw_frame(DWORD mode) if (!publish(published.data(), static_cast(published.size()))) ExitProcess(23); return count; } + if (mode == 14 || mode == 15) { + std::vector output; + if (!shim_shadow_gpu(mode == 15, output)) return 0xFFFFFFFF; + if (!publish(output.data(), static_cast(output.size()))) ExitProcess(23); + return 0; + } if (mode < 1 || mode > 13) ExitProcess(22); if (mode >= 6) std::fprintf(stderr, "draw_frame mode=%lu\n", static_cast(mode)); std::vector rgba; diff --git a/tests/cpu/shim_shadow_check.py b/tests/cpu/shim_shadow_check.py new file mode 100644 index 000000000..d05fbad4f --- /dev/null +++ b/tests/cpu/shim_shadow_check.py @@ -0,0 +1,72 @@ +"""Compare two submitted shadow frames from one native, mapped and translated EXE.""" +import os +from pathlib import Path +import struct +import subprocess +import sys +from tempfile import TemporaryDirectory + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +import mesh_shadow_check as shadow +from shim_startup_check import EXPECTED, SHIM_IMPORTS, check + + +def build(directory, dxc): + shadow.compile_stage(dxc, directory, "mesh", + shadow.lower(shadow.enumerate_pcs(shadow.mesh_program()), shadow.WORDS, (3, 1), shadow.HEADER), + "ms_6_5", ["-DTRACE_EXPORTS"]) + shadow.compile_stage(dxc, directory, "caster", shadow.translate_caster(shadow.caster_program())) + words = shadow.sampler_words("LESS", "point") + shadow.compile_stage(dxc, directory, "receiver", shadow.translate_receiver(shadow.receiver_program(words), words)) + moved = tuple((x + 4, y) for x, y in shadow.CASTER) + lights = [shadow.light_casters(shadow.LIGHT_SHEAR, True, corners) for corners in (shadow.CASTER, moved)] + maps = [shadow.shadow_map(casters) for casters in lights] + draws = [shadow.draw_inputs([(*point, depth) for point in triangle]) + for casters in lights for triangle, depth in casters] + draws += shadow.camera_draws() + shadow.camera_draws(moved) + state = shadow.comparison_sampler_state(words) + header = struct.pack("<8I", shadow.SIDE, len(lights[0]), len(lights[1]), + state["d3d12_filter"], state["d3d12_comparison_func"], 0, 0, 0) + (directory / "frames.case").write_bytes(header + b"".join(data for data, _ in draws) + + b"".join(trace for _, trace in draws)) + first = shadow.scene_image(maps[0])[0] + second = shadow.scene_image(maps[1], corners=moved)[0] + stale = shadow.scene_image(maps[0], corners=moved)[0] + depths = b"".join(struct.pack(" + +using Microsoft::WRL::ComPtr; + +#if defined(OFFSCREEN_AGILITY_620) +extern "C" const UINT D3D12SDKVersion; +#endif + +void check(HRESULT hr, const char* stage) +{ + if (FAILED(hr)) { + std::fprintf(stderr, "%s failed (HRESULT 0x%08lx)\n", stage, static_cast(hr)); + throw std::runtime_error(stage); + } +} + +HRESULT check_feature(ID3D12Device* device, D3D12_FEATURE feature, void* data, UINT size) +{ +#if defined(OFFSCREEN_AGILITY_620) && defined(__ID3D12Device16_INTERFACE_DEFINED__) + ComPtr versioned; + if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&versioned)))) + return versioned->CheckFeatureSupport1(feature, data, size, D3D12SDKVersion); +#endif + return device->CheckFeatureSupport(feature, data, size); +} + +ComPtr resource(ID3D12Device* device, const D3D12_RESOURCE_DESC& desc, + D3D12_HEAP_TYPE heap, D3D12_RESOURCE_STATES state, + const D3D12_CLEAR_VALUE* clear) +{ + D3D12_HEAP_PROPERTIES props{}; + props.Type = heap; + props.CreationNodeMask = props.VisibleNodeMask = 1; + ComPtr result; + check(device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, state, clear, + IID_PPV_ARGS(&result)), "Create resource"); + return result; +} + +ComPtr make_buffer(ID3D12Device* device, UINT64 length, D3D12_HEAP_TYPE heap, + D3D12_RESOURCE_STATES state, D3D12_RESOURCE_FLAGS flags) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + desc.Width = length; + desc.Height = desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + desc.Flags = flags; + return resource(device, desc, heap, state); +} + +void transition(ID3D12GraphicsCommandList* list, ID3D12Resource* buffer, + D3D12_RESOURCE_STATES before, D3D12_RESOURCE_STATES after) +{ + D3D12_RESOURCE_BARRIER barrier{}; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + barrier.Transition = {buffer, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, before, after}; + list->ResourceBarrier(1, &barrier); +} + +ComputeCommands::ComputeCommands(ID3D12Device* device, ID3D12PipelineState* pipeline, D3D12_COMMAND_LIST_TYPE type) +{ + D3D12_COMMAND_QUEUE_DESC desc{}; + desc.Type = type; + check(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&queue)), "Create compute queue"); + check(device->CreateCommandAllocator(desc.Type, IID_PPV_ARGS(&allocator)), "Create compute allocator"); + check(device->CreateCommandList(0, desc.Type, allocator.Get(), pipeline, IID_PPV_ARGS(&list)), + "Create compute command list"); +} + +void ComputeCommands::submit_and_wait(ID3D12Device* device) +{ + check(list->Close(), "Close compute command list"); + ComPtr fence; + check(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)), "Create compute fence"); + ID3D12CommandList* submitted[]{list.Get()}; + queue->ExecuteCommandLists(1, submitted); + check(queue->Signal(fence.Get(), 1), "Signal compute fence"); + check(fence->SetEventOnCompletion(1, nullptr), "Wait for compute"); + check(device->GetDeviceRemovedReason(), "Compute completion"); +} diff --git a/tests/desktop_gpu.h b/tests/desktop_gpu.h index e64b4b86d..5fde21393 100644 --- a/tests/desktop_gpu.h +++ b/tests/desktop_gpu.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include void check(HRESULT hr, const char* stage); @@ -48,7 +49,7 @@ void check_compute_to_pixel(ID3D12Device* device, const char* compute, const cha 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); + const char* fixture, bool stale, std::vector* frames = nullptr); 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 index 9cc4a146a..030ca58a8 100644 --- a/tests/mesh_shadow_check.py +++ b/tests/mesh_shadow_check.py @@ -74,12 +74,12 @@ def shadow_map(casters): return texels -def caster_light(shear): - return [project(ndc(corner), shear, CASTER_DEPTH) for corner in CASTER] +def caster_light(shear, corners=CASTER): + return [project(ndc(corner), shear, CASTER_DEPTH) for corner in corners] -def light_casters(shear, hidden): - casters = [(caster_light(shear), CASTER_DEPTH)] +def light_casters(shear, hidden, corners=CASTER): + casters = [(caster_light(shear, corners), CASTER_DEPTH)] if hidden: casters.append(([ndc(corner) for corner in HIDDEN], HIDDEN_DEPTH)) return casters @@ -91,9 +91,9 @@ def exact16(value): return value -def scene_image(texels, compare="LESS", *, use_map=True): +def scene_image(texels, compare="LESS", *, use_map=True, corners=CASTER): """Exact expected colours: the lit caster over the receiver, the receiver lit or in shadow.""" - caster = [ndc(corner) for corner in CASTER] + caster = [ndc(corner) for corner in corners] pixels = bytearray() shadowed = 0 for y in range(SIDE): @@ -140,13 +140,13 @@ def light_draws(shear, hidden): return [draw_inputs([(*point, depth) for point in triangle]) for triangle, depth in light_casters(shear, hidden)] -def camera_draws(): +def camera_draws(corners=CASTER): receiver_uv = [] for point in RECEIVER: lx, ly = project(point, LIGHT_SHEAR, RECEIVER_DEPTH) receiver_uv.append(((lx + 1) / 2, (1 - ly) / 2, 0)) receiver = draw_inputs([(*point, RECEIVER_DEPTH) for point in RECEIVER], receiver_uv, reference=RECEIVER_DEPTH) - caster = [ndc(corner) for corner in CASTER] + caster = [ndc(corner) for corner in corners] normals = [(x - CENTRE[0], y - CENTRE[1], NORMAL_Z) for x, y in caster] lit = draw_inputs([(*point, CASTER_DEPTH) for point in caster], normals, light=SHADE_LIGHT) return [receiver, lit] diff --git a/tests/mesh_shadow_draw.cpp b/tests/mesh_shadow_draw.cpp index 493659493..5c968e798 100644 --- a/tests/mesh_shadow_draw.cpp +++ b/tests/mesh_shadow_draw.cpp @@ -97,7 +97,8 @@ unsigned count_mismatches(const char* rows, const D3D12_PLACED_SUBRESOURCE_FOOTP } bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const char* receiver_path, - const char* caster_path, const char* case_path, bool stale) + const char* caster_path, const char* case_path, bool stale, + std::vector* frames) { D3D12_FEATURE_DATA_D3D12_OPTIONS7 options{}; check(check_feature(device, D3D12_FEATURE_D3D12_OPTIONS7, &options, sizeof(options)), "Query mesh tier"); @@ -116,7 +117,7 @@ bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const c const auto filter = D3D12_FILTER(header[3]); const auto func = D3D12_COMPARISON_FUNC(header[4]); // Light draws for the previous and the current shadow map, then the receiver and the lit caster. - const UINT draws = previous + current + 2; + const UINT draws = previous + current + (frames ? 4 : 2); if (side == 0 || side > 256 || previous == 0 || current == 0 || draws > 16) throw std::runtime_error("Invalid shadow case header"); const UINT image_bytes = side * side * 4; @@ -124,7 +125,7 @@ bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const c const size_t traces_at = inputs_at + size_t(draws) * draw_bytes; const size_t image_at = traces_at + size_t(draws) * trace_stride; const size_t depth_at = image_at + image_bytes; - if (input.size() != depth_at + image_bytes) throw std::runtime_error("Shadow case size does not match its header"); + if (input.size() != (frames ? image_at : depth_at + image_bytes)) throw std::runtime_error("Shadow case size does not match its header"); const auto mesh = read_file(mesh_path); const auto receiver = read_file(receiver_path); const auto caster = read_file(caster_path); @@ -199,7 +200,7 @@ bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const c dsv[i] = {dsv_heap->GetCPUDescriptorHandleForHeapStart().ptr + i * dsv_step}; device->CreateDepthStencilView(depths[i].Get(), &dsv_view, dsv[i]); } - D3D12_DESCRIPTOR_HEAP_DESC srv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, + D3D12_DESCRIPTOR_HEAP_DESC srv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; check(device->CreateDescriptorHeap(&srv_desc, IID_PPV_ARGS(&srv_heap)), "Create shadow map heap"); D3D12_SHADER_RESOURCE_VIEW_DESC map_view{}; @@ -207,13 +208,26 @@ bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const c map_view.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; map_view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; map_view.Texture2D.MipLevels = 1; - const UINT bound_map = stale ? 0 : 1; - device->CreateShaderResourceView(depths[bound_map].Get(), &map_view, srv_heap->GetCPUDescriptorHandleForHeapStart()); + const UINT srv_step = device->GetDescriptorHandleIncrementSize(srv_desc.Type); + for (UINT i = 0; i < 2; ++i) { + auto handle = srv_heap->GetCPUDescriptorHandleForHeapStart(); + handle.ptr += i * srv_step; + device->CreateShaderResourceView(depths[i].Get(), &map_view, handle); + } D3D12_PLACED_SUBRESOURCE_FOOTPRINT color_footprint{}, depth_footprint{}; UINT64 color_size = 0, depth_size = 0; device->GetCopyableFootprints(&color_desc, 0, 1, 0, &color_footprint, nullptr, nullptr, &color_size); device->GetCopyableFootprints(&depth_desc, 0, 1, 0, &depth_footprint, nullptr, nullptr, &depth_size); + const auto align_placement = [](UINT64 size) { + constexpr UINT64 alignment = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT; + return (size + alignment - 1) / alignment * alignment; + }; + const UINT64 color_stride = align_placement(color_size), depth_stride = align_placement(depth_size); + if (frames) { + color_size += color_stride; + depth_size += depth_stride; + } const UINT trace_bytes = draws * trace_stride; auto color_readback = make_buffer(device, color_size, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); auto depth_readback = make_buffer(device, depth_size, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST); @@ -252,39 +266,80 @@ bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const c mesh_list->DispatchMesh(1, 1, 1); ++draw; }; - // Pass 1: light-space depth only, first the previous map, then the current one. - for (UINT map = 0; map < 2; ++map) { - list->OMSetRenderTargets(1, &rtv, FALSE, &dsv[map]); - list->ClearDepthStencilView(dsv[map], D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); - for (UINT i = 0; i < (map ? current : previous); ++i) dispatch(); - } - for (UINT map = 0; map < 2; ++map) - transition(list, depths[map].Get(), D3D12_RESOURCE_STATE_DEPTH_WRITE, - D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); - // Pass 2: the receiver samples the bound map, then the lit caster. - ID3D12DescriptorHeap* heaps[]{srv_heap.Get()}; - list->SetDescriptorHeaps(1, heaps); - list->SetGraphicsRootDescriptorTable(4, srv_heap->GetGPUDescriptorHandleForHeapStart()); - list->OMSetRenderTargets(1, &rtv, FALSE, &dsv[2]); - list->ClearRenderTargetView(rtv, clear_color.Color, 0, nullptr); - list->ClearDepthStencilView(dsv[2], D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); - list->SetPipelineState(receiver_pass.Get()); - dispatch(); - list->SetPipelineState(caster_pass.Get()); - dispatch(); - std::printf("light_draws=%u+%u camera_draws=2 bound_map=%s\n", previous, current, stale ? "previous" : "current"); + for (UINT frame = frames ? 0u : 1u; frame < 2; ++frame) { + // The standalone fixture renders both light maps before its single camera pass. + for (UINT map = frames ? frame : 0u; map <= frame; ++map) { + draw = map ? previous : 0; + list->SetPipelineState(light_pass.Get()); + list->OMSetRenderTargets(1, &rtv, FALSE, &dsv[map]); + list->ClearDepthStencilView(dsv[map], D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); + for (UINT i = 0; i < (map ? current : previous); ++i) dispatch(); + transition(list, depths[map].Get(), D3D12_RESOURCE_STATE_DEPTH_WRITE, + D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); + } + ID3D12DescriptorHeap* heaps[]{srv_heap.Get()}; + list->SetDescriptorHeaps(1, heaps); + auto bound = srv_heap->GetGPUDescriptorHandleForHeapStart(); + if (frame && !stale) bound.ptr += srv_step; + list->SetGraphicsRootDescriptorTable(4, bound); + list->OMSetRenderTargets(1, &rtv, FALSE, &dsv[2]); + list->ClearRenderTargetView(rtv, clear_color.Color, 0, nullptr); + list->ClearDepthStencilView(dsv[2], D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr); + draw = previous + current + (frames ? 2 * frame : 0); + list->SetPipelineState(receiver_pass.Get()); + dispatch(); + list->SetPipelineState(caster_pass.Get()); + dispatch(); + auto color_copy = color_footprint, depth_copy = depth_footprint; + if (frames && frame) { + color_copy.Offset += color_stride; + depth_copy.Offset += depth_stride; + } + transition(list, target.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + copy_to_readback(list, target.Get(), color_readback.Get(), color_copy); + transition(list, depths[frame].Get(), D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_SOURCE); + copy_to_readback(list, depths[frame].Get(), depth_readback.Get(), depth_copy); + if (frames && !frame) { + transition(list, target.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET); + transition(list, depths[frame].Get(), D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); + } + } transition(list, trace.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE); list->CopyBufferRegion(trace_readback.Get(), 0, trace.Get(), 0, trace_bytes); - transition(list, target.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); - copy_to_readback(list, target.Get(), color_readback.Get(), color_footprint); - transition(list, depths[1].Get(), D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_SOURCE); - copy_to_readback(list, depths[1].Get(), depth_readback.Get(), depth_footprint); commands.submit_and_wait(device); check(trace_readback->Map(0, nullptr, &mapped), "Map shadow trace"); const bool trace_ok = std::memcmp(mapped, input.data() + traces_at, trace_bytes) == 0; trace_readback->Unmap(0, &empty); + if (!trace_ok) throw std::runtime_error("Mesh export trace mismatch"); + if (frames) { + auto append = [&](ID3D12Resource* readback, D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint, UINT64 stride) { + check(readback->Map(0, nullptr, &mapped), "Map shadow frames"); + auto* source = static_cast(mapped) + footprint.Offset; + for (UINT frame = 0; frame < 2; ++frame) { + for (UINT y = 0; y < side; ++y) { + const auto* row = source + frame * stride + y * footprint.Footprint.RowPitch; + frames->insert(frames->end(), row, row + side * 4); + } + } + readback->Unmap(0, &empty); + }; + append(color_readback.Get(), color_footprint, color_stride); + append(depth_readback.Get(), depth_footprint, depth_stride); + unsigned releases = 0; + auto release = [&](ComPtr& surface) { + if (surface) { + surface.Reset(); + ++releases; + } + }; + for (auto& depth : depths) release(depth); + release(target); + frames->push_back(static_cast(releases)); + return true; + } + std::printf("light_draws=%u+%u camera_draws=2 bound_map=%s\n", previous, current, stale ? "previous" : "current"); check(color_readback->Map(0, nullptr, &mapped), "Map shadow color"); const unsigned mismatches = count_mismatches(static_cast(mapped), color_footprint, input.data() + image_at, side); @@ -295,7 +350,6 @@ bool check_mesh_shadow_draw(ID3D12Device* device, const char* mesh_path, const c depth_readback->Unmap(0, &empty); std::printf("pixels=%u mismatches=%u\n", side * side, mismatches); std::printf("shadow_map_texels=%u shadow_map_mismatches=%u\n", side * side, depth_mismatches); - if (!trace_ok) throw std::runtime_error("Mesh export trace mismatch"); if (depth_mismatches) throw std::runtime_error("Shadow map reference mismatch"); if (mismatches) throw std::runtime_error("Draw reference mismatch"); return true; diff --git a/tests/offscreen_check.cpp b/tests/offscreen_check.cpp index b675b32af..c29c49093 100644 --- a/tests/offscreen_check.cpp +++ b/tests/offscreen_check.cpp @@ -28,24 +28,6 @@ __declspec(dllexport) const char* D3D12SDKPath = ".\\D3D12\\"; using Microsoft::WRL::ComPtr; constexpr UINT side = 64; -void check(HRESULT hr, const char* stage) -{ - if (FAILED(hr)) { - std::fprintf(stderr, "%s failed (HRESULT 0x%08lx)\n", stage, static_cast(hr)); - throw std::runtime_error(stage); - } -} - -HRESULT check_feature(ID3D12Device* device, D3D12_FEATURE feature, void* data, UINT size) -{ -#if defined(OFFSCREEN_AGILITY_620) && defined(__ID3D12Device16_INTERFACE_DEFINED__) - ComPtr versioned; - if (SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&versioned)))) - return versioned->CheckFeatureSupport1(feature, data, size, D3D12SDKVersion); -#endif - return device->CheckFeatureSupport(feature, data, size); -} - bool contains_ignoring_case(const wchar_t* text, const wchar_t* part) { const size_t length = std::wcslen(part); @@ -107,19 +89,6 @@ ComPtr select_adapter(IDXGIFactory6* factory, bool warp, ComPtr resource(ID3D12Device* device, const D3D12_RESOURCE_DESC& desc, - D3D12_HEAP_TYPE heap, D3D12_RESOURCE_STATES state, - const D3D12_CLEAR_VALUE* clear) -{ - D3D12_HEAP_PROPERTIES props{}; - props.Type = heap; - props.CreationNodeMask = props.VisibleNodeMask = 1; - ComPtr result; - check(device->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, state, clear, - IID_PPV_ARGS(&result)), "Create resource"); - return result; -} - void save_bitmap(const char* path, const std::vector& bgra) { BITMAPINFOHEADER info{};