diff options
| author | justr4mzi <ramzihouhou8@gmail.com> | 2026-11-23 18:00:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 11:00:10 -0600 |
| commit | 17cc9a7a92ef4b1e83586562692d34e690f6b079 (patch) | |
| tree | 402b5534bf719aca38ba13b865e18a0104466e06 /runtime/shims/d3d12_com.cpp | |
| parent | 3e1e6ea119eec19ab3ccc4c7a3d83b0dc2b5bf92 (diff) | |
d3d12: validate and track SetDescriptorHeaps and directly-indexed root flags (#6674)
Squash-merged after diff review of PR #6674
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 90 |
1 files changed, 87 insertions, 3 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4dd766b31..58ecb89ee 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -202,6 +202,8 @@ public: | |||
| 202 | const std::vector<IUnknown*>& pinned() const { return held_; } | 202 | const std::vector<IUnknown*>& pinned() const { return held_; } |
| 203 | UINT front_stencil_ref() const { return front_stencil_ref_; } | 203 | UINT front_stencil_ref() const { return front_stencil_ref_; } |
| 204 | UINT back_stencil_ref() const { return back_stencil_ref_; } | 204 | UINT back_stencil_ref() const { return back_stencil_ref_; } |
| 205 | D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; } | ||
| 206 | D3D12_GPU_VIRTUAL_ADDRESS sampler_base() const { return sampler_base_; } | ||
| 205 | 207 | ||
| 206 | D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override { | 208 | D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override { |
| 207 | d3d12_trace::record("ID3D12CommandList::GetType"); | 209 | d3d12_trace::record("ID3D12CommandList::GetType"); |
| @@ -283,9 +285,42 @@ public: | |||
| 283 | set_pipeline(state); | 285 | set_pipeline(state); |
| 284 | } | 286 | } |
| 285 | 287 | ||
| 286 | // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs. | 288 | // Validates shader-visible descriptor heaps and tracks bound heap identities/bases (docs/d3d12_vulkan.md §3.2/§3.4). |
| 287 | void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override { | 289 | void STDMETHODCALLTYPE SetDescriptorHeaps(UINT num, ID3D12DescriptorHeap* const* heaps) override { |
| 288 | d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps", "ignored"); | 290 | d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps"); |
| 291 | if (type_ == D3D12_COMMAND_LIST_TYPE_COPY) return check(false); | ||
| 292 | if (num > 2 || (num && !heaps)) return check(false); | ||
| 293 | |||
| 294 | ID3D12DescriptorHeap* cbv = nullptr; | ||
| 295 | ID3D12DescriptorHeap* smp = nullptr; | ||
| 296 | D3D12_GPU_VIRTUAL_ADDRESS cbv_base = 0; | ||
| 297 | D3D12_GPU_VIRTUAL_ADDRESS smp_base = 0; | ||
| 298 | IUnknown* const owner = this->attached_device(); | ||
| 299 | for (UINT i = 0; i < num; ++i) { | ||
| 300 | ID3D12DescriptorHeap* raw = heaps[i]; | ||
| 301 | if (!raw) return check(false); | ||
| 302 | if (ID3D12DescriptorHeap* real = d3d12_xs::heap_proxy_real_heap(raw)) raw = real; | ||
| 303 | D3D12_DESCRIPTOR_HEAP_DESC desc{}; | ||
| 304 | D3D12_GPU_VIRTUAL_ADDRESS base = 0; | ||
| 305 | IUnknown* const heap_owner = descriptor_heap_info(raw, desc, base); | ||
| 306 | if (!heap_owner || (owner && heap_owner != owner)) return check(false); | ||
| 307 | if (!(desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) return check(false); | ||
| 308 | if (desc.Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) { | ||
| 309 | if (cbv) return check(false); | ||
| 310 | cbv = raw; | ||
| 311 | cbv_base = base; | ||
| 312 | } else if (desc.Type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) { | ||
| 313 | if (smp) return check(false); | ||
| 314 | smp = raw; | ||
| 315 | smp_base = base; | ||
| 316 | } else { | ||
| 317 | return check(false); | ||
| 318 | } | ||
| 319 | } | ||
| 320 | hold(cbv); | ||
| 321 | hold(smp); | ||
| 322 | if (defer([=](GraphicsCommandList& l) { l.verify_bundle_descriptor_heaps(cbv, smp); })) return; | ||
| 323 | set_descriptor_heaps(cbv, cbv_base, smp, smp_base); | ||
| 289 | } | 324 | } |
| 290 | 325 | ||
| 291 | void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { | 326 | void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { |
| @@ -1141,6 +1176,10 @@ private: | |||
| 1141 | RootState graphics_; | 1176 | RootState graphics_; |
| 1142 | std::uintptr_t compute_pipeline_ = 0; | 1177 | std::uintptr_t compute_pipeline_ = 0; |
| 1143 | std::uintptr_t graphics_pipeline_ = 0; | 1178 | std::uintptr_t graphics_pipeline_ = 0; |
| 1179 | ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr; | ||
| 1180 | ID3D12DescriptorHeap* sampler_heap_ = nullptr; | ||
| 1181 | D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0; | ||
| 1182 | D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0; | ||
| 1144 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED | 1183 | std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED |
| 1145 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; | 1184 | std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; |
| 1146 | leonida::IndexBufferView index_{}; | 1185 | leonida::IndexBufferView index_{}; |
| @@ -1205,10 +1244,44 @@ private: | |||
| 1205 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); | 1244 | stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); |
| 1206 | } | 1245 | } |
| 1207 | 1246 | ||
| 1247 | void set_descriptor_heaps(ID3D12DescriptorHeap* cbv, D3D12_GPU_VIRTUAL_ADDRESS cbv_base, | ||
| 1248 | ID3D12DescriptorHeap* smp, D3D12_GPU_VIRTUAL_ADDRESS smp_base) { | ||
| 1249 | cbv_srv_uav_heap_ = cbv; | ||
| 1250 | cbv_srv_uav_base_ = cbv_base; | ||
| 1251 | sampler_heap_ = smp; | ||
| 1252 | sampler_base_ = smp_base; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | // D3D12 bundle rule: every non-null heap bound by a bundle's SetDescriptorHeaps must match the parent list's | ||
| 1256 | // currently bound heap of that type, without clearing any omitted heap slot on the parent. | ||
| 1257 | void verify_bundle_descriptor_heaps(ID3D12DescriptorHeap* cbv, ID3D12DescriptorHeap* smp) { | ||
| 1258 | if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false); | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | void note_direct_heap_indexing(const RootArgumentLayout* layout) const { | ||
| 1262 | if (!layout) return; | ||
| 1263 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { | ||
| 1264 | if (!cbv_srv_uav_heap_) | ||
| 1265 | note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound"); | ||
| 1266 | else | ||
| 1267 | note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | ||
| 1268 | } | ||
| 1269 | if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) { | ||
| 1270 | if (!sampler_heap_) | ||
| 1271 | note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound"); | ||
| 1272 | else | ||
| 1273 | note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)"); | ||
| 1274 | } | ||
| 1275 | } | ||
| 1276 | |||
| 1208 | void reset_state(ID3D12PipelineState* state) { | 1277 | void reset_state(ID3D12PipelineState* state) { |
| 1209 | front_stencil_ref_ = back_stencil_ref_ = 0; | 1278 | front_stencil_ref_ = back_stencil_ref_ = 0; |
| 1210 | compute_ = {}; | 1279 | compute_ = {}; |
| 1211 | graphics_ = {}; | 1280 | graphics_ = {}; |
| 1281 | cbv_srv_uav_heap_ = nullptr; | ||
| 1282 | sampler_heap_ = nullptr; | ||
| 1283 | cbv_srv_uav_base_ = 0; | ||
| 1284 | sampler_base_ = 0; | ||
| 1212 | topology_ = 0; | 1285 | topology_ = 0; |
| 1213 | vertex_buffers_ = {}; | 1286 | vertex_buffers_ = {}; |
| 1214 | index_ = {}; | 1287 | index_ = {}; |
| @@ -1237,6 +1310,7 @@ private: | |||
| 1237 | 1310 | ||
| 1238 | void record_dispatch(UINT x, UINT y, UINT z) { | 1311 | void record_dispatch(UINT x, UINT y, UINT z) { |
| 1239 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 1312 | if (!compute_pipeline_ || !compute_.layout) return check(false); |
| 1313 | note_direct_heap_indexing(compute_.layout); | ||
| 1240 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 1314 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); |
| 1241 | } | 1315 | } |
| 1242 | 1316 | ||
| @@ -1256,6 +1330,7 @@ private: | |||
| 1256 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1330 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 1257 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1331 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 1258 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1332 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| 1333 | note_direct_heap_indexing(graphics_.layout); | ||
| 1259 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, | 1334 | // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, |
| 1260 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 1335 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 1261 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 1336 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; |
| @@ -1892,4 +1967,13 @@ bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Com | |||
| 1892 | return true; | 1967 | return true; |
| 1893 | } | 1968 | } |
| 1894 | 1969 | ||
| 1970 | bool bound_descriptor_heap_bases(ID3D12GraphicsCommandList* list, D3D12_GPU_VIRTUAL_ADDRESS& cbv_srv_uav, | ||
| 1971 | D3D12_GPU_VIRTUAL_ADDRESS& sampler) { | ||
| 1972 | const GraphicsCommandList* l = GraphicsCommandList::from(list); | ||
| 1973 | if (!l) return false; | ||
| 1974 | cbv_srv_uav = l->cbv_srv_uav_base(); | ||
| 1975 | sampler = l->sampler_base(); | ||
| 1976 | return true; | ||
| 1977 | } | ||
| 1978 | |||
| 1895 | } // namespace rt::d3d12 | 1979 | } // namespace rt::d3d12 |