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
31 #include "anv_private.h"
32 #include "util/debug.h"
35 #include "vk_format_info.h"
38 * Exactly one bit must be set in \a aspect.
40 static isl_surf_usage_flags_t
41 choose_isl_surf_usage(VkImageCreateFlags vk_create_flags
,
42 VkImageUsageFlags vk_usage
,
43 VkImageAspectFlags aspect
)
45 isl_surf_usage_flags_t isl_usage
= 0;
47 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
48 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
50 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
51 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
53 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
54 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
56 if (vk_create_flags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
57 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
59 /* Even if we're only using it for transfer operations, clears to depth and
60 * stencil images happen as depth and stencil so they need the right ISL
61 * usage bits or else things will fall apart.
64 case VK_IMAGE_ASPECT_DEPTH_BIT
:
65 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
67 case VK_IMAGE_ASPECT_STENCIL_BIT
:
68 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
70 case VK_IMAGE_ASPECT_COLOR_BIT
:
71 case VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
:
72 case VK_IMAGE_ASPECT_PLANE_1_BIT_KHR
:
73 case VK_IMAGE_ASPECT_PLANE_2_BIT_KHR
:
76 unreachable("bad VkImageAspect");
79 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
80 /* blorp implements transfers by sampling from the source image. */
81 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
84 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
&&
85 aspect
== VK_IMAGE_ASPECT_COLOR_BIT
) {
86 /* blorp implements transfers by rendering into the destination image.
87 * Only request this with color images, as we deal with depth/stencil
88 * formats differently. */
89 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
96 * Exactly one bit must be set in \a aspect.
98 static struct anv_surface
*
99 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
101 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
102 return &image
->planes
[plane
].surface
;
106 add_surface(struct anv_image
*image
, struct anv_surface
*surf
, uint32_t plane
)
108 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
110 if (image
->disjoint
) {
111 surf
->offset
= align_u32(image
->planes
[plane
].size
, surf
->isl
.alignment
);
112 /* Plane offset is always 0 when it's disjoint. */
114 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
115 /* Determine plane's offset only once when the first surface is added. */
116 if (image
->planes
[plane
].size
== 0)
117 image
->planes
[plane
].offset
= image
->size
;
120 image
->size
= surf
->offset
+ surf
->isl
.size
;
121 image
->planes
[plane
].size
= (surf
->offset
+ surf
->isl
.size
) - image
->planes
[plane
].offset
;
123 image
->alignment
= MAX2(image
->alignment
, surf
->isl
.alignment
);
124 image
->planes
[plane
].alignment
= MAX2(image
->planes
[plane
].alignment
,
125 surf
->isl
.alignment
);
130 all_formats_ccs_e_compatible(const struct gen_device_info
*devinfo
,
131 const struct VkImageCreateInfo
*vk_info
)
133 enum isl_format format
=
134 anv_get_isl_format(devinfo
, vk_info
->format
,
135 VK_IMAGE_ASPECT_COLOR_BIT
, vk_info
->tiling
);
137 if (!isl_format_supports_ccs_e(devinfo
, format
))
140 if (!(vk_info
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
))
143 const VkImageFormatListCreateInfoKHR
*fmt_list
=
144 vk_find_struct_const(vk_info
->pNext
, IMAGE_FORMAT_LIST_CREATE_INFO_KHR
);
146 if (!fmt_list
|| fmt_list
->viewFormatCount
== 0)
149 for (uint32_t i
= 0; i
< fmt_list
->viewFormatCount
; i
++) {
150 enum isl_format view_format
=
151 anv_get_isl_format(devinfo
, fmt_list
->pViewFormats
[i
],
152 VK_IMAGE_ASPECT_COLOR_BIT
, vk_info
->tiling
);
154 if (!isl_formats_are_ccs_e_compatible(devinfo
, format
, view_format
))
162 * For color images that have an auxiliary surface, request allocation for an
163 * additional buffer that mainly stores fast-clear values. Use of this buffer
164 * allows us to access the image's subresources while being aware of their
165 * fast-clear values in non-trivial cases (e.g., outside of a render pass in
166 * which a fast clear has occurred).
168 * For the purpose of discoverability, the algorithm used to manage this buffer
169 * is described here. A clear value in this buffer is updated when a fast clear
170 * is performed on a subresource. One of two synchronization operations is
171 * performed in order for a following memory access to use the fast-clear
173 * a. Copy the value from the buffer to the surface state object used for
174 * reading. This is done implicitly when the value is the clear value
175 * predetermined to be the default in other surface state objects. This
176 * is currently only done explicitly for the operation below.
177 * b. Do (a) and use the surface state object to resolve the subresource.
178 * This is only done during layout transitions for decent performance.
180 * With the above scheme, we can fast-clear whenever the hardware allows except
181 * for two cases in which synchronization becomes impossible or undesirable:
182 * * The subresource is in the GENERAL layout and is cleared to a value
183 * other than the special default value.
185 * Performing a synchronization operation in order to read from the
186 * subresource is undesirable in this case. Firstly, b) is not an option
187 * because a layout transition isn't required between a write and read of
188 * an image in the GENERAL layout. Secondly, it's undesirable to do a)
189 * explicitly because it would require large infrastructural changes. The
190 * Vulkan API supports us in deciding not to optimize this layout by
191 * stating that using this layout may cause suboptimal performance. NOTE:
192 * the auxiliary buffer must always be enabled to support a) implicitly.
195 * * For the given miplevel, only some of the layers are cleared at once.
197 * If the user clears each layer to a different value, then tries to
198 * render to multiple layers at once, we have no ability to perform a
199 * synchronization operation in between. a) is not helpful because the
200 * object can only hold one clear value. b) is not an option because a
201 * layout transition isn't required in this case.
204 add_fast_clear_state_buffer(struct anv_image
*image
,
205 VkImageAspectFlagBits aspect
,
207 const struct anv_device
*device
)
209 assert(image
&& device
);
210 assert(image
->planes
[plane
].aux_surface
.isl
.size
> 0 &&
211 image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
);
213 /* The offset to the buffer of clear values must be dword-aligned for GPU
214 * memcpy operations. It is located immediately after the auxiliary surface.
217 /* Tiled images are guaranteed to be 4K aligned, so the image alignment
218 * should also be dword-aligned.
220 assert(image
->alignment
% 4 == 0);
222 /* Auxiliary buffers should be a multiple of 4K, so the start of the clear
223 * values buffer should already be dword-aligned.
225 assert((image
->planes
[plane
].offset
+ image
->planes
[plane
].size
) % 4 == 0);
227 /* This buffer should be at the very end of the plane. */
228 if (image
->disjoint
) {
229 assert(image
->planes
[plane
].size
==
230 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
232 assert(image
->size
==
233 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
236 const unsigned entry_size
= anv_fast_clear_state_entry_size(device
);
237 /* There's no padding between entries, so ensure that they're always a
238 * multiple of 32 bits in order to enable GPU memcpy operations.
240 assert(entry_size
% 4 == 0);
242 const unsigned plane_state_size
=
243 entry_size
* anv_image_aux_levels(image
, aspect
);
245 image
->planes
[plane
].fast_clear_state_offset
=
246 image
->planes
[plane
].offset
+ image
->planes
[plane
].size
;
248 image
->planes
[plane
].size
+= plane_state_size
;
249 image
->size
+= plane_state_size
;
253 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
254 * image's memory requirements (that is, the image's size and alignment).
256 * Exactly one bit must be set in \a aspect.
259 make_surface(const struct anv_device
*dev
,
260 struct anv_image
*image
,
261 const struct anv_image_create_info
*anv_info
,
262 VkImageAspectFlags aspect
)
264 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
267 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
268 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
269 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
270 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
273 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
274 * result with an optionally provided ISL tiling argument.
276 isl_tiling_flags_t tiling_flags
=
277 (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) ?
278 ISL_TILING_LINEAR_BIT
: ISL_TILING_ANY_MASK
;
280 if (anv_info
->isl_tiling_flags
)
281 tiling_flags
&= anv_info
->isl_tiling_flags
;
283 assert(tiling_flags
);
285 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
288 const unsigned plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
289 const struct anv_format_plane plane_format
=
290 anv_get_format_plane(&dev
->info
, image
->vk_format
, aspect
, image
->tiling
);
291 struct anv_surface
*anv_surf
= &image
->planes
[plane
].surface
;
293 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
294 * fall back to linear on Broadwell and earlier because we aren't
295 * guaranteed that we can handle offsets correctly. On Sky Lake, the
296 * horizontal and vertical alignments are sufficiently high that we can
297 * just use RENDER_SURFACE_STATE::X/Y Offset.
299 bool needs_shadow
= false;
300 if (dev
->info
.gen
<= 8 &&
301 (vk_info
->flags
& VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT_KHR
) &&
302 vk_info
->tiling
== VK_IMAGE_TILING_OPTIMAL
) {
303 assert(isl_format_is_compressed(plane_format
.isl_format
));
304 tiling_flags
= ISL_TILING_LINEAR_BIT
;
308 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
309 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
310 .format
= plane_format
.isl_format
,
311 .width
= image
->extent
.width
/ plane_format
.denominator_scales
[0],
312 .height
= image
->extent
.height
/ plane_format
.denominator_scales
[1],
313 .depth
= image
->extent
.depth
,
314 .levels
= vk_info
->mipLevels
,
315 .array_len
= vk_info
->arrayLayers
,
316 .samples
= vk_info
->samples
,
318 .row_pitch
= anv_info
->stride
,
319 .usage
= choose_isl_surf_usage(vk_info
->flags
, image
->usage
, aspect
),
320 .tiling_flags
= tiling_flags
);
322 /* isl_surf_init() will fail only if provided invalid input. Invalid input
323 * is illegal in Vulkan.
327 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_NONE
;
329 add_surface(image
, anv_surf
, plane
);
331 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
332 * create an identical tiled shadow surface for use while texturing so we
333 * don't get garbage performance.
336 assert(aspect
== VK_IMAGE_ASPECT_COLOR_BIT
);
337 assert(tiling_flags
== ISL_TILING_LINEAR_BIT
);
339 ok
= isl_surf_init(&dev
->isl_dev
, &image
->planes
[plane
].shadow_surface
.isl
,
340 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
341 .format
= plane_format
.isl_format
,
342 .width
= image
->extent
.width
,
343 .height
= image
->extent
.height
,
344 .depth
= image
->extent
.depth
,
345 .levels
= vk_info
->mipLevels
,
346 .array_len
= vk_info
->arrayLayers
,
347 .samples
= vk_info
->samples
,
349 .row_pitch
= anv_info
->stride
,
350 .usage
= choose_isl_surf_usage(image
->usage
, image
->usage
, aspect
),
351 .tiling_flags
= ISL_TILING_ANY_MASK
);
353 /* isl_surf_init() will fail only if provided invalid input. Invalid input
354 * is illegal in Vulkan.
358 add_surface(image
, &image
->planes
[plane
].shadow_surface
, plane
);
361 /* Add a HiZ surface to a depth buffer that will be used for rendering.
363 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
364 /* We don't advertise that depth buffers could be used as storage
367 assert(!(image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
369 /* Allow the user to control HiZ enabling. Disable by default on gen7
370 * because resolves are not currently implemented pre-BDW.
372 if (!(image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
373 /* It will never be used as an attachment, HiZ is pointless. */
374 } else if (dev
->info
.gen
== 7) {
375 anv_perf_warn(dev
->instance
, image
, "Implement gen7 HiZ");
376 } else if (vk_info
->mipLevels
> 1) {
377 anv_perf_warn(dev
->instance
, image
, "Enable multi-LOD HiZ");
378 } else if (vk_info
->arrayLayers
> 1) {
379 anv_perf_warn(dev
->instance
, image
,
380 "Implement multi-arrayLayer HiZ clears and resolves");
381 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
382 anv_perf_warn(dev
->instance
, image
, "Enable gen8 multisampled HiZ");
383 } else if (!unlikely(INTEL_DEBUG
& DEBUG_NO_HIZ
)) {
384 assert(image
->planes
[plane
].aux_surface
.isl
.size
== 0);
385 ok
= isl_surf_get_hiz_surf(&dev
->isl_dev
,
386 &image
->planes
[plane
].surface
.isl
,
387 &image
->planes
[plane
].aux_surface
.isl
);
389 add_surface(image
, &image
->planes
[plane
].aux_surface
, plane
);
390 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_HIZ
;
392 } else if ((aspect
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) && vk_info
->samples
== 1) {
393 /* TODO: Disallow compression with :
395 * 1) non multiplanar images (We appear to hit a sampler bug with
396 * CCS & R16G16 format. Putting the clear state a page/4096bytes
397 * further fixes the issue).
399 * 2) alias images, because they might be aliases of images
402 * 3) compression disabled by debug
404 const bool allow_compression
=
405 image
->n_planes
== 1 &&
406 (vk_info
->flags
& VK_IMAGE_CREATE_ALIAS_BIT_KHR
) == 0 &&
407 likely((INTEL_DEBUG
& DEBUG_NO_RBC
) == 0);
409 if (allow_compression
) {
410 assert(image
->planes
[plane
].aux_surface
.isl
.size
== 0);
411 ok
= isl_surf_get_ccs_surf(&dev
->isl_dev
,
412 &image
->planes
[plane
].surface
.isl
,
413 &image
->planes
[plane
].aux_surface
.isl
, 0);
416 /* Disable CCS when it is not useful (i.e., when you can't render
417 * to the image with CCS enabled).
419 if (!isl_format_supports_rendering(&dev
->info
,
420 plane_format
.isl_format
)) {
421 /* While it may be technically possible to enable CCS for this
422 * image, we currently don't have things hooked up to get it
425 anv_perf_warn(dev
->instance
, image
,
426 "This image format doesn't support rendering. "
427 "Not allocating an CCS buffer.");
428 image
->planes
[plane
].aux_surface
.isl
.size
= 0;
432 add_surface(image
, &image
->planes
[plane
].aux_surface
, plane
);
433 add_fast_clear_state_buffer(image
, aspect
, plane
, dev
);
435 /* For images created without MUTABLE_FORMAT_BIT set, we know that
436 * they will always be used with the original format. In
437 * particular, they will always be used with a format that
438 * supports color compression. If it's never used as a storage
439 * image, then it will only be used through the sampler or the as
440 * a render target. This means that it's safe to just leave
441 * compression on at all times for these formats.
443 if (!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) &&
444 all_formats_ccs_e_compatible(&dev
->info
, vk_info
)) {
445 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_CCS_E
;
449 } else if ((aspect
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) && vk_info
->samples
> 1) {
450 assert(!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
451 assert(image
->planes
[plane
].aux_surface
.isl
.size
== 0);
452 ok
= isl_surf_get_mcs_surf(&dev
->isl_dev
,
453 &image
->planes
[plane
].surface
.isl
,
454 &image
->planes
[plane
].aux_surface
.isl
);
456 add_surface(image
, &image
->planes
[plane
].aux_surface
, plane
);
457 add_fast_clear_state_buffer(image
, aspect
, plane
, dev
);
458 image
->planes
[plane
].aux_usage
= ISL_AUX_USAGE_MCS
;
462 assert((image
->planes
[plane
].offset
+ image
->planes
[plane
].size
) == image
->size
);
464 /* Upper bound of the last surface should be smaller than the plane's
467 assert((MAX2(image
->planes
[plane
].surface
.offset
,
468 image
->planes
[plane
].aux_surface
.offset
) +
469 (image
->planes
[plane
].aux_surface
.isl
.size
> 0 ?
470 image
->planes
[plane
].aux_surface
.isl
.size
:
471 image
->planes
[plane
].surface
.isl
.size
)) <=
472 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
474 if (image
->planes
[plane
].aux_surface
.isl
.size
) {
475 /* assert(image->planes[plane].fast_clear_state_offset == */
476 /* (image->planes[plane].aux_surface.offset + image->planes[plane].aux_surface.isl.size)); */
477 assert(image
->planes
[plane
].fast_clear_state_offset
<
478 (image
->planes
[plane
].offset
+ image
->planes
[plane
].size
));
485 anv_image_create(VkDevice _device
,
486 const struct anv_image_create_info
*create_info
,
487 const VkAllocationCallbacks
* alloc
,
490 ANV_FROM_HANDLE(anv_device
, device
, _device
);
491 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
492 struct anv_image
*image
= NULL
;
495 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
497 anv_assert(pCreateInfo
->mipLevels
> 0);
498 anv_assert(pCreateInfo
->arrayLayers
> 0);
499 anv_assert(pCreateInfo
->samples
> 0);
500 anv_assert(pCreateInfo
->extent
.width
> 0);
501 anv_assert(pCreateInfo
->extent
.height
> 0);
502 anv_assert(pCreateInfo
->extent
.depth
> 0);
504 image
= vk_zalloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
505 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
507 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
509 image
->type
= pCreateInfo
->imageType
;
510 image
->extent
= pCreateInfo
->extent
;
511 image
->vk_format
= pCreateInfo
->format
;
512 image
->format
= anv_get_format(pCreateInfo
->format
);
513 image
->aspects
= vk_format_aspects(image
->vk_format
);
514 image
->levels
= pCreateInfo
->mipLevels
;
515 image
->array_size
= pCreateInfo
->arrayLayers
;
516 image
->samples
= pCreateInfo
->samples
;
517 image
->usage
= pCreateInfo
->usage
;
518 image
->tiling
= pCreateInfo
->tiling
;
520 const struct anv_format
*format
= anv_get_format(image
->vk_format
);
521 assert(format
!= NULL
);
523 image
->n_planes
= format
->n_planes
;
526 for_each_bit(b
, image
->aspects
) {
527 r
= make_surface(device
, image
, create_info
, (1 << b
));
532 *pImage
= anv_image_to_handle(image
);
538 vk_free2(&device
->alloc
, alloc
, image
);
544 anv_CreateImage(VkDevice device
,
545 const VkImageCreateInfo
*pCreateInfo
,
546 const VkAllocationCallbacks
*pAllocator
,
549 return anv_image_create(device
,
550 &(struct anv_image_create_info
) {
551 .vk_info
= pCreateInfo
,
558 anv_DestroyImage(VkDevice _device
, VkImage _image
,
559 const VkAllocationCallbacks
*pAllocator
)
561 ANV_FROM_HANDLE(anv_device
, device
, _device
);
562 ANV_FROM_HANDLE(anv_image
, image
, _image
);
567 vk_free2(&device
->alloc
, pAllocator
, image
);
570 static void anv_image_bind_memory_plane(struct anv_device
*device
,
571 struct anv_image
*image
,
573 struct anv_device_memory
*memory
,
574 uint32_t memory_offset
)
577 image
->planes
[plane
].bo
= NULL
;
578 image
->planes
[plane
].bo_offset
= 0;
581 image
->planes
[plane
].bo
= memory
->bo
;
582 image
->planes
[plane
].bo_offset
= memory_offset
;
585 VkResult
anv_BindImageMemory(
588 VkDeviceMemory _memory
,
589 VkDeviceSize memoryOffset
)
591 ANV_FROM_HANDLE(anv_device
, device
, _device
);
592 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
593 ANV_FROM_HANDLE(anv_image
, image
, _image
);
596 anv_foreach_image_aspect_bit(aspect_bit
, image
, image
->aspects
) {
598 anv_image_aspect_to_plane(image
->aspects
, 1UL << aspect_bit
);
599 anv_image_bind_memory_plane(device
, image
, plane
, mem
, memoryOffset
);
605 VkResult
anv_BindImageMemory2KHR(
607 uint32_t bindInfoCount
,
608 const VkBindImageMemoryInfoKHR
* pBindInfos
)
610 ANV_FROM_HANDLE(anv_device
, device
, _device
);
612 for (uint32_t i
= 0; i
< bindInfoCount
; i
++) {
613 const VkBindImageMemoryInfoKHR
*bind_info
= &pBindInfos
[i
];
614 ANV_FROM_HANDLE(anv_device_memory
, mem
, bind_info
->memory
);
615 ANV_FROM_HANDLE(anv_image
, image
, bind_info
->image
);
618 anv_foreach_image_aspect_bit(aspect_bit
, image
, image
->aspects
) {
620 anv_image_aspect_to_plane(image
->aspects
, 1UL << aspect_bit
);
621 anv_image_bind_memory_plane(device
, image
, plane
,
622 mem
, bind_info
->memoryOffset
);
630 anv_surface_get_subresource_layout(struct anv_image
*image
,
631 struct anv_surface
*surface
,
632 const VkImageSubresource
*subresource
,
633 VkSubresourceLayout
*layout
)
635 /* If we are on a non-zero mip level or array slice, we need to
636 * calculate a real offset.
638 anv_assert(subresource
->mipLevel
== 0);
639 anv_assert(subresource
->arrayLayer
== 0);
641 layout
->offset
= surface
->offset
;
642 layout
->rowPitch
= surface
->isl
.row_pitch
;
643 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
644 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
645 layout
->size
= surface
->isl
.size
;
648 void anv_GetImageSubresourceLayout(
651 const VkImageSubresource
* pSubresource
,
652 VkSubresourceLayout
* pLayout
)
654 ANV_FROM_HANDLE(anv_image
, image
, _image
);
656 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
658 anv_surface_get_subresource_layout(image
,
660 pSubresource
->aspectMask
),
661 pSubresource
, pLayout
);
665 * This function determines the optimal buffer to use for a given
666 * VkImageLayout and other pieces of information needed to make that
667 * determination. This does not determine the optimal buffer to use
668 * during a resolve operation.
670 * @param devinfo The device information of the Intel GPU.
671 * @param image The image that may contain a collection of buffers.
672 * @param plane The plane of the image to be accessed.
673 * @param layout The current layout of the image aspect(s).
675 * @return The primary buffer that should be used for the given layout.
678 anv_layout_to_aux_usage(const struct gen_device_info
* const devinfo
,
679 const struct anv_image
* const image
,
680 const VkImageAspectFlagBits aspect
,
681 const VkImageLayout layout
)
683 /* Validate the inputs. */
685 /* The devinfo is needed as the optimal buffer varies across generations. */
686 assert(devinfo
!= NULL
);
688 /* The layout of a NULL image is not properly defined. */
689 assert(image
!= NULL
);
691 /* The aspect must be exactly one of the image aspects. */
692 assert(_mesa_bitcount(aspect
) == 1 && (aspect
& image
->aspects
));
694 /* Determine the optimal buffer. */
696 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
698 /* If there is no auxiliary surface allocated, we must use the one and only
701 if (image
->planes
[plane
].aux_surface
.isl
.size
== 0)
702 return ISL_AUX_USAGE_NONE
;
704 /* All images that use an auxiliary surface are required to be tiled. */
705 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
707 /* Stencil has no aux */
708 assert(aspect
!= VK_IMAGE_ASPECT_STENCIL_BIT
);
710 /* The following switch currently only handles depth stencil aspects.
711 * TODO: Handle the color aspect.
713 if (image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
)
714 return image
->planes
[plane
].aux_usage
;
718 /* Invalid Layouts */
719 case VK_IMAGE_LAYOUT_RANGE_SIZE
:
720 case VK_IMAGE_LAYOUT_MAX_ENUM
:
721 unreachable("Invalid image layout.");
725 * The pre-initialized layout is equivalent to the undefined layout for
726 * optimally-tiled images. We can only do color compression (CCS or HiZ)
729 case VK_IMAGE_LAYOUT_UNDEFINED
:
730 case VK_IMAGE_LAYOUT_PREINITIALIZED
:
731 return ISL_AUX_USAGE_NONE
;
736 * This buffer could be a depth buffer used in a transfer operation. BLORP
737 * currently doesn't use HiZ for transfer operations so we must use the main
738 * buffer for this layout. TODO: Enable HiZ in BLORP.
740 case VK_IMAGE_LAYOUT_GENERAL
:
741 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
:
742 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
:
743 return ISL_AUX_USAGE_NONE
;
746 /* Sampling Layouts */
747 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
:
748 assert((image
->aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) == 0);
750 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
:
751 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL_KHR
:
752 assert(aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
);
753 if (anv_can_sample_with_hiz(devinfo
, image
))
754 return ISL_AUX_USAGE_HIZ
;
756 return ISL_AUX_USAGE_NONE
;
758 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
:
759 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
761 /* On SKL+, the render buffer can be decompressed by the presentation
762 * engine. Support for this feature has not yet landed in the wider
763 * ecosystem. TODO: Update this code when support lands.
765 * From the BDW PRM, Vol 7, Render Target Resolve:
767 * If the MCS is enabled on a non-multisampled render target, the
768 * render target must be resolved before being used for other
769 * purposes (display, texture, CPU lock) The clear value from
770 * SURFACE_STATE is written into pixels in the render target
771 * indicated as clear in the MCS.
773 * Pre-SKL, the render buffer must be resolved before being used for
774 * presentation. We can infer that the auxiliary buffer is not used.
776 return ISL_AUX_USAGE_NONE
;
779 /* Rendering Layouts */
780 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
:
781 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
782 unreachable("Color images are not yet supported.");
784 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
:
785 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR
:
786 assert(aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
);
787 return ISL_AUX_USAGE_HIZ
;
789 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR
:
790 unreachable("VK_KHR_shared_presentable_image is unsupported");
793 /* If the layout isn't recognized in the exhaustive switch above, the
794 * VkImageLayout value is not defined in vulkan.h.
796 unreachable("layout is not a VkImageLayout enumeration member.");
800 static struct anv_state
801 alloc_surface_state(struct anv_device
*device
)
803 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
806 static enum isl_channel_select
807 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
808 struct isl_swizzle format_swizzle
)
810 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
814 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
815 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
816 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
817 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
818 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
819 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
821 unreachable("Invalid swizzle");
826 anv_image_fill_surface_state(struct anv_device
*device
,
827 const struct anv_image
*image
,
828 VkImageAspectFlagBits aspect
,
829 const struct isl_view
*view_in
,
830 isl_surf_usage_flags_t view_usage
,
831 enum isl_aux_usage aux_usage
,
832 const union isl_color_value
*clear_color
,
833 enum anv_image_view_state_flags flags
,
834 struct anv_surface_state
*state_inout
,
835 struct brw_image_param
*image_param_out
)
837 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, aspect
);
839 const struct anv_surface
*surface
= &image
->planes
[plane
].surface
,
840 *aux_surface
= &image
->planes
[plane
].aux_surface
;
842 struct isl_view view
= *view_in
;
843 view
.usage
|= view_usage
;
845 /* For texturing with VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL from a
846 * compressed surface with a shadow surface, we use the shadow instead of
847 * the primary surface. The shadow surface will be tiled, unlike the main
848 * surface, so it should get significantly better performance.
850 if (image
->planes
[plane
].shadow_surface
.isl
.size
> 0 &&
851 isl_format_is_compressed(view
.format
) &&
852 (flags
& ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL
)) {
853 assert(isl_format_is_compressed(surface
->isl
.format
));
854 assert(surface
->isl
.tiling
== ISL_TILING_LINEAR
);
855 assert(image
->planes
[plane
].shadow_surface
.isl
.tiling
!= ISL_TILING_LINEAR
);
856 surface
= &image
->planes
[plane
].shadow_surface
;
859 if (view_usage
== ISL_SURF_USAGE_RENDER_TARGET_BIT
)
860 view
.swizzle
= anv_swizzle_for_render(view
.swizzle
);
862 /* If this is a HiZ buffer we can sample from with a programmable clear
863 * value (SKL+), define the clear value to the optimal constant.
865 union isl_color_value default_clear_color
= { .u32
= { 0, } };
866 if (device
->info
.gen
>= 9 && aux_usage
== ISL_AUX_USAGE_HIZ
)
867 default_clear_color
.f32
[0] = ANV_HZ_FC_VAL
;
869 clear_color
= &default_clear_color
;
871 const uint64_t address
= image
->planes
[plane
].bo_offset
+ surface
->offset
;
872 const uint64_t aux_address
= aux_usage
== ISL_AUX_USAGE_NONE
?
873 0 : (image
->planes
[plane
].bo_offset
+ aux_surface
->offset
);
875 if (view_usage
== ISL_SURF_USAGE_STORAGE_BIT
&&
876 !(flags
& ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
) &&
877 !isl_has_matching_typed_storage_image_format(&device
->info
,
879 /* In this case, we are a writeable storage buffer which needs to be
880 * lowered to linear. All tiling and offset calculations will be done in
883 assert(aux_usage
== ISL_AUX_USAGE_NONE
);
884 isl_buffer_fill_state(&device
->isl_dev
, state_inout
->state
.map
,
886 .size
= surface
->isl
.size
,
887 .format
= ISL_FORMAT_RAW
,
889 .mocs
= device
->default_mocs
);
890 state_inout
->address
= address
,
891 state_inout
->aux_address
= 0;
893 if (view_usage
== ISL_SURF_USAGE_STORAGE_BIT
&&
894 !(flags
& ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
)) {
895 /* Typed surface reads support a very limited subset of the shader
896 * image formats. Translate it into the closest format the hardware
899 assert(aux_usage
== ISL_AUX_USAGE_NONE
);
900 view
.format
= isl_lower_storage_image_format(&device
->info
,
904 const struct isl_surf
*isl_surf
= &surface
->isl
;
906 struct isl_surf tmp_surf
;
907 uint32_t offset_B
= 0, tile_x_sa
= 0, tile_y_sa
= 0;
908 if (isl_format_is_compressed(surface
->isl
.format
) &&
909 !isl_format_is_compressed(view
.format
)) {
910 /* We're creating an uncompressed view of a compressed surface. This
911 * is allowed but only for a single level/layer.
913 assert(surface
->isl
.samples
== 1);
914 assert(view
.levels
== 1);
915 assert(view
.array_len
== 1);
917 isl_surf_get_image_surf(&device
->isl_dev
, isl_surf
,
919 surface
->isl
.dim
== ISL_SURF_DIM_3D
?
920 0 : view
.base_array_layer
,
921 surface
->isl
.dim
== ISL_SURF_DIM_3D
?
922 view
.base_array_layer
: 0,
924 &offset_B
, &tile_x_sa
, &tile_y_sa
);
926 /* The newly created image represents the one subimage we're
927 * referencing with this view so it only has one array slice and
930 view
.base_array_layer
= 0;
933 /* We're making an uncompressed view here. The image dimensions need
934 * to be scaled down by the block size.
936 const struct isl_format_layout
*fmtl
=
937 isl_format_get_layout(surface
->isl
.format
);
938 tmp_surf
.format
= view
.format
;
939 tmp_surf
.logical_level0_px
.width
=
940 DIV_ROUND_UP(tmp_surf
.logical_level0_px
.width
, fmtl
->bw
);
941 tmp_surf
.logical_level0_px
.height
=
942 DIV_ROUND_UP(tmp_surf
.logical_level0_px
.height
, fmtl
->bh
);
943 tmp_surf
.phys_level0_sa
.width
/= fmtl
->bw
;
944 tmp_surf
.phys_level0_sa
.height
/= fmtl
->bh
;
945 tile_x_sa
/= fmtl
->bw
;
946 tile_y_sa
/= fmtl
->bh
;
948 isl_surf
= &tmp_surf
;
950 if (device
->info
.gen
<= 8) {
951 assert(surface
->isl
.tiling
== ISL_TILING_LINEAR
);
952 assert(tile_x_sa
== 0);
953 assert(tile_y_sa
== 0);
957 isl_surf_fill_state(&device
->isl_dev
, state_inout
->state
.map
,
960 .address
= address
+ offset_B
,
961 .clear_color
= *clear_color
,
962 .aux_surf
= &aux_surface
->isl
,
963 .aux_usage
= aux_usage
,
964 .aux_address
= aux_address
,
965 .mocs
= device
->default_mocs
,
966 .x_offset_sa
= tile_x_sa
,
967 .y_offset_sa
= tile_y_sa
);
968 state_inout
->address
= address
+ offset_B
;
969 if (device
->info
.gen
>= 8) {
970 state_inout
->aux_address
= aux_address
;
972 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
973 * used to store other information. This should be ok, however,
974 * because surface buffer addresses are always 4K page alinged.
976 uint32_t *aux_addr_dw
= state_inout
->state
.map
+
977 device
->isl_dev
.ss
.aux_addr_offset
;
978 assert((aux_address
& 0xfff) == 0);
979 assert(aux_address
== (*aux_addr_dw
& 0xfffff000));
980 state_inout
->aux_address
= *aux_addr_dw
;
984 anv_state_flush(device
, state_inout
->state
);
986 if (image_param_out
) {
987 assert(view_usage
== ISL_SURF_USAGE_STORAGE_BIT
);
988 isl_surf_fill_image_param(&device
->isl_dev
, image_param_out
,
989 &surface
->isl
, &view
);
993 static VkImageAspectFlags
994 remap_aspect_flags(VkImageAspectFlags view_aspects
)
996 if (view_aspects
& VK_IMAGE_ASPECT_ANY_COLOR_BIT
) {
997 if (_mesa_bitcount(view_aspects
) == 1)
998 return VK_IMAGE_ASPECT_COLOR_BIT
;
1000 VkImageAspectFlags color_aspects
= 0;
1001 for (uint32_t i
= 0; i
< _mesa_bitcount(view_aspects
); i
++)
1002 color_aspects
|= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
<< i
;
1003 return color_aspects
;
1005 /* No special remapping needed for depth & stencil aspects. */
1006 return view_aspects
;
1010 anv_CreateImageView(VkDevice _device
,
1011 const VkImageViewCreateInfo
*pCreateInfo
,
1012 const VkAllocationCallbacks
*pAllocator
,
1015 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1016 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
1017 struct anv_image_view
*iview
;
1019 iview
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*iview
), 8,
1020 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1022 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1024 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
1026 assert(range
->layerCount
> 0);
1027 assert(range
->baseMipLevel
< image
->levels
);
1029 const VkImageViewUsageCreateInfoKHR
*usage_info
=
1030 vk_find_struct_const(pCreateInfo
, IMAGE_VIEW_USAGE_CREATE_INFO_KHR
);
1031 VkImageUsageFlags view_usage
= usage_info
? usage_info
->usage
: image
->usage
;
1032 /* View usage should be a subset of image usage */
1033 assert((view_usage
& ~image
->usage
) == 0);
1034 assert(view_usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
1035 VK_IMAGE_USAGE_STORAGE_BIT
|
1036 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
1037 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
|
1038 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
1040 switch (image
->type
) {
1042 unreachable("bad VkImageType");
1043 case VK_IMAGE_TYPE_1D
:
1044 case VK_IMAGE_TYPE_2D
:
1045 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
1047 case VK_IMAGE_TYPE_3D
:
1048 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
1049 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
1053 /* First expand aspects to the image's ones (for example
1054 * VK_IMAGE_ASPECT_COLOR_BIT will be converted to
1055 * VK_IMAGE_ASPECT_PLANE_0_BIT_KHR | VK_IMAGE_ASPECT_PLANE_1_BIT_KHR |
1056 * VK_IMAGE_ASPECT_PLANE_2_BIT_KHR for an image of format
1057 * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR.
1059 VkImageAspectFlags expanded_aspects
=
1060 anv_image_expand_aspects(image
, range
->aspectMask
);
1062 iview
->image
= image
;
1064 /* Remap the expanded aspects for the image view. For example if only
1065 * VK_IMAGE_ASPECT_PLANE_1_BIT_KHR was given in range->aspectMask, we will
1066 * convert it to VK_IMAGE_ASPECT_COLOR_BIT since from the point of view of
1067 * the image view, it only has a single plane.
1069 iview
->aspect_mask
= remap_aspect_flags(expanded_aspects
);
1070 iview
->n_planes
= anv_image_aspect_get_planes(iview
->aspect_mask
);
1071 iview
->vk_format
= pCreateInfo
->format
;
1073 iview
->extent
= (VkExtent3D
) {
1074 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
1075 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
1076 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
1079 /* Now go through the underlying image selected planes (computed in
1080 * expanded_aspects) and map them to planes in the image view.
1082 uint32_t iaspect_bit
, vplane
= 0;
1083 anv_foreach_image_aspect_bit(iaspect_bit
, image
, expanded_aspects
) {
1085 anv_image_aspect_to_plane(expanded_aspects
, 1UL << iaspect_bit
);
1086 VkImageAspectFlags vplane_aspect
=
1087 anv_plane_to_aspect(iview
->aspect_mask
, vplane
);
1088 struct anv_format_plane format
=
1089 anv_get_format_plane(&device
->info
, pCreateInfo
->format
,
1090 vplane_aspect
, image
->tiling
);
1092 iview
->planes
[vplane
].image_plane
= iplane
;
1094 iview
->planes
[vplane
].isl
= (struct isl_view
) {
1095 .format
= format
.isl_format
,
1096 .base_level
= range
->baseMipLevel
,
1097 .levels
= anv_get_levelCount(image
, range
),
1098 .base_array_layer
= range
->baseArrayLayer
,
1099 .array_len
= anv_get_layerCount(image
, range
),
1101 .r
= remap_swizzle(pCreateInfo
->components
.r
,
1102 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
1103 .g
= remap_swizzle(pCreateInfo
->components
.g
,
1104 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
1105 .b
= remap_swizzle(pCreateInfo
->components
.b
,
1106 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
1107 .a
= remap_swizzle(pCreateInfo
->components
.a
,
1108 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
1112 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_3D
) {
1113 iview
->planes
[vplane
].isl
.base_array_layer
= 0;
1114 iview
->planes
[vplane
].isl
.array_len
= iview
->extent
.depth
;
1117 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
1118 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
1119 iview
->planes
[vplane
].isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
1121 iview
->planes
[vplane
].isl
.usage
= 0;
1124 if (view_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
||
1125 (view_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
&&
1126 !(iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
))) {
1127 iview
->planes
[vplane
].optimal_sampler_surface_state
.state
= alloc_surface_state(device
);
1128 iview
->planes
[vplane
].general_sampler_surface_state
.state
= alloc_surface_state(device
);
1130 enum isl_aux_usage general_aux_usage
=
1131 anv_layout_to_aux_usage(&device
->info
, image
, 1UL << iaspect_bit
,
1132 VK_IMAGE_LAYOUT_GENERAL
);
1133 enum isl_aux_usage optimal_aux_usage
=
1134 anv_layout_to_aux_usage(&device
->info
, image
, 1UL << iaspect_bit
,
1135 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
1137 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1138 &iview
->planes
[vplane
].isl
,
1139 ISL_SURF_USAGE_TEXTURE_BIT
,
1140 optimal_aux_usage
, NULL
,
1141 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL
,
1142 &iview
->planes
[vplane
].optimal_sampler_surface_state
,
1145 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1146 &iview
->planes
[vplane
].isl
,
1147 ISL_SURF_USAGE_TEXTURE_BIT
,
1148 general_aux_usage
, NULL
,
1150 &iview
->planes
[vplane
].general_sampler_surface_state
,
1154 /* NOTE: This one needs to go last since it may stomp isl_view.format */
1155 if (view_usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
1156 iview
->planes
[vplane
].storage_surface_state
.state
= alloc_surface_state(device
);
1157 iview
->planes
[vplane
].writeonly_storage_surface_state
.state
= alloc_surface_state(device
);
1159 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1160 &iview
->planes
[vplane
].isl
,
1161 ISL_SURF_USAGE_STORAGE_BIT
,
1162 ISL_AUX_USAGE_NONE
, NULL
,
1164 &iview
->planes
[vplane
].storage_surface_state
,
1165 &iview
->planes
[vplane
].storage_image_param
);
1167 anv_image_fill_surface_state(device
, image
, 1ULL << iaspect_bit
,
1168 &iview
->planes
[vplane
].isl
,
1169 ISL_SURF_USAGE_STORAGE_BIT
,
1170 ISL_AUX_USAGE_NONE
, NULL
,
1171 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
,
1172 &iview
->planes
[vplane
].writeonly_storage_surface_state
,
1179 *pView
= anv_image_view_to_handle(iview
);
1185 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
1186 const VkAllocationCallbacks
*pAllocator
)
1188 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1189 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
1194 for (uint32_t plane
= 0; plane
< iview
->n_planes
; plane
++) {
1195 if (iview
->planes
[plane
].optimal_sampler_surface_state
.state
.alloc_size
> 0) {
1196 anv_state_pool_free(&device
->surface_state_pool
,
1197 iview
->planes
[plane
].optimal_sampler_surface_state
.state
);
1200 if (iview
->planes
[plane
].general_sampler_surface_state
.state
.alloc_size
> 0) {
1201 anv_state_pool_free(&device
->surface_state_pool
,
1202 iview
->planes
[plane
].general_sampler_surface_state
.state
);
1205 if (iview
->planes
[plane
].storage_surface_state
.state
.alloc_size
> 0) {
1206 anv_state_pool_free(&device
->surface_state_pool
,
1207 iview
->planes
[plane
].storage_surface_state
.state
);
1210 if (iview
->planes
[plane
].writeonly_storage_surface_state
.state
.alloc_size
> 0) {
1211 anv_state_pool_free(&device
->surface_state_pool
,
1212 iview
->planes
[plane
].writeonly_storage_surface_state
.state
);
1216 vk_free2(&device
->alloc
, pAllocator
, iview
);
1221 anv_CreateBufferView(VkDevice _device
,
1222 const VkBufferViewCreateInfo
*pCreateInfo
,
1223 const VkAllocationCallbacks
*pAllocator
,
1224 VkBufferView
*pView
)
1226 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1227 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
1228 struct anv_buffer_view
*view
;
1230 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
1231 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1233 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1235 /* TODO: Handle the format swizzle? */
1237 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
1238 VK_IMAGE_ASPECT_COLOR_BIT
,
1239 VK_IMAGE_TILING_LINEAR
);
1240 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
1241 view
->bo
= buffer
->bo
;
1242 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
1243 view
->range
= anv_buffer_get_range(buffer
, pCreateInfo
->offset
,
1244 pCreateInfo
->range
);
1245 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
1247 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
1248 view
->surface_state
= alloc_surface_state(device
);
1250 anv_fill_buffer_surface_state(device
, view
->surface_state
,
1252 view
->offset
, view
->range
, format_bs
);
1254 view
->surface_state
= (struct anv_state
){ 0 };
1257 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
1258 view
->storage_surface_state
= alloc_surface_state(device
);
1259 view
->writeonly_storage_surface_state
= alloc_surface_state(device
);
1261 enum isl_format storage_format
=
1262 isl_has_matching_typed_storage_image_format(&device
->info
,
1264 isl_lower_storage_image_format(&device
->info
, view
->format
) :
1267 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
1269 view
->offset
, view
->range
,
1270 (storage_format
== ISL_FORMAT_RAW
? 1 :
1271 isl_format_get_layout(storage_format
)->bpb
/ 8));
1273 /* Write-only accesses should use the original format. */
1274 anv_fill_buffer_surface_state(device
, view
->writeonly_storage_surface_state
,
1276 view
->offset
, view
->range
,
1277 isl_format_get_layout(view
->format
)->bpb
/ 8);
1279 isl_buffer_fill_image_param(&device
->isl_dev
,
1280 &view
->storage_image_param
,
1281 view
->format
, view
->range
);
1283 view
->storage_surface_state
= (struct anv_state
){ 0 };
1284 view
->writeonly_storage_surface_state
= (struct anv_state
){ 0 };
1287 *pView
= anv_buffer_view_to_handle(view
);
1293 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
1294 const VkAllocationCallbacks
*pAllocator
)
1296 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1297 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
1302 if (view
->surface_state
.alloc_size
> 0)
1303 anv_state_pool_free(&device
->surface_state_pool
,
1304 view
->surface_state
);
1306 if (view
->storage_surface_state
.alloc_size
> 0)
1307 anv_state_pool_free(&device
->surface_state_pool
,
1308 view
->storage_surface_state
);
1310 if (view
->writeonly_storage_surface_state
.alloc_size
> 0)
1311 anv_state_pool_free(&device
->surface_state_pool
,
1312 view
->writeonly_storage_surface_state
);
1314 vk_free2(&device
->alloc
, pAllocator
, view
);
1317 const struct anv_surface
*
1318 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
1319 VkImageAspectFlags aspect_mask
)
1321 VkImageAspectFlags sanitized_mask
;
1323 switch (aspect_mask
) {
1324 case VK_IMAGE_ASPECT_COLOR_BIT
:
1325 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1326 sanitized_mask
= VK_IMAGE_ASPECT_COLOR_BIT
;
1328 case VK_IMAGE_ASPECT_DEPTH_BIT
:
1329 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
1330 sanitized_mask
= VK_IMAGE_ASPECT_DEPTH_BIT
;
1332 case VK_IMAGE_ASPECT_STENCIL_BIT
:
1333 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
1334 sanitized_mask
= VK_IMAGE_ASPECT_STENCIL_BIT
;
1336 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
1337 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1338 * combined depth stencil formats. Specifically, it states:
1340 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1341 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1343 * Image views with both depth and stencil aspects are only valid for
1344 * render target attachments, in which case
1345 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1346 * stencil surfaces from the underlying surface.
1348 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1349 sanitized_mask
= VK_IMAGE_ASPECT_DEPTH_BIT
;
1351 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
1352 sanitized_mask
= VK_IMAGE_ASPECT_STENCIL_BIT
;
1355 case VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
:
1356 assert((image
->aspects
& ~VK_IMAGE_ASPECT_ANY_COLOR_BIT
) == 0);
1357 sanitized_mask
= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR
;
1359 case VK_IMAGE_ASPECT_PLANE_1_BIT_KHR
:
1360 assert((image
->aspects
& ~VK_IMAGE_ASPECT_ANY_COLOR_BIT
) == 0);
1361 sanitized_mask
= VK_IMAGE_ASPECT_PLANE_1_BIT_KHR
;
1363 case VK_IMAGE_ASPECT_PLANE_2_BIT_KHR
:
1364 assert((image
->aspects
& ~VK_IMAGE_ASPECT_ANY_COLOR_BIT
) == 0);
1365 sanitized_mask
= VK_IMAGE_ASPECT_PLANE_2_BIT_KHR
;
1368 unreachable("image does not have aspect");
1372 uint32_t plane
= anv_image_aspect_to_plane(image
->aspects
, sanitized_mask
);
1373 return &image
->planes
[plane
].surface
;