2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
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:
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
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
28 #include "radv_debug.h"
29 #include "radv_private.h"
30 #include "vk_format.h"
32 #include "radv_radeon_winsys.h"
35 #include "util/debug.h"
36 #include "util/u_atomic.h"
38 radv_choose_tiling(struct radv_device
*device
,
39 const struct radv_image_create_info
*create_info
)
41 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
43 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
) {
44 assert(pCreateInfo
->samples
<= 1);
45 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
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
56 (pCreateInfo
->extent
.width
> 8 && pCreateInfo
->extent
.height
<= 2))
57 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
60 /* MSAA resources must be 2D tiled. */
61 if (pCreateInfo
->samples
> 1)
62 return RADEON_SURF_MODE_2D
;
64 return RADEON_SURF_MODE_2D
;
68 radv_use_tc_compat_htile_for_image(struct radv_device
*device
,
69 const VkImageCreateInfo
*pCreateInfo
)
71 /* TC-compat HTILE is only available for GFX8+. */
72 if (device
->physical_device
->rad_info
.chip_class
< VI
)
75 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) ||
76 (pCreateInfo
->flags
& VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR
))
79 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
)
82 if (pCreateInfo
->mipLevels
> 1)
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
)
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.
95 if (pCreateInfo
->format
!= VK_FORMAT_D32_SFLOAT_S8_UINT
&&
96 pCreateInfo
->format
!= VK_FORMAT_D32_SFLOAT
&&
97 pCreateInfo
->format
!= VK_FORMAT_D16_UNORM
)
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
);
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.
111 for (unsigned i
= 0; i
< format_list
->viewFormatCount
; ++i
) {
112 if (pCreateInfo
->format
!= format_list
->pViewFormats
[i
])
124 radv_use_dcc_for_image(struct radv_device
*device
,
125 const struct radv_image
*image
,
126 const struct radv_image_create_info
*create_info
,
127 const VkImageCreateInfo
*pCreateInfo
)
129 bool dcc_compatible_formats
;
132 /* DCC (Delta Color Compression) is only available for GFX8+. */
133 if (device
->physical_device
->rad_info
.chip_class
< VI
)
136 if (device
->instance
->debug_flags
& RADV_DEBUG_NO_DCC
)
139 /* FIXME: DCC is broken for shareable images starting with GFX9 */
140 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
144 /* TODO: Enable DCC for storage images. */
145 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) ||
146 (pCreateInfo
->flags
& VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR
))
149 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
)
152 /* TODO: Enable DCC for mipmaps and array layers. */
153 if (pCreateInfo
->mipLevels
> 1 || pCreateInfo
->arrayLayers
> 1)
156 if (create_info
->scanout
)
159 /* FIXME: DCC for MSAA with 4x and 8x samples doesn't work yet, while
160 * 2x can be enabled with an option.
162 if (pCreateInfo
->samples
> 2 ||
163 (pCreateInfo
->samples
== 2 &&
164 !device
->physical_device
->dcc_msaa_allowed
))
167 /* Determine if the formats are DCC compatible. */
168 dcc_compatible_formats
=
169 radv_is_colorbuffer_format_supported(pCreateInfo
->format
,
172 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) {
173 const struct VkImageFormatListCreateInfoKHR
*format_list
=
174 (const struct VkImageFormatListCreateInfoKHR
*)
175 vk_find_struct_const(pCreateInfo
->pNext
,
176 IMAGE_FORMAT_LIST_CREATE_INFO_KHR
);
178 /* We have to ignore the existence of the list if viewFormatCount = 0 */
179 if (format_list
&& format_list
->viewFormatCount
) {
180 /* compatibility is transitive, so we only need to check
181 * one format with everything else. */
182 for (unsigned i
= 0; i
< format_list
->viewFormatCount
; ++i
) {
183 if (!radv_dcc_formats_compatible(pCreateInfo
->format
,
184 format_list
->pViewFormats
[i
]))
185 dcc_compatible_formats
= false;
188 dcc_compatible_formats
= false;
192 if (!dcc_compatible_formats
)
199 radv_init_surface(struct radv_device
*device
,
200 const struct radv_image
*image
,
201 struct radeon_surf
*surface
,
202 const struct radv_image_create_info
*create_info
)
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
;
210 is_depth
= vk_format_has_depth(desc
);
211 is_stencil
= vk_format_has_stencil(desc
);
213 surface
->blk_w
= vk_format_get_blockwidth(pCreateInfo
->format
);
214 surface
->blk_h
= vk_format_get_blockheight(pCreateInfo
->format
);
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) {
221 surface
->flags
= RADEON_SURF_SET(array_mode
, MODE
);
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
);
228 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
230 case VK_IMAGE_TYPE_2D
:
231 if (pCreateInfo
->arrayLayers
> 1)
232 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
234 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
236 case VK_IMAGE_TYPE_3D
:
237 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
240 unreachable("unhandled image type");
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
;
250 surface
->flags
|= RADEON_SURF_SBUFFER
;
252 surface
->flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
254 if (!radv_use_dcc_for_image(device
, image
, create_info
, pCreateInfo
))
255 surface
->flags
|= RADEON_SURF_DISABLE_DCC
;
257 if (create_info
->scanout
)
258 surface
->flags
|= RADEON_SURF_SCANOUT
;
262 static uint32_t si_get_bo_metadata_word1(struct radv_device
*device
)
264 return (ATI_VENDOR_ID
<< 16) | device
->physical_device
->rad_info
.pci_id
;
267 static inline unsigned
268 si_tile_mode_index(const struct radv_image
*image
, unsigned level
, bool stencil
)
271 return image
->surface
.u
.legacy
.stencil_tiling_index
[level
];
273 return image
->surface
.u
.legacy
.tiling_index
[level
];
276 static unsigned radv_map_swizzle(unsigned swizzle
)
280 return V_008F0C_SQ_SEL_Y
;
282 return V_008F0C_SQ_SEL_Z
;
284 return V_008F0C_SQ_SEL_W
;
286 return V_008F0C_SQ_SEL_0
;
288 return V_008F0C_SQ_SEL_1
;
289 default: /* VK_SWIZZLE_X */
290 return V_008F0C_SQ_SEL_X
;
295 radv_make_buffer_descriptor(struct radv_device
*device
,
296 struct radv_buffer
*buffer
,
302 const struct vk_format_description
*desc
;
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
;
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;
312 num_format
= radv_translate_buffer_numformat(desc
, first_non_void
);
313 data_format
= radv_translate_buffer_dataformat(desc
, first_non_void
);
317 state
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
318 S_008F04_STRIDE(stride
);
320 if (device
->physical_device
->rad_info
.chip_class
!= VI
&& stride
) {
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
);
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
)
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
) {
347 va
+= image
->surface
.u
.gfx9
.stencil_offset
;
349 va
+= image
->surface
.u
.gfx9
.surf_offset
;
351 va
+= base_level_info
->offset
;
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);
360 if (chip_class
>= VI
) {
361 state
[6] &= C_008F28_COMPRESSION_EN
;
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
;
373 state
[6] |= S_008F28_COMPRESSION_EN(1);
374 state
[7] = meta_va
>> 8;
375 state
[7] |= image
->surface
.tile_swizzle
;
379 if (chip_class
>= GFX9
) {
380 state
[3] &= C_008F1C_SW_MODE
;
381 state
[4] &= C_008F20_PITCH_GFX9
;
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
);
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
);
391 state
[5] &= C_008F24_META_DATA_ADDRESS
&
392 C_008F24_META_PIPE_ALIGNED
&
393 C_008F24_META_RB_ALIGNED
;
395 struct gfx9_surf_meta_flags meta
;
397 if (image
->dcc_offset
)
398 meta
= image
->surface
.u
.gfx9
.dcc
;
400 meta
= image
->surface
.u
.gfx9
.htile
;
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
);
408 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
409 unsigned index
= si_tile_mode_index(image
, base_level
, is_stencil
);
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);
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
)
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
;
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
:
432 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D_MSAA
;
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
;
439 return V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
441 unreachable("illegal image type");
445 static unsigned gfx9_border_color_swizzle(const enum vk_swizzle swizzle
[4])
447 unsigned bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
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.
456 if (swizzle
[2] == VK_SWIZZLE_Y
)
457 bc_swizzle
= V_008F20_BC_SWIZZLE_WZYX
;
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
;
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
;
475 * Build the sampler view descriptor for a texture.
478 si_make_texture_descriptor(struct radv_device
*device
,
479 struct radv_image
*image
,
480 bool is_storage_image
,
481 VkImageViewType view_type
,
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
,
488 uint32_t *fmask_state
)
490 const struct vk_format_description
*desc
;
491 enum vk_swizzle swizzle
[4];
493 unsigned num_format
, data_format
, type
;
495 desc
= vk_format_description(vk_format
);
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
);
501 vk_format_compose_swizzles(mapping
, desc
->swizzle
, swizzle
);
504 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
506 num_format
= radv_translate_tex_numformat(vk_format
, desc
, first_non_void
);
507 if (num_format
== ~0) {
511 data_format
= radv_translate_tex_dataformat(vk_format
, desc
, first_non_void
);
512 if (data_format
== ~0) {
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
;
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
) {
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;
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 ?
549 S_008F1C_LAST_LEVEL(image
->info
.samples
> 1 ?
550 util_logbase2(image
->info
.samples
) :
552 S_008F1C_TYPE(type
));
554 state
[5] = S_008F24_BASE_ARRAY(first_layer
);
558 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
559 unsigned bc_swizzle
= gfx9_border_color_swizzle(swizzle
);
561 /* Depth is the last accessible layer on Gfx9.
562 * The hw doesn't need to know the total number of layers.
564 if (type
== V_008F1C_SQ_RSRC_IMG_3D
)
565 state
[4] |= S_008F20_DEPTH(depth
- 1);
567 state
[4] |= S_008F20_DEPTH(last_layer
);
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);
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
);
578 if (image
->dcc_offset
) {
579 unsigned swap
= radv_translate_colorswap(vk_format
, FALSE
);
581 state
[6] = S_008F28_ALPHA_IS_ON_MSB(swap
<= 1);
583 /* The last dword is unused by hw. The shader uses it to clear
584 * bits in the first dword of sampler state.
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
;
590 state
[7] = 0xffffffff;
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
);
600 va
= gpu_address
+ image
->offset
+ image
->fmask
.offset
;
602 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
603 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK
;
604 switch (image
->info
.samples
) {
606 num_format
= V_008F14_IMG_FMASK_8_2_2
;
609 num_format
= V_008F14_IMG_FMASK_8_4_4
;
612 num_format
= V_008F14_IMG_FMASK_32_8_8
;
615 unreachable("invalid nr_samples");
618 switch (image
->info
.samples
) {
620 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2
;
623 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4
;
626 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8
;
630 fmask_format
= V_008F14_IMG_DATA_FORMAT_INVALID
;
632 num_format
= V_008F14_IMG_NUM_FORMAT_UINT
;
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));
648 fmask_state
[5] = S_008F24_BASE_ARRAY(first_layer
);
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
);
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
);
664 } else if (fmask_state
)
665 memset(fmask_state
, 0, 8 * 4);
669 radv_query_opaque_metadata(struct radv_device
*device
,
670 struct radv_image
*image
,
671 struct radeon_bo_metadata
*md
)
673 static const VkComponentMapping fixedmapping
;
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
683 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
685 md
->metadata
[0] = 1; /* metadata image format version 1 */
687 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
688 md
->metadata
[1] = si_get_bo_metadata_word1(device
);
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
,
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
);
702 /* Clear the base address and set the relative DCC offset. */
704 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
705 desc
[7] = image
->dcc_offset
>> 8;
707 /* Dwords [2:9] contain the image descriptor. */
708 memcpy(&md
->metadata
[2], desc
, sizeof(desc
));
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;
719 radv_init_metadata(struct radv_device
*device
,
720 struct radv_image
*image
,
721 struct radeon_bo_metadata
*metadata
)
723 struct radeon_surf
*surface
= &image
->surface
;
725 memset(metadata
, 0, sizeof(*metadata
));
727 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
728 metadata
->u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
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;
743 radv_query_opaque_metadata(device
, image
, metadata
);
746 /* The number of samples can be specified independently of the texture. */
748 radv_image_get_fmask_info(struct radv_device
*device
,
749 struct radv_image
*image
,
751 struct radv_fmask_info
*out
)
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
;
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
;
768 assert(!out
->tile_swizzle
|| !image
->shareable
);
772 radv_image_alloc_fmask(struct radv_device
*device
,
773 struct radv_image
*image
)
775 radv_image_get_fmask_info(device
, image
, image
->info
.samples
, &image
->fmask
);
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
);
783 radv_image_get_cmask_info(struct radv_device
*device
,
784 struct radv_image
*image
,
785 struct radv_cmask_info
*out
)
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
;
791 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
792 out
->alignment
= image
->surface
.cmask_alignment
;
793 out
->size
= image
->surface
.cmask_size
;
810 case 16: /* Hawaii */
819 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
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);
825 /* Each element of CMASK is a nibble. */
826 unsigned slice_bytes
= slice_elements
/ 2;
828 out
->slice_tile_max
= (width
* height
) / (128*128);
829 if (out
->slice_tile_max
)
830 out
->slice_tile_max
-= 1;
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
);
838 radv_image_alloc_cmask(struct radv_device
*device
,
839 struct radv_image
*image
)
841 uint32_t clear_value_size
= 0;
842 radv_image_get_cmask_info(device
, image
, &image
->cmask
);
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;
850 image
->size
= image
->cmask
.offset
+ image
->cmask
.size
+ clear_value_size
;
851 image
->alignment
= MAX2(image
->alignment
, image
->cmask
.alignment
);
855 radv_image_alloc_dcc(struct radv_image
*image
)
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
->dcc_pred_offset
= image
->clear_value_offset
+ 16;
862 image
->size
= image
->dcc_offset
+ image
->surface
.dcc_size
+ 24;
863 image
->alignment
= MAX2(image
->alignment
, image
->surface
.dcc_alignment
);
867 radv_image_alloc_htile(struct radv_image
*image
)
869 image
->htile_offset
= align64(image
->size
, image
->surface
.htile_alignment
);
871 /* + 8 for storing the clear values */
872 image
->clear_value_offset
= image
->htile_offset
+ image
->surface
.htile_size
;
873 image
->size
= image
->clear_value_offset
+ 8;
874 if (radv_image_is_tc_compat_htile(image
)) {
875 /* Metadata for the TC-compatible HTILE hardware bug which
876 * have to be fixed by updating ZRANGE_PRECISION when doing
877 * fast depth clears to 0.0f.
879 image
->tc_compat_zrange_offset
= image
->clear_value_offset
+ 8;
880 image
->size
= image
->clear_value_offset
+ 16;
882 image
->alignment
= align64(image
->alignment
, image
->surface
.htile_alignment
);
886 radv_image_can_enable_dcc_or_cmask(struct radv_image
*image
)
888 if (image
->info
.samples
<= 1 &&
889 image
->info
.width
* image
->info
.height
<= 512 * 512) {
890 /* Do not enable CMASK or DCC for small surfaces where the cost
891 * of the eliminate pass can be higher than the benefit of fast
892 * clear. RadeonSI does this, but the image threshold is
898 return image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
&&
899 (image
->exclusive
|| image
->queue_family_mask
== 1);
903 radv_image_can_enable_dcc(struct radv_image
*image
)
905 return radv_image_can_enable_dcc_or_cmask(image
) &&
906 radv_image_has_dcc(image
);
910 radv_image_can_enable_cmask(struct radv_image
*image
)
912 if (image
->surface
.bpe
> 8 && image
->info
.samples
== 1) {
913 /* Do not enable CMASK for non-MSAA images (fast color clear)
914 * because 128 bit formats are not supported, but FMASK might
920 return radv_image_can_enable_dcc_or_cmask(image
) &&
921 image
->info
.levels
== 1 &&
922 image
->info
.depth
== 1 &&
923 !image
->surface
.is_linear
;
927 radv_image_can_enable_fmask(struct radv_image
*image
)
929 return image
->info
.samples
> 1 && vk_format_is_color(image
->vk_format
);
933 radv_image_can_enable_htile(struct radv_image
*image
)
935 return image
->info
.levels
== 1 &&
936 vk_format_is_depth(image
->vk_format
) &&
937 image
->info
.width
* image
->info
.height
>= 8 * 8;
941 radv_image_create(VkDevice _device
,
942 const struct radv_image_create_info
*create_info
,
943 const VkAllocationCallbacks
* alloc
,
946 RADV_FROM_HANDLE(radv_device
, device
, _device
);
947 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
948 struct radv_image
*image
= NULL
;
949 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
951 radv_assert(pCreateInfo
->mipLevels
> 0);
952 radv_assert(pCreateInfo
->arrayLayers
> 0);
953 radv_assert(pCreateInfo
->samples
> 0);
954 radv_assert(pCreateInfo
->extent
.width
> 0);
955 radv_assert(pCreateInfo
->extent
.height
> 0);
956 radv_assert(pCreateInfo
->extent
.depth
> 0);
958 image
= vk_zalloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
959 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
961 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
963 image
->type
= pCreateInfo
->imageType
;
964 image
->info
.width
= pCreateInfo
->extent
.width
;
965 image
->info
.height
= pCreateInfo
->extent
.height
;
966 image
->info
.depth
= pCreateInfo
->extent
.depth
;
967 image
->info
.samples
= pCreateInfo
->samples
;
968 image
->info
.storage_samples
= pCreateInfo
->samples
;
969 image
->info
.array_size
= pCreateInfo
->arrayLayers
;
970 image
->info
.levels
= pCreateInfo
->mipLevels
;
971 image
->info
.num_channels
= vk_format_get_nr_components(pCreateInfo
->format
);
973 image
->vk_format
= pCreateInfo
->format
;
974 image
->tiling
= pCreateInfo
->tiling
;
975 image
->usage
= pCreateInfo
->usage
;
976 image
->flags
= pCreateInfo
->flags
;
978 image
->exclusive
= pCreateInfo
->sharingMode
== VK_SHARING_MODE_EXCLUSIVE
;
979 if (pCreateInfo
->sharingMode
== VK_SHARING_MODE_CONCURRENT
) {
980 for (uint32_t i
= 0; i
< pCreateInfo
->queueFamilyIndexCount
; ++i
)
981 if (pCreateInfo
->pQueueFamilyIndices
[i
] == VK_QUEUE_FAMILY_EXTERNAL_KHR
)
982 image
->queue_family_mask
|= (1u << RADV_MAX_QUEUE_FAMILIES
) - 1u;
984 image
->queue_family_mask
|= 1u << pCreateInfo
->pQueueFamilyIndices
[i
];
987 image
->shareable
= vk_find_struct_const(pCreateInfo
->pNext
,
988 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR
) != NULL
;
989 if (!vk_format_is_depth(pCreateInfo
->format
) && !create_info
->scanout
&& !image
->shareable
) {
990 image
->info
.surf_index
= &device
->image_mrt_offset_counter
;
993 radv_init_surface(device
, image
, &image
->surface
, create_info
);
995 device
->ws
->surface_init(device
->ws
, &image
->info
, &image
->surface
);
997 image
->size
= image
->surface
.surf_size
;
998 image
->alignment
= image
->surface
.surf_alignment
;
1000 if (!create_info
->no_metadata_planes
) {
1001 /* Try to enable DCC first. */
1002 if (radv_image_can_enable_dcc(image
)) {
1003 radv_image_alloc_dcc(image
);
1004 if (image
->info
.samples
> 1) {
1005 /* CMASK should be enabled because DCC fast
1006 * clear with MSAA needs it.
1008 assert(radv_image_can_enable_cmask(image
));
1009 radv_image_alloc_cmask(device
, image
);
1012 /* When DCC cannot be enabled, try CMASK. */
1013 image
->surface
.dcc_size
= 0;
1014 if (radv_image_can_enable_cmask(image
)) {
1015 radv_image_alloc_cmask(device
, image
);
1019 /* Try to enable FMASK for multisampled images. */
1020 if (radv_image_can_enable_fmask(image
)) {
1021 radv_image_alloc_fmask(device
, image
);
1023 /* Otherwise, try to enable HTILE for depth surfaces. */
1024 if (radv_image_can_enable_htile(image
) &&
1025 !(device
->instance
->debug_flags
& RADV_DEBUG_NO_HIZ
)) {
1026 image
->tc_compatible_htile
= image
->surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
;
1027 radv_image_alloc_htile(image
);
1029 image
->surface
.htile_size
= 0;
1033 image
->surface
.dcc_size
= 0;
1034 image
->surface
.htile_size
= 0;
1037 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
) {
1038 image
->alignment
= MAX2(image
->alignment
, 4096);
1039 image
->size
= align64(image
->size
, image
->alignment
);
1042 image
->bo
= device
->ws
->buffer_create(device
->ws
, image
->size
, image
->alignment
,
1043 0, RADEON_FLAG_VIRTUAL
);
1045 vk_free2(&device
->alloc
, alloc
, image
);
1046 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1050 *pImage
= radv_image_to_handle(image
);
1056 radv_image_view_make_descriptor(struct radv_image_view
*iview
,
1057 struct radv_device
*device
,
1058 const VkComponentMapping
*components
,
1059 bool is_storage_image
)
1061 struct radv_image
*image
= iview
->image
;
1062 bool is_stencil
= iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
;
1064 uint32_t *descriptor
;
1065 uint32_t hw_level
= 0;
1067 if (is_storage_image
) {
1068 descriptor
= iview
->storage_descriptor
;
1070 descriptor
= iview
->descriptor
;
1073 assert(image
->surface
.blk_w
% vk_format_get_blockwidth(image
->vk_format
) == 0);
1074 blk_w
= image
->surface
.blk_w
/ vk_format_get_blockwidth(image
->vk_format
) * vk_format_get_blockwidth(iview
->vk_format
);
1076 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
1077 hw_level
= iview
->base_mip
;
1078 si_make_texture_descriptor(device
, image
, is_storage_image
,
1082 hw_level
, hw_level
+ iview
->level_count
- 1,
1084 iview
->base_layer
+ iview
->layer_count
- 1,
1085 iview
->extent
.width
,
1086 iview
->extent
.height
,
1087 iview
->extent
.depth
,
1091 const struct legacy_surf_level
*base_level_info
= NULL
;
1092 if (device
->physical_device
->rad_info
.chip_class
<= GFX9
) {
1094 base_level_info
= &image
->surface
.u
.legacy
.stencil_level
[iview
->base_mip
];
1096 base_level_info
= &image
->surface
.u
.legacy
.level
[iview
->base_mip
];
1098 si_set_mutable_tex_desc_fields(device
, image
,
1102 blk_w
, is_stencil
, is_storage_image
, descriptor
);
1106 radv_image_view_init(struct radv_image_view
*iview
,
1107 struct radv_device
*device
,
1108 const VkImageViewCreateInfo
* pCreateInfo
)
1110 RADV_FROM_HANDLE(radv_image
, image
, pCreateInfo
->image
);
1111 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
1113 switch (image
->type
) {
1114 case VK_IMAGE_TYPE_1D
:
1115 case VK_IMAGE_TYPE_2D
:
1116 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1 <= image
->info
.array_size
);
1118 case VK_IMAGE_TYPE_3D
:
1119 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1
1120 <= radv_minify(image
->info
.depth
, range
->baseMipLevel
));
1123 unreachable("bad VkImageType");
1125 iview
->image
= image
;
1126 iview
->bo
= image
->bo
;
1127 iview
->type
= pCreateInfo
->viewType
;
1128 iview
->vk_format
= pCreateInfo
->format
;
1129 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
1131 if (iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
) {
1132 iview
->vk_format
= vk_format_stencil_only(iview
->vk_format
);
1133 } else if (iview
->aspect_mask
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
1134 iview
->vk_format
= vk_format_depth_only(iview
->vk_format
);
1137 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
1138 iview
->extent
= (VkExtent3D
) {
1139 .width
= image
->info
.width
,
1140 .height
= image
->info
.height
,
1141 .depth
= image
->info
.depth
,
1144 iview
->extent
= (VkExtent3D
) {
1145 .width
= radv_minify(image
->info
.width
, range
->baseMipLevel
),
1146 .height
= radv_minify(image
->info
.height
, range
->baseMipLevel
),
1147 .depth
= radv_minify(image
->info
.depth
, range
->baseMipLevel
),
1151 if (iview
->vk_format
!= image
->vk_format
) {
1152 unsigned view_bw
= vk_format_get_blockwidth(iview
->vk_format
);
1153 unsigned view_bh
= vk_format_get_blockheight(iview
->vk_format
);
1154 unsigned img_bw
= vk_format_get_blockwidth(image
->vk_format
);
1155 unsigned img_bh
= vk_format_get_blockheight(image
->vk_format
);
1157 iview
->extent
.width
= round_up_u32(iview
->extent
.width
* view_bw
, img_bw
);
1158 iview
->extent
.height
= round_up_u32(iview
->extent
.height
* view_bh
, img_bh
);
1160 /* Comment ported from amdvlk -
1161 * If we have the following image:
1162 * Uncompressed pixels Compressed block sizes (4x4)
1163 * mip0: 22 x 22 6 x 6
1164 * mip1: 11 x 11 3 x 3
1169 * On GFX9 the descriptor is always programmed with the WIDTH and HEIGHT of the base level and the HW is
1170 * calculating the degradation of the block sizes down the mip-chain as follows (straight-up
1171 * divide-by-two integer math):
1177 * This means that mip2 will be missing texels.
1179 * Fix this by calculating the base mip's width and height, then convert that, and round it
1180 * back up to get the level 0 size.
1181 * Clamp the converted size between the original values, and next power of two, which
1182 * means we don't oversize the image.
1184 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
1185 vk_format_is_compressed(image
->vk_format
) &&
1186 !vk_format_is_compressed(iview
->vk_format
)) {
1187 unsigned lvl_width
= radv_minify(image
->info
.width
, range
->baseMipLevel
);
1188 unsigned lvl_height
= radv_minify(image
->info
.height
, range
->baseMipLevel
);
1190 lvl_width
= round_up_u32(lvl_width
* view_bw
, img_bw
);
1191 lvl_height
= round_up_u32(lvl_height
* view_bh
, img_bh
);
1193 lvl_width
<<= range
->baseMipLevel
;
1194 lvl_height
<<= range
->baseMipLevel
;
1196 iview
->extent
.width
= CLAMP(lvl_width
, iview
->extent
.width
, iview
->image
->surface
.u
.gfx9
.surf_pitch
);
1197 iview
->extent
.height
= CLAMP(lvl_height
, iview
->extent
.height
, iview
->image
->surface
.u
.gfx9
.surf_height
);
1201 iview
->base_layer
= range
->baseArrayLayer
;
1202 iview
->layer_count
= radv_get_layerCount(image
, range
);
1203 iview
->base_mip
= range
->baseMipLevel
;
1204 iview
->level_count
= radv_get_levelCount(image
, range
);
1206 radv_image_view_make_descriptor(iview
, device
, &pCreateInfo
->components
, false);
1207 radv_image_view_make_descriptor(iview
, device
, &pCreateInfo
->components
, true);
1210 bool radv_layout_has_htile(const struct radv_image
*image
,
1211 VkImageLayout layout
,
1212 unsigned queue_mask
)
1214 if (radv_image_is_tc_compat_htile(image
))
1215 return layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1217 return radv_image_has_htile(image
) &&
1218 (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
1219 layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
) &&
1220 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
1223 bool radv_layout_is_htile_compressed(const struct radv_image
*image
,
1224 VkImageLayout layout
,
1225 unsigned queue_mask
)
1227 if (radv_image_is_tc_compat_htile(image
))
1228 return layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1230 return radv_image_has_htile(image
) &&
1231 (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
1232 layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
) &&
1233 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
1236 bool radv_layout_can_fast_clear(const struct radv_image
*image
,
1237 VkImageLayout layout
,
1238 unsigned queue_mask
)
1240 return layout
== VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
&&
1241 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
1244 bool radv_layout_dcc_compressed(const struct radv_image
*image
,
1245 VkImageLayout layout
,
1246 unsigned queue_mask
)
1248 /* Don't compress compute transfer dst, as image stores are not supported. */
1249 if (layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
&&
1250 (queue_mask
& (1u << RADV_QUEUE_COMPUTE
)))
1253 return radv_image_has_dcc(image
) && layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1257 unsigned radv_image_queue_family_mask(const struct radv_image
*image
, uint32_t family
, uint32_t queue_family
)
1259 if (!image
->exclusive
)
1260 return image
->queue_family_mask
;
1261 if (family
== VK_QUEUE_FAMILY_EXTERNAL_KHR
)
1262 return (1u << RADV_MAX_QUEUE_FAMILIES
) - 1u;
1263 if (family
== VK_QUEUE_FAMILY_IGNORED
)
1264 return 1u << queue_family
;
1265 return 1u << family
;
1269 radv_CreateImage(VkDevice device
,
1270 const VkImageCreateInfo
*pCreateInfo
,
1271 const VkAllocationCallbacks
*pAllocator
,
1275 const VkNativeBufferANDROID
*gralloc_info
=
1276 vk_find_struct_const(pCreateInfo
->pNext
, NATIVE_BUFFER_ANDROID
);
1279 return radv_image_from_gralloc(device
, pCreateInfo
, gralloc_info
,
1280 pAllocator
, pImage
);
1283 const struct wsi_image_create_info
*wsi_info
=
1284 vk_find_struct_const(pCreateInfo
->pNext
, WSI_IMAGE_CREATE_INFO_MESA
);
1285 bool scanout
= wsi_info
&& wsi_info
->scanout
;
1287 return radv_image_create(device
,
1288 &(struct radv_image_create_info
) {
1289 .vk_info
= pCreateInfo
,
1297 radv_DestroyImage(VkDevice _device
, VkImage _image
,
1298 const VkAllocationCallbacks
*pAllocator
)
1300 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1301 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1306 if (image
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
)
1307 device
->ws
->buffer_destroy(image
->bo
);
1309 if (image
->owned_memory
!= VK_NULL_HANDLE
)
1310 radv_FreeMemory(_device
, image
->owned_memory
, pAllocator
);
1312 vk_free2(&device
->alloc
, pAllocator
, image
);
1315 void radv_GetImageSubresourceLayout(
1318 const VkImageSubresource
* pSubresource
,
1319 VkSubresourceLayout
* pLayout
)
1321 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1322 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1323 int level
= pSubresource
->mipLevel
;
1324 int layer
= pSubresource
->arrayLayer
;
1325 struct radeon_surf
*surface
= &image
->surface
;
1327 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
1328 pLayout
->offset
= surface
->u
.gfx9
.offset
[level
] + surface
->u
.gfx9
.surf_slice_size
* layer
;
1329 pLayout
->rowPitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
1330 pLayout
->arrayPitch
= surface
->u
.gfx9
.surf_slice_size
;
1331 pLayout
->depthPitch
= surface
->u
.gfx9
.surf_slice_size
;
1332 pLayout
->size
= surface
->u
.gfx9
.surf_slice_size
;
1333 if (image
->type
== VK_IMAGE_TYPE_3D
)
1334 pLayout
->size
*= u_minify(image
->info
.depth
, level
);
1336 pLayout
->offset
= surface
->u
.legacy
.level
[level
].offset
+ (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4 * layer
;
1337 pLayout
->rowPitch
= surface
->u
.legacy
.level
[level
].nblk_x
* surface
->bpe
;
1338 pLayout
->arrayPitch
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1339 pLayout
->depthPitch
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1340 pLayout
->size
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1341 if (image
->type
== VK_IMAGE_TYPE_3D
)
1342 pLayout
->size
*= u_minify(image
->info
.depth
, level
);
1348 radv_CreateImageView(VkDevice _device
,
1349 const VkImageViewCreateInfo
*pCreateInfo
,
1350 const VkAllocationCallbacks
*pAllocator
,
1353 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1354 struct radv_image_view
*view
;
1356 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1357 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1359 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1361 radv_image_view_init(view
, device
, pCreateInfo
);
1363 *pView
= radv_image_view_to_handle(view
);
1369 radv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
1370 const VkAllocationCallbacks
*pAllocator
)
1372 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1373 RADV_FROM_HANDLE(radv_image_view
, iview
, _iview
);
1377 vk_free2(&device
->alloc
, pAllocator
, iview
);
1380 void radv_buffer_view_init(struct radv_buffer_view
*view
,
1381 struct radv_device
*device
,
1382 const VkBufferViewCreateInfo
* pCreateInfo
)
1384 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCreateInfo
->buffer
);
1386 view
->bo
= buffer
->bo
;
1387 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
1388 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
1389 view
->vk_format
= pCreateInfo
->format
;
1391 radv_make_buffer_descriptor(device
, buffer
, view
->vk_format
,
1392 pCreateInfo
->offset
, view
->range
, view
->state
);
1396 radv_CreateBufferView(VkDevice _device
,
1397 const VkBufferViewCreateInfo
*pCreateInfo
,
1398 const VkAllocationCallbacks
*pAllocator
,
1399 VkBufferView
*pView
)
1401 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1402 struct radv_buffer_view
*view
;
1404 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1405 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1407 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1409 radv_buffer_view_init(view
, device
, pCreateInfo
);
1411 *pView
= radv_buffer_view_to_handle(view
);
1417 radv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
1418 const VkAllocationCallbacks
*pAllocator
)
1420 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1421 RADV_FROM_HANDLE(radv_buffer_view
, view
, bufferView
);
1426 vk_free2(&device
->alloc
, pAllocator
, view
);