2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "anv_private.h"
31 #include "util/debug.h"
33 #include "vk_format_info.h"
36 * Exactly one bit must be set in \a aspect.
38 static isl_surf_usage_flags_t
39 choose_isl_surf_usage(VkImageUsageFlags vk_usage
,
40 VkImageAspectFlags aspect
)
42 isl_surf_usage_flags_t isl_usage
= 0;
44 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
45 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
47 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
48 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
50 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
51 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
53 if (vk_usage
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
54 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
56 /* Even if we're only using it for transfer operations, clears to depth and
57 * stencil images happen as depth and stencil so they need the right ISL
58 * usage bits or else things will fall apart.
61 case VK_IMAGE_ASPECT_DEPTH_BIT
:
62 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
64 case VK_IMAGE_ASPECT_STENCIL_BIT
:
65 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
67 case VK_IMAGE_ASPECT_COLOR_BIT
:
70 unreachable("bad VkImageAspect");
73 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
74 /* blorp implements transfers by sampling from the source image. */
75 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
78 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
&&
79 aspect
== VK_IMAGE_ASPECT_COLOR_BIT
) {
80 /* blorp implements transfers by rendering into the destination image.
81 * Only request this with color images, as we deal with depth/stencil
82 * formats differently. */
83 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
90 * Exactly one bit must be set in \a aspect.
92 static struct anv_surface
*
93 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
97 unreachable("bad VkImageAspect");
98 case VK_IMAGE_ASPECT_COLOR_BIT
:
99 return &image
->color_surface
;
100 case VK_IMAGE_ASPECT_DEPTH_BIT
:
101 return &image
->depth_surface
;
102 case VK_IMAGE_ASPECT_STENCIL_BIT
:
103 return &image
->stencil_surface
;
108 add_surface(struct anv_image
*image
, struct anv_surface
*surf
)
110 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
112 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
113 image
->size
= surf
->offset
+ surf
->isl
.size
;
114 image
->alignment
= MAX2(image
->alignment
, surf
->isl
.alignment
);
118 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
119 * image's memory requirements (that is, the image's size and alignment).
121 * Exactly one bit must be set in \a aspect.
124 make_surface(const struct anv_device
*dev
,
125 struct anv_image
*image
,
126 const struct anv_image_create_info
*anv_info
,
127 VkImageAspectFlags aspect
)
129 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
132 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
133 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
134 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
135 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
138 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
139 * result with an optionally provided ISL tiling argument.
141 isl_tiling_flags_t tiling_flags
=
142 (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) ?
143 ISL_TILING_LINEAR_BIT
: ISL_TILING_ANY_MASK
;
145 if (anv_info
->isl_tiling_flags
)
146 tiling_flags
&= anv_info
->isl_tiling_flags
;
148 assert(tiling_flags
);
150 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
152 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
155 enum isl_format format
= anv_get_isl_format(&dev
->info
, vk_info
->format
,
156 aspect
, vk_info
->tiling
);
157 assert(format
!= ISL_FORMAT_UNSUPPORTED
);
159 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
160 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
162 .width
= image
->extent
.width
,
163 .height
= image
->extent
.height
,
164 .depth
= image
->extent
.depth
,
165 .levels
= vk_info
->mipLevels
,
166 .array_len
= vk_info
->arrayLayers
,
167 .samples
= vk_info
->samples
,
169 .min_pitch
= anv_info
->stride
,
170 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
171 .tiling_flags
= tiling_flags
);
173 /* isl_surf_init() will fail only if provided invalid input. Invalid input
174 * is illegal in Vulkan.
178 add_surface(image
, anv_surf
);
180 /* Add a HiZ surface to a depth buffer that will be used for rendering.
182 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
183 /* Allow the user to control HiZ enabling. Disable by default on gen7
184 * because resolves are not currently implemented pre-BDW.
186 if (!(image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
187 /* It will never be used as an attachment, HiZ is pointless. */
188 } else if (image
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
) {
189 /* From the 1.0.37 spec:
191 * "An attachment used as an input attachment and depth/stencil
192 * attachment must be in either VK_IMAGE_LAYOUT_GENERAL or
193 * VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL."
195 * It will never have a layout of
196 * VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, so HiZ is
197 * currently pointless. If transfer operations learn to use the HiZ
198 * buffer, we can enable HiZ for VK_IMAGE_LAYOUT_GENERAL and support
201 anv_finishme("Implement HiZ for input attachments");
202 } else if (!env_var_as_boolean("INTEL_VK_HIZ", dev
->info
.gen
>= 8)) {
203 anv_finishme("Implement gen7 HiZ");
204 } else if (vk_info
->mipLevels
> 1) {
205 anv_finishme("Test multi-LOD HiZ");
206 } else if (vk_info
->arrayLayers
> 1) {
207 anv_finishme("Implement multi-arrayLayer HiZ clears and resolves");
208 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
209 anv_finishme("Test gen8 multisampled HiZ");
211 assert(image
->aux_surface
.isl
.size
== 0);
212 ok
= isl_surf_get_hiz_surf(&dev
->isl_dev
, &image
->depth_surface
.isl
,
213 &image
->aux_surface
.isl
);
215 add_surface(image
, &image
->aux_surface
);
216 image
->aux_usage
= ISL_AUX_USAGE_HIZ
;
218 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
== 1) {
219 if (!unlikely(INTEL_DEBUG
& DEBUG_NO_RBC
)) {
220 assert(image
->aux_surface
.isl
.size
== 0);
221 ok
= isl_surf_get_ccs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
222 &image
->aux_surface
.isl
);
224 add_surface(image
, &image
->aux_surface
);
226 /* For images created without MUTABLE_FORMAT_BIT set, we know that
227 * they will always be used with the original format. In
228 * particular, they will always be used with a format that
229 * supports color compression. If it's never used as a storage
230 * image, then it will only be used through the sampler or the as
231 * a render target. This means that it's safe to just leave
232 * compression on at all times for these formats.
234 if (!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) &&
235 !(vk_info
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) &&
236 isl_format_supports_ccs_e(&dev
->info
, format
)) {
237 image
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
241 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
> 1) {
242 assert(image
->aux_surface
.isl
.size
== 0);
243 assert(!(vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
));
244 ok
= isl_surf_get_mcs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
245 &image
->aux_surface
.isl
);
247 add_surface(image
, &image
->aux_surface
);
248 image
->aux_usage
= ISL_AUX_USAGE_MCS
;
256 anv_image_create(VkDevice _device
,
257 const struct anv_image_create_info
*create_info
,
258 const VkAllocationCallbacks
* alloc
,
261 ANV_FROM_HANDLE(anv_device
, device
, _device
);
262 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
263 struct anv_image
*image
= NULL
;
266 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
268 anv_assert(pCreateInfo
->mipLevels
> 0);
269 anv_assert(pCreateInfo
->arrayLayers
> 0);
270 anv_assert(pCreateInfo
->samples
> 0);
271 anv_assert(pCreateInfo
->extent
.width
> 0);
272 anv_assert(pCreateInfo
->extent
.height
> 0);
273 anv_assert(pCreateInfo
->extent
.depth
> 0);
275 image
= vk_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
276 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
278 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
280 memset(image
, 0, sizeof(*image
));
281 image
->type
= pCreateInfo
->imageType
;
282 image
->extent
= pCreateInfo
->extent
;
283 image
->vk_format
= pCreateInfo
->format
;
284 image
->aspects
= vk_format_aspects(image
->vk_format
);
285 image
->levels
= pCreateInfo
->mipLevels
;
286 image
->array_size
= pCreateInfo
->arrayLayers
;
287 image
->samples
= pCreateInfo
->samples
;
288 image
->usage
= pCreateInfo
->usage
;
289 image
->tiling
= pCreateInfo
->tiling
;
290 image
->aux_usage
= ISL_AUX_USAGE_NONE
;
293 for_each_bit(b
, image
->aspects
) {
294 r
= make_surface(device
, image
, create_info
, (1 << b
));
299 *pImage
= anv_image_to_handle(image
);
305 vk_free2(&device
->alloc
, alloc
, image
);
311 anv_CreateImage(VkDevice device
,
312 const VkImageCreateInfo
*pCreateInfo
,
313 const VkAllocationCallbacks
*pAllocator
,
316 return anv_image_create(device
,
317 &(struct anv_image_create_info
) {
318 .vk_info
= pCreateInfo
,
325 anv_DestroyImage(VkDevice _device
, VkImage _image
,
326 const VkAllocationCallbacks
*pAllocator
)
328 ANV_FROM_HANDLE(anv_device
, device
, _device
);
329 ANV_FROM_HANDLE(anv_image
, image
, _image
);
334 vk_free2(&device
->alloc
, pAllocator
, image
);
337 VkResult
anv_BindImageMemory(
340 VkDeviceMemory _memory
,
341 VkDeviceSize memoryOffset
)
343 ANV_FROM_HANDLE(anv_device
, device
, _device
);
344 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
345 ANV_FROM_HANDLE(anv_image
, image
, _image
);
348 image
->bo
= &mem
->bo
;
349 image
->offset
= memoryOffset
;
355 if (image
->aux_surface
.isl
.size
> 0) {
357 /* The offset and size must be a multiple of 4K or else the
358 * anv_gem_mmap call below will return NULL.
360 assert((image
->offset
+ image
->aux_surface
.offset
) % 4096 == 0);
361 assert(image
->aux_surface
.isl
.size
% 4096 == 0);
363 /* Auxiliary surfaces need to have their memory cleared to 0 before they
364 * can be used. For CCS surfaces, this puts them in the "resolved"
365 * state so they can be used with CCS enabled before we ever touch it
366 * from the GPU. For HiZ, we need something valid or else we may get
367 * GPU hangs on some hardware and 0 works fine.
369 void *map
= anv_gem_mmap(device
, image
->bo
->gem_handle
,
370 image
->offset
+ image
->aux_surface
.offset
,
371 image
->aux_surface
.isl
.size
,
372 device
->info
.has_llc
? 0 : I915_MMAP_WC
);
374 /* If anv_gem_mmap returns NULL, it's likely that the kernel was
375 * not able to find space on the host to create a proper mapping.
378 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
380 memset(map
, 0, image
->aux_surface
.isl
.size
);
382 anv_gem_munmap(map
, image
->aux_surface
.isl
.size
);
389 anv_surface_get_subresource_layout(struct anv_image
*image
,
390 struct anv_surface
*surface
,
391 const VkImageSubresource
*subresource
,
392 VkSubresourceLayout
*layout
)
394 /* If we are on a non-zero mip level or array slice, we need to
395 * calculate a real offset.
397 anv_assert(subresource
->mipLevel
== 0);
398 anv_assert(subresource
->arrayLayer
== 0);
400 layout
->offset
= surface
->offset
;
401 layout
->rowPitch
= surface
->isl
.row_pitch
;
402 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
403 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
404 layout
->size
= surface
->isl
.size
;
407 void anv_GetImageSubresourceLayout(
410 const VkImageSubresource
* pSubresource
,
411 VkSubresourceLayout
* pLayout
)
413 ANV_FROM_HANDLE(anv_image
, image
, _image
);
415 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
417 switch (pSubresource
->aspectMask
) {
418 case VK_IMAGE_ASPECT_COLOR_BIT
:
419 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
420 pSubresource
, pLayout
);
422 case VK_IMAGE_ASPECT_DEPTH_BIT
:
423 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
424 pSubresource
, pLayout
);
426 case VK_IMAGE_ASPECT_STENCIL_BIT
:
427 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
428 pSubresource
, pLayout
);
431 assert(!"Invalid image aspect");
435 static struct anv_state
436 alloc_surface_state(struct anv_device
*device
)
438 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
441 static enum isl_channel_select
442 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
443 struct isl_swizzle format_swizzle
)
445 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
449 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
450 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
451 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
452 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
453 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
454 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
456 unreachable("Invalid swizzle");
462 anv_CreateImageView(VkDevice _device
,
463 const VkImageViewCreateInfo
*pCreateInfo
,
464 const VkAllocationCallbacks
*pAllocator
,
467 ANV_FROM_HANDLE(anv_device
, device
, _device
);
468 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
469 struct anv_image_view
*iview
;
471 iview
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*iview
), 8,
472 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
474 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
476 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
478 assert(range
->layerCount
> 0);
479 assert(range
->baseMipLevel
< image
->levels
);
480 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
481 VK_IMAGE_USAGE_STORAGE_BIT
|
482 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
483 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
485 switch (image
->type
) {
487 unreachable("bad VkImageType");
488 case VK_IMAGE_TYPE_1D
:
489 case VK_IMAGE_TYPE_2D
:
490 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
492 case VK_IMAGE_TYPE_3D
:
493 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
494 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
498 const struct anv_surface
*surface
=
499 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
501 iview
->image
= image
;
502 iview
->bo
= image
->bo
;
503 iview
->offset
= image
->offset
+ surface
->offset
;
505 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
506 iview
->vk_format
= pCreateInfo
->format
;
508 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
509 range
->aspectMask
, image
->tiling
);
511 iview
->isl
= (struct isl_view
) {
512 .format
= format
.isl_format
,
513 .base_level
= range
->baseMipLevel
,
514 .levels
= anv_get_levelCount(image
, range
),
515 .base_array_layer
= range
->baseArrayLayer
,
516 .array_len
= anv_get_layerCount(image
, range
),
518 .r
= remap_swizzle(pCreateInfo
->components
.r
,
519 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
520 .g
= remap_swizzle(pCreateInfo
->components
.g
,
521 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
522 .b
= remap_swizzle(pCreateInfo
->components
.b
,
523 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
524 .a
= remap_swizzle(pCreateInfo
->components
.a
,
525 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
529 iview
->extent
= (VkExtent3D
) {
530 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
531 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
532 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
535 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_3D
) {
536 iview
->isl
.base_array_layer
= 0;
537 iview
->isl
.array_len
= iview
->extent
.depth
;
540 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
541 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
542 iview
->isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
544 iview
->isl
.usage
= 0;
547 /* If the HiZ buffer can be sampled from, set the constant clear color.
548 * If it cannot, disable the isl aux usage flag.
550 float red_clear_color
= 0.0f
;
551 enum isl_aux_usage surf_usage
= image
->aux_usage
;
552 if (image
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
553 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_DEPTH_BIT
&&
554 anv_can_sample_with_hiz(device
->info
.gen
, image
->samples
)) {
555 /* When a HiZ buffer is sampled on gen9+, ensure that
556 * the constant fast clear value is set in the surface state.
558 if (device
->info
.gen
>= 9)
559 red_clear_color
= ANV_HZ_FC_VAL
;
561 surf_usage
= ISL_AUX_USAGE_NONE
;
565 /* Input attachment surfaces for color are allocated and filled
566 * out at BeginRenderPass time because they need compression information.
567 * Compression is not yet enabled for depth textures and stencil doesn't
568 * allow compression so we can just use the texture surface state from the
571 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
||
572 (image
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
&&
573 !(iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
))) {
574 iview
->sampler_surface_state
= alloc_surface_state(device
);
576 struct isl_view view
= iview
->isl
;
577 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
578 isl_surf_fill_state(&device
->isl_dev
,
579 iview
->sampler_surface_state
.map
,
580 .surf
= &surface
->isl
,
582 .clear_color
.f32
= { red_clear_color
,},
583 .aux_surf
= &image
->aux_surface
.isl
,
584 .aux_usage
= surf_usage
,
585 .mocs
= device
->default_mocs
);
587 anv_state_flush(device
, iview
->sampler_surface_state
);
589 iview
->sampler_surface_state
.alloc_size
= 0;
592 /* NOTE: This one needs to go last since it may stomp isl_view.format */
593 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
594 iview
->storage_surface_state
= alloc_surface_state(device
);
595 iview
->writeonly_storage_surface_state
= alloc_surface_state(device
);
597 struct isl_view view
= iview
->isl
;
598 view
.usage
|= ISL_SURF_USAGE_STORAGE_BIT
;
600 /* Write-only accesses always used a typed write instruction and should
601 * therefore use the real format.
603 isl_surf_fill_state(&device
->isl_dev
,
604 iview
->writeonly_storage_surface_state
.map
,
605 .surf
= &surface
->isl
,
607 .aux_surf
= &image
->aux_surface
.isl
,
608 .aux_usage
= surf_usage
,
609 .mocs
= device
->default_mocs
);
611 if (isl_has_matching_typed_storage_image_format(&device
->info
,
612 format
.isl_format
)) {
613 /* Typed surface reads support a very limited subset of the shader
614 * image formats. Translate it into the closest format the hardware
617 view
.format
= isl_lower_storage_image_format(&device
->info
,
620 isl_surf_fill_state(&device
->isl_dev
,
621 iview
->storage_surface_state
.map
,
622 .surf
= &surface
->isl
,
624 .aux_surf
= &image
->aux_surface
.isl
,
625 .aux_usage
= surf_usage
,
626 .mocs
= device
->default_mocs
);
628 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
631 iview
->bo
->size
- iview
->offset
, 1);
634 isl_surf_fill_image_param(&device
->isl_dev
,
635 &iview
->storage_image_param
,
636 &surface
->isl
, &iview
->isl
);
638 anv_state_flush(device
, iview
->storage_surface_state
);
639 anv_state_flush(device
, iview
->writeonly_storage_surface_state
);
641 iview
->storage_surface_state
.alloc_size
= 0;
642 iview
->writeonly_storage_surface_state
.alloc_size
= 0;
645 *pView
= anv_image_view_to_handle(iview
);
651 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
652 const VkAllocationCallbacks
*pAllocator
)
654 ANV_FROM_HANDLE(anv_device
, device
, _device
);
655 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
660 if (iview
->sampler_surface_state
.alloc_size
> 0) {
661 anv_state_pool_free(&device
->surface_state_pool
,
662 iview
->sampler_surface_state
);
665 if (iview
->storage_surface_state
.alloc_size
> 0) {
666 anv_state_pool_free(&device
->surface_state_pool
,
667 iview
->storage_surface_state
);
670 if (iview
->writeonly_storage_surface_state
.alloc_size
> 0) {
671 anv_state_pool_free(&device
->surface_state_pool
,
672 iview
->writeonly_storage_surface_state
);
675 vk_free2(&device
->alloc
, pAllocator
, iview
);
680 anv_CreateBufferView(VkDevice _device
,
681 const VkBufferViewCreateInfo
*pCreateInfo
,
682 const VkAllocationCallbacks
*pAllocator
,
685 ANV_FROM_HANDLE(anv_device
, device
, _device
);
686 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
687 struct anv_buffer_view
*view
;
689 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
690 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
692 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
694 /* TODO: Handle the format swizzle? */
696 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
697 VK_IMAGE_ASPECT_COLOR_BIT
,
698 VK_IMAGE_TILING_LINEAR
);
699 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
700 view
->bo
= buffer
->bo
;
701 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
702 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
703 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
704 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
706 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
707 view
->surface_state
= alloc_surface_state(device
);
709 anv_fill_buffer_surface_state(device
, view
->surface_state
,
711 view
->offset
, view
->range
, format_bs
);
713 view
->surface_state
= (struct anv_state
){ 0 };
716 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
717 view
->storage_surface_state
= alloc_surface_state(device
);
718 view
->writeonly_storage_surface_state
= alloc_surface_state(device
);
720 enum isl_format storage_format
=
721 isl_has_matching_typed_storage_image_format(&device
->info
,
723 isl_lower_storage_image_format(&device
->info
, view
->format
) :
726 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
728 view
->offset
, view
->range
,
729 (storage_format
== ISL_FORMAT_RAW
? 1 :
730 isl_format_get_layout(storage_format
)->bpb
/ 8));
732 /* Write-only accesses should use the original format. */
733 anv_fill_buffer_surface_state(device
, view
->writeonly_storage_surface_state
,
735 view
->offset
, view
->range
,
736 isl_format_get_layout(view
->format
)->bpb
/ 8);
738 isl_buffer_fill_image_param(&device
->isl_dev
,
739 &view
->storage_image_param
,
740 view
->format
, view
->range
);
742 view
->storage_surface_state
= (struct anv_state
){ 0 };
743 view
->writeonly_storage_surface_state
= (struct anv_state
){ 0 };
746 *pView
= anv_buffer_view_to_handle(view
);
752 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
753 const VkAllocationCallbacks
*pAllocator
)
755 ANV_FROM_HANDLE(anv_device
, device
, _device
);
756 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
761 if (view
->surface_state
.alloc_size
> 0)
762 anv_state_pool_free(&device
->surface_state_pool
,
763 view
->surface_state
);
765 if (view
->storage_surface_state
.alloc_size
> 0)
766 anv_state_pool_free(&device
->surface_state_pool
,
767 view
->storage_surface_state
);
769 if (view
->writeonly_storage_surface_state
.alloc_size
> 0)
770 anv_state_pool_free(&device
->surface_state_pool
,
771 view
->writeonly_storage_surface_state
);
773 vk_free2(&device
->alloc
, pAllocator
, view
);
776 const struct anv_surface
*
777 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
778 VkImageAspectFlags aspect_mask
)
780 switch (aspect_mask
) {
781 case VK_IMAGE_ASPECT_COLOR_BIT
:
782 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
783 return &image
->color_surface
;
784 case VK_IMAGE_ASPECT_DEPTH_BIT
:
785 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
786 return &image
->depth_surface
;
787 case VK_IMAGE_ASPECT_STENCIL_BIT
:
788 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
789 return &image
->stencil_surface
;
790 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
791 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
792 * combined depth stencil formats. Specifically, it states:
794 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
795 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
797 * Image views with both depth and stencil aspects are only valid for
798 * render target attachments, in which case
799 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
800 * stencil surfaces from the underlying surface.
802 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
803 return &image
->depth_surface
;
805 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
806 return &image
->stencil_surface
;
809 unreachable("image does not have aspect");