diff options
| author | Rain156 <77776581+Rain156@users.noreply.github.com> | 2026-11-24 21:17:13 +0800 |
|---|---|---|
| committer | Rain156 <77776581+Rain156@users.noreply.github.com> | 2026-11-24 21:17:13 +0800 |
| commit | 65b8569bbce801f5d083fd5c91cd8bc13f825ece (patch) | |
| tree | 5a08b0a63f618312284831c1c13133b452c0942d /runtime/src | |
| parent | abed2e19382dab7e16fbefbd63959644150394b3 (diff) | |
| parent | 77f44fbe3bd477727a1f5ab6d20c6dd5f40e25e8 (diff) | |
Integrate #461 for the Windows first-run UI
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/console_texture_layout.h | 610 |
1 files changed, 610 insertions, 0 deletions
diff --git a/runtime/src/console_texture_layout.h b/runtime/src/console_texture_layout.h new file mode 100644 index 000000000..782d8584a --- /dev/null +++ b/runtime/src/console_texture_layout.h | |||
| @@ -0,0 +1,610 @@ | |||
| 1 | // Texture layouts of the console GPU (RDNA2, GFX10.3): a texture's size and alignment, where each mip and array slice | ||
| 2 | // starts, the address of each element, and copies between tiled memory and linear rows. Used by the xg_xs.dll shim | ||
| 3 | // (runtime/shims/xg_xs.cpp), whose texture computer answers the title's layout and copy calls from it. | ||
| 4 | // | ||
| 5 | // Ported from Marvisak/VIctory (GPL-3.0), crates/victory-formats/src/texture.rs at commit d2a4bae1f, which follows | ||
| 6 | // AMD's address library (addrlib) as shipped in Mesa under src/amd/addrlib: block dimensions, mip placement and mip | ||
| 7 | // tails from gfx10addrlib.cpp and addrlib2.cpp (ComputeSurfaceInfoMacroTiled, ComputeThinBlockDimension, | ||
| 8 | // GetMipTailDim, GetMaxNumMipsInTail, HwlComputeSurfaceInfoLinear, ComputeSurfaceAddrFromCoordMacroTiled), and the | ||
| 9 | // 256-byte micro tile patterns in kMicro from gfx10SwizzlePattern.h (the RB+ tables, as on RDNA2). addrlib carries | ||
| 10 | // this notice: | ||
| 11 | // | ||
| 12 | // Copyright (C) 2007-2026 Advanced Micro Devices, Inc. All rights reserved. | ||
| 13 | // SPDX-License-Identifier: MIT | ||
| 14 | // | ||
| 15 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | ||
| 16 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation | ||
| 17 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | ||
| 18 | // to permit persons to whom the Software is furnished to do so, subject to the following conditions: the above | ||
| 19 | // copyright notice and this permission notice shall be included in all copies or substantial portions of the | ||
| 20 | // Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. | ||
| 21 | // | ||
| 22 | // Units: an element is a texel, or a 4x4 block of a block-compressed format. Mips of one array slice (or one depth | ||
| 23 | // slab of a thick 3D texture) are stored together, smallest first: the mip tail (the small mips sharing one block) | ||
| 24 | // at offset 0, mip 0 last. | ||
| 25 | #pragma once | ||
| 26 | |||
| 27 | #include <algorithm> | ||
| 28 | #include <array> | ||
| 29 | #include <bit> | ||
| 30 | #include <cstddef> | ||
| 31 | #include <cstdint> | ||
| 32 | #include <cstring> | ||
| 33 | #include <optional> | ||
| 34 | #include <vector> | ||
| 35 | |||
| 36 | namespace leonida::console_texture { | ||
| 37 | |||
| 38 | // A swizzle mode, by its hardware number (addrlib's ADDR_SW_* order). | ||
| 39 | enum : uint32_t { | ||
| 40 | kLinear = 0, | ||
| 41 | kSw256bS = 1, | ||
| 42 | kSw256bD = 2, | ||
| 43 | kSw4kbS = 5, | ||
| 44 | kSw4kbD = 6, | ||
| 45 | kSw64kbS = 9, | ||
| 46 | kSw64kbD = 10, | ||
| 47 | kSw64kbST = 17, | ||
| 48 | kSw64kbDT = 18, | ||
| 49 | kSw4kbSX = 21, | ||
| 50 | kSw4kbDX = 22, | ||
| 51 | kSw4kbRX = 23, | ||
| 52 | kSw64kbZX = 24, | ||
| 53 | kSw64kbSX = 25, | ||
| 54 | kSw64kbDX = 26, | ||
| 55 | kSw64kbRX = 27, | ||
| 56 | }; | ||
| 57 | |||
| 58 | // log2 of the block size in bytes (0 for linear); nullopt for modes GFX10 does not have. | ||
| 59 | inline std::optional<uint32_t> block_log2(uint32_t swizzle) { | ||
| 60 | switch (swizzle) { | ||
| 61 | case 0: return 0; | ||
| 62 | case 1: case 2: return 8; | ||
| 63 | case 5: case 6: case 21: case 22: case 23: return 12; | ||
| 64 | case 9: case 10: case 17: case 18: case 24: case 25: case 26: case 27: return 16; | ||
| 65 | default: return std::nullopt; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | inline bool is_standard(uint32_t s) { return s == 1 || s == 5 || s == 9 || s == 17 || s == 21 || s == 25; } | ||
| 69 | inline bool is_display(uint32_t s) { return s == 2 || s == 6 || s == 10 || s == 18 || s == 22 || s == 26; } | ||
| 70 | inline bool is_z_order(uint32_t s) { return s == 24; } | ||
| 71 | // Pipe and bank XOR modes (_T, _X): their addresses depend on the GPU's pipe configuration. | ||
| 72 | inline bool is_xor(uint32_t s) { return s >= 16; } | ||
| 73 | |||
| 74 | enum class Dimension { tex1d, tex2d, tex3d }; | ||
| 75 | |||
| 76 | enum class Error { | ||
| 77 | ok, | ||
| 78 | invalid, // the description makes no texture, or a coordinate or subresource lies outside it | ||
| 79 | unsupported, // a valid layout whose addressing is not implemented here | ||
| 80 | too_small, // a buffer is smaller than the layout or the rows need | ||
| 81 | }; | ||
| 82 | |||
| 83 | // One plane of a texture. | ||
| 84 | struct Desc { | ||
| 85 | Dimension dimension = Dimension::tex2d; | ||
| 86 | uint32_t width = 1, height = 1; // in texels | ||
| 87 | uint32_t depth = 1; // depth of a 3D texture, else the array size | ||
| 88 | uint32_t mips = 1; | ||
| 89 | uint32_t element_bytes = 4; // 1, 2, 4, 8 or 16 | ||
| 90 | uint32_t block = 1; // texels per element side: 1, or 4 for block compression | ||
| 91 | uint32_t samples = 1; | ||
| 92 | uint32_t swizzle = kLinear; | ||
| 93 | }; | ||
| 94 | |||
| 95 | struct Tail { | ||
| 96 | uint32_t offset = 0; // byte offset inside the tail block | ||
| 97 | uint32_t x = 0, y = 0; // element position the mip starts at | ||
| 98 | }; | ||
| 99 | |||
| 100 | // Where one mip lives inside a slice. | ||
| 101 | struct Mip { | ||
| 102 | uint32_t width = 0, height = 0, depth = 0; // size in elements (depth 1 unless the texture is 3D) | ||
| 103 | uint32_t pitch = 0, rows = 0, depth_pitch = 0; // padded size in elements: whole blocks, or the tail's share | ||
| 104 | uint64_t offset = 0; // byte offset of its first block in the slice (0 in the tail) | ||
| 105 | std::optional<Tail> tail; | ||
| 106 | }; | ||
| 107 | |||
| 108 | namespace detail { | ||
| 109 | |||
| 110 | constexpr uint32_t div_ceil(uint32_t a, uint32_t b) { return (a + b - 1) / b; } | ||
| 111 | constexpr uint32_t round_up(uint32_t a, uint32_t b) { return div_ceil(a, b) * b; } | ||
| 112 | |||
| 113 | // Elements along one side of mip `mip`, as data and copies see it: texels halve rounding down (to at least 1), then | ||
| 114 | // round up to whole elements. | ||
| 115 | constexpr uint32_t dim(uint32_t texels, uint32_t mip, uint32_t block) { | ||
| 116 | return div_ceil(std::max(texels >> mip, 1u), block); | ||
| 117 | } | ||
| 118 | // Elements along one side of mip `mip` for placement (padding, tail, offsets): addrlib halves mip 0's elements | ||
| 119 | // rounding up, which can be one more than dim() for odd sizes. | ||
| 120 | constexpr uint32_t padded_dim(uint32_t texels, uint32_t mip, uint32_t block) { | ||
| 121 | return div_ceil(div_ceil(std::max(texels, 1u), block), 1u << mip); | ||
| 122 | } | ||
| 123 | |||
| 124 | // Micro tile (256 bytes) patterns by log2 of the element size: for each address bit, the element coordinate bit it | ||
| 125 | // takes (x0, y1, z0...), "--" for the bits inside an element. Thin S, thin D, thick S (also the D 3D modes). From | ||
| 126 | // addrlib's GFX10_SW_256_S_RBPLUS, GFX10_SW_256_D_RBPLUS and GFX10_SW_4K_S3_RBPLUS patterns; the larger blocks add | ||
| 127 | // bits Y, X (thin) or Y, X, Z (thick) in turn. | ||
| 128 | inline constexpr const char* kMicro[3][5] = { | ||
| 129 | {"x0x1x2x3y0y1y2y3", "--x0x1x2y0y1y2x3", "----x0x1y0y1y2x2", "------x0y0y1x1x2", "--------y0y1x0x1"}, | ||
| 130 | {"x0x1x2y1y0y2x3y3", "--x0x1x2y0y1y2x3", "----x0x1y0y1x2y2", "------x0y0x1x2y1", "--------x0y0x1y1"}, | ||
| 131 | {"x0x1z0y0z1y1x2z2", "--x0z0y0z1y1x1z2", "----x0y0z0y1x1z1", "------x0z0y0x1z1", "--------z0y0x0z1"}, | ||
| 132 | }; | ||
| 133 | |||
| 134 | // For each axis, the address bit each coordinate bit lands on. | ||
| 135 | inline std::array<std::vector<uint32_t>, 3> pattern(int kind, uint32_t element_log2, uint32_t block_log2) { | ||
| 136 | std::array<std::vector<uint32_t>, 3> axes; | ||
| 137 | const char* micro = kMicro[kind][element_log2]; | ||
| 138 | for (uint32_t bit = 0; bit < 8; ++bit) { | ||
| 139 | const char a = micro[2 * bit]; | ||
| 140 | const int axis = a == 'x' ? 0 : a == 'y' ? 1 : a == 'z' ? 2 : -1; | ||
| 141 | if (axis < 0) continue; | ||
| 142 | const size_t index = static_cast<size_t>(micro[2 * bit + 1] - '0'); | ||
| 143 | auto& to = axes[static_cast<size_t>(axis)]; | ||
| 144 | if (to.size() <= index) to.resize(index + 1, 0); | ||
| 145 | to[index] = bit; | ||
| 146 | } | ||
| 147 | static constexpr int kThin[2] = {1, 0}; | ||
| 148 | static constexpr int kThick[3] = {1, 0, 2}; | ||
| 149 | const int* order = kind == 2 ? kThick : kThin; | ||
| 150 | const uint32_t n = kind == 2 ? 3u : 2u; | ||
| 151 | for (uint32_t bit = 8, i = 0; bit < block_log2; ++bit, ++i) axes[static_cast<size_t>(order[i % n])].push_back(bit); | ||
| 152 | return axes; | ||
| 153 | } | ||
| 154 | |||
| 155 | // Byte offsets inside a block for coordinates 0..2^bits of one axis. | ||
| 156 | inline std::vector<uint32_t> spread(const std::vector<uint32_t>& bits) { | ||
| 157 | std::vector<uint32_t> out(size_t{1} << bits.size()); | ||
| 158 | for (uint32_t c = 0; c < out.size(); ++c) { | ||
| 159 | uint32_t v = 0; | ||
| 160 | for (size_t i = 0; i < bits.size(); ++i) v |= ((c >> i) & 1u) << bits[i]; | ||
| 161 | out[c] = v; | ||
| 162 | } | ||
| 163 | return out; | ||
| 164 | } | ||
| 165 | |||
| 166 | } // namespace detail | ||
| 167 | |||
| 168 | constexpr uint32_t kMaxMips = 16; | ||
| 169 | // The largest width, height and depth (or array size) accepted, in texels. GFX10's image descriptors hold at most | ||
| 170 | // 16384 texels per side, so no texture the GPU can use is refused. The bound also keeps every size computed below | ||
| 171 | // within uint64_t (and its uint32_t steps), which unchecked values from a title would otherwise overflow. | ||
| 172 | constexpr uint32_t kMaxExtent = 1u << 14; | ||
| 173 | |||
| 174 | // 256-byte micro tile dimensions by log2 element size, 2D and 3D. | ||
| 175 | inline constexpr uint32_t kBlock256_2d[5][2] = {{16, 16}, {16, 8}, {8, 8}, {8, 4}, {4, 4}}; | ||
| 176 | inline constexpr uint32_t kBlock256_3d[5][3] = {{8, 4, 8}, {4, 4, 8}, {4, 4, 4}, {4, 2, 4}, {2, 2, 4}}; | ||
| 177 | inline constexpr uint32_t kBlock1k_3d[5][3] = {{16, 8, 8}, {8, 8, 8}, {8, 8, 4}, {8, 4, 4}, {4, 4, 4}}; | ||
| 178 | |||
| 179 | class Layout { | ||
| 180 | public: | ||
| 181 | Desc desc; | ||
| 182 | std::array<uint32_t, 3> block{}; // block dimensions in elements (linear: the pitch alignment, 1, 1) | ||
| 183 | bool thick = false; // 3D blocks span block[2] depth slices (GFX10 3D S and D modes) | ||
| 184 | // Array slices, depth slabs of block[2] slices (thick), or depth slices (thin and linear 3D). | ||
| 185 | uint32_t slices = 0; | ||
| 186 | uint64_t slice_size = 0; // bytes of one slice: its whole mip chain | ||
| 187 | uint64_t size = 0; | ||
| 188 | uint64_t alignment = 0; | ||
| 189 | uint32_t first_tail_mip = 0; // the first mip in the tail; mips.size() without one | ||
| 190 | std::vector<Mip> mips; | ||
| 191 | |||
| 192 | static std::optional<Layout> make(const Desc& d) { | ||
| 193 | const auto log2 = block_log2(d.swizzle); | ||
| 194 | if (!log2) return std::nullopt; | ||
| 195 | const auto in = [](uint32_t v, uint32_t lo, uint32_t hi) { return v >= lo && v <= hi; }; | ||
| 196 | const bool valid = | ||
| 197 | in(d.width, 1, kMaxExtent) && in(d.height, 1, kMaxExtent) && in(d.depth, 1, kMaxExtent) && | ||
| 198 | in(d.mips, 1, kMaxMips) && std::has_single_bit(d.element_bytes) && d.element_bytes <= 16 && | ||
| 199 | (d.block == 1 || d.block == 4) && std::has_single_bit(d.samples) && d.samples <= 8 && | ||
| 200 | (d.samples == 1 || (d.dimension == Dimension::tex2d && d.mips == 1)) && | ||
| 201 | (d.dimension != Dimension::tex1d || d.height == 1) && | ||
| 202 | (d.dimension != Dimension::tex3d || d.swizzle == 0 || d.swizzle == 5 || d.swizzle == 9 || | ||
| 203 | d.swizzle == 17 || d.swizzle == 21 || (d.swizzle >= 24 && d.swizzle <= 27)); | ||
| 204 | if (!valid) return std::nullopt; | ||
| 205 | return *log2 == 0 ? linear(d) : tiled(d, *log2); | ||
| 206 | } | ||
| 207 | |||
| 208 | // Whether offset() and the copies work for this layout: linear and the modes without pipe or bank XOR, | ||
| 209 | // single-sampled. The XOR modes (_X, _T) and MSAA need the GPU's pipe and bank configuration (Series X and S | ||
| 210 | // differ); titles use them for render targets and depth, which the host renders. | ||
| 211 | bool addressable() const { return desc.swizzle == kLinear || !pattern_[0].empty(); } | ||
| 212 | |||
| 213 | // Depth slices a subresource of mip `mip` covers: the mip's depth for 3D textures, else 1 (one array slice). | ||
| 214 | uint32_t subresource_depth(uint32_t mip) const { return mip < mips.size() ? mips[mip].depth : 0; } | ||
| 215 | |||
| 216 | // Byte offset of element (x, y) of mip `mip` in array slice or depth slice `z`. Coordinates must lie inside. | ||
| 217 | Error offset(uint32_t mip, uint32_t x, uint32_t y, uint32_t z, uint64_t& out) const { | ||
| 218 | if (mip >= mips.size()) return Error::invalid; | ||
| 219 | const Mip& m = mips[mip]; | ||
| 220 | const uint32_t depth = desc.dimension == Dimension::tex3d ? m.depth : slices; | ||
| 221 | const bool inside = x < m.width && y < m.height && z < depth; | ||
| 222 | if (!inside) return Error::invalid; | ||
| 223 | if (!addressable()) return Error::unsupported; | ||
| 224 | const auto [slice, inner] = split(z); | ||
| 225 | const uint64_t base = uint64_t{slice} * slice_size + m.offset; | ||
| 226 | if (desc.swizzle == kLinear) { | ||
| 227 | out = base + (uint64_t{y} * m.pitch + x) * desc.element_bytes; | ||
| 228 | return Error::ok; | ||
| 229 | } | ||
| 230 | out = base + row(m, y, inner) + column(m, x); | ||
| 231 | return Error::ok; | ||
| 232 | } | ||
| 233 | |||
| 234 | // Copies one subresource out of tiled memory (the whole texture, `size` bytes) into linear rows: mip `mip` of | ||
| 235 | // array slice `slice` (3D: every depth slice of the mip, slice 0). Rows are row_pitch bytes apart, depth slices | ||
| 236 | // depth_pitch. | ||
| 237 | Error detile(const uint8_t* tiled, size_t tiled_len, uint32_t mip, uint32_t slice, uint8_t* linear, | ||
| 238 | size_t linear_len, size_t row_pitch, size_t depth_pitch) const { | ||
| 239 | return walk(tiled_len, mip, slice, linear_len, row_pitch, depth_pitch, [&](size_t t, size_t l, size_t n) { | ||
| 240 | std::memcpy(linear + l, tiled + t, n); | ||
| 241 | }); | ||
| 242 | } | ||
| 243 | // The reverse of detile(): linear rows into tiled memory. | ||
| 244 | Error tile(uint8_t* tiled, size_t tiled_len, uint32_t mip, uint32_t slice, const uint8_t* linear, | ||
| 245 | size_t linear_len, size_t row_pitch, size_t depth_pitch) const { | ||
| 246 | return walk(tiled_len, mip, slice, linear_len, row_pitch, depth_pitch, [&](size_t t, size_t l, size_t n) { | ||
| 247 | std::memcpy(tiled + t, linear + l, n); | ||
| 248 | }); | ||
| 249 | } | ||
| 250 | |||
| 251 | // Byte offsets inside a block of each element coordinate bit, per axis; empty for linear and XOR/MSAA modes. | ||
| 252 | const std::array<std::vector<uint32_t>, 3>& pattern() const { return pattern_; } | ||
| 253 | |||
| 254 | private: | ||
| 255 | std::array<std::vector<uint32_t>, 3> pattern_; | ||
| 256 | |||
| 257 | // HwlComputeSurfaceInfoLinear: rows aligned to 256 bytes, mips packed smallest first, a 3D texture's depth slices | ||
| 258 | // each hold the chain. | ||
| 259 | static Layout linear(const Desc& d) { | ||
| 260 | using namespace detail; | ||
| 261 | Layout l; | ||
| 262 | l.desc = d; | ||
| 263 | const uint64_t bpe = d.element_bytes; | ||
| 264 | const uint32_t align = 256 / d.element_bytes; | ||
| 265 | const bool three_d = d.dimension == Dimension::tex3d; | ||
| 266 | uint64_t offset = 0; | ||
| 267 | for (uint32_t i = d.mips; i-- > 0;) { | ||
| 268 | const uint32_t h = padded_dim(d.height, i, d.block); | ||
| 269 | const uint32_t pitch = round_up(padded_dim(d.width, i, d.block), align); | ||
| 270 | Mip m; | ||
| 271 | m.width = dim(d.width, i, d.block); | ||
| 272 | m.height = dim(d.height, i, d.block); | ||
| 273 | m.depth = three_d ? dim(d.depth, i, 1) : 1; | ||
| 274 | m.pitch = pitch; | ||
| 275 | m.rows = h; | ||
| 276 | m.depth_pitch = three_d ? d.depth : 1; | ||
| 277 | m.offset = offset; | ||
| 278 | l.mips.push_back(m); | ||
| 279 | offset += uint64_t{pitch} * h * bpe; | ||
| 280 | } | ||
| 281 | std::reverse(l.mips.begin(), l.mips.end()); | ||
| 282 | l.block = {align, 1, 1}; | ||
| 283 | l.slices = d.depth; | ||
| 284 | l.slice_size = offset; | ||
| 285 | l.size = offset * d.depth; | ||
| 286 | l.alignment = 256; | ||
| 287 | l.first_tail_mip = d.mips; | ||
| 288 | return l; | ||
| 289 | } | ||
| 290 | |||
| 291 | static Layout tiled(const Desc& d, uint32_t block_log2) { | ||
| 292 | using namespace detail; | ||
| 293 | Layout l; | ||
| 294 | l.desc = d; | ||
| 295 | const uint32_t element_log2 = static_cast<uint32_t>(std::countr_zero(d.element_bytes)); | ||
| 296 | const uint64_t bpe = d.element_bytes; | ||
| 297 | const uint64_t block_bytes = uint64_t{1} << block_log2; | ||
| 298 | const bool thick = d.dimension == Dimension::tex3d && (is_standard(d.swizzle) || is_display(d.swizzle)); | ||
| 299 | std::array<uint32_t, 3> block{}; | ||
| 300 | if (thick) { // ComputeThickBlockDimension | ||
| 301 | const uint32_t amp = (block_log2 - 10) / 3, rest = (block_log2 - 10) % 3; | ||
| 302 | const auto* b = kBlock1k_3d[element_log2]; | ||
| 303 | block = {b[0] << amp, b[1] << (amp + rest / 2), b[2] << (amp + (rest != 0 ? 1u : 0u))}; | ||
| 304 | } else { // ComputeThinBlockDimension | ||
| 305 | const uint32_t samples_log2 = static_cast<uint32_t>(std::countr_zero(d.samples)); | ||
| 306 | const uint32_t elements_log2 = block_log2 - element_log2 - samples_log2; | ||
| 307 | const bool wide = (samples_log2 & 1) == 0 || (block_log2 & 1) != 0; | ||
| 308 | const uint32_t w = (elements_log2 + (wide ? 1u : 0u)) / 2; | ||
| 309 | block = {1u << w, 1u << (elements_log2 - w), 1}; | ||
| 310 | } | ||
| 311 | uint32_t slices = round_up(d.depth, block[2]); | ||
| 312 | const bool three_d = d.dimension == Dimension::tex3d; | ||
| 313 | // Mip depths placed by a thick layout (thin ones store depth as slices). | ||
| 314 | const auto mip_depth = [&](uint32_t i) { return thick ? padded_dim(d.depth, i, 1) : 1u; }; | ||
| 315 | const auto depth_of = [&](uint32_t i) { return three_d ? dim(d.depth, i, 1) : 1u; }; | ||
| 316 | const auto width = [&](uint32_t i) { return dim(d.width, i, d.block); }; | ||
| 317 | const auto height = [&](uint32_t i) { return dim(d.height, i, d.block); }; | ||
| 318 | uint32_t first_tail_mip = d.mips; | ||
| 319 | uint64_t slice_size = 0; | ||
| 320 | if (d.mips == 1) { | ||
| 321 | Mip m; | ||
| 322 | m.width = width(0); | ||
| 323 | m.height = height(0); | ||
| 324 | m.depth = depth_of(0); | ||
| 325 | m.pitch = round_up(width(0), block[0]); | ||
| 326 | m.rows = round_up(height(0), block[1]); | ||
| 327 | m.depth_pitch = three_d ? slices : 1; | ||
| 328 | l.mips.push_back(m); | ||
| 329 | slice_size = uint64_t{m.pitch} * m.rows * bpe * d.samples; | ||
| 330 | } else { | ||
| 331 | // GetMipTailDim: the largest mip the tail takes. | ||
| 332 | std::array<uint32_t, 3> tail_max = block; | ||
| 333 | if (thick) { | ||
| 334 | switch (block_log2 % 3) { | ||
| 335 | case 0: tail_max[1] >>= 1; break; | ||
| 336 | case 1: tail_max[0] >>= 1; break; | ||
| 337 | default: tail_max[2] >>= 1; break; | ||
| 338 | } | ||
| 339 | } else { | ||
| 340 | tail_max[0] >>= 1; | ||
| 341 | } | ||
| 342 | // GetMaxNumMipsInTail; 256-byte blocks have no tail (ComputeSurfaceInfoMicroTiled). | ||
| 343 | const uint32_t effective = thick ? block_log2 - (block_log2 - 8) / 3 : block_log2; | ||
| 344 | const uint32_t max_in_tail = effective == 8 ? 0 | ||
| 345 | : (effective >= 9 && effective <= 11) ? 1 + (1u << (effective - 9)) | ||
| 346 | : effective - 4; | ||
| 347 | // dsMipmapHtileFix: 8- and 16-bit depth enter the tail as 32-bit. | ||
| 348 | std::array<uint32_t, 3> fixed = tail_max; | ||
| 349 | if (is_z_order(d.swizzle) && element_log2 <= 1) { | ||
| 350 | const auto* b = kBlock256_2d[element_log2]; | ||
| 351 | fixed[0] /= b[0] / kBlock256_2d[2][0]; | ||
| 352 | fixed[1] /= b[1] / kBlock256_2d[2][1]; | ||
| 353 | } | ||
| 354 | std::vector<uint64_t> sizes; | ||
| 355 | for (uint32_t i = 0; i < d.mips; ++i) { | ||
| 356 | const uint32_t w = padded_dim(d.width, i, d.block), h = padded_dim(d.height, i, d.block); | ||
| 357 | if (w <= fixed[0] && h <= fixed[1] && d.mips - i <= max_in_tail) { | ||
| 358 | first_tail_mip = i; | ||
| 359 | slice_size += block_bytes / block[2]; | ||
| 360 | break; | ||
| 361 | } | ||
| 362 | const uint32_t pitch = round_up(w, block[0]), rows = round_up(h, block[1]); | ||
| 363 | const uint32_t depth = round_up(mip_depth(i), block[2]); | ||
| 364 | const uint64_t one = uint64_t{pitch} * rows * bpe; | ||
| 365 | slice_size += one; | ||
| 366 | sizes.push_back(one * block[2]); | ||
| 367 | Mip m; | ||
| 368 | m.width = width(i); | ||
| 369 | m.height = height(i); | ||
| 370 | m.depth = depth_of(i); | ||
| 371 | m.pitch = pitch; | ||
| 372 | m.rows = rows; | ||
| 373 | m.depth_pitch = three_d ? depth : 1; | ||
| 374 | l.mips.push_back(m); | ||
| 375 | } | ||
| 376 | // Standard mips from the smallest up, after the tail block. | ||
| 377 | uint64_t offset = first_tail_mip < d.mips ? block_bytes : 0; | ||
| 378 | for (uint32_t i = first_tail_mip; i-- > 0;) { | ||
| 379 | l.mips[i].offset = offset; | ||
| 380 | offset += sizes[i]; | ||
| 381 | } | ||
| 382 | uint32_t micro[2]; | ||
| 383 | if (thick) { | ||
| 384 | micro[0] = kBlock256_3d[element_log2][0]; | ||
| 385 | micro[1] = kBlock256_3d[element_log2][1]; | ||
| 386 | } else { | ||
| 387 | micro[0] = kBlock256_2d[element_log2][0]; | ||
| 388 | micro[1] = kBlock256_2d[element_log2][1]; | ||
| 389 | } | ||
| 390 | uint32_t pitch = tail_max[0], rows = tail_max[1]; | ||
| 391 | for (uint32_t i = first_tail_mip; i < d.mips; ++i) { | ||
| 392 | const uint32_t m = max_in_tail - 1 - (i - first_tail_mip); | ||
| 393 | const uint32_t offset_in_tail = m > 6 ? 16u << m : m << 8; | ||
| 394 | // Address bits 8.. of the offset, odd ones x, even ones y. | ||
| 395 | const auto bits = [&](uint32_t from) { | ||
| 396 | uint32_t v = 0; | ||
| 397 | for (uint32_t b = 0; b < 6; ++b) v |= ((offset_in_tail >> (from + 2 * b)) & 1u) << b; | ||
| 398 | return v; | ||
| 399 | }; | ||
| 400 | uint32_t x = bits(9), y = bits(8); | ||
| 401 | if ((block_log2 & 1) != 0) { | ||
| 402 | std::swap(x, y); | ||
| 403 | if ((element_log2 & 1) != 0) { | ||
| 404 | y = (y << 1) | (x & 1); | ||
| 405 | x >>= 1; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | Mip t; | ||
| 409 | t.width = width(i); | ||
| 410 | t.height = height(i); | ||
| 411 | t.depth = depth_of(i); | ||
| 412 | t.pitch = pitch; | ||
| 413 | t.rows = rows; | ||
| 414 | t.depth_pitch = thick ? round_up(mip_depth(i), kBlock256_3d[element_log2][2]) : 1; | ||
| 415 | t.offset = 0; | ||
| 416 | t.tail = Tail{offset_in_tail, x * micro[0], y * micro[1]}; | ||
| 417 | l.mips.push_back(t); | ||
| 418 | pitch = std::max(pitch >> 1, micro[0]); | ||
| 419 | rows = std::max(rows >> 1, micro[1]); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | // Thick layouts store each depth slab's chain together. | ||
| 423 | const uint64_t slab = thick ? block[2] : 1; | ||
| 424 | slice_size *= slab; | ||
| 425 | slices /= block[2]; | ||
| 426 | if (!is_xor(d.swizzle) && d.samples == 1) { | ||
| 427 | const int kind = thick ? 2 : (is_display(d.swizzle) ? 1 : 0); | ||
| 428 | const auto bits = detail::pattern(kind, element_log2, block_log2); | ||
| 429 | for (size_t a = 0; a < 3; ++a) l.pattern_[a] = detail::spread(bits[a]); | ||
| 430 | } | ||
| 431 | l.block = block; | ||
| 432 | l.thick = thick; | ||
| 433 | l.slices = slices; | ||
| 434 | l.slice_size = slice_size; | ||
| 435 | l.size = slice_size * slices; | ||
| 436 | l.alignment = block_bytes; | ||
| 437 | l.first_tail_mip = first_tail_mip; | ||
| 438 | return l; | ||
| 439 | } | ||
| 440 | |||
| 441 | // The slice and the depth inside it for z (an array slice or a 3D depth slice). | ||
| 442 | std::pair<uint32_t, uint32_t> split(uint32_t z) const { | ||
| 443 | return thick ? std::pair{z / block[2], z % block[2]} : std::pair{z, 0u}; | ||
| 444 | } | ||
| 445 | |||
| 446 | // The part of an address that depends on the row (and depth in a thick block). | ||
| 447 | uint64_t row(const Mip& m, uint32_t y, uint32_t z) const { | ||
| 448 | const uint32_t bw = block[0], bh = block[1]; | ||
| 449 | const int log2 = std::countr_zero(alignment); | ||
| 450 | // In the tail the pattern of the shifted position gives the mip's offset inside the tail block. | ||
| 451 | y += m.tail ? m.tail->y : 0; | ||
| 452 | const uint64_t blocks = uint64_t{y / bh} * (m.pitch / bw); | ||
| 453 | return (blocks << log2) + (pattern_[1][y % bh] | pattern_[2][z]); | ||
| 454 | } | ||
| 455 | |||
| 456 | // The part of an address that depends on the column. | ||
| 457 | uint64_t column(const Mip& m, uint32_t x) const { | ||
| 458 | const uint32_t bw = block[0]; | ||
| 459 | x += m.tail ? m.tail->x : 0; | ||
| 460 | return (uint64_t{x / bw} << std::countr_zero(alignment)) + pattern_[0][x % bw]; | ||
| 461 | } | ||
| 462 | |||
| 463 | // Visits contiguous runs as (tiled byte offset, linear byte offset, length). | ||
| 464 | template <class Copy> | ||
| 465 | Error walk(size_t tiled_len, uint32_t mip, uint32_t slice, size_t linear_len, size_t row_pitch, | ||
| 466 | size_t depth_pitch, Copy&& copy) const { | ||
| 467 | if (mip >= mips.size()) return Error::invalid; | ||
| 468 | const Mip& m = mips[mip]; | ||
| 469 | const uint32_t depth = m.depth; | ||
| 470 | uint32_t first = slice; | ||
| 471 | if (desc.dimension == Dimension::tex3d) { | ||
| 472 | if (slice != 0) return Error::invalid; | ||
| 473 | first = 0; | ||
| 474 | } | ||
| 475 | if (first >= slices) return Error::invalid; | ||
| 476 | if (!addressable()) return Error::unsupported; | ||
| 477 | const size_t n = desc.element_bytes; | ||
| 478 | const size_t bytes = size_t{m.width} * n; | ||
| 479 | // need = (depth - 1) * depth_pitch + (height - 1) * row_pitch + bytes, refused on overflow. | ||
| 480 | const size_t dz = depth - 1u, dy = m.height - 1u; | ||
| 481 | if ((dz && depth_pitch > SIZE_MAX / dz) || (dy && row_pitch > SIZE_MAX / dy)) return Error::too_small; | ||
| 482 | const size_t depth_offset = dz * depth_pitch, row_offset = dy * row_pitch; | ||
| 483 | if (depth_offset > SIZE_MAX - row_offset || depth_offset + row_offset > SIZE_MAX - bytes) | ||
| 484 | return Error::too_small; | ||
| 485 | const size_t need = depth_offset + row_offset + bytes; | ||
| 486 | if (tiled_len < size || linear_len < need || row_pitch < bytes) return Error::too_small; | ||
| 487 | const bool is_linear = desc.swizzle == kLinear; | ||
| 488 | // The row and depth contribute one common base to every column, so the same contiguous runs repeat on | ||
| 489 | // every row, including mip tails. | ||
| 490 | struct Run { | ||
| 491 | size_t tiled, linear, len; | ||
| 492 | }; | ||
| 493 | std::vector<Run> runs; | ||
| 494 | if (!is_linear) { | ||
| 495 | size_t first_linear = 0, first_tiled = static_cast<size_t>(column(m, 0)); | ||
| 496 | for (uint32_t x = 1; x < m.width; ++x) { | ||
| 497 | const size_t off = static_cast<size_t>(column(m, x)); | ||
| 498 | const size_t at = x * n; | ||
| 499 | if (off != first_tiled + (at - first_linear)) { | ||
| 500 | runs.push_back({first_tiled, first_linear, at - first_linear}); | ||
| 501 | first_tiled = off; | ||
| 502 | first_linear = at; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | runs.push_back({first_tiled, first_linear, bytes - first_linear}); | ||
| 506 | } | ||
| 507 | for (uint32_t z = 0; z < depth; ++z) { | ||
| 508 | const auto [s, inner] = split(first + z); | ||
| 509 | const uint64_t base = uint64_t{s} * slice_size + m.offset; | ||
| 510 | for (uint32_t y = 0; y < m.height; ++y) { | ||
| 511 | const size_t linear_row = z * depth_pitch + y * row_pitch; | ||
| 512 | if (is_linear) { | ||
| 513 | copy(static_cast<size_t>(base + uint64_t{y} * m.pitch * n), linear_row, bytes); | ||
| 514 | } else { | ||
| 515 | const size_t tiled_row = static_cast<size_t>(base + row(m, y, inner)); | ||
| 516 | for (const Run& r : runs) copy(tiled_row + r.tiled, linear_row + r.linear, r.len); | ||
| 517 | } | ||
| 518 | } | ||
| 519 | } | ||
| 520 | return Error::ok; | ||
| 521 | } | ||
| 522 | }; | ||
| 523 | |||
| 524 | // What a DXGI format stores per element: bytes, texels per element side (4 for block compression), and whether its | ||
| 525 | // stencil is a second plane (the GPU keeps depth and stencil apart). nullopt for 96-bit, 1-bit, packed 4:2:2 and | ||
| 526 | // video formats, which have no layout here. | ||
| 527 | struct Format { | ||
| 528 | uint32_t bytes, block; | ||
| 529 | bool stencil; | ||
| 530 | }; | ||
| 531 | inline std::optional<Format> format(uint32_t dxgi) { | ||
| 532 | const auto in = [&](uint32_t lo, uint32_t hi) { return dxgi >= lo && dxgi <= hi; }; | ||
| 533 | if (in(1, 4)) return Format{16, 1, false}; | ||
| 534 | if (in(9, 18)) return Format{8, 1, false}; | ||
| 535 | if (in(19, 22) || in(44, 47)) return Format{4, 1, true}; | ||
| 536 | if (in(23, 43) || dxgi == 67 || in(87, 93) || dxgi == 116 || dxgi == 117) return Format{4, 1, false}; | ||
| 537 | if (in(48, 59) || dxgi == 85 || dxgi == 86 || dxgi == 115) return Format{2, 1, false}; | ||
| 538 | if (in(118, 120)) return Format{2, 1, true}; | ||
| 539 | if (in(60, 65)) return Format{1, 1, false}; | ||
| 540 | if (in(70, 72) || in(79, 81)) return Format{8, 4, false}; | ||
| 541 | if (in(73, 78) || in(82, 84) || in(94, 99)) return Format{16, 4, false}; | ||
| 542 | return std::nullopt; | ||
| 543 | } | ||
| 544 | |||
| 545 | // A texture as the console allocates it: a D3D12-style description with a swizzle mode. | ||
| 546 | struct TextureDesc { | ||
| 547 | Dimension dimension = Dimension::tex2d; | ||
| 548 | uint32_t width = 1, height = 1; // in texels | ||
| 549 | uint32_t depth_or_array = 1; // depth of a 3D texture, else the array size | ||
| 550 | uint32_t mips = 1; // 0: the full chain | ||
| 551 | uint32_t format = 0; // DXGI format number | ||
| 552 | uint32_t samples = 1; | ||
| 553 | uint32_t swizzle = kLinear; | ||
| 554 | }; | ||
| 555 | |||
| 556 | struct Plane { | ||
| 557 | uint64_t offset = 0; // from the start of the texture | ||
| 558 | bool stencil = false; | ||
| 559 | Layout layout; | ||
| 560 | }; | ||
| 561 | |||
| 562 | // Every data plane of a texture (depth, then stencil), placed one after the other. No compression metadata planes | ||
| 563 | // (HTILE, CMASK, FMASK, DCC): the console adds them to render targets and depth buffers unless the description | ||
| 564 | // denies them, so those are smaller here than on the console; the host GPU renders them, so nothing reads that | ||
| 565 | // memory. | ||
| 566 | struct Texture { | ||
| 567 | std::vector<Plane> planes; | ||
| 568 | uint64_t size = 0; | ||
| 569 | uint64_t alignment = 0; | ||
| 570 | |||
| 571 | static std::optional<Texture> make(const TextureDesc& desc) { | ||
| 572 | const auto f = format(desc.format); | ||
| 573 | if (!f) return std::nullopt; | ||
| 574 | const uint32_t depth3d = desc.dimension == Dimension::tex3d ? desc.depth_or_array : 1; | ||
| 575 | const uint32_t mips = | ||
| 576 | desc.mips ? desc.mips | ||
| 577 | : 32u - static_cast<uint32_t>(std::countl_zero( | ||
| 578 | std::max({desc.width, desc.height, depth3d, 1u}))); | ||
| 579 | const auto plane = [&](uint32_t bytes) { | ||
| 580 | Desc d; | ||
| 581 | d.dimension = desc.dimension; | ||
| 582 | d.width = desc.width; | ||
| 583 | d.height = desc.height; | ||
| 584 | d.depth = desc.depth_or_array; | ||
| 585 | d.mips = mips; | ||
| 586 | d.element_bytes = bytes; | ||
| 587 | d.block = f->block; | ||
| 588 | d.samples = std::max(desc.samples, 1u); | ||
| 589 | d.swizzle = desc.swizzle; | ||
| 590 | return d; | ||
| 591 | }; | ||
| 592 | Texture t; | ||
| 593 | auto main = Layout::make(plane(f->bytes)); | ||
| 594 | if (!main) return std::nullopt; | ||
| 595 | t.planes.push_back({0, false, std::move(*main)}); | ||
| 596 | if (f->stencil) { | ||
| 597 | auto s = Layout::make(plane(1)); | ||
| 598 | if (!s) return std::nullopt; | ||
| 599 | const uint64_t a = s->alignment; | ||
| 600 | const uint64_t offset = (t.planes[0].layout.size + a - 1) / a * a; | ||
| 601 | t.planes.push_back({offset, true, std::move(*s)}); | ||
| 602 | } | ||
| 603 | const Plane& last = t.planes.back(); | ||
| 604 | t.size = last.offset + last.layout.size; | ||
| 605 | for (const Plane& p : t.planes) t.alignment = std::max(t.alignment, p.layout.alignment); | ||
| 606 | return t; | ||
| 607 | } | ||
| 608 | }; | ||
| 609 | |||
| 610 | } // namespace leonida::console_texture | ||