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_private.h"
29 #include "vk_format.h"
30 #include "radv_radeon_winsys.h"
32 #include "util/debug.h"
34 radv_choose_tiling(struct radv_device
*Device
,
35 const struct radv_image_create_info
*create_info
)
37 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
39 if (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
) {
40 assert(pCreateInfo
->samples
<= 1);
41 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
44 /* Textures with a very small height are recommended to be linear. */
45 if (pCreateInfo
->imageType
== VK_IMAGE_TYPE_1D
||
46 /* Only very thin and long 2D textures should benefit from
48 (pCreateInfo
->extent
.width
> 8 && pCreateInfo
->extent
.height
<= 2))
49 return RADEON_SURF_MODE_LINEAR_ALIGNED
;
51 /* MSAA resources must be 2D tiled. */
52 if (pCreateInfo
->samples
> 1)
53 return RADEON_SURF_MODE_2D
;
55 return RADEON_SURF_MODE_2D
;
58 radv_init_surface(struct radv_device
*device
,
59 struct radeon_surf
*surface
,
60 const struct radv_image_create_info
*create_info
)
62 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
63 unsigned array_mode
= radv_choose_tiling(device
, create_info
);
64 const struct vk_format_description
*desc
=
65 vk_format_description(pCreateInfo
->format
);
66 bool is_depth
, is_stencil
, blendable
;
68 is_depth
= vk_format_has_depth(desc
);
69 is_stencil
= vk_format_has_stencil(desc
);
71 surface
->blk_w
= vk_format_get_blockwidth(pCreateInfo
->format
);
72 surface
->blk_h
= vk_format_get_blockheight(pCreateInfo
->format
);
74 surface
->bpe
= vk_format_get_blocksize(pCreateInfo
->format
);
75 /* align byte per element on dword */
76 if (surface
->bpe
== 3) {
79 surface
->flags
= RADEON_SURF_SET(array_mode
, MODE
);
81 switch (pCreateInfo
->imageType
){
82 case VK_IMAGE_TYPE_1D
:
83 if (pCreateInfo
->arrayLayers
> 1)
84 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D_ARRAY
, TYPE
);
86 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_1D
, TYPE
);
88 case VK_IMAGE_TYPE_2D
:
89 if (pCreateInfo
->arrayLayers
> 1)
90 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D_ARRAY
, TYPE
);
92 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_2D
, TYPE
);
94 case VK_IMAGE_TYPE_3D
:
95 surface
->flags
|= RADEON_SURF_SET(RADEON_SURF_TYPE_3D
, TYPE
);
98 unreachable("unhandled image type");
102 surface
->flags
|= RADEON_SURF_ZBUFFER
;
106 surface
->flags
|= RADEON_SURF_SBUFFER
|
107 RADEON_SURF_HAS_SBUFFER_MIPTREE
;
109 surface
->flags
|= RADEON_SURF_HAS_TILE_MODE_INDEX
;
111 if ((pCreateInfo
->usage
& (VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
112 VK_IMAGE_USAGE_STORAGE_BIT
)) ||
113 (pCreateInfo
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) ||
114 (pCreateInfo
->tiling
== VK_IMAGE_TILING_LINEAR
) ||
115 device
->physical_device
->rad_info
.chip_class
< VI
||
116 create_info
->scanout
|| (device
->debug_flags
& RADV_DEBUG_NO_DCC
) ||
117 !radv_is_colorbuffer_format_supported(pCreateInfo
->format
, &blendable
))
118 surface
->flags
|= RADEON_SURF_DISABLE_DCC
;
119 if (create_info
->scanout
)
120 surface
->flags
|= RADEON_SURF_SCANOUT
;
123 #define ATI_VENDOR_ID 0x1002
124 static uint32_t si_get_bo_metadata_word1(struct radv_device
*device
)
126 return (ATI_VENDOR_ID
<< 16) | device
->physical_device
->rad_info
.pci_id
;
129 static inline unsigned
130 si_tile_mode_index(const struct radv_image
*image
, unsigned level
, bool stencil
)
133 return image
->surface
.stencil_tiling_index
[level
];
135 return image
->surface
.tiling_index
[level
];
138 static unsigned radv_map_swizzle(unsigned swizzle
)
142 return V_008F0C_SQ_SEL_Y
;
144 return V_008F0C_SQ_SEL_Z
;
146 return V_008F0C_SQ_SEL_W
;
148 return V_008F0C_SQ_SEL_0
;
150 return V_008F0C_SQ_SEL_1
;
151 default: /* VK_SWIZZLE_X */
152 return V_008F0C_SQ_SEL_X
;
157 radv_make_buffer_descriptor(struct radv_device
*device
,
158 struct radv_buffer
*buffer
,
164 const struct vk_format_description
*desc
;
166 uint64_t gpu_address
= device
->ws
->buffer_get_va(buffer
->bo
);
167 uint64_t va
= gpu_address
+ buffer
->offset
;
168 unsigned num_format
, data_format
;
170 desc
= vk_format_description(vk_format
);
171 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
172 stride
= desc
->block
.bits
/ 8;
174 num_format
= radv_translate_buffer_numformat(desc
, first_non_void
);
175 data_format
= radv_translate_buffer_dataformat(desc
, first_non_void
);
179 state
[1] = S_008F04_BASE_ADDRESS_HI(va
>> 32) |
180 S_008F04_STRIDE(stride
);
182 state
[3] = S_008F0C_DST_SEL_X(radv_map_swizzle(desc
->swizzle
[0])) |
183 S_008F0C_DST_SEL_Y(radv_map_swizzle(desc
->swizzle
[1])) |
184 S_008F0C_DST_SEL_Z(radv_map_swizzle(desc
->swizzle
[2])) |
185 S_008F0C_DST_SEL_W(radv_map_swizzle(desc
->swizzle
[3])) |
186 S_008F0C_NUM_FORMAT(num_format
) |
187 S_008F0C_DATA_FORMAT(data_format
);
191 si_set_mutable_tex_desc_fields(struct radv_device
*device
,
192 struct radv_image
*image
,
193 const struct radeon_surf_level
*base_level_info
,
194 unsigned base_level
, unsigned first_level
,
195 unsigned block_width
, bool is_stencil
,
198 uint64_t gpu_address
= device
->ws
->buffer_get_va(image
->bo
) + image
->offset
;
199 uint64_t va
= gpu_address
+ base_level_info
->offset
;
200 unsigned pitch
= base_level_info
->nblk_x
* block_width
;
202 state
[1] &= C_008F14_BASE_ADDRESS_HI
;
203 state
[3] &= C_008F1C_TILING_INDEX
;
204 state
[4] &= C_008F20_PITCH_GFX6
;
205 state
[6] &= C_008F28_COMPRESSION_EN
;
210 state
[1] |= S_008F14_BASE_ADDRESS_HI(va
>> 40);
211 state
[3] |= S_008F1C_TILING_INDEX(si_tile_mode_index(image
, base_level
,
213 state
[4] |= S_008F20_PITCH_GFX6(pitch
- 1);
215 if (image
->surface
.dcc_size
&& image
->surface
.level
[first_level
].dcc_enabled
) {
216 state
[6] |= S_008F28_COMPRESSION_EN(1);
217 state
[7] = (gpu_address
+
219 base_level_info
->dcc_offset
) >> 8;
223 static unsigned radv_tex_dim(VkImageType image_type
, VkImageViewType view_type
,
224 unsigned nr_layers
, unsigned nr_samples
, bool is_storage_image
)
226 if (view_type
== VK_IMAGE_VIEW_TYPE_CUBE
|| view_type
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
)
227 return is_storage_image
? V_008F1C_SQ_RSRC_IMG_2D_ARRAY
: V_008F1C_SQ_RSRC_IMG_CUBE
;
228 switch (image_type
) {
229 case VK_IMAGE_TYPE_1D
:
230 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_1D_ARRAY
: V_008F1C_SQ_RSRC_IMG_1D
;
231 case VK_IMAGE_TYPE_2D
:
233 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D_MSAA
;
235 return nr_layers
> 1 ? V_008F1C_SQ_RSRC_IMG_2D_ARRAY
: V_008F1C_SQ_RSRC_IMG_2D
;
236 case VK_IMAGE_TYPE_3D
:
237 if (view_type
== VK_IMAGE_VIEW_TYPE_3D
)
238 return V_008F1C_SQ_RSRC_IMG_3D
;
240 return V_008F1C_SQ_RSRC_IMG_2D_ARRAY
;
242 unreachable("illegale image type");
246 * Build the sampler view descriptor for a texture.
249 si_make_texture_descriptor(struct radv_device
*device
,
250 struct radv_image
*image
,
252 VkImageViewType view_type
,
254 const VkComponentMapping
*mapping
,
255 unsigned first_level
, unsigned last_level
,
256 unsigned first_layer
, unsigned last_layer
,
257 unsigned width
, unsigned height
, unsigned depth
,
259 uint32_t *fmask_state
)
261 const struct vk_format_description
*desc
;
262 enum vk_swizzle swizzle
[4];
264 unsigned num_format
, data_format
, type
;
266 desc
= vk_format_description(vk_format
);
268 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_ZS
) {
269 const unsigned char swizzle_xxxx
[4] = {0, 0, 0, 0};
270 vk_format_compose_swizzles(mapping
, swizzle_xxxx
, swizzle
);
272 vk_format_compose_swizzles(mapping
, desc
->swizzle
, swizzle
);
275 first_non_void
= vk_format_get_first_non_void_channel(vk_format
);
277 num_format
= radv_translate_tex_numformat(vk_format
, desc
, first_non_void
);
278 if (num_format
== ~0) {
282 data_format
= radv_translate_tex_dataformat(vk_format
, desc
, first_non_void
);
283 if (data_format
== ~0) {
287 type
= radv_tex_dim(image
->type
, view_type
, image
->info
.array_size
, image
->info
.samples
,
288 (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
289 if (type
== V_008F1C_SQ_RSRC_IMG_1D_ARRAY
) {
291 depth
= image
->info
.array_size
;
292 } else if (type
== V_008F1C_SQ_RSRC_IMG_2D_ARRAY
||
293 type
== V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY
) {
294 if (view_type
!= VK_IMAGE_VIEW_TYPE_3D
)
295 depth
= image
->info
.array_size
;
296 } else if (type
== V_008F1C_SQ_RSRC_IMG_CUBE
)
297 depth
= image
->info
.array_size
/ 6;
300 state
[1] = (S_008F14_DATA_FORMAT_GFX6(data_format
) |
301 S_008F14_NUM_FORMAT_GFX6(num_format
));
302 state
[2] = (S_008F18_WIDTH(width
- 1) |
303 S_008F18_HEIGHT(height
- 1) |
304 S_008F18_PERF_MOD(4));
305 state
[3] = (S_008F1C_DST_SEL_X(radv_map_swizzle(swizzle
[0])) |
306 S_008F1C_DST_SEL_Y(radv_map_swizzle(swizzle
[1])) |
307 S_008F1C_DST_SEL_Z(radv_map_swizzle(swizzle
[2])) |
308 S_008F1C_DST_SEL_W(radv_map_swizzle(swizzle
[3])) |
309 S_008F1C_BASE_LEVEL(image
->info
.samples
> 1 ?
311 S_008F1C_LAST_LEVEL(image
->info
.samples
> 1 ?
312 util_logbase2(image
->info
.samples
) :
314 S_008F1C_POW2_PAD(image
->info
.levels
> 1) |
315 S_008F1C_TYPE(type
));
316 state
[4] = S_008F20_DEPTH(depth
- 1);
317 state
[5] = (S_008F24_BASE_ARRAY(first_layer
) |
318 S_008F24_LAST_ARRAY(last_layer
));
322 if (image
->dcc_offset
) {
323 unsigned swap
= radv_translate_colorswap(vk_format
, FALSE
);
325 state
[6] = S_008F28_ALPHA_IS_ON_MSB(swap
<= 1);
327 /* The last dword is unused by hw. The shader uses it to clear
328 * bits in the first dword of sampler state.
330 if (device
->physical_device
->rad_info
.chip_class
<= CIK
&& image
->info
.samples
<= 1) {
331 if (first_level
== last_level
)
332 state
[7] = C_008F30_MAX_ANISO_RATIO
;
334 state
[7] = 0xffffffff;
338 /* Initialize the sampler view for FMASK. */
339 if (image
->fmask
.size
) {
340 uint32_t fmask_format
;
341 uint64_t gpu_address
= device
->ws
->buffer_get_va(image
->bo
);
344 va
= gpu_address
+ image
->offset
+ image
->fmask
.offset
;
346 switch (image
->info
.samples
) {
348 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2
;
351 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4
;
354 fmask_format
= V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8
;
358 fmask_format
= V_008F14_IMG_DATA_FORMAT_INVALID
;
361 fmask_state
[0] = va
>> 8;
362 fmask_state
[1] = S_008F14_BASE_ADDRESS_HI(va
>> 40) |
363 S_008F14_DATA_FORMAT_GFX6(fmask_format
) |
364 S_008F14_NUM_FORMAT_GFX6(V_008F14_IMG_NUM_FORMAT_UINT
);
365 fmask_state
[2] = S_008F18_WIDTH(width
- 1) |
366 S_008F18_HEIGHT(height
- 1);
367 fmask_state
[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X
) |
368 S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X
) |
369 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X
) |
370 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X
) |
371 S_008F1C_TILING_INDEX(image
->fmask
.tile_mode_index
) |
372 S_008F1C_TYPE(radv_tex_dim(image
->type
, view_type
, 1, 0, false));
373 fmask_state
[4] = S_008F20_DEPTH(depth
- 1) |
374 S_008F20_PITCH_GFX6(image
->fmask
.pitch_in_pixels
- 1);
375 fmask_state
[5] = S_008F24_BASE_ARRAY(first_layer
) |
376 S_008F24_LAST_ARRAY(last_layer
);
383 radv_query_opaque_metadata(struct radv_device
*device
,
384 struct radv_image
*image
,
385 struct radeon_bo_metadata
*md
)
387 static const VkComponentMapping fixedmapping
;
390 /* Metadata image format format version 1:
391 * [0] = 1 (metadata format identifier)
392 * [1] = (VENDOR_ID << 16) | PCI_ID
393 * [2:9] = image descriptor for the whole resource
394 * [2] is always 0, because the base address is cleared
395 * [9] is the DCC offset bits [39:8] from the beginning of
397 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
399 md
->metadata
[0] = 1; /* metadata image format version 1 */
401 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
402 md
->metadata
[1] = si_get_bo_metadata_word1(device
);
405 si_make_texture_descriptor(device
, image
, true,
406 (VkImageViewType
)image
->type
, image
->vk_format
,
407 &fixedmapping
, 0, image
->info
.levels
- 1, 0,
408 image
->info
.array_size
,
409 image
->info
.width
, image
->info
.height
,
413 si_set_mutable_tex_desc_fields(device
, image
, &image
->surface
.level
[0], 0, 0,
414 image
->surface
.blk_w
, false, desc
);
416 /* Clear the base address and set the relative DCC offset. */
418 desc
[1] &= C_008F14_BASE_ADDRESS_HI
;
419 desc
[7] = image
->dcc_offset
>> 8;
421 /* Dwords [2:9] contain the image descriptor. */
422 memcpy(&md
->metadata
[2], desc
, sizeof(desc
));
424 /* Dwords [10:..] contain the mipmap level offsets. */
425 for (i
= 0; i
<= image
->info
.levels
- 1; i
++)
426 md
->metadata
[10+i
] = image
->surface
.level
[i
].offset
>> 8;
428 md
->size_metadata
= (11 + image
->info
.levels
- 1) * 4;
432 radv_init_metadata(struct radv_device
*device
,
433 struct radv_image
*image
,
434 struct radeon_bo_metadata
*metadata
)
436 struct radeon_surf
*surface
= &image
->surface
;
438 memset(metadata
, 0, sizeof(*metadata
));
439 metadata
->microtile
= surface
->level
[0].mode
>= RADEON_SURF_MODE_1D
?
440 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
441 metadata
->macrotile
= surface
->level
[0].mode
>= RADEON_SURF_MODE_2D
?
442 RADEON_LAYOUT_TILED
: RADEON_LAYOUT_LINEAR
;
443 metadata
->pipe_config
= surface
->pipe_config
;
444 metadata
->bankw
= surface
->bankw
;
445 metadata
->bankh
= surface
->bankh
;
446 metadata
->tile_split
= surface
->tile_split
;
447 metadata
->mtilea
= surface
->mtilea
;
448 metadata
->num_banks
= surface
->num_banks
;
449 metadata
->stride
= surface
->level
[0].pitch_bytes
;
450 metadata
->scanout
= (surface
->flags
& RADEON_SURF_SCANOUT
) != 0;
452 radv_query_opaque_metadata(device
, image
, metadata
);
455 /* The number of samples can be specified independently of the texture. */
457 radv_image_get_fmask_info(struct radv_device
*device
,
458 struct radv_image
*image
,
460 struct radv_fmask_info
*out
)
462 /* FMASK is allocated like an ordinary texture. */
463 struct radeon_surf fmask
= image
->surface
;
464 struct radeon_surf_info info
= image
->info
;
465 memset(out
, 0, sizeof(*out
));
467 fmask
.bo_alignment
= 0;
469 fmask
.flags
|= RADEON_SURF_FMASK
;
471 /* Force 2D tiling if it wasn't set. This may occur when creating
472 * FMASK for MSAA resolve on R6xx. On R6xx, the single-sample
473 * destination buffer must have an FMASK too. */
474 fmask
.flags
= RADEON_SURF_CLR(fmask
.flags
, MODE
);
475 fmask
.flags
|= RADEON_SURF_SET(RADEON_SURF_MODE_2D
, MODE
);
477 fmask
.flags
|= RADEON_SURF_HAS_TILE_MODE_INDEX
;
479 switch (nr_samples
) {
491 device
->ws
->surface_init(device
->ws
, &info
, &fmask
);
492 assert(fmask
.level
[0].mode
== RADEON_SURF_MODE_2D
);
494 out
->slice_tile_max
= (fmask
.level
[0].nblk_x
* fmask
.level
[0].nblk_y
) / 64;
495 if (out
->slice_tile_max
)
496 out
->slice_tile_max
-= 1;
498 out
->tile_mode_index
= fmask
.tiling_index
[0];
499 out
->pitch_in_pixels
= fmask
.level
[0].nblk_x
;
500 out
->bank_height
= fmask
.bankh
;
501 out
->alignment
= MAX2(256, fmask
.bo_alignment
);
502 out
->size
= fmask
.bo_size
;
506 radv_image_alloc_fmask(struct radv_device
*device
,
507 struct radv_image
*image
)
509 radv_image_get_fmask_info(device
, image
, image
->info
.samples
, &image
->fmask
);
511 image
->fmask
.offset
= align64(image
->size
, image
->fmask
.alignment
);
512 image
->size
= image
->fmask
.offset
+ image
->fmask
.size
;
513 image
->alignment
= MAX2(image
->alignment
, image
->fmask
.alignment
);
517 radv_image_get_cmask_info(struct radv_device
*device
,
518 struct radv_image
*image
,
519 struct radv_cmask_info
*out
)
521 unsigned pipe_interleave_bytes
= device
->physical_device
->rad_info
.pipe_interleave_bytes
;
522 unsigned num_pipes
= device
->physical_device
->rad_info
.num_tile_pipes
;
523 unsigned cl_width
, cl_height
;
538 case 16: /* Hawaii */
547 unsigned base_align
= num_pipes
* pipe_interleave_bytes
;
549 unsigned width
= align(image
->info
.width
, cl_width
*8);
550 unsigned height
= align(image
->info
.height
, cl_height
*8);
551 unsigned slice_elements
= (width
* height
) / (8*8);
553 /* Each element of CMASK is a nibble. */
554 unsigned slice_bytes
= slice_elements
/ 2;
556 out
->slice_tile_max
= (width
* height
) / (128*128);
557 if (out
->slice_tile_max
)
558 out
->slice_tile_max
-= 1;
560 out
->alignment
= MAX2(256, base_align
);
561 out
->size
= (image
->type
== VK_IMAGE_TYPE_3D
? image
->info
.depth
: image
->info
.array_size
) *
562 align(slice_bytes
, base_align
);
566 radv_image_alloc_cmask(struct radv_device
*device
,
567 struct radv_image
*image
)
569 radv_image_get_cmask_info(device
, image
, &image
->cmask
);
571 image
->cmask
.offset
= align64(image
->size
, image
->cmask
.alignment
);
572 /* + 8 for storing the clear values */
573 image
->clear_value_offset
= image
->cmask
.offset
+ image
->cmask
.size
;
574 image
->size
= image
->cmask
.offset
+ image
->cmask
.size
+ 8;
575 image
->alignment
= MAX2(image
->alignment
, image
->cmask
.alignment
);
579 radv_image_alloc_dcc(struct radv_device
*device
,
580 struct radv_image
*image
)
582 image
->dcc_offset
= align64(image
->size
, image
->surface
.dcc_alignment
);
583 /* + 8 for storing the clear values */
584 image
->clear_value_offset
= image
->dcc_offset
+ image
->surface
.dcc_size
;
585 image
->size
= image
->dcc_offset
+ image
->surface
.dcc_size
+ 8;
586 image
->alignment
= MAX2(image
->alignment
, image
->surface
.dcc_alignment
);
590 radv_image_alloc_htile(struct radv_device
*device
,
591 struct radv_image
*image
)
593 if ((device
->debug_flags
& RADV_DEBUG_NO_HIZ
) || image
->info
.levels
> 1) {
594 image
->surface
.htile_size
= 0;
598 image
->htile_offset
= align64(image
->size
, image
->surface
.htile_alignment
);
600 /* + 8 for storing the clear values */
601 image
->clear_value_offset
= image
->htile_offset
+ image
->surface
.htile_size
;
602 image
->size
= image
->clear_value_offset
+ 8;
603 image
->alignment
= align64(image
->alignment
, image
->surface
.htile_alignment
);
607 radv_image_create(VkDevice _device
,
608 const struct radv_image_create_info
*create_info
,
609 const VkAllocationCallbacks
* alloc
,
612 RADV_FROM_HANDLE(radv_device
, device
, _device
);
613 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
614 struct radv_image
*image
= NULL
;
615 bool can_cmask_dcc
= false;
616 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
618 radv_assert(pCreateInfo
->mipLevels
> 0);
619 radv_assert(pCreateInfo
->arrayLayers
> 0);
620 radv_assert(pCreateInfo
->samples
> 0);
621 radv_assert(pCreateInfo
->extent
.width
> 0);
622 radv_assert(pCreateInfo
->extent
.height
> 0);
623 radv_assert(pCreateInfo
->extent
.depth
> 0);
625 image
= vk_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
626 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
628 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
630 memset(image
, 0, sizeof(*image
));
631 image
->type
= pCreateInfo
->imageType
;
632 image
->info
.width
= pCreateInfo
->extent
.width
;
633 image
->info
.height
= pCreateInfo
->extent
.height
;
634 image
->info
.depth
= pCreateInfo
->extent
.depth
;
635 image
->info
.samples
= pCreateInfo
->samples
;
636 image
->info
.array_size
= pCreateInfo
->arrayLayers
;
637 image
->info
.levels
= pCreateInfo
->mipLevels
;
639 image
->vk_format
= pCreateInfo
->format
;
640 image
->tiling
= pCreateInfo
->tiling
;
641 image
->usage
= pCreateInfo
->usage
;
642 image
->flags
= pCreateInfo
->flags
;
644 image
->exclusive
= pCreateInfo
->sharingMode
== VK_SHARING_MODE_EXCLUSIVE
;
645 if (pCreateInfo
->sharingMode
== VK_SHARING_MODE_CONCURRENT
) {
646 for (uint32_t i
= 0; i
< pCreateInfo
->queueFamilyIndexCount
; ++i
)
647 image
->queue_family_mask
|= 1u << pCreateInfo
->pQueueFamilyIndices
[i
];
650 radv_init_surface(device
, &image
->surface
, create_info
);
652 device
->ws
->surface_init(device
->ws
, &image
->info
, &image
->surface
);
654 image
->size
= image
->surface
.bo_size
;
655 image
->alignment
= image
->surface
.bo_alignment
;
657 if (image
->exclusive
|| image
->queue_family_mask
== 1)
658 can_cmask_dcc
= true;
660 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) &&
661 image
->surface
.dcc_size
&& can_cmask_dcc
)
662 radv_image_alloc_dcc(device
, image
);
664 image
->surface
.dcc_size
= 0;
666 if ((pCreateInfo
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) &&
667 pCreateInfo
->mipLevels
== 1 &&
668 !image
->surface
.dcc_size
&& image
->info
.depth
== 1 && can_cmask_dcc
)
669 radv_image_alloc_cmask(device
, image
);
670 if (image
->info
.samples
> 1 && vk_format_is_color(pCreateInfo
->format
)) {
671 radv_image_alloc_fmask(device
, image
);
672 } else if (vk_format_is_depth(pCreateInfo
->format
)) {
674 radv_image_alloc_htile(device
, image
);
678 if (create_info
->stride
&& create_info
->stride
!= image
->surface
.level
[0].pitch_bytes
) {
679 image
->surface
.level
[0].nblk_x
= create_info
->stride
/ image
->surface
.bpe
;
680 image
->surface
.level
[0].pitch_bytes
= create_info
->stride
;
681 image
->surface
.level
[0].slice_size
= create_info
->stride
* image
->surface
.level
[0].nblk_y
;
684 if (pCreateInfo
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
) {
685 image
->alignment
= MAX2(image
->alignment
, 4096);
686 image
->size
= align64(image
->size
, image
->alignment
);
689 image
->bo
= device
->ws
->buffer_create(device
->ws
, image
->size
, image
->alignment
,
690 0, RADEON_FLAG_VIRTUAL
);
692 vk_free2(&device
->alloc
, alloc
, image
);
693 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
697 *pImage
= radv_image_to_handle(image
);
703 radv_image_view_init(struct radv_image_view
*iview
,
704 struct radv_device
*device
,
705 const VkImageViewCreateInfo
* pCreateInfo
,
706 struct radv_cmd_buffer
*cmd_buffer
,
707 VkImageUsageFlags usage_mask
)
709 RADV_FROM_HANDLE(radv_image
, image
, pCreateInfo
->image
);
710 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
712 bool is_stencil
= false;
713 switch (image
->type
) {
714 case VK_IMAGE_TYPE_1D
:
715 case VK_IMAGE_TYPE_2D
:
716 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1 <= image
->info
.array_size
);
718 case VK_IMAGE_TYPE_3D
:
719 assert(range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1
720 <= radv_minify(image
->info
.depth
, range
->baseMipLevel
));
723 unreachable("bad VkImageType");
725 iview
->image
= image
;
726 iview
->bo
= image
->bo
;
727 iview
->type
= pCreateInfo
->viewType
;
728 iview
->vk_format
= pCreateInfo
->format
;
729 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
731 if (iview
->aspect_mask
== VK_IMAGE_ASPECT_STENCIL_BIT
) {
733 iview
->vk_format
= vk_format_stencil_only(iview
->vk_format
);
734 } else if (iview
->aspect_mask
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
735 iview
->vk_format
= vk_format_depth_only(iview
->vk_format
);
738 iview
->extent
= (VkExtent3D
) {
739 .width
= radv_minify(image
->info
.width
, range
->baseMipLevel
),
740 .height
= radv_minify(image
->info
.height
, range
->baseMipLevel
),
741 .depth
= radv_minify(image
->info
.depth
, range
->baseMipLevel
),
744 iview
->extent
.width
= round_up_u32(iview
->extent
.width
* vk_format_get_blockwidth(iview
->vk_format
),
745 vk_format_get_blockwidth(image
->vk_format
));
746 iview
->extent
.height
= round_up_u32(iview
->extent
.height
* vk_format_get_blockheight(iview
->vk_format
),
747 vk_format_get_blockheight(image
->vk_format
));
749 assert(image
->surface
.blk_w
% vk_format_get_blockwidth(image
->vk_format
) == 0);
750 blk_w
= image
->surface
.blk_w
/ vk_format_get_blockwidth(image
->vk_format
) * vk_format_get_blockwidth(iview
->vk_format
);
751 iview
->base_layer
= range
->baseArrayLayer
;
752 iview
->layer_count
= radv_get_layerCount(image
, range
);
753 iview
->base_mip
= range
->baseMipLevel
;
755 si_make_texture_descriptor(device
, image
, false,
758 &pCreateInfo
->components
,
759 0, radv_get_levelCount(image
, range
) - 1,
760 range
->baseArrayLayer
,
761 range
->baseArrayLayer
+ radv_get_layerCount(image
, range
) - 1,
763 iview
->extent
.height
,
766 iview
->fmask_descriptor
);
767 si_set_mutable_tex_desc_fields(device
, image
,
768 is_stencil
? &image
->surface
.stencil_level
[range
->baseMipLevel
] : &image
->surface
.level
[range
->baseMipLevel
], range
->baseMipLevel
,
770 blk_w
, is_stencil
, iview
->descriptor
);
773 void radv_image_set_optimal_micro_tile_mode(struct radv_device
*device
,
774 struct radv_image
*image
, uint32_t micro_tile_mode
)
776 /* These magic numbers were copied from addrlib. It doesn't use any
777 * definitions for them either. They are all 2D_TILED_THIN1 modes with
778 * different bpp and micro tile mode.
780 if (device
->physical_device
->rad_info
.chip_class
>= CIK
) {
781 switch (micro_tile_mode
) {
782 case 0: /* displayable */
783 image
->surface
.tiling_index
[0] = 10;
786 image
->surface
.tiling_index
[0] = 14;
788 case 3: /* rotated */
789 image
->surface
.tiling_index
[0] = 28;
791 default: /* depth, thick */
792 assert(!"unexpected micro mode");
796 switch (micro_tile_mode
) {
797 case 0: /* displayable */
798 switch (image
->surface
.bpe
) {
800 image
->surface
.tiling_index
[0] = 10;
803 image
->surface
.tiling_index
[0] = 11;
806 image
->surface
.tiling_index
[0] = 12;
811 switch (image
->surface
.bpe
) {
813 image
->surface
.tiling_index
[0] = 14;
816 image
->surface
.tiling_index
[0] = 15;
819 image
->surface
.tiling_index
[0] = 16;
822 image
->surface
.tiling_index
[0] = 17;
826 default: /* depth, thick */
827 assert(!"unexpected micro mode");
832 image
->surface
.micro_tile_mode
= micro_tile_mode
;
835 bool radv_layout_has_htile(const struct radv_image
*image
,
836 VkImageLayout layout
)
838 return (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
839 layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
842 bool radv_layout_is_htile_compressed(const struct radv_image
*image
,
843 VkImageLayout layout
)
845 return layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
;
848 bool radv_layout_can_expclear(const struct radv_image
*image
,
849 VkImageLayout layout
)
851 return (layout
== VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
||
852 layout
== VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
);
855 bool radv_layout_can_fast_clear(const struct radv_image
*image
,
856 VkImageLayout layout
,
859 return layout
== VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
&&
860 queue_mask
== (1u << RADV_QUEUE_GENERAL
);
864 unsigned radv_image_queue_family_mask(const struct radv_image
*image
, uint32_t family
, uint32_t queue_family
)
866 if (!image
->exclusive
)
867 return image
->queue_family_mask
;
868 if (family
== VK_QUEUE_FAMILY_IGNORED
)
869 return 1u << queue_family
;
874 radv_CreateImage(VkDevice device
,
875 const VkImageCreateInfo
*pCreateInfo
,
876 const VkAllocationCallbacks
*pAllocator
,
879 return radv_image_create(device
,
880 &(struct radv_image_create_info
) {
881 .vk_info
= pCreateInfo
,
889 radv_DestroyImage(VkDevice _device
, VkImage _image
,
890 const VkAllocationCallbacks
*pAllocator
)
892 RADV_FROM_HANDLE(radv_device
, device
, _device
);
893 RADV_FROM_HANDLE(radv_image
, image
, _image
);
898 if (image
->flags
& VK_IMAGE_CREATE_SPARSE_BINDING_BIT
)
899 device
->ws
->buffer_destroy(image
->bo
);
901 vk_free2(&device
->alloc
, pAllocator
, image
);
904 void radv_GetImageSubresourceLayout(
907 const VkImageSubresource
* pSubresource
,
908 VkSubresourceLayout
* pLayout
)
910 RADV_FROM_HANDLE(radv_image
, image
, _image
);
911 int level
= pSubresource
->mipLevel
;
912 int layer
= pSubresource
->arrayLayer
;
914 pLayout
->offset
= image
->surface
.level
[level
].offset
+ image
->surface
.level
[level
].slice_size
* layer
;
915 pLayout
->rowPitch
= image
->surface
.level
[level
].pitch_bytes
;
916 pLayout
->arrayPitch
= image
->surface
.level
[level
].slice_size
;
917 pLayout
->depthPitch
= image
->surface
.level
[level
].slice_size
;
918 pLayout
->size
= image
->surface
.level
[level
].slice_size
;
919 if (image
->type
== VK_IMAGE_TYPE_3D
)
920 pLayout
->size
*= image
->surface
.level
[level
].nblk_z
;
925 radv_CreateImageView(VkDevice _device
,
926 const VkImageViewCreateInfo
*pCreateInfo
,
927 const VkAllocationCallbacks
*pAllocator
,
930 RADV_FROM_HANDLE(radv_device
, device
, _device
);
931 struct radv_image_view
*view
;
933 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
934 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
936 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
938 radv_image_view_init(view
, device
, pCreateInfo
, NULL
, ~0);
940 *pView
= radv_image_view_to_handle(view
);
946 radv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
947 const VkAllocationCallbacks
*pAllocator
)
949 RADV_FROM_HANDLE(radv_device
, device
, _device
);
950 RADV_FROM_HANDLE(radv_image_view
, iview
, _iview
);
954 vk_free2(&device
->alloc
, pAllocator
, iview
);
957 void radv_buffer_view_init(struct radv_buffer_view
*view
,
958 struct radv_device
*device
,
959 const VkBufferViewCreateInfo
* pCreateInfo
,
960 struct radv_cmd_buffer
*cmd_buffer
)
962 RADV_FROM_HANDLE(radv_buffer
, buffer
, pCreateInfo
->buffer
);
964 view
->bo
= buffer
->bo
;
965 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
966 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
967 view
->vk_format
= pCreateInfo
->format
;
969 radv_make_buffer_descriptor(device
, buffer
, view
->vk_format
,
970 pCreateInfo
->offset
, view
->range
, view
->state
);
974 radv_CreateBufferView(VkDevice _device
,
975 const VkBufferViewCreateInfo
*pCreateInfo
,
976 const VkAllocationCallbacks
*pAllocator
,
979 RADV_FROM_HANDLE(radv_device
, device
, _device
);
980 struct radv_buffer_view
*view
;
982 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
983 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
985 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
987 radv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
989 *pView
= radv_buffer_view_to_handle(view
);
995 radv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
996 const VkAllocationCallbacks
*pAllocator
)
998 RADV_FROM_HANDLE(radv_device
, device
, _device
);
999 RADV_FROM_HANDLE(radv_buffer_view
, view
, bufferView
);
1004 vk_free2(&device
->alloc
, pAllocator
, view
);