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
)
78 if (pCreateInfo
->flags
& (VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
|
79 VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR
))
82 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
)
85 if (pCreateInfo
->mipLevels
> 1)
88 /* FIXME: for some reason TC compat with 2/4/8 samples breaks some cts
89 * tests - disable for now */
90 if (pCreateInfo
->samples
>= 2 &&
91 pCreateInfo
->format
== VK_FORMAT_D32_SFLOAT_S8_UINT
)
94 /* GFX9 supports both 32-bit and 16-bit depth surfaces, while GFX8 only
95 * supports 32-bit. Though, it's possible to enable TC-compat for
96 * 16-bit depth surfaces if no Z planes are compressed.
98 if (pCreateInfo
->format
!= VK_FORMAT_D32_SFLOAT_S8_UINT
&&
99 pCreateInfo
->format
!= VK_FORMAT_D32_SFLOAT
&&
100 pCreateInfo
->format
!= VK_FORMAT_D16_UNORM
)
107 radv_use_dcc_for_image(struct radv_device
*device
,
108 const struct radv_image_create_info
*create_info
,
109 const VkImageCreateInfo
*pCreateInfo
)
111 bool dcc_compatible_formats
;
113 bool shareable
= vk_find_struct_const(pCreateInfo
->pNext
,
114 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR
) != NULL
;
116 /* DCC (Delta Color Compression) is only available for GFX8+. */
117 if (device
->physical_device
->rad_info
.chip_class
< VI
)
120 if (device
->instance
->debug_flags
& RADV_DEBUG_NO_DCC
)
123 /* FIXME: DCC is broken for shareable images starting with GFX9 */
124 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
128 /* TODO: Enable DCC for storage images. */
129 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) ||
130 (pCreateInfo
->flags
& VK_IMAGE_CREATE_EXTENDED_USAGE_BIT_KHR
))
133 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
)
136 /* TODO: Enable DCC for mipmaps and array layers. */
137 if (pCreateInfo
->mipLevels
> 1 || pCreateInfo
->arrayLayers
> 1)
140 if (create_info
->scanout
)
143 /* FIXME: DCC for MSAA with 4x and 8x samples doesn't work yet, while
144 * 2x can be enabled with an option.
146 if (pCreateInfo
->samples
> 2 ||
147 (pCreateInfo
->samples
== 2 &&
148 !device
->physical_device
->dcc_msaa_allowed
))
151 /* Determine if the formats are DCC compatible. */
152 dcc_compatible_formats
=
153 radv_is_colorbuffer_format_supported(pCreateInfo
->format
,
156 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) {
157 const struct VkImageFormatListCreateInfoKHR
*format_list
=
158 (const struct VkImageFormatListCreateInfoKHR
*)
159 vk_find_struct_const(pCreateInfo
->pNext
,
160 IMAGE_FORMAT_LIST_CREATE_INFO_KHR
);
162 /* We have to ignore the existence of the list if viewFormatCount = 0 */
163 if (format_list
&& format_list
->viewFormatCount
) {
164 /* compatibility is transitive, so we only need to check
165 * one format with everything else. */
166 for (unsigned i
= 0; i
< format_list
->viewFormatCount
; ++i
) {
167 if (!radv_dcc_formats_compatible(pCreateInfo
->format
,
168 format_list
->pViewFormats
[i
]))
169 dcc_compatible_formats
= false;
172 dcc_compatible_formats
= false;
176 if (!dcc_compatible_formats
)
183 radv_init_surface(struct radv_device
*device
,
184 struct radeon_surf
*surface
,
185 const struct radv_image_create_info
*create_info
)
187 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
188 unsigned array_mode
= radv_choose_tiling(device
, create_info
);
189 const struct vk_format_description
*desc
=
190 vk_format_description(pCreateInfo
->format
);
191 bool is_depth
, is_stencil
;
193 is_depth
= vk_format_has_depth(desc
);
194 is_stencil
= vk_format_has_stencil(desc
);
196 surface
->blk_w
= vk_format_get_blockwidth(pCreateInfo
->format
);
197 surface
->blk_h
= vk_format_get_blockheight(pCreateInfo
->format
);
199 surface
->bpe
= vk_format_get_blocksize(vk_format_depth_only(pCreateInfo
->format
));
200 /* align byte per element on dword */
201 if (surface
->bpe
== 3) {
204 surface
->flags
= RADEON_SURF_SET(array_mode
, MODE
);
206 switch (pCreateInfo
->imageType
){
207 case VK_IMAGE_TYPE_1D
:
208 if (pCreateInfo
->arrayLayers
> 1)
209 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
211 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
213 case VK_IMAGE_TYPE_2D
:
214 if (pCreateInfo
->arrayLayers
> 1)
215 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
217 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
219 case VK_IMAGE_TYPE_3D
:
220 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
223 unreachable("unhandled image type");
227 surface
->flags
|= RADEON_SURF_ZBUFFER
;
228 if (radv_use_tc_compat_htile_for_image(device
, pCreateInfo
))
229 surface
->flags
|= RADEON_SURF_TC_COMPATIBLE_HTILE
;
233 surface
->flags
|= RADEON_SURF_SBUFFER
;
235 surface
->flags
|= RADEON_SURF_OPTIMIZE_FOR_SPACE
;
237 if (!radv_use_dcc_for_image(device
, create_info
, pCreateInfo
))
238 surface
->flags
|= RADEON_SURF_DISABLE_DCC
;
240 if (create_info
->scanout
)
241 surface
->flags
|= RADEON_SURF_SCANOUT
;
245 static uint32_t si_get_bo_metadata_word1(struct radv_device
*device
)
247 return (ATI_VENDOR_ID
<< 16) | device
->physical_device
->rad_info
.pci_id
;
250 static inline unsigned
251 si_tile_mode_index(const struct radv_image
*image
, unsigned level
, bool stencil
)
254 return image
->surface
.u
.legacy
.stencil_tiling_index
[level
];
256 return image
->surface
.u
.legacy
.tiling_index
[level
];
259 static unsigned radv_map_swizzle(unsigned swizzle
)
263 return V_008F0C_SQ_SEL_Y
;
265 return V_008F0C_SQ_SEL_Z
;
267 return V_008F0C_SQ_SEL_W
;
269 return V_008F0C_SQ_SEL_0
;
271 return V_008F0C_SQ_SEL_1
;
272 default: /* VK_SWIZZLE_X */
273 return V_008F0C_SQ_SEL_X
;
278 radv_make_buffer_descriptor(struct radv_device
*device
,
279 struct radv_buffer
*buffer
,
285 const struct vk_format_description
*desc
;
287 uint64_t gpu_address
= radv_buffer_get_va(buffer
->bo
);
288 uint64_t va
= gpu_address
+ buffer
->offset
;
289 unsigned num_format
, data_format
;
291 desc
= vk_format_description(vk_format
);
292 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
293 stride
= desc
->block
.bits
/ 8;
295 num_format
= radv_translate_buffer_numformat(desc
, first_non_void
);
296 data_format
= radv_translate_buffer_dataformat(desc
, first_non_void
);
300 state
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
301 S_008F04_STRIDE(stride
);
303 if (device
->physical_device
->rad_info
.chip_class
!= VI
&& stride
) {
308 state
[3] = S_008F0C_DST_SEL_X(radv_map_swizzle(desc
->swizzle
[0])) |
309 S_008F0C_DST_SEL_Y(radv_map_swizzle(desc
->swizzle
[1])) |
310 S_008F0C_DST_SEL_Z(radv_map_swizzle(desc
->swizzle
[2])) |
311 S_008F0C_DST_SEL_W(radv_map_swizzle(desc
->swizzle
[3])) |
312 S_008F0C_NUM_FORMAT(num_format
) |
313 S_008F0C_DATA_FORMAT(data_format
);
317 si_set_mutable_tex_desc_fields(struct radv_device
*device
,
318 struct radv_image
*image
,
319 const struct legacy_surf_level
*base_level_info
,
320 unsigned base_level
, unsigned first_level
,
321 unsigned block_width
, bool is_stencil
,
322 bool is_storage_image
, uint32_t *state
)
324 uint64_t gpu_address
= image
->bo
? radv_buffer_get_va(image
->bo
) + image
->offset
: 0;
325 uint64_t va
= gpu_address
;
326 enum chip_class chip_class
= device
->physical_device
->rad_info
.chip_class
;
327 uint64_t meta_va
= 0;
328 if (chip_class
>= GFX9
) {
330 va
+= image
->surface
.u
.gfx9
.stencil_offset
;
332 va
+= image
->surface
.u
.gfx9
.surf_offset
;
334 va
+= base_level_info
->offset
;
337 if (chip_class
>= GFX9
||
338 base_level_info
->mode
== RADEON_SURF_MODE_2D
)
339 state
[0] |= image
->surface
.tile_swizzle
;
340 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
341 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
343 if (chip_class
>= VI
) {
344 state
[6] &= C_008F28_COMPRESSION_EN
;
346 if (!is_storage_image
&& radv_dcc_enabled(image
, first_level
)) {
347 meta_va
= gpu_address
+ image
->dcc_offset
;
348 if (chip_class
<= VI
)
349 meta_va
+= base_level_info
->dcc_offset
;
350 } else if (!is_storage_image
&&
351 radv_image_is_tc_compat_htile(image
)) {
352 meta_va
= gpu_address
+ image
->htile_offset
;
356 state
[6] |= S_008F28_COMPRESSION_EN(1);
357 state
[7] = meta_va
>> 8;
358 state
[7] |= image
->surface
.tile_swizzle
;
362 if (chip_class
>= GFX9
) {
363 state
[3] &= C_008F1C_SW_MODE
;
364 state
[4] &= C_008F20_PITCH_GFX9
;
367 state
[3] |= S_008F1C_SW_MODE(image
->surface
.u
.gfx9
.stencil
.swizzle_mode
);
368 state
[4] |= S_008F20_PITCH_GFX9(image
->surface
.u
.gfx9
.stencil
.epitch
);
370 state
[3] |= S_008F1C_SW_MODE(image
->surface
.u
.gfx9
.surf
.swizzle_mode
);
371 state
[4] |= S_008F20_PITCH_GFX9(image
->surface
.u
.gfx9
.surf
.epitch
);
374 state
[5] &= C_008F24_META_DATA_ADDRESS
&
375 C_008F24_META_PIPE_ALIGNED
&
376 C_008F24_META_RB_ALIGNED
;
378 struct gfx9_surf_meta_flags meta
;
380 if (image
->dcc_offset
)
381 meta
= image
->surface
.u
.gfx9
.dcc
;
383 meta
= image
->surface
.u
.gfx9
.htile
;
385 state
[5] |= S_008F24_META_DATA_ADDRESS(meta_va
>> 40) |
386 S_008F24_META_PIPE_ALIGNED(meta
.pipe_aligned
) |
387 S_008F24_META_RB_ALIGNED(meta
.rb_aligned
);
391 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
392 unsigned index
= si_tile_mode_index(image
, base_level
, is_stencil
);
394 state
[3] &= C_008F1C_TILING_INDEX
;
395 state
[3] |= S_008F1C_TILING_INDEX(index
);
396 state
[4] &= C_008F20_PITCH_GFX6
;
397 state
[4] |= S_008F20_PITCH_GFX6(pitch
- 1);
401 static unsigned radv_tex_dim(VkImageType image_type
, VkImageViewType view_type
,
402 unsigned nr_layers
, unsigned nr_samples
, bool is_storage_image
, bool gfx9
)
404 if (view_type
== VK_IMAGE_VIEW_TYPE_CUBE
|| view_type
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
)
405 return is_storage_image
? V_008F1C_SQ_RSRC_IMG_2D_ARRAY
: V_008F1C_SQ_RSRC_IMG_CUBE
;
407 /* GFX9 allocates 1D textures as 2D. */
408 if (gfx9
&& image_type
== VK_IMAGE_TYPE_1D
)
409 image_type
= VK_IMAGE_TYPE_2D
;
410 switch (image_type
) {
411 case VK_IMAGE_TYPE_1D
:
412 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_1D_ARRAY
: V_008F1C_SQ_RSRC_IMG_1D
;
413 case VK_IMAGE_TYPE_2D
:
415 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D_MSAA
;
417 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D
;
418 case VK_IMAGE_TYPE_3D
:
419 if (view_type
== VK_IMAGE_VIEW_TYPE_3D
)
420 return V_008F1C_SQ_RSRC_IMG_3D
;
422 return V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
424 unreachable("illegal image type");
428 static unsigned gfx9_border_color_swizzle(const enum vk_swizzle swizzle
[4])
430 unsigned bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
432 if (swizzle
[3] == VK_SWIZZLE_X
) {
433 /* For the pre-defined border color values (white, opaque
434 * black, transparent black), the only thing that matters is
435 * that the alpha channel winds up in the correct place
436 * (because the RGB channels are all the same) so either of
437 * these enumerations will work.
439 if (swizzle
[2] == VK_SWIZZLE_Y
)
440 bc_swizzle
= V_008F20_BC_SWIZZLE_WZYX
;
442 bc_swizzle
= V_008F20_BC_SWIZZLE_WXYZ
;
443 } else if (swizzle
[0] == VK_SWIZZLE_X
) {
444 if (swizzle
[1] == VK_SWIZZLE_Y
)
445 bc_swizzle
= V_008F20_BC_SWIZZLE_XYZW
;
447 bc_swizzle
= V_008F20_BC_SWIZZLE_XWYZ
;
448 } else if (swizzle
[1] == VK_SWIZZLE_X
) {
449 bc_swizzle
= V_008F20_BC_SWIZZLE_YXWZ
;
450 } else if (swizzle
[2] == VK_SWIZZLE_X
) {
451 bc_swizzle
= V_008F20_BC_SWIZZLE_ZYXW
;
458 * Build the sampler view descriptor for a texture.
461 si_make_texture_descriptor(struct radv_device
*device
,
462 struct radv_image
*image
,
463 bool is_storage_image
,
464 VkImageViewType view_type
,
466 const VkComponentMapping
*mapping
,
467 unsigned first_level
, unsigned last_level
,
468 unsigned first_layer
, unsigned last_layer
,
469 unsigned width
, unsigned height
, unsigned depth
,
471 uint32_t *fmask_state
)
473 const struct vk_format_description
*desc
;
474 enum vk_swizzle swizzle
[4];
476 unsigned num_format
, data_format
, type
;
478 desc
= vk_format_description(vk_format
);
480 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
) {
481 const unsigned char swizzle_xxxx
[4] = {0, 0, 0, 0};
482 vk_format_compose_swizzles(mapping
, swizzle_xxxx
, swizzle
);
484 vk_format_compose_swizzles(mapping
, desc
->swizzle
, swizzle
);
487 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
489 num_format
= radv_translate_tex_numformat(vk_format
, desc
, first_non_void
);
490 if (num_format
== ~0) {
494 data_format
= radv_translate_tex_dataformat(vk_format
, desc
, first_non_void
);
495 if (data_format
== ~0) {
499 /* S8 with either Z16 or Z32 HTILE need a special format. */
500 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
501 vk_format
== VK_FORMAT_S8_UINT
&&
502 radv_image_is_tc_compat_htile(image
)) {
503 if (image
->vk_format
== VK_FORMAT_D32_SFLOAT_S8_UINT
)
504 data_format
= V_008F14_IMG_DATA_FORMAT_S8_32
;
505 else if (image
->vk_format
== VK_FORMAT_D16_UNORM_S8_UINT
)
506 data_format
= V_008F14_IMG_DATA_FORMAT_S8_16
;
508 type
= radv_tex_dim(image
->type
, view_type
, image
->info
.array_size
, image
->info
.samples
,
509 is_storage_image
, device
->physical_device
->rad_info
.chip_class
>= GFX9
);
510 if (type
== V_008F1C_SQ_RSRC_IMG_1D_ARRAY
) {
512 depth
= image
->info
.array_size
;
513 } else if (type
== V_008F1C_SQ_RSRC_IMG_2D_ARRAY
||
514 type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
515 if (view_type
!= VK_IMAGE_VIEW_TYPE_3D
)
516 depth
= image
->info
.array_size
;
517 } else if (type
== V_008F1C_SQ_RSRC_IMG_CUBE
)
518 depth
= image
->info
.array_size
/ 6;
521 state
[1] = (S_008F14_DATA_FORMAT_GFX6(data_format
) |
522 S_008F14_NUM_FORMAT_GFX6(num_format
));
523 state
[2] = (S_008F18_WIDTH(width
- 1) |
524 S_008F18_HEIGHT(height
- 1) |
525 S_008F18_PERF_MOD(4));
526 state
[3] = (S_008F1C_DST_SEL_X(radv_map_swizzle(swizzle
[0])) |
527 S_008F1C_DST_SEL_Y(radv_map_swizzle(swizzle
[1])) |
528 S_008F1C_DST_SEL_Z(radv_map_swizzle(swizzle
[2])) |
529 S_008F1C_DST_SEL_W(radv_map_swizzle(swizzle
[3])) |
530 S_008F1C_BASE_LEVEL(image
->info
.samples
> 1 ?
532 S_008F1C_LAST_LEVEL(image
->info
.samples
> 1 ?
533 util_logbase2(image
->info
.samples
) :
535 S_008F1C_TYPE(type
));
537 state
[5] = S_008F24_BASE_ARRAY(first_layer
);
541 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
542 unsigned bc_swizzle
= gfx9_border_color_swizzle(swizzle
);
544 /* Depth is the last accessible layer on Gfx9.
545 * The hw doesn't need to know the total number of layers.
547 if (type
== V_008F1C_SQ_RSRC_IMG_3D
)
548 state
[4] |= S_008F20_DEPTH(depth
- 1);
550 state
[4] |= S_008F20_DEPTH(last_layer
);
552 state
[4] |= S_008F20_BC_SWIZZLE(bc_swizzle
);
553 state
[5] |= S_008F24_MAX_MIP(image
->info
.samples
> 1 ?
554 util_logbase2(image
->info
.samples
) :
555 image
->info
.levels
- 1);
557 state
[3] |= S_008F1C_POW2_PAD(image
->info
.levels
> 1);
558 state
[4] |= S_008F20_DEPTH(depth
- 1);
559 state
[5] |= S_008F24_LAST_ARRAY(last_layer
);
561 if (image
->dcc_offset
) {
562 unsigned swap
= radv_translate_colorswap(vk_format
, FALSE
);
564 state
[6] = S_008F28_ALPHA_IS_ON_MSB(swap
<= 1);
566 /* The last dword is unused by hw. The shader uses it to clear
567 * bits in the first dword of sampler state.
569 if (device
->physical_device
->rad_info
.chip_class
<= CIK
&& image
->info
.samples
<= 1) {
570 if (first_level
== last_level
)
571 state
[7] = C_008F30_MAX_ANISO_RATIO
;
573 state
[7] = 0xffffffff;
577 /* Initialize the sampler view for FMASK. */
578 if (radv_image_has_fmask(image
)) {
579 uint32_t fmask_format
, num_format
;
580 uint64_t gpu_address
= radv_buffer_get_va(image
->bo
);
583 va
= gpu_address
+ image
->offset
+ image
->fmask
.offset
;
585 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
586 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK
;
587 switch (image
->info
.samples
) {
589 num_format
= V_008F14_IMG_FMASK_8_2_2
;
592 num_format
= V_008F14_IMG_FMASK_8_4_4
;
595 num_format
= V_008F14_IMG_FMASK_32_8_8
;
598 unreachable("invalid nr_samples");
601 switch (image
->info
.samples
) {
603 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2
;
606 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4
;
609 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8
;
613 fmask_format
= V_008F14_IMG_DATA_FORMAT_INVALID
;
615 num_format
= V_008F14_IMG_NUM_FORMAT_UINT
;
618 fmask_state
[0] = va
>> 8;
619 fmask_state
[0] |= image
->fmask
.tile_swizzle
;
620 fmask_state
[1] = S_008F14_BASE_ADDRESS_HI(va
>> 40) |
621 S_008F14_DATA_FORMAT_GFX6(fmask_format
) |
622 S_008F14_NUM_FORMAT_GFX6(num_format
);
623 fmask_state
[2] = S_008F18_WIDTH(width
- 1) |
624 S_008F18_HEIGHT(height
- 1);
625 fmask_state
[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X
) |
626 S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X
) |
627 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X
) |
628 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X
) |
629 S_008F1C_TYPE(radv_tex_dim(image
->type
, view_type
, image
->info
.array_size
, 0, false, false));
631 fmask_state
[5] = S_008F24_BASE_ARRAY(first_layer
);
635 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
636 fmask_state
[3] |= S_008F1C_SW_MODE(image
->surface
.u
.gfx9
.fmask
.swizzle_mode
);
637 fmask_state
[4] |= S_008F20_DEPTH(last_layer
) |
638 S_008F20_PITCH_GFX9(image
->surface
.u
.gfx9
.fmask
.epitch
);
639 fmask_state
[5] |= S_008F24_META_PIPE_ALIGNED(image
->surface
.u
.gfx9
.cmask
.pipe_aligned
) |
640 S_008F24_META_RB_ALIGNED(image
->surface
.u
.gfx9
.cmask
.rb_aligned
);
642 fmask_state
[3] |= S_008F1C_TILING_INDEX(image
->fmask
.tile_mode_index
);
643 fmask_state
[4] |= S_008F20_DEPTH(depth
- 1) |
644 S_008F20_PITCH_GFX6(image
->fmask
.pitch_in_pixels
- 1);
645 fmask_state
[5] |= S_008F24_LAST_ARRAY(last_layer
);
647 } else if (fmask_state
)
648 memset(fmask_state
, 0, 8 * 4);
652 radv_query_opaque_metadata(struct radv_device
*device
,
653 struct radv_image
*image
,
654 struct radeon_bo_metadata
*md
)
656 static const VkComponentMapping fixedmapping
;
659 /* Metadata image format format version 1:
660 * [0] = 1 (metadata format identifier)
661 * [1] = (VENDOR_ID << 16) | PCI_ID
662 * [2:9] = image descriptor for the whole resource
663 * [2] is always 0, because the base address is cleared
664 * [9] is the DCC offset bits [39:8] from the beginning of
666 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
668 md
->metadata
[0] = 1; /* metadata image format version 1 */
670 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
671 md
->metadata
[1] = si_get_bo_metadata_word1(device
);
674 si_make_texture_descriptor(device
, image
, false,
675 (VkImageViewType
)image
->type
, image
->vk_format
,
676 &fixedmapping
, 0, image
->info
.levels
- 1, 0,
677 image
->info
.array_size
,
678 image
->info
.width
, image
->info
.height
,
682 si_set_mutable_tex_desc_fields(device
, image
, &image
->surface
.u
.legacy
.level
[0], 0, 0,
683 image
->surface
.blk_w
, false, false, desc
);
685 /* Clear the base address and set the relative DCC offset. */
687 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
688 desc
[7] = image
->dcc_offset
>> 8;
690 /* Dwords [2:9] contain the image descriptor. */
691 memcpy(&md
->metadata
[2], desc
, sizeof(desc
));
693 /* Dwords [10:..] contain the mipmap level offsets. */
694 if (device
->physical_device
->rad_info
.chip_class
<= VI
) {
695 for (i
= 0; i
<= image
->info
.levels
- 1; i
++)
696 md
->metadata
[10+i
] = image
->surface
.u
.legacy
.level
[i
].offset
>> 8;
697 md
->size_metadata
= (11 + image
->info
.levels
- 1) * 4;
702 radv_init_metadata(struct radv_device
*device
,
703 struct radv_image
*image
,
704 struct radeon_bo_metadata
*metadata
)
706 struct radeon_surf
*surface
= &image
->surface
;
708 memset(metadata
, 0, sizeof(*metadata
));
710 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
711 metadata
->u
.gfx9
.swizzle_mode
= surface
->u
.gfx9
.surf
.swizzle_mode
;
713 metadata
->u
.legacy
.microtile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_1D
?
714 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
715 metadata
->u
.legacy
.macrotile
= surface
->u
.legacy
.level
[0].mode
>= RADEON_SURF_MODE_2D
?
716 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
717 metadata
->u
.legacy
.pipe_config
= surface
->u
.legacy
.pipe_config
;
718 metadata
->u
.legacy
.bankw
= surface
->u
.legacy
.bankw
;
719 metadata
->u
.legacy
.bankh
= surface
->u
.legacy
.bankh
;
720 metadata
->u
.legacy
.tile_split
= surface
->u
.legacy
.tile_split
;
721 metadata
->u
.legacy
.mtilea
= surface
->u
.legacy
.mtilea
;
722 metadata
->u
.legacy
.num_banks
= surface
->u
.legacy
.num_banks
;
723 metadata
->u
.legacy
.stride
= surface
->u
.legacy
.level
[0].nblk_x
* surface
->bpe
;
724 metadata
->u
.legacy
.scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
726 radv_query_opaque_metadata(device
, image
, metadata
);
729 /* The number of samples can be specified independently of the texture. */
731 radv_image_get_fmask_info(struct radv_device
*device
,
732 struct radv_image
*image
,
734 struct radv_fmask_info
*out
)
736 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
737 out
->alignment
= image
->surface
.fmask_alignment
;
738 out
->size
= image
->surface
.fmask_size
;
739 out
->tile_swizzle
= image
->surface
.fmask_tile_swizzle
;
743 out
->slice_tile_max
= image
->surface
.u
.legacy
.fmask
.slice_tile_max
;
744 out
->tile_mode_index
= image
->surface
.u
.legacy
.fmask
.tiling_index
;
745 out
->pitch_in_pixels
= image
->surface
.u
.legacy
.fmask
.pitch_in_pixels
;
746 out
->bank_height
= image
->surface
.u
.legacy
.fmask
.bankh
;
747 out
->tile_swizzle
= image
->surface
.fmask_tile_swizzle
;
748 out
->alignment
= image
->surface
.fmask_alignment
;
749 out
->size
= image
->surface
.fmask_size
;
751 assert(!out
->tile_swizzle
|| !image
->shareable
);
755 radv_image_alloc_fmask(struct radv_device
*device
,
756 struct radv_image
*image
)
758 radv_image_get_fmask_info(device
, image
, image
->info
.samples
, &image
->fmask
);
760 image
->fmask
.offset
= align64(image
->size
, image
->fmask
.alignment
);
761 image
->size
= image
->fmask
.offset
+ image
->fmask
.size
;
762 image
->alignment
= MAX2(image
->alignment
, image
->fmask
.alignment
);
766 radv_image_get_cmask_info(struct radv_device
*device
,
767 struct radv_image
*image
,
768 struct radv_cmask_info
*out
)
770 unsigned pipe_interleave_bytes
= device
->physical_device
->rad_info
.pipe_interleave_bytes
;
771 unsigned num_pipes
= device
->physical_device
->rad_info
.num_tile_pipes
;
772 unsigned cl_width
, cl_height
;
774 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
775 out
->alignment
= image
->surface
.cmask_alignment
;
776 out
->size
= image
->surface
.cmask_size
;
793 case 16: /* Hawaii */
802 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
804 unsigned width
= align(image
->info
.width
, cl_width
*8);
805 unsigned height
= align(image
->info
.height
, cl_height
*8);
806 unsigned slice_elements
= (width
* height
) / (8*8);
808 /* Each element of CMASK is a nibble. */
809 unsigned slice_bytes
= slice_elements
/ 2;
811 out
->slice_tile_max
= (width
* height
) / (128*128);
812 if (out
->slice_tile_max
)
813 out
->slice_tile_max
-= 1;
815 out
->alignment
= MAX2(256, base_align
);
816 out
->size
= (image
->type
== VK_IMAGE_TYPE_3D
? image
->info
.depth
: image
->info
.array_size
) *
817 align(slice_bytes
, base_align
);
821 radv_image_alloc_cmask(struct radv_device
*device
,
822 struct radv_image
*image
)
824 uint32_t clear_value_size
= 0;
825 radv_image_get_cmask_info(device
, image
, &image
->cmask
);
827 image
->cmask
.offset
= align64(image
->size
, image
->cmask
.alignment
);
828 /* + 8 for storing the clear values */
829 if (!image
->clear_value_offset
) {
830 image
->clear_value_offset
= image
->cmask
.offset
+ image
->cmask
.size
;
831 clear_value_size
= 8;
833 image
->size
= image
->cmask
.offset
+ image
->cmask
.size
+ clear_value_size
;
834 image
->alignment
= MAX2(image
->alignment
, image
->cmask
.alignment
);
838 radv_image_alloc_dcc(struct radv_image
*image
)
840 image
->dcc_offset
= align64(image
->size
, image
->surface
.dcc_alignment
);
841 /* + 16 for storing the clear values + dcc pred */
842 image
->clear_value_offset
= image
->dcc_offset
+ image
->surface
.dcc_size
;
843 image
->dcc_pred_offset
= image
->clear_value_offset
+ 8;
844 image
->size
= image
->dcc_offset
+ image
->surface
.dcc_size
+ 16;
845 image
->alignment
= MAX2(image
->alignment
, image
->surface
.dcc_alignment
);
849 radv_image_alloc_htile(struct radv_image
*image
)
851 image
->htile_offset
= align64(image
->size
, image
->surface
.htile_alignment
);
853 /* + 8 for storing the clear values */
854 image
->clear_value_offset
= image
->htile_offset
+ image
->surface
.htile_size
;
855 image
->size
= image
->clear_value_offset
+ 8;
856 image
->alignment
= align64(image
->alignment
, image
->surface
.htile_alignment
);
860 radv_image_can_enable_dcc_or_cmask(struct radv_image
*image
)
862 if (image
->info
.samples
<= 1 &&
863 image
->info
.width
* image
->info
.height
<= 512 * 512) {
864 /* Do not enable CMASK or DCC for small surfaces where the cost
865 * of the eliminate pass can be higher than the benefit of fast
866 * clear. RadeonSI does this, but the image threshold is
872 return image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
&&
873 (image
->exclusive
|| image
->queue_family_mask
== 1);
877 radv_image_can_enable_dcc(struct radv_image
*image
)
879 return radv_image_can_enable_dcc_or_cmask(image
) &&
880 radv_image_has_dcc(image
);
884 radv_image_can_enable_cmask(struct radv_image
*image
)
886 if (image
->surface
.bpe
> 8 && image
->info
.samples
== 1) {
887 /* Do not enable CMASK for non-MSAA images (fast color clear)
888 * because 128 bit formats are not supported, but FMASK might
894 return radv_image_can_enable_dcc_or_cmask(image
) &&
895 image
->info
.levels
== 1 &&
896 image
->info
.depth
== 1 &&
897 !image
->surface
.is_linear
;
901 radv_image_can_enable_fmask(struct radv_image
*image
)
903 return image
->info
.samples
> 1 && vk_format_is_color(image
->vk_format
);
907 radv_image_can_enable_htile(struct radv_image
*image
)
909 return image
->info
.levels
== 1 && vk_format_is_depth(image
->vk_format
);
913 radv_image_create(VkDevice _device
,
914 const struct radv_image_create_info
*create_info
,
915 const VkAllocationCallbacks
* alloc
,
918 RADV_FROM_HANDLE(radv_device
, device
, _device
);
919 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
920 struct radv_image
*image
= NULL
;
921 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
923 radv_assert(pCreateInfo
->mipLevels
> 0);
924 radv_assert(pCreateInfo
->arrayLayers
> 0);
925 radv_assert(pCreateInfo
->samples
> 0);
926 radv_assert(pCreateInfo
->extent
.width
> 0);
927 radv_assert(pCreateInfo
->extent
.height
> 0);
928 radv_assert(pCreateInfo
->extent
.depth
> 0);
930 image
= vk_zalloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
931 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
933 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
935 image
->type
= pCreateInfo
->imageType
;
936 image
->info
.width
= pCreateInfo
->extent
.width
;
937 image
->info
.height
= pCreateInfo
->extent
.height
;
938 image
->info
.depth
= pCreateInfo
->extent
.depth
;
939 image
->info
.samples
= pCreateInfo
->samples
;
940 image
->info
.color_samples
= pCreateInfo
->samples
;
941 image
->info
.array_size
= pCreateInfo
->arrayLayers
;
942 image
->info
.levels
= pCreateInfo
->mipLevels
;
943 image
->info
.num_channels
= vk_format_get_nr_components(pCreateInfo
->format
);
945 image
->vk_format
= pCreateInfo
->format
;
946 image
->tiling
= pCreateInfo
->tiling
;
947 image
->usage
= pCreateInfo
->usage
;
948 image
->flags
= pCreateInfo
->flags
;
950 image
->exclusive
= pCreateInfo
->sharingMode
== VK_SHARING_MODE_EXCLUSIVE
;
951 if (pCreateInfo
->sharingMode
== VK_SHARING_MODE_CONCURRENT
) {
952 for (uint32_t i
= 0; i
< pCreateInfo
->queueFamilyIndexCount
; ++i
)
953 if (pCreateInfo
->pQueueFamilyIndices
[i
] == VK_QUEUE_FAMILY_EXTERNAL_KHR
)
954 image
->queue_family_mask
|= (1u << RADV_MAX_QUEUE_FAMILIES
) - 1u;
956 image
->queue_family_mask
|= 1u << pCreateInfo
->pQueueFamilyIndices
[i
];
959 image
->shareable
= vk_find_struct_const(pCreateInfo
->pNext
,
960 EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR
) != NULL
;
961 if (!vk_format_is_depth(pCreateInfo
->format
) && !create_info
->scanout
&& !image
->shareable
) {
962 image
->info
.surf_index
= &device
->image_mrt_offset_counter
;
965 radv_init_surface(device
, &image
->surface
, create_info
);
967 device
->ws
->surface_init(device
->ws
, &image
->info
, &image
->surface
);
969 image
->size
= image
->surface
.surf_size
;
970 image
->alignment
= image
->surface
.surf_alignment
;
972 if (!create_info
->no_metadata_planes
) {
973 /* Try to enable DCC first. */
974 if (radv_image_can_enable_dcc(image
)) {
975 radv_image_alloc_dcc(image
);
976 if (image
->info
.samples
> 1) {
977 /* CMASK should be enabled because DCC fast
978 * clear with MSAA needs it.
980 assert(radv_image_can_enable_cmask(image
));
981 radv_image_alloc_cmask(device
, image
);
984 /* When DCC cannot be enabled, try CMASK. */
985 image
->surface
.dcc_size
= 0;
986 if (radv_image_can_enable_cmask(image
)) {
987 radv_image_alloc_cmask(device
, image
);
991 /* Try to enable FMASK for multisampled images. */
992 if (radv_image_can_enable_fmask(image
)) {
993 radv_image_alloc_fmask(device
, image
);
995 /* Otherwise, try to enable HTILE for depth surfaces. */
996 if (radv_image_can_enable_htile(image
) &&
997 !(device
->instance
->debug_flags
& RADV_DEBUG_NO_HIZ
)) {
998 radv_image_alloc_htile(image
);
999 image
->tc_compatible_htile
= image
->surface
.flags
& RADEON_SURF_TC_COMPATIBLE_HTILE
;
1001 image
->surface
.htile_size
= 0;
1005 image
->surface
.dcc_size
= 0;
1006 image
->surface
.htile_size
= 0;
1009 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
) {
1010 image
->alignment
= MAX2(image
->alignment
, 4096);
1011 image
->size
= align64(image
->size
, image
->alignment
);
1014 image
->bo
= device
->ws
->buffer_create(device
->ws
, image
->size
, image
->alignment
,
1015 0, RADEON_FLAG_VIRTUAL
);
1017 vk_free2(&device
->alloc
, alloc
, image
);
1018 return vk_error(device
->instance
, VK_ERROR_OUT_OF_DEVICE_MEMORY
);
1022 *pImage
= radv_image_to_handle(image
);
1028 radv_image_view_make_descriptor(struct radv_image_view
*iview
,
1029 struct radv_device
*device
,
1030 const VkComponentMapping
*components
,
1031 bool is_storage_image
)
1033 struct radv_image
*image
= iview
->image
;
1034 bool is_stencil
= iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
;
1036 uint32_t *descriptor
;
1037 uint32_t hw_level
= 0;
1039 if (is_storage_image
) {
1040 descriptor
= iview
->storage_descriptor
;
1042 descriptor
= iview
->descriptor
;
1045 assert(image
->surface
.blk_w
% vk_format_get_blockwidth(image
->vk_format
) == 0);
1046 blk_w
= image
->surface
.blk_w
/ vk_format_get_blockwidth(image
->vk_format
) * vk_format_get_blockwidth(iview
->vk_format
);
1048 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
)
1049 hw_level
= iview
->base_mip
;
1050 si_make_texture_descriptor(device
, image
, is_storage_image
,
1054 hw_level
, hw_level
+ iview
->level_count
- 1,
1056 iview
->base_layer
+ iview
->layer_count
- 1,
1057 iview
->extent
.width
,
1058 iview
->extent
.height
,
1059 iview
->extent
.depth
,
1063 const struct legacy_surf_level
*base_level_info
= NULL
;
1064 if (device
->physical_device
->rad_info
.chip_class
<= GFX9
) {
1066 base_level_info
= &image
->surface
.u
.legacy
.stencil_level
[iview
->base_mip
];
1068 base_level_info
= &image
->surface
.u
.legacy
.level
[iview
->base_mip
];
1070 si_set_mutable_tex_desc_fields(device
, image
,
1074 blk_w
, is_stencil
, is_storage_image
, descriptor
);
1078 radv_image_view_init(struct radv_image_view
*iview
,
1079 struct radv_device
*device
,
1080 const VkImageViewCreateInfo
* pCreateInfo
)
1082 RADV_FROM_HANDLE(radv_image
, image
, pCreateInfo
->image
);
1083 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
1085 switch (image
->type
) {
1086 case VK_IMAGE_TYPE_1D
:
1087 case VK_IMAGE_TYPE_2D
:
1088 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1 <= image
->info
.array_size
);
1090 case VK_IMAGE_TYPE_3D
:
1091 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1
1092 <= radv_minify(image
->info
.depth
, range
->baseMipLevel
));
1095 unreachable("bad VkImageType");
1097 iview
->image
= image
;
1098 iview
->bo
= image
->bo
;
1099 iview
->type
= pCreateInfo
->viewType
;
1100 iview
->vk_format
= pCreateInfo
->format
;
1101 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
1103 if (iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
) {
1104 iview
->vk_format
= vk_format_stencil_only(iview
->vk_format
);
1105 } else if (iview
->aspect_mask
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
1106 iview
->vk_format
= vk_format_depth_only(iview
->vk_format
);
1109 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
1110 iview
->extent
= (VkExtent3D
) {
1111 .width
= image
->info
.width
,
1112 .height
= image
->info
.height
,
1113 .depth
= image
->info
.depth
,
1116 iview
->extent
= (VkExtent3D
) {
1117 .width
= radv_minify(image
->info
.width
, range
->baseMipLevel
),
1118 .height
= radv_minify(image
->info
.height
, range
->baseMipLevel
),
1119 .depth
= radv_minify(image
->info
.depth
, range
->baseMipLevel
),
1123 if (iview
->vk_format
!= image
->vk_format
) {
1124 unsigned view_bw
= vk_format_get_blockwidth(iview
->vk_format
);
1125 unsigned view_bh
= vk_format_get_blockheight(iview
->vk_format
);
1126 unsigned img_bw
= vk_format_get_blockwidth(image
->vk_format
);
1127 unsigned img_bh
= vk_format_get_blockheight(image
->vk_format
);
1129 iview
->extent
.width
= round_up_u32(iview
->extent
.width
* view_bw
, img_bw
);
1130 iview
->extent
.height
= round_up_u32(iview
->extent
.height
* view_bh
, img_bh
);
1132 /* Comment ported from amdvlk -
1133 * If we have the following image:
1134 * Uncompressed pixels Compressed block sizes (4x4)
1135 * mip0: 22 x 22 6 x 6
1136 * mip1: 11 x 11 3 x 3
1141 * On GFX9 the descriptor is always programmed with the WIDTH and HEIGHT of the base level and the HW is
1142 * calculating the degradation of the block sizes down the mip-chain as follows (straight-up
1143 * divide-by-two integer math):
1149 * This means that mip2 will be missing texels.
1151 * Fix this by calculating the base mip's width and height, then convert that, and round it
1152 * back up to get the level 0 size.
1153 * Clamp the converted size between the original values, and next power of two, which
1154 * means we don't oversize the image.
1156 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
&&
1157 vk_format_is_compressed(image
->vk_format
) &&
1158 !vk_format_is_compressed(iview
->vk_format
)) {
1159 unsigned rounded_img_w
= util_next_power_of_two(iview
->extent
.width
);
1160 unsigned rounded_img_h
= util_next_power_of_two(iview
->extent
.height
);
1161 unsigned lvl_width
= radv_minify(image
->info
.width
, range
->baseMipLevel
);
1162 unsigned lvl_height
= radv_minify(image
->info
.height
, range
->baseMipLevel
);
1164 lvl_width
= round_up_u32(lvl_width
* view_bw
, img_bw
);
1165 lvl_height
= round_up_u32(lvl_height
* view_bh
, img_bh
);
1167 lvl_width
<<= range
->baseMipLevel
;
1168 lvl_height
<<= range
->baseMipLevel
;
1170 iview
->extent
.width
= CLAMP(lvl_width
, iview
->extent
.width
, rounded_img_w
);
1171 iview
->extent
.height
= CLAMP(lvl_height
, iview
->extent
.height
, rounded_img_h
);
1175 iview
->base_layer
= range
->baseArrayLayer
;
1176 iview
->layer_count
= radv_get_layerCount(image
, range
);
1177 iview
->base_mip
= range
->baseMipLevel
;
1178 iview
->level_count
= radv_get_levelCount(image
, range
);
1180 radv_image_view_make_descriptor(iview
, device
, &pCreateInfo
->components
, false);
1181 radv_image_view_make_descriptor(iview
, device
, &pCreateInfo
->components
, true);
1184 bool radv_layout_has_htile(const struct radv_image
*image
,
1185 VkImageLayout layout
,
1186 unsigned queue_mask
)
1188 if (radv_image_is_tc_compat_htile(image
))
1189 return layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1191 return radv_image_has_htile(image
) &&
1192 (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
1193 layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
) &&
1194 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
1197 bool radv_layout_is_htile_compressed(const struct radv_image
*image
,
1198 VkImageLayout layout
,
1199 unsigned queue_mask
)
1201 if (radv_image_is_tc_compat_htile(image
))
1202 return layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1204 return radv_image_has_htile(image
) &&
1205 (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
1206 layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
) &&
1207 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
1210 bool radv_layout_can_fast_clear(const struct radv_image
*image
,
1211 VkImageLayout layout
,
1212 unsigned queue_mask
)
1214 return layout
== VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
&&
1215 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
1218 bool radv_layout_dcc_compressed(const struct radv_image
*image
,
1219 VkImageLayout layout
,
1220 unsigned queue_mask
)
1222 /* Don't compress compute transfer dst, as image stores are not supported. */
1223 if (layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
&&
1224 (queue_mask
& (1u << RADV_QUEUE_COMPUTE
)))
1227 return radv_image_has_dcc(image
) && layout
!= VK_IMAGE_LAYOUT_GENERAL
;
1231 unsigned radv_image_queue_family_mask(const struct radv_image
*image
, uint32_t family
, uint32_t queue_family
)
1233 if (!image
->exclusive
)
1234 return image
->queue_family_mask
;
1235 if (family
== VK_QUEUE_FAMILY_EXTERNAL_KHR
)
1236 return (1u << RADV_MAX_QUEUE_FAMILIES
) - 1u;
1237 if (family
== VK_QUEUE_FAMILY_IGNORED
)
1238 return 1u << queue_family
;
1239 return 1u << family
;
1243 radv_CreateImage(VkDevice device
,
1244 const VkImageCreateInfo
*pCreateInfo
,
1245 const VkAllocationCallbacks
*pAllocator
,
1249 const VkNativeBufferANDROID
*gralloc_info
=
1250 vk_find_struct_const(pCreateInfo
->pNext
, NATIVE_BUFFER_ANDROID
);
1253 return radv_image_from_gralloc(device
, pCreateInfo
, gralloc_info
,
1254 pAllocator
, pImage
);
1257 const struct wsi_image_create_info
*wsi_info
=
1258 vk_find_struct_const(pCreateInfo
->pNext
, WSI_IMAGE_CREATE_INFO_MESA
);
1259 bool scanout
= wsi_info
&& wsi_info
->scanout
;
1261 return radv_image_create(device
,
1262 &(struct radv_image_create_info
) {
1263 .vk_info
= pCreateInfo
,
1271 radv_DestroyImage(VkDevice _device
, VkImage _image
,
1272 const VkAllocationCallbacks
*pAllocator
)
1274 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1275 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1280 if (image
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
)
1281 device
->ws
->buffer_destroy(image
->bo
);
1283 if (image
->owned_memory
!= VK_NULL_HANDLE
)
1284 radv_FreeMemory(_device
, image
->owned_memory
, pAllocator
);
1286 vk_free2(&device
->alloc
, pAllocator
, image
);
1289 void radv_GetImageSubresourceLayout(
1292 const VkImageSubresource
* pSubresource
,
1293 VkSubresourceLayout
* pLayout
)
1295 RADV_FROM_HANDLE(radv_image
, image
, _image
);
1296 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1297 int level
= pSubresource
->mipLevel
;
1298 int layer
= pSubresource
->arrayLayer
;
1299 struct radeon_surf
*surface
= &image
->surface
;
1301 if (device
->physical_device
->rad_info
.chip_class
>= GFX9
) {
1302 pLayout
->offset
= surface
->u
.gfx9
.offset
[level
] + surface
->u
.gfx9
.surf_slice_size
* layer
;
1303 pLayout
->rowPitch
= surface
->u
.gfx9
.surf_pitch
* surface
->bpe
;
1304 pLayout
->arrayPitch
= surface
->u
.gfx9
.surf_slice_size
;
1305 pLayout
->depthPitch
= surface
->u
.gfx9
.surf_slice_size
;
1306 pLayout
->size
= surface
->u
.gfx9
.surf_slice_size
;
1307 if (image
->type
== VK_IMAGE_TYPE_3D
)
1308 pLayout
->size
*= u_minify(image
->info
.depth
, level
);
1310 pLayout
->offset
= surface
->u
.legacy
.level
[level
].offset
+ (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4 * layer
;
1311 pLayout
->rowPitch
= surface
->u
.legacy
.level
[level
].nblk_x
* surface
->bpe
;
1312 pLayout
->arrayPitch
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1313 pLayout
->depthPitch
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1314 pLayout
->size
= (uint64_t)surface
->u
.legacy
.level
[level
].slice_size_dw
* 4;
1315 if (image
->type
== VK_IMAGE_TYPE_3D
)
1316 pLayout
->size
*= u_minify(image
->info
.depth
, level
);
1322 radv_CreateImageView(VkDevice _device
,
1323 const VkImageViewCreateInfo
*pCreateInfo
,
1324 const VkAllocationCallbacks
*pAllocator
,
1327 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1328 struct radv_image_view
*view
;
1330 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1331 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1333 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1335 radv_image_view_init(view
, device
, pCreateInfo
);
1337 *pView
= radv_image_view_to_handle(view
);
1343 radv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
1344 const VkAllocationCallbacks
*pAllocator
)
1346 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1347 RADV_FROM_HANDLE(radv_image_view
, iview
, _iview
);
1351 vk_free2(&device
->alloc
, pAllocator
, iview
);
1354 void radv_buffer_view_init(struct radv_buffer_view
*view
,
1355 struct radv_device
*device
,
1356 const VkBufferViewCreateInfo
* pCreateInfo
)
1358 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCreateInfo
->buffer
);
1360 view
->bo
= buffer
->bo
;
1361 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
1362 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
1363 view
->vk_format
= pCreateInfo
->format
;
1365 radv_make_buffer_descriptor(device
, buffer
, view
->vk_format
,
1366 pCreateInfo
->offset
, view
->range
, view
->state
);
1370 radv_CreateBufferView(VkDevice _device
,
1371 const VkBufferViewCreateInfo
*pCreateInfo
,
1372 const VkAllocationCallbacks
*pAllocator
,
1373 VkBufferView
*pView
)
1375 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1376 struct radv_buffer_view
*view
;
1378 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1379 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1381 return vk_error(device
->instance
, VK_ERROR_OUT_OF_HOST_MEMORY
);
1383 radv_buffer_view_init(view
, device
, pCreateInfo
);
1385 *pView
= radv_buffer_view_to_handle(view
);
1391 radv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
1392 const VkAllocationCallbacks
*pAllocator
)
1394 RADV_FROM_HANDLE(radv_device
, device
, _device
);
1395 RADV_FROM_HANDLE(radv_buffer_view
, view
, bufferView
);
1400 vk_free2(&device
->alloc
, pAllocator
, view
);