diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 17:09:46 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 17:09:46 -0500 |
| commit | cc1f4d35975eec3d3cea8f852a103502b9a97093 (patch) | |
| tree | 197b916135bc6b6bf20fa2dae5b9b4ca4432953d /runtime/src | |
| parent | 01bc91eb2c599dfa4415a5cacc0913462802e53d (diff) | |
| parent | 16586f723968dfddbc2e478b933ec2819afe0c33 (diff) | |
Merge pull request #230 from Jan Trencansky
# Conflicts:
# docs/d3d12_vulkan.md
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/gfx10_descriptors.h | 629 | ||||
| -rw-r--r-- | runtime/src/vulkan_bindless.h | 579 |
2 files changed, 1208 insertions, 0 deletions
diff --git a/runtime/src/gfx10_descriptors.h b/runtime/src/gfx10_descriptors.h new file mode 100644 index 000000000..31f549313 --- /dev/null +++ b/runtime/src/gfx10_descriptors.h | |||
| @@ -0,0 +1,629 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // GFX10 resource descriptors: the T# (image, 8 dwords), S# (sampler, 4 dwords) and V# (buffer, 4 dwords) the | ||
| 4 | // console's shaders load from descriptor heaps. Translated shaders hash a T# or S# to find its bindless index and | ||
| 5 | // dereference a V# (docs/d3d12_vulkan.md sections 3.4, 5.1 and 5.3). This header writes them for Create*View and | ||
| 6 | // CreateSampler, reads them back when a heap is scanned, and maps what they say to Vulkan. It needs the Vulkan | ||
| 7 | // headers only. | ||
| 8 | // | ||
| 9 | // Where the layout comes from: | ||
| 10 | // - Field positions: Mesa's register database, src/amd/registers/gfx10-rsrc.json, the SQ_IMG_RSRC_WORD0..7, | ||
| 11 | // SQ_IMG_SAMP_WORD0..3 and SQ_BUF_RSRC_WORD0..3 types with their gfx103 variants (RDNA2). The comments below | ||
| 12 | // name those fields. | ||
| 13 | // - Enum values in the fields: Mesa's gfx6.json (SQ_RSRC_IMG_TYPE, SQ_SEL_XYZW01, SQ_TEX_CLAMP, SQ_TEX_XY_FILTER, | ||
| 14 | // SQ_TEX_MIP_FILTER, SQ_TEX_DEPTH_COMPARE, SQ_TEX_BORDER_COLOR, SQ_IMG_FILTER_TYPE). | ||
| 15 | // - Which value goes into which field: Mesa's src/amd/common/ac_descriptors.c (ac_build_gfx10_texture_descriptor, | ||
| 16 | // ac_build_sampler_descriptor, ac_build_buffer_descriptor) and radv's radv_image_view.c and radv_sampler.c. | ||
| 17 | // - Formats: the GFX10_FORMAT enum, which tools/gfx10_formats.json holds too. A format names its channel sizes | ||
| 18 | // from the top bit down, and says nothing about which channel is red (src/amd/common/gfx10_format_table.py); | ||
| 19 | // that is DST_SEL's job. | ||
| 20 | // Mesa is MIT-licensed; only positions and values are taken from it. What this repository measured agrees with | ||
| 21 | // it: tools/rdna2spirv.py reads the T# FORMAT at word 1 bits 28:20, the V# base as word 0 | (word 1 & 0xFFFF) | ||
| 22 | // << 32, its stride at word 1 bits 29:16, NUM_RECORDS in word 2 and FORMAT and DST_SEL at word 3 bits 18:12 and | ||
| 23 | // 11:0, and runtime/test/shaders/cs_imgstore.asm builds T#s with word 1 = FORMAT << 20. | ||
| 24 | // | ||
| 25 | // Only what a Vulkan view or sampler needs is encoded. Every other bit (tiling mode, compression metadata, | ||
| 26 | // performance hints, RESOURCE_LEVEL) is written as zero and ignored when decoding: no hardware reads these | ||
| 27 | // descriptors, and what the console's driver puts in those bits is not known. | ||
| 28 | |||
| 29 | #include <vulkan/vulkan.h> | ||
| 30 | |||
| 31 | #include <algorithm> | ||
| 32 | #include <array> | ||
| 33 | #include <bit> | ||
| 34 | #include <cstdint> | ||
| 35 | |||
| 36 | namespace leonida::gfx10 { | ||
| 37 | |||
| 38 | using ImageWords = std::array<std::uint32_t, 8>; // a T# | ||
| 39 | using SamplerWords = std::array<std::uint32_t, 4>; // an S# | ||
| 40 | using BufferWords = std::array<std::uint32_t, 4>; // a V# | ||
| 41 | using Swizzle = std::array<std::uint8_t, 4>; // DST_SEL_X..W | ||
| 42 | |||
| 43 | namespace detail { | ||
| 44 | |||
| 45 | constexpr std::uint32_t field(std::uint32_t value, unsigned low, unsigned width) { | ||
| 46 | return (value & ((1u << width) - 1)) << low; | ||
| 47 | } | ||
| 48 | |||
| 49 | constexpr std::uint32_t get(std::uint32_t word, unsigned low, unsigned width) { | ||
| 50 | return (word >> low) & ((1u << width) - 1); | ||
| 51 | } | ||
| 52 | |||
| 53 | // Mesa's util_unsigned_fixed and util_signed_fixed with 8 fraction bits, after its CLAMP. A NaN becomes `low`. | ||
| 54 | inline float clamped(float value, float low, float high) { | ||
| 55 | return value > low ? (value < high ? value : high) : low; | ||
| 56 | } | ||
| 57 | |||
| 58 | inline std::uint32_t unsigned_fixed(float value, float low, float high) { | ||
| 59 | return std::uint32_t(clamped(value, low, high) * 256.0f); | ||
| 60 | } | ||
| 61 | |||
| 62 | inline std::uint32_t signed_fixed(float value, float low, float high) { | ||
| 63 | return std::uint32_t(std::int32_t(clamped(value, low, high) * 256.0f)); | ||
| 64 | } | ||
| 65 | |||
| 66 | } // namespace detail | ||
| 67 | |||
| 68 | // SQ_SEL_XYZW01: what one DST_SEL field selects. 2 and 3 are reserved. | ||
| 69 | inline constexpr std::uint8_t select_zero = 0, select_one = 1, select_x = 4, select_y = 5, select_z = 6, select_w = 7; | ||
| 70 | inline constexpr Swizzle identity_swizzle{select_x, select_y, select_z, select_w}; | ||
| 71 | |||
| 72 | inline constexpr bool valid_swizzle(const Swizzle& swizzle) { | ||
| 73 | return std::all_of(swizzle.begin(), swizzle.end(), [](std::uint8_t s) { return s <= 7 && s != 2 && s != 3; }); | ||
| 74 | } | ||
| 75 | |||
| 76 | // ---- T#: SQ_IMG_RSRC_WORD0..7 ---- | ||
| 77 | |||
| 78 | // SQ_RSRC_IMG_TYPE (WORD3.TYPE). 0..7 are reserved, which is how a 32-byte heap slot holding a V# is told apart. | ||
| 79 | enum class ImageType : std::uint32_t { | ||
| 80 | image_1d = 8, | ||
| 81 | image_2d = 9, | ||
| 82 | image_3d = 10, | ||
| 83 | cube = 11, | ||
| 84 | image_1d_array = 12, | ||
| 85 | image_2d_array = 13, | ||
| 86 | image_2d_msaa = 14, | ||
| 87 | image_2d_msaa_array = 15, | ||
| 88 | }; | ||
| 89 | |||
| 90 | inline constexpr bool multisampled(ImageType type) { | ||
| 91 | return type == ImageType::image_2d_msaa || type == ImageType::image_2d_msaa_array; | ||
| 92 | } | ||
| 93 | |||
| 94 | // What a T# says. decode() returns the normal form: depth is 1 unless the type is 3D, layer_count is 1 for 3D, | ||
| 95 | // samples is 1 unless the type is multisampled, and a multisampled image has one mip. | ||
| 96 | struct Image { | ||
| 97 | std::uint64_t address = 0; // console VA of the image; the T# keeps it in 256-byte units (48 bits) | ||
| 98 | std::uint32_t format = 0; // GFX10_FORMAT_* code, 9 bits | ||
| 99 | ImageType type = ImageType::image_2d; | ||
| 100 | std::uint32_t width = 1; // of mip 0, 1..16384 | ||
| 101 | std::uint32_t height = 1; | ||
| 102 | std::uint32_t depth = 1; // of mip 0, for 3D: 1..8192 | ||
| 103 | std::uint32_t first_layer = 0; // arrays and cubes (a cube's layers are its faces, six to a cube) | ||
| 104 | std::uint32_t layer_count = 1; | ||
| 105 | std::uint32_t first_mip = 0; // the view's range | ||
| 106 | std::uint32_t mip_count = 1; | ||
| 107 | std::uint32_t resource_mips = 1; // of the whole resource | ||
| 108 | std::uint32_t samples = 1; // a power of two | ||
| 109 | Swizzle swizzle = identity_swizzle; | ||
| 110 | float min_lod = 0.0f; // the view's LOD clamp, 0..15 in steps of 1/256 (D3D12 ResourceMinLODClamp) | ||
| 111 | bool operator==(const Image&) const = default; | ||
| 112 | }; | ||
| 113 | |||
| 114 | // As ac_build_gfx10_texture_descriptor and ac_set_mutable_tex_desc_fields fill it: | ||
| 115 | // WORD0 BASE_ADDRESS = address >> 8 | ||
| 116 | // WORD1 BASE_ADDRESS_HI [7:0] = address >> 40, MIN_LOD [19:8] (4.8 fixed), FORMAT [28:20], WIDTH_LO [31:30] | ||
| 117 | // WORD2 WIDTH_HI [11:0], HEIGHT [27:14] (width - 1 and height - 1) | ||
| 118 | // WORD3 DST_SEL_X/Y/Z/W [11:0], BASE_LEVEL [15:12], LAST_LEVEL [19:16], TYPE [31:28] | ||
| 119 | // WORD4 DEPTH [12:0] = depth - 1 for 3D, else the last layer; BASE_ARRAY [28:16] = the first layer | ||
| 120 | // WORD5 MAX_MIP [7:4] = resource mips - 1 | ||
| 121 | // A multisampled image has BASE_LEVEL 0 and log2(samples) in LAST_LEVEL and MAX_MIP. | ||
| 122 | inline ImageWords encode(const Image& image) { | ||
| 123 | using detail::field; | ||
| 124 | const bool msaa = multisampled(image.type); | ||
| 125 | const std::uint32_t sample_log2 = std::uint32_t(std::bit_width(std::max(image.samples, 1u)) - 1); | ||
| 126 | const std::uint32_t base_level = msaa ? 0 : image.first_mip; | ||
| 127 | const std::uint32_t last_level = msaa ? sample_log2 : image.first_mip + image.mip_count - 1; | ||
| 128 | const std::uint32_t max_mip = msaa ? sample_log2 : image.resource_mips - 1; | ||
| 129 | const std::uint32_t last = | ||
| 130 | image.type == ImageType::image_3d ? image.depth - 1 : image.first_layer + image.layer_count - 1; | ||
| 131 | const std::uint64_t units = image.address >> 8; | ||
| 132 | const std::uint32_t width = image.width - 1, height = image.height - 1; | ||
| 133 | ImageWords words{}; | ||
| 134 | words[0] = std::uint32_t(units); | ||
| 135 | words[1] = field(std::uint32_t(units >> 32), 0, 8) | field(detail::unsigned_fixed(image.min_lod, 0, 15), 8, 12) | | ||
| 136 | field(image.format, 20, 9) | field(width, 30, 2); | ||
| 137 | words[2] = field(width >> 2, 0, 12) | field(height, 14, 14); | ||
| 138 | words[3] = field(image.swizzle[0], 0, 3) | field(image.swizzle[1], 3, 3) | field(image.swizzle[2], 6, 3) | | ||
| 139 | field(image.swizzle[3], 9, 3) | field(base_level, 12, 4) | field(last_level, 16, 4) | | ||
| 140 | field(std::uint32_t(image.type), 28, 4); | ||
| 141 | words[4] = field(last, 0, 13) | field(image.first_layer, 16, 13); | ||
| 142 | words[5] = field(max_mip, 4, 4); | ||
| 143 | return words; | ||
| 144 | } | ||
| 145 | |||
| 146 | // TYPE names an image, so these eight dwords are a T# and not a V# followed by something else. | ||
| 147 | inline constexpr bool is_image(const ImageWords& words) { return detail::get(words[3], 28, 4) >= 8; } | ||
| 148 | |||
| 149 | // False when the dwords are not an image descriptor, or a field contradicts another: a reserved DST_SEL, a last | ||
| 150 | // level or layer before the first, or a cube whose layers are not whole cubes. | ||
| 151 | inline bool decode(const ImageWords& words, Image& out) { | ||
| 152 | using detail::get; | ||
| 153 | if (!is_image(words)) return false; | ||
| 154 | Image image{}; | ||
| 155 | image.type = ImageType(get(words[3], 28, 4)); | ||
| 156 | image.address = ((std::uint64_t(get(words[1], 0, 8)) << 32) | words[0]) << 8; | ||
| 157 | image.min_lod = float(get(words[1], 8, 12)) / 256.0f; | ||
| 158 | image.format = get(words[1], 20, 9); | ||
| 159 | image.width = (get(words[1], 30, 2) | (get(words[2], 0, 12) << 2)) + 1; | ||
| 160 | image.height = get(words[2], 14, 14) + 1; | ||
| 161 | for (unsigned i = 0; i < 4; ++i) image.swizzle[i] = std::uint8_t(get(words[3], 3 * i, 3)); | ||
| 162 | if (!valid_swizzle(image.swizzle)) return false; | ||
| 163 | |||
| 164 | const std::uint32_t base_level = get(words[3], 12, 4), last_level = get(words[3], 16, 4); | ||
| 165 | if (multisampled(image.type)) { | ||
| 166 | if (base_level) return false; | ||
| 167 | image.samples = 1u << last_level; | ||
| 168 | } else { | ||
| 169 | if (last_level < base_level) return false; | ||
| 170 | image.first_mip = base_level; | ||
| 171 | image.mip_count = last_level - base_level + 1; | ||
| 172 | image.resource_mips = get(words[5], 4, 4) + 1; | ||
| 173 | } | ||
| 174 | |||
| 175 | const std::uint32_t last = get(words[4], 0, 13); | ||
| 176 | image.first_layer = get(words[4], 16, 13); | ||
| 177 | if (image.type == ImageType::image_3d) { | ||
| 178 | image.depth = last + 1; | ||
| 179 | } else { | ||
| 180 | if (last < image.first_layer) return false; | ||
| 181 | image.layer_count = last - image.first_layer + 1; | ||
| 182 | if (image.type == ImageType::cube && image.layer_count % 6) return false; | ||
| 183 | } | ||
| 184 | out = image; | ||
| 185 | return true; | ||
| 186 | } | ||
| 187 | |||
| 188 | // The view type translated shaders sample a T# of this type through. A cube is a six-layer 2D array there: the | ||
| 189 | // shader has already chosen the face (tools/rdna2spirv.py, image_op; docs/design.md, "Images and samplers"). | ||
| 190 | inline constexpr VkImageViewType vulkan_view_type(ImageType type) { | ||
| 191 | switch (type) { | ||
| 192 | case ImageType::image_1d: return VK_IMAGE_VIEW_TYPE_1D; | ||
| 193 | case ImageType::image_1d_array: return VK_IMAGE_VIEW_TYPE_1D_ARRAY; | ||
| 194 | case ImageType::image_3d: return VK_IMAGE_VIEW_TYPE_3D; | ||
| 195 | case ImageType::cube: | ||
| 196 | case ImageType::image_2d_array: | ||
| 197 | case ImageType::image_2d_msaa_array: return VK_IMAGE_VIEW_TYPE_2D_ARRAY; | ||
| 198 | default: return VK_IMAGE_VIEW_TYPE_2D; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | // ---- formats ---- | ||
| 203 | |||
| 204 | // One row for each Vulkan format: its GFX10_FORMAT code, and the DST_SEL that reads its channels as R, G, B, A | ||
| 205 | // (0 and 1 where the format has no such channel). A format's channels are X, Y, Z, W from the lowest bit (the | ||
| 206 | // first byte) up: Mesa pairs the channels of a format in that order with the sizes in the code's name reversed | ||
| 207 | // (gfx10_format_table.py). So R8G8B8A8 is the identity and B8G8R8A8 is the same code read as Z, Y, X, W. The | ||
| 208 | // first row of a code is the format vulkan_format() returns for it: the one in the identity order, where Vulkan | ||
| 209 | // has one. Depth formats share the codes of the colour formats with the same bits. | ||
| 210 | struct FormatRow { | ||
| 211 | std::uint32_t gfx10; | ||
| 212 | VkFormat vulkan; | ||
| 213 | Swizzle channels; | ||
| 214 | }; | ||
| 215 | |||
| 216 | inline constexpr Swizzle channels_r{select_x, select_zero, select_zero, select_one}; | ||
| 217 | inline constexpr Swizzle channels_rg{select_x, select_y, select_zero, select_one}; | ||
| 218 | inline constexpr Swizzle channels_rgb{select_x, select_y, select_z, select_one}; | ||
| 219 | inline constexpr Swizzle channels_rgba = identity_swizzle; | ||
| 220 | inline constexpr Swizzle channels_bgr{select_z, select_y, select_x, select_one}; | ||
| 221 | inline constexpr Swizzle channels_bgra{select_z, select_y, select_x, select_w}; | ||
| 222 | |||
| 223 | inline constexpr FormatRow format_table[] = { | ||
| 224 | {1, VK_FORMAT_R8_UNORM, channels_r}, // GFX10_FORMAT_8_UNORM | ||
| 225 | {2, VK_FORMAT_R8_SNORM, channels_r}, // GFX10_FORMAT_8_SNORM | ||
| 226 | {3, VK_FORMAT_R8_USCALED, channels_r}, // GFX10_FORMAT_8_USCALED | ||
| 227 | {4, VK_FORMAT_R8_SSCALED, channels_r}, // GFX10_FORMAT_8_SSCALED | ||
| 228 | {5, VK_FORMAT_R8_UINT, channels_r}, // GFX10_FORMAT_8_UINT | ||
| 229 | {5, VK_FORMAT_S8_UINT, channels_r}, // GFX10_FORMAT_8_UINT | ||
| 230 | {6, VK_FORMAT_R8_SINT, channels_r}, // GFX10_FORMAT_8_SINT | ||
| 231 | {7, VK_FORMAT_R16_UNORM, channels_r}, // GFX10_FORMAT_16_UNORM | ||
| 232 | {7, VK_FORMAT_D16_UNORM, channels_r}, // GFX10_FORMAT_16_UNORM | ||
| 233 | {8, VK_FORMAT_R16_SNORM, channels_r}, // GFX10_FORMAT_16_SNORM | ||
| 234 | {9, VK_FORMAT_R16_USCALED, channels_r}, // GFX10_FORMAT_16_USCALED | ||
| 235 | {10, VK_FORMAT_R16_SSCALED, channels_r}, // GFX10_FORMAT_16_SSCALED | ||
| 236 | {11, VK_FORMAT_R16_UINT, channels_r}, // GFX10_FORMAT_16_UINT | ||
| 237 | {12, VK_FORMAT_R16_SINT, channels_r}, // GFX10_FORMAT_16_SINT | ||
| 238 | {13, VK_FORMAT_R16_SFLOAT, channels_r}, // GFX10_FORMAT_16_FLOAT | ||
| 239 | {14, VK_FORMAT_R8G8_UNORM, channels_rg}, // GFX10_FORMAT_8_8_UNORM | ||
| 240 | {15, VK_FORMAT_R8G8_SNORM, channels_rg}, // GFX10_FORMAT_8_8_SNORM | ||
| 241 | {16, VK_FORMAT_R8G8_USCALED, channels_rg}, // GFX10_FORMAT_8_8_USCALED | ||
| 242 | {17, VK_FORMAT_R8G8_SSCALED, channels_rg}, // GFX10_FORMAT_8_8_SSCALED | ||
| 243 | {18, VK_FORMAT_R8G8_UINT, channels_rg}, // GFX10_FORMAT_8_8_UINT | ||
| 244 | {19, VK_FORMAT_R8G8_SINT, channels_rg}, // GFX10_FORMAT_8_8_SINT | ||
| 245 | {20, VK_FORMAT_R32_UINT, channels_r}, // GFX10_FORMAT_32_UINT | ||
| 246 | {21, VK_FORMAT_R32_SINT, channels_r}, // GFX10_FORMAT_32_SINT | ||
| 247 | {22, VK_FORMAT_R32_SFLOAT, channels_r}, // GFX10_FORMAT_32_FLOAT | ||
| 248 | {22, VK_FORMAT_D32_SFLOAT, channels_r}, // GFX10_FORMAT_32_FLOAT | ||
| 249 | {23, VK_FORMAT_R16G16_UNORM, channels_rg}, // GFX10_FORMAT_16_16_UNORM | ||
| 250 | {24, VK_FORMAT_R16G16_SNORM, channels_rg}, // GFX10_FORMAT_16_16_SNORM | ||
| 251 | {25, VK_FORMAT_R16G16_USCALED, channels_rg}, // GFX10_FORMAT_16_16_USCALED | ||
| 252 | {26, VK_FORMAT_R16G16_SSCALED, channels_rg}, // GFX10_FORMAT_16_16_SSCALED | ||
| 253 | {27, VK_FORMAT_R16G16_UINT, channels_rg}, // GFX10_FORMAT_16_16_UINT | ||
| 254 | {28, VK_FORMAT_R16G16_SINT, channels_rg}, // GFX10_FORMAT_16_16_SINT | ||
| 255 | {29, VK_FORMAT_R16G16_SFLOAT, channels_rg}, // GFX10_FORMAT_16_16_FLOAT | ||
| 256 | {36, VK_FORMAT_B10G11R11_UFLOAT_PACK32, channels_rgb}, // GFX10_FORMAT_10_11_11_FLOAT | ||
| 257 | {50, VK_FORMAT_A2B10G10R10_UNORM_PACK32, channels_rgba}, // GFX10_FORMAT_2_10_10_10_UNORM | ||
| 258 | {50, VK_FORMAT_A2R10G10B10_UNORM_PACK32, channels_bgra}, // GFX10_FORMAT_2_10_10_10_UNORM | ||
| 259 | {51, VK_FORMAT_A2B10G10R10_SNORM_PACK32, channels_rgba}, // GFX10_FORMAT_2_10_10_10_SNORM | ||
| 260 | {52, VK_FORMAT_A2B10G10R10_USCALED_PACK32, channels_rgba}, // GFX10_FORMAT_2_10_10_10_USCALED | ||
| 261 | {53, VK_FORMAT_A2B10G10R10_SSCALED_PACK32, channels_rgba}, // GFX10_FORMAT_2_10_10_10_SSCALED | ||
| 262 | {54, VK_FORMAT_A2B10G10R10_UINT_PACK32, channels_rgba}, // GFX10_FORMAT_2_10_10_10_UINT | ||
| 263 | {54, VK_FORMAT_A2R10G10B10_UINT_PACK32, channels_bgra}, // GFX10_FORMAT_2_10_10_10_UINT | ||
| 264 | {55, VK_FORMAT_A2B10G10R10_SINT_PACK32, channels_rgba}, // GFX10_FORMAT_2_10_10_10_SINT | ||
| 265 | {56, VK_FORMAT_R8G8B8A8_UNORM, channels_rgba}, // GFX10_FORMAT_8_8_8_8_UNORM | ||
| 266 | {56, VK_FORMAT_B8G8R8A8_UNORM, channels_bgra}, // GFX10_FORMAT_8_8_8_8_UNORM | ||
| 267 | {57, VK_FORMAT_R8G8B8A8_SNORM, channels_rgba}, // GFX10_FORMAT_8_8_8_8_SNORM | ||
| 268 | {58, VK_FORMAT_R8G8B8A8_USCALED, channels_rgba}, // GFX10_FORMAT_8_8_8_8_USCALED | ||
| 269 | {59, VK_FORMAT_R8G8B8A8_SSCALED, channels_rgba}, // GFX10_FORMAT_8_8_8_8_SSCALED | ||
| 270 | {60, VK_FORMAT_R8G8B8A8_UINT, channels_rgba}, // GFX10_FORMAT_8_8_8_8_UINT | ||
| 271 | {61, VK_FORMAT_R8G8B8A8_SINT, channels_rgba}, // GFX10_FORMAT_8_8_8_8_SINT | ||
| 272 | {62, VK_FORMAT_R32G32_UINT, channels_rg}, // GFX10_FORMAT_32_32_UINT | ||
| 273 | {63, VK_FORMAT_R32G32_SINT, channels_rg}, // GFX10_FORMAT_32_32_SINT | ||
| 274 | {64, VK_FORMAT_R32G32_SFLOAT, channels_rg}, // GFX10_FORMAT_32_32_FLOAT | ||
| 275 | {65, VK_FORMAT_R16G16B16A16_UNORM, channels_rgba}, // GFX10_FORMAT_16_16_16_16_UNORM | ||
| 276 | {66, VK_FORMAT_R16G16B16A16_SNORM, channels_rgba}, // GFX10_FORMAT_16_16_16_16_SNORM | ||
| 277 | {67, VK_FORMAT_R16G16B16A16_USCALED, channels_rgba}, // GFX10_FORMAT_16_16_16_16_USCALED | ||
| 278 | {68, VK_FORMAT_R16G16B16A16_SSCALED, channels_rgba}, // GFX10_FORMAT_16_16_16_16_SSCALED | ||
| 279 | {69, VK_FORMAT_R16G16B16A16_UINT, channels_rgba}, // GFX10_FORMAT_16_16_16_16_UINT | ||
| 280 | {70, VK_FORMAT_R16G16B16A16_SINT, channels_rgba}, // GFX10_FORMAT_16_16_16_16_SINT | ||
| 281 | {71, VK_FORMAT_R16G16B16A16_SFLOAT, channels_rgba}, // GFX10_FORMAT_16_16_16_16_FLOAT | ||
| 282 | {72, VK_FORMAT_R32G32B32_UINT, channels_rgb}, // GFX10_FORMAT_32_32_32_UINT | ||
| 283 | {73, VK_FORMAT_R32G32B32_SINT, channels_rgb}, // GFX10_FORMAT_32_32_32_SINT | ||
| 284 | {74, VK_FORMAT_R32G32B32_SFLOAT, channels_rgb}, // GFX10_FORMAT_32_32_32_FLOAT | ||
| 285 | {75, VK_FORMAT_R32G32B32A32_UINT, channels_rgba}, // GFX10_FORMAT_32_32_32_32_UINT | ||
| 286 | {76, VK_FORMAT_R32G32B32A32_SINT, channels_rgba}, // GFX10_FORMAT_32_32_32_32_SINT | ||
| 287 | {77, VK_FORMAT_R32G32B32A32_SFLOAT, channels_rgba}, // GFX10_FORMAT_32_32_32_32_FLOAT | ||
| 288 | {128, VK_FORMAT_R8_SRGB, channels_r}, // GFX10_FORMAT_8_SRGB | ||
| 289 | {129, VK_FORMAT_R8G8_SRGB, channels_rg}, // GFX10_FORMAT_8_8_SRGB | ||
| 290 | {130, VK_FORMAT_R8G8B8A8_SRGB, channels_rgba}, // GFX10_FORMAT_8_8_8_8_SRGB | ||
| 291 | {130, VK_FORMAT_B8G8R8A8_SRGB, channels_bgra}, // GFX10_FORMAT_8_8_8_8_SRGB | ||
| 292 | {132, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, channels_rgb}, // GFX10_FORMAT_5_9_9_9_FLOAT | ||
| 293 | {133, VK_FORMAT_B5G6R5_UNORM_PACK16, channels_rgb}, // GFX10_FORMAT_5_6_5_UNORM | ||
| 294 | {133, VK_FORMAT_R5G6B5_UNORM_PACK16, channels_bgr}, // GFX10_FORMAT_5_6_5_UNORM | ||
| 295 | {134, VK_FORMAT_A1R5G5B5_UNORM_PACK16, channels_bgra}, // GFX10_FORMAT_1_5_5_5_UNORM | ||
| 296 | {136, VK_FORMAT_A4B4G4R4_UNORM_PACK16, channels_rgba}, // GFX10_FORMAT_4_4_4_4_UNORM | ||
| 297 | {136, VK_FORMAT_A4R4G4B4_UNORM_PACK16, channels_bgra}, // GFX10_FORMAT_4_4_4_4_UNORM | ||
| 298 | {141, VK_FORMAT_D24_UNORM_S8_UINT, channels_r}, // GFX10_FORMAT_8_24_UNORM | ||
| 299 | {141, VK_FORMAT_X8_D24_UNORM_PACK32, channels_r}, // GFX10_FORMAT_8_24_UNORM | ||
| 300 | {146, VK_FORMAT_D32_SFLOAT_S8_UINT, channels_r}, // GFX10_FORMAT_X24_8_32_FLOAT | ||
| 301 | {169, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, channels_rgba}, // GFX10_FORMAT_BC1_UNORM | ||
| 302 | {169, VK_FORMAT_BC1_RGB_UNORM_BLOCK, channels_rgb}, // GFX10_FORMAT_BC1_UNORM | ||
| 303 | {170, VK_FORMAT_BC1_RGBA_SRGB_BLOCK, channels_rgba}, // GFX10_FORMAT_BC1_SRGB | ||
| 304 | {170, VK_FORMAT_BC1_RGB_SRGB_BLOCK, channels_rgb}, // GFX10_FORMAT_BC1_SRGB | ||
| 305 | {171, VK_FORMAT_BC2_UNORM_BLOCK, channels_rgba}, // GFX10_FORMAT_BC2_UNORM | ||
| 306 | {172, VK_FORMAT_BC2_SRGB_BLOCK, channels_rgba}, // GFX10_FORMAT_BC2_SRGB | ||
| 307 | {173, VK_FORMAT_BC3_UNORM_BLOCK, channels_rgba}, // GFX10_FORMAT_BC3_UNORM | ||
| 308 | {174, VK_FORMAT_BC3_SRGB_BLOCK, channels_rgba}, // GFX10_FORMAT_BC3_SRGB | ||
| 309 | {175, VK_FORMAT_BC4_UNORM_BLOCK, channels_r}, // GFX10_FORMAT_BC4_UNORM | ||
| 310 | {176, VK_FORMAT_BC4_SNORM_BLOCK, channels_r}, // GFX10_FORMAT_BC4_SNORM | ||
| 311 | {177, VK_FORMAT_BC5_UNORM_BLOCK, channels_rg}, // GFX10_FORMAT_BC5_UNORM | ||
| 312 | {178, VK_FORMAT_BC5_SNORM_BLOCK, channels_rg}, // GFX10_FORMAT_BC5_SNORM | ||
| 313 | {179, VK_FORMAT_BC6H_UFLOAT_BLOCK, channels_rgb}, // GFX10_FORMAT_BC6_UFLOAT | ||
| 314 | {180, VK_FORMAT_BC6H_SFLOAT_BLOCK, channels_rgb}, // GFX10_FORMAT_BC6_SFLOAT | ||
| 315 | {181, VK_FORMAT_BC7_UNORM_BLOCK, channels_rgba}, // GFX10_FORMAT_BC7_UNORM | ||
| 316 | {182, VK_FORMAT_BC7_SRGB_BLOCK, channels_rgba}, // GFX10_FORMAT_BC7_SRGB | ||
| 317 | }; | ||
| 318 | |||
| 319 | inline constexpr const FormatRow* format_row(VkFormat format) { | ||
| 320 | for (const FormatRow& row : format_table) | ||
| 321 | if (row.vulkan == format) return &row; | ||
| 322 | return nullptr; | ||
| 323 | } | ||
| 324 | |||
| 325 | // The Vulkan format with the code's bits, in the hardware's channel order where Vulkan has such a format. | ||
| 326 | // VK_FORMAT_UNDEFINED: not in the table. A code that depth and colour formats share gives the colour format. | ||
| 327 | inline constexpr VkFormat vulkan_format(std::uint32_t gfx10_format) { | ||
| 328 | for (const FormatRow& row : format_table) | ||
| 329 | if (row.gfx10 == gfx10_format) return row.vulkan; | ||
| 330 | return VK_FORMAT_UNDEFINED; | ||
| 331 | } | ||
| 332 | |||
| 333 | // 0 (GFX10_FORMAT_INVALID): not in the table. | ||
| 334 | inline constexpr std::uint32_t format_from_vulkan(VkFormat format) { | ||
| 335 | const FormatRow* row = format_row(format); | ||
| 336 | return row ? row->gfx10 : 0; | ||
| 337 | } | ||
| 338 | |||
| 339 | // The DST_SEL a T# of this format carries when the view applies no component mapping of its own: it reads the | ||
| 340 | // format's channels as R, G, B, A. The identity for a format that is not in the table. | ||
| 341 | inline constexpr Swizzle format_swizzle(VkFormat format) { | ||
| 342 | const FormatRow* row = format_row(format); | ||
| 343 | return row ? row->channels : identity_swizzle; | ||
| 344 | } | ||
| 345 | |||
| 346 | // The format to view an image of `image_format` in when a T# names `gfx10_format`: the image's own format when | ||
| 347 | // the codes agree, else the format of that code with the image's channel order (the sRGB view of a UNORM image, | ||
| 348 | // say). VK_FORMAT_UNDEFINED: the table has no such format. | ||
| 349 | inline constexpr VkFormat vulkan_view_format(VkFormat image_format, std::uint32_t gfx10_format) { | ||
| 350 | const FormatRow* image = format_row(image_format); | ||
| 351 | if (!image) return VK_FORMAT_UNDEFINED; | ||
| 352 | if (image->gfx10 == gfx10_format) return image_format; | ||
| 353 | for (const FormatRow& row : format_table) | ||
| 354 | if (row.gfx10 == gfx10_format && row.channels == image->channels) return row.vulkan; | ||
| 355 | return VK_FORMAT_UNDEFINED; | ||
| 356 | } | ||
| 357 | |||
| 358 | // DST_SEL as the component mapping of a view whose format is `view_format`: X, Y, Z, W name the hardware's | ||
| 359 | // channels, and the format says which of R, G, B, A each one is. A channel the format does not have reads as zero. | ||
| 360 | inline constexpr VkComponentMapping vulkan_swizzle(const Swizzle& swizzle, VkFormat view_format) { | ||
| 361 | const Swizzle channels = format_swizzle(view_format); | ||
| 362 | const auto component = [&](std::uint8_t select) { | ||
| 363 | if (select == select_one) return VK_COMPONENT_SWIZZLE_ONE; | ||
| 364 | if (select >= select_x && select <= select_w) | ||
| 365 | for (unsigned i = 0; i < 4; ++i) | ||
| 366 | if (channels[i] == select) return VkComponentSwizzle(VK_COMPONENT_SWIZZLE_R + i); | ||
| 367 | return VK_COMPONENT_SWIZZLE_ZERO; | ||
| 368 | }; | ||
| 369 | return {component(swizzle[0]), component(swizzle[1]), component(swizzle[2]), component(swizzle[3])}; | ||
| 370 | } | ||
| 371 | |||
| 372 | // The same for a format in the hardware's own order (X, Y, Z, W are R, G, B, A). | ||
| 373 | inline constexpr VkComponentMapping vulkan_swizzle(const Swizzle& swizzle) { | ||
| 374 | return vulkan_swizzle(swizzle, VK_FORMAT_R8G8B8A8_UNORM); | ||
| 375 | } | ||
| 376 | |||
| 377 | // ---- S#: SQ_IMG_SAMP_WORD0..3 ---- | ||
| 378 | |||
| 379 | enum class AddressMode : std::uint32_t { // SQ_TEX_CLAMP | ||
| 380 | wrap = 0, | ||
| 381 | mirror = 1, | ||
| 382 | clamp_last_texel = 2, | ||
| 383 | mirror_once_last_texel = 3, | ||
| 384 | clamp_half_border = 4, | ||
| 385 | mirror_once_half_border = 5, | ||
| 386 | clamp_border = 6, | ||
| 387 | mirror_once_border = 7, | ||
| 388 | }; | ||
| 389 | |||
| 390 | enum class Filter : std::uint32_t { // SQ_TEX_XY_FILTER | ||
| 391 | point = 0, | ||
| 392 | bilinear = 1, | ||
| 393 | aniso_point = 2, | ||
| 394 | aniso_bilinear = 3, | ||
| 395 | }; | ||
| 396 | |||
| 397 | enum class MipFilter : std::uint32_t { // SQ_TEX_MIP_FILTER | ||
| 398 | none = 0, | ||
| 399 | point = 1, | ||
| 400 | linear = 2, | ||
| 401 | }; | ||
| 402 | |||
| 403 | enum class CompareFunction : std::uint32_t { // SQ_TEX_DEPTH_COMPARE; the same values as VkCompareOp | ||
| 404 | never = 0, | ||
| 405 | less = 1, | ||
| 406 | equal = 2, | ||
| 407 | less_equal = 3, | ||
| 408 | greater = 4, | ||
| 409 | not_equal = 5, | ||
| 410 | greater_equal = 6, | ||
| 411 | always = 7, | ||
| 412 | }; | ||
| 413 | |||
| 414 | enum class BorderColor : std::uint32_t { // SQ_TEX_BORDER_COLOR | ||
| 415 | transparent_black = 0, | ||
| 416 | opaque_black = 1, | ||
| 417 | opaque_white = 2, | ||
| 418 | table = 3, // SQ_TEX_BORDER_COLOR_REGISTER: the colour at border_color_index in the device's table | ||
| 419 | }; | ||
| 420 | |||
| 421 | enum class FilterMode : std::uint32_t { // SQ_IMG_FILTER_TYPE: D3D12's MINIMUM_ and MAXIMUM_ filters | ||
| 422 | blend = 0, | ||
| 423 | minimum = 1, | ||
| 424 | maximum = 2, | ||
| 425 | }; | ||
| 426 | |||
| 427 | // What an S# says. decode() returns the normal form: max_anisotropy is 1, 2, 4, 8 or 16. | ||
| 428 | struct Sampler { | ||
| 429 | AddressMode address_u = AddressMode::wrap; | ||
| 430 | AddressMode address_v = AddressMode::wrap; | ||
| 431 | AddressMode address_w = AddressMode::wrap; | ||
| 432 | Filter mag_filter = Filter::point; | ||
| 433 | Filter min_filter = Filter::point; | ||
| 434 | MipFilter mip_filter = MipFilter::none; | ||
| 435 | std::uint32_t max_anisotropy = 1; | ||
| 436 | CompareFunction compare = CompareFunction::never; // never: not a comparison sampler, as radv writes it | ||
| 437 | float min_lod = 0.0f; // 0..15 in steps of 1/256 | ||
| 438 | float max_lod = 0.0f; | ||
| 439 | float lod_bias = 0.0f; // -32..31 in steps of 1/256 | ||
| 440 | BorderColor border_color = BorderColor::transparent_black; | ||
| 441 | std::uint32_t border_color_index = 0; // 12 bits | ||
| 442 | bool unnormalized = false; | ||
| 443 | FilterMode filter_mode = FilterMode::blend; | ||
| 444 | bool operator==(const Sampler&) const = default; | ||
| 445 | }; | ||
| 446 | |||
| 447 | // As ac_build_sampler_descriptor fills it for GFX10: | ||
| 448 | // WORD0 CLAMP_X [2:0], CLAMP_Y [5:3], CLAMP_Z [8:6], MAX_ANISO_RATIO [11:9] = log2(anisotropy), at most 4, | ||
| 449 | // DEPTH_COMPARE_FUNC [14:12], FORCE_UNNORMALIZED [15], FILTER_MODE [30:29] | ||
| 450 | // WORD1 MIN_LOD [11:0], MAX_LOD [23:12] (4.8 fixed, clamped to 0..15) | ||
| 451 | // WORD2 LOD_BIAS [13:0] (signed 5.8 fixed, clamped to -32..31), XY_MAG_FILTER [21:20], XY_MIN_FILTER [23:22], | ||
| 452 | // MIP_FILTER [27:26] | ||
| 453 | // WORD3 BORDER_COLOR_PTR [11:0], BORDER_COLOR_TYPE [31:30] | ||
| 454 | // Mesa also sets tuning fields from the anisotropy (ANISO_THRESHOLD, ANISO_BIAS, PERF_MIP, ANISO_OVERRIDE) and | ||
| 455 | // TRUNC_COORD for point filters. They are not written here, and decode() ignores them. | ||
| 456 | inline SamplerWords encode(const Sampler& sampler) { | ||
| 457 | using detail::field; | ||
| 458 | const std::uint32_t aniso_log2 = | ||
| 459 | std::min(std::uint32_t(std::bit_width(std::max(sampler.max_anisotropy, 1u)) - 1), 4u); | ||
| 460 | SamplerWords words{}; | ||
| 461 | words[0] = field(std::uint32_t(sampler.address_u), 0, 3) | field(std::uint32_t(sampler.address_v), 3, 3) | | ||
| 462 | field(std::uint32_t(sampler.address_w), 6, 3) | field(aniso_log2, 9, 3) | | ||
| 463 | field(std::uint32_t(sampler.compare), 12, 3) | field(sampler.unnormalized ? 1u : 0u, 15, 1) | | ||
| 464 | field(std::uint32_t(sampler.filter_mode), 29, 2); | ||
| 465 | words[1] = field(detail::unsigned_fixed(sampler.min_lod, 0, 15), 0, 12) | | ||
| 466 | field(detail::unsigned_fixed(sampler.max_lod, 0, 15), 12, 12); | ||
| 467 | words[2] = field(detail::signed_fixed(sampler.lod_bias, -32, 31), 0, 14) | | ||
| 468 | field(std::uint32_t(sampler.mag_filter), 20, 2) | field(std::uint32_t(sampler.min_filter), 22, 2) | | ||
| 469 | field(std::uint32_t(sampler.mip_filter), 26, 2); | ||
| 470 | words[3] = field(sampler.border_color_index, 0, 12) | field(std::uint32_t(sampler.border_color), 30, 2); | ||
| 471 | return words; | ||
| 472 | } | ||
| 473 | |||
| 474 | // Every bit pattern is a sampler; values the enums do not name (MIP_FILTER 3, FILTER_MODE 3) are kept as they | ||
| 475 | // are, and vulkan_sampler() treats them as the default. | ||
| 476 | inline Sampler decode(const SamplerWords& words) { | ||
| 477 | using detail::get; | ||
| 478 | Sampler sampler{}; | ||
| 479 | sampler.address_u = AddressMode(get(words[0], 0, 3)); | ||
| 480 | sampler.address_v = AddressMode(get(words[0], 3, 3)); | ||
| 481 | sampler.address_w = AddressMode(get(words[0], 6, 3)); | ||
| 482 | sampler.max_anisotropy = 1u << std::min(get(words[0], 9, 3), 4u); | ||
| 483 | sampler.compare = CompareFunction(get(words[0], 12, 3)); | ||
| 484 | sampler.unnormalized = get(words[0], 15, 1) != 0; | ||
| 485 | sampler.filter_mode = FilterMode(get(words[0], 29, 2)); | ||
| 486 | sampler.min_lod = float(get(words[1], 0, 12)) / 256.0f; | ||
| 487 | sampler.max_lod = float(get(words[1], 12, 12)) / 256.0f; | ||
| 488 | const std::uint32_t bias = get(words[2], 0, 14); | ||
| 489 | sampler.lod_bias = float(std::int32_t(bias ^ 0x2000u) - 0x2000) / 256.0f; // sign-extend 14 bits | ||
| 490 | sampler.mag_filter = Filter(get(words[2], 20, 2)); | ||
| 491 | sampler.min_filter = Filter(get(words[2], 22, 2)); | ||
| 492 | sampler.mip_filter = MipFilter(get(words[2], 26, 2)); | ||
| 493 | sampler.border_color_index = get(words[3], 0, 12); | ||
| 494 | sampler.border_color = BorderColor(get(words[3], 30, 2)); | ||
| 495 | return sampler; | ||
| 496 | } | ||
| 497 | |||
| 498 | // The inverse of radv_tex_wrap. Vulkan has no half-border or mirror-once-border mode; the nearest one is used. | ||
| 499 | inline constexpr VkSamplerAddressMode vulkan_address_mode(AddressMode mode) { | ||
| 500 | switch (mode) { | ||
| 501 | case AddressMode::wrap: return VK_SAMPLER_ADDRESS_MODE_REPEAT; | ||
| 502 | case AddressMode::mirror: return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT; | ||
| 503 | case AddressMode::clamp_border: return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER; | ||
| 504 | case AddressMode::mirror_once_last_texel: | ||
| 505 | case AddressMode::mirror_once_half_border: | ||
| 506 | case AddressMode::mirror_once_border: return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE; | ||
| 507 | default: return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // clamp_last_texel, clamp_half_border | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | inline constexpr VkSamplerReductionMode vulkan_reduction_mode(FilterMode mode) { | ||
| 512 | switch (mode) { | ||
| 513 | case FilterMode::minimum: return VK_SAMPLER_REDUCTION_MODE_MIN; | ||
| 514 | case FilterMode::maximum: return VK_SAMPLER_REDUCTION_MODE_MAX; | ||
| 515 | default: return VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | // The inverse of radv_make_sampler_descriptor. Left to the caller, because they need more than this structure: | ||
| 520 | // - border_color == table: VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK is returned; the colour itself is in a table | ||
| 521 | // the S# only indexes (VK_EXT_custom_border_color); | ||
| 522 | // - filter_mode != blend: chain a VkSamplerReductionModeCreateInfo with vulkan_reduction_mode(); | ||
| 523 | // - mirror-once modes need samplerMirrorClampToEdge, anisotropy needs samplerAnisotropy. | ||
| 524 | // MIP_FILTER_NONE samples the base level only, which Vulkan expresses as nearest mip filtering with the LOD held | ||
| 525 | // in [0, 0.25] (the Vulkan specification's note on samplers without mipmapping). An unnormalised sampler gets | ||
| 526 | // what Vulkan requires of one: no mips, no anisotropy, no comparison. | ||
| 527 | inline VkSamplerCreateInfo vulkan_sampler(const Sampler& sampler) { | ||
| 528 | static_assert(int(CompareFunction::never) == int(VK_COMPARE_OP_NEVER) && | ||
| 529 | int(CompareFunction::less) == int(VK_COMPARE_OP_LESS) && | ||
| 530 | int(CompareFunction::equal) == int(VK_COMPARE_OP_EQUAL) && | ||
| 531 | int(CompareFunction::less_equal) == int(VK_COMPARE_OP_LESS_OR_EQUAL) && | ||
| 532 | int(CompareFunction::greater) == int(VK_COMPARE_OP_GREATER) && | ||
| 533 | int(CompareFunction::not_equal) == int(VK_COMPARE_OP_NOT_EQUAL) && | ||
| 534 | int(CompareFunction::greater_equal) == int(VK_COMPARE_OP_GREATER_OR_EQUAL) && | ||
| 535 | int(CompareFunction::always) == int(VK_COMPARE_OP_ALWAYS)); | ||
| 536 | const auto filter = [](Filter f) { | ||
| 537 | return f == Filter::bilinear || f == Filter::aniso_bilinear ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; | ||
| 538 | }; | ||
| 539 | VkSamplerCreateInfo info{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; | ||
| 540 | info.magFilter = filter(sampler.mag_filter); | ||
| 541 | info.minFilter = filter(sampler.min_filter); | ||
| 542 | info.mipmapMode = | ||
| 543 | sampler.mip_filter == MipFilter::linear ? VK_SAMPLER_MIPMAP_MODE_LINEAR : VK_SAMPLER_MIPMAP_MODE_NEAREST; | ||
| 544 | info.addressModeU = vulkan_address_mode(sampler.address_u); | ||
| 545 | info.addressModeV = vulkan_address_mode(sampler.address_v); | ||
| 546 | info.addressModeW = vulkan_address_mode(sampler.address_w); | ||
| 547 | info.mipLodBias = sampler.lod_bias; | ||
| 548 | info.anisotropyEnable = sampler.max_anisotropy > 1 ? VK_TRUE : VK_FALSE; | ||
| 549 | info.maxAnisotropy = float(std::max(sampler.max_anisotropy, 1u)); | ||
| 550 | info.compareEnable = sampler.compare != CompareFunction::never ? VK_TRUE : VK_FALSE; | ||
| 551 | info.compareOp = VkCompareOp(sampler.compare); | ||
| 552 | info.minLod = sampler.min_lod; | ||
| 553 | info.maxLod = sampler.max_lod; | ||
| 554 | if (sampler.mip_filter == MipFilter::none) { | ||
| 555 | info.minLod = 0.0f; | ||
| 556 | info.maxLod = 0.25f; | ||
| 557 | } | ||
| 558 | switch (sampler.border_color) { | ||
| 559 | case BorderColor::opaque_black: info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK; break; | ||
| 560 | case BorderColor::opaque_white: info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; break; | ||
| 561 | default: info.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; break; | ||
| 562 | } | ||
| 563 | if (sampler.unnormalized) { | ||
| 564 | info.unnormalizedCoordinates = VK_TRUE; | ||
| 565 | info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; | ||
| 566 | info.minLod = info.maxLod = 0.0f; | ||
| 567 | info.anisotropyEnable = VK_FALSE; | ||
| 568 | info.maxAnisotropy = 1.0f; | ||
| 569 | info.compareEnable = VK_FALSE; | ||
| 570 | } | ||
| 571 | return info; | ||
| 572 | } | ||
| 573 | |||
| 574 | // ---- V#: SQ_BUF_RSRC_WORD0..3 ---- | ||
| 575 | |||
| 576 | // SQ_BUF_RSRC_WORD3.OOB_SELECT: the bounds check the hardware makes. The translator makes its own from the | ||
| 577 | // stride (tools/rdna2spirv.py, buffer_address): stride 0 checks the byte offset against num_records, otherwise | ||
| 578 | // the index, or the offset against num_records * stride. | ||
| 579 | enum class OobSelect : std::uint32_t { | ||
| 580 | structured_with_offset = 0, | ||
| 581 | structured = 1, | ||
| 582 | disabled = 2, | ||
| 583 | raw = 3, | ||
| 584 | }; | ||
| 585 | |||
| 586 | struct Buffer { | ||
| 587 | std::uint64_t address = 0; // console VA of the first byte, 48 bits | ||
| 588 | std::uint32_t stride = 0; // bytes a record, 14 bits; 0: a raw buffer | ||
| 589 | std::uint32_t num_records = 0; // records, or bytes when the stride is 0 | ||
| 590 | std::uint32_t format = 0; // GFX10_FORMAT_* code below 128, for typed loads and stores | ||
| 591 | Swizzle swizzle = identity_swizzle; | ||
| 592 | OobSelect oob_select = OobSelect::structured_with_offset; | ||
| 593 | bool operator==(const Buffer&) const = default; | ||
| 594 | }; | ||
| 595 | |||
| 596 | // As ac_build_buffer_descriptor fills it: | ||
| 597 | // WORD0 BASE_ADDRESS = address | ||
| 598 | // WORD1 BASE_ADDRESS_HI [15:0] = address >> 32, STRIDE [29:16] | ||
| 599 | // WORD2 NUM_RECORDS | ||
| 600 | // WORD3 DST_SEL_X/Y/Z/W [11:0], FORMAT [18:12], OOB_SELECT [29:28], TYPE [31:30] = 0 (SQ_RSRC_BUF) | ||
| 601 | inline BufferWords encode(const Buffer& buffer) { | ||
| 602 | using detail::field; | ||
| 603 | BufferWords words{}; | ||
| 604 | words[0] = std::uint32_t(buffer.address); | ||
| 605 | words[1] = field(std::uint32_t(buffer.address >> 32), 0, 16) | field(buffer.stride, 16, 14); | ||
| 606 | words[2] = buffer.num_records; | ||
| 607 | words[3] = field(buffer.swizzle[0], 0, 3) | field(buffer.swizzle[1], 3, 3) | field(buffer.swizzle[2], 6, 3) | | ||
| 608 | field(buffer.swizzle[3], 9, 3) | field(buffer.format, 12, 7) | | ||
| 609 | field(std::uint32_t(buffer.oob_select), 28, 2); | ||
| 610 | return words; | ||
| 611 | } | ||
| 612 | |||
| 613 | // False when TYPE is not a buffer's (an image's TYPE has its top bit set) or a DST_SEL is reserved. | ||
| 614 | inline bool decode(const BufferWords& words, Buffer& out) { | ||
| 615 | using detail::get; | ||
| 616 | if (get(words[3], 30, 2) != 0) return false; | ||
| 617 | Buffer buffer{}; | ||
| 618 | buffer.address = (std::uint64_t(get(words[1], 0, 16)) << 32) | words[0]; | ||
| 619 | buffer.stride = get(words[1], 16, 14); | ||
| 620 | buffer.num_records = words[2]; | ||
| 621 | for (unsigned i = 0; i < 4; ++i) buffer.swizzle[i] = std::uint8_t(get(words[3], 3 * i, 3)); | ||
| 622 | if (!valid_swizzle(buffer.swizzle)) return false; | ||
| 623 | buffer.format = get(words[3], 12, 7); | ||
| 624 | buffer.oob_select = OobSelect(get(words[3], 28, 2)); | ||
| 625 | out = buffer; | ||
| 626 | return true; | ||
| 627 | } | ||
| 628 | |||
| 629 | } // namespace leonida::gfx10 | ||
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h new file mode 100644 index 000000000..8cb895adb --- /dev/null +++ b/runtime/src/vulkan_bindless.h | |||
| @@ -0,0 +1,579 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | // The bindless descriptor set of translated shaders: set 0 of the contract in docs/design.md and | ||
| 4 | // docs/d3d12_vulkan.md section 5, as tools/rdna2spirv.py declares it. | ||
| 5 | // | ||
| 6 | // binding 0 sampled 2D images 4096 5 sampled 1D images 256 | ||
| 7 | // 1 samplers 1024 6 sampled 3D images 256 | ||
| 8 | // 2 the descriptor hash table 1 7 3D r32ui storage images 256 | ||
| 9 | // 3 sampled 2D-array images 1024 8 + 3 * dimension + class 256 each: | ||
| 10 | // 4 2D r32ui storage images 1024 storage images by component class | ||
| 11 | // dimension: 0 = 2D, 1 = 2D array, 2 = 3D, 3 = 1D (the translator's STORE_DIMS); class: 0 = float, 1 = uint, 2 = sint. | ||
| 12 | // The counts are those of runtime/test/vk_pipeline_check.cpp, which creates every translated shader against them. | ||
| 13 | // | ||
| 14 | // A shader turns a T# or S# into an index through the hash table (descriptor_table.h) and uses that index in | ||
| 15 | // whichever binding the instruction calls for: the sampled array of the image's dimension, or one of its storage | ||
| 16 | // arrays. So an image has one index, the same in every binding of its dimension, and each dimension has its own | ||
| 17 | // index space. The storage arrays hold 256, so an image with storage views gets an index below 256; one without | ||
| 18 | // takes a higher index while there is one. Index 0 of every array is a fallback, because a miss gives 0: a 1x1 | ||
| 19 | // image of zeros, a nearest clamping sampler, and 1x1 storage images. The caller owns the views and samplers it | ||
| 20 | // adds and keeps them alive while they are in the set; this class owns only the fallbacks and the table's buffer. | ||
| 21 | // | ||
| 22 | // Device features: runtimeDescriptorArray for the shaders. With BindlessFeatures::update_after_bind | ||
| 23 | // (descriptorBindingSampledImageUpdateAfterBind, ...StorageImage..., ...StorageBuffer...) flush() may run while | ||
| 24 | // command buffers that use the set are pending, as long as no descriptor they use changes and flush_needs_idle() | ||
| 25 | // is false; without it the caller flushes only when none is. With partially_bound (descriptorBindingPartiallyBound) only index 0 is written at | ||
| 26 | // creation; otherwise every element of every array starts as the fallback. | ||
| 27 | // | ||
| 28 | // Not thread-safe: the owner serialises calls. | ||
| 29 | |||
| 30 | #ifndef VK_NO_PROTOTYPES | ||
| 31 | #define VK_NO_PROTOTYPES | ||
| 32 | #endif | ||
| 33 | #include "descriptor_table.h" | ||
| 34 | |||
| 35 | #include <vulkan/vulkan.h> | ||
| 36 | |||
| 37 | #include <algorithm> | ||
| 38 | #include <array> | ||
| 39 | #include <cstdint> | ||
| 40 | #include <cstring> | ||
| 41 | #include <unordered_map> | ||
| 42 | #include <vector> | ||
| 43 | |||
| 44 | namespace leonida { | ||
| 45 | |||
| 46 | // The translator's image dimensions; the values are its STORE_DIMS indices. A cube is a 2D array of six layers. | ||
| 47 | enum class ImageDimension : std::uint32_t { dim_2d = 0, dim_2d_array = 1, dim_3d = 2, dim_1d = 3 }; | ||
| 48 | |||
| 49 | // Storage views of one image by the component type a shader reads or writes it with (the translator picks by the | ||
| 50 | // T# format at run time). r32ui is for the atomic instructions, whose image type is R32ui (2D and 3D only). | ||
| 51 | // A null view leaves the fallback in that binding. | ||
| 52 | struct StorageViews { | ||
| 53 | VkImageView as_float = VK_NULL_HANDLE; | ||
| 54 | VkImageView as_uint = VK_NULL_HANDLE; | ||
| 55 | VkImageView as_sint = VK_NULL_HANDLE; | ||
| 56 | VkImageView r32ui = VK_NULL_HANDLE; | ||
| 57 | }; | ||
| 58 | |||
| 59 | struct BindlessFeatures { | ||
| 60 | bool update_after_bind = false; | ||
| 61 | bool partially_bound = false; | ||
| 62 | }; | ||
| 63 | |||
| 64 | namespace bindless { | ||
| 65 | |||
| 66 | struct Binding { | ||
| 67 | VkDescriptorType type; | ||
| 68 | std::uint32_t count; | ||
| 69 | }; | ||
| 70 | |||
| 71 | inline constexpr std::uint32_t table_binding = 2; | ||
| 72 | inline constexpr std::uint32_t sampler_binding = 1; | ||
| 73 | inline constexpr std::uint32_t storage_indices = 256; // what every storage array of bindings 8-19 holds | ||
| 74 | inline constexpr std::uint32_t no_binding = 0xffffffffu; | ||
| 75 | |||
| 76 | // Indexed by binding number. | ||
| 77 | inline constexpr std::array<Binding, 20> bindings = {{ | ||
| 78 | {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 4096}, {VK_DESCRIPTOR_TYPE_SAMPLER, 1024}, | ||
| 79 | {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1024}, | ||
| 80 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1024}, {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, | ||
| 81 | {VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 82 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 83 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 84 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 85 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 86 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 87 | {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, {VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 256}, | ||
| 88 | }}; | ||
| 89 | |||
| 90 | constexpr std::uint32_t sampled_binding(ImageDimension dimension) { | ||
| 91 | constexpr std::uint32_t by_dimension[] = {0, 3, 6, 5}; | ||
| 92 | return by_dimension[std::uint32_t(dimension)]; | ||
| 93 | } | ||
| 94 | |||
| 95 | // component_class: 0 = float, 1 = uint, 2 = sint. | ||
| 96 | constexpr std::uint32_t storage_binding(ImageDimension dimension, std::uint32_t component_class) { | ||
| 97 | return 8 + 3 * std::uint32_t(dimension) + component_class; | ||
| 98 | } | ||
| 99 | |||
| 100 | constexpr std::uint32_t r32ui_binding(ImageDimension dimension) { | ||
| 101 | return dimension == ImageDimension::dim_2d ? 4 : dimension == ImageDimension::dim_3d ? 7 : no_binding; | ||
| 102 | } | ||
| 103 | |||
| 104 | // How many images of a dimension the set holds, the fallback included. | ||
| 105 | constexpr std::uint32_t image_capacity(ImageDimension dimension) { return bindings[sampled_binding(dimension)].count; } | ||
| 106 | |||
| 107 | } // namespace bindless | ||
| 108 | |||
| 109 | class BindlessSet { | ||
| 110 | public: | ||
| 111 | BindlessSet(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceMemoryProperties& memory, | ||
| 112 | BindlessFeatures features = {}) | ||
| 113 | : device_(device), memory_(memory), features_(features) { | ||
| 114 | ok_ = vk_.load(get, device) && create_layout() && create_fallbacks() && create_table_buffer() && fill(); | ||
| 115 | } | ||
| 116 | |||
| 117 | ~BindlessSet() { | ||
| 118 | release_retired(); | ||
| 119 | if (table_buffer_.buffer) destroy(table_buffer_); | ||
| 120 | if (fallback_sampler_) vk_.vkDestroySampler(device_, fallback_sampler_, nullptr); | ||
| 121 | for (const VkImageView view : fallback_views_) vk_.vkDestroyImageView(device_, view, nullptr); | ||
| 122 | for (const auto& image : fallback_images_) { | ||
| 123 | if (image.image) vk_.vkDestroyImage(device_, image.image, nullptr); | ||
| 124 | if (image.memory) vk_.vkFreeMemory(device_, image.memory, nullptr); | ||
| 125 | } | ||
| 126 | if (pool_) vk_.vkDestroyDescriptorPool(device_, pool_, nullptr); // frees the set | ||
| 127 | if (layout_) vk_.vkDestroyDescriptorSetLayout(device_, layout_, nullptr); | ||
| 128 | } | ||
| 129 | |||
| 130 | BindlessSet(const BindlessSet&) = delete; | ||
| 131 | BindlessSet& operator=(const BindlessSet&) = delete; | ||
| 132 | |||
| 133 | bool ok() const { return ok_; } | ||
| 134 | // Set 0 of every pipeline layout made for translated shaders. | ||
| 135 | VkDescriptorSetLayout layout() const { return layout_; } | ||
| 136 | // Bound at set 0. The same set for the life of this object: flush() updates it in place. | ||
| 137 | VkDescriptorSet set() const { return set_; } | ||
| 138 | |||
| 139 | // The fallback images are created with undefined contents: record this once, in a command buffer that executes | ||
| 140 | // before anything uses the set. It moves them to GENERAL, the layout their descriptors name, and zeroes them. | ||
| 141 | bool fallbacks_ready() const { return fallbacks_ready_; } | ||
| 142 | void record_fallback_init(VkCommandBuffer command_buffer) { | ||
| 143 | fallbacks_ready_ = true; | ||
| 144 | std::vector<VkImageMemoryBarrier> barriers; | ||
| 145 | for (const auto& image : fallback_images_) { | ||
| 146 | VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER}; | ||
| 147 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 148 | barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | ||
| 149 | barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; | ||
| 150 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 151 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | ||
| 152 | barrier.image = image.image; | ||
| 153 | barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 154 | barriers.push_back(barrier); | ||
| 155 | } | ||
| 156 | vk_.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, | ||
| 157 | nullptr, 0, nullptr, std::uint32_t(barriers.size()), barriers.data()); | ||
| 158 | const VkClearColorValue zero{}; | ||
| 159 | const VkImageSubresourceRange whole{VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 160 | for (const auto& image : fallback_images_) | ||
| 161 | vk_.vkCmdClearColorImage(command_buffer, image.image, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &whole); | ||
| 162 | VkMemoryBarrier done{VK_STRUCTURE_TYPE_MEMORY_BARRIER}; | ||
| 163 | done.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | ||
| 164 | done.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT; | ||
| 165 | vk_.vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, | ||
| 166 | &done, 0, nullptr, 0, nullptr); | ||
| 167 | } | ||
| 168 | |||
| 169 | // Gives an image an index in its dimension's bindings: 0 when the dimension is full. `sampled` goes to the | ||
| 170 | // sampled array in `sampled_layout` (the layout the image is in when shaders read it); the storage views go to | ||
| 171 | // the storage arrays in GENERAL. The caller maps the T# to the index with insert_key(). Takes effect at flush(). | ||
| 172 | std::uint32_t add_image(ImageDimension dimension, VkImageView sampled, VkImageLayout sampled_layout, | ||
| 173 | const StorageViews& storage = {}) { | ||
| 174 | const bool stores = storage.as_float || storage.as_uint || storage.as_sint || storage.r32ui; | ||
| 175 | auto& used = image_used_[std::uint32_t(dimension)]; | ||
| 176 | const std::uint32_t low_end = std::min(std::uint32_t(used.size()), bindless::storage_indices); | ||
| 177 | std::uint32_t index = stores ? 0 : allocate(used, low_end, std::uint32_t(used.size())); | ||
| 178 | if (!index) index = allocate(used, 1, low_end); | ||
| 179 | if (index) write_image(dimension, index, sampled, sampled_layout, storage); | ||
| 180 | return index; | ||
| 181 | } | ||
| 182 | |||
| 183 | // New views or a new layout for an index add_image returned (a texture's state changed, say). | ||
| 184 | void update_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, | ||
| 185 | const StorageViews& storage = {}) { | ||
| 186 | const auto& used = image_used_[std::uint32_t(dimension)]; | ||
| 187 | if (index && index < used.size() && used[index]) write_image(dimension, index, sampled, sampled_layout, storage); | ||
| 188 | } | ||
| 189 | |||
| 190 | // Returns the index to the fallback and frees it. The caller removes the image's keys with erase_key() first. | ||
| 191 | void remove_image(ImageDimension dimension, std::uint32_t index) { | ||
| 192 | auto& used = image_used_[std::uint32_t(dimension)]; | ||
| 193 | if (!index || index >= used.size() || !used[index]) return; | ||
| 194 | write_image(dimension, index, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_GENERAL, {}); | ||
| 195 | used[index] = 0; | ||
| 196 | } | ||
| 197 | |||
| 198 | // 0 when the sampler array is full. | ||
| 199 | std::uint32_t add_sampler(VkSampler sampler) { | ||
| 200 | const std::uint32_t index = allocate(sampler_used_, 1, std::uint32_t(sampler_used_.size())); | ||
| 201 | if (index) write_sampler(index, sampler); | ||
| 202 | return index; | ||
| 203 | } | ||
| 204 | |||
| 205 | void remove_sampler(std::uint32_t index) { | ||
| 206 | if (!index || index >= sampler_used_.size() || !sampler_used_[index]) return; | ||
| 207 | write_sampler(index, fallback_sampler_); | ||
| 208 | sampler_used_[index] = 0; | ||
| 209 | } | ||
| 210 | |||
| 211 | // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders | ||
| 212 | // at flush(). | ||
| 213 | bool insert_key(const DescriptorKey& key, std::uint32_t index) { | ||
| 214 | table_dirty_ = true; | ||
| 215 | return table_.insert(key, index); | ||
| 216 | } | ||
| 217 | // Erasing compacts the key's probe cluster, so other keys' entries may change slots. | ||
| 218 | bool erase_key(const DescriptorKey& key) { | ||
| 219 | if (!table_.erase(key)) return false; | ||
| 220 | table_dirty_ = entries_moved_ = true; | ||
| 221 | return true; | ||
| 222 | } | ||
| 223 | const DescriptorTable& table() const { return table_; } | ||
| 224 | |||
| 225 | // True when the next flush() will move entries a running shader may be probing, or replace the table's | ||
| 226 | // buffer: the caller then flushes only once no command buffer that uses the set is still executing. Without | ||
| 227 | // it the flush only adds entries to the buffer in place, which a running lookup cannot see half done. | ||
| 228 | bool flush_needs_idle() const { | ||
| 229 | return table_dirty_ && (entries_moved_ || table_.size_bytes() > table_buffer_.size); | ||
| 230 | } | ||
| 231 | |||
| 232 | // Writes the pending descriptor changes and the table: before the commands that use the set are executed. | ||
| 233 | // A table that has grown gets a new buffer (binding 2 is rewritten); the old one is kept until | ||
| 234 | // release_retired(). False when that buffer cannot be made: the set then still has the table as of the last | ||
| 235 | // flush. | ||
| 236 | bool flush() { | ||
| 237 | bool written = true; | ||
| 238 | if (table_dirty_) { | ||
| 239 | const std::vector<std::uint32_t> words = table_.build_gpu_buffer(); | ||
| 240 | const VkDeviceSize bytes = words.size() * sizeof(std::uint32_t); | ||
| 241 | if (bytes > table_buffer_.size) { | ||
| 242 | TableBuffer larger; | ||
| 243 | if (create(larger, bytes)) { | ||
| 244 | retired_.push_back(table_buffer_); | ||
| 245 | table_buffer_ = larger; | ||
| 246 | write_table_binding(); | ||
| 247 | } else { | ||
| 248 | written = false; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | if (written) { | ||
| 252 | std::memcpy(table_buffer_.mapped, words.data(), bytes); | ||
| 253 | table_dirty_ = entries_moved_ = false; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | if (!pending_.empty()) { | ||
| 257 | std::vector<VkWriteDescriptorSet> writes(pending_.size()); | ||
| 258 | for (std::size_t i = 0; i < pending_.size(); ++i) { | ||
| 259 | writes[i] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 260 | writes[i].dstSet = set_; | ||
| 261 | writes[i].dstBinding = pending_[i].binding; | ||
| 262 | writes[i].dstArrayElement = pending_[i].index; | ||
| 263 | writes[i].descriptorCount = 1; | ||
| 264 | writes[i].descriptorType = bindless::bindings[pending_[i].binding].type; | ||
| 265 | writes[i].pImageInfo = &pending_[i].info; | ||
| 266 | } | ||
| 267 | vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); | ||
| 268 | pending_.clear(); | ||
| 269 | pending_slots_.clear(); | ||
| 270 | } | ||
| 271 | return written; | ||
| 272 | } | ||
| 273 | |||
| 274 | // Frees the buffers of tables that have since grown: when nothing submitted before the last flush() still runs. | ||
| 275 | void release_retired() { | ||
| 276 | for (auto& buffer : retired_) destroy(buffer); | ||
| 277 | retired_.clear(); | ||
| 278 | } | ||
| 279 | |||
| 280 | private: | ||
| 281 | struct Functions { | ||
| 282 | #define LEONIDA_BINDLESS_FUNCTIONS(X) \ | ||
| 283 | X(vkCreateDescriptorSetLayout) X(vkDestroyDescriptorSetLayout) X(vkCreateDescriptorPool) \ | ||
| 284 | X(vkDestroyDescriptorPool) X(vkAllocateDescriptorSets) X(vkUpdateDescriptorSets) X(vkCreateBuffer) \ | ||
| 285 | X(vkDestroyBuffer) X(vkGetBufferMemoryRequirements) X(vkBindBufferMemory) X(vkAllocateMemory) \ | ||
| 286 | X(vkFreeMemory) X(vkMapMemory) X(vkCreateImage) X(vkDestroyImage) X(vkGetImageMemoryRequirements) \ | ||
| 287 | X(vkBindImageMemory) X(vkCreateImageView) X(vkDestroyImageView) X(vkCreateSampler) X(vkDestroySampler) \ | ||
| 288 | X(vkCmdPipelineBarrier) X(vkCmdClearColorImage) | ||
| 289 | #define LEONIDA_BINDLESS_MEMBER(name) PFN_##name name = nullptr; | ||
| 290 | LEONIDA_BINDLESS_FUNCTIONS(LEONIDA_BINDLESS_MEMBER) | ||
| 291 | #undef LEONIDA_BINDLESS_MEMBER | ||
| 292 | |||
| 293 | bool load(PFN_vkGetDeviceProcAddr get, VkDevice device) { | ||
| 294 | bool all = true; | ||
| 295 | #define LEONIDA_BINDLESS_LOAD(name) \ | ||
| 296 | name = reinterpret_cast<PFN_##name>(get(device, #name)); \ | ||
| 297 | all = all && name != nullptr; | ||
| 298 | LEONIDA_BINDLESS_FUNCTIONS(LEONIDA_BINDLESS_LOAD) | ||
| 299 | #undef LEONIDA_BINDLESS_LOAD | ||
| 300 | #undef LEONIDA_BINDLESS_FUNCTIONS | ||
| 301 | return all; | ||
| 302 | } | ||
| 303 | }; | ||
| 304 | |||
| 305 | struct FallbackImage { | ||
| 306 | VkImage image = VK_NULL_HANDLE; | ||
| 307 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 308 | }; | ||
| 309 | |||
| 310 | struct TableBuffer { | ||
| 311 | VkBuffer buffer = VK_NULL_HANDLE; | ||
| 312 | VkDeviceMemory memory = VK_NULL_HANDLE; | ||
| 313 | void* mapped = nullptr; | ||
| 314 | std::size_t size = 0; | ||
| 315 | }; | ||
| 316 | |||
| 317 | struct Pending { | ||
| 318 | std::uint32_t binding; | ||
| 319 | std::uint32_t index; | ||
| 320 | VkDescriptorImageInfo info; | ||
| 321 | }; | ||
| 322 | |||
| 323 | static constexpr std::uint32_t dimensions = 4, classes = 3; | ||
| 324 | |||
| 325 | // The first free index in [begin, end), marked used; 0 when there is none. | ||
| 326 | static std::uint32_t allocate(std::vector<std::uint8_t>& used, std::uint32_t begin, std::uint32_t end) { | ||
| 327 | for (std::uint32_t i = begin; i < end; ++i) { | ||
| 328 | if (used[i]) continue; | ||
| 329 | used[i] = 1; | ||
| 330 | return i; | ||
| 331 | } | ||
| 332 | return 0; | ||
| 333 | } | ||
| 334 | |||
| 335 | std::uint32_t memory_type(std::uint32_t allowed, VkMemoryPropertyFlags wanted) const { | ||
| 336 | for (std::uint32_t i = 0; i < memory_.memoryTypeCount; ++i) | ||
| 337 | if ((allowed & (1u << i)) && (memory_.memoryTypes[i].propertyFlags & wanted) == wanted) return i; | ||
| 338 | return 0xffffffffu; | ||
| 339 | } | ||
| 340 | |||
| 341 | bool create_layout() { | ||
| 342 | std::array<VkDescriptorSetLayoutBinding, bindless::bindings.size()> entries{}; | ||
| 343 | std::array<VkDescriptorBindingFlags, bindless::bindings.size()> flags{}; | ||
| 344 | std::array<VkDescriptorPoolSize, bindless::bindings.size()> sizes{}; | ||
| 345 | for (std::uint32_t b = 0; b < entries.size(); ++b) { | ||
| 346 | entries[b] = {b, bindless::bindings[b].type, bindless::bindings[b].count, VK_SHADER_STAGE_ALL, nullptr}; | ||
| 347 | flags[b] = (features_.update_after_bind ? VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT : 0) | | ||
| 348 | (features_.partially_bound ? VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT : 0); | ||
| 349 | sizes[b] = {bindless::bindings[b].type, bindless::bindings[b].count}; | ||
| 350 | } | ||
| 351 | VkDescriptorSetLayoutBindingFlagsCreateInfo flags_info{ | ||
| 352 | VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO}; | ||
| 353 | flags_info.bindingCount = std::uint32_t(flags.size()); | ||
| 354 | flags_info.pBindingFlags = flags.data(); | ||
| 355 | VkDescriptorSetLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, &flags_info}; | ||
| 356 | layout_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT : 0; | ||
| 357 | layout_info.bindingCount = std::uint32_t(entries.size()); | ||
| 358 | layout_info.pBindings = entries.data(); | ||
| 359 | if (vk_.vkCreateDescriptorSetLayout(device_, &layout_info, nullptr, &layout_) != VK_SUCCESS) return false; | ||
| 360 | |||
| 361 | VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; | ||
| 362 | pool_info.flags = features_.update_after_bind ? VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT : 0; | ||
| 363 | pool_info.maxSets = 1; | ||
| 364 | pool_info.poolSizeCount = std::uint32_t(sizes.size()); | ||
| 365 | pool_info.pPoolSizes = sizes.data(); | ||
| 366 | if (vk_.vkCreateDescriptorPool(device_, &pool_info, nullptr, &pool_) != VK_SUCCESS) return false; | ||
| 367 | VkDescriptorSetAllocateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; | ||
| 368 | set_info.descriptorPool = pool_; | ||
| 369 | set_info.descriptorSetCount = 1; | ||
| 370 | set_info.pSetLayouts = &layout_; | ||
| 371 | if (vk_.vkAllocateDescriptorSets(device_, &set_info, &set_) != VK_SUCCESS) return false; | ||
| 372 | |||
| 373 | for (std::uint32_t d = 0; d < dimensions; ++d) { | ||
| 374 | image_used_[d].assign(bindless::image_capacity(ImageDimension(d)), 0); | ||
| 375 | image_used_[d][0] = 1; | ||
| 376 | } | ||
| 377 | sampler_used_.assign(bindless::bindings[bindless::sampler_binding].count, 0); | ||
| 378 | sampler_used_[0] = 1; | ||
| 379 | return true; | ||
| 380 | } | ||
| 381 | |||
| 382 | // One 1x1 image for each image type and component class, and the views of them every binding needs. | ||
| 383 | bool create_fallbacks() { | ||
| 384 | constexpr VkImageType types[] = {VK_IMAGE_TYPE_2D, VK_IMAGE_TYPE_3D, VK_IMAGE_TYPE_1D}; | ||
| 385 | constexpr VkFormat formats[classes] = {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R32_UINT, VK_FORMAT_R32_SINT}; | ||
| 386 | // dimension -> (index into types, view type) | ||
| 387 | constexpr std::uint32_t type_of[dimensions] = {0, 0, 1, 2}; | ||
| 388 | constexpr VkImageViewType view_types[dimensions] = {VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_VIEW_TYPE_2D_ARRAY, | ||
| 389 | VK_IMAGE_VIEW_TYPE_3D, VK_IMAGE_VIEW_TYPE_1D}; | ||
| 390 | for (std::uint32_t t = 0; t < 3; ++t) { | ||
| 391 | for (std::uint32_t c = 0; c < classes; ++c) { | ||
| 392 | VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; | ||
| 393 | info.imageType = types[t]; | ||
| 394 | info.format = formats[c]; | ||
| 395 | info.extent = {1, 1, 1}; | ||
| 396 | info.mipLevels = 1; | ||
| 397 | info.arrayLayers = 1; | ||
| 398 | info.samples = VK_SAMPLE_COUNT_1_BIT; | ||
| 399 | info.usage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | | ||
| 400 | (c == 0 ? VK_IMAGE_USAGE_SAMPLED_BIT : 0); | ||
| 401 | FallbackImage& image = fallback_images_[t * classes + c]; | ||
| 402 | if (vk_.vkCreateImage(device_, &info, nullptr, &image.image) != VK_SUCCESS) return false; | ||
| 403 | VkMemoryRequirements requirements{}; | ||
| 404 | vk_.vkGetImageMemoryRequirements(device_, image.image, &requirements); | ||
| 405 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 406 | allocation.allocationSize = requirements.size; | ||
| 407 | allocation.memoryTypeIndex = memory_type(requirements.memoryTypeBits, 0); | ||
| 408 | if (allocation.memoryTypeIndex == 0xffffffffu || | ||
| 409 | vk_.vkAllocateMemory(device_, &allocation, nullptr, &image.memory) != VK_SUCCESS || | ||
| 410 | vk_.vkBindImageMemory(device_, image.image, image.memory, 0) != VK_SUCCESS) | ||
| 411 | return false; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | for (std::uint32_t d = 0; d < dimensions; ++d) { | ||
| 415 | for (std::uint32_t c = 0; c < classes; ++c) { | ||
| 416 | VkImageViewCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; | ||
| 417 | info.image = fallback_images_[type_of[d] * classes + c].image; | ||
| 418 | info.viewType = view_types[d]; | ||
| 419 | info.format = formats[c]; | ||
| 420 | info.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}; | ||
| 421 | VkImageView view = VK_NULL_HANDLE; | ||
| 422 | if (vk_.vkCreateImageView(device_, &info, nullptr, &view) != VK_SUCCESS) return false; | ||
| 423 | fallback_views_.push_back(view); | ||
| 424 | fallback_view_[d][c] = view; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | VkSamplerCreateInfo sampler{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; | ||
| 428 | sampler.magFilter = sampler.minFilter = VK_FILTER_NEAREST; | ||
| 429 | sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; | ||
| 430 | sampler.addressModeU = sampler.addressModeV = sampler.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; | ||
| 431 | sampler.maxLod = VK_LOD_CLAMP_NONE; | ||
| 432 | return vk_.vkCreateSampler(device_, &sampler, nullptr, &fallback_sampler_) == VK_SUCCESS; | ||
| 433 | } | ||
| 434 | |||
| 435 | bool create(TableBuffer& out, std::size_t bytes) const { | ||
| 436 | VkBufferCreateInfo info{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; | ||
| 437 | info.size = bytes; | ||
| 438 | info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; | ||
| 439 | TableBuffer made; | ||
| 440 | made.size = bytes; | ||
| 441 | if (vk_.vkCreateBuffer(device_, &info, nullptr, &made.buffer) != VK_SUCCESS) return false; | ||
| 442 | VkMemoryRequirements requirements{}; | ||
| 443 | vk_.vkGetBufferMemoryRequirements(device_, made.buffer, &requirements); | ||
| 444 | VkMemoryAllocateInfo allocation{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; | ||
| 445 | allocation.allocationSize = requirements.size; | ||
| 446 | allocation.memoryTypeIndex = memory_type( | ||
| 447 | requirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); | ||
| 448 | const bool ready = allocation.memoryTypeIndex != 0xffffffffu && | ||
| 449 | vk_.vkAllocateMemory(device_, &allocation, nullptr, &made.memory) == VK_SUCCESS && | ||
| 450 | vk_.vkBindBufferMemory(device_, made.buffer, made.memory, 0) == VK_SUCCESS && | ||
| 451 | vk_.vkMapMemory(device_, made.memory, 0, VK_WHOLE_SIZE, 0, &made.mapped) == VK_SUCCESS; | ||
| 452 | if (!ready) { | ||
| 453 | destroy(made); | ||
| 454 | return false; | ||
| 455 | } | ||
| 456 | out = made; | ||
| 457 | return true; | ||
| 458 | } | ||
| 459 | |||
| 460 | void destroy(TableBuffer& buffer) const { | ||
| 461 | if (buffer.buffer) vk_.vkDestroyBuffer(device_, buffer.buffer, nullptr); | ||
| 462 | if (buffer.memory) vk_.vkFreeMemory(device_, buffer.memory, nullptr); // unmaps it | ||
| 463 | buffer = {}; | ||
| 464 | } | ||
| 465 | |||
| 466 | bool create_table_buffer() { | ||
| 467 | const std::vector<std::uint32_t> words = table_.build_gpu_buffer(); | ||
| 468 | const VkDeviceSize bytes = words.size() * sizeof(std::uint32_t); | ||
| 469 | if (!create(table_buffer_, bytes)) return false; | ||
| 470 | std::memcpy(table_buffer_.mapped, words.data(), bytes); | ||
| 471 | table_dirty_ = entries_moved_ = false; | ||
| 472 | return true; | ||
| 473 | } | ||
| 474 | |||
| 475 | void write_table_binding() const { | ||
| 476 | const VkDescriptorBufferInfo info{table_buffer_.buffer, 0, VK_WHOLE_SIZE}; | ||
| 477 | VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 478 | write.dstSet = set_; | ||
| 479 | write.dstBinding = bindless::table_binding; | ||
| 480 | write.descriptorCount = 1; | ||
| 481 | write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; | ||
| 482 | write.pBufferInfo = &info; | ||
| 483 | vk_.vkUpdateDescriptorSets(device_, 1, &write, 0, nullptr); | ||
| 484 | } | ||
| 485 | |||
| 486 | VkDescriptorImageInfo fallback_info(std::uint32_t binding) const { | ||
| 487 | if (binding == bindless::sampler_binding) return {fallback_sampler_, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED}; | ||
| 488 | for (std::uint32_t d = 0; d < dimensions; ++d) { | ||
| 489 | const ImageDimension dimension{d}; | ||
| 490 | std::uint32_t component_class = classes; // of the fallback view this binding takes | ||
| 491 | if (binding == bindless::sampled_binding(dimension)) component_class = 0; | ||
| 492 | if (binding == bindless::r32ui_binding(dimension)) component_class = 1; | ||
| 493 | for (std::uint32_t c = 0; c < classes; ++c) | ||
| 494 | if (binding == bindless::storage_binding(dimension, c)) component_class = c; | ||
| 495 | if (component_class < classes) | ||
| 496 | return {VK_NULL_HANDLE, fallback_view_[d][component_class], VK_IMAGE_LAYOUT_GENERAL}; | ||
| 497 | } | ||
| 498 | return {}; | ||
| 499 | } | ||
| 500 | |||
| 501 | // The table at binding 2 and the fallback at index 0 of every array, or in every element without partially_bound. | ||
| 502 | bool fill() { | ||
| 503 | write_table_binding(); | ||
| 504 | std::vector<std::vector<VkDescriptorImageInfo>> infos; | ||
| 505 | std::vector<VkWriteDescriptorSet> writes; | ||
| 506 | infos.reserve(bindless::bindings.size()); | ||
| 507 | for (std::uint32_t b = 0; b < bindless::bindings.size(); ++b) { | ||
| 508 | if (b == bindless::table_binding) continue; | ||
| 509 | const std::uint32_t count = features_.partially_bound ? 1 : bindless::bindings[b].count; | ||
| 510 | infos.emplace_back(count, fallback_info(b)); | ||
| 511 | VkWriteDescriptorSet write{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; | ||
| 512 | write.dstSet = set_; | ||
| 513 | write.dstBinding = b; | ||
| 514 | write.descriptorCount = count; | ||
| 515 | write.descriptorType = bindless::bindings[b].type; | ||
| 516 | write.pImageInfo = infos.back().data(); | ||
| 517 | writes.push_back(write); | ||
| 518 | } | ||
| 519 | vk_.vkUpdateDescriptorSets(device_, std::uint32_t(writes.size()), writes.data(), 0, nullptr); | ||
| 520 | return true; | ||
| 521 | } | ||
| 522 | |||
| 523 | // A slot has one pending write: a later one takes its place. So a view or sampler that is removed before the | ||
| 524 | // next flush(), and may be destroyed by then, is never written. | ||
| 525 | void queue(std::uint32_t binding, std::uint32_t index, const VkDescriptorImageInfo& info) { | ||
| 526 | const std::uint64_t slot = (std::uint64_t(binding) << 32) | index; | ||
| 527 | const auto [at, added] = pending_slots_.try_emplace(slot, pending_.size()); | ||
| 528 | if (added) | ||
| 529 | pending_.push_back({binding, index, info}); | ||
| 530 | else | ||
| 531 | pending_[at->second].info = info; | ||
| 532 | } | ||
| 533 | |||
| 534 | void write_sampler(std::uint32_t index, VkSampler sampler) { | ||
| 535 | queue(bindless::sampler_binding, index, {sampler, VK_NULL_HANDLE, VK_IMAGE_LAYOUT_UNDEFINED}); | ||
| 536 | } | ||
| 537 | |||
| 538 | // Queues every binding of the dimension at `index`: the given views, or the fallback where one is null. | ||
| 539 | void write_image(ImageDimension dimension, std::uint32_t index, VkImageView sampled, VkImageLayout sampled_layout, | ||
| 540 | const StorageViews& storage) { | ||
| 541 | const std::uint32_t sampled_at = bindless::sampled_binding(dimension); | ||
| 542 | queue(sampled_at, index, | ||
| 543 | sampled ? VkDescriptorImageInfo{VK_NULL_HANDLE, sampled, sampled_layout} : fallback_info(sampled_at)); | ||
| 544 | const auto store = [&](std::uint32_t binding, VkImageView view) { | ||
| 545 | if (binding == bindless::no_binding || index >= bindless::bindings[binding].count) return; | ||
| 546 | queue(binding, index, | ||
| 547 | view ? VkDescriptorImageInfo{VK_NULL_HANDLE, view, VK_IMAGE_LAYOUT_GENERAL} : fallback_info(binding)); | ||
| 548 | }; | ||
| 549 | store(bindless::storage_binding(dimension, 0), storage.as_float); | ||
| 550 | store(bindless::storage_binding(dimension, 1), storage.as_uint); | ||
| 551 | store(bindless::storage_binding(dimension, 2), storage.as_sint); | ||
| 552 | store(bindless::r32ui_binding(dimension), storage.r32ui); | ||
| 553 | } | ||
| 554 | |||
| 555 | VkDevice device_; | ||
| 556 | VkPhysicalDeviceMemoryProperties memory_; | ||
| 557 | BindlessFeatures features_; | ||
| 558 | Functions vk_; | ||
| 559 | bool ok_ = false; | ||
| 560 | VkDescriptorSetLayout layout_ = VK_NULL_HANDLE; | ||
| 561 | VkDescriptorPool pool_ = VK_NULL_HANDLE; | ||
| 562 | VkDescriptorSet set_ = VK_NULL_HANDLE; | ||
| 563 | std::array<FallbackImage, 3 * classes> fallback_images_{}; // [image type: 2D, 3D, 1D][class] | ||
| 564 | std::vector<VkImageView> fallback_views_; | ||
| 565 | VkImageView fallback_view_[dimensions][classes] = {}; | ||
| 566 | VkSampler fallback_sampler_ = VK_NULL_HANDLE; | ||
| 567 | bool fallbacks_ready_ = false; | ||
| 568 | std::array<std::vector<std::uint8_t>, dimensions> image_used_; | ||
| 569 | std::vector<std::uint8_t> sampler_used_; | ||
| 570 | std::vector<Pending> pending_; | ||
| 571 | std::unordered_map<std::uint64_t, std::size_t> pending_slots_; // binding and index -> place in pending_ | ||
| 572 | DescriptorTable table_{64}; | ||
| 573 | TableBuffer table_buffer_; | ||
| 574 | bool table_dirty_ = false; // the table differs from the buffer | ||
| 575 | bool entries_moved_ = false; // ... and not only by entries added | ||
| 576 | std::vector<TableBuffer> retired_; | ||
| 577 | }; | ||
| 578 | |||
| 579 | } // namespace leonida | ||