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"
34 #include "util/debug.h"
35 #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
<= GFX8
) {
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
< GFX8
)
75 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) ||
76 (pCreateInfo
->flags
& VK_IMAGE_CREATE_EXTENDED_USAGE_BIT
))
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 (format_list
->pViewFormats
[i
] == VK_FORMAT_UNDEFINED
)
115 if (pCreateInfo
->format
!= format_list
->pViewFormats
[i
])
127 radv_surface_has_scanout(struct radv_device
*device
, const struct radv_image_create_info
*info
)
132 if (!info
->bo_metadata
)
135 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
136 return info
->bo_metadata
->u
.gfx9
.swizzle_mode
== 0 || info
->bo_metadata
->u
.gfx9
.swizzle_mode
% 4 == 2;
138 return info
->bo_metadata
->u
.legacy
.scanout
;
143 radv_use_dcc_for_image(struct radv_device
*device
,
144 const struct radv_image
*image
,
145 const struct radv_image_create_info
*create_info
,
146 const VkImageCreateInfo
*pCreateInfo
)
148 bool dcc_compatible_formats
;
151 /* DCC (Delta Color Compression) is only available for GFX8+. */
152 if (device
->physical_device
->rad_info
.chip_class
< GFX8
)
155 if (device
->instance
->debug_flags
& RADV_DEBUG_NO_DCC
)
158 if (image
->shareable
)
161 /* TODO: Enable DCC for storage images. */
162 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) ||
163 (pCreateInfo
->flags
& VK_IMAGE_CREATE_EXTENDED_USAGE_BIT
))
166 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
)
169 if (vk_format_is_subsampled(pCreateInfo
->format
) ||
170 vk_format_get_plane_count(pCreateInfo
->format
) > 1)
173 /* TODO: Enable DCC for mipmaps and array layers. */
174 if (pCreateInfo
->mipLevels
> 1 || pCreateInfo
->arrayLayers
> 1)
177 if (radv_surface_has_scanout(device
, create_info
))
180 /* FIXME: DCC for MSAA with 4x and 8x samples doesn't work yet, while
181 * 2x can be enabled with an option.
183 if (pCreateInfo
->samples
> 2 ||
184 (pCreateInfo
->samples
== 2 &&
185 !device
->physical_device
->dcc_msaa_allowed
))
188 /* Determine if the formats are DCC compatible. */
189 dcc_compatible_formats
=
190 radv_is_colorbuffer_format_supported(pCreateInfo
->format
,
193 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) {
194 const struct VkImageFormatListCreateInfoKHR
*format_list
=
195 (const struct VkImageFormatListCreateInfoKHR
*)
196 vk_find_struct_const(pCreateInfo
->pNext
,
197 IMAGE_FORMAT_LIST_CREATE_INFO_KHR
);
199 /* We have to ignore the existence of the list if viewFormatCount = 0 */
200 if (format_list
&& format_list
->viewFormatCount
) {
201 /* compatibility is transitive, so we only need to check
202 * one format with everything else. */
203 for (unsigned i
= 0; i
< format_list
->viewFormatCount
; ++i
) {
204 if (format_list
->pViewFormats
[i
] == VK_FORMAT_UNDEFINED
)
207 if (!radv_dcc_formats_compatible(pCreateInfo
->format
,
208 format_list
->pViewFormats
[i
]))
209 dcc_compatible_formats
= false;
212 dcc_compatible_formats
= false;
216 if (!dcc_compatible_formats
)
223 radv_use_tc_compat_cmask_for_image(struct radv_device
*device
,
224 struct radv_image
*image
)
226 if (!(device
->instance
->perftest_flags
& RADV_PERFTEST_TC_COMPAT_CMASK
))
229 /* TC-compat CMASK is only available for GFX8+. */
230 if (device
->physical_device
->rad_info
.chip_class
< GFX8
)
233 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
)
236 if (radv_image_has_dcc(image
))
239 if (!radv_image_has_cmask(image
))
246 radv_prefill_surface_from_metadata(struct radv_device
*device
,
247 struct radeon_surf
*surface
,
248 const struct radv_image_create_info
*create_info
)
250 const struct radeon_bo_metadata
*md
= create_info
->bo_metadata
;
251 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
252 if (md
->u
.gfx9
.swizzle_mode
> 0)
253 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
255 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
257 surface
->u
.gfx9
.surf
.swizzle_mode
= md
->u
.gfx9
.swizzle_mode
;
259 surface
->u
.legacy
.pipe_config
= md
->u
.legacy
.pipe_config
;
260 surface
->u
.legacy
.bankw
= md
->u
.legacy
.bankw
;
261 surface
->u
.legacy
.bankh
= md
->u
.legacy
.bankh
;
262 surface
->u
.legacy
.tile_split
= md
->u
.legacy
.tile_split
;
263 surface
->u
.legacy
.mtilea
= md
->u
.legacy
.mtilea
;
264 surface
->u
.legacy
.num_banks
= md
->u
.legacy
.num_banks
;
266 if (md
->u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
267 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
268 else if (md
->u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
269 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_1D
, MODE
);
271 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_LINEAR_ALIGNED
, MODE
);
277 radv_init_surface(struct radv_device
*device
,
278 const struct radv_image
*image
,
279 struct radeon_surf
*surface
,
281 const struct radv_image_create_info
*create_info
)
283 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
284 unsigned array_mode
= radv_choose_tiling(device
, create_info
);
285 VkFormat format
= vk_format_get_plane_format(pCreateInfo
->format
, plane_id
);
286 const struct vk_format_description
*desc
= vk_format_description(format
);
287 bool is_depth
, is_stencil
;
289 is_depth
= vk_format_has_depth(desc
);
290 is_stencil
= vk_format_has_stencil(desc
);
292 surface
->blk_w
= vk_format_get_blockwidth(format
);
293 surface
->blk_h
= vk_format_get_blockheight(format
);
295 surface
->bpe
= vk_format_get_blocksize(vk_format_depth_only(format
));
296 /* align byte per element on dword */
297 if (surface
->bpe
== 3) {
300 if (create_info
->bo_metadata
) {
301 radv_prefill_surface_from_metadata(device
, surface
, create_info
);
303 surface
->flags
= RADEON_SURF_SET(array_mode
, MODE
);
306 switch (pCreateInfo
->imageType
){
307 case VK_IMAGE_TYPE_1D
:
308 if (pCreateInfo
->arrayLayers
> 1)
309 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
311 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
313 case VK_IMAGE_TYPE_2D
:
314 if (pCreateInfo
->arrayLayers
> 1)
315 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
317 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
319 case VK_IMAGE_TYPE_3D
:
320 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
323 unreachable("unhandled image type");
327 surface
->flags
|= RADEON_SURF_ZBUFFER
;
328 if (radv_use_tc_compat_htile_for_image(device
, pCreateInfo
))
329 surface
->flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
333 surface
->flags
|= RADEON_SURF_SBUFFER
;
335 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
336 pCreateInfo
->imageType
== VK_IMAGE_TYPE_3D
&&
337 vk_format_get_blocksizebits(pCreateInfo
->format
) == 128 &&
338 vk_format_is_compressed(pCreateInfo
->format
))
339 surface
->flags
|= RADEON_SURF_NO_RENDER_TARGET
;
341 surface
->flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
343 if (!radv_use_dcc_for_image(device
, image
, create_info
, pCreateInfo
))
344 surface
->flags
|= RADEON_SURF_DISABLE_DCC
;
346 if (radv_surface_has_scanout(device
, create_info
))
347 surface
->flags
|= RADEON_SURF_SCANOUT
;
352 static uint32_t si_get_bo_metadata_word1(struct radv_device
*device
)
354 return (ATI_VENDOR_ID
<< 16) | device
->physical_device
->rad_info
.pci_id
;
357 static inline unsigned
358 si_tile_mode_index(const struct radv_image_plane
*plane
, unsigned level
, bool stencil
)
361 return plane
->surface
.u
.legacy
.stencil_tiling_index
[level
];
363 return plane
->surface
.u
.legacy
.tiling_index
[level
];
366 static unsigned radv_map_swizzle(unsigned swizzle
)
370 return V_008F0C_SQ_SEL_Y
;
372 return V_008F0C_SQ_SEL_Z
;
374 return V_008F0C_SQ_SEL_W
;
376 return V_008F0C_SQ_SEL_0
;
378 return V_008F0C_SQ_SEL_1
;
379 default: /* VK_SWIZZLE_X */
380 return V_008F0C_SQ_SEL_X
;
385 radv_make_buffer_descriptor(struct radv_device
*device
,
386 struct radv_buffer
*buffer
,
392 const struct vk_format_description
*desc
;
394 uint64_t gpu_address
= radv_buffer_get_va(buffer
->bo
);
395 uint64_t va
= gpu_address
+ buffer
->offset
;
396 unsigned num_format
, data_format
;
398 desc
= vk_format_description(vk_format
);
399 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
400 stride
= desc
->block
.bits
/ 8;
402 num_format
= radv_translate_buffer_numformat(desc
, first_non_void
);
403 data_format
= radv_translate_buffer_dataformat(desc
, first_non_void
);
405 assert(data_format
!= V_008F0C_BUF_DATA_FORMAT_INVALID
);
406 assert(num_format
!= ~0);
410 state
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
411 S_008F04_STRIDE(stride
);
413 if (device
->physical_device
->rad_info
.chip_class
!= GFX8
&& stride
) {
418 state
[3] = S_008F0C_DST_SEL_X(radv_map_swizzle(desc
->swizzle
[0])) |
419 S_008F0C_DST_SEL_Y(radv_map_swizzle(desc
->swizzle
[1])) |
420 S_008F0C_DST_SEL_Z(radv_map_swizzle(desc
->swizzle
[2])) |
421 S_008F0C_DST_SEL_W(radv_map_swizzle(desc
->swizzle
[3])) |
422 S_008F0C_NUM_FORMAT(num_format
) |
423 S_008F0C_DATA_FORMAT(data_format
);
427 si_set_mutable_tex_desc_fields(struct radv_device
*device
,
428 struct radv_image
*image
,
429 const struct legacy_surf_level
*base_level_info
,
431 unsigned base_level
, unsigned first_level
,
432 unsigned block_width
, bool is_stencil
,
433 bool is_storage_image
, uint32_t *state
)
435 struct radv_image_plane
*plane
= &image
->planes
[plane_id
];
436 uint64_t gpu_address
= image
->bo
? radv_buffer_get_va(image
->bo
) + image
->offset
: 0;
437 uint64_t va
= gpu_address
+ plane
->offset
;
438 enum chip_class chip_class
= device
->physical_device
->rad_info
.chip_class
;
439 uint64_t meta_va
= 0;
440 if (chip_class
>= GFX9
) {
442 va
+= plane
->surface
.u
.gfx9
.stencil_offset
;
444 va
+= plane
->surface
.u
.gfx9
.surf_offset
;
446 va
+= base_level_info
->offset
;
449 if (chip_class
>= GFX9
||
450 base_level_info
->mode
== RADEON_SURF_MODE_2D
)
451 state
[0] |= plane
->surface
.tile_swizzle
;
452 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
453 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
455 if (chip_class
>= GFX8
) {
456 state
[6] &= C_008F28_COMPRESSION_EN
;
458 if (!is_storage_image
&& radv_dcc_enabled(image
, first_level
)) {
459 meta_va
= gpu_address
+ image
->dcc_offset
;
460 if (chip_class
<= GFX8
)
461 meta_va
+= base_level_info
->dcc_offset
;
462 } else if (!is_storage_image
&&
463 radv_image_is_tc_compat_htile(image
)) {
464 meta_va
= gpu_address
+ image
->htile_offset
;
468 state
[6] |= S_008F28_COMPRESSION_EN(1);
469 state
[7] = meta_va
>> 8;
470 state
[7] |= plane
->surface
.tile_swizzle
;
474 if (chip_class
>= GFX9
) {
475 state
[3] &= C_008F1C_SW_MODE
;
476 state
[4] &= C_008F20_PITCH
;
479 state
[3] |= S_008F1C_SW_MODE(plane
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
480 state
[4] |= S_008F20_PITCH(plane
->surface
.u
.gfx9
.stencil
.epitch
);
482 state
[3] |= S_008F1C_SW_MODE(plane
->surface
.u
.gfx9
.surf
.swizzle_mode
);
483 state
[4] |= S_008F20_PITCH(plane
->surface
.u
.gfx9
.surf
.epitch
);
486 state
[5] &= C_008F24_META_DATA_ADDRESS
&
487 C_008F24_META_PIPE_ALIGNED
&
488 C_008F24_META_RB_ALIGNED
;
490 struct gfx9_surf_meta_flags meta
;
492 if (image
->dcc_offset
)
493 meta
= plane
->surface
.u
.gfx9
.dcc
;
495 meta
= plane
->surface
.u
.gfx9
.htile
;
497 state
[5] |= S_008F24_META_DATA_ADDRESS(meta_va
>> 40) |
498 S_008F24_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
499 S_008F24_META_RB_ALIGNED(meta
.rb_aligned
);
503 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
504 unsigned index
= si_tile_mode_index(plane
, base_level
, is_stencil
);
506 state
[3] &= C_008F1C_TILING_INDEX
;
507 state
[3] |= S_008F1C_TILING_INDEX(index
);
508 state
[4] &= C_008F20_PITCH
;
509 state
[4] |= S_008F20_PITCH(pitch
- 1);
513 static unsigned radv_tex_dim(VkImageType image_type
, VkImageViewType view_type
,
514 unsigned nr_layers
, unsigned nr_samples
, bool is_storage_image
, bool gfx9
)
516 if (view_type
== VK_IMAGE_VIEW_TYPE_CUBE
|| view_type
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
)
517 return is_storage_image
? V_008F1C_SQ_RSRC_IMG_2D_ARRAY
: V_008F1C_SQ_RSRC_IMG_CUBE
;
519 /* GFX9 allocates 1D textures as 2D. */
520 if (gfx9
&& image_type
== VK_IMAGE_TYPE_1D
)
521 image_type
= VK_IMAGE_TYPE_2D
;
522 switch (image_type
) {
523 case VK_IMAGE_TYPE_1D
:
524 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_1D_ARRAY
: V_008F1C_SQ_RSRC_IMG_1D
;
525 case VK_IMAGE_TYPE_2D
:
527 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D_MSAA
;
529 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D
;
530 case VK_IMAGE_TYPE_3D
:
531 if (view_type
== VK_IMAGE_VIEW_TYPE_3D
)
532 return V_008F1C_SQ_RSRC_IMG_3D
;
534 return V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
536 unreachable("illegal image type");
540 static unsigned gfx9_border_color_swizzle(const enum vk_swizzle swizzle
[4])
542 unsigned bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
544 if (swizzle
[3] == VK_SWIZZLE_X
) {
545 /* For the pre-defined border color values (white, opaque
546 * black, transparent black), the only thing that matters is
547 * that the alpha channel winds up in the correct place
548 * (because the RGB channels are all the same) so either of
549 * these enumerations will work.
551 if (swizzle
[2] == VK_SWIZZLE_Y
)
552 bc_swizzle
= V_008F20_BC_SWIZZLE_WZYX
;
554 bc_swizzle
= V_008F20_BC_SWIZZLE_WXYZ
;
555 } else if (swizzle
[0] == VK_SWIZZLE_X
) {
556 if (swizzle
[1] == VK_SWIZZLE_Y
)
557 bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
559 bc_swizzle
= V_008F20_BC_SWIZZLE_XWYZ
;
560 } else if (swizzle
[1] == VK_SWIZZLE_X
) {
561 bc_swizzle
= V_008F20_BC_SWIZZLE_YXWZ
;
562 } else if (swizzle
[2] == VK_SWIZZLE_X
) {
563 bc_swizzle
= V_008F20_BC_SWIZZLE_ZYXW
;
570 * Build the sampler view descriptor for a texture.
573 si_make_texture_descriptor(struct radv_device
*device
,
574 struct radv_image
*image
,
575 bool is_storage_image
,
576 VkImageViewType view_type
,
578 const VkComponentMapping
*mapping
,
579 unsigned first_level
, unsigned last_level
,
580 unsigned first_layer
, unsigned last_layer
,
581 unsigned width
, unsigned height
, unsigned depth
,
583 uint32_t *fmask_state
)
585 const struct vk_format_description
*desc
;
586 enum vk_swizzle swizzle
[4];
588 unsigned num_format
, data_format
, type
;
590 desc
= vk_format_description(vk_format
);
592 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
) {
593 const unsigned char swizzle_xxxx
[4] = {0, 0, 0, 0};
594 vk_format_compose_swizzles(mapping
, swizzle_xxxx
, swizzle
);
596 vk_format_compose_swizzles(mapping
, desc
->swizzle
, swizzle
);
599 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
601 num_format
= radv_translate_tex_numformat(vk_format
, desc
, first_non_void
);
602 if (num_format
== ~0) {
606 data_format
= radv_translate_tex_dataformat(vk_format
, desc
, first_non_void
);
607 if (data_format
== ~0) {
611 /* S8 with either Z16 or Z32 HTILE need a special format. */
612 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
613 vk_format
== VK_FORMAT_S8_UINT
&&
614 radv_image_is_tc_compat_htile(image
)) {
615 if (image
->vk_format
== VK_FORMAT_D32_SFLOAT_S8_UINT
)
616 data_format
= V_008F14_IMG_DATA_FORMAT_S8_32
;
617 else if (image
->vk_format
== VK_FORMAT_D16_UNORM_S8_UINT
)
618 data_format
= V_008F14_IMG_DATA_FORMAT_S8_16
;
620 type
= radv_tex_dim(image
->type
, view_type
, image
->info
.array_size
, image
->info
.samples
,
621 is_storage_image
, device
->physical_device
->rad_info
.chip_class
>= GFX9
);
622 if (type
== V_008F1C_SQ_RSRC_IMG_1D_ARRAY
) {
624 depth
= image
->info
.array_size
;
625 } else if (type
== V_008F1C_SQ_RSRC_IMG_2D_ARRAY
||
626 type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
627 if (view_type
!= VK_IMAGE_VIEW_TYPE_3D
)
628 depth
= image
->info
.array_size
;
629 } else if (type
== V_008F1C_SQ_RSRC_IMG_CUBE
)
630 depth
= image
->info
.array_size
/ 6;
633 state
[1] = (S_008F14_DATA_FORMAT(data_format
) |
634 S_008F14_NUM_FORMAT(num_format
));
635 state
[2] = (S_008F18_WIDTH(width
- 1) |
636 S_008F18_HEIGHT(height
- 1) |
637 S_008F18_PERF_MOD(4));
638 state
[3] = (S_008F1C_DST_SEL_X(radv_map_swizzle(swizzle
[0])) |
639 S_008F1C_DST_SEL_Y(radv_map_swizzle(swizzle
[1])) |
640 S_008F1C_DST_SEL_Z(radv_map_swizzle(swizzle
[2])) |
641 S_008F1C_DST_SEL_W(radv_map_swizzle(swizzle
[3])) |
642 S_008F1C_BASE_LEVEL(image
->info
.samples
> 1 ?
644 S_008F1C_LAST_LEVEL(image
->info
.samples
> 1 ?
645 util_logbase2(image
->info
.samples
) :
647 S_008F1C_TYPE(type
));
649 state
[5] = S_008F24_BASE_ARRAY(first_layer
);
653 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
654 unsigned bc_swizzle
= gfx9_border_color_swizzle(swizzle
);
656 /* Depth is the last accessible layer on Gfx9.
657 * The hw doesn't need to know the total number of layers.
659 if (type
== V_008F1C_SQ_RSRC_IMG_3D
)
660 state
[4] |= S_008F20_DEPTH(depth
- 1);
662 state
[4] |= S_008F20_DEPTH(last_layer
);
664 state
[4] |= S_008F20_BC_SWIZZLE(bc_swizzle
);
665 state
[5] |= S_008F24_MAX_MIP(image
->info
.samples
> 1 ?
666 util_logbase2(image
->info
.samples
) :
667 image
->info
.levels
- 1);
669 state
[3] |= S_008F1C_POW2_PAD(image
->info
.levels
> 1);
670 state
[4] |= S_008F20_DEPTH(depth
- 1);
671 state
[5] |= S_008F24_LAST_ARRAY(last_layer
);
673 if (image
->dcc_offset
) {
674 unsigned swap
= radv_translate_colorswap(vk_format
, FALSE
);
676 state
[6] = S_008F28_ALPHA_IS_ON_MSB(swap
<= 1);
678 /* The last dword is unused by hw. The shader uses it to clear
679 * bits in the first dword of sampler state.
681 if (device
->physical_device
->rad_info
.chip_class
<= GFX7
&& image
->info
.samples
<= 1) {
682 if (first_level
== last_level
)
683 state
[7] = C_008F30_MAX_ANISO_RATIO
;
685 state
[7] = 0xffffffff;
689 /* Initialize the sampler view for FMASK. */
690 if (radv_image_has_fmask(image
)) {
691 uint32_t fmask_format
, num_format
;
692 uint64_t gpu_address
= radv_buffer_get_va(image
->bo
);
695 assert(image
->plane_count
== 1);
697 va
= gpu_address
+ image
->offset
+ image
->fmask
.offset
;
699 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
700 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK
;
701 switch (image
->info
.samples
) {
703 num_format
= V_008F14_IMG_FMASK_8_2_2
;
706 num_format
= V_008F14_IMG_FMASK_8_4_4
;
709 num_format
= V_008F14_IMG_FMASK_32_8_8
;
712 unreachable("invalid nr_samples");
715 switch (image
->info
.samples
) {
717 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2
;
720 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4
;
723 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8
;
727 fmask_format
= V_008F14_IMG_DATA_FORMAT_INVALID
;
729 num_format
= V_008F14_IMG_NUM_FORMAT_UINT
;
732 fmask_state
[0] = va
>> 8;
733 fmask_state
[0] |= image
->fmask
.tile_swizzle
;
734 fmask_state
[1] = S_008F14_BASE_ADDRESS_HI(va
>> 40) |
735 S_008F14_DATA_FORMAT(fmask_format
) |
736 S_008F14_NUM_FORMAT(num_format
);
737 fmask_state
[2] = S_008F18_WIDTH(width
- 1) |
738 S_008F18_HEIGHT(height
- 1);
739 fmask_state
[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X
) |
740 S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X
) |
741 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X
) |
742 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X
) |
743 S_008F1C_TYPE(radv_tex_dim(image
->type
, view_type
, image
->info
.array_size
, 0, false, false));
745 fmask_state
[5] = S_008F24_BASE_ARRAY(first_layer
);
749 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
750 fmask_state
[3] |= S_008F1C_SW_MODE(image
->planes
[0].surface
.u
.gfx9
.fmask
.swizzle_mode
);
751 fmask_state
[4] |= S_008F20_DEPTH(last_layer
) |
752 S_008F20_PITCH(image
->planes
[0].surface
.u
.gfx9
.fmask
.epitch
);
753 fmask_state
[5] |= S_008F24_META_PIPE_ALIGNED(image
->planes
[0].surface
.u
.gfx9
.cmask
.pipe_aligned
) |
754 S_008F24_META_RB_ALIGNED(image
->planes
[0].surface
.u
.gfx9
.cmask
.rb_aligned
);
756 if (radv_image_is_tc_compat_cmask(image
)) {
757 va
= gpu_address
+ image
->offset
+ image
->cmask
.offset
;
759 fmask_state
[5] |= S_008F24_META_DATA_ADDRESS(va
>> 40);
760 fmask_state
[6] |= S_008F28_COMPRESSION_EN(1);
761 fmask_state
[7] |= va
>> 8;
764 fmask_state
[3] |= S_008F1C_TILING_INDEX(image
->fmask
.tile_mode_index
);
765 fmask_state
[4] |= S_008F20_DEPTH(depth
- 1) |
766 S_008F20_PITCH(image
->fmask
.pitch_in_pixels
- 1);
767 fmask_state
[5] |= S_008F24_LAST_ARRAY(last_layer
);
769 if (radv_image_is_tc_compat_cmask(image
)) {
770 va
= gpu_address
+ image
->offset
+ image
->cmask
.offset
;
772 fmask_state
[6] |= S_008F28_COMPRESSION_EN(1);
773 fmask_state
[7] |= va
>> 8;
776 } else if (fmask_state
)
777 memset(fmask_state
, 0, 8 * 4);
781 radv_query_opaque_metadata(struct radv_device
*device
,
782 struct radv_image
*image
,
783 struct radeon_bo_metadata
*md
)
785 static const VkComponentMapping fixedmapping
;
788 assert(image
->plane_count
== 1);
790 /* Metadata image format format version 1:
791 * [0] = 1 (metadata format identifier)
792 * [1] = (VENDOR_ID << 16) | PCI_ID
793 * [2:9] = image descriptor for the whole resource
794 * [2] is always 0, because the base address is cleared
795 * [9] is the DCC offset bits [39:8] from the beginning of
797 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
799 md
->metadata
[0] = 1; /* metadata image format version 1 */
801 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
802 md
->metadata
[1] = si_get_bo_metadata_word1(device
);
805 si_make_texture_descriptor(device
, image
, false,
806 (VkImageViewType
)image
->type
, image
->vk_format
,
807 &fixedmapping
, 0, image
->info
.levels
- 1, 0,
808 image
->info
.array_size
- 1,
809 image
->info
.width
, image
->info
.height
,
813 si_set_mutable_tex_desc_fields(device
, image
, &image
->planes
[0].surface
.u
.legacy
.level
[0], 0, 0, 0,
814 image
->planes
[0].surface
.blk_w
, false, false, desc
);
816 /* Clear the base address and set the relative DCC offset. */
818 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
819 desc
[7] = image
->dcc_offset
>> 8;
821 /* Dwords [2:9] contain the image descriptor. */
822 memcpy(&md
->metadata
[2], desc
, sizeof(desc
));
824 /* Dwords [10:..] contain the mipmap level offsets. */
825 if (device
->physical_device
->rad_info
.chip_class
<= GFX8
) {
826 for (i
= 0; i
<= image
->info
.levels
- 1; i
++)
827 md
->metadata
[10+i
] = image
->planes
[0].surface
.u
.legacy
.level
[i
].offset
>> 8;
828 md
->size_metadata
= (11 + image
->info
.levels
- 1) * 4;
833 radv_init_metadata(struct radv_device
*device
,
834 struct radv_image
*image
,
835 struct radeon_bo_metadata
*metadata
)
837 struct radeon_surf
*surface
= &image
->planes
[0].surface
;
839 memset(metadata
, 0, sizeof(*metadata
));
841 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
842 metadata
->u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
844 metadata
->u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
845 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
846 metadata
->u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
847 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
848 metadata
->u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
849 metadata
->u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
850 metadata
->u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
851 metadata
->u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
852 metadata
->u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
853 metadata
->u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
854 metadata
->u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
855 metadata
->u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
857 radv_query_opaque_metadata(device
, image
, metadata
);
861 radv_image_override_offset_stride(struct radv_device
*device
,
862 struct radv_image
*image
,
863 uint64_t offset
, uint32_t stride
)
865 struct radeon_surf
*surface
= &image
->planes
[0].surface
;
866 unsigned bpe
= vk_format_get_blocksizebits(image
->vk_format
) / 8;
868 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
870 surface
->u
.gfx9
.surf_pitch
= stride
;
871 surface
->u
.gfx9
.surf_slice_size
=
872 (uint64_t)stride
* surface
->u
.gfx9
.surf_height
* bpe
;
874 surface
->u
.gfx9
.surf_offset
= offset
;
876 surface
->u
.legacy
.level
[0].nblk_x
= stride
;
877 surface
->u
.legacy
.level
[0].slice_size_dw
=
878 ((uint64_t)stride
* surface
->u
.legacy
.level
[0].nblk_y
* bpe
) / 4;
881 for (unsigned i
= 0; i
< ARRAY_SIZE(surface
->u
.legacy
.level
); ++i
)
882 surface
->u
.legacy
.level
[i
].offset
+= offset
;
888 /* The number of samples can be specified independently of the texture. */
890 radv_image_get_fmask_info(struct radv_device
*device
,
891 struct radv_image
*image
,
893 struct radv_fmask_info
*out
)
895 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
896 out
->alignment
= image
->planes
[0].surface
.fmask_alignment
;
897 out
->size
= image
->planes
[0].surface
.fmask_size
;
898 out
->tile_swizzle
= image
->planes
[0].surface
.fmask_tile_swizzle
;
902 out
->slice_tile_max
= image
->planes
[0].surface
.u
.legacy
.fmask
.slice_tile_max
;
903 out
->tile_mode_index
= image
->planes
[0].surface
.u
.legacy
.fmask
.tiling_index
;
904 out
->pitch_in_pixels
= image
->planes
[0].surface
.u
.legacy
.fmask
.pitch_in_pixels
;
905 out
->bank_height
= image
->planes
[0].surface
.u
.legacy
.fmask
.bankh
;
906 out
->tile_swizzle
= image
->planes
[0].surface
.fmask_tile_swizzle
;
907 out
->alignment
= image
->planes
[0].surface
.fmask_alignment
;
908 out
->size
= image
->planes
[0].surface
.fmask_size
;
910 assert(!out
->tile_swizzle
|| !image
->shareable
);
914 radv_image_alloc_fmask(struct radv_device
*device
,
915 struct radv_image
*image
)
917 radv_image_get_fmask_info(device
, image
, image
->info
.samples
, &image
->fmask
);
919 image
->fmask
.offset
= align64(image
->size
, image
->fmask
.alignment
);
920 image
->size
= image
->fmask
.offset
+ image
->fmask
.size
;
921 image
->alignment
= MAX2(image
->alignment
, image
->fmask
.alignment
);
925 radv_image_get_cmask_info(struct radv_device
*device
,
926 struct radv_image
*image
,
927 struct radv_cmask_info
*out
)
929 assert(image
->plane_count
== 1);
931 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
932 out
->alignment
= image
->planes
[0].surface
.cmask_alignment
;
933 out
->size
= image
->planes
[0].surface
.cmask_size
;
937 out
->slice_tile_max
= image
->planes
[0].surface
.u
.legacy
.cmask_slice_tile_max
;
938 out
->alignment
= image
->planes
[0].surface
.cmask_alignment
;
939 out
->size
= image
->planes
[0].surface
.cmask_size
;
943 radv_image_alloc_cmask(struct radv_device
*device
,
944 struct radv_image
*image
)
946 uint32_t clear_value_size
= 0;
947 radv_image_get_cmask_info(device
, image
, &image
->cmask
);
949 image
->cmask
.offset
= align64(image
->size
, image
->cmask
.alignment
);
950 /* + 8 for storing the clear values */
951 if (!image
->clear_value_offset
) {
952 image
->clear_value_offset
= image
->cmask
.offset
+ image
->cmask
.size
;
953 clear_value_size
= 8;
955 image
->size
= image
->cmask
.offset
+ image
->cmask
.size
+ clear_value_size
;
956 image
->alignment
= MAX2(image
->alignment
, image
->cmask
.alignment
);
960 radv_image_alloc_dcc(struct radv_image
*image
)
962 assert(image
->plane_count
== 1);
964 image
->dcc_offset
= align64(image
->size
, image
->planes
[0].surface
.dcc_alignment
);
965 /* + 24 for storing the clear values + fce pred + dcc pred for each mip */
966 image
->clear_value_offset
= image
->dcc_offset
+ image
->planes
[0].surface
.dcc_size
;
967 image
->fce_pred_offset
= image
->clear_value_offset
+ 8 * image
->info
.levels
;
968 image
->dcc_pred_offset
= image
->clear_value_offset
+ 16 * image
->info
.levels
;
969 image
->size
= image
->dcc_offset
+ image
->planes
[0].surface
.dcc_size
+ 24 * image
->info
.levels
;
970 image
->alignment
= MAX2(image
->alignment
, image
->planes
[0].surface
.dcc_alignment
);
974 radv_image_alloc_htile(struct radv_image
*image
)
976 image
->htile_offset
= align64(image
->size
, image
->planes
[0].surface
.htile_alignment
);
978 /* + 8 for storing the clear values */
979 image
->clear_value_offset
= image
->htile_offset
+ image
->planes
[0].surface
.htile_size
;
980 image
->size
= image
->clear_value_offset
+ 8;
981 if (radv_image_is_tc_compat_htile(image
)) {
982 /* Metadata for the TC-compatible HTILE hardware bug which
983 * have to be fixed by updating ZRANGE_PRECISION when doing
984 * fast depth clears to 0.0f.
986 image
->tc_compat_zrange_offset
= image
->clear_value_offset
+ 8;
987 image
->size
= image
->clear_value_offset
+ 16;
989 image
->alignment
= align64(image
->alignment
, image
->planes
[0].surface
.htile_alignment
);
993 radv_image_can_enable_dcc_or_cmask(struct radv_image
*image
)
995 if (image
->info
.samples
<= 1 &&
996 image
->info
.width
* image
->info
.height
<= 512 * 512) {
997 /* Do not enable CMASK or DCC for small surfaces where the cost
998 * of the eliminate pass can be higher than the benefit of fast
999 * clear. RadeonSI does this, but the image threshold is
1005 return image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
&&
1006 (image
->exclusive
|| image
->queue_family_mask
== 1);
1010 radv_image_can_enable_dcc(struct radv_image
*image
)
1012 return radv_image_can_enable_dcc_or_cmask(image
) &&
1013 radv_image_has_dcc(image
);
1017 radv_image_can_enable_cmask(struct radv_image
*image
)
1019 if (image
->planes
[0].surface
.bpe
> 8 && image
->info
.samples
== 1) {
1020 /* Do not enable CMASK for non-MSAA images (fast color clear)
1021 * because 128 bit formats are not supported, but FMASK might
1027 return radv_image_can_enable_dcc_or_cmask(image
) &&
1028 image
->info
.levels
== 1 &&
1029 image
->info
.depth
== 1 &&
1030 !image
->planes
[0].surface
.is_linear
;
1034 radv_image_can_enable_fmask(struct radv_image
*image
)
1036 return image
->info
.samples
> 1 && vk_format_is_color(image
->vk_format
);
1040 radv_image_can_enable_htile(struct radv_image
*image
)
1042 return radv_image_has_htile(image
) &&
1043 image
->info
.levels
== 1 &&
1044 image
->info
.width
* image
->info
.height
>= 8 * 8;
1047 static void radv_image_disable_dcc(struct radv_image
*image
)
1049 for (unsigned i
= 0; i
< image
->plane_count
; ++i
)
1050 image
->planes
[i
].surface
.dcc_size
= 0;
1053 static void radv_image_disable_htile(struct radv_image
*image
)
1055 for (unsigned i
= 0; i
< image
->plane_count
; ++i
)
1056 image
->planes
[i
].surface
.htile_size
= 0;
1060 radv_image_create(VkDevice _device
,
1061 const struct radv_image_create_info
*create_info
,
1062 const VkAllocationCallbacks
* alloc
,
1065 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1066 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
1067 struct radv_image
*image
= NULL
;
1068 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
1070 const unsigned plane_count
= vk_format_get_plane_count(pCreateInfo
->format
);
1071 const size_t image_struct_size
= sizeof(*image
) + sizeof(struct radv_image_plane
) * plane_count
;
1073 radv_assert(pCreateInfo
->mipLevels
> 0);
1074 radv_assert(pCreateInfo
->arrayLayers
> 0);
1075 radv_assert(pCreateInfo
->samples
> 0);
1076 radv_assert(pCreateInfo
->extent
.width
> 0);
1077 radv_assert(pCreateInfo
->extent
.height
> 0);
1078 radv_assert(pCreateInfo
->extent
.depth
> 0);
1080 image
= vk_zalloc2(&device
->alloc
, alloc
, image_struct_size
, 8,
1081 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1083 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1085 image
->type
= pCreateInfo
->imageType
;
1086 image
->info
.width
= pCreateInfo
->extent
.width
;
1087 image
->info
.height
= pCreateInfo
->extent
.height
;
1088 image
->info
.depth
= pCreateInfo
->extent
.depth
;
1089 image
->info
.samples
= pCreateInfo
->samples
;
1090 image
->info
.storage_samples
= pCreateInfo
->samples
;
1091 image
->info
.array_size
= pCreateInfo
->arrayLayers
;
1092 image
->info
.levels
= pCreateInfo
->mipLevels
;
1093 image
->info
.num_channels
= vk_format_get_nr_components(pCreateInfo
->format
);
1095 image
->vk_format
= pCreateInfo
->format
;
1096 image
->tiling
= pCreateInfo
->tiling
;
1097 image
->usage
= pCreateInfo
->usage
;
1098 image
->flags
= pCreateInfo
->flags
;
1100 image
->exclusive
= pCreateInfo
->sharingMode
== VK_SHARING_MODE_EXCLUSIVE
;
1101 if (pCreateInfo
->sharingMode
== VK_SHARING_MODE_CONCURRENT
) {
1102 for (uint32_t i
= 0; i
< pCreateInfo
->queueFamilyIndexCount
; ++i
)
1103 if (pCreateInfo
->pQueueFamilyIndices
[i
] == VK_QUEUE_FAMILY_EXTERNAL
)
1104 image
->queue_family_mask
|= (1u << RADV_MAX_QUEUE_FAMILIES
) - 1u;
1106 image
->queue_family_mask
|= 1u << pCreateInfo
->pQueueFamilyIndices
[i
];
1109 image
->shareable
= vk_find_struct_const(pCreateInfo
->pNext
,
1110 EXTERNAL_MEMORY_IMAGE_CREATE_INFO
) != NULL
;
1111 if (!vk_format_is_depth_or_stencil(pCreateInfo
->format
) &&
1112 !radv_surface_has_scanout(device
, create_info
) && !image
->shareable
) {
1113 image
->info
.surf_index
= &device
->image_mrt_offset_counter
;
1116 image
->plane_count
= plane_count
;
1118 image
->alignment
= 1;
1119 for (unsigned plane
= 0; plane
< plane_count
; ++plane
) {
1120 struct ac_surf_info info
= image
->info
;
1121 radv_init_surface(device
, image
, &image
->planes
[plane
].surface
, plane
, create_info
);
1124 const struct vk_format_description
*desc
= vk_format_description(pCreateInfo
->format
);
1125 assert(info
.width
% desc
->width_divisor
== 0);
1126 assert(info
.height
% desc
->height_divisor
== 0);
1128 info
.width
/= desc
->width_divisor
;
1129 info
.height
/= desc
->height_divisor
;
1132 device
->ws
->surface_init(device
->ws
, &info
, &image
->planes
[plane
].surface
);
1134 image
->planes
[plane
].offset
= align(image
->size
, image
->planes
[plane
].surface
.surf_alignment
);
1135 image
->size
= image
->planes
[plane
].offset
+ image
->planes
[plane
].surface
.surf_size
;
1136 image
->alignment
= image
->planes
[plane
].surface
.surf_alignment
;
1138 image
->planes
[plane
].format
= vk_format_get_plane_format(image
->vk_format
, plane
);
1141 if (!create_info
->no_metadata_planes
) {
1142 /* Try to enable DCC first. */
1143 if (radv_image_can_enable_dcc(image
)) {
1144 radv_image_alloc_dcc(image
);
1145 if (image
->info
.samples
> 1) {
1146 /* CMASK should be enabled because DCC fast
1147 * clear with MSAA needs it.
1149 assert(radv_image_can_enable_cmask(image
));
1150 radv_image_alloc_cmask(device
, image
);
1153 /* When DCC cannot be enabled, try CMASK. */
1154 radv_image_disable_dcc(image
);
1155 if (radv_image_can_enable_cmask(image
)) {
1156 radv_image_alloc_cmask(device
, image
);
1160 /* Try to enable FMASK for multisampled images. */
1161 if (radv_image_can_enable_fmask(image
)) {
1162 radv_image_alloc_fmask(device
, image
);
1164 if (radv_use_tc_compat_cmask_for_image(device
, image
))
1165 image
->tc_compatible_cmask
= true;
1167 /* Otherwise, try to enable HTILE for depth surfaces. */
1168 if (radv_image_can_enable_htile(image
) &&
1169 !(device
->instance
->debug_flags
& RADV_DEBUG_NO_HIZ
)) {
1170 image
->tc_compatible_htile
= image
->planes
[0].surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
;
1171 radv_image_alloc_htile(image
);
1173 radv_image_disable_htile(image
);
1177 radv_image_disable_dcc(image
);
1178 radv_image_disable_htile(image
);
1181 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
) {
1182 image
->alignment
= MAX2(image
->alignment
, 4096);
1183 image
->size
= align64(image
->size
, image
->alignment
);
1186 image
->bo
= device
->ws
->buffer_create(device
->ws
, image
->size
, image
->alignment
,
1187 0, RADEON_FLAG_VIRTUAL
, RADV_BO_PRIORITY_VIRTUAL
);
1189 vk_free2(&device
->alloc
, alloc
, image
);
1190 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1194 *pImage
= radv_image_to_handle(image
);
1200 radv_image_view_make_descriptor(struct radv_image_view
*iview
,
1201 struct radv_device
*device
,
1203 const VkComponentMapping
*components
,
1204 bool is_storage_image
, unsigned plane_id
,
1205 unsigned descriptor_plane_id
)
1207 struct radv_image
*image
= iview
->image
;
1208 struct radv_image_plane
*plane
= &image
->planes
[plane_id
];
1209 const struct vk_format_description
*format_desc
= vk_format_description(image
->vk_format
);
1210 bool is_stencil
= iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
;
1212 union radv_descriptor
*descriptor
;
1213 uint32_t hw_level
= 0;
1215 if (is_storage_image
) {
1216 descriptor
= &iview
->storage_descriptor
;
1218 descriptor
= &iview
->descriptor
;
1221 assert(vk_format_get_plane_count(vk_format
) == 1);
1222 assert(plane
->surface
.blk_w
% vk_format_get_blockwidth(plane
->format
) == 0);
1223 blk_w
= plane
->surface
.blk_w
/ vk_format_get_blockwidth(plane
->format
) * vk_format_get_blockwidth(vk_format
);
1225 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
1226 hw_level
= iview
->base_mip
;
1227 si_make_texture_descriptor(device
, image
, is_storage_image
,
1231 hw_level
, hw_level
+ iview
->level_count
- 1,
1233 iview
->base_layer
+ iview
->layer_count
- 1,
1234 iview
->extent
.width
/ (plane_id
? format_desc
->width_divisor
: 1),
1235 iview
->extent
.height
/ (plane_id
? format_desc
->height_divisor
: 1),
1236 iview
->extent
.depth
,
1237 descriptor
->plane_descriptors
[descriptor_plane_id
],
1238 descriptor_plane_id
? NULL
: descriptor
->fmask_descriptor
);
1240 const struct legacy_surf_level
*base_level_info
= NULL
;
1241 if (device
->physical_device
->rad_info
.chip_class
<= GFX9
) {
1243 base_level_info
= &plane
->surface
.u
.legacy
.stencil_level
[iview
->base_mip
];
1245 base_level_info
= &plane
->surface
.u
.legacy
.level
[iview
->base_mip
];
1247 si_set_mutable_tex_desc_fields(device
, image
,
1252 blk_w
, is_stencil
, is_storage_image
, descriptor
->plane_descriptors
[descriptor_plane_id
]);
1256 radv_plane_from_aspect(VkImageAspectFlags mask
)
1259 case VK_IMAGE_ASPECT_PLANE_1_BIT
:
1261 case VK_IMAGE_ASPECT_PLANE_2_BIT
:
1269 radv_get_aspect_format(struct radv_image
*image
, VkImageAspectFlags mask
)
1272 case VK_IMAGE_ASPECT_PLANE_0_BIT
:
1273 return image
->planes
[0].format
;
1274 case VK_IMAGE_ASPECT_PLANE_1_BIT
:
1275 return image
->planes
[1].format
;
1276 case VK_IMAGE_ASPECT_PLANE_2_BIT
:
1277 return image
->planes
[2].format
;
1278 case VK_IMAGE_ASPECT_STENCIL_BIT
:
1279 return vk_format_stencil_only(image
->vk_format
);
1280 case VK_IMAGE_ASPECT_DEPTH_BIT
:
1281 return vk_format_depth_only(image
->vk_format
);
1282 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
1283 return vk_format_depth_only(image
->vk_format
);
1285 return image
->vk_format
;
1290 radv_image_view_init(struct radv_image_view
*iview
,
1291 struct radv_device
*device
,
1292 const VkImageViewCreateInfo
* pCreateInfo
)
1294 RADV_FROM_HANDLE(radv_image
, image
, pCreateInfo
->image
);
1295 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
1297 switch (image
->type
) {
1298 case VK_IMAGE_TYPE_1D
:
1299 case VK_IMAGE_TYPE_2D
:
1300 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1 <= image
->info
.array_size
);
1302 case VK_IMAGE_TYPE_3D
:
1303 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1
1304 <= radv_minify(image
->info
.depth
, range
->baseMipLevel
));
1307 unreachable("bad VkImageType");
1309 iview
->image
= image
;
1310 iview
->bo
= image
->bo
;
1311 iview
->type
= pCreateInfo
->viewType
;
1312 iview
->plane_id
= radv_plane_from_aspect(pCreateInfo
->subresourceRange
.aspectMask
);
1313 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
1314 iview
->multiple_planes
= vk_format_get_plane_count(image
->vk_format
) > 1 && iview
->aspect_mask
== VK_IMAGE_ASPECT_COLOR_BIT
;
1315 iview
->vk_format
= pCreateInfo
->format
;
1317 if (iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
) {
1318 iview
->vk_format
= vk_format_stencil_only(iview
->vk_format
);
1319 } else if (iview
->aspect_mask
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
1320 iview
->vk_format
= vk_format_depth_only(iview
->vk_format
);
1323 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
1324 iview
->extent
= (VkExtent3D
) {
1325 .width
= image
->info
.width
,
1326 .height
= image
->info
.height
,
1327 .depth
= image
->info
.depth
,
1330 iview
->extent
= (VkExtent3D
) {
1331 .width
= radv_minify(image
->info
.width
, range
->baseMipLevel
),
1332 .height
= radv_minify(image
->info
.height
, range
->baseMipLevel
),
1333 .depth
= radv_minify(image
->info
.depth
, range
->baseMipLevel
),
1337 if (iview
->vk_format
!= image
->planes
[iview
->plane_id
].format
) {
1338 unsigned view_bw
= vk_format_get_blockwidth(iview
->vk_format
);
1339 unsigned view_bh
= vk_format_get_blockheight(iview
->vk_format
);
1340 unsigned img_bw
= vk_format_get_blockwidth(image
->vk_format
);
1341 unsigned img_bh
= vk_format_get_blockheight(image
->vk_format
);
1343 iview
->extent
.width
= round_up_u32(iview
->extent
.width
* view_bw
, img_bw
);
1344 iview
->extent
.height
= round_up_u32(iview
->extent
.height
* view_bh
, img_bh
);
1346 /* Comment ported from amdvlk -
1347 * If we have the following image:
1348 * Uncompressed pixels Compressed block sizes (4x4)
1349 * mip0: 22 x 22 6 x 6
1350 * mip1: 11 x 11 3 x 3
1355 * On GFX9 the descriptor is always programmed with the WIDTH and HEIGHT of the base level and the HW is
1356 * calculating the degradation of the block sizes down the mip-chain as follows (straight-up
1357 * divide-by-two integer math):
1363 * This means that mip2 will be missing texels.
1365 * Fix this by calculating the base mip's width and height, then convert that, and round it
1366 * back up to get the level 0 size.
1367 * Clamp the converted size between the original values, and next power of two, which
1368 * means we don't oversize the image.
1370 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
1371 vk_format_is_compressed(image
->vk_format
) &&
1372 !vk_format_is_compressed(iview
->vk_format
)) {
1373 unsigned lvl_width
= radv_minify(image
->info
.width
, range
->baseMipLevel
);
1374 unsigned lvl_height
= radv_minify(image
->info
.height
, range
->baseMipLevel
);
1376 lvl_width
= round_up_u32(lvl_width
* view_bw
, img_bw
);
1377 lvl_height
= round_up_u32(lvl_height
* view_bh
, img_bh
);
1379 lvl_width
<<= range
->baseMipLevel
;
1380 lvl_height
<<= range
->baseMipLevel
;
1382 iview
->extent
.width
= CLAMP(lvl_width
, iview
->extent
.width
, iview
->image
->planes
[0].surface
.u
.gfx9
.surf_pitch
);
1383 iview
->extent
.height
= CLAMP(lvl_height
, iview
->extent
.height
, iview
->image
->planes
[0].surface
.u
.gfx9
.surf_height
);
1387 iview
->base_layer
= range
->baseArrayLayer
;
1388 iview
->layer_count
= radv_get_layerCount(image
, range
);
1389 iview
->base_mip
= range
->baseMipLevel
;
1390 iview
->level_count
= radv_get_levelCount(image
, range
);
1392 for (unsigned i
= 0; i
< (iview
->multiple_planes
? vk_format_get_plane_count(image
->vk_format
) : 1); ++i
) {
1393 VkFormat format
= vk_format_get_plane_format(iview
->vk_format
, i
);
1394 radv_image_view_make_descriptor(iview
, device
, format
, &pCreateInfo
->components
, false, iview
->plane_id
+ i
, i
);
1395 radv_image_view_make_descriptor(iview
, device
, format
, &pCreateInfo
->components
, true, iview
->plane_id
+ i
, i
);
1399 bool radv_layout_has_htile(const struct radv_image
*image
,
1400 VkImageLayout layout
,
1401 unsigned queue_mask
)
1403 if (radv_image_is_tc_compat_htile(image
))
1404 return layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1406 return radv_image_has_htile(image
) &&
1407 (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
1408 (layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
&&
1409 queue_mask
== (1u << RADV_QUEUE_GENERAL
)));
1412 bool radv_layout_is_htile_compressed(const struct radv_image
*image
,
1413 VkImageLayout layout
,
1414 unsigned queue_mask
)
1416 if (radv_image_is_tc_compat_htile(image
))
1417 return layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1419 return radv_image_has_htile(image
) &&
1420 (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
1421 (layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
&&
1422 queue_mask
== (1u << RADV_QUEUE_GENERAL
)));
1425 bool radv_layout_can_fast_clear(const struct radv_image
*image
,
1426 VkImageLayout layout
,
1427 unsigned queue_mask
)
1429 return layout
== VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
;
1432 bool radv_layout_dcc_compressed(const struct radv_image
*image
,
1433 VkImageLayout layout
,
1434 unsigned queue_mask
)
1436 /* Don't compress compute transfer dst, as image stores are not supported. */
1437 if (layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
&&
1438 (queue_mask
& (1u << RADV_QUEUE_COMPUTE
)))
1441 return radv_image_has_dcc(image
) && layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1445 unsigned radv_image_queue_family_mask(const struct radv_image
*image
, uint32_t family
, uint32_t queue_family
)
1447 if (!image
->exclusive
)
1448 return image
->queue_family_mask
;
1449 if (family
== VK_QUEUE_FAMILY_EXTERNAL
)
1450 return (1u << RADV_MAX_QUEUE_FAMILIES
) - 1u;
1451 if (family
== VK_QUEUE_FAMILY_IGNORED
)
1452 return 1u << queue_family
;
1453 return 1u << family
;
1457 radv_CreateImage(VkDevice device
,
1458 const VkImageCreateInfo
*pCreateInfo
,
1459 const VkAllocationCallbacks
*pAllocator
,
1463 const VkNativeBufferANDROID
*gralloc_info
=
1464 vk_find_struct_const(pCreateInfo
->pNext
, NATIVE_BUFFER_ANDROID
);
1467 return radv_image_from_gralloc(device
, pCreateInfo
, gralloc_info
,
1468 pAllocator
, pImage
);
1471 const struct wsi_image_create_info
*wsi_info
=
1472 vk_find_struct_const(pCreateInfo
->pNext
, WSI_IMAGE_CREATE_INFO_MESA
);
1473 bool scanout
= wsi_info
&& wsi_info
->scanout
;
1475 return radv_image_create(device
,
1476 &(struct radv_image_create_info
) {
1477 .vk_info
= pCreateInfo
,
1485 radv_DestroyImage(VkDevice _device
, VkImage _image
,
1486 const VkAllocationCallbacks
*pAllocator
)
1488 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1489 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1494 if (image
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
)
1495 device
->ws
->buffer_destroy(image
->bo
);
1497 if (image
->owned_memory
!= VK_NULL_HANDLE
)
1498 radv_FreeMemory(_device
, image
->owned_memory
, pAllocator
);
1500 vk_free2(&device
->alloc
, pAllocator
, image
);
1503 void radv_GetImageSubresourceLayout(
1506 const VkImageSubresource
* pSubresource
,
1507 VkSubresourceLayout
* pLayout
)
1509 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1510 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1511 int level
= pSubresource
->mipLevel
;
1512 int layer
= pSubresource
->arrayLayer
;
1514 unsigned plane_id
= radv_plane_from_aspect(pSubresource
->aspectMask
);
1516 struct radv_image_plane
*plane
= &image
->planes
[plane_id
];
1517 struct radeon_surf
*surface
= &plane
->surface
;
1519 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
1520 pLayout
->offset
= plane
->offset
+ surface
->u
.gfx9
.offset
[level
] + surface
->u
.gfx9
.surf_slice_size
* layer
;
1521 if (image
->vk_format
== VK_FORMAT_R32G32B32_UINT
||
1522 image
->vk_format
== VK_FORMAT_R32G32B32_SINT
||
1523 image
->vk_format
== VK_FORMAT_R32G32B32_SFLOAT
) {
1524 /* Adjust the number of bytes between each row because
1525 * the pitch is actually the number of components per
1528 pLayout
->rowPitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
/ 3;
1530 assert(util_is_power_of_two_nonzero(surface
->bpe
));
1531 pLayout
->rowPitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
1534 pLayout
->arrayPitch
= surface
->u
.gfx9
.surf_slice_size
;
1535 pLayout
->depthPitch
= surface
->u
.gfx9
.surf_slice_size
;
1536 pLayout
->size
= surface
->u
.gfx9
.surf_slice_size
;
1537 if (image
->type
== VK_IMAGE_TYPE_3D
)
1538 pLayout
->size
*= u_minify(image
->info
.depth
, level
);
1540 pLayout
->offset
= plane
->offset
+ surface
->u
.legacy
.level
[level
].offset
+ (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4 * layer
;
1541 pLayout
->rowPitch
= surface
->u
.legacy
.level
[level
].nblk_x
* surface
->bpe
;
1542 pLayout
->arrayPitch
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1543 pLayout
->depthPitch
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1544 pLayout
->size
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1545 if (image
->type
== VK_IMAGE_TYPE_3D
)
1546 pLayout
->size
*= u_minify(image
->info
.depth
, level
);
1552 radv_CreateImageView(VkDevice _device
,
1553 const VkImageViewCreateInfo
*pCreateInfo
,
1554 const VkAllocationCallbacks
*pAllocator
,
1557 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1558 struct radv_image_view
*view
;
1560 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1561 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1563 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1565 radv_image_view_init(view
, device
, pCreateInfo
);
1567 *pView
= radv_image_view_to_handle(view
);
1573 radv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
1574 const VkAllocationCallbacks
*pAllocator
)
1576 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1577 RADV_FROM_HANDLE(radv_image_view
, iview
, _iview
);
1581 vk_free2(&device
->alloc
, pAllocator
, iview
);
1584 void radv_buffer_view_init(struct radv_buffer_view
*view
,
1585 struct radv_device
*device
,
1586 const VkBufferViewCreateInfo
* pCreateInfo
)
1588 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCreateInfo
->buffer
);
1590 view
->bo
= buffer
->bo
;
1591 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
1592 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
1593 view
->vk_format
= pCreateInfo
->format
;
1595 radv_make_buffer_descriptor(device
, buffer
, view
->vk_format
,
1596 pCreateInfo
->offset
, view
->range
, view
->state
);
1600 radv_CreateBufferView(VkDevice _device
,
1601 const VkBufferViewCreateInfo
*pCreateInfo
,
1602 const VkAllocationCallbacks
*pAllocator
,
1603 VkBufferView
*pView
)
1605 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1606 struct radv_buffer_view
*view
;
1608 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1609 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1611 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1613 radv_buffer_view_init(view
, device
, pCreateInfo
);
1615 *pView
= radv_buffer_view_to_handle(view
);
1621 radv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
1622 const VkAllocationCallbacks
*pAllocator
)
1624 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1625 RADV_FROM_HANDLE(radv_buffer_view
, view
, bufferView
);
1630 vk_free2(&device
->alloc
, pAllocator
, view
);