upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorDennis Schöneberg <schoenebergde@gmail.com>2026-11-23 16:53:12 +0100
committerGitHub <noreply@github.com>2026-11-23 09:53:12 -0600
commit9a9ca1ec14523c470fa7e87eea1b8a59f8f9fcea (patch)
tree3f895696a5e6d53e91070b7515bfc545c1d4b055 /runtime
parentc3f171d0cbaea1a5deed2e1fa2f9a69346863399 (diff)
d3d12: match Windows recording-time ExecuteIndirect validation (#6830)
Squash-merged after diff review of PR #6830 - d3d12: match Windows recording-time ExecuteIndirect validation
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt10
-rw-r--r--runtime/shims/d3d12_com.cpp23
-rw-r--r--runtime/src/command_list.h5
-rw-r--r--runtime/test/d3d12_execute_indirect_validation_test.cpp459
-rw-r--r--runtime/test/d3d12_state_map_test.cpp4
5 files changed, 499 insertions, 2 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index 95e6dead4..0188e5144 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -1831,6 +1831,16 @@ if(TARGET Vulkan::Headers)
1831 add_test(NAME d3d12_close_parity COMMAND d3d12_close_parity_test) 1831 add_test(NAME d3d12_close_parity COMMAND d3d12_close_parity_test)
1832 set_tests_properties(d3d12_close_parity PROPERTIES SKIP_RETURN_CODE 77) 1832 set_tests_properties(d3d12_close_parity PROPERTIES SKIP_RETURN_CODE 77)
1833 1833
1834 # ExecuteIndirect recording-time Close() parity against live WARP and hardware D3D12, plus GPU dispatch sanity.
1835 add_executable(d3d12_execute_indirect_validation_test test/d3d12_execute_indirect_validation_test.cpp)
1836 target_link_libraries(d3d12_execute_indirect_validation_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
1837 target_compile_definitions(d3d12_execute_indirect_validation_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
1838 target_compile_options(d3d12_execute_indirect_validation_test PRIVATE /W4 /permissive- /utf-8)
1839 add_test(NAME d3d12_execute_indirect_validation COMMAND d3d12_execute_indirect_validation_test)
1840 set_tests_properties(d3d12_execute_indirect_validation PROPERTIES
1841 SKIP_RETURN_CODE 77
1842 FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
1843
1834 # Differential regression: run explicitly (requires Vulkan hardware and Windows WARP). 1844 # Differential regression: run explicitly (requires Vulkan hardware and Windows WARP).
1835 add_executable(d3d12_clear_test test/d3d12_clear_test.cpp) 1845 add_executable(d3d12_clear_test test/d3d12_clear_test.cpp)
1836 target_link_libraries(d3d12_clear_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) 1846 target_link_libraries(d3d12_clear_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index d66e6803c..4dd766b31 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -764,6 +764,29 @@ public:
764 UINT stride = 0; 764 UINT stride = 0;
765 if (!command_signature_info(signature, kind, stride) || !argument_buffer) 765 if (!command_signature_info(signature, kind, stride) || !argument_buffer)
766 return check(false); 766 return check(false);
767 const auto arg_desc = argument_buffer->GetDesc();
768 if (arg_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || argument_buffer_offset % 4 != 0)
769 return check(false);
770 if (count_buffer) {
771 const auto count_desc = count_buffer->GetDesc();
772 if (count_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || count_buffer_offset % 4 != 0 ||
773 count_desc.Width < sizeof(UINT) || count_buffer_offset > count_desc.Width - sizeof(UINT))
774 return check(false);
775 } else if (count_buffer_offset != 0) {
776 return check(false);
777 }
778 if (max_command_count) {
779 const UINT64 record_size =
780 kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS)
781 : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS)
782 : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS)
783 : 0;
784 if (!record_size || stride < record_size || stride % 4 != 0)
785 return check(false);
786 const UINT64 required = UINT64(max_command_count - 1) * stride + record_size;
787 if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required)
788 return check(false);
789 }
767 leonida::ExecuteIndirect cmd{}; 790 leonida::ExecuteIndirect cmd{};
768 cmd.kind = kind; 791 cmd.kind = kind;
769 cmd.stride = stride; 792 cmd.stride = stride;
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
index 1325b789c..4b384bdc1 100644
--- a/runtime/src/command_list.h
+++ b/runtime/src/command_list.h
@@ -870,7 +870,10 @@ public:
870 } 870 }
871 871
872 bool execute_indirect(const ExecuteIndirect& indirect) { 872 bool execute_indirect(const ExecuteIndirect& indirect) {
873 if (!indirect.argument_buffer || !indirect.stride) return false; 873 if (!indirect.argument_buffer || !indirect.stride || indirect.stride % 4) return false;
874 if (indirect.argument_offset % 4) return false;
875 if (!indirect.count_buffer && indirect.count_offset) return false;
876 if (indirect.count_buffer && indirect.count_offset % 4) return false;
874 if (!indirect.max_count) return !closed_; // the PC runtime takes MaxCommandCount 0 as a no-op 877 if (!indirect.max_count) return !closed_; // the PC runtime takes MaxCommandCount 0 as a no-op
875 return append(indirect); 878 return append(indirect);
876 } 879 }
diff --git a/runtime/test/d3d12_execute_indirect_validation_test.cpp b/runtime/test/d3d12_execute_indirect_validation_test.cpp
new file mode 100644
index 000000000..03f61f131
--- /dev/null
+++ b/runtime/test/d3d12_execute_indirect_validation_test.cpp
@@ -0,0 +1,459 @@
1// Host-parity validation and GPU execution sanity test for ID3D12GraphicsCommandList::ExecuteIndirect.
2// Compares Close() verdicts between the Vulkan layer, live Windows WARP, and live hardware D3D12 (0 disagreements),
3// then executes valid ExecuteIndirect dispatches on the Vulkan GPU with validation enabled. No game data.
4#include "vk_test_device.h"
5#include "d3d12_test_shaders.h"
6#include "host_wine.h"
7#include "../shims/d3d12_com.h"
8#include "../src/shader_library.h"
9
10#include <dxgi1_4.h>
11
12#include <array>
13#include <cstdint>
14#include <cstdio>
15#include <cstring>
16#include <filesystem>
17#include <fstream>
18#include <functional>
19#include <limits>
20#include <string>
21#include <vector>
22
23#define CHECK(condition) do { \
24 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return false; } \
25} while (0)
26
27#define REQUIRE(condition) do { \
28 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
29} while (0)
30
31using namespace d3d12test;
32
33namespace {
34
35// Minimal valid SPIR-V 1.0 compute shader: `void main() {}` with LocalSize(1, 1, 1).
36constexpr std::array<std::uint32_t, 35> kMinimalComputeSpv = {
37 0x07230203u, 0x00010000u, 0u, 6u, 0u,
38 (2u << 16) | 17u, 1u, // OpCapability Shader
39 (3u << 16) | 14u, 0u, 1u, // OpMemoryModel Logical GLSL450
40 (5u << 16) | 15u, 5u, 4u, 0x6e69616du, 0x00000000u, // OpEntryPoint GLCompute %4 "main"
41 (6u << 16) | 16u, 4u, 17u, 1u, 1u, 1u, // OpExecutionMode %4 LocalSize 1 1 1
42 (2u << 16) | 19u, 2u, // %2 = OpTypeVoid
43 (3u << 16) | 33u, 3u, 2u, // %3 = OpTypeFunction %2
44 (5u << 16) | 54u, 2u, 4u, 0u, 3u, // %4 = OpFunction %2 None %3
45 (2u << 16) | 248u, 5u, // %5 = OpLabel
46 (1u << 16) | 253u, // OpReturn
47 (1u << 16) | 56u, // OpFunctionEnd
48};
49
50struct Env {
51 ID3D12Device* device = nullptr;
52 Ref<ID3D12Resource> buf256;
53 Ref<ID3D12Resource> count256;
54 Ref<ID3D12Resource> buf3;
55 Ref<ID3D12Resource> tex;
56 Ref<ID3D12CommandSignature> draw16;
57 Ref<ID3D12CommandSignature> draw20;
58 Ref<ID3D12CommandSignature> draw_indexed20;
59 Ref<ID3D12CommandSignature> draw_indexed24;
60 Ref<ID3D12CommandSignature> dispatch12;
61 Ref<ID3D12CommandSignature> dispatch20;
62 Ref<ID3D12RootSignature> empty_root;
63 Ref<ID3D12PipelineState> compute_pso;
64 ID3D12GraphicsCommandList* list = nullptr;
65
66 bool init(ID3D12Device* d, const std::vector<std::uint8_t>* layer_cs_container = nullptr) {
67 device = d;
68 const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
69 const auto mkbuf = [&](UINT64 bytes, Ref<ID3D12Resource>& out) {
70 const auto desc = buffer_desc(bytes);
71 return SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
72 D3D12_RESOURCE_STATE_COMMON, nullptr,
73 IID_PPV_ARGS(&out.p)));
74 };
75 CHECK(mkbuf(256, buf256));
76 CHECK(mkbuf(256, count256));
77 CHECK(mkbuf(3, buf3));
78
79 D3D12_RESOURCE_DESC td{};
80 td.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
81 td.Width = 64;
82 td.Height = 64;
83 td.DepthOrArraySize = 1;
84 td.MipLevels = 1;
85 td.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
86 td.SampleDesc.Count = 1;
87 CHECK(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &td,
88 D3D12_RESOURCE_STATE_COMMON, nullptr,
89 IID_PPV_ARGS(&tex.p))));
90
91 const auto mksig = [&](D3D12_INDIRECT_ARGUMENT_TYPE type, UINT stride, Ref<ID3D12CommandSignature>& out) {
92 D3D12_INDIRECT_ARGUMENT_DESC arg{};
93 arg.Type = type;
94 D3D12_COMMAND_SIGNATURE_DESC desc{stride, 1, &arg, 0};
95 return SUCCEEDED(device->CreateCommandSignature(&desc, nullptr, IID_PPV_ARGS(&out.p)));
96 };
97 CHECK(mksig(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, 16, draw16));
98 CHECK(mksig(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, 20, draw20));
99 CHECK(mksig(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED, 20, draw_indexed20));
100 CHECK(mksig(D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED, 24, draw_indexed24));
101 CHECK(mksig(D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, 12, dispatch12));
102 CHECK(mksig(D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, 20, dispatch20));
103
104 if (layer_cs_container) {
105 D3D12_VERSIONED_ROOT_SIGNATURE_DESC rs_desc{};
106 rs_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0;
107 rs_desc.Desc_1_0 = {0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE};
108 const auto serialized = serialize_root_signature(rs_desc);
109 CHECK(!serialized.empty());
110 CHECK(SUCCEEDED(device->CreateRootSignature(0, serialized.data(), serialized.size(),
111 IID_PPV_ARGS(&empty_root.p))));
112 D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc{};
113 pso_desc.pRootSignature = empty_root.p;
114 pso_desc.CS = {layer_cs_container->data(), layer_cs_container->size()};
115 CHECK(SUCCEEDED(device->CreateComputePipelineState(&pso_desc, IID_PPV_ARGS(&compute_pso.p))));
116 }
117 return true;
118 }
119
120 void bind_compute_if_available() const {
121 if (empty_root.p) list->SetComputeRootSignature(empty_root.p);
122 if (compute_pso.p) list->SetPipelineState(compute_pso.p);
123 }
124};
125
126struct Case {
127 const char* name;
128 bool expected_ok; // Measured on Windows 11 WARP and RTX 5090 HW D3D12
129 std::function<void(Env&)> record;
130};
131
132bool run_case(Env& env, const Case& c, HRESULT& out_hr) {
133 Ref<ID3D12CommandAllocator> alloc;
134 Ref<ID3D12GraphicsCommandList> list;
135 CHECK(SUCCEEDED(env.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p))));
136 CHECK(SUCCEEDED(env.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, nullptr,
137 IID_PPV_ARGS(&list.p))));
138 env.list = list.p;
139 c.record(env);
140 env.list = nullptr;
141 out_hr = list->Close();
142 return true;
143}
144
145std::vector<Case> parity_cases() {
146 constexpr UINT64 kU64Max = std::numeric_limits<UINT64>::max();
147 constexpr UINT kU32Max = std::numeric_limits<UINT>::max();
148 return {
149 // Valid baselines
150 {"valid draw max=1 arg=0 cnt=null", true,
151 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, nullptr, 0); }},
152 {"valid draw max=1 arg=0 cnt=buf@0", true,
153 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, 0); }},
154 {"valid draw max=0 arg=0 cnt=null", true,
155 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, nullptr, 0); }},
156 {"valid draw_indexed max=1 arg=0 cnt=null", true,
157 [](Env& e) { e.list->ExecuteIndirect(e.draw_indexed20.p, 1, e.buf256.p, 0, nullptr, 0); }},
158 {"valid dispatch max=1 arg=0 cnt=null", true,
159 [](Env& e) {
160 e.bind_compute_if_available();
161 e.list->ExecuteIndirect(e.dispatch12.p, 1, e.buf256.p, 0, nullptr, 0);
162 }},
163 {"valid same buffer for arg and count", true,
164 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.buf256.p, 252); }},
165
166 // Non-buffer resources (Dimension != BUFFER)
167 {"argument_buffer is texture (max=1)", false,
168 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.tex.p, 0, nullptr, 0); }},
169 {"argument_buffer is texture (max=0)", false,
170 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.tex.p, 0, nullptr, 0); }},
171 {"count_buffer is texture (max=1)", false,
172 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.tex.p, 0); }},
173 {"count_buffer is texture (max=0)", false,
174 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, e.tex.p, 0); }},
175
176 // 4-byte offset alignment
177 {"arg_offset=1 unaligned (max=1)", false,
178 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 1, nullptr, 0); }},
179 {"arg_offset=2 unaligned (max=1)", false,
180 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 2, nullptr, 0); }},
181 {"arg_offset=2 unaligned (max=0)", false,
182 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 2, nullptr, 0); }},
183 {"arg_offset=4 aligned (max=1)", true,
184 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 4, nullptr, 0); }},
185 {"count_offset=1 unaligned (max=1)", false,
186 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, 1); }},
187 {"count_offset=2 unaligned (max=1)", false,
188 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, 2); }},
189 {"count_offset=2 unaligned (max=0)", false,
190 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, e.count256.p, 2); }},
191 {"count_offset=4 aligned (max=1)", true,
192 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, 4); }},
193
194 // Null count buffer with non-zero count offset
195 {"count_buffer=null count_offset=4 (max=1)", false,
196 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, nullptr, 4); }},
197 {"count_buffer=null count_offset=2 (max=1)", false,
198 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, nullptr, 2); }},
199 {"count_buffer=null count_offset=4 (max=0)", false,
200 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, nullptr, 4); }},
201 {"count_buffer=null count_offset=U64MAX (max=1)", false,
202 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, nullptr, kU64Max); }},
203
204 // Argument buffer bounds (exact fit, OOB, overflow wrap, max=0 vs max>0)
205 {"arg_offset=240 exact fit (240+16==256, max=1)", true,
206 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 240, nullptr, 0); }},
207 {"arg_offset=244 OOB (244+16>256, max=1)", false,
208 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 244, nullptr, 0); }},
209 {"arg_offset=256 ==width (max=1)", false,
210 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 256, nullptr, 0); }},
211 {"arg_offset=260 >width (max=1)", false,
212 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 260, nullptr, 0); }},
213 {"arg_offset=256 ==width (max=0 no-op)", true,
214 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 256, nullptr, 0); }},
215 {"arg_offset=260 >width (max=0 no-op)", true,
216 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 260, nullptr, 0); }},
217 {"arg_offset=U64MAX-3 wrap (max=1)", false,
218 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, kU64Max - 3, nullptr, 0); }},
219 {"arg_offset=U64MAX-3 aligned (max=0 no-op)", true,
220 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, kU64Max - 3, nullptr, 0); }},
221 {"arg_offset=U64MAX unaligned+wrap (max=1)", false,
222 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, kU64Max, nullptr, 0); }},
223 {"arg_offset=240 max=2 cnt=null (OOB)", false,
224 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 2, e.buf256.p, 240, nullptr, 0); }},
225 {"arg_offset=240 max=2 cnt=buf (OOB)", false,
226 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 2, e.buf256.p, 240, e.count256.p, 0); }},
227 {"arg_offset=0 max=U32MAX cnt=null (OOB)", false,
228 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, kU32Max, e.buf256.p, 0, nullptr, 0); }},
229 {"arg_offset=0 max=U32MAX cnt=buf (OOB)", false,
230 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, kU32Max, e.buf256.p, 0, e.count256.p, 0); }},
231 {"arg_buf width=3 (max=0 no-op)", true,
232 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf3.p, 0, nullptr, 0); }},
233 {"arg_buf width=3 (max=1 OOB)", false,
234 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf3.p, 0, nullptr, 0); }},
235
236 // Padded stride exact-fit: (max_count - 1) * stride + record_size
237 {"draw stride=20 off=240 max=1 (240+16==256)", true,
238 [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 1, e.buf256.p, 240, nullptr, 0); }},
239 {"draw stride=20 off=220 max=2 (220+20+16==256)", true,
240 [](Env& e) { e.list->ExecuteIndirect(e.draw20.p, 2, e.buf256.p, 220, nullptr, 0); }},
241 {"dispatch stride=20 off=244 max=1 (244+12==256)", true,
242 [](Env& e) {
243 e.bind_compute_if_available();
244 e.list->ExecuteIndirect(e.dispatch20.p, 1, e.buf256.p, 244, nullptr, 0);
245 }},
246 {"draw_indexed stride=24 off=240 max=1 (OOB)", false,
247 [](Env& e) { e.list->ExecuteIndirect(e.draw_indexed24.p, 1, e.buf256.p, 240, nullptr, 0); }},
248
249 // Count buffer bounds (checked even when max=0)
250 {"count_offset=252 exact fit (252+4==256, max=1)", true,
251 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, 252); }},
252 {"count_offset=256 ==width (max=1)", false,
253 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, 256); }},
254 {"count_offset=256 ==width (max=0)", false,
255 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, e.count256.p, 256); }},
256 {"count_offset=260 >width (max=0)", false,
257 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, e.count256.p, 260); }},
258 {"count_offset=U64MAX-3 wrap (max=1)", false,
259 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.count256.p, kU64Max - 3); }},
260 {"count_offset=U64MAX-3 wrap (max=0)", false,
261 [=](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, e.count256.p, kU64Max - 3); }},
262 {"count_buf width=3 off=0 (max=1)", false,
263 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, e.buf256.p, 0, e.buf3.p, 0); }},
264 {"count_buf width=3 off=0 (max=0)", false,
265 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, e.buf256.p, 0, e.buf3.p, 0); }},
266 };
267}
268
269bool run_positive_gpu_dispatch(Env& env) {
270 const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD};
271 const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
272 const auto desc256 = buffer_desc(256);
273 Ref<ID3D12Resource> upload, args;
274 CHECK(SUCCEEDED(env.device->CreateCommittedResource(&upload_heap, D3D12_HEAP_FLAG_NONE, &desc256,
275 D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
276 IID_PPV_ARGS(&upload.p))));
277 CHECK(SUCCEEDED(env.device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc256,
278 D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
279 IID_PPV_ARGS(&args.p))));
280 void* mapped = nullptr;
281 CHECK(SUCCEEDED(upload->Map(0, nullptr, &mapped)) && mapped);
282 std::memset(mapped, 0, 256);
283 const D3D12_DISPATCH_ARGUMENTS dispatch_args{1, 1, 1};
284 std::memcpy(static_cast<std::uint8_t*>(mapped) + 244, &dispatch_args, sizeof(dispatch_args));
285 upload->Unmap(0, nullptr);
286
287 const D3D12_COMMAND_QUEUE_DESC qdesc{D3D12_COMMAND_LIST_TYPE_DIRECT};
288 Ref<ID3D12CommandQueue> queue;
289 Ref<ID3D12CommandAllocator> alloc;
290 Ref<ID3D12GraphicsCommandList> list;
291 Ref<ID3D12Fence> fence;
292 CHECK(SUCCEEDED(env.device->CreateCommandQueue(&qdesc, IID_PPV_ARGS(&queue.p))));
293 CHECK(SUCCEEDED(env.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p))));
294 CHECK(SUCCEEDED(env.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, env.compute_pso.p,
295 IID_PPV_ARGS(&list.p))));
296 CHECK(SUCCEEDED(env.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
297
298 list->CopyBufferRegion(args.p, 244, upload.p, 244, sizeof(dispatch_args));
299 const auto to_indirect = transition(args.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
300 list->ResourceBarrier(1, &to_indirect);
301 list->SetComputeRootSignature(env.empty_root.p);
302 list->SetPipelineState(env.compute_pso.p);
303 // Exact-fit padded-stride indirect dispatch (244 + 12 == 256) plus max_command_count == 0 no-op.
304 list->ExecuteIndirect(env.dispatch20.p, 1, args.p, 244, nullptr, 0);
305 list->ExecuteIndirect(env.dispatch20.p, 0, args.p, 260, nullptr, 0);
306 CHECK(SUCCEEDED(list->Close()));
307
308 ID3D12CommandList* lists[] = {list.p};
309 queue->ExecuteCommandLists(1, lists);
310 CHECK(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
311 CHECK(SUCCEEDED(queue->Signal(fence.p, 1)));
312 const HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr);
313 CHECK(ev != nullptr);
314 const bool done = SUCCEEDED(fence->SetEventOnCompletion(1, ev)) && WaitForSingleObject(ev, 20000) == WAIT_OBJECT_0;
315 CloseHandle(ev);
316 CHECK(done);
317 return true;
318}
319
320} // namespace
321
322int main() {
323 std::setvbuf(stdout, nullptr, _IONBF, 0);
324 vktest::Device vk;
325 vk.request_buffer_device_address = true;
326 const auto status = vk.initialize();
327 if (status == vktest::DeviceStatus::unavailable) {
328 std::fprintf(stderr, "SKIP: no usable Vulkan 1.3 device\n");
329 return 77;
330 }
331 REQUIRE(status == vktest::DeviceStatus::ready);
332 const unsigned errors_before = vk.validation_errors();
333
334 // Write minimal synthetic compute shader fixture for positive ExecuteIndirect dispatch.
335 std::vector<std::uint8_t> program = fssm_program({0x2200u, 0x11u, 0x22u, 0x33u});
336 const auto container = shader_container(program);
337 const auto fixture = std::filesystem::temp_directory_path() /
338 ("leonida_exec_ind_val_" + std::to_string(GetCurrentProcessId()));
339 std::filesystem::create_directories(fixture);
340 {
341 std::ofstream spv(fixture / "noop.spv", std::ios::binary);
342 spv.write(reinterpret_cast<const char*>(kMinimalComputeSpv.data()),
343 static_cast<std::streamsize>(kMinimalComputeSpv.size() * sizeof(std::uint32_t)));
344 std::ofstream index(fixture / "index.json", std::ios::binary);
345 index << "{\"noop\":{\"stage\":\"cs\",\"fssm_sha256\":\"" << leonida::sha256_hex(program)
346 << "\",\"bindings\":[],\"push_constant_bytes\":0,\"local_size\":[1,1,1],"
347 "\"uses_code_base\":false,\"wave64\":false}}";
348 }
349
350 const auto table = parity_cases();
351 std::vector<HRESULT> layer_hr(table.size(), E_FAIL);
352 std::vector<HRESULT> warp_hr(table.size(), E_FAIL);
353 std::vector<HRESULT> hw_hr(table.size(), E_FAIL);
354
355 {
356 rt::d3d12::VulkanDeviceInfo info{};
357 info.get_instance_proc = vk.instance_proc;
358 info.instance = vk.instance;
359 info.physical = vk.physical;
360 info.device = vk.device;
361 info.queue = vk.queue;
362 info.queue_family = vk.family;
363 info.buffer_device_address = vk.buffer_device_address;
364 info.shader_dir = fixture.string();
365 Ref<ID3D12Device> ours;
366 REQUIRE(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p))));
367 Env env;
368 REQUIRE(env.init(ours.p, &container));
369 for (std::size_t i = 0; i < table.size(); ++i) REQUIRE(run_case(env, table[i], layer_hr[i]));
370
371 // Layer-only null-pointer safety checks (Windows D3D12 dereferences these and crashes).
372 HRESULT null_hr = S_OK;
373 REQUIRE(run_case(env, {"null arg max=1", false,
374 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 1, nullptr, 0, nullptr, 0); }},
375 null_hr));
376 REQUIRE(FAILED(null_hr));
377 REQUIRE(run_case(env, {"null arg max=0", false,
378 [](Env& e) { e.list->ExecuteIndirect(e.draw16.p, 0, nullptr, 0, nullptr, 0); }},
379 null_hr));
380 REQUIRE(FAILED(null_hr));
381 REQUIRE(run_case(env, {"null sig max=1", false,
382 [](Env& e) { e.list->ExecuteIndirect(nullptr, 1, e.buf256.p, 0, nullptr, 0); }},
383 null_hr));
384 REQUIRE(FAILED(null_hr));
385
386 // Positive valid GPU ExecuteIndirect dispatch on Vulkan.
387 REQUIRE(run_positive_gpu_dispatch(env));
388 }
389
390 bool warp_ran = false;
391 bool hw_ran = false;
392 std::string hw_name;
393 if (!hostwine::host_is_wine()) {
394 Ref<IDXGIFactory4> factory;
395 if (SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) {
396 Ref<IDXGIAdapter> warp_adapter;
397 Ref<ID3D12Device> warp_dev;
398 if (SUCCEEDED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp_adapter.p))) &&
399 SUCCEEDED(D3D12CreateDevice(warp_adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&warp_dev.p)))) {
400 Env env;
401 REQUIRE(env.init(warp_dev.p));
402 for (std::size_t i = 0; i < table.size(); ++i) REQUIRE(run_case(env, table[i], warp_hr[i]));
403 REQUIRE(warp_dev->GetDeviceRemovedReason() == S_OK);
404 warp_ran = true;
405 }
406 for (UINT i = 0;; ++i) {
407 Ref<IDXGIAdapter1> adapter;
408 if (factory->EnumAdapters1(i, &adapter.p) == DXGI_ERROR_NOT_FOUND) break;
409 DXGI_ADAPTER_DESC1 desc{};
410 adapter->GetDesc1(&desc);
411 if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) continue;
412 Ref<ID3D12Device> hw_dev;
413 if (SUCCEEDED(D3D12CreateDevice(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&hw_dev.p)))) {
414 Env env;
415 REQUIRE(env.init(hw_dev.p));
416 for (std::size_t j = 0; j < table.size(); ++j) REQUIRE(run_case(env, table[j], hw_hr[j]));
417 REQUIRE(hw_dev->GetDeviceRemovedReason() == S_OK);
418 char name_buf[256]{};
419 std::snprintf(name_buf, sizeof(name_buf), "%ls", desc.Description);
420 hw_name = name_buf;
421 hw_ran = true;
422 break;
423 }
424 }
425 }
426 }
427
428 std::error_code ec;
429 std::filesystem::remove_all(fixture, ec);
430
431 unsigned failures = 0;
432 std::printf("%-48s | %-10s | %-10s | %-10s\n", "case", "layer", "WARP", hw_ran ? hw_name.c_str() : "HW");
433 for (std::size_t i = 0; i < table.size(); ++i) {
434 const bool layer_ok = SUCCEEDED(layer_hr[i]);
435 const bool warp_ok = warp_ran ? SUCCEEDED(warp_hr[i]) : table[i].expected_ok;
436 const bool hw_ok = hw_ran ? SUCCEEDED(hw_hr[i]) : table[i].expected_ok;
437 std::printf("%-48s | 0x%08lx | %-10s | %-10s\n",
438 table[i].name,
439 static_cast<unsigned long>(layer_hr[i]),
440 warp_ran ? (SUCCEEDED(warp_hr[i]) ? "S_OK" : "FAIL") : (warp_ok ? "(S_OK)" : "(FAIL)"),
441 hw_ran ? (SUCCEEDED(hw_hr[i]) ? "S_OK" : "FAIL") : (hw_ok ? "(S_OK)" : "(FAIL)"));
442 if (layer_ok != warp_ok || layer_ok != hw_ok || layer_ok != table[i].expected_ok) {
443 std::fprintf(stderr, "FAIL %s: layer=%d warp=%d hw=%d expected=%d\n",
444 table[i].name, int(layer_ok), int(warp_ok), int(hw_ok), int(table[i].expected_ok));
445 ++failures;
446 }
447 }
448
449 const unsigned validation_errors = vk.validation_errors() - errors_before;
450 if (validation_errors) {
451 std::fprintf(stderr, "%u Vulkan validation errors\n", validation_errors);
452 ++failures;
453 }
454 std::printf("execute_indirect_validation: %zu parity cases (WARP=%s, HW=%s), positive GPU dispatch ok, "
455 "%u validation errors, %u failures\n",
456 table.size(), warp_ran ? "live" : "measured", hw_ran ? hw_name.c_str() : "measured",
457 validation_errors, failures);
458 return failures ? 1 : 0;
459}
diff --git a/runtime/test/d3d12_state_map_test.cpp b/runtime/test/d3d12_state_map_test.cpp
index d7466b781..9db6ad0c1 100644
--- a/runtime/test/d3d12_state_map_test.cpp
+++ b/runtime/test/d3d12_state_map_test.cpp
@@ -614,7 +614,9 @@ int main() {
614 replay(invalid, VK_NULL_HANDLE, resolve, vk) == ReplayResult::invalid_indirect_arguments; 614 replay(invalid, VK_NULL_HANDLE, resolve, vk) == ReplayResult::invalid_indirect_arguments;
615 }; 615 };
616 CHECK(rejected({IndirectKind::draw, 12, 1, 1, 0, 0, 0})); 616 CHECK(rejected({IndirectKind::draw, 12, 1, 1, 0, 0, 0}));
617 CHECK(rejected({IndirectKind::draw, 16, 1, 1, 2, 0, 0})); 617 CHECK(!CommandList{}.execute_indirect({IndirectKind::draw, 16, 1, 1, 2, 0, 0}));
618 CHECK(!CommandList{}.execute_indirect({IndirectKind::draw, 16, 1, 1, 0, 2, 2}));
619 CHECK(!CommandList{}.execute_indirect({IndirectKind::draw, 16, 1, 1, 0, 0, 4}));
618 CHECK(rejected({IndirectKind::draw, 16, 2, 1, 112, 0, 0})); 620 CHECK(rejected({IndirectKind::draw, 16, 2, 1, 112, 0, 0}));
619 CHECK(rejected({IndirectKind::draw, 16, 1, 1, 4, 2, 16})); 621 CHECK(rejected({IndirectKind::draw, 16, 1, 1, 4, 2, 16}));
620 arguments.buffer_offset = UINT64_MAX - 3; 622 arguments.buffer_offset = UINT64_MAX - 3;