| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* Copyright 2024 Alyssa Rosenzweig |
| 4 |
* Copyright 2022-2023 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "hk_image_view.h" |
| 8 |
#include "util/format/u_format.h" |
| 9 |
#include "vulkan/vulkan_core.h" |
| 10 |
|
| 11 |
#include "agx_helpers.h" |
| 12 |
#include "agx_nir_texture.h" |
| 13 |
#include "agx_pack.h" |
| 14 |
#include "hk_device.h" |
| 15 |
#include "hk_entrypoints.h" |
| 16 |
#include "hk_image.h" |
| 17 |
#include "hk_physical_device.h" |
| 18 |
|
| 19 |
#include "layout.h" |
| 20 |
#include "vk_format.h" |
| 21 |
#include "vk_meta.h" |
| 22 |
|
| 23 |
enum hk_desc_usage { |
| 24 |
HK_DESC_USAGE_SAMPLED, |
| 25 |
HK_DESC_USAGE_STORAGE, |
| 26 |
HK_DESC_USAGE_INPUT, |
| 27 |
HK_DESC_USAGE_BG_EOT, |
| 28 |
HK_DESC_USAGE_LAYERED_BG_EOT, |
| 29 |
HK_DESC_USAGE_EMRT, |
| 30 |
}; |
| 31 |
|
| 32 |
static bool |
| 33 |
hk_image_view_type_is_array(VkImageViewType view_type) |
| 34 |
{ |
| 35 |
switch (view_type) { |
| 36 |
case VK_IMAGE_VIEW_TYPE_1D: |
| 37 |
case VK_IMAGE_VIEW_TYPE_2D: |
| 38 |
case VK_IMAGE_VIEW_TYPE_3D: |
| 39 |
case VK_IMAGE_VIEW_TYPE_CUBE: |
| 40 |
return false; |
| 41 |
|
| 42 |
case VK_IMAGE_VIEW_TYPE_1D_ARRAY: |
| 43 |
case VK_IMAGE_VIEW_TYPE_2D_ARRAY: |
| 44 |
case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: |
| 45 |
return true; |
| 46 |
|
| 47 |
default: |
| 48 |
UNREACHABLE("Invalid image view type"); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static enum agx_texture_dimension |
| 53 |
translate_image_view_type(VkImageViewType view_type, bool msaa, bool layered, |
| 54 |
enum hk_desc_usage usage) |
| 55 |
{ |
| 56 |
if (usage == HK_DESC_USAGE_EMRT || usage == HK_DESC_USAGE_INPUT || |
| 57 |
(usage == HK_DESC_USAGE_LAYERED_BG_EOT && layered)) { |
| 58 |
return msaa ? AGX_TEXTURE_DIMENSION_2D_ARRAY_MULTISAMPLED |
| 59 |
: AGX_TEXTURE_DIMENSION_2D_ARRAY; |
| 60 |
} |
| 61 |
|
| 62 |
/* For background/EOT, we ignore the application-provided view type */ |
| 63 |
if (usage == HK_DESC_USAGE_BG_EOT || usage == HK_DESC_USAGE_LAYERED_BG_EOT) { |
| 64 |
return msaa ? AGX_TEXTURE_DIMENSION_2D_MULTISAMPLED |
| 65 |
: AGX_TEXTURE_DIMENSION_2D; |
| 66 |
} |
| 67 |
|
| 68 |
bool cubes_to_2d = usage != HK_DESC_USAGE_SAMPLED; |
| 69 |
|
| 70 |
switch (view_type) { |
| 71 |
case VK_IMAGE_VIEW_TYPE_1D: |
| 72 |
case VK_IMAGE_VIEW_TYPE_2D: |
| 73 |
return msaa ? AGX_TEXTURE_DIMENSION_2D_MULTISAMPLED |
| 74 |
: AGX_TEXTURE_DIMENSION_2D; |
| 75 |
|
| 76 |
case VK_IMAGE_VIEW_TYPE_1D_ARRAY: |
| 77 |
case VK_IMAGE_VIEW_TYPE_2D_ARRAY: |
| 78 |
return msaa ? AGX_TEXTURE_DIMENSION_2D_ARRAY_MULTISAMPLED |
| 79 |
: AGX_TEXTURE_DIMENSION_2D_ARRAY; |
| 80 |
|
| 81 |
case VK_IMAGE_VIEW_TYPE_3D: |
| 82 |
assert(!msaa); |
| 83 |
return AGX_TEXTURE_DIMENSION_3D; |
| 84 |
|
| 85 |
case VK_IMAGE_VIEW_TYPE_CUBE: |
| 86 |
assert(!msaa); |
| 87 |
return cubes_to_2d ? AGX_TEXTURE_DIMENSION_2D_ARRAY |
| 88 |
: AGX_TEXTURE_DIMENSION_CUBE; |
| 89 |
|
| 90 |
case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: |
| 91 |
assert(!msaa); |
| 92 |
return cubes_to_2d ? AGX_TEXTURE_DIMENSION_2D_ARRAY |
| 93 |
: AGX_TEXTURE_DIMENSION_CUBE_ARRAY; |
| 94 |
|
| 95 |
default: |
| 96 |
UNREACHABLE("Invalid image view type"); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
static enum pipe_swizzle |
| 101 |
vk_swizzle_to_pipe(VkComponentSwizzle swizzle) |
| 102 |
{ |
| 103 |
switch (swizzle) { |
| 104 |
case VK_COMPONENT_SWIZZLE_R: |
| 105 |
return PIPE_SWIZZLE_X; |
| 106 |
case VK_COMPONENT_SWIZZLE_G: |
| 107 |
return PIPE_SWIZZLE_Y; |
| 108 |
case VK_COMPONENT_SWIZZLE_B: |
| 109 |
return PIPE_SWIZZLE_Z; |
| 110 |
case VK_COMPONENT_SWIZZLE_A: |
| 111 |
return PIPE_SWIZZLE_W; |
| 112 |
case VK_COMPONENT_SWIZZLE_ONE: |
| 113 |
return PIPE_SWIZZLE_1; |
| 114 |
case VK_COMPONENT_SWIZZLE_ZERO: |
| 115 |
return PIPE_SWIZZLE_0; |
| 116 |
default: |
| 117 |
UNREACHABLE("Invalid component swizzle"); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
static enum pipe_format |
| 122 |
get_stencil_format(enum pipe_format format) |
| 123 |
{ |
| 124 |
/* Note: R8 is not a stencil format, but our copy code will internally cast |
| 125 |
* between R8 and S8. It's simplest to handle here. |
| 126 |
*/ |
| 127 |
switch (format) { |
| 128 |
case PIPE_FORMAT_R8_UINT: |
| 129 |
case PIPE_FORMAT_S8_UINT: |
| 130 |
return PIPE_FORMAT_S8_UINT; |
| 131 |
case PIPE_FORMAT_Z24_UNORM_S8_UINT: |
| 132 |
return PIPE_FORMAT_X24S8_UINT; |
| 133 |
case PIPE_FORMAT_S8_UINT_Z24_UNORM: |
| 134 |
return PIPE_FORMAT_S8X24_UINT; |
| 135 |
case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: |
| 136 |
return PIPE_FORMAT_X32_S8X24_UINT; |
| 137 |
default: |
| 138 |
UNREACHABLE("Unsupported depth/stencil format"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
struct hk_3d { |
| 143 |
unsigned x, y, z; |
| 144 |
}; |
| 145 |
|
| 146 |
static struct hk_3d |
| 147 |
view_denominator(struct hk_image_view *view) |
| 148 |
{ |
| 149 |
enum pipe_format view_format = hk_format_to_pipe_format(view->vk.format); |
| 150 |
enum pipe_format img_format = |
| 151 |
hk_format_to_pipe_format(view->vk.image->format); |
| 152 |
|
| 153 |
if (util_format_is_compressed(view_format)) { |
| 154 |
/* |
| 155 |
* We can do an uncompressed view of a compressed image but not the other |
| 156 |
* way around. |
| 157 |
*/ |
| 158 |
assert(util_format_is_compressed(img_format)); |
| 159 |
assert(util_format_get_blockwidth(img_format) == |
| 160 |
util_format_get_blockwidth(view_format)); |
| 161 |
assert(util_format_get_blockheight(img_format) == |
| 162 |
util_format_get_blockheight(view_format)); |
| 163 |
assert(util_format_get_blockdepth(img_format) == |
| 164 |
util_format_get_blockdepth(view_format)); |
| 165 |
|
| 166 |
return (struct hk_3d){1, 1, 1}; |
| 167 |
} |
| 168 |
|
| 169 |
if (!util_format_is_compressed(img_format)) { |
| 170 |
/* Both formats uncompressed */ |
| 171 |
return (struct hk_3d){1, 1, 1}; |
| 172 |
} |
| 173 |
|
| 174 |
/* Else, img is compressed but view is not */ |
| 175 |
return (struct hk_3d){ |
| 176 |
util_format_get_blockwidth(img_format), |
| 177 |
util_format_get_blockheight(img_format), |
| 178 |
util_format_get_blockdepth(img_format), |
| 179 |
}; |
| 180 |
} |
| 181 |
|
| 182 |
static enum pipe_format |
| 183 |
format_for_plane(struct hk_image_view *view, unsigned view_plane) |
| 184 |
{ |
| 185 |
const struct vk_format_ycbcr_info *ycbcr_info = |
| 186 |
vk_format_get_ycbcr_info(view->vk.format); |
| 187 |
|
| 188 |
assert(ycbcr_info || view_plane == 0); |
| 189 |
VkFormat plane_format = |
| 190 |
ycbcr_info ? ycbcr_info->planes[view_plane].format : view->vk.format; |
| 191 |
|
| 192 |
enum pipe_format p_format = hk_format_to_pipe_format(plane_format); |
| 193 |
if (view->vk.aspects == VK_IMAGE_ASPECT_STENCIL_BIT) |
| 194 |
p_format = get_stencil_format(p_format); |
| 195 |
|
| 196 |
return p_format; |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
pack_texture(struct hk_image_view *view, unsigned view_plane, |
| 201 |
enum hk_desc_usage usage, struct agx_texture_packed *out) |
| 202 |
{ |
| 203 |
struct hk_image *image = container_of(view->vk.image, struct hk_image, vk); |
| 204 |
const uint8_t image_plane = view->planes[view_plane].image_plane; |
| 205 |
struct hk_image_plane *plane = &image->planes[image_plane]; |
| 206 |
struct ail_layout *layout = &plane->layout; |
| 207 |
uint64_t base_addr = hk_image_base_address(image, image_plane); |
| 208 |
|
| 209 |
bool cubes_to_2d = usage != HK_DESC_USAGE_SAMPLED; |
| 210 |
|
| 211 |
unsigned level = view->vk.base_mip_level; |
| 212 |
unsigned layer = view->vk.base_array_layer; |
| 213 |
|
| 214 |
enum pipe_format p_format = format_for_plane(view, view_plane); |
| 215 |
const struct util_format_description *desc = |
| 216 |
util_format_description(p_format); |
| 217 |
|
| 218 |
struct hk_3d denom = view_denominator(view); |
| 219 |
|
| 220 |
uint8_t format_swizzle[4] = { |
| 221 |
desc->swizzle[0], |
| 222 |
desc->swizzle[1], |
| 223 |
desc->swizzle[2], |
| 224 |
desc->swizzle[3], |
| 225 |
}; |
| 226 |
|
| 227 |
/* Different APIs have different depth/stencil swizzle rules. Vulkan expects |
| 228 |
* R001 behaviour, override here because Mesa's format table is not that. |
| 229 |
*/ |
| 230 |
if (util_format_is_depth_or_stencil(p_format)) { |
| 231 |
format_swizzle[0] = PIPE_SWIZZLE_X; |
| 232 |
format_swizzle[1] = PIPE_SWIZZLE_0; |
| 233 |
format_swizzle[2] = PIPE_SWIZZLE_0; |
| 234 |
format_swizzle[3] = PIPE_SWIZZLE_1; |
| 235 |
} |
| 236 |
|
| 237 |
/* We only have a single swizzle for the user swizzle and the format |
| 238 |
* fixup, so compose them now. |
| 239 |
*/ |
| 240 |
uint8_t out_swizzle[4]; |
| 241 |
uint8_t view_swizzle[4] = { |
| 242 |
vk_swizzle_to_pipe(view->vk.swizzle.r), |
| 243 |
vk_swizzle_to_pipe(view->vk.swizzle.g), |
| 244 |
vk_swizzle_to_pipe(view->vk.swizzle.b), |
| 245 |
vk_swizzle_to_pipe(view->vk.swizzle.a), |
| 246 |
}; |
| 247 |
|
| 248 |
unsigned layers = view->vk.layer_count; |
| 249 |
if (view->vk.view_type == VK_IMAGE_VIEW_TYPE_3D) { |
| 250 |
layers = DIV_ROUND_UP(layout->depth_px, denom.z); |
| 251 |
} else if (!cubes_to_2d && |
| 252 |
(view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE || |
| 253 |
view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)) { |
| 254 |
|
| 255 |
layers /= 6; |
| 256 |
} |
| 257 |
|
| 258 |
util_format_compose_swizzles(format_swizzle, view_swizzle, out_swizzle); |
| 259 |
|
| 260 |
agx_pack(out, TEXTURE, cfg) { |
| 261 |
cfg.dimension = translate_image_view_type( |
| 262 |
view->vk.view_type, view->vk.image->samples > 1, layers > 1, usage); |
| 263 |
cfg.layout = agx_translate_layout(layout->tiling); |
| 264 |
cfg.channels = ail_pixel_format[p_format].channels; |
| 265 |
cfg.type = ail_pixel_format[p_format].type; |
| 266 |
cfg.srgb = util_format_is_srgb(p_format); |
| 267 |
|
| 268 |
cfg.swizzle_r = agx_channel_from_pipe(out_swizzle[0]); |
| 269 |
cfg.swizzle_g = agx_channel_from_pipe(out_swizzle[1]); |
| 270 |
cfg.swizzle_b = agx_channel_from_pipe(out_swizzle[2]); |
| 271 |
cfg.swizzle_a = agx_channel_from_pipe(out_swizzle[3]); |
| 272 |
|
| 273 |
if (denom.x > 1) { |
| 274 |
assert(view->vk.level_count == 1); |
| 275 |
assert(view->vk.layer_count == 1); |
| 276 |
|
| 277 |
cfg.address = base_addr + ail_get_layer_level_B(layout, layer, level); |
| 278 |
cfg.width = DIV_ROUND_UP(u_minify(layout->width_px, level), denom.x); |
| 279 |
cfg.height = DIV_ROUND_UP(u_minify(layout->height_px, level), denom.y); |
| 280 |
cfg.first_level = 0; |
| 281 |
cfg.last_level = 1; |
| 282 |
} else { |
| 283 |
cfg.address = base_addr + ail_get_layer_offset_B(layout, layer); |
| 284 |
cfg.width = layout->width_px; |
| 285 |
cfg.height = layout->height_px; |
| 286 |
cfg.first_level = level; |
| 287 |
cfg.last_level = level + view->vk.level_count - 1; |
| 288 |
} |
| 289 |
|
| 290 |
/* To implement sparse resident textures, the hardware texture descriptor |
| 291 |
* can instead point to a secondary page table controlled in userspace. |
| 292 |
* This allows remapping pages and - crucially - disabling unmapped pages |
| 293 |
* to read zero and report non-resident with shader residency queries. |
| 294 |
* When we have a sparse map, we need to point to it here. |
| 295 |
* |
| 296 |
* However, there's a wrinkle: when handling uncompressed views of |
| 297 |
* compressed images in the above code, we need to offset the image |
| 298 |
* address to point to the specific mip level rather than use the hardware |
| 299 |
* "first level" field. This ensures the layouts are consistent despite us |
| 300 |
* munging the image dimensions. In that case, we need to also offset the |
| 301 |
* sparse page table accordingly. Of course, the sparse page table is in |
| 302 |
* terms of pages, so this trick only works when the mip level is |
| 303 |
* page-aligned. |
| 304 |
* |
| 305 |
* However, if the mip level is NOT page-aligned, it is in the mip tail by |
| 306 |
* definition. As the mip tail is always resident, there is no need for a |
| 307 |
* sparse page table. So either: |
| 308 |
* |
| 309 |
* 1. We are in the mip tail and don't need a sparse map, or |
| 310 |
* 2. We are not but the level is page-aligned in the sparse map. |
| 311 |
* |
| 312 |
* Either way we're okay. |
| 313 |
*/ |
| 314 |
if (plane->sparse_map && level < layout->mip_tail_first_lod) { |
| 315 |
unsigned page = 0; |
| 316 |
if (denom.x > 1) { |
| 317 |
page = ail_bytes_to_pages(layout->level_offsets_B[level]); |
| 318 |
} |
| 319 |
|
| 320 |
cfg.mode = AGX_IMAGE_MODE_SPARSE; |
| 321 |
cfg.address = plane->sparse_map->va->addr + |
| 322 |
ail_page_to_sparse_index_el(layout, layer, page) * |
| 323 |
AIL_SPARSE_ELSIZE_B; |
| 324 |
} |
| 325 |
|
| 326 |
cfg.srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB); |
| 327 |
cfg.unk_mipmapped = layout->levels > 1; |
| 328 |
cfg.srgb_2_channel = cfg.srgb && util_format_colormask(desc) == 0x3; |
| 329 |
|
| 330 |
if (layout->compressed) { |
| 331 |
cfg.compressed = true; |
| 332 |
cfg.extended = true; |
| 333 |
} |
| 334 |
|
| 335 |
if (layout->compressed) { |
| 336 |
cfg.acceleration_buffer = base_addr + layout->metadata_offset_B + |
| 337 |
(layer * layout->compression_layer_stride_B); |
| 338 |
} |
| 339 |
|
| 340 |
if (layout->tiling == AIL_TILING_LINEAR && |
| 341 |
(hk_image_view_type_is_array(view->vk.view_type))) { |
| 342 |
|
| 343 |
cfg.depth_linear = layers; |
| 344 |
cfg.layer_stride_linear = layout->layer_stride_B - 0x80; |
| 345 |
cfg.extended = true; |
| 346 |
} else { |
| 347 |
assert((layout->tiling != AIL_TILING_LINEAR) || (layers == 1)); |
| 348 |
cfg.depth = layers; |
| 349 |
} |
| 350 |
|
| 351 |
if (view->vk.image->samples > 1) { |
| 352 |
cfg.samples = agx_translate_sample_count(view->vk.image->samples); |
| 353 |
} |
| 354 |
|
| 355 |
if (layout->tiling == AIL_TILING_LINEAR) { |
| 356 |
cfg.stride = ail_get_linear_stride_B(layout, 0) - 16; |
| 357 |
} else { |
| 358 |
cfg.page_aligned_layers = layout->page_aligned_layers; |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
static void |
| 364 |
pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, |
| 365 |
enum hk_desc_usage usage, struct agx_pbe_packed *out) |
| 366 |
{ |
| 367 |
struct hk_image *image = container_of(view->vk.image, struct hk_image, vk); |
| 368 |
const uint8_t image_plane = view->planes[view_plane].image_plane; |
| 369 |
struct ail_layout *layout = &image->planes[image_plane].layout; |
| 370 |
uint64_t base_addr = hk_image_base_address(image, image_plane); |
| 371 |
|
| 372 |
unsigned level = view->vk.base_mip_level; |
| 373 |
unsigned layer = view->vk.base_array_layer; |
| 374 |
|
| 375 |
enum pipe_format p_format = format_for_plane(view, view_plane); |
| 376 |
const struct util_format_description *desc = |
| 377 |
util_format_description(p_format); |
| 378 |
|
| 379 |
bool eot = |
| 380 |
usage == HK_DESC_USAGE_BG_EOT || usage == HK_DESC_USAGE_LAYERED_BG_EOT; |
| 381 |
|
| 382 |
/* The tilebuffer is already in sRGB space if needed. Do not convert for |
| 383 |
* end-of-tile descriptors. |
| 384 |
*/ |
| 385 |
if (eot) |
| 386 |
p_format = util_format_linear(p_format); |
| 387 |
|
| 388 |
bool msaa = view->vk.image->samples > 1; |
| 389 |
struct hk_3d denom = view_denominator(view); |
| 390 |
|
| 391 |
unsigned layers = view->vk.view_type == VK_IMAGE_VIEW_TYPE_3D |
| 392 |
? image->vk.extent.depth |
| 393 |
: view->vk.layer_count; |
| 394 |
|
| 395 |
agx_pack(out, PBE, cfg) { |
| 396 |
cfg.dimension = |
| 397 |
translate_image_view_type(view->vk.view_type, msaa, layers > 1, usage); |
| 398 |
cfg.layout = agx_translate_layout(layout->tiling); |
| 399 |
cfg.channels = ail_pixel_format[p_format].channels; |
| 400 |
cfg.type = ail_pixel_format[p_format].type; |
| 401 |
cfg.srgb = util_format_is_srgb(p_format); |
| 402 |
|
| 403 |
assert(desc->nr_channels >= 1 && desc->nr_channels <= 4); |
| 404 |
|
| 405 |
for (unsigned i = 0; i < desc->nr_channels; ++i) { |
| 406 |
if (desc->swizzle[i] == 0) |
| 407 |
cfg.swizzle_r = i; |
| 408 |
else if (desc->swizzle[i] == 1) |
| 409 |
cfg.swizzle_g = i; |
| 410 |
else if (desc->swizzle[i] == 2) |
| 411 |
cfg.swizzle_b = i; |
| 412 |
else if (desc->swizzle[i] == 3) |
| 413 |
cfg.swizzle_a = i; |
| 414 |
} |
| 415 |
|
| 416 |
cfg.buffer = base_addr + ail_get_layer_offset_B(layout, layer); |
| 417 |
cfg.unk_mipmapped = layout->levels > 1; |
| 418 |
|
| 419 |
if (msaa & !eot) { |
| 420 |
/* Multisampled images are bound like buffer textures, with |
| 421 |
* addressing arithmetic to determine the texel to write. |
| 422 |
* |
| 423 |
* Note that the end-of-tile program uses real multisample images |
| 424 |
* with image_write_block instructions. |
| 425 |
*/ |
| 426 |
unsigned blocksize_B = util_format_get_blocksize(p_format); |
| 427 |
unsigned size_px = |
| 428 |
(layout->size_B - layout->layer_stride_B * layer) / blocksize_B; |
| 429 |
|
| 430 |
cfg.dimension = AGX_TEXTURE_DIMENSION_2D; |
| 431 |
cfg.layout = AGX_LAYOUT_LINEAR; |
| 432 |
cfg.width = AGX_TEXTURE_BUFFER_WIDTH; |
| 433 |
cfg.height = DIV_ROUND_UP(size_px, cfg.width); |
| 434 |
cfg.stride = (cfg.width * blocksize_B) - 4; |
| 435 |
cfg.layers = 1; |
| 436 |
cfg.levels = 1; |
| 437 |
|
| 438 |
cfg.buffer += layout->level_offsets_B[level]; |
| 439 |
cfg.level = 0; |
| 440 |
} else { |
| 441 |
if (denom.x > 1) { |
| 442 |
assert(denom.z == 1 && "todo how to handle?"); |
| 443 |
assert(view->vk.level_count == 1); |
| 444 |
assert(view->vk.layer_count == 1); |
| 445 |
|
| 446 |
cfg.buffer = |
| 447 |
base_addr + ail_get_layer_level_B(layout, layer, level); |
| 448 |
cfg.width = |
| 449 |
DIV_ROUND_UP(u_minify(layout->width_px, level), denom.x); |
| 450 |
cfg.height = |
| 451 |
DIV_ROUND_UP(u_minify(layout->height_px, level), denom.y); |
| 452 |
cfg.level = 0; |
| 453 |
} else { |
| 454 |
cfg.buffer = base_addr + ail_get_layer_offset_B(layout, layer); |
| 455 |
cfg.width = layout->width_px; |
| 456 |
cfg.height = layout->height_px; |
| 457 |
cfg.level = level; |
| 458 |
} |
| 459 |
|
| 460 |
if (layout->tiling == AIL_TILING_LINEAR && |
| 461 |
(hk_image_view_type_is_array(view->vk.view_type))) { |
| 462 |
|
| 463 |
cfg.depth_linear = layers; |
| 464 |
cfg.layer_stride_linear = (layout->layer_stride_B - 0x80); |
| 465 |
cfg.extended = true; |
| 466 |
} else { |
| 467 |
assert((layout->tiling != AIL_TILING_LINEAR) || (layers == 1)); |
| 468 |
cfg.layers = layers; |
| 469 |
} |
| 470 |
|
| 471 |
cfg.levels = image->vk.mip_levels; |
| 472 |
|
| 473 |
if (layout->tiling == AIL_TILING_LINEAR) { |
| 474 |
cfg.stride = ail_get_linear_stride_B(layout, level) - 4; |
| 475 |
assert(cfg.levels == 1); |
| 476 |
} else { |
| 477 |
cfg.page_aligned_layers = layout->page_aligned_layers; |
| 478 |
} |
| 479 |
|
| 480 |
if (image->vk.samples > 1) |
| 481 |
cfg.samples = agx_translate_sample_count(image->vk.samples); |
| 482 |
} |
| 483 |
|
| 484 |
if (layout->compressed && usage != HK_DESC_USAGE_EMRT) { |
| 485 |
cfg.compressed = true; |
| 486 |
cfg.extended = true; |
| 487 |
|
| 488 |
cfg.acceleration_buffer = base_addr + layout->metadata_offset_B + |
| 489 |
(layer * layout->compression_layer_stride_B); |
| 490 |
} |
| 491 |
|
| 492 |
/* When the descriptor isn't extended architecturally, we use |
| 493 |
* the last 8 bytes as a sideband to accelerate image atomics. |
| 494 |
*/ |
| 495 |
if (!cfg.extended && |
| 496 |
(layout->writeable_image || usage == HK_DESC_USAGE_EMRT)) { |
| 497 |
|
| 498 |
if (msaa) { |
| 499 |
assert(denom.x == 1 && "no MSAA of block-compressed"); |
| 500 |
|
| 501 |
cfg.aligned_width_msaa_sw = |
| 502 |
align(u_minify(layout->width_px, level), |
| 503 |
layout->tilesize_el[level].width_el); |
| 504 |
|
| 505 |
cfg.sample_count_log2_sw = util_logbase2(image->vk.samples); |
| 506 |
} else { |
| 507 |
cfg.level_offset_sw = ail_get_level_offset_B(layout, cfg.level); |
| 508 |
} |
| 509 |
|
| 510 |
if (layout->tiling != AIL_TILING_LINEAR) { |
| 511 |
struct ail_tile tile_size = layout->tilesize_el[level]; |
| 512 |
cfg.tile_width_sw = tile_size.width_el; |
| 513 |
cfg.tile_height_sw = tile_size.height_el; |
| 514 |
|
| 515 |
cfg.layer_stride_sw = layout->layer_stride_B; |
| 516 |
} |
| 517 |
} |
| 518 |
}; |
| 519 |
} |
| 520 |
|
| 521 |
static VkResult |
| 522 |
hk_image_view_init(struct hk_device *dev, struct hk_image_view *view, |
| 523 |
const VkImageViewCreateInfo *pCreateInfo) |
| 524 |
{ |
| 525 |
VK_FROM_HANDLE(hk_image, image, pCreateInfo->image); |
| 526 |
memset(view, 0, sizeof(*view)); |
| 527 |
|
| 528 |
vk_image_view_init(&dev->vk, &view->vk, pCreateInfo); |
| 529 |
|
| 530 |
/* First, figure out which image planes we need. For depth/stencil, we only |
| 531 |
* have one aspect viewed at a time. |
| 532 |
*/ |
| 533 |
if (image->vk.aspects & |
| 534 |
(VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) { |
| 535 |
|
| 536 |
view->plane_count = 1; |
| 537 |
view->planes[0].image_plane = |
| 538 |
hk_image_aspects_to_plane(image, view->vk.aspects); |
| 539 |
} else { |
| 540 |
/* For other formats, retrieve the plane count from the aspect mask |
| 541 |
* and then walk through the aspect mask to map each image plane |
| 542 |
* to its corresponding view plane |
| 543 |
*/ |
| 544 |
assert(util_bitcount(view->vk.aspects) == |
| 545 |
vk_format_get_plane_count(view->vk.format)); |
| 546 |
view->plane_count = 0; |
| 547 |
u_foreach_bit(aspect_bit, view->vk.aspects) { |
| 548 |
view->planes[view->plane_count++].image_plane = |
| 549 |
hk_image_aspects_to_plane(image, 1u << aspect_bit); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
/* Fill in each view plane separately */ |
| 554 |
for (unsigned p = 0; p < view->plane_count; p++) { |
| 555 |
pack_texture(view, p, HK_DESC_USAGE_SAMPLED, &view->planes[p].sampled); |
| 556 |
pack_texture(view, p, HK_DESC_USAGE_STORAGE, &view->planes[p].ro_storage); |
| 557 |
pack_texture(view, p, HK_DESC_USAGE_INPUT, &view->planes[p].ia); |
| 558 |
pack_texture(view, p, HK_DESC_USAGE_EMRT, &view->planes[p].emrt_texture); |
| 559 |
pack_texture(view, p, HK_DESC_USAGE_BG_EOT, &view->planes[p].background); |
| 560 |
pack_texture(view, p, HK_DESC_USAGE_LAYERED_BG_EOT, |
| 561 |
&view->planes[p].layered_background); |
| 562 |
|
| 563 |
pack_pbe(dev, view, p, HK_DESC_USAGE_STORAGE, &view->planes[p].storage); |
| 564 |
pack_pbe(dev, view, p, HK_DESC_USAGE_BG_EOT, &view->planes[p].eot); |
| 565 |
pack_pbe(dev, view, p, HK_DESC_USAGE_EMRT, &view->planes[p].emrt_pbe); |
| 566 |
pack_pbe(dev, view, p, HK_DESC_USAGE_LAYERED_BG_EOT, |
| 567 |
&view->planes[p].layered_eot); |
| 568 |
} |
| 569 |
|
| 570 |
return VK_SUCCESS; |
| 571 |
} |
| 572 |
|
| 573 |
VKAPI_ATTR void VKAPI_CALL |
| 574 |
hk_DestroyImageView(VkDevice _device, VkImageView imageView, |
| 575 |
const VkAllocationCallbacks *pAllocator) |
| 576 |
{ |
| 577 |
VK_FROM_HANDLE(hk_device, dev, _device); |
| 578 |
VK_FROM_HANDLE(hk_image_view, view, imageView); |
| 579 |
|
| 580 |
if (!view) |
| 581 |
return; |
| 582 |
|
| 583 |
vk_image_view_finish(&view->vk); |
| 584 |
vk_free2(&dev->vk.alloc, pAllocator, view); |
| 585 |
} |
| 586 |
|
| 587 |
VKAPI_ATTR VkResult VKAPI_CALL |
| 588 |
hk_CreateImageView(VkDevice _device, const VkImageViewCreateInfo *pCreateInfo, |
| 589 |
const VkAllocationCallbacks *pAllocator, VkImageView *pView) |
| 590 |
{ |
| 591 |
VK_FROM_HANDLE(hk_device, dev, _device); |
| 592 |
struct hk_image_view *view; |
| 593 |
VkResult result; |
| 594 |
|
| 595 |
view = vk_alloc2(&dev->vk.alloc, pAllocator, sizeof(*view), 8, |
| 596 |
VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); |
| 597 |
if (!view) |
| 598 |
return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY); |
| 599 |
|
| 600 |
result = hk_image_view_init(dev, view, pCreateInfo); |
| 601 |
if (result != VK_SUCCESS) { |
| 602 |
hk_DestroyImageView(_device, hk_image_view_to_handle(view), pAllocator); |
| 603 |
return result; |
| 604 |
} |
| 605 |
|
| 606 |
*pView = hk_image_view_to_handle(view); |
| 607 |
|
| 608 |
return VK_SUCCESS; |
| 609 |
} |