upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/shims/d3d12_com.cpp
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/shims/d3d12_com.cpp
parentc432cdce127ed2d22a99aa6ef57d91db4a10b5bb (diff)
Runtime: add opt-in D3D12 call tracing
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp55
1 files changed, 43 insertions, 12 deletions
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); }