upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorPersudes <360035573+Persudes@users.noreply.github.com>2026-11-23 14:22:02 +0200
committerubiquitarianism <283527159+ubiquitarianism@users.noreply.github.com>2026-11-23 19:42:49 -0500
commite7f17716f1b3a02018cd4f0e03591d601eb9c068 (patch)
tree1b899c7be6c2860527f3c33b71f81d3c13b5d8de /runtime/src
parent0b589ea999bde037c6c9a6f8aaa272a906216334 (diff)
fix(runtime): validate TiledKind and D3D12 bounds in resource_tiling
(cherry picked from commit b07c4747f16af7cebd50d9d8a93f952fde560784)
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/resource_tiling.h11
1 files changed, 8 insertions, 3 deletions
diff --git a/runtime/src/resource_tiling.h b/runtime/src/resource_tiling.h
index 14a79b911..99302f2b7 100644
--- a/runtime/src/resource_tiling.h
+++ b/runtime/src/resource_tiling.h
@@ -107,11 +107,14 @@ constexpr TileShape small_tile_shape(bool volume, std::uint32_t element_bytes) {
107 } 107 }
108} 108}
109 109
110// False when the resource cannot be tiled: an untileable element size, no mips, or more than 2^32 tiles. 110// False when the resource cannot be tiled: an unknown kind, an untileable element size, out-of-range mips/layers/extents, or more than 2^32 tiles.
111inline bool resource_tiling(const TilingDesc& desc, ResourceTiling& out) { 111inline bool resource_tiling(const TilingDesc& desc, ResourceTiling& out) {
112 out = {}; 112 out = {};
113 constexpr std::uint64_t max_tiles = 0xffffffffu; 113 constexpr std::uint64_t max_tiles = 0xffffffffu;
114 constexpr std::uint64_t max_packed_bytes = max_tiles * tile_size_in_bytes; 114 constexpr std::uint64_t max_packed_bytes = max_tiles * tile_size_in_bytes;
115 constexpr std::uint32_t max_mips = 0xffu; // D3D12_PACKED_MIP_INFO mip counters are UINT8
116 constexpr std::uint32_t max_depth_or_layers = 0xffffu; // D3D12_RESOURCE_DESC::DepthOrArraySize is UINT16
117 constexpr std::uint64_t max_subresource_extent_16 = 0xffffu; // D3D12_SUBRESOURCE_TILING HeightInTiles / DepthInTiles are UINT16
115 const auto up = [](std::uint64_t value, std::uint64_t by) -> std::uint64_t { 118 const auto up = [](std::uint64_t value, std::uint64_t by) -> std::uint64_t {
116 return value ? 1 + (value - 1) / by : 0; 119 return value ? 1 + (value - 1) / by : 0;
117 }; 120 };
@@ -123,10 +126,12 @@ inline bool resource_tiling(const TilingDesc& desc, ResourceTiling& out) {
123 out.subresources = {{out.total_tiles, 1, 1, 0}}; 126 out.subresources = {{out.total_tiles, 1, 1, 0}};
124 return true; 127 return true;
125 } 128 }
129 if (desc.kind != TiledKind::texture2d && desc.kind != TiledKind::texture3d) return false;
126 const bool volume = desc.kind == TiledKind::texture3d; 130 const bool volume = desc.kind == TiledKind::texture3d;
127 if (desc.samples != 1 && (desc.samples != 4 || volume || desc.mips != 1 || desc.block != 1)) return false; 131 if (desc.samples != 1 && (desc.samples != 4 || volume || desc.mips != 1 || desc.block != 1)) return false;
128 const TileShape elements = element_tile_shape(volume, desc.element_bytes, desc.samples); 132 const TileShape elements = element_tile_shape(volume, desc.element_bytes, desc.samples);
129 if (!elements.width || !desc.mips || !desc.block || !desc.width || !desc.height || !desc.depth_or_layers) 133 if (!elements.width || !desc.mips || desc.mips > max_mips || !desc.block || !desc.width || !desc.height ||
134 !desc.depth_or_layers || desc.depth_or_layers > max_depth_or_layers)
130 return false; 135 return false;
131 const std::uint64_t shape_w = std::uint64_t(elements.width) * desc.block; 136 const std::uint64_t shape_w = std::uint64_t(elements.width) * desc.block;
132 const std::uint64_t shape_h = std::uint64_t(elements.height) * desc.block; 137 const std::uint64_t shape_h = std::uint64_t(elements.height) * desc.block;
@@ -149,7 +154,7 @@ inline bool resource_tiling(const TilingDesc& desc, ResourceTiling& out) {
149 const std::uint64_t tw = up(w, result.shape.width); 154 const std::uint64_t tw = up(w, result.shape.width);
150 const std::uint64_t th = up(h, result.shape.height); 155 const std::uint64_t th = up(h, result.shape.height);
151 const std::uint64_t td = up(d, result.shape.depth); 156 const std::uint64_t td = up(d, result.shape.depth);
152 if (tw > max_tiles || th > max_tiles || td > max_tiles) return false; 157 if (tw > max_tiles || th > max_subresource_extent_16 || td > max_subresource_extent_16) return false;
153 const std::uint64_t xy = tw * th; 158 const std::uint64_t xy = tw * th;
154 if (xy > max_tiles || xy > max_tiles / td) return false; 159 if (xy > max_tiles || xy > max_tiles / td) return false;
155 const std::uint64_t mip_tiles = xy * td; 160 const std::uint64_t mip_tiles = xy * td;