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:
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp136
1 files changed, 121 insertions, 15 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index b490f7ab0..af14c6b9b 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -24,6 +24,22 @@
24 24
25namespace rt::d3d12 { 25namespace rt::d3d12 {
26 26
27static std::uint64_t buffer_tiles(ID3D12Resource* buffer) {
28 if (!buffer) return 0;
29 const auto desc = buffer->GetDesc();
30 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || !desc.Width) return 0;
31 return 1 + (desc.Width - 1) / leonida::tile_size_in_bytes;
32}
33
34static bool buffer_tile_region(const D3D12_TILED_RESOURCE_COORDINATE* start, const D3D12_TILE_REGION_SIZE* size,
35 std::uint64_t tiles, leonida::BufferTileRegion& region) {
36 if (start && (start->Y || start->Z || start->Subresource)) return false;
37 if (size && size->UseBox && (size->Height != 1 || size->Depth != 1)) return false;
38 region.first = start ? start->X : 0;
39 region.count = size ? (size->UseBox ? size->Width : size->NumTiles) : (start ? 1 : tiles);
40 return region.count && region.first < tiles && region.count <= tiles - region.first;
41}
42
27void stub(const char* method) { 43void stub(const char* method) {
28 // Generated API stubs pass a bare method name; explanatory diagnostics are not separate calls. 44 // Generated API stubs pass a bare method name; explanatory diagnostics are not separate calls.
29 if (!std::strchr(method, '(')) d3d12_trace::record(method, "unsupported"); 45 if (!std::strchr(method, '(')) d3d12_trace::record(method, "unsupported");
@@ -231,7 +247,9 @@ public:
231 } 247 }
232 248
233 // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs. 249 // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs.
234 void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override {} 250 void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override {
251 d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps", "ignored");
252 }
235 253
236 void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { 254 void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override {
237 constants(compute_, index, 1, &value, offset); 255 constants(compute_, index, 1, &value, offset);
@@ -267,6 +285,7 @@ public:
267 address(graphics_, index, va, rt::rootsig::ParamType::Descriptor); 285 address(graphics_, index, va, rt::rootsig::ParamType::Descriptor);
268 } 286 }
269 void STDMETHODCALLTYPE SetGraphicsRootDescriptorTable(UINT index, D3D12_GPU_DESCRIPTOR_HANDLE table) override { 287 void STDMETHODCALLTYPE SetGraphicsRootDescriptorTable(UINT index, D3D12_GPU_DESCRIPTOR_HANDLE table) override {
288 d3d12_trace::record("ID3D12GraphicsCommandList::SetGraphicsRootDescriptorTable");
270 address(graphics_, index, table.ptr, rt::rootsig::ParamType::Table); 289 address(graphics_, index, table.ptr, rt::rootsig::ParamType::Table);
271 } 290 }
272 291
@@ -417,6 +436,25 @@ public:
417 check(list_.copy_buffer({resource_id(dst), dst_offset, resource_id(src), src_offset, bytes})); 436 check(list_.copy_buffer({resource_id(dst), dst_offset, resource_id(src), src_offset, bytes}));
418 } 437 }
419 438
439 void STDMETHODCALLTYPE CopyTiles(ID3D12Resource* tiled, const D3D12_TILED_RESOURCE_COORDINATE* start,
440 const D3D12_TILE_REGION_SIZE* size, ID3D12Resource* linear,
441 UINT64 linear_offset, D3D12_TILE_COPY_FLAGS flags) override {
442 d3d12_trace::record("ID3D12GraphicsCommandList::CopyTiles");
443 const auto direction = flags & ~D3D12_TILE_COPY_FLAG_NO_HAZARD;
444 const bool to_linear = direction == D3D12_TILE_COPY_FLAG_SWIZZLED_TILED_RESOURCE_TO_LINEAR_BUFFER;
445 leonida::BufferTileRegion region;
446 if (!start || !size || !linear || linear == tiled ||
447 !buffer_tile_region(start, size, buffer_tiles(tiled), region) ||
448 (!to_linear && direction != D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE))
449 return check(false);
450 const auto tiled_id = resource_id(tiled), linear_id = resource_id(linear);
451 const auto tiled_offset = region.first * leonida::tile_size_in_bytes;
452 const auto bytes = region.count * leonida::tile_size_in_bytes;
453 check(list_.copy_buffer(to_linear
454 ? leonida::CopyBuffer{linear_id, linear_offset, tiled_id, tiled_offset, bytes, tiled_id}
455 : leonida::CopyBuffer{tiled_id, tiled_offset, linear_id, linear_offset, bytes, tiled_id}));
456 }
457
420 void STDMETHODCALLTYPE CopyTextureRegion(const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z, 458 void STDMETHODCALLTYPE CopyTextureRegion(const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z,
421 const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) override { 459 const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) override {
422 d3d12_trace::record("ID3D12GraphicsCommandList::CopyTextureRegion"); 460 d3d12_trace::record("ID3D12GraphicsCommandList::CopyTextureRegion");
@@ -1074,22 +1112,36 @@ public:
1074 return true; 1112 return true;
1075 } 1113 }
1076 1114
1077 // Reserved resources are fully backed at creation (docs/d3d12_vulkan.md section 4.2), so tile mappings are not 1115 void STDMETHODCALLTYPE UpdateTileMappings(ID3D12Resource* resource, UINT region_count,
1078 // emulated: every tile keeps its own memory. What that loses is reported once each: two resources mapped to the 1116 const D3D12_TILED_RESOURCE_COORDINATE* starts,
1079 // same heap tiles do not alias, and a tile unmapped (or remapped) keeps its contents. 1117 const D3D12_TILE_REGION_SIZE* sizes, ID3D12Heap* heap, UINT range_count,
1080 void STDMETHODCALLTYPE UpdateTileMappings(ID3D12Resource*, UINT, const D3D12_TILED_RESOURCE_COORDINATE*, 1118 const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets,
1081 const D3D12_TILE_REGION_SIZE*, ID3D12Heap*, UINT, 1119 const UINT* counts, D3D12_TILE_MAPPING_FLAGS mapping_flags) override {
1082 const D3D12_TILE_RANGE_FLAGS*, const UINT*, const UINT*, 1120 constexpr auto method = "ID3D12CommandQueue::UpdateTileMappings";
1083 D3D12_TILE_MAPPING_FLAGS) override { 1121 if (!update_buffer_tiles(resource, region_count, starts, sizes, heap, range_count, flags, offsets,
1084 d3d12_trace::record("ID3D12CommandQueue::UpdateTileMappings", "ignored"); 1122 counts, mapping_flags)) {
1085 note_once("ID3D12CommandQueue::UpdateTileMappings ignored: reserved resources are fully backed, mappings are not emulated"); 1123 d3d12_trace::record(method, "refused");
1124 note_once("ID3D12CommandQueue::UpdateTileMappings: invalid mapping or buffer sparse residency unavailable");
1125 } else {
1126 d3d12_trace::record(method, "ok");
1127 }
1086 } 1128 }
1087 1129
1088 void STDMETHODCALLTYPE CopyTileMappings(ID3D12Resource*, const D3D12_TILED_RESOURCE_COORDINATE*, ID3D12Resource*, 1130 void STDMETHODCALLTYPE CopyTileMappings(ID3D12Resource* dst, const D3D12_TILED_RESOURCE_COORDINATE* dst_start,
1089 const D3D12_TILED_RESOURCE_COORDINATE*, const D3D12_TILE_REGION_SIZE*, 1131 ID3D12Resource* src, const D3D12_TILED_RESOURCE_COORDINATE* src_start,
1090 D3D12_TILE_MAPPING_FLAGS) override { 1132 const D3D12_TILE_REGION_SIZE* size, D3D12_TILE_MAPPING_FLAGS flags) override {
1091 d3d12_trace::record("ID3D12CommandQueue::CopyTileMappings", "ignored"); 1133 constexpr auto method = "ID3D12CommandQueue::CopyTileMappings";
1092 note_once("ID3D12CommandQueue::CopyTileMappings ignored: reserved resources are fully backed, mappings are not emulated"); 1134 std::lock_guard lock(mutex_);
1135 leonida::BufferTileRegion from, to;
1136 if ((flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD) || !dst_start || !src_start || !size ||
1137 !buffer_tile_region(src_start, size, buffer_tiles(src), from) ||
1138 !buffer_tile_region(dst_start, size, buffer_tiles(dst), to) ||
1139 !queue_.copy_tile_mappings(resource_id(dst), to.first, resource_id(src), from)) {
1140 d3d12_trace::record(method, "refused");
1141 note_once("ID3D12CommandQueue::CopyTileMappings: invalid mapping or buffer sparse residency unavailable");
1142 } else {
1143 d3d12_trace::record(method, "ok");
1144 }
1093 } 1145 }
1094 1146
1095 HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override { 1147 HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override {
@@ -1245,6 +1297,60 @@ private:
1245 queue_.marker(kind, label.c_str()); 1297 queue_.marker(kind, label.c_str());
1246 } 1298 }
1247 1299
1300 bool update_buffer_tiles(ID3D12Resource* resource, UINT region_count, const D3D12_TILED_RESOURCE_COORDINATE* starts,
1301 const D3D12_TILE_REGION_SIZE* sizes, ID3D12Heap* heap, UINT range_count,
1302 const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, const UINT* counts,
1303 D3D12_TILE_MAPPING_FLAGS mapping_flags) {
1304 if (!region_count || !range_count || (!starts && region_count != 1) ||
1305 (mapping_flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD)) return false;
1306 const auto tiles = buffer_tiles(resource);
1307 std::vector<leonida::BufferTileRegion> regions(region_count);
1308 std::uint64_t total = 0;
1309 for (UINT i = 0; i < region_count; ++i) {
1310 if (!buffer_tile_region(starts ? starts + i : nullptr, sizes ? sizes + i : nullptr, tiles, regions[i]))
1311 return false;
1312 if (regions[i].count > UINT64_MAX - total) return false;
1313 total += regions[i].count;
1314 }
1315
1316 std::vector<leonida::BufferTileRange> ranges;
1317 ranges.reserve(range_count);
1318 std::uint64_t remaining = total, heap_tiles_required = 0;
1319 for (UINT i = 0; i < range_count; ++i) {
1320 const auto count = counts ? counts[i] : (range_count == 1 ? total : 1);
1321 if (!count) continue;
1322 if (count > remaining) return false;
1323 remaining -= count;
1324 using leonida::TileRangeMode;
1325 const auto flag = flags ? flags[i] : D3D12_TILE_RANGE_FLAG_NONE;
1326 switch (flag) {
1327 case D3D12_TILE_RANGE_FLAG_NONE:
1328 case D3D12_TILE_RANGE_FLAG_REUSE_SINGLE_TILE: {
1329 if (!offsets) return false;
1330 const auto used = flag == D3D12_TILE_RANGE_FLAG_NONE ? count : 1;
1331 if (used > UINT64_MAX - offsets[i]) return false;
1332 heap_tiles_required = std::max(heap_tiles_required, offsets[i] + used);
1333 ranges.push_back({flag == D3D12_TILE_RANGE_FLAG_NONE ? TileRangeMode::sequential : TileRangeMode::reuse,
1334 offsets[i], count});
1335 break;
1336 }
1337 case D3D12_TILE_RANGE_FLAG_NULL:
1338 ranges.push_back({TileRangeMode::unmap, 0, count});
1339 break;
1340 case D3D12_TILE_RANGE_FLAG_SKIP:
1341 ranges.push_back({TileRangeMode::skip, 0, count});
1342 break;
1343 default:
1344 return false;
1345 }
1346 }
1347 if (remaining || (heap_tiles_required &&
1348 (!heap || heap_tiles_required > heap->GetDesc().SizeInBytes / leonida::tile_size_in_bytes))) return false;
1349 std::lock_guard lock(mutex_);
1350 return queue_.update_tiles(resource_id(resource), regions, ranges,
1351 [&] { return heap_tiles_required ? tile_heap(heap, context_) : nullptr; });
1352 }
1353
1248 D3D12_COMMAND_QUEUE_DESC desc_; 1354 D3D12_COMMAND_QUEUE_DESC desc_;
1249 leonida::VulkanContext& context_; 1355 leonida::VulkanContext& context_;
1250 std::mutex mutex_; 1356 std::mutex mutex_;