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
:
73 unreachable("bad VkImageAspect");
76 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
77 /* blorp implements transfers by sampling from the source image. */
78 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
81 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
&&
82 aspect
== VK_IMAGE_ASPECT_COLOR_BIT
) {
83 /* blorp implements transfers by rendering into the destination image.
84 * Only request this with color images, as we deal with depth/stencil
85 * formats differently. */
86 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
93 * Exactly one bit must be set in \a aspect.
95 static struct anv_surface
*
96 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
100 unreachable("bad VkImageAspect");
101 case VK_IMAGE_ASPECT_COLOR_BIT
:
102 return &image
->color_surface
;
103 case VK_IMAGE_ASPECT_DEPTH_BIT
:
104 return &image
->depth_surface
;
105 case VK_IMAGE_ASPECT_STENCIL_BIT
:
106 return &image
->stencil_surface
;
111 add_surface(struct anv_image
*image
, struct anv_surface
*surf
)
113 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
115 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
116 image
->size
= surf
->offset
+ surf
->isl
.size
;
117 image
->alignment
= MAX2(image
->alignment
, surf
->isl
.alignment
);
122 all_formats_ccs_e_compatible(const struct gen_device_info
*devinfo
,
123 const struct VkImageCreateInfo
*vk_info
)
125 enum isl_format format
=
126 anv_get_isl_format(devinfo
, vk_info
->format
,
127 VK_IMAGE_ASPECT_COLOR_BIT
, vk_info
->tiling
);
129 if (!isl_format_supports_ccs_e(devinfo
, format
))
132 if (!(vk_info
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
))
135 const VkImageFormatListCreateInfoKHR
*fmt_list
=
136 vk_find_struct_const(vk_info
->pNext
, IMAGE_FORMAT_LIST_CREATE_INFO_KHR
);
138 if (!fmt_list
|| fmt_list
->viewFormatCount
== 0)
141 for (uint32_t i
= 0; i
< fmt_list
->viewFormatCount
; i
++) {
142 enum isl_format view_format
=
143 anv_get_isl_format(devinfo
, fmt_list
->pViewFormats
[i
],
144 VK_IMAGE_ASPECT_COLOR_BIT
, vk_info
->tiling
);
146 if (!isl_formats_are_ccs_e_compatible(devinfo
, format
, view_format
))
154 * For color images that have an auxiliary surface, request allocation for an
155 * additional buffer that mainly stores fast-clear values. Use of this buffer
156 * allows us to access the image's subresources while being aware of their
157 * fast-clear values in non-trivial cases (e.g., outside of a render pass in
158 * which a fast clear has occurred).
160 * For the purpose of discoverability, the algorithm used to manage this buffer
161 * is described here. A clear value in this buffer is updated when a fast clear
162 * is performed on a subresource. One of two synchronization operations is
163 * performed in order for a following memory access to use the fast-clear
165 * a. Copy the value from the buffer to the surface state object used for
166 * reading. This is done implicitly when the value is the clear value
167 * predetermined to be the default in other surface state objects. This
168 * is currently only done explicitly for the operation below.
169 * b. Do (a) and use the surface state object to resolve the subresource.
170 * This is only done during layout transitions for decent performance.
172 * With the above scheme, we can fast-clear whenever the hardware allows except
173 * for two cases in which synchronization becomes impossible or undesirable:
174 * * The subresource is in the GENERAL layout and is cleared to a value
175 * other than the special default value.
177 * Performing a synchronization operation in order to read from the
178 * subresource is undesirable in this case. Firstly, b) is not an option
179 * because a layout transition isn't required between a write and read of
180 * an image in the GENERAL layout. Secondly, it's undesirable to do a)
181 * explicitly because it would require large infrastructural changes. The
182 * Vulkan API supports us in deciding not to optimize this layout by
183 * stating that using this layout may cause suboptimal performance. NOTE:
184 * the auxiliary buffer must always be enabled to support a) implicitly.
187 * * For the given miplevel, only some of the layers are cleared at once.
189 * If the user clears each layer to a different value, then tries to
190 * render to multiple layers at once, we have no ability to perform a
191 * synchronization operation in between. a) is not helpful because the
192 * object can only hold one clear value. b) is not an option because a
193 * layout transition isn't required in this case.
196 add_fast_clear_state_buffer(struct anv_image
*image
,
197 const struct anv_device
*device
)
199 assert(image
&& device
);
200 assert(image
->aux_surface
.isl
.size
> 0 &&
201 image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
203 /* The offset to the buffer of clear values must be dword-aligned for GPU
204 * memcpy operations. It is located immediately after the auxiliary surface.
207 /* Tiled images are guaranteed to be 4K aligned, so the image alignment
208 * should also be dword-aligned.
210 assert(image
->alignment
% 4 == 0);
212 /* Auxiliary buffers should be a multiple of 4K, so the start of the clear
213 * values buffer should already be dword-aligned.
215 assert(image
->aux_surface
.isl
.size
% 4 == 0);
217 /* This buffer should be at the very end of the image. */
218 assert(image
->size
==
219 image
->aux_surface
.offset
+ image
->aux_surface
.isl
.size
);
221 const unsigned entry_size
= anv_fast_clear_state_entry_size(device
);
222 /* There's no padding between entries, so ensure that they're always a
223 * multiple of 32 bits in order to enable GPU memcpy operations.
225 assert(entry_size
% 4 == 0);
226 image
->size
+= entry_size
* anv_image_aux_levels(image
);
230 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
231 * image's memory requirements (that is, the image's size and alignment).
233 * Exactly one bit must be set in \a aspect.
236 make_surface(const struct anv_device
*dev
,
237 struct anv_image
*image
,
238 const struct anv_image_create_info
*anv_info
,
239 VkImageAspectFlags aspect
)
241 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
244 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
245 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
246 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
247 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
250 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
251 * result with an optionally provided ISL tiling argument.
253 isl_tiling_flags_t tiling_flags
=
254 (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) ?
255 ISL_TILING_LINEAR_BIT
: ISL_TILING_ANY_MASK
;
257 if (anv_info
->isl_tiling_flags
)
258 tiling_flags
&= anv_info
->isl_tiling_flags
;
260 assert(tiling_flags
);
262 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
264 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
267 enum isl_format format
= anv_get_isl_format(&dev
->info
, vk_info
->format
,
268 aspect
, vk_info
->tiling
);
269 assert(format
!= ISL_FORMAT_UNSUPPORTED
);
271 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
272 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
274 .width
= image
->extent
.width
,
275 .height
= image
->extent
.height
,
276 .depth
= image
->extent
.depth
,
277 .levels
= vk_info
->mipLevels
,
278 .array_len
= vk_info
->arrayLayers
,
279 .samples
= vk_info
->samples
,
281 .row_pitch
= anv_info
->stride
,
282 .usage
= choose_isl_surf_usage(vk_info
->flags
, image
->usage
, aspect
),
283 .tiling_flags
= tiling_flags
);
285 /* isl_surf_init() will fail only if provided invalid input. Invalid input
286 * is illegal in Vulkan.
290 add_surface(image
, anv_surf
);
292 /* Add a HiZ surface to a depth buffer that will be used for rendering.
294 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
295 /* We don't advertise that depth buffers could be used as storage
298 assert(!(image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
300 /* Allow the user to control HiZ enabling. Disable by default on gen7
301 * because resolves are not currently implemented pre-BDW.
303 if (!(image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
304 /* It will never be used as an attachment, HiZ is pointless. */
305 } else if (dev
->info
.gen
== 7) {
306 anv_perf_warn(dev
->instance
, image
, "Implement gen7 HiZ");
307 } else if (vk_info
->mipLevels
> 1) {
308 anv_perf_warn(dev
->instance
, image
, "Enable multi-LOD HiZ");
309 } else if (vk_info
->arrayLayers
> 1) {
310 anv_perf_warn(dev
->instance
, image
,
311 "Implement multi-arrayLayer HiZ clears and resolves");
312 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
313 anv_perf_warn(dev
->instance
, image
, "Enable gen8 multisampled HiZ");
314 } else if (!unlikely(INTEL_DEBUG
& DEBUG_NO_HIZ
)) {
315 assert(image
->aux_surface
.isl
.size
== 0);
316 ok
= isl_surf_get_hiz_surf(&dev
->isl_dev
, &image
->depth_surface
.isl
,
317 &image
->aux_surface
.isl
);
319 add_surface(image
, &image
->aux_surface
);
320 image
->aux_usage
= ISL_AUX_USAGE_HIZ
;
322 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
== 1) {
323 if (!unlikely(INTEL_DEBUG
& DEBUG_NO_RBC
)) {
324 assert(image
->aux_surface
.isl
.size
== 0);
325 ok
= isl_surf_get_ccs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
326 &image
->aux_surface
.isl
, 0);
329 /* Disable CCS when it is not useful (i.e., when you can't render
330 * to the image with CCS enabled).
332 if (!isl_format_supports_rendering(&dev
->info
, format
)) {
333 /* While it may be technically possible to enable CCS for this
334 * image, we currently don't have things hooked up to get it
337 anv_perf_warn(dev
->instance
, image
,
338 "This image format doesn't support rendering. "
339 "Not allocating an CCS buffer.");
340 image
->aux_surface
.isl
.size
= 0;
344 add_surface(image
, &image
->aux_surface
);
345 add_fast_clear_state_buffer(image
, dev
);
347 /* For images created without MUTABLE_FORMAT_BIT set, we know that
348 * they will always be used with the original format. In
349 * particular, they will always be used with a format that
350 * supports color compression. If it's never used as a storage
351 * image, then it will only be used through the sampler or the as
352 * a render target. This means that it's safe to just leave
353 * compression on at all times for these formats.
355 if (!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) &&
356 all_formats_ccs_e_compatible(&dev
->info
, vk_info
)) {
357 image
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
361 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
> 1) {
362 assert(image
->aux_surface
.isl
.size
== 0);
363 assert(!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
364 ok
= isl_surf_get_mcs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
365 &image
->aux_surface
.isl
);
367 add_surface(image
, &image
->aux_surface
);
368 add_fast_clear_state_buffer(image
, dev
);
369 image
->aux_usage
= ISL_AUX_USAGE_MCS
;
377 anv_image_create(VkDevice _device
,
378 const struct anv_image_create_info
*create_info
,
379 const VkAllocationCallbacks
* alloc
,
382 ANV_FROM_HANDLE(anv_device
, device
, _device
);
383 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
384 struct anv_image
*image
= NULL
;
387 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
389 anv_assert(pCreateInfo
->mipLevels
> 0);
390 anv_assert(pCreateInfo
->arrayLayers
> 0);
391 anv_assert(pCreateInfo
->samples
> 0);
392 anv_assert(pCreateInfo
->extent
.width
> 0);
393 anv_assert(pCreateInfo
->extent
.height
> 0);
394 anv_assert(pCreateInfo
->extent
.depth
> 0);
396 image
= vk_zalloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
397 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
399 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
401 image
->type
= pCreateInfo
->imageType
;
402 image
->extent
= pCreateInfo
->extent
;
403 image
->vk_format
= pCreateInfo
->format
;
404 image
->aspects
= vk_format_aspects(image
->vk_format
);
405 image
->levels
= pCreateInfo
->mipLevels
;
406 image
->array_size
= pCreateInfo
->arrayLayers
;
407 image
->samples
= pCreateInfo
->samples
;
408 image
->usage
= pCreateInfo
->usage
;
409 image
->tiling
= pCreateInfo
->tiling
;
410 image
->aux_usage
= ISL_AUX_USAGE_NONE
;
413 for_each_bit(b
, image
->aspects
) {
414 r
= make_surface(device
, image
, create_info
, (1 << b
));
419 *pImage
= anv_image_to_handle(image
);
425 vk_free2(&device
->alloc
, alloc
, image
);
431 anv_CreateImage(VkDevice device
,
432 const VkImageCreateInfo
*pCreateInfo
,
433 const VkAllocationCallbacks
*pAllocator
,
436 return anv_image_create(device
,
437 &(struct anv_image_create_info
) {
438 .vk_info
= pCreateInfo
,
445 anv_DestroyImage(VkDevice _device
, VkImage _image
,
446 const VkAllocationCallbacks
*pAllocator
)
448 ANV_FROM_HANDLE(anv_device
, device
, _device
);
449 ANV_FROM_HANDLE(anv_image
, image
, _image
);
454 vk_free2(&device
->alloc
, pAllocator
, image
);
458 anv_bind_image_memory(const VkBindImageMemoryInfoKHR
*pBindInfo
)
460 ANV_FROM_HANDLE(anv_device_memory
, mem
, pBindInfo
->memory
);
461 ANV_FROM_HANDLE(anv_image
, image
, pBindInfo
->image
);
463 assert(pBindInfo
->sType
== VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO_KHR
);
472 image
->offset
= pBindInfo
->memoryOffset
;
475 VkResult
anv_BindImageMemory(
478 VkDeviceMemory memory
,
479 VkDeviceSize memoryOffset
)
481 anv_bind_image_memory(
482 &(VkBindImageMemoryInfoKHR
) {
483 .sType
= VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO_KHR
,
486 .memoryOffset
= memoryOffset
,
492 VkResult
anv_BindImageMemory2KHR(
494 uint32_t bindInfoCount
,
495 const VkBindImageMemoryInfoKHR
* pBindInfos
)
497 for (uint32_t i
= 0; i
< bindInfoCount
; i
++)
498 anv_bind_image_memory(&pBindInfos
[i
]);
504 anv_surface_get_subresource_layout(struct anv_image
*image
,
505 struct anv_surface
*surface
,
506 const VkImageSubresource
*subresource
,
507 VkSubresourceLayout
*layout
)
509 /* If we are on a non-zero mip level or array slice, we need to
510 * calculate a real offset.
512 anv_assert(subresource
->mipLevel
== 0);
513 anv_assert(subresource
->arrayLayer
== 0);
515 layout
->offset
= surface
->offset
;
516 layout
->rowPitch
= surface
->isl
.row_pitch
;
517 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
518 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
519 layout
->size
= surface
->isl
.size
;
522 void anv_GetImageSubresourceLayout(
525 const VkImageSubresource
* pSubresource
,
526 VkSubresourceLayout
* pLayout
)
528 ANV_FROM_HANDLE(anv_image
, image
, _image
);
530 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
532 switch (pSubresource
->aspectMask
) {
533 case VK_IMAGE_ASPECT_COLOR_BIT
:
534 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
535 pSubresource
, pLayout
);
537 case VK_IMAGE_ASPECT_DEPTH_BIT
:
538 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
539 pSubresource
, pLayout
);
541 case VK_IMAGE_ASPECT_STENCIL_BIT
:
542 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
543 pSubresource
, pLayout
);
546 assert(!"Invalid image aspect");
551 * This function determines the optimal buffer to use for a given
552 * VkImageLayout and other pieces of information needed to make that
553 * determination. This does not determine the optimal buffer to use
554 * during a resolve operation.
556 * @param devinfo The device information of the Intel GPU.
557 * @param image The image that may contain a collection of buffers.
558 * @param aspects The aspect(s) of the image to be accessed.
559 * @param layout The current layout of the image aspect(s).
561 * @return The primary buffer that should be used for the given layout.
564 anv_layout_to_aux_usage(const struct gen_device_info
* const devinfo
,
565 const struct anv_image
* const image
,
566 const VkImageAspectFlags aspects
,
567 const VkImageLayout layout
)
569 /* Validate the inputs. */
571 /* The devinfo is needed as the optimal buffer varies across generations. */
572 assert(devinfo
!= NULL
);
574 /* The layout of a NULL image is not properly defined. */
575 assert(image
!= NULL
);
577 /* The aspects must be a subset of the image aspects. */
578 assert(aspects
& image
->aspects
&& aspects
<= image
->aspects
);
580 /* Determine the optimal buffer. */
582 /* If there is no auxiliary surface allocated, we must use the one and only
585 if (image
->aux_surface
.isl
.size
== 0)
586 return ISL_AUX_USAGE_NONE
;
588 /* All images that use an auxiliary surface are required to be tiled. */
589 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
591 /* On BDW+, when clearing the stencil aspect of a depth stencil image,
592 * the HiZ buffer allows us to record the clear with a relatively small
593 * number of packets. Prior to BDW, the HiZ buffer provides no known benefit
594 * to the stencil aspect.
596 if (devinfo
->gen
< 8 && aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
)
597 return ISL_AUX_USAGE_NONE
;
599 const bool color_aspect
= aspects
== VK_IMAGE_ASPECT_COLOR_BIT
;
601 /* The following switch currently only handles depth stencil aspects.
602 * TODO: Handle the color aspect.
605 return image
->aux_usage
;
609 /* Invalid Layouts */
610 case VK_IMAGE_LAYOUT_RANGE_SIZE
:
611 case VK_IMAGE_LAYOUT_MAX_ENUM
:
612 unreachable("Invalid image layout.");
616 * The pre-initialized layout is equivalent to the undefined layout for
617 * optimally-tiled images. We can only do color compression (CCS or HiZ)
620 case VK_IMAGE_LAYOUT_UNDEFINED
:
621 case VK_IMAGE_LAYOUT_PREINITIALIZED
:
622 return ISL_AUX_USAGE_NONE
;
627 * This buffer could be a depth buffer used in a transfer operation. BLORP
628 * currently doesn't use HiZ for transfer operations so we must use the main
629 * buffer for this layout. TODO: Enable HiZ in BLORP.
631 case VK_IMAGE_LAYOUT_GENERAL
:
632 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
:
633 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
:
634 return ISL_AUX_USAGE_NONE
;
637 /* Sampling Layouts */
638 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
:
639 assert(!color_aspect
);
641 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
:
642 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL_KHR
:
643 if (anv_can_sample_with_hiz(devinfo
, aspects
, image
->samples
))
644 return ISL_AUX_USAGE_HIZ
;
646 return ISL_AUX_USAGE_NONE
;
648 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
:
649 assert(color_aspect
);
651 /* On SKL+, the render buffer can be decompressed by the presentation
652 * engine. Support for this feature has not yet landed in the wider
653 * ecosystem. TODO: Update this code when support lands.
655 * From the BDW PRM, Vol 7, Render Target Resolve:
657 * If the MCS is enabled on a non-multisampled render target, the
658 * render target must be resolved before being used for other
659 * purposes (display, texture, CPU lock) The clear value from
660 * SURFACE_STATE is written into pixels in the render target
661 * indicated as clear in the MCS.
663 * Pre-SKL, the render buffer must be resolved before being used for
664 * presentation. We can infer that the auxiliary buffer is not used.
666 return ISL_AUX_USAGE_NONE
;
669 /* Rendering Layouts */
670 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
:
671 assert(color_aspect
);
672 unreachable("Color images are not yet supported.");
674 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
:
675 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR
:
676 assert(!color_aspect
);
677 return ISL_AUX_USAGE_HIZ
;
679 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR
:
680 unreachable("VK_KHR_shared_presentable_image is unsupported");
683 /* If the layout isn't recognized in the exhaustive switch above, the
684 * VkImageLayout value is not defined in vulkan.h.
686 unreachable("layout is not a VkImageLayout enumeration member.");
690 static struct anv_state
691 alloc_surface_state(struct anv_device
*device
)
693 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
696 static enum isl_channel_select
697 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
698 struct isl_swizzle format_swizzle
)
700 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
704 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
705 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
706 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
707 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
708 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
709 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
711 unreachable("Invalid swizzle");
716 anv_image_fill_surface_state(struct anv_device
*device
,
717 const struct anv_image
*image
,
718 VkImageAspectFlagBits aspect
,
719 const struct isl_view
*view_in
,
720 isl_surf_usage_flags_t view_usage
,
721 enum isl_aux_usage aux_usage
,
722 const union isl_color_value
*clear_color
,
723 enum anv_image_view_state_flags flags
,
724 struct anv_state
*state
,
725 struct brw_image_param
*image_param_out
)
727 const struct anv_surface
*surface
=
728 anv_image_get_surface_for_aspect_mask(image
, aspect
);
730 struct isl_view view
= *view_in
;
731 view
.usage
|= view_usage
;
733 if (view_usage
== ISL_SURF_USAGE_RENDER_TARGET_BIT
)
734 view
.swizzle
= anv_swizzle_for_render(view
.swizzle
);
736 /* If this is a HiZ buffer we can sample from with a programmable clear
737 * value (SKL+), define the clear value to the optimal constant.
739 union isl_color_value default_clear_color
= { .u32
= { 0, } };
740 if (device
->info
.gen
>= 9 && aux_usage
== ISL_AUX_USAGE_HIZ
)
741 default_clear_color
.f32
[0] = ANV_HZ_FC_VAL
;
743 clear_color
= &default_clear_color
;
745 if (view_usage
== ISL_SURF_USAGE_STORAGE_BIT
&&
746 !(flags
& ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
) &&
747 !isl_has_matching_typed_storage_image_format(&device
->info
,
749 /* In this case, we are a writeable storage buffer which needs to be
750 * lowered to linear. All tiling and offset calculations will be done in
753 assert(aux_usage
== ISL_AUX_USAGE_NONE
);
754 isl_buffer_fill_state(&device
->isl_dev
, state
->map
,
755 .size
= surface
->isl
.size
,
756 .format
= ISL_FORMAT_RAW
,
758 .mocs
= device
->default_mocs
);
760 if (view_usage
== ISL_SURF_USAGE_STORAGE_BIT
&&
761 !(flags
& ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
)) {
762 /* Typed surface reads support a very limited subset of the shader
763 * image formats. Translate it into the closest format the hardware
766 assert(aux_usage
== ISL_AUX_USAGE_NONE
);
767 view
.format
= isl_lower_storage_image_format(&device
->info
,
771 isl_surf_fill_state(&device
->isl_dev
, state
->map
,
772 .surf
= &surface
->isl
,
774 .clear_color
= *clear_color
,
775 .aux_surf
= &image
->aux_surface
.isl
,
776 .aux_usage
= aux_usage
,
777 .mocs
= device
->default_mocs
);
780 anv_state_flush(device
, *state
);
782 if (image_param_out
) {
783 assert(view_usage
== ISL_SURF_USAGE_STORAGE_BIT
);
784 isl_surf_fill_image_param(&device
->isl_dev
, image_param_out
,
785 &surface
->isl
, &view
);
790 anv_CreateImageView(VkDevice _device
,
791 const VkImageViewCreateInfo
*pCreateInfo
,
792 const VkAllocationCallbacks
*pAllocator
,
795 ANV_FROM_HANDLE(anv_device
, device
, _device
);
796 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
797 struct anv_image_view
*iview
;
799 iview
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*iview
), 8,
800 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
802 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
804 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
806 assert(range
->layerCount
> 0);
807 assert(range
->baseMipLevel
< image
->levels
);
809 const VkImageViewUsageCreateInfoKHR
*usage_info
=
810 vk_find_struct_const(pCreateInfo
, IMAGE_VIEW_USAGE_CREATE_INFO_KHR
);
811 VkImageUsageFlags view_usage
= usage_info
? usage_info
->usage
: image
->usage
;
812 /* View usage should be a subset of image usage */
813 assert((view_usage
& ~image
->usage
) == 0);
814 assert(view_usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
815 VK_IMAGE_USAGE_STORAGE_BIT
|
816 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
817 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
|
818 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
820 switch (image
->type
) {
822 unreachable("bad VkImageType");
823 case VK_IMAGE_TYPE_1D
:
824 case VK_IMAGE_TYPE_2D
:
825 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
827 case VK_IMAGE_TYPE_3D
:
828 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
829 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
833 const struct anv_surface
*surface
=
834 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
836 iview
->image
= image
;
837 iview
->bo
= image
->bo
;
838 iview
->offset
= image
->offset
+ surface
->offset
;
840 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
841 iview
->vk_format
= pCreateInfo
->format
;
843 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
844 range
->aspectMask
, image
->tiling
);
846 iview
->isl
= (struct isl_view
) {
847 .format
= format
.isl_format
,
848 .base_level
= range
->baseMipLevel
,
849 .levels
= anv_get_levelCount(image
, range
),
850 .base_array_layer
= range
->baseArrayLayer
,
851 .array_len
= anv_get_layerCount(image
, range
),
853 .r
= remap_swizzle(pCreateInfo
->components
.r
,
854 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
855 .g
= remap_swizzle(pCreateInfo
->components
.g
,
856 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
857 .b
= remap_swizzle(pCreateInfo
->components
.b
,
858 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
859 .a
= remap_swizzle(pCreateInfo
->components
.a
,
860 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
864 iview
->extent
= (VkExtent3D
) {
865 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
866 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
867 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
870 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_3D
) {
871 iview
->isl
.base_array_layer
= 0;
872 iview
->isl
.array_len
= iview
->extent
.depth
;
875 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
876 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
877 iview
->isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
879 iview
->isl
.usage
= 0;
882 /* Input attachment surfaces for color are allocated and filled
883 * out at BeginRenderPass time because they need compression information.
884 * Compression is not yet enabled for depth textures and stencil doesn't
885 * allow compression so we can just use the texture surface state from the
888 if (view_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
||
889 (view_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
&&
890 !(iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
))) {
891 iview
->optimal_sampler_surface_state
= alloc_surface_state(device
);
892 iview
->general_sampler_surface_state
= alloc_surface_state(device
);
894 iview
->general_sampler_aux_usage
=
895 anv_layout_to_aux_usage(&device
->info
, image
, iview
->aspect_mask
,
896 VK_IMAGE_LAYOUT_GENERAL
);
897 iview
->optimal_sampler_aux_usage
=
898 anv_layout_to_aux_usage(&device
->info
, image
, iview
->aspect_mask
,
899 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
901 anv_image_fill_surface_state(device
, image
, iview
->aspect_mask
,
902 &iview
->isl
, ISL_SURF_USAGE_TEXTURE_BIT
,
903 iview
->optimal_sampler_aux_usage
, NULL
,
904 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL
,
905 &iview
->optimal_sampler_surface_state
,
908 anv_image_fill_surface_state(device
, image
, iview
->aspect_mask
,
909 &iview
->isl
, ISL_SURF_USAGE_TEXTURE_BIT
,
910 iview
->general_sampler_aux_usage
, NULL
,
912 &iview
->general_sampler_surface_state
,
916 /* NOTE: This one needs to go last since it may stomp isl_view.format */
917 if (view_usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
918 iview
->storage_surface_state
= alloc_surface_state(device
);
919 iview
->writeonly_storage_surface_state
= alloc_surface_state(device
);
921 anv_image_fill_surface_state(device
, image
, iview
->aspect_mask
,
922 &iview
->isl
, ISL_SURF_USAGE_STORAGE_BIT
,
923 ISL_AUX_USAGE_NONE
, NULL
,
925 &iview
->storage_surface_state
,
926 &iview
->storage_image_param
);
928 anv_image_fill_surface_state(device
, image
, iview
->aspect_mask
,
929 &iview
->isl
, ISL_SURF_USAGE_STORAGE_BIT
,
930 ISL_AUX_USAGE_NONE
, NULL
,
931 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY
,
932 &iview
->writeonly_storage_surface_state
,
936 *pView
= anv_image_view_to_handle(iview
);
942 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
943 const VkAllocationCallbacks
*pAllocator
)
945 ANV_FROM_HANDLE(anv_device
, device
, _device
);
946 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
951 if (iview
->optimal_sampler_surface_state
.alloc_size
> 0) {
952 anv_state_pool_free(&device
->surface_state_pool
,
953 iview
->optimal_sampler_surface_state
);
956 if (iview
->general_sampler_surface_state
.alloc_size
> 0) {
957 anv_state_pool_free(&device
->surface_state_pool
,
958 iview
->general_sampler_surface_state
);
961 if (iview
->storage_surface_state
.alloc_size
> 0) {
962 anv_state_pool_free(&device
->surface_state_pool
,
963 iview
->storage_surface_state
);
966 if (iview
->writeonly_storage_surface_state
.alloc_size
> 0) {
967 anv_state_pool_free(&device
->surface_state_pool
,
968 iview
->writeonly_storage_surface_state
);
971 vk_free2(&device
->alloc
, pAllocator
, iview
);
976 anv_CreateBufferView(VkDevice _device
,
977 const VkBufferViewCreateInfo
*pCreateInfo
,
978 const VkAllocationCallbacks
*pAllocator
,
981 ANV_FROM_HANDLE(anv_device
, device
, _device
);
982 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
983 struct anv_buffer_view
*view
;
985 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
986 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
988 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
990 /* TODO: Handle the format swizzle? */
992 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
993 VK_IMAGE_ASPECT_COLOR_BIT
,
994 VK_IMAGE_TILING_LINEAR
);
995 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
996 view
->bo
= buffer
->bo
;
997 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
998 view
->range
= anv_buffer_get_range(buffer
, pCreateInfo
->offset
,
1000 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
1002 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
1003 view
->surface_state
= alloc_surface_state(device
);
1005 anv_fill_buffer_surface_state(device
, view
->surface_state
,
1007 view
->offset
, view
->range
, format_bs
);
1009 view
->surface_state
= (struct anv_state
){ 0 };
1012 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
1013 view
->storage_surface_state
= alloc_surface_state(device
);
1014 view
->writeonly_storage_surface_state
= alloc_surface_state(device
);
1016 enum isl_format storage_format
=
1017 isl_has_matching_typed_storage_image_format(&device
->info
,
1019 isl_lower_storage_image_format(&device
->info
, view
->format
) :
1022 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
1024 view
->offset
, view
->range
,
1025 (storage_format
== ISL_FORMAT_RAW
? 1 :
1026 isl_format_get_layout(storage_format
)->bpb
/ 8));
1028 /* Write-only accesses should use the original format. */
1029 anv_fill_buffer_surface_state(device
, view
->writeonly_storage_surface_state
,
1031 view
->offset
, view
->range
,
1032 isl_format_get_layout(view
->format
)->bpb
/ 8);
1034 isl_buffer_fill_image_param(&device
->isl_dev
,
1035 &view
->storage_image_param
,
1036 view
->format
, view
->range
);
1038 view
->storage_surface_state
= (struct anv_state
){ 0 };
1039 view
->writeonly_storage_surface_state
= (struct anv_state
){ 0 };
1042 *pView
= anv_buffer_view_to_handle(view
);
1048 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
1049 const VkAllocationCallbacks
*pAllocator
)
1051 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1052 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
1057 if (view
->surface_state
.alloc_size
> 0)
1058 anv_state_pool_free(&device
->surface_state_pool
,
1059 view
->surface_state
);
1061 if (view
->storage_surface_state
.alloc_size
> 0)
1062 anv_state_pool_free(&device
->surface_state_pool
,
1063 view
->storage_surface_state
);
1065 if (view
->writeonly_storage_surface_state
.alloc_size
> 0)
1066 anv_state_pool_free(&device
->surface_state_pool
,
1067 view
->writeonly_storage_surface_state
);
1069 vk_free2(&device
->alloc
, pAllocator
, view
);
1072 const struct anv_surface
*
1073 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
1074 VkImageAspectFlags aspect_mask
)
1076 switch (aspect_mask
) {
1077 case VK_IMAGE_ASPECT_COLOR_BIT
:
1078 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
1079 return &image
->color_surface
;
1080 case VK_IMAGE_ASPECT_DEPTH_BIT
:
1081 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
1082 return &image
->depth_surface
;
1083 case VK_IMAGE_ASPECT_STENCIL_BIT
:
1084 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
1085 return &image
->stencil_surface
;
1086 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
1087 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1088 * combined depth stencil formats. Specifically, it states:
1090 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1091 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1093 * Image views with both depth and stencil aspects are only valid for
1094 * render target attachments, in which case
1095 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1096 * stencil surfaces from the underlying surface.
1098 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
1099 return &image
->depth_surface
;
1101 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
1102 return &image
->stencil_surface
;
1105 unreachable("image does not have aspect");