diff options
| author | DenRakEiw <89697885+DenRakEiw@users.noreply.github.com> | 2026-11-23 22:32:17 +0100 |
|---|---|---|
| committer | DenRakEiw <89697885+DenRakEiw@users.noreply.github.com> | 2026-11-23 22:32:17 +0100 |
| commit | d59013b815317e1e8386b1fdf29b45d9bae46c65 (patch) | |
| tree | 1c5d82465f590bf6565fbf4e9ad1dd6606e76a82 /runtime/shims/d3d12_com.cpp | |
| parent | 5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff) | |
d3d12: harden Set*Root* closed-list, zero-count null, and foreign root sig handling
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index f25168a2a..2162b2a0b 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -1161,16 +1161,20 @@ private: | |||
| 1161 | // signature that is already bound changes nothing: "existing root signature bindings do not become stale" | 1161 | // signature that is already bound changes nothing: "existing root signature bindings do not become stale" |
| 1162 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. | 1162 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. |
| 1163 | void bind_root(RootState& state, ID3D12RootSignature* signature) { | 1163 | void bind_root(RootState& state, ID3D12RootSignature* signature) { |
| 1164 | if (list_.closed()) return; | ||
| 1165 | const RootArgumentLayout* layout = root_signature_layout(signature); | ||
| 1166 | if (signature && !layout) { | ||
| 1167 | stub("ID3D12GraphicsCommandList::Set*RootSignature(foreign root signature)"); | ||
| 1168 | return check(false); | ||
| 1169 | } | ||
| 1164 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | 1170 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { |
| 1165 | l.bind_root(compute ? l.compute_ : l.graphics_, signature); | 1171 | l.bind_root(compute ? l.compute_ : l.graphics_, signature); |
| 1166 | })) | 1172 | })) |
| 1167 | return hold(signature); // the bundle keeps it for as long as it can be executed | 1173 | return hold(signature); // the bundle keeps it for as long as it can be executed |
| 1168 | const RootArgumentLayout* layout = root_signature_layout(signature); | ||
| 1169 | if (layout && layout == state.layout) return; | 1174 | if (layout && layout == state.layout) return; |
| 1170 | hold(signature); // state.layout points into it | 1175 | hold(signature); // state.layout points into it |
| 1171 | state.layout = layout; | 1176 | state.layout = layout; |
| 1172 | state.data = {}; | 1177 | state.data = {}; |
| 1173 | if (signature && !state.layout) stub("ID3D12GraphicsCommandList::Set*RootSignature(foreign root signature)"); | ||
| 1174 | // CreateRootSignature accepted it for the other bind point only (its log line says why). | 1178 | // CreateRootSignature accepted it for the other bind point only (its log line says why). |
| 1175 | if (layout && !(is_compute(state) ? layout->compute_ok : layout->graphics_ok)) { | 1179 | if (layout && !(is_compute(state) ? layout->compute_ok : layout->graphics_ok)) { |
| 1176 | stub(is_compute(state) ? "SetComputeRootSignature(signature that does not fit the compute layout)" | 1180 | stub(is_compute(state) ? "SetComputeRootSignature(signature that does not fit the compute layout)" |
| @@ -1205,31 +1209,30 @@ private: | |||
| 1205 | } | 1209 | } |
| 1206 | 1210 | ||
| 1207 | void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { | 1211 | void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { |
| 1212 | if (list_.closed()) return; | ||
| 1208 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { | 1213 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { |
| 1209 | // The layout is the executing list's, so the range is checked when it runs; no signature takes more | 1214 | // The layout is the executing list's, so the range is checked when it runs; no signature takes more |
| 1210 | // than D3D12_MAX_ROOT_COST dwords, which bounds what is copied from the caller's memory. | 1215 | // than D3D12_MAX_ROOT_COST dwords, which bounds what is copied from the caller's memory. |
| 1211 | if (count > D3D12_MAX_ROOT_COST) return check(false); | 1216 | if (count > D3D12_MAX_ROOT_COST || (count && !data)) return check(false); |
| 1212 | const auto* words = static_cast<const std::uint32_t*>(data); | 1217 | const auto* words = static_cast<const std::uint32_t*>(data); |
| 1213 | std::vector<std::uint32_t> copy; | 1218 | std::vector<std::uint32_t> copy; |
| 1214 | if (data) copy.assign(words, words + count); | 1219 | if (count) copy.assign(words, words + count); |
| 1215 | // A count of 0 with a non-null pointer is a valid call; an empty vector's data() may be null. | 1220 | defer([=, compute = &state == &compute_, copy = std::move(copy)](GraphicsCommandList& l) { |
| 1216 | static constexpr std::uint32_t none = 0; | ||
| 1217 | defer([=, compute = &state == &compute_, copy = std::move(copy), | ||
| 1218 | given = data != nullptr](GraphicsCommandList& l) { | ||
| 1219 | l.constants(compute ? l.compute_ : l.graphics_, index, count, | 1221 | l.constants(compute ? l.compute_ : l.graphics_, index, count, |
| 1220 | !given ? nullptr : copy.empty() ? &none : copy.data(), offset); | 1222 | copy.empty() ? nullptr : copy.data(), offset); |
| 1221 | }); | 1223 | }); |
| 1222 | return; | 1224 | return; |
| 1223 | } | 1225 | } |
| 1224 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); | 1226 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); |
| 1225 | if (!p) return; | 1227 | if (!p) return; |
| 1226 | if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); | 1228 | if ((count && !data) || std::uint64_t(offset) + count > p->constant_count) return check(false); |
| 1227 | const auto* values = static_cast<const std::uint32_t*>(data); | 1229 | const auto* values = static_cast<const std::uint32_t*>(data); |
| 1228 | const std::uint32_t first = first_dword(state, *p); | 1230 | const std::uint32_t first = first_dword(state, *p); |
| 1229 | for (UINT i = 0; i < count; ++i) write(state, first + offset + i, values[i]); | 1231 | for (UINT i = 0; i < count; ++i) write(state, first + offset + i, values[i]); |
| 1230 | } | 1232 | } |
| 1231 | 1233 | ||
| 1232 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { | 1234 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { |
| 1235 | if (list_.closed()) return; | ||
| 1233 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | 1236 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { |
| 1234 | l.address(compute ? l.compute_ : l.graphics_, index, va, type); | 1237 | l.address(compute ? l.compute_ : l.graphics_, index, va, type); |
| 1235 | })) | 1238 | })) |