2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include <drm_fourcc.h>
32 #include "anv_private.h"
33 #include "util/debug.h"
36 #include "vk_format_info.h"
38 static isl_surf_usage_flags_t
39 choose_isl_surf_usage(VkImageCreateFlags vk_create_flags
,
40 VkImageUsageFlags vk_usage
,
41 isl_surf_usage_flags_t isl_extra_usage
,
42 VkImageAspectFlagBits aspect
)
44 isl_surf_usage_flags_t isl_usage
= isl_extra_usage
;
46 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
47 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
49 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
50 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
52 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
53 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
55 if (vk_create_flags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
56 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
58 /* Even if we're only using it for transfer operations, clears to depth and
59 * stencil images happen as depth and stencil so they need the right ISL
60 * usage bits or else things will fall apart.
63 case VK_IMAGE_ASPECT_DEPTH_BIT
:
64 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
66 case VK_IMAGE_ASPECT_STENCIL_BIT
:
67 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
69 case VK_IMAGE_ASPECT_COLOR_BIT
:
70 case VK_IMAGE_ASPECT_PLANE_0_BIT
:
71 case VK_IMAGE_ASPECT_PLANE_1_BIT
:
72 case VK_IMAGE_ASPECT_PLANE_2_BIT
:
75 unreachable("bad VkImageAspect");
78 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
79 /* blorp implements transfers by sampling from the source image. */
80 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
83 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
&&
84 aspect
== VK_IMAGE_ASPECT_COLOR_BIT
) {
85 /* blorp implements transfers by rendering into the destination image.
86 * Only request this with color images, as we deal with depth/stencil
87 * formats differently. */
88 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
94 static isl_tiling_flags_t
95 choose_isl_tiling_flags(const struct anv_image_create_info
*anv_info
,
96 const struct isl_drm_modifier_info
*isl_mod_info
,
99 const VkImageCreateInfo
*base_info
= anv_info
->vk_info
;
100 isl_tiling_flags_t flags
= 0;
102 switch (base_info
->tiling
) {
104 unreachable("bad VkImageTiling");
105 case VK_IMAGE_TILING_OPTIMAL
:
106 flags
= ISL_TILING_ANY_MASK
;
108 case VK_IMAGE_TILING_LINEAR
:
109 flags
= ISL_TILING_LINEAR_BIT
;
113 if (anv_info
->isl_tiling_flags
)
114 flags
&= anv_info
->isl_tiling_flags
;
117 flags
&= ISL_TILING_LINEAR_BIT
| ISL_TILING_X_BIT
;
120 flags
&= 1 << isl_mod_info
->tiling
;
127 static struct anv_surface
*
128 get_surface(struct anv_image
*image
, VkImageAspectFlagBits aspect
)
130 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
131 return &image
->planes
[plane
].surface
;
135 add_surface(struct anv_image
*image
, struct anv_surface
*surf
, uint32_t plane
)
137 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
139 if (image
->disjoint
) {
140 surf
->offset
= align_u32(image
->planes
[plane
].size
, surf
->isl
.alignment
);
141 /* Plane offset is always 0 when it's disjoint. */
143 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
144 /* Determine plane's offset only once when the first surface is added. */
145 if (image
->planes
[plane
].size
== 0)
146 image
->planes
[plane
].offset
= image
->size
;
149 image
->size
= surf
->offset
+ surf
->isl
.size
;
150 image
->planes
[plane
].size
= (surf
->offset
+ surf
->isl
.size
) - image
->planes
[plane
].offset
;
152 image
->alignment
= MAX2(image
->alignment
, surf
->isl
.alignment
);
153 image
->planes
[plane
].alignment
= MAX2(image
->planes
[plane
].alignment
,
154 surf
->isl
.alignment
);
159 all_formats_ccs_e_compatible(const struct gen_device_info
*devinfo
,
160 const struct VkImageCreateInfo
*vk_info
)
162 enum isl_format format
=
163 anv_get_isl_format(devinfo
, vk_info
->format
,
164 VK_IMAGE_ASPECT_COLOR_BIT
, vk_info
->tiling
);
166 if (!isl_format_supports_ccs_e(devinfo
, format
))
169 if (!(vk_info
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
))
172 const VkImageFormatListCreateInfoKHR
*fmt_list
=
173 vk_find_struct_const(vk_info
->pNext
, IMAGE_FORMAT_LIST_CREATE_INFO_KHR
);
175 if (!fmt_list
|| fmt_list
->viewFormatCount
== 0)
178 for (uint32_t i
= 0; i
< fmt_list
->viewFormatCount
; i
++) {
179 enum isl_format view_format
=
180 anv_get_isl_format(devinfo
, fmt_list
->pViewFormats
[i
],
181 VK_IMAGE_ASPECT_COLOR_BIT
, vk_info
->tiling
);
183 if (!isl_formats_are_ccs_e_compatible(devinfo
, format
, view_format
))
191 * For color images that have an auxiliary surface, request allocation for an
192 * additional buffer that mainly stores fast-clear values. Use of this buffer
193 * allows us to access the image's subresources while being aware of their
194 * fast-clear values in non-trivial cases (e.g., outside of a render pass in
195 * which a fast clear has occurred).
197 * In order to avoid having multiple clear colors for a single plane of an
198 * image (hence a single RENDER_SURFACE_STATE), we only allow fast-clears on
199 * the first slice (level 0, layer 0). At the time of our testing (Jan 17,
200 * 2018), there were no known applications which would benefit from fast-
201 * clearing more than just the first slice.
203 * The fast clear portion of the image is laid out in the following order:
205 * * 1 or 4 dwords (depending on hardware generation) for the clear color
206 * * 1 dword for the anv_fast_clear_type of the clear color
207 * * On gen9+, 1 dword per level and layer of the image (3D levels count
208 * multiple layers) in level-major order for compression state.
210 * For the purpose of discoverability, the algorithm used to manage
211 * compression and fast-clears is described here:
213 * * On a transition from UNDEFINED or PREINITIALIZED to a defined layout,
214 * all of the values in the fast clear portion of the image are initialized
217 * * On fast-clear, the clear value is written into surface state and also
218 * into the buffer and the fast clear type is set appropriately. Both
219 * setting the fast-clear value in the buffer and setting the fast-clear
220 * type happen from the GPU using MI commands.
222 * * Whenever a render or blorp operation is performed with CCS_E, we call
223 * genX(cmd_buffer_mark_image_written) to set the compression state to
224 * true (which is represented by UINT32_MAX).
226 * * On pipeline barrier transitions, the worst-case transition is computed
227 * from the image layouts. The command streamer inspects the fast clear
228 * type and compression state dwords and constructs a predicate. The
229 * worst-case resolve is performed with the given predicate and the fast
230 * clear and compression state is set accordingly.
232 * See anv_layout_to_aux_usage and anv_layout_to_fast_clear_type functions for
233 * details on exactly what is allowed in what layouts.
235 * On gen7-9, we do not have a concept of indirect clear colors in hardware.
236 * In order to deal with this, we have to do some clear color management.
238 * * For LOAD_OP_LOAD at the top of a renderpass, we have to copy the clear
239 * value from the buffer into the surface state with MI commands.
241 * * For any blorp operations, we pass the address to the clear value into
242 * blorp and it knows to copy the clear color.
245 add_aux_state_tracking_buffer(struct anv_image
*image
,
246 VkImageAspectFlagBits aspect
,
248 const struct anv_device
*device
)
250 assert(image
&& device
);
251 assert(image
->planes
[plane
].aux_surface
.isl
.size
> 0 &&
252 image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
254 /* Compressed images must be tiled and therefore everything should be 4K
255 * aligned. The CCS has the same alignment requirements. This is good
256 * because we need at least dword-alignment for MI_LOAD/STORE operations.
258 assert(image
->alignment
% 4 == 0);
259 assert((image
->planes
[plane
].offset
+ image
->planes
[plane
].size
) % 4 == 0);
261 /* This buffer should be at the very end of the plane. */
262 if (image
->disjoint
) {
263 assert(image
->planes
[plane
].size
==
264 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
266 assert(image
->size
==
267 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
270 const unsigned clear_color_state_size
= device
->info
.gen
>= 10 ?
271 device
->isl_dev
.ss
.clear_color_state_size
:
272 device
->isl_dev
.ss
.clear_value_size
;
274 /* Clear color and fast clear type */
275 unsigned state_size
= clear_color_state_size
+ 4;
277 /* We only need to track compression on CCS_E surfaces. */
278 if (image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_CCS_E
) {
279 if (image
->type
== VK_IMAGE_TYPE_3D
) {
280 for (uint32_t l
= 0; l
< image
->levels
; l
++)
281 state_size
+= anv_minify(image
->extent
.depth
, l
) * 4;
283 state_size
+= image
->levels
* image
->array_size
* 4;
287 image
->planes
[plane
].fast_clear_state_offset
=
288 image
->planes
[plane
].offset
+ image
->planes
[plane
].size
;
290 image
->planes
[plane
].size
+= state_size
;
291 image
->size
+= state_size
;
295 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
296 * image's memory requirements (that is, the image's size and alignment).
299 make_surface(const struct anv_device
*dev
,
300 struct anv_image
*image
,
301 const struct anv_image_create_info
*anv_info
,
302 isl_tiling_flags_t tiling_flags
,
303 VkImageAspectFlagBits aspect
)
305 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
308 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
309 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
310 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
311 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
314 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
317 const unsigned plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
318 const struct anv_format_plane plane_format
=
319 anv_get_format_plane(&dev
->info
, image
->vk_format
, aspect
, image
->tiling
);
320 struct anv_surface
*anv_surf
= &image
->planes
[plane
].surface
;
322 const isl_surf_usage_flags_t usage
=
323 choose_isl_surf_usage(vk_info
->flags
, image
->usage
,
324 anv_info
->isl_extra_usage_flags
, aspect
);
326 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
327 * fall back to linear on Broadwell and earlier because we aren't
328 * guaranteed that we can handle offsets correctly. On Sky Lake, the
329 * horizontal and vertical alignments are sufficiently high that we can
330 * just use RENDER_SURFACE_STATE::X/Y Offset.
332 bool needs_shadow
= false;
333 if (dev
->info
.gen
<= 8 &&
334 (vk_info
->flags
& VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT
) &&
335 vk_info
->tiling
== VK_IMAGE_TILING_OPTIMAL
) {
336 assert(isl_format_is_compressed(plane_format
.isl_format
));
337 tiling_flags
= ISL_TILING_LINEAR_BIT
;
341 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
342 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
343 .format
= plane_format
.isl_format
,
344 .width
= image
->extent
.width
/ plane_format
.denominator_scales
[0],
345 .height
= image
->extent
.height
/ plane_format
.denominator_scales
[1],
346 .depth
= image
->extent
.depth
,
347 .levels
= vk_info
->mipLevels
,
348 .array_len
= vk_info
->arrayLayers
,
349 .samples
= vk_info
->samples
,
351 .row_pitch
= anv_info
->stride
,
353 .tiling_flags
= tiling_flags
);
356 return VK_ERROR_OUT_OF_DEVICE_MEMORY
;
358 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_NONE
;
360 add_surface(image
, anv_surf
, plane
);
362 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
363 * create an identical tiled shadow surface for use while texturing so we
364 * don't get garbage performance.
367 assert(aspect
== VK_IMAGE_ASPECT_COLOR_BIT
);
368 assert(tiling_flags
== ISL_TILING_LINEAR_BIT
);
370 ok
= isl_surf_init(&dev
->isl_dev
, &image
->planes
[plane
].shadow_surface
.isl
,
371 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
372 .format
= plane_format
.isl_format
,
373 .width
= image
->extent
.width
,
374 .height
= image
->extent
.height
,
375 .depth
= image
->extent
.depth
,
376 .levels
= vk_info
->mipLevels
,
377 .array_len
= vk_info
->arrayLayers
,
378 .samples
= vk_info
->samples
,
380 .row_pitch
= anv_info
->stride
,
382 .tiling_flags
= ISL_TILING_ANY_MASK
);
384 /* isl_surf_init() will fail only if provided invalid input. Invalid input
385 * is illegal in Vulkan.
389 add_surface(image
, &image
->planes
[plane
].shadow_surface
, plane
);
392 /* Add a HiZ surface to a depth buffer that will be used for rendering.
394 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
395 /* We don't advertise that depth buffers could be used as storage
398 assert(!(image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
400 /* Allow the user to control HiZ enabling. Disable by default on gen7
401 * because resolves are not currently implemented pre-BDW.
403 if (!(image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
404 /* It will never be used as an attachment, HiZ is pointless. */
405 } else if (dev
->info
.gen
== 7) {
406 anv_perf_warn(dev
->instance
, image
, "Implement gen7 HiZ");
407 } else if (vk_info
->mipLevels
> 1) {
408 anv_perf_warn(dev
->instance
, image
, "Enable multi-LOD HiZ");
409 } else if (vk_info
->arrayLayers
> 1) {
410 anv_perf_warn(dev
->instance
, image
,
411 "Implement multi-arrayLayer HiZ clears and resolves");
412 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
413 anv_perf_warn(dev
->instance
, image
, "Enable gen8 multisampled HiZ");
414 } else if (!unlikely(INTEL_DEBUG
& DEBUG_NO_HIZ
)) {
415 assert(image
->planes
[plane
].aux_surface
.isl
.size
== 0);
416 ok
= isl_surf_get_hiz_surf(&dev
->isl_dev
,
417 &image
->planes
[plane
].surface
.isl
,
418 &image
->planes
[plane
].aux_surface
.isl
);
420 add_surface(image
, &image
->planes
[plane
].aux_surface
, plane
);
421 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_HIZ
;
423 } else if ((aspect
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) && vk_info
->samples
== 1) {
424 /* TODO: Disallow compression with :
426 * 1) non multiplanar images (We appear to hit a sampler bug with
427 * CCS & R16G16 format. Putting the clear state a page/4096bytes
428 * further fixes the issue).
430 * 2) alias images, because they might be aliases of images
433 * 3) compression disabled by debug
435 const bool allow_compression
=
436 image
->n_planes
== 1 &&
437 (vk_info
->flags
& VK_IMAGE_CREATE_ALIAS_BIT
) == 0 &&
438 likely((INTEL_DEBUG
& DEBUG_NO_RBC
) == 0);
440 if (allow_compression
) {
441 assert(image
->planes
[plane
].aux_surface
.isl
.size
== 0);
442 ok
= isl_surf_get_ccs_surf(&dev
->isl_dev
,
443 &image
->planes
[plane
].surface
.isl
,
444 &image
->planes
[plane
].aux_surface
.isl
, 0);
447 /* Disable CCS when it is not useful (i.e., when you can't render
448 * to the image with CCS enabled).
450 if (!isl_format_supports_rendering(&dev
->info
,
451 plane_format
.isl_format
)) {
452 /* While it may be technically possible to enable CCS for this
453 * image, we currently don't have things hooked up to get it
456 anv_perf_warn(dev
->instance
, image
,
457 "This image format doesn't support rendering. "
458 "Not allocating an CCS buffer.");
459 image
->planes
[plane
].aux_surface
.isl
.size
= 0;
463 add_surface(image
, &image
->planes
[plane
].aux_surface
, plane
);
464 add_aux_state_tracking_buffer(image
, aspect
, plane
, dev
);
466 /* For images created without MUTABLE_FORMAT_BIT set, we know that
467 * they will always be used with the original format. In
468 * particular, they will always be used with a format that
469 * supports color compression. If it's never used as a storage
470 * image, then it will only be used through the sampler or the as
471 * a render target. This means that it's safe to just leave
472 * compression on at all times for these formats.
474 if (!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) &&
475 all_formats_ccs_e_compatible(&dev
->info
, vk_info
)) {
476 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_CCS_E
;
480 } else if ((aspect
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) && vk_info
->samples
> 1) {
481 assert(!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
482 assert(image
->planes
[plane
].aux_surface
.isl
.size
== 0);
483 ok
= isl_surf_get_mcs_surf(&dev
->isl_dev
,
484 &image
->planes
[plane
].surface
.isl
,
485 &image
->planes
[plane
].aux_surface
.isl
);
487 add_surface(image
, &image
->planes
[plane
].aux_surface
, plane
);
488 add_aux_state_tracking_buffer(image
, aspect
, plane
, dev
);
489 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_MCS
;
493 assert((image
->planes
[plane
].offset
+ image
->planes
[plane
].size
) == image
->size
);
495 /* Upper bound of the last surface should be smaller than the plane's
498 assert((MAX2(image
->planes
[plane
].surface
.offset
,
499 image
->planes
[plane
].aux_surface
.offset
) +
500 (image
->planes
[plane
].aux_surface
.isl
.size
> 0 ?
501 image
->planes
[plane
].aux_surface
.isl
.size
:
502 image
->planes
[plane
].surface
.isl
.size
)) <=
503 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
505 if (image
->planes
[plane
].aux_surface
.isl
.size
) {
506 /* assert(image->planes[plane].fast_clear_state_offset == */
507 /* (image->planes[plane].aux_surface.offset + image->planes[plane].aux_surface.isl.size)); */
508 assert(image
->planes
[plane
].fast_clear_state_offset
<
509 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
516 score_drm_format_mod(uint64_t modifier
)
519 case DRM_FORMAT_MOD_LINEAR
: return 1;
520 case I915_FORMAT_MOD_X_TILED
: return 2;
521 case I915_FORMAT_MOD_Y_TILED
: return 3;
522 case I915_FORMAT_MOD_Y_TILED_CCS
: return 4;
523 default: unreachable("bad DRM format modifier");
527 static const struct isl_drm_modifier_info
*
528 choose_drm_format_mod(const struct anv_physical_device
*device
,
529 uint32_t modifier_count
, const uint64_t *modifiers
)
531 uint64_t best_mod
= UINT64_MAX
;
532 uint32_t best_score
= 0;
534 for (uint32_t i
= 0; i
< modifier_count
; ++i
) {
535 uint32_t score
= score_drm_format_mod(modifiers
[i
]);
536 if (score
> best_score
) {
537 best_mod
= modifiers
[i
];
543 return isl_drm_modifier_get_info(best_mod
);
549 anv_image_create(VkDevice _device
,
550 const struct anv_image_create_info
*create_info
,
551 const VkAllocationCallbacks
* alloc
,
554 ANV_FROM_HANDLE(anv_device
, device
, _device
);
555 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
556 const struct isl_drm_modifier_info
*isl_mod_info
= NULL
;
557 struct anv_image
*image
= NULL
;
560 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
562 const struct wsi_image_create_info
*wsi_info
=
563 vk_find_struct_const(pCreateInfo
->pNext
, WSI_IMAGE_CREATE_INFO_MESA
);
564 if (wsi_info
&& wsi_info
->modifier_count
> 0) {
565 isl_mod_info
= choose_drm_format_mod(&device
->instance
->physicalDevice
,
566 wsi_info
->modifier_count
,
567 wsi_info
->modifiers
);
568 assert(isl_mod_info
);
571 anv_assert(pCreateInfo
->mipLevels
> 0);
572 anv_assert(pCreateInfo
->arrayLayers
> 0);
573 anv_assert(pCreateInfo
->samples
> 0);
574 anv_assert(pCreateInfo
->extent
.width
> 0);
575 anv_assert(pCreateInfo
->extent
.height
> 0);
576 anv_assert(pCreateInfo
->extent
.depth
> 0);
578 image
= vk_zalloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
579 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
583 image
->type
= pCreateInfo
->imageType
;
584 image
->extent
= pCreateInfo
->extent
;
585 image
->vk_format
= pCreateInfo
->format
;
586 image
->format
= anv_get_format(pCreateInfo
->format
);
587 image
->aspects
= vk_format_aspects(image
->vk_format
);
588 image
->levels
= pCreateInfo
->mipLevels
;
589 image
->array_size
= pCreateInfo
->arrayLayers
;
590 image
->samples
= pCreateInfo
->samples
;
591 image
->usage
= pCreateInfo
->usage
;
592 image
->tiling
= pCreateInfo
->tiling
;
593 image
->disjoint
= pCreateInfo
->flags
& VK_IMAGE_CREATE_DISJOINT_BIT
;
594 image
->needs_set_tiling
= wsi_info
&& wsi_info
->scanout
;
595 image
->drm_format_mod
= isl_mod_info
? isl_mod_info
->modifier
:
596 DRM_FORMAT_MOD_INVALID
;
598 const struct anv_format
*format
= anv_get_format(image
->vk_format
);
599 assert(format
!= NULL
);
601 const isl_tiling_flags_t isl_tiling_flags
=
602 choose_isl_tiling_flags(create_info
, isl_mod_info
,
603 image
->needs_set_tiling
);
605 image
->n_planes
= format
->n_planes
;
608 for_each_bit(b
, image
->aspects
) {
609 r
= make_surface(device
, image
, create_info
, isl_tiling_flags
,
615 *pImage
= anv_image_to_handle(image
);
621 vk_free2(&device
->alloc
, alloc
, image
);
627 anv_CreateImage(VkDevice device
,
628 const VkImageCreateInfo
*pCreateInfo
,
629 const VkAllocationCallbacks
*pAllocator
,
633 const VkNativeBufferANDROID
*gralloc_info
=
634 vk_find_struct_const(pCreateInfo
->pNext
, NATIVE_BUFFER_ANDROID
);
637 return anv_image_from_gralloc(device
, pCreateInfo
, gralloc_info
,
641 return anv_image_create(device
,
642 &(struct anv_image_create_info
) {
643 .vk_info
= pCreateInfo
,
650 anv_DestroyImage(VkDevice _device
, VkImage _image
,
651 const VkAllocationCallbacks
*pAllocator
)
653 ANV_FROM_HANDLE(anv_device
, device
, _device
);
654 ANV_FROM_HANDLE(anv_image
, image
, _image
);
659 for (uint32_t p
= 0; p
< image
->n_planes
; ++p
) {
660 if (image
->planes
[p
].bo_is_owned
) {
661 assert(image
->planes
[p
].address
.bo
!= NULL
);
662 anv_bo_cache_release(device
, &device
->bo_cache
,
663 image
->planes
[p
].address
.bo
);
667 vk_free2(&device
->alloc
, pAllocator
, image
);
670 static void anv_image_bind_memory_plane(struct anv_device
*device
,
671 struct anv_image
*image
,
673 struct anv_device_memory
*memory
,
674 uint32_t memory_offset
)
676 assert(!image
->planes
[plane
].bo_is_owned
);
679 image
->planes
[plane
].address
= ANV_NULL_ADDRESS
;
683 image
->planes
[plane
].address
= (struct anv_address
) {
685 .offset
= memory_offset
,
689 VkResult
anv_BindImageMemory(
692 VkDeviceMemory _memory
,
693 VkDeviceSize memoryOffset
)
695 ANV_FROM_HANDLE(anv_device
, device
, _device
);
696 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
697 ANV_FROM_HANDLE(anv_image
, image
, _image
);
700 anv_foreach_image_aspect_bit(aspect_bit
, image
, image
->aspects
) {
702 anv_image_aspect_to_plane(image
->aspects
, 1UL << aspect_bit
);
703 anv_image_bind_memory_plane(device
, image
, plane
, mem
, memoryOffset
);
709 VkResult
anv_BindImageMemory2(
711 uint32_t bindInfoCount
,
712 const VkBindImageMemoryInfo
* pBindInfos
)
714 ANV_FROM_HANDLE(anv_device
, device
, _device
);
716 for (uint32_t i
= 0; i
< bindInfoCount
; i
++) {
717 const VkBindImageMemoryInfo
*bind_info
= &pBindInfos
[i
];
718 ANV_FROM_HANDLE(anv_device_memory
, mem
, bind_info
->memory
);
719 ANV_FROM_HANDLE(anv_image
, image
, bind_info
->image
);
720 VkImageAspectFlags aspects
= image
->aspects
;
722 vk_foreach_struct_const(s
, bind_info
->pNext
) {
724 case VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO
: {
725 const VkBindImagePlaneMemoryInfo
*plane_info
=
726 (const VkBindImagePlaneMemoryInfo
*) s
;
728 aspects
= plane_info
->planeAspect
;
732 anv_debug_ignored_stype(s
->sType
);
738 anv_foreach_image_aspect_bit(aspect_bit
, image
, aspects
) {
740 anv_image_aspect_to_plane(image
->aspects
, 1UL << aspect_bit
);
741 anv_image_bind_memory_plane(device
, image
, plane
,
742 mem
, bind_info
->memoryOffset
);
749 void anv_GetImageSubresourceLayout(
752 const VkImageSubresource
* subresource
,
753 VkSubresourceLayout
* layout
)
755 ANV_FROM_HANDLE(anv_image
, image
, _image
);
757 const struct anv_surface
*surface
;
758 if (subresource
->aspectMask
== VK_IMAGE_ASPECT_PLANE_1_BIT_KHR
&&
759 image
->drm_format_mod
!= DRM_FORMAT_MOD_INVALID
&&
760 isl_drm_modifier_has_aux(image
->drm_format_mod
))
761 surface
= &image
->planes
[0].aux_surface
;
763 surface
= get_surface(image
, subresource
->aspectMask
);
765 assert(__builtin_popcount(subresource
->aspectMask
) == 1);
767 layout
->offset
= surface
->offset
;
768 layout
->rowPitch
= surface
->isl
.row_pitch
;
769 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
770 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
772 if (subresource
->mipLevel
> 0 || subresource
->arrayLayer
> 0) {
773 assert(surface
->isl
.tiling
== ISL_TILING_LINEAR
);
776 isl_surf_get_image_offset_B_tile_sa(&surface
->isl
,
777 subresource
->mipLevel
,
778 subresource
->arrayLayer
,
779 0 /* logical_z_offset_px */,
780 &offset_B
, NULL
, NULL
);
781 layout
->offset
+= offset_B
;
782 layout
->size
= layout
->rowPitch
* anv_minify(image
->extent
.height
,
783 subresource
->mipLevel
);
785 layout
->size
= surface
->isl
.size
;
790 * This function determines the optimal buffer to use for a given
791 * VkImageLayout and other pieces of information needed to make that
792 * determination. This does not determine the optimal buffer to use
793 * during a resolve operation.
795 * @param devinfo The device information of the Intel GPU.
796 * @param image The image that may contain a collection of buffers.
797 * @param aspect The aspect of the image to be accessed.
798 * @param layout The current layout of the image aspect(s).
800 * @return The primary buffer that should be used for the given layout.
803 anv_layout_to_aux_usage(const struct gen_device_info
* const devinfo
,
804 const struct anv_image
* const image
,
805 const VkImageAspectFlagBits aspect
,
806 const VkImageLayout layout
)
808 /* Validate the inputs. */
810 /* The devinfo is needed as the optimal buffer varies across generations. */
811 assert(devinfo
!= NULL
);
813 /* The layout of a NULL image is not properly defined. */
814 assert(image
!= NULL
);
816 /* The aspect must be exactly one of the image aspects. */
817 assert(_mesa_bitcount(aspect
) == 1 && (aspect
& image
->aspects
));
819 /* Determine the optimal buffer. */
821 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
823 /* If there is no auxiliary surface allocated, we must use the one and only
826 if (image
->planes
[plane
].aux_surface
.isl
.size
== 0)
827 return ISL_AUX_USAGE_NONE
;
829 /* All images that use an auxiliary surface are required to be tiled. */
830 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
832 /* Stencil has no aux */
833 assert(aspect
!= VK_IMAGE_ASPECT_STENCIL_BIT
);
837 /* Invalid Layouts */
838 case VK_IMAGE_LAYOUT_RANGE_SIZE
:
839 case VK_IMAGE_LAYOUT_MAX_ENUM
:
840 unreachable("Invalid image layout.");
844 * The pre-initialized layout is equivalent to the undefined layout for
845 * optimally-tiled images. We can only do color compression (CCS or HiZ)
848 case VK_IMAGE_LAYOUT_UNDEFINED
:
849 case VK_IMAGE_LAYOUT_PREINITIALIZED
:
850 return ISL_AUX_USAGE_NONE
;
855 case VK_IMAGE_LAYOUT_GENERAL
:
856 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
:
857 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
:
858 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
859 /* This buffer could be a depth buffer used in a transfer operation.
860 * BLORP currently doesn't use HiZ for transfer operations so we must
861 * use the main buffer for this layout. TODO: Enable HiZ in BLORP.
863 assert(image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_HIZ
);
864 return ISL_AUX_USAGE_NONE
;
866 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
867 return image
->planes
[plane
].aux_usage
;
871 /* Sampling Layouts */
872 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
:
873 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL
:
874 assert((image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) == 0);
876 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
:
877 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
878 if (anv_can_sample_with_hiz(devinfo
, image
))
879 return ISL_AUX_USAGE_HIZ
;
881 return ISL_AUX_USAGE_NONE
;
883 return image
->planes
[plane
].aux_usage
;
887 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
: {
888 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
890 /* When handing the image off to the presentation engine, we need to
891 * ensure that things are properly resolved. For images with no
892 * modifier, we assume that they follow the old rules and always need
893 * a full resolve because the PE doesn't understand any form of
894 * compression. For images with modifiers, we use the aux usage from
897 const struct isl_drm_modifier_info
*mod_info
=
898 isl_drm_modifier_get_info(image
->drm_format_mod
);
899 return mod_info
? mod_info
->aux_usage
: ISL_AUX_USAGE_NONE
;
903 /* Rendering Layouts */
904 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
:
905 assert(aspect
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
906 if (image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_NONE
) {
907 assert(image
->samples
== 1);
908 return ISL_AUX_USAGE_CCS_D
;
910 assert(image
->planes
[plane
].aux_usage
!= ISL_AUX_USAGE_CCS_D
);
911 return image
->planes
[plane
].aux_usage
;
914 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
:
915 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL
:
916 assert(aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
);
917 return ISL_AUX_USAGE_HIZ
;
919 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR
:
920 unreachable("VK_KHR_shared_presentable_image is unsupported");
923 /* If the layout isn't recognized in the exhaustive switch above, the
924 * VkImageLayout value is not defined in vulkan.h.
926 unreachable("layout is not a VkImageLayout enumeration member.");
930 * This function returns the level of unresolved fast-clear support of the
931 * given image in the given VkImageLayout.
933 * @param devinfo The device information of the Intel GPU.
934 * @param image The image that may contain a collection of buffers.
935 * @param aspect The aspect of the image to be accessed.
936 * @param layout The current layout of the image aspect(s).
938 enum anv_fast_clear_type
939 anv_layout_to_fast_clear_type(const struct gen_device_info
* const devinfo
,
940 const struct anv_image
* const image
,
941 const VkImageAspectFlagBits aspect
,
942 const VkImageLayout layout
)
944 /* The aspect must be exactly one of the image aspects. */
945 assert(_mesa_bitcount(aspect
) == 1 && (aspect
& image
->aspects
));
947 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
949 /* If there is no auxiliary surface allocated, there are no fast-clears */
950 if (image
->planes
[plane
].aux_surface
.isl
.size
== 0)
951 return ANV_FAST_CLEAR_NONE
;
953 /* All images that use an auxiliary surface are required to be tiled. */
954 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
956 /* Stencil has no aux */
957 assert(aspect
!= VK_IMAGE_ASPECT_STENCIL_BIT
);
959 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
960 /* For depth images (with HiZ), the layout supports fast-clears if and
961 * only if it supports HiZ. However, we only support fast-clears to the
962 * default depth value.
964 enum isl_aux_usage aux_usage
=
965 anv_layout_to_aux_usage(devinfo
, image
, aspect
, layout
);
966 return aux_usage
== ISL_AUX_USAGE_HIZ
?
967 ANV_FAST_CLEAR_DEFAULT_VALUE
: ANV_FAST_CLEAR_NONE
;
970 assert(image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
);
972 /* We don't support MSAA fast-clears on Ivybridge or Bay Trail because they
973 * lack the MI ALU which we need to determine the predicates.
975 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&& image
->samples
> 1)
976 return ANV_FAST_CLEAR_NONE
;
979 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
:
980 return ANV_FAST_CLEAR_ANY
;
982 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
: {
983 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
985 /* We do not yet support any modifiers which support clear color so we
986 * just always return NONE. One day, this will change.
988 const struct isl_drm_modifier_info
*mod_info
=
989 isl_drm_modifier_get_info(image
->drm_format_mod
);
990 assert(!mod_info
|| !mod_info
->supports_clear_color
);
992 return ANV_FAST_CLEAR_NONE
;
996 /* If the image has MCS or CCS_E enabled all the time then we can use
997 * fast-clear as long as the clear color is the default value of zero
998 * since this is the default value we program into every surface state
999 * used for texturing.
1001 if (image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_MCS
||
1002 image
->planes
[plane
].aux_usage
== ISL_AUX_USAGE_CCS_E
)
1003 return ANV_FAST_CLEAR_DEFAULT_VALUE
;
1005 return ANV_FAST_CLEAR_NONE
;
1010 static struct anv_state
1011 alloc_surface_state(struct anv_device
*device
)
1013 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
1016 static enum isl_channel_select
1017 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
1018 struct isl_swizzle format_swizzle
)
1020 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
1021 swizzle
= component
;
1024 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
1025 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
1026 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
1027 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
1028 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
1029 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
1031 unreachable("Invalid swizzle");
1036 anv_image_fill_surface_state(struct anv_device
*device
,
1037 const struct anv_image
*image
,
1038 VkImageAspectFlagBits aspect
,
1039 const struct isl_view
*view_in
,
1040 isl_surf_usage_flags_t view_usage
,
1041 enum isl_aux_usage aux_usage
,
1042 const union isl_color_value
*clear_color
,
1043 enum anv_image_view_state_flags flags
,
1044 struct anv_surface_state
*state_inout
,
1045 struct brw_image_param
*image_param_out
)
1047 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
1049 const struct anv_surface
*surface
= &image
->planes
[plane
].surface
,
1050 *aux_surface
= &image
->planes
[plane
].aux_surface
;
1052 struct isl_view view
= *view_in
;
1053 view
.usage
|= view_usage
;
1055 /* For texturing with VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL from a
1056 * compressed surface with a shadow surface, we use the shadow instead of
1057 * the primary surface. The shadow surface will be tiled, unlike the main
1058 * surface, so it should get significantly better performance.
1060 if (image
->planes
[plane
].shadow_surface
.isl
.size
> 0 &&
1061 isl_format_is_compressed(view
.format
) &&
1062 (flags
& ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL
)) {
1063 assert(isl_format_is_compressed(surface
->isl
.format
));
1064 assert(surface
->isl
.tiling
== ISL_TILING_LINEAR
);
1065 assert(image
->planes
[plane
].shadow_surface
.isl
.tiling
!= ISL_TILING_LINEAR
);
1066 surface
= &image
->planes
[plane
].shadow_surface
;
1069 if (view_usage
== ISL_SURF_USAGE_RENDER_TARGET_BIT
)
1070 view
.swizzle
= anv_swizzle_for_render(view
.swizzle
);
1072 /* If this is a HiZ buffer we can sample from with a programmable clear
1073 * value (SKL+), define the clear value to the optimal constant.
1075 union isl_color_value default_clear_color
= { .u32
= { 0, } };
1076 if (device
->info
.gen
>= 9 && aux_usage
== ISL_AUX_USAGE_HIZ
)
1077 default_clear_color
.f32
[0] = ANV_HZ_FC_VAL
;
1079 clear_color
= &default_clear_color
;
1081 const struct anv_address address
=
1082 anv_address_add(image
->planes
[plane
].address
, surface
->offset
);
1084 if (view_usage
== ISL_SURF_USAGE_STORAGE_BIT
&&
1085 !(flags
& ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
) &&
1086 !isl_has_matching_typed_storage_image_format(&device
->info
,
1088 /* In this case, we are a writeable storage buffer which needs to be
1089 * lowered to linear. All tiling and offset calculations will be done in
1092 assert(aux_usage
== ISL_AUX_USAGE_NONE
);
1093 isl_buffer_fill_state(&device
->isl_dev
, state_inout
->state
.map
,
1094 .address
= anv_address_physical(address
),
1095 .size
= surface
->isl
.size
,
1096 .format
= ISL_FORMAT_RAW
,
1098 .mocs
= device
->default_mocs
);
1099 state_inout
->address
= address
,
1100 state_inout
->aux_address
= ANV_NULL_ADDRESS
;
1101 state_inout
->clear_address
= ANV_NULL_ADDRESS
;
1103 if (view_usage
== ISL_SURF_USAGE_STORAGE_BIT
&&
1104 !(flags
& ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
)) {
1105 /* Typed surface reads support a very limited subset of the shader
1106 * image formats. Translate it into the closest format the hardware
1109 assert(aux_usage
== ISL_AUX_USAGE_NONE
);
1110 view
.format
= isl_lower_storage_image_format(&device
->info
,
1114 const struct isl_surf
*isl_surf
= &surface
->isl
;
1116 struct isl_surf tmp_surf
;
1117 uint32_t offset_B
= 0, tile_x_sa
= 0, tile_y_sa
= 0;
1118 if (isl_format_is_compressed(surface
->isl
.format
) &&
1119 !isl_format_is_compressed(view
.format
)) {
1120 /* We're creating an uncompressed view of a compressed surface. This
1121 * is allowed but only for a single level/layer.
1123 assert(surface
->isl
.samples
== 1);
1124 assert(view
.levels
== 1);
1125 assert(view
.array_len
== 1);
1127 isl_surf_get_image_surf(&device
->isl_dev
, isl_surf
,
1129 surface
->isl
.dim
== ISL_SURF_DIM_3D
?
1130 0 : view
.base_array_layer
,
1131 surface
->isl
.dim
== ISL_SURF_DIM_3D
?
1132 view
.base_array_layer
: 0,
1134 &offset_B
, &tile_x_sa
, &tile_y_sa
);
1136 /* The newly created image represents the one subimage we're
1137 * referencing with this view so it only has one array slice and
1140 view
.base_array_layer
= 0;
1141 view
.base_level
= 0;
1143 /* We're making an uncompressed view here. The image dimensions need
1144 * to be scaled down by the block size.
1146 const struct isl_format_layout
*fmtl
=
1147 isl_format_get_layout(surface
->isl
.format
);
1148 tmp_surf
.format
= view
.format
;
1149 tmp_surf
.logical_level0_px
.width
=
1150 DIV_ROUND_UP(tmp_surf
.logical_level0_px
.width
, fmtl
->bw
);
1151 tmp_surf
.logical_level0_px
.height
=
1152 DIV_ROUND_UP(tmp_surf
.logical_level0_px
.height
, fmtl
->bh
);
1153 tmp_surf
.phys_level0_sa
.width
/= fmtl
->bw
;
1154 tmp_surf
.phys_level0_sa
.height
/= fmtl
->bh
;
1155 tile_x_sa
/= fmtl
->bw
;
1156 tile_y_sa
/= fmtl
->bh
;
1158 isl_surf
= &tmp_surf
;
1160 if (device
->info
.gen
<= 8) {
1161 assert(surface
->isl
.tiling
== ISL_TILING_LINEAR
);
1162 assert(tile_x_sa
== 0);
1163 assert(tile_y_sa
== 0);
1167 state_inout
->address
= anv_address_add(address
, offset_B
);
1169 struct anv_address aux_address
= ANV_NULL_ADDRESS
;
1170 if (aux_usage
!= ISL_AUX_USAGE_NONE
) {
1171 aux_address
= anv_address_add(image
->planes
[plane
].address
,
1172 aux_surface
->offset
);
1174 state_inout
->aux_address
= aux_address
;
1176 struct anv_address clear_address
= ANV_NULL_ADDRESS
;
1177 if (device
->info
.gen
>= 10 && aux_usage
!= ISL_AUX_USAGE_NONE
) {
1178 if (aux_usage
== ISL_AUX_USAGE_HIZ
) {
1179 clear_address
= (struct anv_address
) {
1180 .bo
= &device
->hiz_clear_bo
,
1184 clear_address
= anv_image_get_clear_color_addr(device
, image
, aspect
);
1187 state_inout
->clear_address
= clear_address
;
1189 isl_surf_fill_state(&device
->isl_dev
, state_inout
->state
.map
,
1192 .address
= anv_address_physical(state_inout
->address
),
1193 .clear_color
= *clear_color
,
1194 .aux_surf
= &aux_surface
->isl
,
1195 .aux_usage
= aux_usage
,
1196 .aux_address
= anv_address_physical(aux_address
),
1197 .clear_address
= anv_address_physical(clear_address
),
1198 .use_clear_address
= !anv_address_is_null(clear_address
),
1199 .mocs
= device
->default_mocs
,
1200 .x_offset_sa
= tile_x_sa
,
1201 .y_offset_sa
= tile_y_sa
);
1203 /* With the exception of gen8, the bottom 12 bits of the MCS base address
1204 * are used to store other information. This should be ok, however,
1205 * because the surface buffer addresses are always 4K page aligned.
1207 uint32_t *aux_addr_dw
= state_inout
->state
.map
+
1208 device
->isl_dev
.ss
.aux_addr_offset
;
1209 assert((aux_address
.offset
& 0xfff) == 0);
1210 state_inout
->aux_address
.offset
|= *aux_addr_dw
& 0xfff;
1212 if (device
->info
.gen
>= 10 && clear_address
.bo
) {
1213 uint32_t *clear_addr_dw
= state_inout
->state
.map
+
1214 device
->isl_dev
.ss
.clear_color_state_offset
;
1215 assert((clear_address
.offset
& 0x3f) == 0);
1216 state_inout
->clear_address
.offset
|= *clear_addr_dw
& 0x3f;
1220 anv_state_flush(device
, state_inout
->state
);
1222 if (image_param_out
) {
1223 assert(view_usage
== ISL_SURF_USAGE_STORAGE_BIT
);
1224 isl_surf_fill_image_param(&device
->isl_dev
, image_param_out
,
1225 &surface
->isl
, &view
);
1229 static VkImageAspectFlags
1230 remap_aspect_flags(VkImageAspectFlags view_aspects
)
1232 if (view_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) {
1233 if (_mesa_bitcount(view_aspects
) == 1)
1234 return VK_IMAGE_ASPECT_COLOR_BIT
;
1236 VkImageAspectFlags color_aspects
= 0;
1237 for (uint32_t i
= 0; i
< _mesa_bitcount(view_aspects
); i
++)
1238 color_aspects
|= VK_IMAGE_ASPECT_PLANE_0_BIT
<< i
;
1239 return color_aspects
;
1241 /* No special remapping needed for depth & stencil aspects. */
1242 return view_aspects
;
1246 anv_CreateImageView(VkDevice _device
,
1247 const VkImageViewCreateInfo
*pCreateInfo
,
1248 const VkAllocationCallbacks
*pAllocator
,
1251 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1252 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
1253 struct anv_image_view
*iview
;
1255 iview
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*iview
), 8,
1256 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1258 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1260 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
1262 assert(range
->layerCount
> 0);
1263 assert(range
->baseMipLevel
< image
->levels
);
1265 const VkImageViewUsageCreateInfo
*usage_info
=
1266 vk_find_struct_const(pCreateInfo
, IMAGE_VIEW_USAGE_CREATE_INFO
);
1267 VkImageUsageFlags view_usage
= usage_info
? usage_info
->usage
: image
->usage
;
1268 /* View usage should be a subset of image usage */
1269 assert((view_usage
& ~image
->usage
) == 0);
1270 assert(view_usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
1271 VK_IMAGE_USAGE_STORAGE_BIT
|
1272 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
1273 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
|
1274 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
1276 switch (image
->type
) {
1278 unreachable("bad VkImageType");
1279 case VK_IMAGE_TYPE_1D
:
1280 case VK_IMAGE_TYPE_2D
:
1281 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
1283 case VK_IMAGE_TYPE_3D
:
1284 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
1285 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
1289 /* First expand aspects to the image's ones (for example
1290 * VK_IMAGE_ASPECT_COLOR_BIT will be converted to
1291 * VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT |
1292 * VK_IMAGE_ASPECT_PLANE_2_BIT for an image of format
1293 * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR.
1295 VkImageAspectFlags expanded_aspects
=
1296 anv_image_expand_aspects(image
, range
->aspectMask
);
1298 iview
->image
= image
;
1300 /* Remap the expanded aspects for the image view. For example if only
1301 * VK_IMAGE_ASPECT_PLANE_1_BIT was given in range->aspectMask, we will
1302 * convert it to VK_IMAGE_ASPECT_COLOR_BIT since from the point of view of
1303 * the image view, it only has a single plane.
1305 iview
->aspect_mask
= remap_aspect_flags(expanded_aspects
);
1306 iview
->n_planes
= anv_image_aspect_get_planes(iview
->aspect_mask
);
1307 iview
->vk_format
= pCreateInfo
->format
;
1309 iview
->extent
= (VkExtent3D
) {
1310 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
1311 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
1312 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
1315 /* Now go through the underlying image selected planes (computed in
1316 * expanded_aspects) and map them to planes in the image view.
1318 uint32_t iaspect_bit
, vplane
= 0;
1319 anv_foreach_image_aspect_bit(iaspect_bit
, image
, expanded_aspects
) {
1321 anv_image_aspect_to_plane(expanded_aspects
, 1UL << iaspect_bit
);
1322 VkImageAspectFlags vplane_aspect
=
1323 anv_plane_to_aspect(iview
->aspect_mask
, vplane
);
1324 struct anv_format_plane format
=
1325 anv_get_format_plane(&device
->info
, pCreateInfo
->format
,
1326 vplane_aspect
, image
->tiling
);
1328 iview
->planes
[vplane
].image_plane
= iplane
;
1330 iview
->planes
[vplane
].isl
= (struct isl_view
) {
1331 .format
= format
.isl_format
,
1332 .base_level
= range
->baseMipLevel
,
1333 .levels
= anv_get_levelCount(image
, range
),
1334 .base_array_layer
= range
->baseArrayLayer
,
1335 .array_len
= anv_get_layerCount(image
, range
),
1337 .r
= remap_swizzle(pCreateInfo
->components
.r
,
1338 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
1339 .g
= remap_swizzle(pCreateInfo
->components
.g
,
1340 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
1341 .b
= remap_swizzle(pCreateInfo
->components
.b
,
1342 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
1343 .a
= remap_swizzle(pCreateInfo
->components
.a
,
1344 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
1348 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_3D
) {
1349 iview
->planes
[vplane
].isl
.base_array_layer
= 0;
1350 iview
->planes
[vplane
].isl
.array_len
= iview
->extent
.depth
;
1353 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
1354 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
1355 iview
->planes
[vplane
].isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
1357 iview
->planes
[vplane
].isl
.usage
= 0;
1360 if (view_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
||
1361 (view_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
&&
1362 !(iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
))) {
1363 iview
->planes
[vplane
].optimal_sampler_surface_state
.state
= alloc_surface_state(device
);
1364 iview
->planes
[vplane
].general_sampler_surface_state
.state
= alloc_surface_state(device
);
1366 enum isl_aux_usage general_aux_usage
=
1367 anv_layout_to_aux_usage(&device
->info
, image
, 1UL << iaspect_bit
,
1368 VK_IMAGE_LAYOUT_GENERAL
);
1369 enum isl_aux_usage optimal_aux_usage
=
1370 anv_layout_to_aux_usage(&device
->info
, image
, 1UL << iaspect_bit
,
1371 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
1373 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1374 &iview
->planes
[vplane
].isl
,
1375 ISL_SURF_USAGE_TEXTURE_BIT
,
1376 optimal_aux_usage
, NULL
,
1377 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL
,
1378 &iview
->planes
[vplane
].optimal_sampler_surface_state
,
1381 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1382 &iview
->planes
[vplane
].isl
,
1383 ISL_SURF_USAGE_TEXTURE_BIT
,
1384 general_aux_usage
, NULL
,
1386 &iview
->planes
[vplane
].general_sampler_surface_state
,
1390 /* NOTE: This one needs to go last since it may stomp isl_view.format */
1391 if (view_usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
1392 iview
->planes
[vplane
].storage_surface_state
.state
= alloc_surface_state(device
);
1393 iview
->planes
[vplane
].writeonly_storage_surface_state
.state
= alloc_surface_state(device
);
1395 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1396 &iview
->planes
[vplane
].isl
,
1397 ISL_SURF_USAGE_STORAGE_BIT
,
1398 ISL_AUX_USAGE_NONE
, NULL
,
1400 &iview
->planes
[vplane
].storage_surface_state
,
1401 &iview
->planes
[vplane
].storage_image_param
);
1403 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1404 &iview
->planes
[vplane
].isl
,
1405 ISL_SURF_USAGE_STORAGE_BIT
,
1406 ISL_AUX_USAGE_NONE
, NULL
,
1407 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
,
1408 &iview
->planes
[vplane
].writeonly_storage_surface_state
,
1415 *pView
= anv_image_view_to_handle(iview
);
1421 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
1422 const VkAllocationCallbacks
*pAllocator
)
1424 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1425 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
1430 for (uint32_t plane
= 0; plane
< iview
->n_planes
; plane
++) {
1431 if (iview
->planes
[plane
].optimal_sampler_surface_state
.state
.alloc_size
> 0) {
1432 anv_state_pool_free(&device
->surface_state_pool
,
1433 iview
->planes
[plane
].optimal_sampler_surface_state
.state
);
1436 if (iview
->planes
[plane
].general_sampler_surface_state
.state
.alloc_size
> 0) {
1437 anv_state_pool_free(&device
->surface_state_pool
,
1438 iview
->planes
[plane
].general_sampler_surface_state
.state
);
1441 if (iview
->planes
[plane
].storage_surface_state
.state
.alloc_size
> 0) {
1442 anv_state_pool_free(&device
->surface_state_pool
,
1443 iview
->planes
[plane
].storage_surface_state
.state
);
1446 if (iview
->planes
[plane
].writeonly_storage_surface_state
.state
.alloc_size
> 0) {
1447 anv_state_pool_free(&device
->surface_state_pool
,
1448 iview
->planes
[plane
].writeonly_storage_surface_state
.state
);
1452 vk_free2(&device
->alloc
, pAllocator
, iview
);
1457 anv_CreateBufferView(VkDevice _device
,
1458 const VkBufferViewCreateInfo
*pCreateInfo
,
1459 const VkAllocationCallbacks
*pAllocator
,
1460 VkBufferView
*pView
)
1462 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1463 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
1464 struct anv_buffer_view
*view
;
1466 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1467 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1469 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1471 /* TODO: Handle the format swizzle? */
1473 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
1474 VK_IMAGE_ASPECT_COLOR_BIT
,
1475 VK_IMAGE_TILING_LINEAR
);
1476 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
1477 view
->range
= anv_buffer_get_range(buffer
, pCreateInfo
->offset
,
1478 pCreateInfo
->range
);
1479 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
1481 view
->address
= anv_address_add(buffer
->address
, pCreateInfo
->offset
);
1483 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
1484 view
->surface_state
= alloc_surface_state(device
);
1486 anv_fill_buffer_surface_state(device
, view
->surface_state
,
1488 view
->address
, view
->range
, format_bs
);
1490 view
->surface_state
= (struct anv_state
){ 0 };
1493 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
1494 view
->storage_surface_state
= alloc_surface_state(device
);
1495 view
->writeonly_storage_surface_state
= alloc_surface_state(device
);
1497 enum isl_format storage_format
=
1498 isl_has_matching_typed_storage_image_format(&device
->info
,
1500 isl_lower_storage_image_format(&device
->info
, view
->format
) :
1503 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
1505 view
->address
, view
->range
,
1506 (storage_format
== ISL_FORMAT_RAW
? 1 :
1507 isl_format_get_layout(storage_format
)->bpb
/ 8));
1509 /* Write-only accesses should use the original format. */
1510 anv_fill_buffer_surface_state(device
, view
->writeonly_storage_surface_state
,
1512 view
->address
, view
->range
,
1513 isl_format_get_layout(view
->format
)->bpb
/ 8);
1515 isl_buffer_fill_image_param(&device
->isl_dev
,
1516 &view
->storage_image_param
,
1517 view
->format
, view
->range
);
1519 view
->storage_surface_state
= (struct anv_state
){ 0 };
1520 view
->writeonly_storage_surface_state
= (struct anv_state
){ 0 };
1523 *pView
= anv_buffer_view_to_handle(view
);
1529 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
1530 const VkAllocationCallbacks
*pAllocator
)
1532 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1533 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
1538 if (view
->surface_state
.alloc_size
> 0)
1539 anv_state_pool_free(&device
->surface_state_pool
,
1540 view
->surface_state
);
1542 if (view
->storage_surface_state
.alloc_size
> 0)
1543 anv_state_pool_free(&device
->surface_state_pool
,
1544 view
->storage_surface_state
);
1546 if (view
->writeonly_storage_surface_state
.alloc_size
> 0)
1547 anv_state_pool_free(&device
->surface_state_pool
,
1548 view
->writeonly_storage_surface_state
);
1550 vk_free2(&device
->alloc
, pAllocator
, view
);
1553 const struct anv_surface
*
1554 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
1555 VkImageAspectFlags aspect_mask
)
1557 VkImageAspectFlags sanitized_mask
;
1559 switch (aspect_mask
) {
1560 case VK_IMAGE_ASPECT_COLOR_BIT
:
1561 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1562 sanitized_mask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1564 case VK_IMAGE_ASPECT_DEPTH_BIT
:
1565 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
1566 sanitized_mask
= VK_IMAGE_ASPECT_DEPTH_BIT
;
1568 case VK_IMAGE_ASPECT_STENCIL_BIT
:
1569 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
1570 sanitized_mask
= VK_IMAGE_ASPECT_STENCIL_BIT
;
1572 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
1573 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1574 * combined depth stencil formats. Specifically, it states:
1576 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1577 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1579 * Image views with both depth and stencil aspects are only valid for
1580 * render target attachments, in which case
1581 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1582 * stencil surfaces from the underlying surface.
1584 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1585 sanitized_mask
= VK_IMAGE_ASPECT_DEPTH_BIT
;
1587 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
1588 sanitized_mask
= VK_IMAGE_ASPECT_STENCIL_BIT
;
1591 case VK_IMAGE_ASPECT_PLANE_0_BIT
:
1592 assert((image
->aspects
& ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) == 0);
1593 sanitized_mask
= VK_IMAGE_ASPECT_PLANE_0_BIT
;
1595 case VK_IMAGE_ASPECT_PLANE_1_BIT
:
1596 assert((image
->aspects
& ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) == 0);
1597 sanitized_mask
= VK_IMAGE_ASPECT_PLANE_1_BIT
;
1599 case VK_IMAGE_ASPECT_PLANE_2_BIT
:
1600 assert((image
->aspects
& ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV
) == 0);
1601 sanitized_mask
= VK_IMAGE_ASPECT_PLANE_2_BIT
;
1604 unreachable("image does not have aspect");
1608 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, sanitized_mask
);
1609 return &image
->planes
[plane
].surface
;