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 /* blorp implements transfers by rendering into the destination image. */
80 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
87 * Exactly one bit must be set in \a aspect.
89 static struct anv_surface
*
90 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
94 unreachable("bad VkImageAspect");
95 case VK_IMAGE_ASPECT_COLOR_BIT
:
96 return &image
->color_surface
;
97 case VK_IMAGE_ASPECT_DEPTH_BIT
:
98 return &image
->depth_surface
;
99 case VK_IMAGE_ASPECT_STENCIL_BIT
:
100 return &image
->stencil_surface
;
105 add_surface(struct anv_image
*image
, struct anv_surface
*surf
)
107 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
109 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
110 image
->size
= surf
->offset
+ surf
->isl
.size
;
111 image
->alignment
= MAX2(image
->alignment
, surf
->isl
.alignment
);
115 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
116 * image's memory requirements (that is, the image's size and alignment).
118 * Exactly one bit must be set in \a aspect.
121 make_surface(const struct anv_device
*dev
,
122 struct anv_image
*image
,
123 const struct anv_image_create_info
*anv_info
,
124 VkImageAspectFlags aspect
)
126 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
129 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
130 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
131 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
132 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
135 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
136 * result with an optionally provided ISL tiling argument.
138 isl_tiling_flags_t tiling_flags
=
139 (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) ?
140 ISL_TILING_LINEAR_BIT
: ISL_TILING_ANY_MASK
;
142 if (anv_info
->isl_tiling_flags
)
143 tiling_flags
&= anv_info
->isl_tiling_flags
;
145 assert(tiling_flags
);
147 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
149 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
152 enum isl_format format
= anv_get_isl_format(&dev
->info
, vk_info
->format
,
153 aspect
, vk_info
->tiling
);
154 assert(format
!= ISL_FORMAT_UNSUPPORTED
);
156 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
157 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
159 .width
= image
->extent
.width
,
160 .height
= image
->extent
.height
,
161 .depth
= image
->extent
.depth
,
162 .levels
= vk_info
->mipLevels
,
163 .array_len
= vk_info
->arrayLayers
,
164 .samples
= vk_info
->samples
,
166 .min_pitch
= anv_info
->stride
,
167 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
168 .tiling_flags
= tiling_flags
);
170 /* isl_surf_init() will fail only if provided invalid input. Invalid input
171 * is illegal in Vulkan.
175 add_surface(image
, anv_surf
);
177 /* Add a HiZ surface to a depth buffer that will be used for rendering.
179 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
) {
180 /* Allow the user to control HiZ enabling. Disable by default on gen7
181 * because resolves are not currently implemented pre-BDW.
183 if (!(image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
184 /* It will never be used as an attachment, HiZ is pointless. */
185 } else if (image
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
) {
186 /* From the 1.0.37 spec:
188 * "An attachment used as an input attachment and depth/stencil
189 * attachment must be in either VK_IMAGE_LAYOUT_GENERAL or
190 * VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL."
192 * It will never have a layout of
193 * VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, so HiZ is
194 * currently pointless. If transfer operations learn to use the HiZ
195 * buffer, we can enable HiZ for VK_IMAGE_LAYOUT_GENERAL and support
198 anv_finishme("Implement HiZ for input attachments");
199 } else if (!env_var_as_boolean("INTEL_VK_HIZ", dev
->info
.gen
>= 8)) {
200 anv_finishme("Implement gen7 HiZ");
201 } else if (vk_info
->mipLevels
> 1) {
202 anv_finishme("Test multi-LOD HiZ");
203 } else if (vk_info
->arrayLayers
> 1) {
204 anv_finishme("Implement multi-arrayLayer HiZ clears and resolves");
205 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
206 anv_finishme("Test gen8 multisampled HiZ");
208 assert(image
->aux_surface
.isl
.size
== 0);
209 isl_surf_get_hiz_surf(&dev
->isl_dev
, &image
->depth_surface
.isl
,
210 &image
->aux_surface
.isl
);
211 add_surface(image
, &image
->aux_surface
);
212 image
->aux_usage
= ISL_AUX_USAGE_HIZ
;
214 } else if (aspect
== VK_IMAGE_ASPECT_COLOR_BIT
&& vk_info
->samples
== 1) {
215 if (!unlikely(INTEL_DEBUG
& DEBUG_NO_RBC
)) {
216 assert(image
->aux_surface
.isl
.size
== 0);
217 ok
= isl_surf_get_ccs_surf(&dev
->isl_dev
, &anv_surf
->isl
,
218 &image
->aux_surface
.isl
);
220 add_surface(image
, &image
->aux_surface
);
222 /* For images created without MUTABLE_FORMAT_BIT set, we know that
223 * they will always be used with the original format. In
224 * particular, they will always be used with a format that
225 * supports color compression. This means that it's safe to just
226 * leave compression on at all times for these formats.
228 if (!(vk_info
->flags
& VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT
) &&
229 isl_format_supports_lossless_compression(&dev
->info
, format
)) {
230 if (vk_info
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
232 * For now, we leave compression off for anything that may
233 * be used as a storage image. This is because accessing
234 * storage images may involve ccs-incompatible views or even
235 * untyped messages which don't support compression at all.
237 anv_finishme("Enable CCS for storage images");
239 image
->aux_usage
= ISL_AUX_USAGE_CCS_E
;
250 anv_image_create(VkDevice _device
,
251 const struct anv_image_create_info
*create_info
,
252 const VkAllocationCallbacks
* alloc
,
255 ANV_FROM_HANDLE(anv_device
, device
, _device
);
256 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
257 struct anv_image
*image
= NULL
;
260 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
262 anv_assert(pCreateInfo
->mipLevels
> 0);
263 anv_assert(pCreateInfo
->arrayLayers
> 0);
264 anv_assert(pCreateInfo
->samples
> 0);
265 anv_assert(pCreateInfo
->extent
.width
> 0);
266 anv_assert(pCreateInfo
->extent
.height
> 0);
267 anv_assert(pCreateInfo
->extent
.depth
> 0);
269 image
= vk_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
270 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
272 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
274 memset(image
, 0, sizeof(*image
));
275 image
->type
= pCreateInfo
->imageType
;
276 image
->extent
= pCreateInfo
->extent
;
277 image
->vk_format
= pCreateInfo
->format
;
278 image
->aspects
= vk_format_aspects(image
->vk_format
);
279 image
->levels
= pCreateInfo
->mipLevels
;
280 image
->array_size
= pCreateInfo
->arrayLayers
;
281 image
->samples
= pCreateInfo
->samples
;
282 image
->usage
= pCreateInfo
->usage
;
283 image
->tiling
= pCreateInfo
->tiling
;
284 image
->aux_usage
= ISL_AUX_USAGE_NONE
;
287 for_each_bit(b
, image
->aspects
) {
288 r
= make_surface(device
, image
, create_info
, (1 << b
));
293 *pImage
= anv_image_to_handle(image
);
299 vk_free2(&device
->alloc
, alloc
, image
);
305 anv_CreateImage(VkDevice device
,
306 const VkImageCreateInfo
*pCreateInfo
,
307 const VkAllocationCallbacks
*pAllocator
,
310 return anv_image_create(device
,
311 &(struct anv_image_create_info
) {
312 .vk_info
= pCreateInfo
,
319 anv_DestroyImage(VkDevice _device
, VkImage _image
,
320 const VkAllocationCallbacks
*pAllocator
)
322 ANV_FROM_HANDLE(anv_device
, device
, _device
);
323 ANV_FROM_HANDLE(anv_image
, image
, _image
);
328 vk_free2(&device
->alloc
, pAllocator
, image
);
331 VkResult
anv_BindImageMemory(
334 VkDeviceMemory _memory
,
335 VkDeviceSize memoryOffset
)
337 ANV_FROM_HANDLE(anv_device
, device
, _device
);
338 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
339 ANV_FROM_HANDLE(anv_image
, image
, _image
);
342 image
->bo
= &mem
->bo
;
343 image
->offset
= memoryOffset
;
349 if (image
->aux_surface
.isl
.size
> 0) {
351 /* The offset and size must be a multiple of 4K or else the
352 * anv_gem_mmap call below will return NULL.
354 assert((image
->offset
+ image
->aux_surface
.offset
) % 4096 == 0);
355 assert(image
->aux_surface
.isl
.size
% 4096 == 0);
357 /* Auxiliary surfaces need to have their memory cleared to 0 before they
358 * can be used. For CCS surfaces, this puts them in the "resolved"
359 * state so they can be used with CCS enabled before we ever touch it
360 * from the GPU. For HiZ, we need something valid or else we may get
361 * GPU hangs on some hardware and 0 works fine.
363 void *map
= anv_gem_mmap(device
, image
->bo
->gem_handle
,
364 image
->offset
+ image
->aux_surface
.offset
,
365 image
->aux_surface
.isl
.size
,
366 device
->info
.has_llc
? 0 : I915_MMAP_WC
);
368 /* If anv_gem_mmap returns NULL, it's likely that the kernel was
369 * not able to find space on the host to create a proper mapping.
372 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
374 memset(map
, 0, image
->aux_surface
.isl
.size
);
376 anv_gem_munmap(map
, image
->aux_surface
.isl
.size
);
383 anv_surface_get_subresource_layout(struct anv_image
*image
,
384 struct anv_surface
*surface
,
385 const VkImageSubresource
*subresource
,
386 VkSubresourceLayout
*layout
)
388 /* If we are on a non-zero mip level or array slice, we need to
389 * calculate a real offset.
391 anv_assert(subresource
->mipLevel
== 0);
392 anv_assert(subresource
->arrayLayer
== 0);
394 layout
->offset
= surface
->offset
;
395 layout
->rowPitch
= surface
->isl
.row_pitch
;
396 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
397 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
398 layout
->size
= surface
->isl
.size
;
401 void anv_GetImageSubresourceLayout(
404 const VkImageSubresource
* pSubresource
,
405 VkSubresourceLayout
* pLayout
)
407 ANV_FROM_HANDLE(anv_image
, image
, _image
);
409 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
411 switch (pSubresource
->aspectMask
) {
412 case VK_IMAGE_ASPECT_COLOR_BIT
:
413 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
414 pSubresource
, pLayout
);
416 case VK_IMAGE_ASPECT_DEPTH_BIT
:
417 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
418 pSubresource
, pLayout
);
420 case VK_IMAGE_ASPECT_STENCIL_BIT
:
421 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
422 pSubresource
, pLayout
);
425 assert(!"Invalid image aspect");
429 static struct anv_state
430 alloc_surface_state(struct anv_device
*device
)
432 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
435 static enum isl_channel_select
436 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
437 struct isl_swizzle format_swizzle
)
439 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
443 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
444 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
445 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
446 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
447 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
448 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
450 unreachable("Invalid swizzle");
456 anv_CreateImageView(VkDevice _device
,
457 const VkImageViewCreateInfo
*pCreateInfo
,
458 const VkAllocationCallbacks
*pAllocator
,
461 ANV_FROM_HANDLE(anv_device
, device
, _device
);
462 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
463 struct anv_image_view
*iview
;
465 iview
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*iview
), 8,
466 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
468 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
470 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
472 assert(range
->layerCount
> 0);
473 assert(range
->baseMipLevel
< image
->levels
);
474 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
475 VK_IMAGE_USAGE_STORAGE_BIT
|
476 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
477 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
479 switch (image
->type
) {
481 unreachable("bad VkImageType");
482 case VK_IMAGE_TYPE_1D
:
483 case VK_IMAGE_TYPE_2D
:
484 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
486 case VK_IMAGE_TYPE_3D
:
487 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
488 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
492 const struct anv_surface
*surface
=
493 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
495 iview
->image
= image
;
496 iview
->bo
= image
->bo
;
497 iview
->offset
= image
->offset
+ surface
->offset
;
499 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
500 iview
->vk_format
= pCreateInfo
->format
;
502 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
503 range
->aspectMask
, image
->tiling
);
505 iview
->isl
= (struct isl_view
) {
506 .format
= format
.isl_format
,
507 .base_level
= range
->baseMipLevel
,
508 .levels
= anv_get_levelCount(image
, range
),
509 .base_array_layer
= range
->baseArrayLayer
,
510 .array_len
= anv_get_layerCount(image
, range
),
512 .r
= remap_swizzle(pCreateInfo
->components
.r
,
513 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
514 .g
= remap_swizzle(pCreateInfo
->components
.g
,
515 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
516 .b
= remap_swizzle(pCreateInfo
->components
.b
,
517 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
518 .a
= remap_swizzle(pCreateInfo
->components
.a
,
519 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
523 iview
->extent
= (VkExtent3D
) {
524 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
525 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
526 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
529 if (image
->type
== VK_IMAGE_TYPE_3D
) {
530 iview
->isl
.base_array_layer
= 0;
531 iview
->isl
.array_len
= iview
->extent
.depth
;
534 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
535 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
536 iview
->isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
538 iview
->isl
.usage
= 0;
541 /* If the HiZ buffer can be sampled from, set the constant clear color.
542 * If it cannot, disable the isl aux usage flag.
544 float red_clear_color
= 0.0f
;
545 enum isl_aux_usage surf_usage
= image
->aux_usage
;
546 if (image
->aux_usage
== ISL_AUX_USAGE_HIZ
) {
547 if (iview
->aspect_mask
& VK_IMAGE_ASPECT_DEPTH_BIT
&&
548 anv_can_sample_with_hiz(device
->info
.gen
, image
->samples
)) {
549 /* When a HiZ buffer is sampled on gen9+, ensure that
550 * the constant fast clear value is set in the surface state.
552 if (device
->info
.gen
>= 9)
553 red_clear_color
= ANV_HZ_FC_VAL
;
555 surf_usage
= ISL_AUX_USAGE_NONE
;
559 /* Input attachment surfaces for color are allocated and filled
560 * out at BeginRenderPass time because they need compression information.
561 * Compression is not yet enabled for depth textures and stencil doesn't
562 * allow compression so we can just use the texture surface state from the
565 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
||
566 (image
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
&&
567 !(iview
->aspect_mask
& VK_IMAGE_ASPECT_COLOR_BIT
))) {
568 iview
->sampler_surface_state
= alloc_surface_state(device
);
570 struct isl_view view
= iview
->isl
;
571 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
572 isl_surf_fill_state(&device
->isl_dev
,
573 iview
->sampler_surface_state
.map
,
574 .surf
= &surface
->isl
,
576 .clear_color
.f32
= { red_clear_color
,},
577 .aux_surf
= &image
->aux_surface
.isl
,
578 .aux_usage
= surf_usage
,
579 .mocs
= device
->default_mocs
);
581 if (!device
->info
.has_llc
)
582 anv_state_clflush(iview
->sampler_surface_state
);
584 iview
->sampler_surface_state
.alloc_size
= 0;
587 /* NOTE: This one needs to go last since it may stomp isl_view.format */
588 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
589 iview
->storage_surface_state
= alloc_surface_state(device
);
591 if (isl_has_matching_typed_storage_image_format(&device
->info
,
592 format
.isl_format
)) {
593 struct isl_view view
= iview
->isl
;
594 view
.usage
|= ISL_SURF_USAGE_STORAGE_BIT
;
595 view
.format
= isl_lower_storage_image_format(&device
->info
,
597 isl_surf_fill_state(&device
->isl_dev
,
598 iview
->storage_surface_state
.map
,
599 .surf
= &surface
->isl
,
601 .aux_surf
= &image
->aux_surface
.isl
,
602 .aux_usage
= surf_usage
,
603 .mocs
= device
->default_mocs
);
605 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
608 iview
->bo
->size
- iview
->offset
, 1);
611 isl_surf_fill_image_param(&device
->isl_dev
,
612 &iview
->storage_image_param
,
613 &surface
->isl
, &iview
->isl
);
615 if (!device
->info
.has_llc
)
616 anv_state_clflush(iview
->storage_surface_state
);
618 iview
->storage_surface_state
.alloc_size
= 0;
621 *pView
= anv_image_view_to_handle(iview
);
627 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
628 const VkAllocationCallbacks
*pAllocator
)
630 ANV_FROM_HANDLE(anv_device
, device
, _device
);
631 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
636 if (iview
->sampler_surface_state
.alloc_size
> 0) {
637 anv_state_pool_free(&device
->surface_state_pool
,
638 iview
->sampler_surface_state
);
641 if (iview
->storage_surface_state
.alloc_size
> 0) {
642 anv_state_pool_free(&device
->surface_state_pool
,
643 iview
->storage_surface_state
);
646 vk_free2(&device
->alloc
, pAllocator
, iview
);
651 anv_CreateBufferView(VkDevice _device
,
652 const VkBufferViewCreateInfo
*pCreateInfo
,
653 const VkAllocationCallbacks
*pAllocator
,
656 ANV_FROM_HANDLE(anv_device
, device
, _device
);
657 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
658 struct anv_buffer_view
*view
;
660 view
= vk_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
661 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
663 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
665 /* TODO: Handle the format swizzle? */
667 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
668 VK_IMAGE_ASPECT_COLOR_BIT
,
669 VK_IMAGE_TILING_LINEAR
);
670 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
671 view
->bo
= buffer
->bo
;
672 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
673 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
674 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
675 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
677 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
678 view
->surface_state
= alloc_surface_state(device
);
680 anv_fill_buffer_surface_state(device
, view
->surface_state
,
682 view
->offset
, view
->range
, format_bs
);
684 view
->surface_state
= (struct anv_state
){ 0 };
687 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
688 view
->storage_surface_state
= alloc_surface_state(device
);
690 enum isl_format storage_format
=
691 isl_has_matching_typed_storage_image_format(&device
->info
,
693 isl_lower_storage_image_format(&device
->info
, view
->format
) :
696 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
698 view
->offset
, view
->range
,
699 (storage_format
== ISL_FORMAT_RAW
? 1 :
700 isl_format_get_layout(storage_format
)->bpb
/ 8));
702 isl_buffer_fill_image_param(&device
->isl_dev
,
703 &view
->storage_image_param
,
704 view
->format
, view
->range
);
706 view
->storage_surface_state
= (struct anv_state
){ 0 };
709 *pView
= anv_buffer_view_to_handle(view
);
715 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
716 const VkAllocationCallbacks
*pAllocator
)
718 ANV_FROM_HANDLE(anv_device
, device
, _device
);
719 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
724 if (view
->surface_state
.alloc_size
> 0)
725 anv_state_pool_free(&device
->surface_state_pool
,
726 view
->surface_state
);
728 if (view
->storage_surface_state
.alloc_size
> 0)
729 anv_state_pool_free(&device
->surface_state_pool
,
730 view
->storage_surface_state
);
732 vk_free2(&device
->alloc
, pAllocator
, view
);
735 const struct anv_surface
*
736 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
737 VkImageAspectFlags aspect_mask
)
739 switch (aspect_mask
) {
740 case VK_IMAGE_ASPECT_COLOR_BIT
:
741 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
742 return &image
->color_surface
;
743 case VK_IMAGE_ASPECT_DEPTH_BIT
:
744 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
745 return &image
->depth_surface
;
746 case VK_IMAGE_ASPECT_STENCIL_BIT
:
747 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
748 return &image
->stencil_surface
;
749 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
750 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
751 * combined depth stencil formats. Specifically, it states:
753 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
754 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
756 * Image views with both depth and stencil aspects are only valid for
757 * render target attachments, in which case
758 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
759 * stencil surfaces from the underlying surface.
761 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
762 return &image
->depth_surface
;
764 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
765 return &image
->stencil_surface
;
768 unreachable("image does not have aspect");