diff options
| author | gabrielsteglich <63128571+gabrielsteglich@users.noreply.github.com> | 2026-11-22 13:13:27 -0300 |
|---|---|---|
| committer | gabrielsteglich <63128571+gabrielsteglich@users.noreply.github.com> | 2026-11-22 13:39:08 -0300 |
| commit | f8e89767f66de8f2a375615ca96a10459a87fef7 (patch) | |
| tree | 14548229b6e2b26c68105d88de104cdf8ae37502 /runtime/shims/d3d12_com.cpp | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
Runtime: map sparse reserved RGBA8 texture tiles (D3D-285)
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 121 |
1 files changed, 87 insertions, 34 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 663f5f3a5..502fb1143 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -24,20 +24,39 @@ | |||
| 24 | 24 | ||
| 25 | namespace rt::d3d12 { | 25 | namespace rt::d3d12 { |
| 26 | 26 | ||
| 27 | static std::uint64_t buffer_tiles(ID3D12Resource* buffer) { | 27 | struct TileGrid { |
| 28 | if (!buffer) return 0; | 28 | std::uint64_t columns = 0; |
| 29 | const auto desc = buffer->GetDesc(); | 29 | std::uint64_t rows = 0; |
| 30 | if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || !desc.Width) return 0; | 30 | }; |
| 31 | return 1 + (desc.Width - 1) / leonida::tile_size_in_bytes; | 31 | |
| 32 | static TileGrid tile_grid(ID3D12Resource* resource) { | ||
| 33 | if (!resource) return {}; | ||
| 34 | const auto desc = resource->GetDesc(); | ||
| 35 | if (desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && desc.Width) | ||
| 36 | return {1 + (desc.Width - 1) / leonida::tile_size_in_bytes, 1}; | ||
| 37 | if (!is_sparse_image(resource)) return {}; | ||
| 38 | constexpr auto shape = leonida::element_tile_shape(false, 4); | ||
| 39 | return {1 + (desc.Width - 1) / shape.width, 1 + (desc.Height - 1) / shape.height}; | ||
| 32 | } | 40 | } |
| 33 | 41 | ||
| 34 | static bool buffer_tile_region(const D3D12_TILED_RESOURCE_COORDINATE* start, const D3D12_TILE_REGION_SIZE* size, | 42 | static bool append_tile_regions(const D3D12_TILED_RESOURCE_COORDINATE* start, const D3D12_TILE_REGION_SIZE* size, |
| 35 | std::uint64_t tiles, leonida::BufferTileRegion& region) { | 43 | TileGrid grid, std::vector<leonida::TileRegion>& regions) { |
| 36 | if (start && (start->Y || start->Z || start->Subresource)) return false; | 44 | const auto x = start ? start->X : 0; |
| 37 | if (size && size->UseBox && (size->Height != 1 || size->Depth != 1)) return false; | 45 | const auto y = start ? start->Y : 0; |
| 38 | region.first = start ? start->X : 0; | 46 | if ((start && (start->Z || start->Subresource)) || x >= grid.columns || y >= grid.rows) return false; |
| 39 | region.count = size ? (size->UseBox ? size->Width : size->NumTiles) : (start ? 1 : tiles); | 47 | const auto first = y * grid.columns + x; |
| 40 | return region.count && region.first < tiles && region.count <= tiles - region.first; | 48 | if (size && size->UseBox) { |
| 49 | if (size->Depth != 1 || !size->Width || !size->Height || | ||
| 50 | size->Width > grid.columns - x || size->Height > grid.rows - y) return false; | ||
| 51 | for (UINT row = 0; row < size->Height; ++row) | ||
| 52 | regions.push_back({first + row * grid.columns, size->Width}); | ||
| 53 | return true; | ||
| 54 | } | ||
| 55 | const auto tiles = grid.columns * grid.rows; | ||
| 56 | const auto count = size ? size->NumTiles : (start ? 1 : tiles); | ||
| 57 | if (!count || count > tiles - first) return false; | ||
| 58 | regions.push_back({first, count}); | ||
| 59 | return true; | ||
| 41 | } | 60 | } |
| 42 | 61 | ||
| 43 | void stub(const char* method) { | 62 | void stub(const char* method) { |
| @@ -440,12 +459,41 @@ public: | |||
| 440 | d3d12_trace::record("ID3D12GraphicsCommandList::CopyTiles"); | 459 | d3d12_trace::record("ID3D12GraphicsCommandList::CopyTiles"); |
| 441 | const auto direction = flags & ~D3D12_TILE_COPY_FLAG_NO_HAZARD; | 460 | const auto direction = flags & ~D3D12_TILE_COPY_FLAG_NO_HAZARD; |
| 442 | const bool to_linear = direction == D3D12_TILE_COPY_FLAG_SWIZZLED_TILED_RESOURCE_TO_LINEAR_BUFFER; | 461 | const bool to_linear = direction == D3D12_TILE_COPY_FLAG_SWIZZLED_TILED_RESOURCE_TO_LINEAR_BUFFER; |
| 443 | leonida::BufferTileRegion region; | 462 | const auto grid = tile_grid(tiled); |
| 463 | std::vector<leonida::TileRegion> regions; | ||
| 444 | if (!start || !size || !linear || linear == tiled || | 464 | if (!start || !size || !linear || linear == tiled || |
| 445 | !buffer_tile_region(start, size, buffer_tiles(tiled), region) || | 465 | !append_tile_regions(start, size, grid, regions) || |
| 446 | (!to_linear && direction != D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE)) | 466 | (!to_linear && direction != D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE)) |
| 447 | return check(false); | 467 | return check(false); |
| 448 | const auto tiled_id = resource_id(tiled), linear_id = resource_id(linear); | 468 | const auto tiled_id = resource_id(tiled), linear_id = resource_id(linear); |
| 469 | if (is_sparse_image(tiled)) { | ||
| 470 | const auto buffer = linear->GetDesc(); | ||
| 471 | const auto desc = tiled->GetDesc(); | ||
| 472 | const auto count = size->UseBox ? std::uint64_t(size->Width) * size->Height : size->NumTiles; | ||
| 473 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || buffer.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || | ||
| 474 | linear_offset > buffer.Width || count > (buffer.Width - linear_offset) / leonida::tile_size_in_bytes) | ||
| 475 | return check(false); | ||
| 476 | constexpr auto shape = leonida::element_tile_shape(false, 4); | ||
| 477 | const leonida::CopyLocation image{tiled_id, false, 0, 0, 0, 0, 0, 0}; | ||
| 478 | for (const auto& region : regions) { | ||
| 479 | for (std::uint64_t tile = region.first; tile < region.first + region.count; ++tile) { | ||
| 480 | const auto x = std::uint32_t(tile % grid.columns) * shape.width; | ||
| 481 | const auto y = std::uint32_t(tile / grid.columns) * shape.height; | ||
| 482 | const auto width = std::min(shape.width, std::uint32_t(desc.Width) - x); | ||
| 483 | const auto height = std::min(shape.height, desc.Height - y); | ||
| 484 | const leonida::CopyLocation footprint{ | ||
| 485 | linear_id, true, 0, linear_offset, shape.width, shape.height, 1, shape.width * 4}; | ||
| 486 | auto copy = to_linear | ||
| 487 | ? leonida::CopyTexture{footprint, 0, 0, 0, image, true, {x, y, 0, x + width, y + height, 1}} | ||
| 488 | : leonida::CopyTexture{image, x, y, 0, footprint, true, {0, 0, 0, width, height, 1}}; | ||
| 489 | copy.tile_footprint = true; | ||
| 490 | check(list_.copy_texture(copy)); | ||
| 491 | linear_offset += leonida::tile_size_in_bytes; | ||
| 492 | } | ||
| 493 | } | ||
| 494 | return; | ||
| 495 | } | ||
| 496 | const auto& region = regions.front(); | ||
| 449 | const auto tiled_offset = region.first * leonida::tile_size_in_bytes; | 497 | const auto tiled_offset = region.first * leonida::tile_size_in_bytes; |
| 450 | const auto bytes = region.count * leonida::tile_size_in_bytes; | 498 | const auto bytes = region.count * leonida::tile_size_in_bytes; |
| 451 | check(list_.copy_buffer(to_linear | 499 | check(list_.copy_buffer(to_linear |
| @@ -1116,10 +1164,10 @@ public: | |||
| 1116 | const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, | 1164 | const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, |
| 1117 | const UINT* counts, D3D12_TILE_MAPPING_FLAGS mapping_flags) override { | 1165 | const UINT* counts, D3D12_TILE_MAPPING_FLAGS mapping_flags) override { |
| 1118 | constexpr auto method = "ID3D12CommandQueue::UpdateTileMappings"; | 1166 | constexpr auto method = "ID3D12CommandQueue::UpdateTileMappings"; |
| 1119 | if (!update_buffer_tiles(resource, region_count, starts, sizes, heap, range_count, flags, offsets, | 1167 | if (!update_tiles(resource, region_count, starts, sizes, heap, range_count, flags, offsets, counts, |
| 1120 | counts, mapping_flags)) { | 1168 | mapping_flags)) { |
| 1121 | d3d12_trace::record(method, "refused"); | 1169 | d3d12_trace::record(method, "refused"); |
| 1122 | note_once("ID3D12CommandQueue::UpdateTileMappings: invalid mapping or buffer sparse residency unavailable"); | 1170 | note_once("ID3D12CommandQueue::UpdateTileMappings: invalid mapping or sparse residency unavailable"); |
| 1123 | } else { | 1171 | } else { |
| 1124 | d3d12_trace::record(method, "ok"); | 1172 | d3d12_trace::record(method, "ok"); |
| 1125 | } | 1173 | } |
| @@ -1130,13 +1178,13 @@ public: | |||
| 1130 | const D3D12_TILE_REGION_SIZE* size, D3D12_TILE_MAPPING_FLAGS flags) override { | 1178 | const D3D12_TILE_REGION_SIZE* size, D3D12_TILE_MAPPING_FLAGS flags) override { |
| 1131 | constexpr auto method = "ID3D12CommandQueue::CopyTileMappings"; | 1179 | constexpr auto method = "ID3D12CommandQueue::CopyTileMappings"; |
| 1132 | std::lock_guard lock(mutex_); | 1180 | std::lock_guard lock(mutex_); |
| 1133 | leonida::BufferTileRegion from, to; | 1181 | std::vector<leonida::TileRegion> from, to; |
| 1134 | if ((flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD) || !dst_start || !src_start || !size || | 1182 | if ((flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD) || !dst_start || !src_start || !size || |
| 1135 | !buffer_tile_region(src_start, size, buffer_tiles(src), from) || | 1183 | !append_tile_regions(src_start, size, tile_grid(src), from) || |
| 1136 | !buffer_tile_region(dst_start, size, buffer_tiles(dst), to) || | 1184 | !append_tile_regions(dst_start, size, tile_grid(dst), to) || |
| 1137 | !queue_.copy_tile_mappings(resource_id(dst), to.first, resource_id(src), from)) { | 1185 | !queue_.copy_tile_mappings(resource_id(dst), to, resource_id(src), from)) { |
| 1138 | d3d12_trace::record(method, "refused"); | 1186 | d3d12_trace::record(method, "refused"); |
| 1139 | note_once("ID3D12CommandQueue::CopyTileMappings: invalid mapping or buffer sparse residency unavailable"); | 1187 | note_once("ID3D12CommandQueue::CopyTileMappings: invalid mapping or sparse residency unavailable"); |
| 1140 | } else { | 1188 | } else { |
| 1141 | d3d12_trace::record(method, "ok"); | 1189 | d3d12_trace::record(method, "ok"); |
| 1142 | } | 1190 | } |
| @@ -1295,23 +1343,26 @@ private: | |||
| 1295 | queue_.marker(kind, label.c_str()); | 1343 | queue_.marker(kind, label.c_str()); |
| 1296 | } | 1344 | } |
| 1297 | 1345 | ||
| 1298 | bool update_buffer_tiles(ID3D12Resource* resource, UINT region_count, const D3D12_TILED_RESOURCE_COORDINATE* starts, | 1346 | bool update_tiles(ID3D12Resource* resource, UINT region_count, const D3D12_TILED_RESOURCE_COORDINATE* starts, |
| 1299 | const D3D12_TILE_REGION_SIZE* sizes, ID3D12Heap* heap, UINT range_count, | 1347 | const D3D12_TILE_REGION_SIZE* sizes, ID3D12Heap* heap, UINT range_count, |
| 1300 | const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, const UINT* counts, | 1348 | const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, const UINT* counts, |
| 1301 | D3D12_TILE_MAPPING_FLAGS mapping_flags) { | 1349 | D3D12_TILE_MAPPING_FLAGS mapping_flags) { |
| 1302 | if (!region_count || !range_count || (!starts && region_count != 1) || | 1350 | if (!region_count || !range_count || (!starts && region_count != 1) || |
| 1303 | (mapping_flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD)) return false; | 1351 | (mapping_flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD)) return false; |
| 1304 | const auto tiles = buffer_tiles(resource); | 1352 | const auto grid = tile_grid(resource); |
| 1305 | std::vector<leonida::BufferTileRegion> regions(region_count); | 1353 | std::vector<leonida::TileRegion> regions; |
| 1306 | std::uint64_t total = 0; | 1354 | regions.reserve(region_count); |
| 1307 | for (UINT i = 0; i < region_count; ++i) { | 1355 | for (UINT i = 0; i < region_count; ++i) { |
| 1308 | if (!buffer_tile_region(starts ? starts + i : nullptr, sizes ? sizes + i : nullptr, tiles, regions[i])) | 1356 | if (!append_tile_regions(starts ? starts + i : nullptr, sizes ? sizes + i : nullptr, grid, regions)) |
| 1309 | return false; | 1357 | return false; |
| 1310 | if (regions[i].count > UINT64_MAX - total) return false; | 1358 | } |
| 1311 | total += regions[i].count; | 1359 | std::uint64_t total = 0; |
| 1360 | for (const auto& region : regions) { | ||
| 1361 | if (region.count > UINT64_MAX - total) return false; | ||
| 1362 | total += region.count; | ||
| 1312 | } | 1363 | } |
| 1313 | 1364 | ||
| 1314 | std::vector<leonida::BufferTileRange> ranges; | 1365 | std::vector<leonida::TileRange> ranges; |
| 1315 | ranges.reserve(range_count); | 1366 | ranges.reserve(range_count); |
| 1316 | std::uint64_t remaining = total, heap_tiles_required = 0; | 1367 | std::uint64_t remaining = total, heap_tiles_required = 0; |
| 1317 | for (UINT i = 0; i < range_count; ++i) { | 1368 | for (UINT i = 0; i < range_count; ++i) { |
| @@ -1346,7 +1397,9 @@ private: | |||
| 1346 | (!heap || heap_tiles_required > heap->GetDesc().SizeInBytes / leonida::tile_size_in_bytes))) return false; | 1397 | (!heap || heap_tiles_required > heap->GetDesc().SizeInBytes / leonida::tile_size_in_bytes))) return false; |
| 1347 | std::lock_guard lock(mutex_); | 1398 | std::lock_guard lock(mutex_); |
| 1348 | return queue_.update_tiles(resource_id(resource), regions, ranges, | 1399 | return queue_.update_tiles(resource_id(resource), regions, ranges, |
| 1349 | [&] { return heap_tiles_required ? tile_heap(heap, context_) : nullptr; }); | 1400 | [&](bool image, std::uint32_t types) { |
| 1401 | return heap_tiles_required ? tile_heap(heap, context_, image, types) : nullptr; | ||
| 1402 | }); | ||
| 1350 | } | 1403 | } |
| 1351 | 1404 | ||
| 1352 | D3D12_COMMAND_QUEUE_DESC desc_; | 1405 | D3D12_COMMAND_QUEUE_DESC desc_; |