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:
authormohamedsaad.lahlou <169635474+lahlousaad4@users.noreply.github.com>2026-11-22 03:07:36 +0100
committermohamedsaad.lahlou <169635474+lahlousaad4@users.noreply.github.com>2026-11-22 03:07:36 +0100
commit4e1de429fdaaef0a5a5b84548e9b29e4b4c67d45 (patch)
tree428dc6b5cef8d04caff5c8528e240bcc16763462 /runtime
parentc432cdce127ed2d22a99aa6ef57d91db4a10b5bb (diff)
Runtime: add opt-in D3D12 call tracing
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt2
-rw-r--r--runtime/README.md15
-rw-r--r--runtime/shims/d3d12_com.cpp55
-rw-r--r--runtime/shims/d3d12_trace.h36
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp96
-rw-r--r--runtime/shims/d3d12_xs.cpp66
-rw-r--r--runtime/shims/d3d12_xs_device.cpp11
-rw-r--r--runtime/src/main.cpp11
-rw-r--r--runtime/test/check_d3d12_trace.py61
-rw-r--r--runtime/test/d3d12_com_test.cpp7
-rw-r--r--runtime/test/d3d12_gcl8_test.cpp11
11 files changed, 279 insertions, 92 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index 4cf9bd183..976559cc7 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -497,6 +497,8 @@ if(TARGET Vulkan::Headers)
497 target_compile_definitions(d3d12_gcl8_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) 497 target_compile_definitions(d3d12_gcl8_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
498 target_compile_options(d3d12_gcl8_test PRIVATE /W4 /permissive- /utf-8) 498 target_compile_options(d3d12_gcl8_test PRIVATE /W4 /permissive- /utf-8)
499 add_test(NAME d3d12_gcl8 COMMAND d3d12_gcl8_test) 499 add_test(NAME d3d12_gcl8 COMMAND d3d12_gcl8_test)
500 add_test(NAME d3d12_trace COMMAND Python3::Interpreter -I
501 "${CMAKE_CURRENT_SOURCE_DIR}/test/check_d3d12_trace.py" "$<TARGET_FILE:d3d12_gcl8_test>")
500 502
501 # The Vulkan device behind the Xbox-ABI proxy, end to end. 503 # The Vulkan device behind the Xbox-ABI proxy, end to end.
502 add_executable(d3d12_vulkan_device_test test/d3d12_vulkan_device_test.cpp) 504 add_executable(d3d12_vulkan_device_test test/d3d12_vulkan_device_test.cpp)
diff --git a/runtime/README.md b/runtime/README.md
index a234d84a1..74212ff75 100644
--- a/runtime/README.md
+++ b/runtime/README.md
@@ -188,6 +188,21 @@ work.
188 188
189No game data, keys, or extracted assets belong in this directory or in the repository. 189No game data, keys, or extracted assets belong in this directory or in the repository.
190 190
191### First-boot D3D12 trace
192
193On a Vulkan-enabled build, select the Vulkan backend to trace device, command-list and queue calls handled
194by Leonida. Put `--trace-d3d12` before the separator for game arguments.
195
196```powershell
197$env:LEONIDA_D3D12 = 'vulkan'
198.\runtime\build\gta6rt.exe --run $gameExe --trace-d3d12 2>&1 |
199 Select-String '^\[d3d12-trace\]'
200```
201
202Each trace line contains a sequence number, a fixed API name and an outcome. `called` marks a void API call
203whose validation may be deferred. `ok`, `failed` and `unsupported` report known results. `ignored` marks a no-op.
204Share only the lines prefixed with `[d3d12-trace]`. Other runtime diagnostics are separate.
205
191### Typed clear regression 206### Typed clear regression
192 207
193`clear_color_test` checks numeric UINT conversion and unchanged float clears without a GPU. 208`clear_color_test` checks numeric UINT conversion and unchanged float clears without a GPU.
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 14bad2f9c..c45c5f534 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -2,6 +2,7 @@
2 2
3#include "d3d12_stubs.h" // generated 3#include "d3d12_stubs.h" // generated
4#include "d3d12_com_object.h" 4#include "d3d12_com_object.h"
5#include "d3d12_trace.h"
5 6
6#include <atomic> 7#include <atomic>
7#include <condition_variable> 8#include <condition_variable>
@@ -17,6 +18,8 @@
17namespace rt::d3d12 { 18namespace rt::d3d12 {
18 19
19void stub(const char* method) { 20void stub(const char* method) {
21 // Generated API stubs pass a bare method name; explanatory diagnostics are not separate calls.
22 if (!std::strchr(method, '(')) d3d12_trace::record(method, "unsupported");
20 static std::mutex mutex; 23 static std::mutex mutex;
21 static std::set<std::string> seen; 24 static std::set<std::string> seen;
22 std::lock_guard lock(mutex); 25 std::lock_guard lock(mutex);
@@ -78,27 +81,34 @@ public:
78 UINT front_stencil_ref() const { return front_stencil_ref_; } 81 UINT front_stencil_ref() const { return front_stencil_ref_; }
79 UINT back_stencil_ref() const { return back_stencil_ref_; } 82 UINT back_stencil_ref() const { return back_stencil_ref_; }
80 83
81 D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override { return type_; } 84 D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override {
85 d3d12_trace::record("ID3D12CommandList::GetType");
86 return type_;
87 }
82 D3D12_COMMAND_LIST_TYPE type() const { return type_; } 88 D3D12_COMMAND_LIST_TYPE type() const { return type_; }
83 89
84 // E_INVALIDARG when a call during recording was invalid, as D3D12's Close reports it. 90 // E_INVALIDARG when a call during recording was invalid, as D3D12's Close reports it.
85 HRESULT STDMETHODCALLTYPE Close() override { 91 HRESULT STDMETHODCALLTYPE Close() override {
86 if (!list_.close()) return E_FAIL; 92 constexpr auto method = "ID3D12GraphicsCommandList::Close";
87 return invalid_ ? E_INVALIDARG : S_OK; 93 if (!list_.close()) return d3d12_trace::result(method, E_FAIL);
94 return d3d12_trace::result(method, invalid_ ? E_INVALIDARG : S_OK);
88 } 95 }
89 96
90 HRESULT STDMETHODCALLTYPE Reset(ID3D12CommandAllocator* allocator, ID3D12PipelineState* state) override { 97 HRESULT STDMETHODCALLTYPE Reset(ID3D12CommandAllocator* allocator, ID3D12PipelineState* state) override {
98 constexpr auto method = "ID3D12GraphicsCommandList::Reset";
91 CommandAllocator* command_allocator = CommandAllocator::from(allocator); 99 CommandAllocator* command_allocator = CommandAllocator::from(allocator);
92 if (!command_allocator || command_allocator->type() != type_) return E_INVALIDARG; 100 if (!command_allocator || command_allocator->type() != type_)
93 if (!list_.reset()) return E_FAIL; // D3D12 resets only closed lists 101 return d3d12_trace::result(method, E_INVALIDARG);
102 if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists
94 invalid_ = false; 103 invalid_ = false;
95 front_stencil_ref_ = back_stencil_ref_ = 0; // Reset returns every list state to its default 104 front_stencil_ref_ = back_stencil_ref_ = 0; // Reset returns every list state to its default
96 stencil_ref_set_ = false; 105 stencil_ref_set_ = false;
97 if (state) stub("ID3D12GraphicsCommandList::Reset(initial pipeline state)"); 106 if (state) stub("ID3D12GraphicsCommandList::Reset(initial pipeline state)");
98 return S_OK; 107 return d3d12_trace::result(method, S_OK);
99 } 108 }
100 109
101 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { 110 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override {
111 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle");
102 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); 112 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
103 GraphicsCommandList* bundle = from(command_list); 113 GraphicsCommandList* bundle = from(command_list);
104 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || 114 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ ||
@@ -115,6 +125,7 @@ public:
115 } 125 }
116 126
117 void STDMETHODCALLTYPE ResourceBarrier(UINT count, const D3D12_RESOURCE_BARRIER* barriers) override { 127 void STDMETHODCALLTYPE ResourceBarrier(UINT count, const D3D12_RESOURCE_BARRIER* barriers) override {
128 d3d12_trace::record("ID3D12GraphicsCommandList::ResourceBarrier");
118 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 129 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
119 for (UINT i = 0; i < count; ++i) { 130 for (UINT i = 0; i < count; ++i) {
120 const auto& b = barriers[i]; 131 const auto& b = barriers[i];
@@ -139,6 +150,7 @@ public:
139 } 150 }
140 151
141 void STDMETHODCALLTYPE Barrier(UINT32 groups, const D3D12_BARRIER_GROUP* group) override { 152 void STDMETHODCALLTYPE Barrier(UINT32 groups, const D3D12_BARRIER_GROUP* group) override {
153 d3d12_trace::record("ID3D12GraphicsCommandList7::Barrier");
142 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 154 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
143 for (UINT32 g = 0; g < groups; ++g) { 155 for (UINT32 g = 0; g < groups; ++g) {
144 for (UINT32 i = 0; i < group[g].NumBarriers; ++i) { 156 for (UINT32 i = 0; i < group[g].NumBarriers; ++i) {
@@ -175,12 +187,14 @@ public:
175 187
176 void STDMETHODCALLTYPE CopyBufferRegion(ID3D12Resource* dst, UINT64 dst_offset, ID3D12Resource* src, 188 void STDMETHODCALLTYPE CopyBufferRegion(ID3D12Resource* dst, UINT64 dst_offset, ID3D12Resource* src,
177 UINT64 src_offset, UINT64 bytes) override { 189 UINT64 src_offset, UINT64 bytes) override {
190 d3d12_trace::record("ID3D12GraphicsCommandList::CopyBufferRegion");
178 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 191 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
179 check(list_.copy_buffer({resource_id(dst), dst_offset, resource_id(src), src_offset, bytes})); 192 check(list_.copy_buffer({resource_id(dst), dst_offset, resource_id(src), src_offset, bytes}));
180 } 193 }
181 194
182 void STDMETHODCALLTYPE CopyTextureRegion(const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z, 195 void STDMETHODCALLTYPE CopyTextureRegion(const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z,
183 const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) override { 196 const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) override {
197 d3d12_trace::record("ID3D12GraphicsCommandList::CopyTextureRegion");
184 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 198 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
185 if (!dst || !src) return check(false); 199 if (!dst || !src) return check(false);
186 leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}}; 200 leonida::CopyTexture copy{location(*dst), x, y, z, location(*src), box != nullptr, {}};
@@ -189,6 +203,7 @@ public:
189 } 203 }
190 204
191 void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override { 205 void STDMETHODCALLTYPE CopyResource(ID3D12Resource* dst, ID3D12Resource* src) override {
206 d3d12_trace::record("ID3D12GraphicsCommandList::CopyResource");
192 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 207 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
193 check(list_.copy_resource({resource_id(dst), resource_id(src)})); 208 check(list_.copy_resource({resource_id(dst), resource_id(src)}));
194 } 209 }
@@ -196,6 +211,7 @@ public:
196 // The format picks the member of a typeless family to resolve as; the textures' own format is used. 211 // The format picks the member of a typeless family to resolve as; the textures' own format is used.
197 void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, 212 void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src,
198 UINT src_subresource, DXGI_FORMAT) override { 213 UINT src_subresource, DXGI_FORMAT) override {
214 d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource");
199 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 215 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
200 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource})); 216 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource}));
201 } 217 }
@@ -203,6 +219,7 @@ public:
203 void STDMETHODCALLTYPE ResolveSubresourceRegion(ID3D12Resource* dst, UINT dst_subresource, UINT x, UINT y, 219 void STDMETHODCALLTYPE ResolveSubresourceRegion(ID3D12Resource* dst, UINT dst_subresource, UINT x, UINT y,
204 ID3D12Resource* src, UINT src_subresource, D3D12_RECT* rect, 220 ID3D12Resource* src, UINT src_subresource, D3D12_RECT* rect,
205 DXGI_FORMAT format, D3D12_RESOLVE_MODE mode) override { 221 DXGI_FORMAT format, D3D12_RESOLVE_MODE mode) override {
222 d3d12_trace::record("ID3D12GraphicsCommandList1::ResolveSubresourceRegion");
206 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT || mode != D3D12_RESOLVE_MODE_AVERAGE || 223 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT || mode != D3D12_RESOLVE_MODE_AVERAGE ||
207 !color_resolve_format_matches(format, dst, src)) 224 !color_resolve_format_matches(format, dst, src))
208 return check(false); 225 return check(false);
@@ -213,12 +230,14 @@ public:
213 230
214 // A null buffer turns predication off (docs/d3d12_vulkan.md section 4.7). Bundles may not set it. 231 // A null buffer turns predication off (docs/d3d12_vulkan.md section 4.7). Bundles may not set it.
215 void STDMETHODCALLTYPE SetPredication(ID3D12Resource* buffer, UINT64 offset, D3D12_PREDICATION_OP op) override { 232 void STDMETHODCALLTYPE SetPredication(ID3D12Resource* buffer, UINT64 offset, D3D12_PREDICATION_OP op) override {
233 d3d12_trace::record("ID3D12GraphicsCommandList::SetPredication");
216 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 234 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
217 check(list_.set_predication({resource_id(buffer), offset, u(op)})); 235 check(list_.set_predication({resource_id(buffer), offset, u(op)}));
218 } 236 }
219 237
220 void STDMETHODCALLTYPE ClearRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE view, const FLOAT color[4], UINT count, 238 void STDMETHODCALLTYPE ClearRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE view, const FLOAT color[4], UINT count,
221 const D3D12_RECT* rects) override { 239 const D3D12_RECT* rects) override {
240 d3d12_trace::record("ID3D12GraphicsCommandList::ClearRenderTargetView");
222 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 241 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
223 if (!view.ptr || !color) return check(false); 242 if (!view.ptr || !color) return check(false);
224 check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr), 243 check(list_.clear_render_target(*reinterpret_cast<const leonida::TargetView*>(view.ptr),
@@ -228,6 +247,7 @@ public:
228 void STDMETHODCALLTYPE ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE view, D3D12_CLEAR_FLAGS flags, 247 void STDMETHODCALLTYPE ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE view, D3D12_CLEAR_FLAGS flags,
229 FLOAT depth, UINT8 stencil, UINT count, 248 FLOAT depth, UINT8 stencil, UINT count,
230 const D3D12_RECT* rects) override { 249 const D3D12_RECT* rects) override {
250 d3d12_trace::record("ID3D12GraphicsCommandList::ClearDepthStencilView");
231 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 251 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
232 if (!view.ptr) return check(false); 252 if (!view.ptr) return check(false);
233 check(list_.clear_depth_stencil(*reinterpret_cast<const leonida::TargetView*>(view.ptr), u(flags), depth, 253 check(list_.clear_depth_stencil(*reinterpret_cast<const leonida::TargetView*>(view.ptr), u(flags), depth,
@@ -239,6 +259,7 @@ public:
239 // yet. Retain the values for that path and report that nothing consumed them, rather than emit a call whose 259 // yet. Retain the values for that path and report that nothing consumed them, rather than emit a call whose
240 // effect no later command can observe. 260 // effect no later command can observe.
241 void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { 261 void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override {
262 d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef");
242 if (list_.closed()) return check(false); 263 if (list_.closed()) return check(false);
243 front_stencil_ref_ = front; 264 front_stencil_ref_ = front;
244 back_stencil_ref_ = back; 265 back_stencil_ref_ = back;
@@ -384,9 +405,13 @@ public:
384 bool ok() const { return queue_.ok(); } 405 bool ok() const { return queue_.ok(); }
385 leonida::ReplayResult last_result() const { return last_result_; } 406 leonida::ReplayResult last_result() const { return last_result_; }
386 407
387 D3D12_COMMAND_QUEUE_DESC STDMETHODCALLTYPE GetDesc() override { return desc_; } 408 D3D12_COMMAND_QUEUE_DESC STDMETHODCALLTYPE GetDesc() override {
409 d3d12_trace::record("ID3D12CommandQueue::GetDesc");
410 return desc_;
411 }
388 412
389 void STDMETHODCALLTYPE ExecuteCommandLists(UINT count, ID3D12CommandList* const* lists) override { 413 void STDMETHODCALLTYPE ExecuteCommandLists(UINT count, ID3D12CommandList* const* lists) override {
414 constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists";
390 std::lock_guard lock(mutex_); 415 std::lock_guard lock(mutex_);
391 std::vector<const leonida::CommandList*> recorded; 416 std::vector<const leonida::CommandList*> recorded;
392 for (UINT i = 0; i < count; ++i) { 417 for (UINT i = 0; i < count; ++i) {
@@ -394,11 +419,13 @@ public:
394 if (!list || list->type() == D3D12_COMMAND_LIST_TYPE_BUNDLE) { 419 if (!list || list->type() == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
395 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists: list %u is not a direct command list\n", i); 420 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists: list %u is not a direct command list\n", i);
396 last_result_ = leonida::ReplayResult::unsupported; 421 last_result_ = leonida::ReplayResult::unsupported;
422 d3d12_trace::record(method, "failed");
397 return; 423 return;
398 } 424 }
399 recorded.push_back(&list->commands()); 425 recorded.push_back(&list->commands());
400 } 426 }
401 last_result_ = queue_.execute(recorded); 427 last_result_ = queue_.execute(recorded);
428 d3d12_trace::record(method, last_result_ == leonida::ReplayResult::success ? "ok" : "failed");
402 if (last_result_ != leonida::ReplayResult::success) 429 if (last_result_ != leonida::ReplayResult::success)
403 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists failed: %s; nothing was submitted\n", 430 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists failed: %s; nothing was submitted\n",
404 name(last_result_)); 431 name(last_result_));
@@ -411,29 +438,33 @@ public:
411 const D3D12_TILE_REGION_SIZE*, ID3D12Heap*, UINT, 438 const D3D12_TILE_REGION_SIZE*, ID3D12Heap*, UINT,
412 const D3D12_TILE_RANGE_FLAGS*, const UINT*, const UINT*, 439 const D3D12_TILE_RANGE_FLAGS*, const UINT*, const UINT*,
413 D3D12_TILE_MAPPING_FLAGS) override { 440 D3D12_TILE_MAPPING_FLAGS) override {
441 d3d12_trace::record("ID3D12CommandQueue::UpdateTileMappings", "ignored");
414 note_once("ID3D12CommandQueue::UpdateTileMappings ignored: reserved resources are fully backed, mappings are not emulated"); 442 note_once("ID3D12CommandQueue::UpdateTileMappings ignored: reserved resources are fully backed, mappings are not emulated");
415 } 443 }
416 444
417 void STDMETHODCALLTYPE CopyTileMappings(ID3D12Resource*, const D3D12_TILED_RESOURCE_COORDINATE*, ID3D12Resource*, 445 void STDMETHODCALLTYPE CopyTileMappings(ID3D12Resource*, const D3D12_TILED_RESOURCE_COORDINATE*, ID3D12Resource*,
418 const D3D12_TILED_RESOURCE_COORDINATE*, const D3D12_TILE_REGION_SIZE*, 446 const D3D12_TILED_RESOURCE_COORDINATE*, const D3D12_TILE_REGION_SIZE*,
419 D3D12_TILE_MAPPING_FLAGS) override { 447 D3D12_TILE_MAPPING_FLAGS) override {
448 d3d12_trace::record("ID3D12CommandQueue::CopyTileMappings", "ignored");
420 note_once("ID3D12CommandQueue::CopyTileMappings ignored: reserved resources are fully backed, mappings are not emulated"); 449 note_once("ID3D12CommandQueue::CopyTileMappings ignored: reserved resources are fully backed, mappings are not emulated");
421 } 450 }
422 451
423 HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override { 452 HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override {
453 constexpr auto method = "ID3D12CommandQueue::Signal";
424 Fence* f = Fence::from(fence); 454 Fence* f = Fence::from(fence);
425 if (!f) return E_INVALIDARG; 455 if (!f) return d3d12_trace::result(method, E_INVALIDARG);
426 std::lock_guard lock(mutex_); 456 std::lock_guard lock(mutex_);
427 if (!f->claim(value)) return S_OK; 457 if (!f->claim(value)) return d3d12_trace::result(method, S_OK);
428 return queue_.signal(f->semaphore(), value) ? S_OK : E_FAIL; 458 return d3d12_trace::result(method, queue_.signal(f->semaphore(), value) ? S_OK : E_FAIL);
429 } 459 }
430 460
431 HRESULT STDMETHODCALLTYPE Wait(ID3D12Fence* fence, UINT64 value) override { 461 HRESULT STDMETHODCALLTYPE Wait(ID3D12Fence* fence, UINT64 value) override {
462 constexpr auto method = "ID3D12CommandQueue::Wait";
432 Fence* f = Fence::from(fence); 463 Fence* f = Fence::from(fence);
433 if (!f) return E_INVALIDARG; 464 if (!f) return d3d12_trace::result(method, E_INVALIDARG);
434 std::lock_guard lock(mutex_); 465 std::lock_guard lock(mutex_);
435 queue_.wait(f->semaphore(), value); 466 queue_.wait(f->semaphore(), value);
436 return S_OK; 467 return d3d12_trace::result(method, S_OK);
437 } 468 }
438 469
439 static CommandQueue* from(IUnknown* object) { return Child::from<CommandQueue>(object); } 470 static CommandQueue* from(IUnknown* object) { return Child::from<CommandQueue>(object); }
diff --git a/runtime/shims/d3d12_trace.h b/runtime/shims/d3d12_trace.h
new file mode 100644
index 000000000..82432eb18
--- /dev/null
+++ b/runtime/shims/d3d12_trace.h
@@ -0,0 +1,36 @@
1#pragma once
2
3#include <windows.h>
4
5#include <atomic>
6#include <cstdio>
7#include <mutex>
8
9namespace rt::d3d12_trace {
10
11inline std::atomic<bool> tracing{false};
12
13inline void set_enabled(bool enable) noexcept {
14 tracing.store(enable, std::memory_order_relaxed);
15}
16
17inline bool enabled() noexcept {
18 return tracing.load(std::memory_order_relaxed);
19}
20
21// Callers supply fixed method names and outcomes only. Keep one ordered record per call across threads.
22inline void record(const char* method, const char* outcome = "called") noexcept {
23 if (!enabled()) return;
24 static std::mutex mutex;
25 static unsigned long long sequence = 0;
26 const std::lock_guard lock(mutex);
27 std::fprintf(stderr, "[d3d12-trace] #%llu %s %s\n", ++sequence, method, outcome);
28 std::fflush(stderr);
29}
30
31inline HRESULT result(const char* method, HRESULT code) noexcept {
32 record(method, code == E_NOTIMPL ? "unsupported" : FAILED(code) ? "failed" : "ok");
33 return code;
34}
35
36} // namespace rt::d3d12_trace
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index 6267263bd..d226f5a86 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -8,6 +8,7 @@
8 8
9#include "d3d12_stubs.h" // generated 9#include "d3d12_stubs.h" // generated
10#include "d3d12_com_object.h" 10#include "d3d12_com_object.h"
11#include "d3d12_trace.h"
11#include "d3d12_xs_device.h" 12#include "d3d12_xs_device.h"
12#include "../src/gfx10_descriptors.h" 13#include "../src/gfx10_descriptors.h"
13#include "../src/vulkan_bindless.h" 14#include "../src/vulkan_bindless.h"
@@ -1110,53 +1111,59 @@ public:
1110 1111
1111 HRESULT STDMETHODCALLTYPE CreateCommandQueue(const D3D12_COMMAND_QUEUE_DESC* desc, REFIID riid, 1112 HRESULT STDMETHODCALLTYPE CreateCommandQueue(const D3D12_COMMAND_QUEUE_DESC* desc, REFIID riid,
1112 void** out) override { 1113 void** out) override {
1113 if (!desc) return E_INVALIDARG; 1114 constexpr auto method = "ID3D12Device::CreateCommandQueue";
1115 if (!desc) return d3d12_trace::result(method, E_INVALIDARG);
1114 // Copy and compute queues share the one graphics VkQueue for now (section 8.2, question 12). 1116 // Copy and compute queues share the one graphics VkQueue for now (section 8.2, question 12).
1115 if (desc->Type != D3D12_COMMAND_LIST_TYPE_DIRECT) log_once("command queue type on the direct VkQueue:", desc->Type); 1117 if (desc->Type != D3D12_COMMAND_LIST_TYPE_DIRECT) log_once("command queue type on the direct VkQueue:", desc->Type);
1116 return create_command_queue(*state_.context, state_.info.queue, state_.info.queue_family, *desc, riid, out, 1118 return d3d12_trace::result(method, create_command_queue(*state_.context, state_.info.queue, state_.info.queue_family, *desc, riid, out,
1117 identity()); 1119 identity()));
1118 } 1120 }
1119 1121
1120 HRESULT STDMETHODCALLTYPE CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void** out) override { 1122 HRESULT STDMETHODCALLTYPE CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void** out) override {
1121 return create_command_allocator(type, riid, out, identity()); 1123 constexpr auto method = "ID3D12Device::CreateCommandAllocator";
1124 return d3d12_trace::result(method, create_command_allocator(type, riid, out, identity()));
1122 } 1125 }
1123 1126
1124 HRESULT STDMETHODCALLTYPE CreateCommandList(UINT, D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, 1127 HRESULT STDMETHODCALLTYPE CreateCommandList(UINT, D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator,
1125 ID3D12PipelineState* state, REFIID riid, void** out) override { 1128 ID3D12PipelineState* state, REFIID riid, void** out) override {
1129 constexpr auto method = "ID3D12Device::CreateCommandList";
1126 if (state) stub("ID3D12Device::CreateCommandList(initial pipeline state)"); 1130 if (state) stub("ID3D12Device::CreateCommandList(initial pipeline state)");
1127 return create_command_list(type, allocator, riid, out, identity()); 1131 return d3d12_trace::result(method, create_command_list(type, allocator, riid, out, identity()));
1128 } 1132 }
1129 1133
1130 // Device4: a list created closed, without an allocator. 1134 // Device4: a list created closed, without an allocator.
1131 HRESULT STDMETHODCALLTYPE CreateCommandList1(UINT, D3D12_COMMAND_LIST_TYPE type, D3D12_COMMAND_LIST_FLAGS, 1135 HRESULT STDMETHODCALLTYPE CreateCommandList1(UINT, D3D12_COMMAND_LIST_TYPE type, D3D12_COMMAND_LIST_FLAGS,
1132 REFIID riid, void** out) override { 1136 REFIID riid, void** out) override {
1133 if (!out) return E_POINTER; 1137 constexpr auto method = "ID3D12Device4::CreateCommandList1";
1138 if (!out) return d3d12_trace::result(method, E_POINTER);
1134 *out = nullptr; 1139 *out = nullptr;
1135 ID3D12CommandAllocator* allocator = nullptr; 1140 ID3D12CommandAllocator* allocator = nullptr;
1136 HRESULT hr = create_command_allocator(type, IID_PPV_ARGS(&allocator), identity()); 1141 HRESULT hr = create_command_allocator(type, IID_PPV_ARGS(&allocator), identity());
1137 if (FAILED(hr)) return hr; 1142 if (FAILED(hr)) return d3d12_trace::result(method, hr);
1138 ID3D12GraphicsCommandList* list = nullptr; 1143 ID3D12GraphicsCommandList* list = nullptr;
1139 hr = create_command_list(type, allocator, IID_PPV_ARGS(&list), identity()); 1144 hr = create_command_list(type, allocator, IID_PPV_ARGS(&list), identity());
1140 allocator->Release(); 1145 allocator->Release();
1141 if (FAILED(hr)) return hr; 1146 if (FAILED(hr)) return d3d12_trace::result(method, hr);
1142 list->Close(); 1147 list->Close();
1143 hr = list->QueryInterface(riid, out); 1148 hr = list->QueryInterface(riid, out);
1144 list->Release(); 1149 list->Release();
1145 return hr; 1150 return d3d12_trace::result(method, hr);
1146 } 1151 }
1147 1152
1148 HRESULT STDMETHODCALLTYPE CreateFence(UINT64 initial, D3D12_FENCE_FLAGS flags, REFIID riid, void** out) override { 1153 HRESULT STDMETHODCALLTYPE CreateFence(UINT64 initial, D3D12_FENCE_FLAGS flags, REFIID riid, void** out) override {
1149 return create_fence(*state_.context, initial, flags, riid, out, identity()); 1154 constexpr auto method = "ID3D12Device::CreateFence";
1155 return d3d12_trace::result(method, create_fence(*state_.context, initial, flags, riid, out, identity()));
1150 } 1156 }
1151 1157
1152 HRESULT STDMETHODCALLTYPE CreateDescriptorHeap(const D3D12_DESCRIPTOR_HEAP_DESC* desc, REFIID riid, 1158 HRESULT STDMETHODCALLTYPE CreateDescriptorHeap(const D3D12_DESCRIPTOR_HEAP_DESC* desc, REFIID riid,
1153 void** out) override { 1159 void** out) override {
1154 if (!out) return E_POINTER; 1160 constexpr auto method = "ID3D12Device::CreateDescriptorHeap";
1161 if (!out) return d3d12_trace::result(method, E_POINTER);
1155 *out = nullptr; 1162 *out = nullptr;
1156 if (!desc || !desc->NumDescriptors) return E_INVALIDARG; 1163 if (!desc || !desc->NumDescriptors) return d3d12_trace::result(method, E_INVALIDARG);
1157 auto* heap = new DescriptorHeap(*desc); 1164 auto* heap = new DescriptorHeap(*desc);
1158 heap->attach_device(identity()); 1165 heap->attach_device(identity());
1159 return hand_out(heap, riid, out); 1166 return d3d12_trace::result(method, hand_out(heap, riid, out));
1160 } 1167 }
1161 1168
1162 UINT STDMETHODCALLTYPE GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE) override { 1169 UINT STDMETHODCALLTYPE GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE) override {
@@ -1165,6 +1172,7 @@ public:
1165 1172
1166 void STDMETHODCALLTYPE CreateRenderTargetView(ID3D12Resource* resource, const D3D12_RENDER_TARGET_VIEW_DESC* desc, 1173 void STDMETHODCALLTYPE CreateRenderTargetView(ID3D12Resource* resource, const D3D12_RENDER_TARGET_VIEW_DESC* desc,
1167 D3D12_CPU_DESCRIPTOR_HANDLE destination) override { 1174 D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
1175 d3d12_trace::record("ID3D12Device::CreateRenderTargetView");
1168 if (!destination.ptr) return; 1176 if (!destination.ptr) return;
1169 leonida::TargetView view{}; 1177 leonida::TargetView view{};
1170 if (const Resource* r = Resource::from<Resource>(resource)) { 1178 if (const Resource* r = Resource::from<Resource>(resource)) {
@@ -1220,6 +1228,7 @@ public:
1220 1228
1221 void STDMETHODCALLTYPE CreateDepthStencilView(ID3D12Resource* resource, const D3D12_DEPTH_STENCIL_VIEW_DESC* desc, 1229 void STDMETHODCALLTYPE CreateDepthStencilView(ID3D12Resource* resource, const D3D12_DEPTH_STENCIL_VIEW_DESC* desc,
1222 D3D12_CPU_DESCRIPTOR_HANDLE destination) override { 1230 D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
1231 d3d12_trace::record("ID3D12Device::CreateDepthStencilView");
1223 if (!destination.ptr) return; 1232 if (!destination.ptr) return;
1224 leonida::TargetView view{}; 1233 leonida::TargetView view{};
1225 if (const Resource* r = Resource::from<Resource>(resource)) { 1234 if (const Resource* r = Resource::from<Resource>(resource)) {
@@ -1280,6 +1289,7 @@ public:
1280 void STDMETHODCALLTYPE CreateShaderResourceView(ID3D12Resource* resource, 1289 void STDMETHODCALLTYPE CreateShaderResourceView(ID3D12Resource* resource,
1281 const D3D12_SHADER_RESOURCE_VIEW_DESC* desc, 1290 const D3D12_SHADER_RESOURCE_VIEW_DESC* desc,
1282 D3D12_CPU_DESCRIPTOR_HANDLE destination) override { 1291 D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
1292 d3d12_trace::record("ID3D12Device::CreateShaderResourceView");
1283 if (!destination.ptr) return; 1293 if (!destination.ptr) return;
1284 std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize); 1294 std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize);
1285 const Resource* r = Resource::from<Resource>(resource); 1295 const Resource* r = Resource::from<Resource>(resource);
@@ -1338,6 +1348,7 @@ public:
1338 void STDMETHODCALLTYPE CreateUnorderedAccessView(ID3D12Resource* resource, ID3D12Resource* counter, 1348 void STDMETHODCALLTYPE CreateUnorderedAccessView(ID3D12Resource* resource, ID3D12Resource* counter,
1339 const D3D12_UNORDERED_ACCESS_VIEW_DESC* desc, 1349 const D3D12_UNORDERED_ACCESS_VIEW_DESC* desc,
1340 D3D12_CPU_DESCRIPTOR_HANDLE destination) override { 1350 D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
1351 d3d12_trace::record("ID3D12Device::CreateUnorderedAccessView");
1341 if (!destination.ptr) return; 1352 if (!destination.ptr) return;
1342 std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize); 1353 std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize);
1343 const Resource* r = Resource::from<Resource>(resource); 1354 const Resource* r = Resource::from<Resource>(resource);
@@ -1381,6 +1392,7 @@ public:
1381 // A sampler is an S# in the first 16 bytes of the slot; the shader finds the sampler by those four dwords. 1392 // A sampler is an S# in the first 16 bytes of the slot; the shader finds the sampler by those four dwords.
1382 void STDMETHODCALLTYPE CreateSampler(const D3D12_SAMPLER_DESC* desc, 1393 void STDMETHODCALLTYPE CreateSampler(const D3D12_SAMPLER_DESC* desc,
1383 D3D12_CPU_DESCRIPTOR_HANDLE destination) override { 1394 D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
1395 d3d12_trace::record("ID3D12Device::CreateSampler");
1384 if (!destination.ptr) return; 1396 if (!destination.ptr) return;
1385 std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize); 1397 std::memset(reinterpret_cast<void*>(destination.ptr), 0, kDescriptorSize);
1386 if (!desc) return; 1398 if (!desc) return;
@@ -1394,6 +1406,7 @@ public:
1394 // on WARP: docs/d3d12_vulkan.md section 3.2. 1406 // on WARP: docs/d3d12_vulkan.md section 3.2.
1395 void STDMETHODCALLTYPE CreateSampler2(const D3D12_SAMPLER_DESC2* desc, 1407 void STDMETHODCALLTYPE CreateSampler2(const D3D12_SAMPLER_DESC2* desc,
1396 D3D12_CPU_DESCRIPTOR_HANDLE destination) override { 1408 D3D12_CPU_DESCRIPTOR_HANDLE destination) override {
1409 d3d12_trace::record("ID3D12Device11::CreateSampler2");
1397 if (!destination.ptr || !desc) return; 1410 if (!destination.ptr || !desc) return;
1398 const char* refusal = sampler2_refusal(*desc); 1411 const char* refusal = sampler2_refusal(*desc);
1399 if (refusal) { 1412 if (refusal) {
@@ -1449,8 +1462,9 @@ public:
1449 HRESULT STDMETHODCALLTYPE CreateCommittedResource(const D3D12_HEAP_PROPERTIES* heap, D3D12_HEAP_FLAGS heap_flags, 1462 HRESULT STDMETHODCALLTYPE CreateCommittedResource(const D3D12_HEAP_PROPERTIES* heap, D3D12_HEAP_FLAGS heap_flags,
1450 const D3D12_RESOURCE_DESC* desc, D3D12_RESOURCE_STATES, 1463 const D3D12_RESOURCE_DESC* desc, D3D12_RESOURCE_STATES,
1451 const D3D12_CLEAR_VALUE*, REFIID riid, void** out) override { 1464 const D3D12_CLEAR_VALUE*, REFIID riid, void** out) override {
1465 constexpr auto method = "ID3D12Device::CreateCommittedResource";
1452 if (out) *out = nullptr; 1466 if (out) *out = nullptr;
1453 if (!heap || !desc) return E_INVALIDARG; 1467 if (!heap || !desc) return d3d12_trace::result(method, E_INVALIDARG);
1454 auto* resource = new Resource(state_, *desc, *heap, heap_flags); 1468 auto* resource = new Resource(state_, *desc, *heap, heap_flags);
1455 resource->attach_device(identity()); 1469 resource->attach_device(identity());
1456 HRESULT hr = E_NOTIMPL; 1470 HRESULT hr = E_NOTIMPL;
@@ -1482,13 +1496,13 @@ public:
1482 } 1496 }
1483 if (FAILED(hr)) { 1497 if (FAILED(hr)) {
1484 resource->Release(); 1498 resource->Release();
1485 return hr; 1499 return d3d12_trace::result(method, hr);
1486 } 1500 }
1487 if (!out) { // a capability check: D3D12 allows a null output 1501 if (!out) { // a capability check: D3D12 allows a null output
1488 resource->Release(); 1502 resource->Release();
1489 return S_FALSE; 1503 return d3d12_trace::result(method, S_FALSE);
1490 } 1504 }
1491 return hand_out(resource, riid, out); 1505 return d3d12_trace::result(method, hand_out(resource, riid, out));
1492 } 1506 }
1493 1507
1494 // Reserved (tiled) resources, for the first boot (docs/d3d12_vulkan.md section 4.2): the whole resource is 1508 // Reserved (tiled) resources, for the first boot (docs/d3d12_vulkan.md section 4.2): the whole resource is
@@ -1501,7 +1515,8 @@ public:
1501 // uses it for reserved resources too is an assumption. 1515 // uses it for reserved resources too is an assumption.
1502 HRESULT STDMETHODCALLTYPE CreateReservedResource(const D3D12_RESOURCE_DESC* desc, D3D12_RESOURCE_STATES, 1516 HRESULT STDMETHODCALLTYPE CreateReservedResource(const D3D12_RESOURCE_DESC* desc, D3D12_RESOURCE_STATES,
1503 const D3D12_CLEAR_VALUE*, REFIID riid, void** out) override { 1517 const D3D12_CLEAR_VALUE*, REFIID riid, void** out) override {
1504 return create_reserved(desc, riid, out); 1518 constexpr auto method = "ID3D12Device::CreateReservedResource";
1519 return d3d12_trace::result(method, create_reserved(desc, riid, out));
1505 } 1520 }
1506 1521
1507 // Device4. Protected sessions are not supported. 1522 // Device4. Protected sessions are not supported.
@@ -1713,41 +1728,42 @@ public:
1713 } 1728 }
1714 1729
1715 HRESULT STDMETHODCALLTYPE CheckFeatureSupport(D3D12_FEATURE feature, void* data, UINT size) override { 1730 HRESULT STDMETHODCALLTYPE CheckFeatureSupport(D3D12_FEATURE feature, void* data, UINT size) override {
1731 constexpr auto method = "ID3D12Device::CheckFeatureSupport";
1716 log_once("CheckFeatureSupport feature", feature); 1732 log_once("CheckFeatureSupport feature", feature);
1717 if (!data) return E_INVALIDARG; 1733 if (!data) return d3d12_trace::result(method, E_INVALIDARG);
1718 constexpr VkSampleCountFlags d3d12_multisample_sample_counts = 1734 constexpr VkSampleCountFlags d3d12_multisample_sample_counts =
1719 VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT | 1735 VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT |
1720 VK_SAMPLE_COUNT_16_BIT | VK_SAMPLE_COUNT_32_BIT; 1736 VK_SAMPLE_COUNT_16_BIT | VK_SAMPLE_COUNT_32_BIT;
1721 switch (feature) { 1737 switch (feature) {
1722 case D3D12_FEATURE_FEATURE_LEVELS: { 1738 case D3D12_FEATURE_FEATURE_LEVELS: {
1723 if (size != sizeof(D3D12_FEATURE_DATA_FEATURE_LEVELS)) return E_INVALIDARG; 1739 if (size != sizeof(D3D12_FEATURE_DATA_FEATURE_LEVELS)) return d3d12_trace::result(method, E_INVALIDARG);
1724 auto* levels = static_cast<D3D12_FEATURE_DATA_FEATURE_LEVELS*>(data); 1740 auto* levels = static_cast<D3D12_FEATURE_DATA_FEATURE_LEVELS*>(data);
1725 D3D_FEATURE_LEVEL best = D3D_FEATURE_LEVEL(0); 1741 D3D_FEATURE_LEVEL best = D3D_FEATURE_LEVEL(0);
1726 for (UINT i = 0; i < levels->NumFeatureLevels; ++i) 1742 for (UINT i = 0; i < levels->NumFeatureLevels; ++i)
1727 if (levels->pFeatureLevelsRequested[i] <= D3D_FEATURE_LEVEL_12_1) 1743 if (levels->pFeatureLevelsRequested[i] <= D3D_FEATURE_LEVEL_12_1)
1728 best = std::max(best, levels->pFeatureLevelsRequested[i]); 1744 best = std::max(best, levels->pFeatureLevelsRequested[i]);
1729 if (!best) return DXGI_ERROR_UNSUPPORTED; 1745 if (!best) return d3d12_trace::result(method, DXGI_ERROR_UNSUPPORTED);
1730 levels->MaxSupportedFeatureLevel = best; 1746 levels->MaxSupportedFeatureLevel = best;
1731 return S_OK; 1747 return d3d12_trace::result(method, S_OK);
1732 } 1748 }
1733 case D3D12_FEATURE_ARCHITECTURE: 1749 case D3D12_FEATURE_ARCHITECTURE:
1734 case D3D12_FEATURE_ARCHITECTURE1: { 1750 case D3D12_FEATURE_ARCHITECTURE1: {
1735 const bool v1 = feature == D3D12_FEATURE_ARCHITECTURE1; 1751 const bool v1 = feature == D3D12_FEATURE_ARCHITECTURE1;
1736 if (size != (v1 ? sizeof(D3D12_FEATURE_DATA_ARCHITECTURE1) : sizeof(D3D12_FEATURE_DATA_ARCHITECTURE))) 1752 if (size != (v1 ? sizeof(D3D12_FEATURE_DATA_ARCHITECTURE1) : sizeof(D3D12_FEATURE_DATA_ARCHITECTURE)))
1737 return E_INVALIDARG; 1753 return d3d12_trace::result(method, E_INVALIDARG);
1738 auto* arch = static_cast<D3D12_FEATURE_DATA_ARCHITECTURE1*>(data); 1754 auto* arch = static_cast<D3D12_FEATURE_DATA_ARCHITECTURE1*>(data);
1739 const UINT node = arch->NodeIndex; 1755 const UINT node = arch->NodeIndex;
1740 if (node != 0) return E_INVALIDARG; 1756 if (node != 0) return d3d12_trace::result(method, E_INVALIDARG);
1741 // A discrete desktop GPU: not tile-based, no UMA. The console is UMA (question 11); the layer keeps 1757 // A discrete desktop GPU: not tile-based, no UMA. The console is UMA (question 11); the layer keeps
1742 // upload heaps host-visible, so the game's upload path still works. 1758 // upload heaps host-visible, so the game's upload path still works.
1743 arch->TileBasedRenderer = FALSE; 1759 arch->TileBasedRenderer = FALSE;
1744 arch->UMA = FALSE; 1760 arch->UMA = FALSE;
1745 arch->CacheCoherentUMA = FALSE; 1761 arch->CacheCoherentUMA = FALSE;
1746 if (v1) arch->IsolatedMMU = TRUE; 1762 if (v1) arch->IsolatedMMU = TRUE;
1747 return S_OK; 1763 return d3d12_trace::result(method, S_OK);
1748 } 1764 }
1749 case D3D12_FEATURE_D3D12_OPTIONS: { 1765 case D3D12_FEATURE_D3D12_OPTIONS: {
1750 if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS)) return E_INVALIDARG; 1766 if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS)) return d3d12_trace::result(method, E_INVALIDARG);
1751 auto* opts = static_cast<D3D12_FEATURE_DATA_D3D12_OPTIONS*>(data); 1767 auto* opts = static_cast<D3D12_FEATURE_DATA_D3D12_OPTIONS*>(data);
1752 *opts = {}; 1768 *opts = {};
1753 opts->DoublePrecisionFloatShaderOps = TRUE; 1769 opts->DoublePrecisionFloatShaderOps = TRUE;
@@ -1765,27 +1781,27 @@ public:
1765 opts->CrossAdapterRowMajorTextureSupported = FALSE; 1781 opts->CrossAdapterRowMajorTextureSupported = FALSE;
1766 opts->VPAndRTArrayIndexFromAnyShaderFeedingRasterizerSupportedWithoutGSEmulation = TRUE; 1782 opts->VPAndRTArrayIndexFromAnyShaderFeedingRasterizerSupportedWithoutGSEmulation = TRUE;
1767 opts->ResourceHeapTier = D3D12_RESOURCE_HEAP_TIER_1; 1783 opts->ResourceHeapTier = D3D12_RESOURCE_HEAP_TIER_1;
1768 return S_OK; 1784 return d3d12_trace::result(method, S_OK);
1769 } 1785 }
1770 case D3D12_FEATURE_D3D12_OPTIONS17: { 1786 case D3D12_FEATURE_D3D12_OPTIONS17: {
1771 // Non-normalized samplers are core Vulkan (unnormalizedCoordinates), with the restrictions 1787 // Non-normalized samplers are core Vulkan (unnormalizedCoordinates), with the restrictions
1772 // CreateSampler2 checks. No manual write tracking, as the RTX 4090 answers too. 1788 // CreateSampler2 checks. No manual write tracking, as the RTX 4090 answers too.
1773 if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS17)) return E_INVALIDARG; 1789 if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS17)) return d3d12_trace::result(method, E_INVALIDARG);
1774 auto* opts = static_cast<D3D12_FEATURE_DATA_D3D12_OPTIONS17*>(data); 1790 auto* opts = static_cast<D3D12_FEATURE_DATA_D3D12_OPTIONS17*>(data);
1775 *opts = {}; 1791 *opts = {};
1776 opts->NonNormalizedCoordinateSamplersSupported = TRUE; 1792 opts->NonNormalizedCoordinateSamplersSupported = TRUE;
1777 return S_OK; 1793 return d3d12_trace::result(method, S_OK);
1778 } 1794 }
1779 case D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT: { 1795 case D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT: {
1780 if (size != sizeof(D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT)) return E_INVALIDARG; 1796 if (size != sizeof(D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT)) return d3d12_trace::result(method, E_INVALIDARG);
1781 auto* va = static_cast<D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT*>(data); 1797 auto* va = static_cast<D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT*>(data);
1782 // The console and PC BDA support 48-bit GPU virtual addresses (section 4.1). 1798 // The console and PC BDA support 48-bit GPU virtual addresses (section 4.1).
1783 va->MaxGPUVirtualAddressBitsPerResource = 40; 1799 va->MaxGPUVirtualAddressBitsPerResource = 40;
1784 va->MaxGPUVirtualAddressBitsPerProcess = 48; 1800 va->MaxGPUVirtualAddressBitsPerProcess = 48;
1785 return S_OK; 1801 return d3d12_trace::result(method, S_OK);
1786 } 1802 }
1787 case D3D12_FEATURE_FORMAT_SUPPORT: { 1803 case D3D12_FEATURE_FORMAT_SUPPORT: {
1788 if (size != sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)) return E_INVALIDARG; 1804 if (size != sizeof(D3D12_FEATURE_DATA_FORMAT_SUPPORT)) return d3d12_trace::result(method, E_INVALIDARG);
1789 auto* fs = static_cast<D3D12_FEATURE_DATA_FORMAT_SUPPORT*>(data); 1805 auto* fs = static_cast<D3D12_FEATURE_DATA_FORMAT_SUPPORT*>(data);
1790 fs->Support1 = D3D12_FORMAT_SUPPORT1_NONE; 1806 fs->Support1 = D3D12_FORMAT_SUPPORT1_NONE;
1791 fs->Support2 = D3D12_FORMAT_SUPPORT2_NONE; 1807 fs->Support2 = D3D12_FORMAT_SUPPORT2_NONE;
@@ -1793,7 +1809,7 @@ public:
1793 const Format depth_fmt = vulkan_format(fs->Format, true); 1809 const Format depth_fmt = vulkan_format(fs->Format, true);
1794 const VkFormat vk_fmt = fmt.format != VK_FORMAT_UNDEFINED ? fmt.format : depth_fmt.format; 1810 const VkFormat vk_fmt = fmt.format != VK_FORMAT_UNDEFINED ? fmt.format : depth_fmt.format;
1795 if (vk_fmt == VK_FORMAT_UNDEFINED || !state_.format_properties) { 1811 if (vk_fmt == VK_FORMAT_UNDEFINED || !state_.format_properties) {
1796 return DXGI_ERROR_UNSUPPORTED; 1812 return d3d12_trace::result(method, DXGI_ERROR_UNSUPPORTED);
1797 } 1813 }
1798 VkFormatProperties props{}; 1814 VkFormatProperties props{};
1799 state_.format_properties(state_.info.physical, vk_fmt, &props); 1815 state_.format_properties(state_.info.physical, vk_fmt, &props);
@@ -1852,16 +1868,16 @@ public:
1852 } 1868 }
1853 fs->Support1 = s1; 1869 fs->Support1 = s1;
1854 fs->Support2 = s2; 1870 fs->Support2 = s2;
1855 return S_OK; 1871 return d3d12_trace::result(method, S_OK);
1856 } 1872 }
1857 case D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS: { 1873 case D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS: {
1858 if (size != sizeof(D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS)) return E_INVALIDARG; 1874 if (size != sizeof(D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS)) return d3d12_trace::result(method, E_INVALIDARG);
1859 auto* levels = static_cast<D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS*>(data); 1875 auto* levels = static_cast<D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS*>(data);
1860 levels->NumQualityLevels = 0; 1876 levels->NumQualityLevels = 0;
1861 if (levels->Flags != D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE || 1877 if (levels->Flags != D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE ||
1862 levels->SampleCount == 0 || levels->SampleCount > D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT || 1878 levels->SampleCount == 0 || levels->SampleCount > D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT ||
1863 (levels->SampleCount & (levels->SampleCount - 1)) != 0) 1879 (levels->SampleCount & (levels->SampleCount - 1)) != 0)
1864 return E_INVALIDARG; 1880 return d3d12_trace::result(method, E_INVALIDARG);
1865 Format format = vulkan_format(levels->Format, false); 1881 Format format = vulkan_format(levels->Format, false);
1866 if (format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT) { 1882 if (format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1867 D3D12_RESOURCE_DESC depth_desc{}; 1883 D3D12_RESOURCE_DESC depth_desc{};
@@ -1870,7 +1886,7 @@ public:
1870 format = state_.texture_format(depth_desc); // includes the D24-to-D32 fallback used by creation 1886 format = state_.texture_format(depth_desc); // includes the D24-to-D32 fallback used by creation
1871 } 1887 }
1872 if (format.format == VK_FORMAT_UNDEFINED || !state_.image_format_properties) 1888 if (format.format == VK_FORMAT_UNDEFINED || !state_.image_format_properties)
1873 return DXGI_ERROR_UNSUPPORTED; 1889 return d3d12_trace::result(method, DXGI_ERROR_UNSUPPORTED);
1874 1890
1875 // Vulkan exposes one standard sample pattern per supported count, so each supported count maps to 1891 // Vulkan exposes one standard sample pattern per supported count, so each supported count maps to
1876 // one D3D12 quality level (quality index 0). Query the matching colour or depth/stencil attachment 1892 // one D3D12 quality level (quality index 0). Query the matching colour or depth/stencil attachment
@@ -1882,7 +1898,7 @@ public:
1882 else if (format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT) 1898 else if (format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT)
1883 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; 1899 usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
1884 else 1900 else
1885 return DXGI_ERROR_UNSUPPORTED; 1901 return d3d12_trace::result(method, DXGI_ERROR_UNSUPPORTED);
1886 const VkImageCreateFlags flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0; 1902 const VkImageCreateFlags flags = format.typeless ? VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT : 0;
1887 VkImageFormatProperties image_properties{}; 1903 VkImageFormatProperties image_properties{};
1888 const VkResult result = state_.image_format_properties(state_.info.physical, format.format, 1904 const VkResult result = state_.image_format_properties(state_.info.physical, format.format,
@@ -1891,11 +1907,11 @@ public:
1891 const VkSampleCountFlagBits samples = VkSampleCountFlagBits(levels->SampleCount); 1907 const VkSampleCountFlagBits samples = VkSampleCountFlagBits(levels->SampleCount);
1892 if (result == VK_SUCCESS && (image_properties.sampleCounts & samples)) 1908 if (result == VK_SUCCESS && (image_properties.sampleCounts & samples))
1893 levels->NumQualityLevels = 1; 1909 levels->NumQualityLevels = 1;
1894 return S_OK; 1910 return d3d12_trace::result(method, S_OK);
1895 } 1911 }
1896 default: 1912 default:
1897 // Not answered yet. The log above lists every query; answers are added as the trace asks for them. 1913 // Not answered yet. The log above lists every query; answers are added as the trace asks for them.
1898 return E_INVALIDARG; 1914 return d3d12_trace::result(method, E_INVALIDARG);
1899 } 1915 }
1900 } 1916 }
1901 1917
diff --git a/runtime/shims/d3d12_xs.cpp b/runtime/shims/d3d12_xs.cpp
index 3e7626d92..85997fce7 100644
--- a/runtime/shims/d3d12_xs.cpp
+++ b/runtime/shims/d3d12_xs.cpp
@@ -18,6 +18,7 @@
18#ifdef LEONIDA_VULKAN_DEVICE 18#ifdef LEONIDA_VULKAN_DEVICE
19#include "d3d12_com.h" 19#include "d3d12_com.h"
20#endif 20#endif
21#include "d3d12_trace.h"
21#include "d3d12_xs_device.h" 22#include "d3d12_xs_device.h"
22#include "d3d12_xs_rootsig.h" 23#include "d3d12_xs_rootsig.h"
23#include "shims.h" 24#include "shims.h"
@@ -121,26 +122,31 @@ HRESULT WINAPI create_root_signature_deserializer(const void* data, SIZE_T size,
121// (96 bytes) and no more. The PC D3D12CreateDevice needs a minimum feature level instead, so 12_0 is used as the 122// (96 bytes) and no more. The PC D3D12CreateDevice needs a minimum feature level instead, so 12_0 is used as the
122// least restrictive floor; higher feature levels are still exposed by the created device. 123// least restrictive floor; higher feature levels are still exposed by the created device.
123HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, const IID* riid, void** out) { 124HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, const IID* riid, void** out) {
124 if (!out) return E_POINTER; 125 constexpr auto method = "D3D12XboxCreateDevice";
126 using rt::d3d12_trace::result;
127 if (!out) return result(method, E_POINTER);
125 *out = nullptr; 128 *out = nullptr;
126 if (!riid) return E_POINTER; 129 if (!riid) return result(method, E_POINTER);
127 130
128 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice adapter=%p params=%p riid=%08lx-%04x-%04x\n", 131 const bool tracing = rt::d3d12_trace::enabled();
129 adapter, params, static_cast<unsigned long>(riid->Data1), riid->Data2, riid->Data3); 132 if (!tracing) {
130 if (params) { 133 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice adapter=%p params=%p riid=%08lx-%04x-%04x\n",
131 const uint32_t* words = static_cast<const uint32_t*>(params); 134 adapter, params, static_cast<unsigned long>(riid->Data1), riid->Data2, riid->Data3);
132 char line[24 * 11 + 1]; 135 if (params) {
133 line[0] = '\0'; 136 const uint32_t* words = static_cast<const uint32_t*>(params);
134 size_t used = 0; 137 char line[24 * 11 + 1];
135 for (int i = 0; i < 24 && used < sizeof(line); ++i) { 138 line[0] = '\0';
136 int n = std::snprintf(line + used, sizeof(line) - used, "%s%08x", i ? " " : "", 139 size_t used = 0;
137 static_cast<unsigned>(words[i])); 140 for (int i = 0; i < 24 && used < sizeof(line); ++i) {
138 if (n < 0) break; 141 int n = std::snprintf(line + used, sizeof(line) - used, "%s%08x", i ? " " : "",
139 used += static_cast<size_t>(n); 142 static_cast<unsigned>(words[i]));
143 if (n < 0) break;
144 used += static_cast<size_t>(n);
145 }
146 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice params[24] =%s\n", line);
147 } else {
148 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice params = null\n");
140 } 149 }
141 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice params[24] =%s\n", line);
142 } else {
143 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice params = null\n");
144 } 150 }
145 151
146 static std::atomic<bool> proxy_logged{false}; 152 static std::atomic<bool> proxy_logged{false};
@@ -162,24 +168,25 @@ HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, cons
162 if (FAILED(vhr)) { 168 if (FAILED(vhr)) {
163 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: Vulkan device failed (hr=0x%08lx)\n", 169 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: Vulkan device failed (hr=0x%08lx)\n",
164 static_cast<unsigned long>(vhr)); 170 static_cast<unsigned long>(vhr));
165 return vhr; 171 return result(method, vhr);
166 } 172 }
167 vhr = rt::d3d12_xs::create_device_proxy(vulkan, *riid, out); 173 vhr = rt::d3d12_xs::create_device_proxy(vulkan, *riid, out);
168 if (FAILED(vhr)) { 174 if (FAILED(vhr)) {
169 vulkan->Release(); 175 vulkan->Release();
170 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: proxy over the Vulkan device failed " 176 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: proxy over the Vulkan device failed "
171 "(hr=0x%08lx)\n", static_cast<unsigned long>(vhr)); 177 "(hr=0x%08lx)\n", static_cast<unsigned long>(vhr));
172 return vhr; 178 return result(method, vhr);
173 } 179 }
174 rt::d3d12::set_device_outer(vulkan, static_cast<IUnknown*>(*out)); 180 rt::d3d12::set_device_outer(vulkan, static_cast<IUnknown*>(*out));
175 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice -> proxy %p (Vulkan device %p)\n", *out, 181 if (!tracing)
176 static_cast<void*>(vulkan)); 182 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice -> proxy %p (Vulkan device %p)\n", *out,
177 return S_OK; 183 static_cast<void*>(vulkan));
184 return result(method, S_OK);
178 } 185 }
179#endif 186#endif
180 187
181 static auto create = reinterpret_cast<CreateDeviceFn>(host("D3D12CreateDevice")); 188 static auto create = reinterpret_cast<CreateDeviceFn>(host("D3D12CreateDevice"));
182 if (!create) return E_NOTIMPL; 189 if (!create) return result(method, E_NOTIMPL);
183 190
184 ID3D12Device* real = nullptr; 191 ID3D12Device* real = nullptr;
185 HRESULT hr = create(adapter, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), 192 HRESULT hr = create(adapter, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device),
@@ -188,7 +195,7 @@ HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, cons
188 if (real) real->Release(); 195 if (real) real->Release();
189 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: PC D3D12CreateDevice failed (hr=0x%08lx)\n", 196 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: PC D3D12CreateDevice failed (hr=0x%08lx)\n",
190 static_cast<unsigned long>(hr)); 197 static_cast<unsigned long>(hr));
191 return FAILED(hr) ? hr : E_FAIL; 198 return result(method, FAILED(hr) ? hr : E_FAIL);
192 } 199 }
193 200
194 hr = rt::d3d12_xs::create_device_proxy(real, *riid, out); 201 hr = rt::d3d12_xs::create_device_proxy(real, *riid, out);
@@ -197,11 +204,12 @@ HRESULT WINAPI D3D12XboxCreateDevice(IUnknown* adapter, const void* params, cons
197 real->Release(); 204 real->Release();
198 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: proxy creation failed (hr=0x%08lx)\n", 205 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice: proxy creation failed (hr=0x%08lx)\n",
199 static_cast<unsigned long>(hr)); 206 static_cast<unsigned long>(hr));
200 return hr; 207 return result(method, hr);
201 } 208 }
202 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice -> proxy %p (PC device %p)\n", *out, 209 if (!tracing)
203 static_cast<void*>(real)); 210 std::fprintf(stderr, "[rt] d3d12_xs: D3D12XboxCreateDevice -> proxy %p (PC device %p)\n", *out,
204 return S_OK; 211 static_cast<void*>(real));
212 return result(method, S_OK);
205} 213}
206 214
207static_assert(std::is_same_v<decltype(&serialize_root_signature), SerializeFn>); 215static_assert(std::is_same_v<decltype(&serialize_root_signature), SerializeFn>);
diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp
index 18c8495ac..376005067 100644
--- a/runtime/shims/d3d12_xs_device.cpp
+++ b/runtime/shims/d3d12_xs_device.cpp
@@ -8,6 +8,7 @@
8// are IUnknown (0-2), slot 14 (which wraps the heap), 62 and 76, plus the heap proxy's 9/10 conversion from the 8// are IUnknown (0-2), slot 14 (which wraps the heap), 62 and 76, plus the heap proxy's 9/10 conversion from the
9// PC hidden-pointer form to the Xbox RAX form. 9// PC hidden-pointer form to the Xbox RAX form.
10#include "d3d12_xs_device.h" 10#include "d3d12_xs_device.h"
11#include "d3d12_trace.h"
11 12
12#include <windows.h> 13#include <windows.h>
13 14
@@ -324,8 +325,9 @@ HRESULT create_heap_proxy(ID3D12DescriptorHeap* real, DeviceProxy* device, REFII
324 proxy_release(device); 325 proxy_release(device);
325 return hr; 326 return hr;
326 } 327 }
327 std::fprintf(stderr, "[rt] d3d12_xs: heap proxy %p wraps PC heap %p (device proxy %p)\n", 328 if (!d3d12_trace::enabled())
328 static_cast<void*>(proxy), static_cast<void*>(real), static_cast<void*>(device)); 329 std::fprintf(stderr, "[rt] d3d12_xs: heap proxy %p wraps PC heap %p (device proxy %p)\n",
330 static_cast<void*>(proxy), static_cast<void*>(real), static_cast<void*>(device));
329 heap_release(proxy); 331 heap_release(proxy);
330 return S_OK; 332 return S_OK;
331} 333}
@@ -518,8 +520,9 @@ HRESULT create_device_proxy(ID3D12Device* real, REFIID riid, void** out) {
518 delete proxy; 520 delete proxy;
519 return hr; 521 return hr;
520 } 522 }
521 std::fprintf(stderr, "[rt] d3d12_xs: device proxy %p wraps PC device %p (%u PC vtable slots)\n", 523 if (!d3d12_trace::enabled())
522 static_cast<void*>(proxy), static_cast<void*>(real), slot_count); 524 std::fprintf(stderr, "[rt] d3d12_xs: device proxy %p wraps PC device %p (%u PC vtable slots)\n",
525 static_cast<void*>(proxy), static_cast<void*>(real), slot_count);
523 proxy_release(proxy); // drop the creation reference; `out` holds the caller's reference 526 proxy_release(proxy); // drop the creation reference; `out` holds the caller's reference
524 return S_OK; 527 return S_OK;
525} 528}
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp
index 8e2ac0b03..20c870377 100644
--- a/runtime/src/main.cpp
+++ b/runtime/src/main.cpp
@@ -9,7 +9,7 @@
9// gta6rt --init <image> [--game-dir DIR] 9// gta6rt --init <image> [--game-dir DIR]
10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run. 10// The same mapping, then each loaded DLL's TLS callbacks and DllMain(DLL_PROCESS_ATTACH) are run.
11// 11//
12// gta6rt --run <exe> [--game-dir DIR] [-- game arguments...] 12// gta6rt --run <exe> [--game-dir DIR] [--trace-d3d12] [-- game arguments...]
13// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and 13// The same, then the EXE's own TLS callbacks and its entry point, on a game thread. The game's argv and
14// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run 14// command line are <exe name> plus the arguments after "--"; exit/ExitProcess from the game end the run
15// (shims/process.cpp). gta6rt prints the binding report first and returns the game's exit code. 15// (shims/process.cpp). gta6rt prints the binding report first and returns the game's exit code.
@@ -21,6 +21,7 @@
21#include <vector> 21#include <vector>
22 22
23#include "pe_loader.h" 23#include "pe_loader.h"
24#include "../shims/d3d12_trace.h"
24#include "../shims/process_shim.h" 25#include "../shims/process_shim.h"
25#include "../shims/shims.h" 26#include "../shims/shims.h"
26 27
@@ -38,8 +39,8 @@ const char* binding_name(rt::Binding b) {
38int usage() { 39int usage() {
39 std::fprintf(stderr, 40 std::fprintf(stderr,
40 "usage: gta6rt --dry-run <image> [--game-dir DIR]\n" 41 "usage: gta6rt --dry-run <image> [--game-dir DIR]\n"
41 " gta6rt --init <image> [--game-dir DIR]\n" 42 " gta6rt --init <image> [--game-dir DIR] [--trace-d3d12]\n"
42 " gta6rt --run <exe> [--game-dir DIR] [-- game arguments...]\n"); 43 " gta6rt --run <exe> [--game-dir DIR] [--trace-d3d12] [-- game arguments...]\n");
43 return 2; 44 return 2;
44} 45}
45 46
@@ -47,7 +48,7 @@ int usage() {
47 48
48int wmain(int argc, wchar_t** argv) { 49int wmain(int argc, wchar_t** argv) {
49 std::filesystem::path image, game_dir; 50 std::filesystem::path image, game_dir;
50 bool dry_run = false, init = false, run = false; 51 bool dry_run = false, init = false, run = false, trace_d3d12 = false;
51 std::vector<std::string> game_args; 52 std::vector<std::string> game_args;
52 for (int i = 1; i < argc; ++i) { 53 for (int i = 1; i < argc; ++i) {
53 std::wstring a = argv[i]; 54 std::wstring a = argv[i];
@@ -58,6 +59,7 @@ int wmain(int argc, wchar_t** argv) {
58 if (a == L"--dry-run") dry_run = true; 59 if (a == L"--dry-run") dry_run = true;
59 else if (a == L"--init") init = true; 60 else if (a == L"--init") init = true;
60 else if (a == L"--run") run = true; 61 else if (a == L"--run") run = true;
62 else if (a == L"--trace-d3d12") trace_d3d12 = true;
61 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i]; 63 else if (a == L"--game-dir" && i + 1 < argc) game_dir = argv[++i];
62 else if (image.empty()) image = a; 64 else if (image.empty()) image = a;
63 else return usage(); 65 else return usage();
@@ -65,6 +67,7 @@ int wmain(int argc, wchar_t** argv) {
65 if (image.empty()) return usage(); 67 if (image.empty()) return usage();
66 if (!dry_run && !init && !run) return usage(); 68 if (!dry_run && !init && !run) return usage();
67 if (game_dir.empty()) game_dir = image.parent_path(); 69 if (game_dir.empty()) game_dir = image.parent_path();
70 rt::d3d12_trace::set_enabled(trace_d3d12);
68 71
69 try { 72 try {
70 rt::Loader loader(game_dir); 73 rt::Loader loader(game_dir);
diff --git a/runtime/test/check_d3d12_trace.py b/runtime/test/check_d3d12_trace.py
new file mode 100644
index 000000000..d1333590c
--- /dev/null
+++ b/runtime/test/check_d3d12_trace.py
@@ -0,0 +1,61 @@
1"""Check the trace on the CPU command-list fixture, or on d3d12_com_test with --queue."""
2
3import argparse
4import re
5import subprocess
6
7
8def run_fixture(exe, *args):
9 process = subprocess.run([exe, *args], capture_output=True, text=True, encoding="utf-8", errors="replace",
10 timeout=30)
11 if process.returncode:
12 raise SystemExit(f"{exe} exited with {process.returncode}\n{process.stdout}{process.stderr}")
13 return [line for line in process.stderr.splitlines() if line.startswith("[d3d12-trace]")]
14
15
16def main():
17 parser = argparse.ArgumentParser(description=__doc__)
18 parser.add_argument("exe")
19 parser.add_argument("--queue", action="store_true", help="use the Vulkan-backed d3d12_com_test fixture")
20 options = parser.parse_args()
21
22 if run_fixture(options.exe):
23 raise SystemExit("D3D12 trace appeared with tracing disabled")
24
25 trace = run_fixture(options.exe, "--trace-d3d12")
26 entries = []
27 for number, line in enumerate(trace, 1):
28 if "synthetic-marker-argument" in line:
29 raise SystemExit("D3D12 trace leaked the synthetic marker payload")
30 match = re.fullmatch(r"\[d3d12-trace\] #(\d+) ([A-Za-z_][\w:]* (?:called|ok|failed|unsupported|ignored))", line)
31 if not match or int(match[1]) != number:
32 raise SystemExit(f"Invalid trace sequence at entry {number}\n{line}")
33 entries.append(match[2])
34
35 if options.queue:
36 expected = [
37 "ID3D12CommandQueue::ExecuteCommandLists failed",
38 "ID3D12GraphicsCommandList::SetMarker unsupported",
39 "ID3D12GraphicsCommandList::Close failed",
40 "ID3D12CommandQueue::Wait ok",
41 "ID3D12CommandQueue::ExecuteCommandLists ok",
42 "ID3D12CommandQueue::Signal ok",
43 ]
44 else:
45 expected = [
46 "ID3D12GraphicsCommandList::SetMarker unsupported",
47 "ID3D12GraphicsCommandList::SetMarker unsupported",
48 "ID3D12GraphicsCommandList::Close ok",
49 "ID3D12GraphicsCommandList::Close failed",
50 "ID3D12GraphicsCommandList::Reset ok",
51 ]
52
53 observed = iter(entries)
54 for entry in expected:
55 if entry not in observed:
56 raise SystemExit(f"Missing or out-of-order trace entry {entry!r}\n" + "\n".join(trace))
57 print(f"D3D12 trace verified with {len(entries)} ordered entries")
58
59
60if __name__ == "__main__":
61 main()
diff --git a/runtime/test/d3d12_com_test.cpp b/runtime/test/d3d12_com_test.cpp
index 13aec1629..3a4f066c3 100644
--- a/runtime/test/d3d12_com_test.cpp
+++ b/runtime/test/d3d12_com_test.cpp
@@ -4,6 +4,7 @@
4// Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error fails the test. 4// Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error fails the test.
5#include "vk_test_device.h" 5#include "vk_test_device.h"
6#include "../shims/d3d12_com.h" 6#include "../shims/d3d12_com.h"
7#include "../shims/d3d12_trace.h"
7 8
8#include <array> 9#include <array>
9#include <cstdint> 10#include <cstdint>
@@ -27,7 +28,9 @@ struct Ref {
27 28
28} // namespace 29} // namespace
29 30
30int main() { 31int main(int argc, char** argv) {
32 if (argc == 2 && std::strcmp(argv[1], "--trace-d3d12") == 0) rt::d3d12_trace::set_enabled(true);
33 else if (argc != 1) return 2;
31 using namespace leonida; 34 using namespace leonida;
32 vktest::Device d; 35 vktest::Device d;
33 EXPECT(d.create()); 36 EXPECT(d.create());
@@ -267,7 +270,7 @@ int main() {
267 EXPECT(invalid_bundle_call([&](auto* b) { b->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); })); 270 EXPECT(invalid_bundle_call([&](auto* b) { b->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); }));
268 271
269 list->Barrier(1, &group); 272 list->Barrier(1, &group);
270 list->DrawInstanced(3, 1, 0, 0); // not implemented: reported once by its generated stub, recording continues 273 list->SetMarker(0, nullptr, 0); // not implemented: reported once by its generated stub, recording continues
271 EXPECT(list->Close() == S_OK); 274 EXPECT(list->Close() == S_OK);
272 EXPECT(list->Close() == E_FAIL); // already closed 275 EXPECT(list->Close() == E_FAIL); // already closed
273 276
diff --git a/runtime/test/d3d12_gcl8_test.cpp b/runtime/test/d3d12_gcl8_test.cpp
index 166e98109..f6df5acc5 100644
--- a/runtime/test/d3d12_gcl8_test.cpp
+++ b/runtime/test/d3d12_gcl8_test.cpp
@@ -5,9 +5,11 @@
5// The method is recorded but not applied: the backend has no pipeline or draw path that could consume per-face 5// The method is recorded but not applied: the backend has no pipeline or draw path that could consume per-face
6// stencil references yet (docs/d3d12_vulkan.md section 3.2). 6// stencil references yet (docs/d3d12_vulkan.md section 3.2).
7#include "../shims/d3d12_com.h" 7#include "../shims/d3d12_com.h"
8#include "../shims/d3d12_trace.h"
8 9
9#include <cstddef> 10#include <cstddef>
10#include <cstdio> 11#include <cstdio>
12#include <cstring>
11 13
12#define EXPECT(condition) do { \ 14#define EXPECT(condition) do { \
13 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ 15 if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
@@ -25,7 +27,9 @@ struct Ref {
25 27
26} // namespace 28} // namespace
27 29
28int main() { 30int main(int argc, char** argv) {
31 if (argc == 2 && std::strcmp(argv[1], "--trace-d3d12") == 0) rt::d3d12_trace::set_enabled(true);
32 else if (argc != 1) return 2;
29 using namespace rt::d3d12; 33 using namespace rt::d3d12;
30 Ref<ID3D12CommandAllocator> allocator; 34 Ref<ID3D12CommandAllocator> allocator;
31 Ref<ID3D12GraphicsCommandList7> list7; 35 Ref<ID3D12GraphicsCommandList7> list7;
@@ -63,7 +67,12 @@ int main() {
63 EXPECT(front_back_stencil_ref(list8.p, front, back)); 67 EXPECT(front_back_stencil_ref(list8.p, front, back));
64 EXPECT(front == 3 && back == 4); 68 EXPECT(front == 3 && back == 4);
65 69
70 // The generated stub is still invoked for each call, even when the legacy diagnostic is deduplicated.
71 constexpr char marker[] = "synthetic-marker-argument";
72 list8.p->SetMarker(0, marker, static_cast<UINT>(sizeof(marker)));
73 list8.p->SetMarker(0, marker, static_cast<UINT>(sizeof(marker)));
66 EXPECT(list8.p->Close() == S_OK); 74 EXPECT(list8.p->Close() == S_OK);
75 EXPECT(list8.p->Close() == E_FAIL);
67 EXPECT(SUCCEEDED(list7.p->Reset(allocator.p, nullptr))); 76 EXPECT(SUCCEEDED(list7.p->Reset(allocator.p, nullptr)));
68 EXPECT(front_back_stencil_ref(list8.p, front, back)); 77 EXPECT(front_back_stencil_ref(list8.p, front, back));
69 EXPECT(front == 0 && back == 0); // Reset returns list state to its default 78 EXPECT(front == 0 && back == 0); // Reset returns list state to its default