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"
34 #include "vk_format_info.h"
37 * Exactly one bit must be set in \a aspect.
39 static isl_surf_usage_flags_t
40 choose_isl_surf_usage(VkImageUsageFlags vk_usage
,
41 VkImageAspectFlags aspect
)
43 isl_surf_usage_flags_t isl_usage
= 0;
45 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
46 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
48 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
49 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
51 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
52 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
54 if (vk_usage
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
55 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
57 /* Even if we're only using it for transfer operations, clears to depth and
58 * stencil images happen as depth and stencil so they need the right ISL
59 * usage bits or else things will fall apart.
62 case VK_IMAGE_ASPECT_DEPTH_BIT
:
63 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
65 case VK_IMAGE_ASPECT_STENCIL_BIT
:
66 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
68 case VK_IMAGE_ASPECT_COLOR_BIT
:
71 unreachable("bad VkImageAspect");
74 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
75 /* blorp implements transfers by sampling from the source image. */
76 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
79 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
&&
80 aspect
== VK_IMAGE_ASPECT_COLOR_BIT
) {
81 /* blorp implements transfers by rendering into the destination image.
82 * Only request this with color images, as we deal with depth/stencil
83 * formats differently. */
84 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
91 * Exactly one bit must be set in \a aspect.
93 static struct anv_surface
*
94 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
98 unreachable("bad VkImageAspect");
99 case VK_IMAGE_ASPECT_COLOR_BIT
:
100 return &image
->color_surface
;
101 case VK_IMAGE_ASPECT_DEPTH_BIT
:
102 return &image
->depth_surface
;
103 case VK_IMAGE_ASPECT_STENCIL_BIT
:
104 return &image
->stencil_surface
;
109 add_surface(struct anv_image
*image
, struct anv_surface
*surf
)
111 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
113 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
114 image
->size
= surf
->offset
+ surf
->isl
.size
;
115 image
->alignment
= MAX2(image
->alignment
, surf
->isl
.alignment
);
119 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
120 * image's memory requirements (that is, the image's size and alignment).
122 * Exactly one bit must be set in \a aspect.
125 make_surface(const struct anv_device
*dev
,
126 struct anv_image
*image
,
127 const struct anv_image_create_info
*anv_info
,
128 VkImageAspectFlags aspect
)
130 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
133 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
134 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
135 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
136 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
139 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
140 * result with an optionally provided ISL tiling argument.
142 isl_tiling_flags_t tiling_flags
=
143 (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) ?
144 ISL_TILING_LINEAR_BIT
: ISL_TILING_ANY_MASK
;
146 if (anv_info
->isl_tiling_flags
)
147 tiling_flags
&= anv_info
->isl_tiling_flags
;
149 assert(tiling_flags
);
151 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
153 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
156 enum isl_format format
= anv_get_isl_format(&dev
->info
, vk_info
->format
,
157 aspect
, vk_info
->tiling
);
158 assert(format
!= ISL_FORMAT_UNSUPPORTED
);
160 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
161 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
163 .width
= image
->extent
.width
,
164 .height
= image
->extent
.height
,
165 .depth
= image
->extent
.depth
,
166 .levels
= vk_info
->mipLevels
,
167 .array_len
= vk_info
->arrayLayers
,
168 .samples
= vk_info
->samples
,
170 .row_pitch
= anv_info
->stride
,
171 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
172 .tiling_flags
= tiling_flags
);
174 /* isl_surf_init() will fail only if provided invalid input. Invalid input
175 * is illegal in Vulkan.
179 add_surface(image
, anv_surf
);
181 /* Add a HiZ surface to a depth buffer that will be used for rendering.
183 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
184 /* We don't advertise that depth buffers could be used as storage
187 assert(!(image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
189 /* Allow the user to control HiZ enabling. Disable by default on gen7
190 * because resolves are not currently implemented pre-BDW.
192 if (!(image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
193 /* It will never be used as an attachment, HiZ is pointless. */
194 } else if (dev
->info
.gen
== 7) {
195 anv_perf_warn("Implement gen7 HiZ");
196 } else if (vk_info
->mipLevels
> 1) {
197 anv_perf_warn("Enable multi-LOD HiZ");
198 } else if (vk_info
->arrayLayers
> 1) {
199 anv_perf_warn("Implement multi-arrayLayer HiZ clears and resolves");
200 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
201 anv_perf_warn("Enable gen8 multisampled HiZ");
202 } else if (!unlikely(INTEL_DEBUG
& DEBUG_NO_HIZ
)) {
203 assert(image
->aux_surface
.isl
.size
== 0);
204 ok
= isl_surf_get_hiz_surf(&dev
->isl_dev
, &image
->depth_surface
.isl
,
205 &image
->aux_surface
.isl
);
207 add_surface(image
, &image
->aux_surface
);
208 image
->aux_usage
= ISL_AUX_USAGE_HIZ
;
210 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
== 1) {
211 if (!unlikely(INTEL_DEBUG
& DEBUG_NO_RBC
)) {
212 assert(image
->aux_surface
.isl
.size
== 0);
213 ok
= isl_surf_get_ccs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
214 &image
->aux_surface
.isl
);
216 add_surface(image
, &image
->aux_surface
);
218 /* For images created without MUTABLE_FORMAT_BIT set, we know that
219 * they will always be used with the original format. In
220 * particular, they will always be used with a format that
221 * supports color compression. If it's never used as a storage
222 * image, then it will only be used through the sampler or the as
223 * a render target. This means that it's safe to just leave
224 * compression on at all times for these formats.
226 if (!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) &&
227 !(vk_info
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) &&
228 isl_format_supports_ccs_e(&dev
->info
, format
)) {
229 image
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
233 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
> 1) {
234 assert(image
->aux_surface
.isl
.size
== 0);
235 assert(!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
236 ok
= isl_surf_get_mcs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
237 &image
->aux_surface
.isl
);
239 add_surface(image
, &image
->aux_surface
);
240 image
->aux_usage
= ISL_AUX_USAGE_MCS
;
248 anv_image_create(VkDevice _device
,
249 const struct anv_image_create_info
*create_info
,
250 const VkAllocationCallbacks
* alloc
,
253 ANV_FROM_HANDLE(anv_device
, device
, _device
);
254 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
255 struct anv_image
*image
= NULL
;
258 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
260 anv_assert(pCreateInfo
->mipLevels
> 0);
261 anv_assert(pCreateInfo
->arrayLayers
> 0);
262 anv_assert(pCreateInfo
->samples
> 0);
263 anv_assert(pCreateInfo
->extent
.width
> 0);
264 anv_assert(pCreateInfo
->extent
.height
> 0);
265 anv_assert(pCreateInfo
->extent
.depth
> 0);
267 image
= vk_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
268 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
270 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
272 memset(image
, 0, sizeof(*image
));
273 image
->type
= pCreateInfo
->imageType
;
274 image
->extent
= pCreateInfo
->extent
;
275 image
->vk_format
= pCreateInfo
->format
;
276 image
->aspects
= vk_format_aspects(image
->vk_format
);
277 image
->levels
= pCreateInfo
->mipLevels
;
278 image
->array_size
= pCreateInfo
->arrayLayers
;
279 image
->samples
= pCreateInfo
->samples
;
280 image
->usage
= pCreateInfo
->usage
;
281 image
->tiling
= pCreateInfo
->tiling
;
282 image
->aux_usage
= ISL_AUX_USAGE_NONE
;
285 for_each_bit(b
, image
->aspects
) {
286 r
= make_surface(device
, image
, create_info
, (1 << b
));
291 *pImage
= anv_image_to_handle(image
);
297 vk_free2(&device
->alloc
, alloc
, image
);
303 anv_CreateImage(VkDevice device
,
304 const VkImageCreateInfo
*pCreateInfo
,
305 const VkAllocationCallbacks
*pAllocator
,
308 return anv_image_create(device
,
309 &(struct anv_image_create_info
) {
310 .vk_info
= pCreateInfo
,
317 anv_DestroyImage(VkDevice _device
, VkImage _image
,
318 const VkAllocationCallbacks
*pAllocator
)
320 ANV_FROM_HANDLE(anv_device
, device
, _device
);
321 ANV_FROM_HANDLE(anv_image
, image
, _image
);
326 vk_free2(&device
->alloc
, pAllocator
, image
);
329 VkResult
anv_BindImageMemory(
332 VkDeviceMemory _memory
,
333 VkDeviceSize memoryOffset
)
335 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
336 ANV_FROM_HANDLE(anv_image
, image
, _image
);
345 image
->offset
= memoryOffset
;
351 anv_surface_get_subresource_layout(struct anv_image
*image
,
352 struct anv_surface
*surface
,
353 const VkImageSubresource
*subresource
,
354 VkSubresourceLayout
*layout
)
356 /* If we are on a non-zero mip level or array slice, we need to
357 * calculate a real offset.
359 anv_assert(subresource
->mipLevel
== 0);
360 anv_assert(subresource
->arrayLayer
== 0);
362 layout
->offset
= surface
->offset
;
363 layout
->rowPitch
= surface
->isl
.row_pitch
;
364 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
365 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
366 layout
->size
= surface
->isl
.size
;
369 void anv_GetImageSubresourceLayout(
372 const VkImageSubresource
* pSubresource
,
373 VkSubresourceLayout
* pLayout
)
375 ANV_FROM_HANDLE(anv_image
, image
, _image
);
377 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
379 switch (pSubresource
->aspectMask
) {
380 case VK_IMAGE_ASPECT_COLOR_BIT
:
381 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
382 pSubresource
, pLayout
);
384 case VK_IMAGE_ASPECT_DEPTH_BIT
:
385 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
386 pSubresource
, pLayout
);
388 case VK_IMAGE_ASPECT_STENCIL_BIT
:
389 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
390 pSubresource
, pLayout
);
393 assert(!"Invalid image aspect");
398 * This function determines the optimal buffer to use for a given
399 * VkImageLayout and other pieces of information needed to make that
400 * determination. This does not determine the optimal buffer to use
401 * during a resolve operation.
403 * @param devinfo The device information of the Intel GPU.
404 * @param image The image that may contain a collection of buffers.
405 * @param aspects The aspect(s) of the image to be accessed.
406 * @param layout The current layout of the image aspect(s).
408 * @return The primary buffer that should be used for the given layout.
411 anv_layout_to_aux_usage(const struct gen_device_info
* const devinfo
,
412 const struct anv_image
* const image
,
413 const VkImageAspectFlags aspects
,
414 const VkImageLayout layout
)
416 /* Validate the inputs. */
418 /* The devinfo is needed as the optimal buffer varies across generations. */
419 assert(devinfo
!= NULL
);
421 /* The layout of a NULL image is not properly defined. */
422 assert(image
!= NULL
);
424 /* The aspects must be a subset of the image aspects. */
425 assert(aspects
& image
->aspects
&& aspects
<= image
->aspects
);
427 /* Determine the optimal buffer. */
429 /* If there is no auxiliary surface allocated, we must use the one and only
432 if (image
->aux_surface
.isl
.size
== 0)
433 return ISL_AUX_USAGE_NONE
;
435 /* All images that use an auxiliary surface are required to be tiled. */
436 assert(image
->tiling
== VK_IMAGE_TILING_OPTIMAL
);
438 /* On BDW+, when clearing the stencil aspect of a depth stencil image,
439 * the HiZ buffer allows us to record the clear with a relatively small
440 * number of packets. Prior to BDW, the HiZ buffer provides no known benefit
441 * to the stencil aspect.
443 if (devinfo
->gen
< 8 && aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
)
444 return ISL_AUX_USAGE_NONE
;
446 const bool color_aspect
= aspects
== VK_IMAGE_ASPECT_COLOR_BIT
;
448 /* The following switch currently only handles depth stencil aspects.
449 * TODO: Handle the color aspect.
452 return image
->aux_usage
;
456 /* Invalid Layouts */
457 case VK_IMAGE_LAYOUT_RANGE_SIZE
:
458 case VK_IMAGE_LAYOUT_MAX_ENUM
:
459 unreachable("Invalid image layout.");
463 * The pre-initialized layout is equivalent to the undefined layout for
464 * optimally-tiled images. We can only do color compression (CCS or HiZ)
467 case VK_IMAGE_LAYOUT_UNDEFINED
:
468 case VK_IMAGE_LAYOUT_PREINITIALIZED
:
469 return ISL_AUX_USAGE_NONE
;
474 * This buffer could be a depth buffer used in a transfer operation. BLORP
475 * currently doesn't use HiZ for transfer operations so we must use the main
476 * buffer for this layout. TODO: Enable HiZ in BLORP.
478 case VK_IMAGE_LAYOUT_GENERAL
:
479 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
:
480 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
:
481 return ISL_AUX_USAGE_NONE
;
484 /* Sampling Layouts */
485 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL
:
486 assert(!color_aspect
);
488 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
:
489 if (anv_can_sample_with_hiz(devinfo
, aspects
, image
->samples
))
490 return ISL_AUX_USAGE_HIZ
;
492 return ISL_AUX_USAGE_NONE
;
494 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
:
495 assert(color_aspect
);
497 /* On SKL+, the render buffer can be decompressed by the presentation
498 * engine. Support for this feature has not yet landed in the wider
499 * ecosystem. TODO: Update this code when support lands.
501 * From the BDW PRM, Vol 7, Render Target Resolve:
503 * If the MCS is enabled on a non-multisampled render target, the
504 * render target must be resolved before being used for other
505 * purposes (display, texture, CPU lock) The clear value from
506 * SURFACE_STATE is written into pixels in the render target
507 * indicated as clear in the MCS.
509 * Pre-SKL, the render buffer must be resolved before being used for
510 * presentation. We can infer that the auxiliary buffer is not used.
512 return ISL_AUX_USAGE_NONE
;
515 /* Rendering Layouts */
516 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
:
517 assert(color_aspect
);
518 unreachable("Color images are not yet supported.");
520 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
:
521 assert(!color_aspect
);
522 return ISL_AUX_USAGE_HIZ
;
524 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR
:
525 unreachable("VK_KHR_shared_presentable_image is unsupported");
528 /* If the layout isn't recognized in the exhaustive switch above, the
529 * VkImageLayout value is not defined in vulkan.h.
531 unreachable("layout is not a VkImageLayout enumeration member.");
535 static struct anv_state
536 alloc_surface_state(struct anv_device
*device
)
538 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
541 static enum isl_channel_select
542 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
543 struct isl_swizzle format_swizzle
)
545 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
549 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
550 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
551 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
552 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
553 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
554 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
556 unreachable("Invalid swizzle");
562 anv_CreateImageView(VkDevice _device
,
563 const VkImageViewCreateInfo
*pCreateInfo
,
564 const VkAllocationCallbacks
*pAllocator
,
567 ANV_FROM_HANDLE(anv_device
, device
, _device
);
568 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
569 struct anv_image_view
*iview
;
571 iview
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*iview
), 8,
572 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
574 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
576 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
578 assert(range
->layerCount
> 0);
579 assert(range
->baseMipLevel
< image
->levels
);
580 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
581 VK_IMAGE_USAGE_STORAGE_BIT
|
582 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
583 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
585 switch (image
->type
) {
587 unreachable("bad VkImageType");
588 case VK_IMAGE_TYPE_1D
:
589 case VK_IMAGE_TYPE_2D
:
590 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
592 case VK_IMAGE_TYPE_3D
:
593 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
594 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
598 const struct anv_surface
*surface
=
599 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
601 iview
->image
= image
;
602 iview
->bo
= image
->bo
;
603 iview
->offset
= image
->offset
+ surface
->offset
;
605 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
606 iview
->vk_format
= pCreateInfo
->format
;
608 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
609 range
->aspectMask
, image
->tiling
);
611 iview
->isl
= (struct isl_view
) {
612 .format
= format
.isl_format
,
613 .base_level
= range
->baseMipLevel
,
614 .levels
= anv_get_levelCount(image
, range
),
615 .base_array_layer
= range
->baseArrayLayer
,
616 .array_len
= anv_get_layerCount(image
, range
),
618 .r
= remap_swizzle(pCreateInfo
->components
.r
,
619 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
620 .g
= remap_swizzle(pCreateInfo
->components
.g
,
621 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
622 .b
= remap_swizzle(pCreateInfo
->components
.b
,
623 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
624 .a
= remap_swizzle(pCreateInfo
->components
.a
,
625 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
629 iview
->extent
= (VkExtent3D
) {
630 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
631 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
632 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
635 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_3D
) {
636 iview
->isl
.base_array_layer
= 0;
637 iview
->isl
.array_len
= iview
->extent
.depth
;
640 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
641 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
642 iview
->isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
644 iview
->isl
.usage
= 0;
647 /* Input attachment surfaces for color are allocated and filled
648 * out at BeginRenderPass time because they need compression information.
649 * Compression is not yet enabled for depth textures and stencil doesn't
650 * allow compression so we can just use the texture surface state from the
653 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
||
654 (image
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
&&
655 !(iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
))) {
656 iview
->sampler_surface_state
= alloc_surface_state(device
);
657 iview
->no_aux_sampler_surface_state
= alloc_surface_state(device
);
659 /* Sampling is performed in one of two buffer configurations in anv: with
660 * an auxiliary buffer or without it. Sampler states aren't always needed
661 * for both configurations, but are currently created unconditionally for
664 * TODO: Consider allocating each surface state only when necessary.
667 /* Create a sampler state with the optimal aux_usage for sampling. This
668 * may use the aux_buffer.
670 const enum isl_aux_usage surf_usage
=
671 anv_layout_to_aux_usage(&device
->info
, image
, iview
->aspect_mask
,
672 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
);
674 /* If this is a HiZ buffer we can sample from with a programmable clear
675 * value (SKL+), define the clear value to the optimal constant.
677 const float red_clear_color
= surf_usage
== ISL_AUX_USAGE_HIZ
&&
678 device
->info
.gen
>= 9 ?
679 ANV_HZ_FC_VAL
: 0.0f
;
681 struct isl_view view
= iview
->isl
;
682 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
683 isl_surf_fill_state(&device
->isl_dev
,
684 iview
->sampler_surface_state
.map
,
685 .surf
= &surface
->isl
,
687 .clear_color
.f32
= { red_clear_color
,},
688 .aux_surf
= &image
->aux_surface
.isl
,
689 .aux_usage
= surf_usage
,
690 .mocs
= device
->default_mocs
);
692 /* Create a sampler state that only uses the main buffer. */
693 isl_surf_fill_state(&device
->isl_dev
,
694 iview
->no_aux_sampler_surface_state
.map
,
695 .surf
= &surface
->isl
,
697 .mocs
= device
->default_mocs
);
699 anv_state_flush(device
, iview
->sampler_surface_state
);
700 anv_state_flush(device
, iview
->no_aux_sampler_surface_state
);
702 iview
->sampler_surface_state
.alloc_size
= 0;
703 iview
->no_aux_sampler_surface_state
.alloc_size
= 0;
706 /* NOTE: This one needs to go last since it may stomp isl_view.format */
707 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
708 iview
->storage_surface_state
= alloc_surface_state(device
);
709 iview
->writeonly_storage_surface_state
= alloc_surface_state(device
);
711 struct isl_view view
= iview
->isl
;
712 view
.usage
|= ISL_SURF_USAGE_STORAGE_BIT
;
714 /* Write-only accesses always used a typed write instruction and should
715 * therefore use the real format.
717 isl_surf_fill_state(&device
->isl_dev
,
718 iview
->writeonly_storage_surface_state
.map
,
719 .surf
= &surface
->isl
,
721 .aux_surf
= &image
->aux_surface
.isl
,
722 .aux_usage
= image
->aux_usage
,
723 .mocs
= device
->default_mocs
);
725 if (isl_has_matching_typed_storage_image_format(&device
->info
,
726 format
.isl_format
)) {
727 /* Typed surface reads support a very limited subset of the shader
728 * image formats. Translate it into the closest format the hardware
731 view
.format
= isl_lower_storage_image_format(&device
->info
,
734 isl_surf_fill_state(&device
->isl_dev
,
735 iview
->storage_surface_state
.map
,
736 .surf
= &surface
->isl
,
738 .aux_surf
= &image
->aux_surface
.isl
,
739 .aux_usage
= image
->aux_usage
,
740 .mocs
= device
->default_mocs
);
742 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
745 iview
->bo
->size
- iview
->offset
, 1);
748 isl_surf_fill_image_param(&device
->isl_dev
,
749 &iview
->storage_image_param
,
750 &surface
->isl
, &iview
->isl
);
752 anv_state_flush(device
, iview
->storage_surface_state
);
753 anv_state_flush(device
, iview
->writeonly_storage_surface_state
);
755 iview
->storage_surface_state
.alloc_size
= 0;
756 iview
->writeonly_storage_surface_state
.alloc_size
= 0;
759 *pView
= anv_image_view_to_handle(iview
);
765 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
766 const VkAllocationCallbacks
*pAllocator
)
768 ANV_FROM_HANDLE(anv_device
, device
, _device
);
769 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
774 if (iview
->sampler_surface_state
.alloc_size
> 0) {
775 anv_state_pool_free(&device
->surface_state_pool
,
776 iview
->sampler_surface_state
);
779 if (iview
->no_aux_sampler_surface_state
.alloc_size
> 0) {
780 anv_state_pool_free(&device
->surface_state_pool
,
781 iview
->no_aux_sampler_surface_state
);
784 if (iview
->storage_surface_state
.alloc_size
> 0) {
785 anv_state_pool_free(&device
->surface_state_pool
,
786 iview
->storage_surface_state
);
789 if (iview
->writeonly_storage_surface_state
.alloc_size
> 0) {
790 anv_state_pool_free(&device
->surface_state_pool
,
791 iview
->writeonly_storage_surface_state
);
794 vk_free2(&device
->alloc
, pAllocator
, iview
);
799 anv_CreateBufferView(VkDevice _device
,
800 const VkBufferViewCreateInfo
*pCreateInfo
,
801 const VkAllocationCallbacks
*pAllocator
,
804 ANV_FROM_HANDLE(anv_device
, device
, _device
);
805 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
806 struct anv_buffer_view
*view
;
808 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
809 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
811 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
813 /* TODO: Handle the format swizzle? */
815 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
816 VK_IMAGE_ASPECT_COLOR_BIT
,
817 VK_IMAGE_TILING_LINEAR
);
818 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
819 view
->bo
= buffer
->bo
;
820 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
821 view
->range
= anv_buffer_get_range(buffer
, pCreateInfo
->offset
,
823 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
825 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
826 view
->surface_state
= alloc_surface_state(device
);
828 anv_fill_buffer_surface_state(device
, view
->surface_state
,
830 view
->offset
, view
->range
, format_bs
);
832 view
->surface_state
= (struct anv_state
){ 0 };
835 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
836 view
->storage_surface_state
= alloc_surface_state(device
);
837 view
->writeonly_storage_surface_state
= alloc_surface_state(device
);
839 enum isl_format storage_format
=
840 isl_has_matching_typed_storage_image_format(&device
->info
,
842 isl_lower_storage_image_format(&device
->info
, view
->format
) :
845 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
847 view
->offset
, view
->range
,
848 (storage_format
== ISL_FORMAT_RAW
? 1 :
849 isl_format_get_layout(storage_format
)->bpb
/ 8));
851 /* Write-only accesses should use the original format. */
852 anv_fill_buffer_surface_state(device
, view
->writeonly_storage_surface_state
,
854 view
->offset
, view
->range
,
855 isl_format_get_layout(view
->format
)->bpb
/ 8);
857 isl_buffer_fill_image_param(&device
->isl_dev
,
858 &view
->storage_image_param
,
859 view
->format
, view
->range
);
861 view
->storage_surface_state
= (struct anv_state
){ 0 };
862 view
->writeonly_storage_surface_state
= (struct anv_state
){ 0 };
865 *pView
= anv_buffer_view_to_handle(view
);
871 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
872 const VkAllocationCallbacks
*pAllocator
)
874 ANV_FROM_HANDLE(anv_device
, device
, _device
);
875 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
880 if (view
->surface_state
.alloc_size
> 0)
881 anv_state_pool_free(&device
->surface_state_pool
,
882 view
->surface_state
);
884 if (view
->storage_surface_state
.alloc_size
> 0)
885 anv_state_pool_free(&device
->surface_state_pool
,
886 view
->storage_surface_state
);
888 if (view
->writeonly_storage_surface_state
.alloc_size
> 0)
889 anv_state_pool_free(&device
->surface_state_pool
,
890 view
->writeonly_storage_surface_state
);
892 vk_free2(&device
->alloc
, pAllocator
, view
);
895 const struct anv_surface
*
896 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
897 VkImageAspectFlags aspect_mask
)
899 switch (aspect_mask
) {
900 case VK_IMAGE_ASPECT_COLOR_BIT
:
901 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
902 return &image
->color_surface
;
903 case VK_IMAGE_ASPECT_DEPTH_BIT
:
904 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
905 return &image
->depth_surface
;
906 case VK_IMAGE_ASPECT_STENCIL_BIT
:
907 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
908 return &image
->stencil_surface
;
909 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
910 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
911 * combined depth stencil formats. Specifically, it states:
913 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
914 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
916 * Image views with both depth and stencil aspects are only valid for
917 * render target attachments, in which case
918 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
919 * stencil surfaces from the underlying surface.
921 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
922 return &image
->depth_surface
;
924 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
925 return &image
->stencil_surface
;
928 unreachable("image does not have aspect");