radv: Clamp gfx9 image view extents to the allocated image extents.
[mesa.git] / src / amd / vulkan / radv_image.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * IN THE SOFTWARE.
26 */
27
28 #include "radv_debug.h"
29 #include "radv_private.h"
30 #include "vk_format.h"
31 #include "vk_util.h"
32 #include "radv_radeon_winsys.h"
33 #include "sid.h"
34 #include "gfx9d.h"
35 #include "util/debug.h"
36 #include "util/u_atomic.h"
37 static unsigned
38 radv_choose_tiling(struct radv_device *device,
39 const struct radv_image_create_info *create_info)
40 {
41 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
42
43 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) {
44 assert(pCreateInfo->samples <= 1);
45 return RADEON_SURF_MODE_LINEAR_ALIGNED;
46 }
47
48 if (!vk_format_is_compressed(pCreateInfo->format) &&
49 !vk_format_is_depth_or_stencil(pCreateInfo->format)
50 && device->physical_device->rad_info.chip_class <= VI) {
51 /* this causes hangs in some VK CTS tests on GFX9. */
52 /* Textures with a very small height are recommended to be linear. */
53 if (pCreateInfo->imageType == VK_IMAGE_TYPE_1D ||
54 /* Only very thin and long 2D textures should benefit from
55 * linear_aligned. */
56 (pCreateInfo->extent.width > 8 && pCreateInfo->extent.height <= 2))
57 return RADEON_SURF_MODE_LINEAR_ALIGNED;
58 }
59
60 /* MSAA resources must be 2D tiled. */
61 if (pCreateInfo->samples > 1)
62 return RADEON_SURF_MODE_2D;
63
64 return RADEON_SURF_MODE_2D;
65 }
66
67 static bool
68 radv_use_tc_compat_htile_for_image(struct radv_device *device,
69 const VkImageCreateInfo *pCreateInfo)
70 {
71 /* TC-compat HTILE is only available for GFX8+. */
72 if (device->physical_device->rad_info.chip_class < VI)
73 return false;
74
75 if ((pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT) ||
76 (pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR))
77 return false;
78
79 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR)
80 return false;
81
82 if (pCreateInfo->mipLevels > 1)
83 return false;
84
85 /* FIXME: for some reason TC compat with 2/4/8 samples breaks some cts
86 * tests - disable for now */
87 if (pCreateInfo->samples >= 2 &&
88 pCreateInfo->format == VK_FORMAT_D32_SFLOAT_S8_UINT)
89 return false;
90
91 /* GFX9 supports both 32-bit and 16-bit depth surfaces, while GFX8 only
92 * supports 32-bit. Though, it's possible to enable TC-compat for
93 * 16-bit depth surfaces if no Z planes are compressed.
94 */
95 if (pCreateInfo->format != VK_FORMAT_D32_SFLOAT_S8_UINT &&
96 pCreateInfo->format != VK_FORMAT_D32_SFLOAT &&
97 pCreateInfo->format != VK_FORMAT_D16_UNORM)
98 return false;
99
100 if (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
101 const struct VkImageFormatListCreateInfoKHR *format_list =
102 (const struct VkImageFormatListCreateInfoKHR *)
103 vk_find_struct_const(pCreateInfo->pNext,
104 IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
105
106 /* We have to ignore the existence of the list if viewFormatCount = 0 */
107 if (format_list && format_list->viewFormatCount) {
108 /* compatibility is transitive, so we only need to check
109 * one format with everything else.
110 */
111 for (unsigned i = 0; i < format_list->viewFormatCount; ++i) {
112 if (pCreateInfo->format != format_list->pViewFormats[i])
113 return false;
114 }
115 } else {
116 return false;
117 }
118 }
119
120 return true;
121 }
122
123 static bool
124 radv_use_dcc_for_image(struct radv_device *device,
125 const struct radv_image_create_info *create_info,
126 const VkImageCreateInfo *pCreateInfo)
127 {
128 bool dcc_compatible_formats;
129 bool blendable;
130 bool shareable = vk_find_struct_const(pCreateInfo->pNext,
131 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR) != NULL;
132
133 /* DCC (Delta Color Compression) is only available for GFX8+. */
134 if (device->physical_device->rad_info.chip_class < VI)
135 return false;
136
137 if (device->instance->debug_flags & RADV_DEBUG_NO_DCC)
138 return false;
139
140 /* FIXME: DCC is broken for shareable images starting with GFX9 */
141 if (device->physical_device->rad_info.chip_class >= GFX9 &&
142 shareable)
143 return false;
144
145 /* TODO: Enable DCC for storage images. */
146 if ((pCreateInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT) ||
147 (pCreateInfo->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR))
148 return false;
149
150 if (pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR)
151 return false;
152
153 /* TODO: Enable DCC for mipmaps and array layers. */
154 if (pCreateInfo->mipLevels > 1 || pCreateInfo->arrayLayers > 1)
155 return false;
156
157 if (create_info->scanout)
158 return false;
159
160 /* FIXME: DCC for MSAA with 4x and 8x samples doesn't work yet, while
161 * 2x can be enabled with an option.
162 */
163 if (pCreateInfo->samples > 2 ||
164 (pCreateInfo->samples == 2 &&
165 !device->physical_device->dcc_msaa_allowed))
166 return false;
167
168 /* Determine if the formats are DCC compatible. */
169 dcc_compatible_formats =
170 radv_is_colorbuffer_format_supported(pCreateInfo->format,
171 &blendable);
172
173 if (pCreateInfo->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) {
174 const struct VkImageFormatListCreateInfoKHR *format_list =
175 (const struct VkImageFormatListCreateInfoKHR *)
176 vk_find_struct_const(pCreateInfo->pNext,
177 IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
178
179 /* We have to ignore the existence of the list if viewFormatCount = 0 */
180 if (format_list && format_list->viewFormatCount) {
181 /* compatibility is transitive, so we only need to check
182 * one format with everything else. */
183 for (unsigned i = 0; i < format_list->viewFormatCount; ++i) {
184 if (!radv_dcc_formats_compatible(pCreateInfo->format,
185 format_list->pViewFormats[i]))
186 dcc_compatible_formats = false;
187 }
188 } else {
189 dcc_compatible_formats = false;
190 }
191 }
192
193 if (!dcc_compatible_formats)
194 return false;
195
196 return true;
197 }
198
199 static int
200 radv_init_surface(struct radv_device *device,
201 struct radeon_surf *surface,
202 const struct radv_image_create_info *create_info)
203 {
204 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
205 unsigned array_mode = radv_choose_tiling(device, create_info);
206 const struct vk_format_description *desc =
207 vk_format_description(pCreateInfo->format);
208 bool is_depth, is_stencil;
209
210 is_depth = vk_format_has_depth(desc);
211 is_stencil = vk_format_has_stencil(desc);
212
213 surface->blk_w = vk_format_get_blockwidth(pCreateInfo->format);
214 surface->blk_h = vk_format_get_blockheight(pCreateInfo->format);
215
216 surface->bpe = vk_format_get_blocksize(vk_format_depth_only(pCreateInfo->format));
217 /* align byte per element on dword */
218 if (surface->bpe == 3) {
219 surface->bpe = 4;
220 }
221 surface->flags = RADEON_SURF_SET(array_mode, MODE);
222
223 switch (pCreateInfo->imageType){
224 case VK_IMAGE_TYPE_1D:
225 if (pCreateInfo->arrayLayers > 1)
226 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY, TYPE);
227 else
228 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_1D, TYPE);
229 break;
230 case VK_IMAGE_TYPE_2D:
231 if (pCreateInfo->arrayLayers > 1)
232 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY, TYPE);
233 else
234 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_2D, TYPE);
235 break;
236 case VK_IMAGE_TYPE_3D:
237 surface->flags |= RADEON_SURF_SET(RADEON_SURF_TYPE_3D, TYPE);
238 break;
239 default:
240 unreachable("unhandled image type");
241 }
242
243 if (is_depth) {
244 surface->flags |= RADEON_SURF_ZBUFFER;
245 if (radv_use_tc_compat_htile_for_image(device, pCreateInfo))
246 surface->flags |= RADEON_SURF_TC_COMPATIBLE_HTILE;
247 }
248
249 if (is_stencil)
250 surface->flags |= RADEON_SURF_SBUFFER;
251
252 surface->flags |= RADEON_SURF_OPTIMIZE_FOR_SPACE;
253
254 if (!radv_use_dcc_for_image(device, create_info, pCreateInfo))
255 surface->flags |= RADEON_SURF_DISABLE_DCC;
256
257 if (create_info->scanout)
258 surface->flags |= RADEON_SURF_SCANOUT;
259 return 0;
260 }
261
262 static uint32_t si_get_bo_metadata_word1(struct radv_device *device)
263 {
264 return (ATI_VENDOR_ID << 16) | device->physical_device->rad_info.pci_id;
265 }
266
267 static inline unsigned
268 si_tile_mode_index(const struct radv_image *image, unsigned level, bool stencil)
269 {
270 if (stencil)
271 return image->surface.u.legacy.stencil_tiling_index[level];
272 else
273 return image->surface.u.legacy.tiling_index[level];
274 }
275
276 static unsigned radv_map_swizzle(unsigned swizzle)
277 {
278 switch (swizzle) {
279 case VK_SWIZZLE_Y:
280 return V_008F0C_SQ_SEL_Y;
281 case VK_SWIZZLE_Z:
282 return V_008F0C_SQ_SEL_Z;
283 case VK_SWIZZLE_W:
284 return V_008F0C_SQ_SEL_W;
285 case VK_SWIZZLE_0:
286 return V_008F0C_SQ_SEL_0;
287 case VK_SWIZZLE_1:
288 return V_008F0C_SQ_SEL_1;
289 default: /* VK_SWIZZLE_X */
290 return V_008F0C_SQ_SEL_X;
291 }
292 }
293
294 static void
295 radv_make_buffer_descriptor(struct radv_device *device,
296 struct radv_buffer *buffer,
297 VkFormat vk_format,
298 unsigned offset,
299 unsigned range,
300 uint32_t *state)
301 {
302 const struct vk_format_description *desc;
303 unsigned stride;
304 uint64_t gpu_address = radv_buffer_get_va(buffer->bo);
305 uint64_t va = gpu_address + buffer->offset;
306 unsigned num_format, data_format;
307 int first_non_void;
308 desc = vk_format_description(vk_format);
309 first_non_void = vk_format_get_first_non_void_channel(vk_format);
310 stride = desc->block.bits / 8;
311
312 num_format = radv_translate_buffer_numformat(desc, first_non_void);
313 data_format = radv_translate_buffer_dataformat(desc, first_non_void);
314
315 va += offset;
316 state[0] = va;
317 state[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
318 S_008F04_STRIDE(stride);
319
320 if (device->physical_device->rad_info.chip_class != VI && stride) {
321 range /= stride;
322 }
323
324 state[2] = range;
325 state[3] = S_008F0C_DST_SEL_X(radv_map_swizzle(desc->swizzle[0])) |
326 S_008F0C_DST_SEL_Y(radv_map_swizzle(desc->swizzle[1])) |
327 S_008F0C_DST_SEL_Z(radv_map_swizzle(desc->swizzle[2])) |
328 S_008F0C_DST_SEL_W(radv_map_swizzle(desc->swizzle[3])) |
329 S_008F0C_NUM_FORMAT(num_format) |
330 S_008F0C_DATA_FORMAT(data_format);
331 }
332
333 static void
334 si_set_mutable_tex_desc_fields(struct radv_device *device,
335 struct radv_image *image,
336 const struct legacy_surf_level *base_level_info,
337 unsigned base_level, unsigned first_level,
338 unsigned block_width, bool is_stencil,
339 bool is_storage_image, uint32_t *state)
340 {
341 uint64_t gpu_address = image->bo ? radv_buffer_get_va(image->bo) + image->offset : 0;
342 uint64_t va = gpu_address;
343 enum chip_class chip_class = device->physical_device->rad_info.chip_class;
344 uint64_t meta_va = 0;
345 if (chip_class >= GFX9) {
346 if (is_stencil)
347 va += image->surface.u.gfx9.stencil_offset;
348 else
349 va += image->surface.u.gfx9.surf_offset;
350 } else
351 va += base_level_info->offset;
352
353 state[0] = va >> 8;
354 if (chip_class >= GFX9 ||
355 base_level_info->mode == RADEON_SURF_MODE_2D)
356 state[0] |= image->surface.tile_swizzle;
357 state[1] &= C_008F14_BASE_ADDRESS_HI;
358 state[1] |= S_008F14_BASE_ADDRESS_HI(va >> 40);
359
360 if (chip_class >= VI) {
361 state[6] &= C_008F28_COMPRESSION_EN;
362 state[7] = 0;
363 if (!is_storage_image && radv_dcc_enabled(image, first_level)) {
364 meta_va = gpu_address + image->dcc_offset;
365 if (chip_class <= VI)
366 meta_va += base_level_info->dcc_offset;
367 } else if (!is_storage_image &&
368 radv_image_is_tc_compat_htile(image)) {
369 meta_va = gpu_address + image->htile_offset;
370 }
371
372 if (meta_va) {
373 state[6] |= S_008F28_COMPRESSION_EN(1);
374 state[7] = meta_va >> 8;
375 state[7] |= image->surface.tile_swizzle;
376 }
377 }
378
379 if (chip_class >= GFX9) {
380 state[3] &= C_008F1C_SW_MODE;
381 state[4] &= C_008F20_PITCH_GFX9;
382
383 if (is_stencil) {
384 state[3] |= S_008F1C_SW_MODE(image->surface.u.gfx9.stencil.swizzle_mode);
385 state[4] |= S_008F20_PITCH_GFX9(image->surface.u.gfx9.stencil.epitch);
386 } else {
387 state[3] |= S_008F1C_SW_MODE(image->surface.u.gfx9.surf.swizzle_mode);
388 state[4] |= S_008F20_PITCH_GFX9(image->surface.u.gfx9.surf.epitch);
389 }
390
391 state[5] &= C_008F24_META_DATA_ADDRESS &
392 C_008F24_META_PIPE_ALIGNED &
393 C_008F24_META_RB_ALIGNED;
394 if (meta_va) {
395 struct gfx9_surf_meta_flags meta;
396
397 if (image->dcc_offset)
398 meta = image->surface.u.gfx9.dcc;
399 else
400 meta = image->surface.u.gfx9.htile;
401
402 state[5] |= S_008F24_META_DATA_ADDRESS(meta_va >> 40) |
403 S_008F24_META_PIPE_ALIGNED(meta.pipe_aligned) |
404 S_008F24_META_RB_ALIGNED(meta.rb_aligned);
405 }
406 } else {
407 /* SI-CI-VI */
408 unsigned pitch = base_level_info->nblk_x * block_width;
409 unsigned index = si_tile_mode_index(image, base_level, is_stencil);
410
411 state[3] &= C_008F1C_TILING_INDEX;
412 state[3] |= S_008F1C_TILING_INDEX(index);
413 state[4] &= C_008F20_PITCH_GFX6;
414 state[4] |= S_008F20_PITCH_GFX6(pitch - 1);
415 }
416 }
417
418 static unsigned radv_tex_dim(VkImageType image_type, VkImageViewType view_type,
419 unsigned nr_layers, unsigned nr_samples, bool is_storage_image, bool gfx9)
420 {
421 if (view_type == VK_IMAGE_VIEW_TYPE_CUBE || view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
422 return is_storage_image ? V_008F1C_SQ_RSRC_IMG_2D_ARRAY : V_008F1C_SQ_RSRC_IMG_CUBE;
423
424 /* GFX9 allocates 1D textures as 2D. */
425 if (gfx9 && image_type == VK_IMAGE_TYPE_1D)
426 image_type = VK_IMAGE_TYPE_2D;
427 switch (image_type) {
428 case VK_IMAGE_TYPE_1D:
429 return nr_layers > 1 ? V_008F1C_SQ_RSRC_IMG_1D_ARRAY : V_008F1C_SQ_RSRC_IMG_1D;
430 case VK_IMAGE_TYPE_2D:
431 if (nr_samples > 1)
432 return nr_layers > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY : V_008F1C_SQ_RSRC_IMG_2D_MSAA;
433 else
434 return nr_layers > 1 ? V_008F1C_SQ_RSRC_IMG_2D_ARRAY : V_008F1C_SQ_RSRC_IMG_2D;
435 case VK_IMAGE_TYPE_3D:
436 if (view_type == VK_IMAGE_VIEW_TYPE_3D)
437 return V_008F1C_SQ_RSRC_IMG_3D;
438 else
439 return V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
440 default:
441 unreachable("illegal image type");
442 }
443 }
444
445 static unsigned gfx9_border_color_swizzle(const enum vk_swizzle swizzle[4])
446 {
447 unsigned bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
448
449 if (swizzle[3] == VK_SWIZZLE_X) {
450 /* For the pre-defined border color values (white, opaque
451 * black, transparent black), the only thing that matters is
452 * that the alpha channel winds up in the correct place
453 * (because the RGB channels are all the same) so either of
454 * these enumerations will work.
455 */
456 if (swizzle[2] == VK_SWIZZLE_Y)
457 bc_swizzle = V_008F20_BC_SWIZZLE_WZYX;
458 else
459 bc_swizzle = V_008F20_BC_SWIZZLE_WXYZ;
460 } else if (swizzle[0] == VK_SWIZZLE_X) {
461 if (swizzle[1] == VK_SWIZZLE_Y)
462 bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
463 else
464 bc_swizzle = V_008F20_BC_SWIZZLE_XWYZ;
465 } else if (swizzle[1] == VK_SWIZZLE_X) {
466 bc_swizzle = V_008F20_BC_SWIZZLE_YXWZ;
467 } else if (swizzle[2] == VK_SWIZZLE_X) {
468 bc_swizzle = V_008F20_BC_SWIZZLE_ZYXW;
469 }
470
471 return bc_swizzle;
472 }
473
474 /**
475 * Build the sampler view descriptor for a texture.
476 */
477 static void
478 si_make_texture_descriptor(struct radv_device *device,
479 struct radv_image *image,
480 bool is_storage_image,
481 VkImageViewType view_type,
482 VkFormat vk_format,
483 const VkComponentMapping *mapping,
484 unsigned first_level, unsigned last_level,
485 unsigned first_layer, unsigned last_layer,
486 unsigned width, unsigned height, unsigned depth,
487 uint32_t *state,
488 uint32_t *fmask_state)
489 {
490 const struct vk_format_description *desc;
491 enum vk_swizzle swizzle[4];
492 int first_non_void;
493 unsigned num_format, data_format, type;
494
495 desc = vk_format_description(vk_format);
496
497 if (desc->colorspace == VK_FORMAT_COLORSPACE_ZS) {
498 const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
499 vk_format_compose_swizzles(mapping, swizzle_xxxx, swizzle);
500 } else {
501 vk_format_compose_swizzles(mapping, desc->swizzle, swizzle);
502 }
503
504 first_non_void = vk_format_get_first_non_void_channel(vk_format);
505
506 num_format = radv_translate_tex_numformat(vk_format, desc, first_non_void);
507 if (num_format == ~0) {
508 num_format = 0;
509 }
510
511 data_format = radv_translate_tex_dataformat(vk_format, desc, first_non_void);
512 if (data_format == ~0) {
513 data_format = 0;
514 }
515
516 /* S8 with either Z16 or Z32 HTILE need a special format. */
517 if (device->physical_device->rad_info.chip_class >= GFX9 &&
518 vk_format == VK_FORMAT_S8_UINT &&
519 radv_image_is_tc_compat_htile(image)) {
520 if (image->vk_format == VK_FORMAT_D32_SFLOAT_S8_UINT)
521 data_format = V_008F14_IMG_DATA_FORMAT_S8_32;
522 else if (image->vk_format == VK_FORMAT_D16_UNORM_S8_UINT)
523 data_format = V_008F14_IMG_DATA_FORMAT_S8_16;
524 }
525 type = radv_tex_dim(image->type, view_type, image->info.array_size, image->info.samples,
526 is_storage_image, device->physical_device->rad_info.chip_class >= GFX9);
527 if (type == V_008F1C_SQ_RSRC_IMG_1D_ARRAY) {
528 height = 1;
529 depth = image->info.array_size;
530 } else if (type == V_008F1C_SQ_RSRC_IMG_2D_ARRAY ||
531 type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
532 if (view_type != VK_IMAGE_VIEW_TYPE_3D)
533 depth = image->info.array_size;
534 } else if (type == V_008F1C_SQ_RSRC_IMG_CUBE)
535 depth = image->info.array_size / 6;
536
537 state[0] = 0;
538 state[1] = (S_008F14_DATA_FORMAT_GFX6(data_format) |
539 S_008F14_NUM_FORMAT_GFX6(num_format));
540 state[2] = (S_008F18_WIDTH(width - 1) |
541 S_008F18_HEIGHT(height - 1) |
542 S_008F18_PERF_MOD(4));
543 state[3] = (S_008F1C_DST_SEL_X(radv_map_swizzle(swizzle[0])) |
544 S_008F1C_DST_SEL_Y(radv_map_swizzle(swizzle[1])) |
545 S_008F1C_DST_SEL_Z(radv_map_swizzle(swizzle[2])) |
546 S_008F1C_DST_SEL_W(radv_map_swizzle(swizzle[3])) |
547 S_008F1C_BASE_LEVEL(image->info.samples > 1 ?
548 0 : first_level) |
549 S_008F1C_LAST_LEVEL(image->info.samples > 1 ?
550 util_logbase2(image->info.samples) :
551 last_level) |
552 S_008F1C_TYPE(type));
553 state[4] = 0;
554 state[5] = S_008F24_BASE_ARRAY(first_layer);
555 state[6] = 0;
556 state[7] = 0;
557
558 if (device->physical_device->rad_info.chip_class >= GFX9) {
559 unsigned bc_swizzle = gfx9_border_color_swizzle(swizzle);
560
561 /* Depth is the last accessible layer on Gfx9.
562 * The hw doesn't need to know the total number of layers.
563 */
564 if (type == V_008F1C_SQ_RSRC_IMG_3D)
565 state[4] |= S_008F20_DEPTH(depth - 1);
566 else
567 state[4] |= S_008F20_DEPTH(last_layer);
568
569 state[4] |= S_008F20_BC_SWIZZLE(bc_swizzle);
570 state[5] |= S_008F24_MAX_MIP(image->info.samples > 1 ?
571 util_logbase2(image->info.samples) :
572 image->info.levels - 1);
573 } else {
574 state[3] |= S_008F1C_POW2_PAD(image->info.levels > 1);
575 state[4] |= S_008F20_DEPTH(depth - 1);
576 state[5] |= S_008F24_LAST_ARRAY(last_layer);
577 }
578 if (image->dcc_offset) {
579 unsigned swap = radv_translate_colorswap(vk_format, FALSE);
580
581 state[6] = S_008F28_ALPHA_IS_ON_MSB(swap <= 1);
582 } else {
583 /* The last dword is unused by hw. The shader uses it to clear
584 * bits in the first dword of sampler state.
585 */
586 if (device->physical_device->rad_info.chip_class <= CIK && image->info.samples <= 1) {
587 if (first_level == last_level)
588 state[7] = C_008F30_MAX_ANISO_RATIO;
589 else
590 state[7] = 0xffffffff;
591 }
592 }
593
594 /* Initialize the sampler view for FMASK. */
595 if (radv_image_has_fmask(image)) {
596 uint32_t fmask_format, num_format;
597 uint64_t gpu_address = radv_buffer_get_va(image->bo);
598 uint64_t va;
599
600 va = gpu_address + image->offset + image->fmask.offset;
601
602 if (device->physical_device->rad_info.chip_class >= GFX9) {
603 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK;
604 switch (image->info.samples) {
605 case 2:
606 num_format = V_008F14_IMG_FMASK_8_2_2;
607 break;
608 case 4:
609 num_format = V_008F14_IMG_FMASK_8_4_4;
610 break;
611 case 8:
612 num_format = V_008F14_IMG_FMASK_32_8_8;
613 break;
614 default:
615 unreachable("invalid nr_samples");
616 }
617 } else {
618 switch (image->info.samples) {
619 case 2:
620 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2;
621 break;
622 case 4:
623 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4;
624 break;
625 case 8:
626 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8;
627 break;
628 default:
629 assert(0);
630 fmask_format = V_008F14_IMG_DATA_FORMAT_INVALID;
631 }
632 num_format = V_008F14_IMG_NUM_FORMAT_UINT;
633 }
634
635 fmask_state[0] = va >> 8;
636 fmask_state[0] |= image->fmask.tile_swizzle;
637 fmask_state[1] = S_008F14_BASE_ADDRESS_HI(va >> 40) |
638 S_008F14_DATA_FORMAT_GFX6(fmask_format) |
639 S_008F14_NUM_FORMAT_GFX6(num_format);
640 fmask_state[2] = S_008F18_WIDTH(width - 1) |
641 S_008F18_HEIGHT(height - 1);
642 fmask_state[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X) |
643 S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X) |
644 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X) |
645 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X) |
646 S_008F1C_TYPE(radv_tex_dim(image->type, view_type, image->info.array_size, 0, false, false));
647 fmask_state[4] = 0;
648 fmask_state[5] = S_008F24_BASE_ARRAY(first_layer);
649 fmask_state[6] = 0;
650 fmask_state[7] = 0;
651
652 if (device->physical_device->rad_info.chip_class >= GFX9) {
653 fmask_state[3] |= S_008F1C_SW_MODE(image->surface.u.gfx9.fmask.swizzle_mode);
654 fmask_state[4] |= S_008F20_DEPTH(last_layer) |
655 S_008F20_PITCH_GFX9(image->surface.u.gfx9.fmask.epitch);
656 fmask_state[5] |= S_008F24_META_PIPE_ALIGNED(image->surface.u.gfx9.cmask.pipe_aligned) |
657 S_008F24_META_RB_ALIGNED(image->surface.u.gfx9.cmask.rb_aligned);
658 } else {
659 fmask_state[3] |= S_008F1C_TILING_INDEX(image->fmask.tile_mode_index);
660 fmask_state[4] |= S_008F20_DEPTH(depth - 1) |
661 S_008F20_PITCH_GFX6(image->fmask.pitch_in_pixels - 1);
662 fmask_state[5] |= S_008F24_LAST_ARRAY(last_layer);
663 }
664 } else if (fmask_state)
665 memset(fmask_state, 0, 8 * 4);
666 }
667
668 static void
669 radv_query_opaque_metadata(struct radv_device *device,
670 struct radv_image *image,
671 struct radeon_bo_metadata *md)
672 {
673 static const VkComponentMapping fixedmapping;
674 uint32_t desc[8], i;
675
676 /* Metadata image format format version 1:
677 * [0] = 1 (metadata format identifier)
678 * [1] = (VENDOR_ID << 16) | PCI_ID
679 * [2:9] = image descriptor for the whole resource
680 * [2] is always 0, because the base address is cleared
681 * [9] is the DCC offset bits [39:8] from the beginning of
682 * the buffer
683 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
684 */
685 md->metadata[0] = 1; /* metadata image format version 1 */
686
687 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
688 md->metadata[1] = si_get_bo_metadata_word1(device);
689
690
691 si_make_texture_descriptor(device, image, false,
692 (VkImageViewType)image->type, image->vk_format,
693 &fixedmapping, 0, image->info.levels - 1, 0,
694 image->info.array_size - 1,
695 image->info.width, image->info.height,
696 image->info.depth,
697 desc, NULL);
698
699 si_set_mutable_tex_desc_fields(device, image, &image->surface.u.legacy.level[0], 0, 0,
700 image->surface.blk_w, false, false, desc);
701
702 /* Clear the base address and set the relative DCC offset. */
703 desc[0] = 0;
704 desc[1] &= C_008F14_BASE_ADDRESS_HI;
705 desc[7] = image->dcc_offset >> 8;
706
707 /* Dwords [2:9] contain the image descriptor. */
708 memcpy(&md->metadata[2], desc, sizeof(desc));
709
710 /* Dwords [10:..] contain the mipmap level offsets. */
711 if (device->physical_device->rad_info.chip_class <= VI) {
712 for (i = 0; i <= image->info.levels - 1; i++)
713 md->metadata[10+i] = image->surface.u.legacy.level[i].offset >> 8;
714 md->size_metadata = (11 + image->info.levels - 1) * 4;
715 }
716 }
717
718 void
719 radv_init_metadata(struct radv_device *device,
720 struct radv_image *image,
721 struct radeon_bo_metadata *metadata)
722 {
723 struct radeon_surf *surface = &image->surface;
724
725 memset(metadata, 0, sizeof(*metadata));
726
727 if (device->physical_device->rad_info.chip_class >= GFX9) {
728 metadata->u.gfx9.swizzle_mode = surface->u.gfx9.surf.swizzle_mode;
729 } else {
730 metadata->u.legacy.microtile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_1D ?
731 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
732 metadata->u.legacy.macrotile = surface->u.legacy.level[0].mode >= RADEON_SURF_MODE_2D ?
733 RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
734 metadata->u.legacy.pipe_config = surface->u.legacy.pipe_config;
735 metadata->u.legacy.bankw = surface->u.legacy.bankw;
736 metadata->u.legacy.bankh = surface->u.legacy.bankh;
737 metadata->u.legacy.tile_split = surface->u.legacy.tile_split;
738 metadata->u.legacy.mtilea = surface->u.legacy.mtilea;
739 metadata->u.legacy.num_banks = surface->u.legacy.num_banks;
740 metadata->u.legacy.stride = surface->u.legacy.level[0].nblk_x * surface->bpe;
741 metadata->u.legacy.scanout = (surface->flags & RADEON_SURF_SCANOUT) != 0;
742 }
743 radv_query_opaque_metadata(device, image, metadata);
744 }
745
746 /* The number of samples can be specified independently of the texture. */
747 static void
748 radv_image_get_fmask_info(struct radv_device *device,
749 struct radv_image *image,
750 unsigned nr_samples,
751 struct radv_fmask_info *out)
752 {
753 if (device->physical_device->rad_info.chip_class >= GFX9) {
754 out->alignment = image->surface.fmask_alignment;
755 out->size = image->surface.fmask_size;
756 out->tile_swizzle = image->surface.fmask_tile_swizzle;
757 return;
758 }
759
760 out->slice_tile_max = image->surface.u.legacy.fmask.slice_tile_max;
761 out->tile_mode_index = image->surface.u.legacy.fmask.tiling_index;
762 out->pitch_in_pixels = image->surface.u.legacy.fmask.pitch_in_pixels;
763 out->bank_height = image->surface.u.legacy.fmask.bankh;
764 out->tile_swizzle = image->surface.fmask_tile_swizzle;
765 out->alignment = image->surface.fmask_alignment;
766 out->size = image->surface.fmask_size;
767
768 assert(!out->tile_swizzle || !image->shareable);
769 }
770
771 static void
772 radv_image_alloc_fmask(struct radv_device *device,
773 struct radv_image *image)
774 {
775 radv_image_get_fmask_info(device, image, image->info.samples, &image->fmask);
776
777 image->fmask.offset = align64(image->size, image->fmask.alignment);
778 image->size = image->fmask.offset + image->fmask.size;
779 image->alignment = MAX2(image->alignment, image->fmask.alignment);
780 }
781
782 static void
783 radv_image_get_cmask_info(struct radv_device *device,
784 struct radv_image *image,
785 struct radv_cmask_info *out)
786 {
787 unsigned pipe_interleave_bytes = device->physical_device->rad_info.pipe_interleave_bytes;
788 unsigned num_pipes = device->physical_device->rad_info.num_tile_pipes;
789 unsigned cl_width, cl_height;
790
791 if (device->physical_device->rad_info.chip_class >= GFX9) {
792 out->alignment = image->surface.cmask_alignment;
793 out->size = image->surface.cmask_size;
794 return;
795 }
796
797 switch (num_pipes) {
798 case 2:
799 cl_width = 32;
800 cl_height = 16;
801 break;
802 case 4:
803 cl_width = 32;
804 cl_height = 32;
805 break;
806 case 8:
807 cl_width = 64;
808 cl_height = 32;
809 break;
810 case 16: /* Hawaii */
811 cl_width = 64;
812 cl_height = 64;
813 break;
814 default:
815 assert(0);
816 return;
817 }
818
819 unsigned base_align = num_pipes * pipe_interleave_bytes;
820
821 unsigned width = align(image->surface.u.legacy.level[0].nblk_x, cl_width*8);
822 unsigned height = align(image->surface.u.legacy.level[0].nblk_y, cl_height*8);
823 unsigned slice_elements = (width * height) / (8*8);
824
825 /* Each element of CMASK is a nibble. */
826 unsigned slice_bytes = slice_elements / 2;
827
828 out->slice_tile_max = (width * height) / (128*128);
829 if (out->slice_tile_max)
830 out->slice_tile_max -= 1;
831
832 out->alignment = MAX2(256, base_align);
833 out->size = (image->type == VK_IMAGE_TYPE_3D ? image->info.depth : image->info.array_size) *
834 align(slice_bytes, base_align);
835 }
836
837 static void
838 radv_image_alloc_cmask(struct radv_device *device,
839 struct radv_image *image)
840 {
841 uint32_t clear_value_size = 0;
842 radv_image_get_cmask_info(device, image, &image->cmask);
843
844 image->cmask.offset = align64(image->size, image->cmask.alignment);
845 /* + 8 for storing the clear values */
846 if (!image->clear_value_offset) {
847 image->clear_value_offset = image->cmask.offset + image->cmask.size;
848 clear_value_size = 8;
849 }
850 image->size = image->cmask.offset + image->cmask.size + clear_value_size;
851 image->alignment = MAX2(image->alignment, image->cmask.alignment);
852 }
853
854 static void
855 radv_image_alloc_dcc(struct radv_image *image)
856 {
857 image->dcc_offset = align64(image->size, image->surface.dcc_alignment);
858 /* + 16 for storing the clear values + dcc pred */
859 image->clear_value_offset = image->dcc_offset + image->surface.dcc_size;
860 image->fce_pred_offset = image->clear_value_offset + 8;
861 image->size = image->dcc_offset + image->surface.dcc_size + 16;
862 image->alignment = MAX2(image->alignment, image->surface.dcc_alignment);
863 }
864
865 static void
866 radv_image_alloc_htile(struct radv_image *image)
867 {
868 image->htile_offset = align64(image->size, image->surface.htile_alignment);
869
870 /* + 8 for storing the clear values */
871 image->clear_value_offset = image->htile_offset + image->surface.htile_size;
872 image->size = image->clear_value_offset + 8;
873 image->alignment = align64(image->alignment, image->surface.htile_alignment);
874 }
875
876 static inline bool
877 radv_image_can_enable_dcc_or_cmask(struct radv_image *image)
878 {
879 if (image->info.samples <= 1 &&
880 image->info.width * image->info.height <= 512 * 512) {
881 /* Do not enable CMASK or DCC for small surfaces where the cost
882 * of the eliminate pass can be higher than the benefit of fast
883 * clear. RadeonSI does this, but the image threshold is
884 * different.
885 */
886 return false;
887 }
888
889 return image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT &&
890 (image->exclusive || image->queue_family_mask == 1);
891 }
892
893 static inline bool
894 radv_image_can_enable_dcc(struct radv_image *image)
895 {
896 return radv_image_can_enable_dcc_or_cmask(image) &&
897 radv_image_has_dcc(image);
898 }
899
900 static inline bool
901 radv_image_can_enable_cmask(struct radv_image *image)
902 {
903 if (image->surface.bpe > 8 && image->info.samples == 1) {
904 /* Do not enable CMASK for non-MSAA images (fast color clear)
905 * because 128 bit formats are not supported, but FMASK might
906 * still be used.
907 */
908 return false;
909 }
910
911 return radv_image_can_enable_dcc_or_cmask(image) &&
912 image->info.levels == 1 &&
913 image->info.depth == 1 &&
914 !image->surface.is_linear;
915 }
916
917 static inline bool
918 radv_image_can_enable_fmask(struct radv_image *image)
919 {
920 return image->info.samples > 1 && vk_format_is_color(image->vk_format);
921 }
922
923 static inline bool
924 radv_image_can_enable_htile(struct radv_image *image)
925 {
926 return image->info.levels == 1 &&
927 vk_format_is_depth(image->vk_format) &&
928 image->info.width * image->info.height >= 8 * 8;
929 }
930
931 VkResult
932 radv_image_create(VkDevice _device,
933 const struct radv_image_create_info *create_info,
934 const VkAllocationCallbacks* alloc,
935 VkImage *pImage)
936 {
937 RADV_FROM_HANDLE(radv_device, device, _device);
938 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
939 struct radv_image *image = NULL;
940 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
941
942 radv_assert(pCreateInfo->mipLevels > 0);
943 radv_assert(pCreateInfo->arrayLayers > 0);
944 radv_assert(pCreateInfo->samples > 0);
945 radv_assert(pCreateInfo->extent.width > 0);
946 radv_assert(pCreateInfo->extent.height > 0);
947 radv_assert(pCreateInfo->extent.depth > 0);
948
949 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
950 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
951 if (!image)
952 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
953
954 image->type = pCreateInfo->imageType;
955 image->info.width = pCreateInfo->extent.width;
956 image->info.height = pCreateInfo->extent.height;
957 image->info.depth = pCreateInfo->extent.depth;
958 image->info.samples = pCreateInfo->samples;
959 image->info.storage_samples = pCreateInfo->samples;
960 image->info.array_size = pCreateInfo->arrayLayers;
961 image->info.levels = pCreateInfo->mipLevels;
962 image->info.num_channels = vk_format_get_nr_components(pCreateInfo->format);
963
964 image->vk_format = pCreateInfo->format;
965 image->tiling = pCreateInfo->tiling;
966 image->usage = pCreateInfo->usage;
967 image->flags = pCreateInfo->flags;
968
969 image->exclusive = pCreateInfo->sharingMode == VK_SHARING_MODE_EXCLUSIVE;
970 if (pCreateInfo->sharingMode == VK_SHARING_MODE_CONCURRENT) {
971 for (uint32_t i = 0; i < pCreateInfo->queueFamilyIndexCount; ++i)
972 if (pCreateInfo->pQueueFamilyIndices[i] == VK_QUEUE_FAMILY_EXTERNAL_KHR)
973 image->queue_family_mask |= (1u << RADV_MAX_QUEUE_FAMILIES) - 1u;
974 else
975 image->queue_family_mask |= 1u << pCreateInfo->pQueueFamilyIndices[i];
976 }
977
978 image->shareable = vk_find_struct_const(pCreateInfo->pNext,
979 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR) != NULL;
980 if (!vk_format_is_depth(pCreateInfo->format) && !create_info->scanout && !image->shareable) {
981 image->info.surf_index = &device->image_mrt_offset_counter;
982 }
983
984 radv_init_surface(device, &image->surface, create_info);
985
986 device->ws->surface_init(device->ws, &image->info, &image->surface);
987
988 image->size = image->surface.surf_size;
989 image->alignment = image->surface.surf_alignment;
990
991 if (!create_info->no_metadata_planes) {
992 /* Try to enable DCC first. */
993 if (radv_image_can_enable_dcc(image)) {
994 radv_image_alloc_dcc(image);
995 if (image->info.samples > 1) {
996 /* CMASK should be enabled because DCC fast
997 * clear with MSAA needs it.
998 */
999 assert(radv_image_can_enable_cmask(image));
1000 radv_image_alloc_cmask(device, image);
1001 }
1002 } else {
1003 /* When DCC cannot be enabled, try CMASK. */
1004 image->surface.dcc_size = 0;
1005 if (radv_image_can_enable_cmask(image)) {
1006 radv_image_alloc_cmask(device, image);
1007 }
1008 }
1009
1010 /* Try to enable FMASK for multisampled images. */
1011 if (radv_image_can_enable_fmask(image)) {
1012 radv_image_alloc_fmask(device, image);
1013 } else {
1014 /* Otherwise, try to enable HTILE for depth surfaces. */
1015 if (radv_image_can_enable_htile(image) &&
1016 !(device->instance->debug_flags & RADV_DEBUG_NO_HIZ)) {
1017 radv_image_alloc_htile(image);
1018 image->tc_compatible_htile = image->surface.flags & RADEON_SURF_TC_COMPATIBLE_HTILE;
1019 } else {
1020 image->surface.htile_size = 0;
1021 }
1022 }
1023 } else {
1024 image->surface.dcc_size = 0;
1025 image->surface.htile_size = 0;
1026 }
1027
1028 if (pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
1029 image->alignment = MAX2(image->alignment, 4096);
1030 image->size = align64(image->size, image->alignment);
1031 image->offset = 0;
1032
1033 image->bo = device->ws->buffer_create(device->ws, image->size, image->alignment,
1034 0, RADEON_FLAG_VIRTUAL);
1035 if (!image->bo) {
1036 vk_free2(&device->alloc, alloc, image);
1037 return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
1038 }
1039 }
1040
1041 *pImage = radv_image_to_handle(image);
1042
1043 return VK_SUCCESS;
1044 }
1045
1046 static void
1047 radv_image_view_make_descriptor(struct radv_image_view *iview,
1048 struct radv_device *device,
1049 const VkComponentMapping *components,
1050 bool is_storage_image)
1051 {
1052 struct radv_image *image = iview->image;
1053 bool is_stencil = iview->aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT;
1054 uint32_t blk_w;
1055 uint32_t *descriptor;
1056 uint32_t hw_level = 0;
1057
1058 if (is_storage_image) {
1059 descriptor = iview->storage_descriptor;
1060 } else {
1061 descriptor = iview->descriptor;
1062 }
1063
1064 assert(image->surface.blk_w % vk_format_get_blockwidth(image->vk_format) == 0);
1065 blk_w = image->surface.blk_w / vk_format_get_blockwidth(image->vk_format) * vk_format_get_blockwidth(iview->vk_format);
1066
1067 if (device->physical_device->rad_info.chip_class >= GFX9)
1068 hw_level = iview->base_mip;
1069 si_make_texture_descriptor(device, image, is_storage_image,
1070 iview->type,
1071 iview->vk_format,
1072 components,
1073 hw_level, hw_level + iview->level_count - 1,
1074 iview->base_layer,
1075 iview->base_layer + iview->layer_count - 1,
1076 iview->extent.width,
1077 iview->extent.height,
1078 iview->extent.depth,
1079 descriptor,
1080 descriptor + 8);
1081
1082 const struct legacy_surf_level *base_level_info = NULL;
1083 if (device->physical_device->rad_info.chip_class <= GFX9) {
1084 if (is_stencil)
1085 base_level_info = &image->surface.u.legacy.stencil_level[iview->base_mip];
1086 else
1087 base_level_info = &image->surface.u.legacy.level[iview->base_mip];
1088 }
1089 si_set_mutable_tex_desc_fields(device, image,
1090 base_level_info,
1091 iview->base_mip,
1092 iview->base_mip,
1093 blk_w, is_stencil, is_storage_image, descriptor);
1094 }
1095
1096 void
1097 radv_image_view_init(struct radv_image_view *iview,
1098 struct radv_device *device,
1099 const VkImageViewCreateInfo* pCreateInfo)
1100 {
1101 RADV_FROM_HANDLE(radv_image, image, pCreateInfo->image);
1102 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
1103
1104 switch (image->type) {
1105 case VK_IMAGE_TYPE_1D:
1106 case VK_IMAGE_TYPE_2D:
1107 assert(range->baseArrayLayer + radv_get_layerCount(image, range) - 1 <= image->info.array_size);
1108 break;
1109 case VK_IMAGE_TYPE_3D:
1110 assert(range->baseArrayLayer + radv_get_layerCount(image, range) - 1
1111 <= radv_minify(image->info.depth, range->baseMipLevel));
1112 break;
1113 default:
1114 unreachable("bad VkImageType");
1115 }
1116 iview->image = image;
1117 iview->bo = image->bo;
1118 iview->type = pCreateInfo->viewType;
1119 iview->vk_format = pCreateInfo->format;
1120 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
1121
1122 if (iview->aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
1123 iview->vk_format = vk_format_stencil_only(iview->vk_format);
1124 } else if (iview->aspect_mask == VK_IMAGE_ASPECT_DEPTH_BIT) {
1125 iview->vk_format = vk_format_depth_only(iview->vk_format);
1126 }
1127
1128 if (device->physical_device->rad_info.chip_class >= GFX9) {
1129 iview->extent = (VkExtent3D) {
1130 .width = image->info.width,
1131 .height = image->info.height,
1132 .depth = image->info.depth,
1133 };
1134 } else {
1135 iview->extent = (VkExtent3D) {
1136 .width = radv_minify(image->info.width , range->baseMipLevel),
1137 .height = radv_minify(image->info.height, range->baseMipLevel),
1138 .depth = radv_minify(image->info.depth , range->baseMipLevel),
1139 };
1140 }
1141
1142 if (iview->vk_format != image->vk_format) {
1143 unsigned view_bw = vk_format_get_blockwidth(iview->vk_format);
1144 unsigned view_bh = vk_format_get_blockheight(iview->vk_format);
1145 unsigned img_bw = vk_format_get_blockwidth(image->vk_format);
1146 unsigned img_bh = vk_format_get_blockheight(image->vk_format);
1147
1148 iview->extent.width = round_up_u32(iview->extent.width * view_bw, img_bw);
1149 iview->extent.height = round_up_u32(iview->extent.height * view_bh, img_bh);
1150
1151 /* Comment ported from amdvlk -
1152 * If we have the following image:
1153 * Uncompressed pixels Compressed block sizes (4x4)
1154 * mip0: 22 x 22 6 x 6
1155 * mip1: 11 x 11 3 x 3
1156 * mip2: 5 x 5 2 x 2
1157 * mip3: 2 x 2 1 x 1
1158 * mip4: 1 x 1 1 x 1
1159 *
1160 * On GFX9 the descriptor is always programmed with the WIDTH and HEIGHT of the base level and the HW is
1161 * calculating the degradation of the block sizes down the mip-chain as follows (straight-up
1162 * divide-by-two integer math):
1163 * mip0: 6x6
1164 * mip1: 3x3
1165 * mip2: 1x1
1166 * mip3: 1x1
1167 *
1168 * This means that mip2 will be missing texels.
1169 *
1170 * Fix this by calculating the base mip's width and height, then convert that, and round it
1171 * back up to get the level 0 size.
1172 * Clamp the converted size between the original values, and next power of two, which
1173 * means we don't oversize the image.
1174 */
1175 if (device->physical_device->rad_info.chip_class >= GFX9 &&
1176 vk_format_is_compressed(image->vk_format) &&
1177 !vk_format_is_compressed(iview->vk_format)) {
1178 unsigned lvl_width = radv_minify(image->info.width , range->baseMipLevel);
1179 unsigned lvl_height = radv_minify(image->info.height, range->baseMipLevel);
1180
1181 lvl_width = round_up_u32(lvl_width * view_bw, img_bw);
1182 lvl_height = round_up_u32(lvl_height * view_bh, img_bh);
1183
1184 lvl_width <<= range->baseMipLevel;
1185 lvl_height <<= range->baseMipLevel;
1186
1187 iview->extent.width = CLAMP(lvl_width, iview->extent.width, iview->image->surface.u.gfx9.surf_pitch);
1188 iview->extent.height = CLAMP(lvl_height, iview->extent.height, iview->image->surface.u.gfx9.surf_height);
1189 }
1190 }
1191
1192 iview->base_layer = range->baseArrayLayer;
1193 iview->layer_count = radv_get_layerCount(image, range);
1194 iview->base_mip = range->baseMipLevel;
1195 iview->level_count = radv_get_levelCount(image, range);
1196
1197 radv_image_view_make_descriptor(iview, device, &pCreateInfo->components, false);
1198 radv_image_view_make_descriptor(iview, device, &pCreateInfo->components, true);
1199 }
1200
1201 bool radv_layout_has_htile(const struct radv_image *image,
1202 VkImageLayout layout,
1203 unsigned queue_mask)
1204 {
1205 if (radv_image_is_tc_compat_htile(image))
1206 return layout != VK_IMAGE_LAYOUT_GENERAL;
1207
1208 return radv_image_has_htile(image) &&
1209 (layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
1210 layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) &&
1211 queue_mask == (1u << RADV_QUEUE_GENERAL);
1212 }
1213
1214 bool radv_layout_is_htile_compressed(const struct radv_image *image,
1215 VkImageLayout layout,
1216 unsigned queue_mask)
1217 {
1218 if (radv_image_is_tc_compat_htile(image))
1219 return layout != VK_IMAGE_LAYOUT_GENERAL;
1220
1221 return radv_image_has_htile(image) &&
1222 (layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
1223 layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) &&
1224 queue_mask == (1u << RADV_QUEUE_GENERAL);
1225 }
1226
1227 bool radv_layout_can_fast_clear(const struct radv_image *image,
1228 VkImageLayout layout,
1229 unsigned queue_mask)
1230 {
1231 return layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL &&
1232 queue_mask == (1u << RADV_QUEUE_GENERAL);
1233 }
1234
1235 bool radv_layout_dcc_compressed(const struct radv_image *image,
1236 VkImageLayout layout,
1237 unsigned queue_mask)
1238 {
1239 /* Don't compress compute transfer dst, as image stores are not supported. */
1240 if (layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL &&
1241 (queue_mask & (1u << RADV_QUEUE_COMPUTE)))
1242 return false;
1243
1244 return radv_image_has_dcc(image) && layout != VK_IMAGE_LAYOUT_GENERAL;
1245 }
1246
1247
1248 unsigned radv_image_queue_family_mask(const struct radv_image *image, uint32_t family, uint32_t queue_family)
1249 {
1250 if (!image->exclusive)
1251 return image->queue_family_mask;
1252 if (family == VK_QUEUE_FAMILY_EXTERNAL_KHR)
1253 return (1u << RADV_MAX_QUEUE_FAMILIES) - 1u;
1254 if (family == VK_QUEUE_FAMILY_IGNORED)
1255 return 1u << queue_family;
1256 return 1u << family;
1257 }
1258
1259 VkResult
1260 radv_CreateImage(VkDevice device,
1261 const VkImageCreateInfo *pCreateInfo,
1262 const VkAllocationCallbacks *pAllocator,
1263 VkImage *pImage)
1264 {
1265 #ifdef ANDROID
1266 const VkNativeBufferANDROID *gralloc_info =
1267 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
1268
1269 if (gralloc_info)
1270 return radv_image_from_gralloc(device, pCreateInfo, gralloc_info,
1271 pAllocator, pImage);
1272 #endif
1273
1274 const struct wsi_image_create_info *wsi_info =
1275 vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
1276 bool scanout = wsi_info && wsi_info->scanout;
1277
1278 return radv_image_create(device,
1279 &(struct radv_image_create_info) {
1280 .vk_info = pCreateInfo,
1281 .scanout = scanout,
1282 },
1283 pAllocator,
1284 pImage);
1285 }
1286
1287 void
1288 radv_DestroyImage(VkDevice _device, VkImage _image,
1289 const VkAllocationCallbacks *pAllocator)
1290 {
1291 RADV_FROM_HANDLE(radv_device, device, _device);
1292 RADV_FROM_HANDLE(radv_image, image, _image);
1293
1294 if (!image)
1295 return;
1296
1297 if (image->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)
1298 device->ws->buffer_destroy(image->bo);
1299
1300 if (image->owned_memory != VK_NULL_HANDLE)
1301 radv_FreeMemory(_device, image->owned_memory, pAllocator);
1302
1303 vk_free2(&device->alloc, pAllocator, image);
1304 }
1305
1306 void radv_GetImageSubresourceLayout(
1307 VkDevice _device,
1308 VkImage _image,
1309 const VkImageSubresource* pSubresource,
1310 VkSubresourceLayout* pLayout)
1311 {
1312 RADV_FROM_HANDLE(radv_image, image, _image);
1313 RADV_FROM_HANDLE(radv_device, device, _device);
1314 int level = pSubresource->mipLevel;
1315 int layer = pSubresource->arrayLayer;
1316 struct radeon_surf *surface = &image->surface;
1317
1318 if (device->physical_device->rad_info.chip_class >= GFX9) {
1319 pLayout->offset = surface->u.gfx9.offset[level] + surface->u.gfx9.surf_slice_size * layer;
1320 pLayout->rowPitch = surface->u.gfx9.surf_pitch * surface->bpe;
1321 pLayout->arrayPitch = surface->u.gfx9.surf_slice_size;
1322 pLayout->depthPitch = surface->u.gfx9.surf_slice_size;
1323 pLayout->size = surface->u.gfx9.surf_slice_size;
1324 if (image->type == VK_IMAGE_TYPE_3D)
1325 pLayout->size *= u_minify(image->info.depth, level);
1326 } else {
1327 pLayout->offset = surface->u.legacy.level[level].offset + (uint64_t)surface->u.legacy.level[level].slice_size_dw * 4 * layer;
1328 pLayout->rowPitch = surface->u.legacy.level[level].nblk_x * surface->bpe;
1329 pLayout->arrayPitch = (uint64_t)surface->u.legacy.level[level].slice_size_dw * 4;
1330 pLayout->depthPitch = (uint64_t)surface->u.legacy.level[level].slice_size_dw * 4;
1331 pLayout->size = (uint64_t)surface->u.legacy.level[level].slice_size_dw * 4;
1332 if (image->type == VK_IMAGE_TYPE_3D)
1333 pLayout->size *= u_minify(image->info.depth, level);
1334 }
1335 }
1336
1337
1338 VkResult
1339 radv_CreateImageView(VkDevice _device,
1340 const VkImageViewCreateInfo *pCreateInfo,
1341 const VkAllocationCallbacks *pAllocator,
1342 VkImageView *pView)
1343 {
1344 RADV_FROM_HANDLE(radv_device, device, _device);
1345 struct radv_image_view *view;
1346
1347 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
1348 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1349 if (view == NULL)
1350 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1351
1352 radv_image_view_init(view, device, pCreateInfo);
1353
1354 *pView = radv_image_view_to_handle(view);
1355
1356 return VK_SUCCESS;
1357 }
1358
1359 void
1360 radv_DestroyImageView(VkDevice _device, VkImageView _iview,
1361 const VkAllocationCallbacks *pAllocator)
1362 {
1363 RADV_FROM_HANDLE(radv_device, device, _device);
1364 RADV_FROM_HANDLE(radv_image_view, iview, _iview);
1365
1366 if (!iview)
1367 return;
1368 vk_free2(&device->alloc, pAllocator, iview);
1369 }
1370
1371 void radv_buffer_view_init(struct radv_buffer_view *view,
1372 struct radv_device *device,
1373 const VkBufferViewCreateInfo* pCreateInfo)
1374 {
1375 RADV_FROM_HANDLE(radv_buffer, buffer, pCreateInfo->buffer);
1376
1377 view->bo = buffer->bo;
1378 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
1379 buffer->size - pCreateInfo->offset : pCreateInfo->range;
1380 view->vk_format = pCreateInfo->format;
1381
1382 radv_make_buffer_descriptor(device, buffer, view->vk_format,
1383 pCreateInfo->offset, view->range, view->state);
1384 }
1385
1386 VkResult
1387 radv_CreateBufferView(VkDevice _device,
1388 const VkBufferViewCreateInfo *pCreateInfo,
1389 const VkAllocationCallbacks *pAllocator,
1390 VkBufferView *pView)
1391 {
1392 RADV_FROM_HANDLE(radv_device, device, _device);
1393 struct radv_buffer_view *view;
1394
1395 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
1396 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1397 if (!view)
1398 return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
1399
1400 radv_buffer_view_init(view, device, pCreateInfo);
1401
1402 *pView = radv_buffer_view_to_handle(view);
1403
1404 return VK_SUCCESS;
1405 }
1406
1407 void
1408 radv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
1409 const VkAllocationCallbacks *pAllocator)
1410 {
1411 RADV_FROM_HANDLE(radv_device, device, _device);
1412 RADV_FROM_HANDLE(radv_buffer_view, view, bufferView);
1413
1414 if (!view)
1415 return;
1416
1417 vk_free2(&device->alloc, pAllocator, view);
1418 }