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 /* FINISHME: Support aux surfaces */
45 isl_usage
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
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_usage
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
57 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
59 if (vk_usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
62 unreachable("bad VkImageAspect");
63 case VK_IMAGE_ASPECT_DEPTH_BIT
:
64 isl_usage
&= ~ISL_SURF_USAGE_DISABLE_AUX_BIT
;
65 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
67 case VK_IMAGE_ASPECT_STENCIL_BIT
:
68 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
73 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
74 /* Meta 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 /* Meta 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
= MAX(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 (image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
182 /* Allow the user to control HiZ enabling. Disable by default on gen7
183 * because resolves are not currently implemented pre-BDW.
185 if (!env_var_as_boolean("INTEL_VK_HIZ", dev
->info
.gen
>= 8)) {
186 anv_finishme("Implement gen7 HiZ");
187 } else if (vk_info
->mipLevels
> 1) {
188 anv_finishme("Test multi-LOD HiZ");
189 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
190 anv_finishme("Test gen8 multisampled HiZ");
192 isl_surf_get_hiz_surf(&dev
->isl_dev
, &image
->depth_surface
.isl
,
193 &image
->hiz_surface
.isl
);
194 add_surface(image
, &image
->hiz_surface
);
202 * Parameter @a format is required and overrides VkImageCreateInfo::format.
204 static VkImageUsageFlags
205 anv_image_get_full_usage(const VkImageCreateInfo
*info
,
206 VkImageAspectFlags aspects
)
208 VkImageUsageFlags usage
= info
->usage
;
210 if (info
->samples
> 1 &&
211 (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)) {
212 /* Meta will resolve the image by binding it as a texture. */
213 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
216 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
217 /* Meta will transfer from the image by binding it as a texture. */
218 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
221 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
222 /* For non-clear transfer operations, meta will transfer to the image by
223 * binding it as a color attachment, even if the image format is not
226 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
228 if (aspects
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
229 /* vkCmdClearDepthStencilImage() only requires that
230 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
231 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
232 * clears the image, though, by binding it as a depthstencil
235 usage
|= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
;
243 anv_image_create(VkDevice _device
,
244 const struct anv_image_create_info
*create_info
,
245 const VkAllocationCallbacks
* alloc
,
248 ANV_FROM_HANDLE(anv_device
, device
, _device
);
249 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
250 struct anv_image
*image
= NULL
;
253 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
255 anv_assert(pCreateInfo
->mipLevels
> 0);
256 anv_assert(pCreateInfo
->arrayLayers
> 0);
257 anv_assert(pCreateInfo
->samples
> 0);
258 anv_assert(pCreateInfo
->extent
.width
> 0);
259 anv_assert(pCreateInfo
->extent
.height
> 0);
260 anv_assert(pCreateInfo
->extent
.depth
> 0);
262 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
263 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
265 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
267 memset(image
, 0, sizeof(*image
));
268 image
->type
= pCreateInfo
->imageType
;
269 image
->extent
= pCreateInfo
->extent
;
270 image
->vk_format
= pCreateInfo
->format
;
271 image
->aspects
= vk_format_aspects(image
->vk_format
);
272 image
->levels
= pCreateInfo
->mipLevels
;
273 image
->array_size
= pCreateInfo
->arrayLayers
;
274 image
->samples
= pCreateInfo
->samples
;
275 image
->usage
= anv_image_get_full_usage(pCreateInfo
, image
->aspects
);
276 image
->tiling
= pCreateInfo
->tiling
;
279 for_each_bit(b
, image
->aspects
) {
280 r
= make_surface(device
, image
, create_info
, (1 << b
));
285 *pImage
= anv_image_to_handle(image
);
291 anv_free2(&device
->alloc
, alloc
, image
);
297 anv_CreateImage(VkDevice device
,
298 const VkImageCreateInfo
*pCreateInfo
,
299 const VkAllocationCallbacks
*pAllocator
,
302 return anv_image_create(device
,
303 &(struct anv_image_create_info
) {
304 .vk_info
= pCreateInfo
,
311 anv_DestroyImage(VkDevice _device
, VkImage _image
,
312 const VkAllocationCallbacks
*pAllocator
)
314 ANV_FROM_HANDLE(anv_device
, device
, _device
);
316 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
319 VkResult
anv_BindImageMemory(
322 VkDeviceMemory _memory
,
323 VkDeviceSize memoryOffset
)
325 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
326 ANV_FROM_HANDLE(anv_image
, image
, _image
);
329 image
->bo
= &mem
->bo
;
330 image
->offset
= memoryOffset
;
340 anv_surface_get_subresource_layout(struct anv_image
*image
,
341 struct anv_surface
*surface
,
342 const VkImageSubresource
*subresource
,
343 VkSubresourceLayout
*layout
)
345 /* If we are on a non-zero mip level or array slice, we need to
346 * calculate a real offset.
348 anv_assert(subresource
->mipLevel
== 0);
349 anv_assert(subresource
->arrayLayer
== 0);
351 layout
->offset
= surface
->offset
;
352 layout
->rowPitch
= surface
->isl
.row_pitch
;
353 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
354 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
355 layout
->size
= surface
->isl
.size
;
358 void anv_GetImageSubresourceLayout(
361 const VkImageSubresource
* pSubresource
,
362 VkSubresourceLayout
* pLayout
)
364 ANV_FROM_HANDLE(anv_image
, image
, _image
);
366 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
368 switch (pSubresource
->aspectMask
) {
369 case VK_IMAGE_ASPECT_COLOR_BIT
:
370 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
371 pSubresource
, pLayout
);
373 case VK_IMAGE_ASPECT_DEPTH_BIT
:
374 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
375 pSubresource
, pLayout
);
377 case VK_IMAGE_ASPECT_STENCIL_BIT
:
378 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
379 pSubresource
, pLayout
);
382 assert(!"Invalid image aspect");
386 static struct anv_state
387 alloc_surface_state(struct anv_device
*device
,
388 struct anv_cmd_buffer
*cmd_buffer
)
391 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
393 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
397 static enum isl_channel_select
398 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
399 struct isl_swizzle format_swizzle
)
401 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
405 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
406 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
407 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
408 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
409 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
410 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
412 unreachable("Invalid swizzle");
417 anv_image_view_init(struct anv_image_view
*iview
,
418 struct anv_device
*device
,
419 const VkImageViewCreateInfo
* pCreateInfo
,
420 struct anv_cmd_buffer
*cmd_buffer
,
421 VkImageUsageFlags usage_mask
)
423 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
424 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
426 assert(range
->layerCount
> 0);
427 assert(range
->baseMipLevel
< image
->levels
);
428 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
429 VK_IMAGE_USAGE_STORAGE_BIT
|
430 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
431 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
433 switch (image
->type
) {
435 unreachable("bad VkImageType");
436 case VK_IMAGE_TYPE_1D
:
437 case VK_IMAGE_TYPE_2D
:
438 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
440 case VK_IMAGE_TYPE_3D
:
441 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
442 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
446 const struct anv_surface
*surface
=
447 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
449 iview
->image
= image
;
450 iview
->bo
= image
->bo
;
451 iview
->offset
= image
->offset
+ surface
->offset
;
453 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
454 iview
->vk_format
= pCreateInfo
->format
;
456 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
457 range
->aspectMask
, image
->tiling
);
459 iview
->base_layer
= range
->baseArrayLayer
;
460 iview
->base_mip
= range
->baseMipLevel
;
462 struct isl_view isl_view
= {
463 .format
= format
.isl_format
,
464 .base_level
= range
->baseMipLevel
,
465 .levels
= anv_get_levelCount(image
, range
),
466 .base_array_layer
= range
->baseArrayLayer
,
467 .array_len
= anv_get_layerCount(image
, range
),
469 .r
= remap_swizzle(pCreateInfo
->components
.r
,
470 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
471 .g
= remap_swizzle(pCreateInfo
->components
.g
,
472 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
473 .b
= remap_swizzle(pCreateInfo
->components
.b
,
474 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
475 .a
= remap_swizzle(pCreateInfo
->components
.a
,
476 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
480 iview
->extent
= (VkExtent3D
) {
481 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
482 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
483 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
486 isl_surf_usage_flags_t cube_usage
;
487 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
488 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
489 cube_usage
= ISL_SURF_USAGE_CUBE_BIT
;
494 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
495 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
497 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_TEXTURE_BIT
;
498 isl_surf_fill_state(&device
->isl_dev
,
499 iview
->sampler_surface_state
.map
,
500 .surf
= &surface
->isl
,
502 .mocs
= device
->default_mocs
);
504 if (!device
->info
.has_llc
)
505 anv_state_clflush(iview
->sampler_surface_state
);
507 iview
->sampler_surface_state
.alloc_size
= 0;
510 /* This is kind-of hackish. It is possible, due to get_full_usage above,
511 * to get a surface state with a non-renderable format but with
512 * VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT. This happens in particular for
513 * formats which aren't renderable but where we want to use Vulkan copy
514 * commands so VK_IMAGE_USAGE_TRANSFER_DST_BIT is set. In the case of a
515 * copy, meta will use a format that we can render to, but most of the rest
516 * of the time, we don't want to create those surface states. Once we
517 * start using blorp for copies, this problem will go away and we can
518 * remove a lot of hacks.
520 if ((image
->usage
& usage_mask
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) &&
521 isl_format_supports_rendering(&device
->info
, isl_view
.format
)) {
522 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
524 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_RENDER_TARGET_BIT
;
525 isl_surf_fill_state(&device
->isl_dev
,
526 iview
->color_rt_surface_state
.map
,
527 .surf
= &surface
->isl
,
529 .mocs
= device
->default_mocs
);
531 if (!device
->info
.has_llc
)
532 anv_state_clflush(iview
->color_rt_surface_state
);
534 iview
->color_rt_surface_state
.alloc_size
= 0;
537 /* NOTE: This one needs to go last since it may stomp isl_view.format */
538 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_STORAGE_BIT
) {
539 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
541 if (isl_has_matching_typed_storage_image_format(&device
->info
,
542 format
.isl_format
)) {
543 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_STORAGE_BIT
;
544 isl_view
.format
= isl_lower_storage_image_format(&device
->info
,
546 if (image
->type
== VK_IMAGE_TYPE_3D
) {
547 isl_view
.base_array_layer
= 0;
548 isl_view
.array_len
= iview
->extent
.depth
;
550 isl_surf_fill_state(&device
->isl_dev
,
551 iview
->storage_surface_state
.map
,
552 .surf
= &surface
->isl
,
554 .mocs
= device
->default_mocs
);
556 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
559 iview
->bo
->size
- iview
->offset
, 1);
562 isl_surf_fill_image_param(&device
->isl_dev
,
563 &iview
->storage_image_param
,
564 &surface
->isl
, &isl_view
);
566 if (!device
->info
.has_llc
)
567 anv_state_clflush(iview
->storage_surface_state
);
569 iview
->storage_surface_state
.alloc_size
= 0;
574 anv_CreateImageView(VkDevice _device
,
575 const VkImageViewCreateInfo
*pCreateInfo
,
576 const VkAllocationCallbacks
*pAllocator
,
579 ANV_FROM_HANDLE(anv_device
, device
, _device
);
580 struct anv_image_view
*view
;
582 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
583 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
585 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
587 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, ~0);
589 *pView
= anv_image_view_to_handle(view
);
595 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
596 const VkAllocationCallbacks
*pAllocator
)
598 ANV_FROM_HANDLE(anv_device
, device
, _device
);
599 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
601 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
602 anv_state_pool_free(&device
->surface_state_pool
,
603 iview
->color_rt_surface_state
);
606 if (iview
->sampler_surface_state
.alloc_size
> 0) {
607 anv_state_pool_free(&device
->surface_state_pool
,
608 iview
->sampler_surface_state
);
611 if (iview
->storage_surface_state
.alloc_size
> 0) {
612 anv_state_pool_free(&device
->surface_state_pool
,
613 iview
->storage_surface_state
);
616 anv_free2(&device
->alloc
, pAllocator
, iview
);
620 void anv_buffer_view_init(struct anv_buffer_view
*view
,
621 struct anv_device
*device
,
622 const VkBufferViewCreateInfo
* pCreateInfo
,
623 struct anv_cmd_buffer
*cmd_buffer
)
625 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
627 /* TODO: Handle the format swizzle? */
629 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
630 VK_IMAGE_ASPECT_COLOR_BIT
,
631 VK_IMAGE_TILING_LINEAR
);
632 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
633 view
->bo
= buffer
->bo
;
634 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
635 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
636 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
637 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
639 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
640 view
->surface_state
= alloc_surface_state(device
, cmd_buffer
);
642 anv_fill_buffer_surface_state(device
, view
->surface_state
,
644 view
->offset
, view
->range
, format_bs
);
646 view
->surface_state
= (struct anv_state
){ 0 };
649 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
650 view
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
652 enum isl_format storage_format
=
653 isl_has_matching_typed_storage_image_format(&device
->info
,
655 isl_lower_storage_image_format(&device
->info
, view
->format
) :
658 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
660 view
->offset
, view
->range
,
661 (storage_format
== ISL_FORMAT_RAW
? 1 :
662 isl_format_get_layout(storage_format
)->bpb
/ 8));
664 isl_buffer_fill_image_param(&device
->isl_dev
,
665 &view
->storage_image_param
,
666 view
->format
, view
->range
);
668 view
->storage_surface_state
= (struct anv_state
){ 0 };
673 anv_CreateBufferView(VkDevice _device
,
674 const VkBufferViewCreateInfo
*pCreateInfo
,
675 const VkAllocationCallbacks
*pAllocator
,
678 ANV_FROM_HANDLE(anv_device
, device
, _device
);
679 struct anv_buffer_view
*view
;
681 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
682 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
684 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
686 anv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
688 *pView
= anv_buffer_view_to_handle(view
);
694 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
695 const VkAllocationCallbacks
*pAllocator
)
697 ANV_FROM_HANDLE(anv_device
, device
, _device
);
698 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
700 if (view
->surface_state
.alloc_size
> 0)
701 anv_state_pool_free(&device
->surface_state_pool
,
702 view
->surface_state
);
704 if (view
->storage_surface_state
.alloc_size
> 0)
705 anv_state_pool_free(&device
->surface_state_pool
,
706 view
->storage_surface_state
);
708 anv_free2(&device
->alloc
, pAllocator
, view
);
711 const struct anv_surface
*
712 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
713 VkImageAspectFlags aspect_mask
)
715 switch (aspect_mask
) {
716 case VK_IMAGE_ASPECT_COLOR_BIT
:
717 /* Dragons will eat you.
719 * Meta attaches all destination surfaces as color render targets. Guess
720 * what surface the Meta Dragons really want.
722 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
723 return &image
->depth_surface
;
724 } else if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
725 return &image
->stencil_surface
;
727 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
728 return &image
->color_surface
;
731 case VK_IMAGE_ASPECT_DEPTH_BIT
:
732 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
733 return &image
->depth_surface
;
734 case VK_IMAGE_ASPECT_STENCIL_BIT
:
735 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
736 return &image
->stencil_surface
;
737 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
738 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
739 * combined depth stencil formats. Specifically, it states:
741 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
742 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
744 * Image views with both depth and stencil aspects are only valid for
745 * render target attachments, in which case
746 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
747 * stencil surfaces from the underlying surface.
749 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
750 return &image
->depth_surface
;
752 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
753 return &image
->stencil_surface
;
756 unreachable("image does not have aspect");