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"
32 #include "vk_format_info.h"
35 * Exactly one bit must be set in \a aspect.
37 static isl_surf_usage_flags_t
38 choose_isl_surf_usage(VkImageUsageFlags vk_usage
,
39 VkImageAspectFlags aspect
)
41 isl_surf_usage_flags_t isl_usage
= 0;
43 /* FINISHME: Support aux surfaces */
44 isl_usage
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
46 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
47 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
49 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
50 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
52 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
53 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
55 if (vk_usage
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
56 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
58 if (vk_usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
61 unreachable("bad VkImageAspect");
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
;
71 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
72 /* Meta implements transfers by sampling from the source image. */
73 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
76 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
77 /* Meta implements transfers by rendering into the destination image. */
78 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
85 * Exactly one bit must be set in \a aspect.
87 static struct anv_surface
*
88 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
92 unreachable("bad VkImageAspect");
93 case VK_IMAGE_ASPECT_COLOR_BIT
:
94 return &image
->color_surface
;
95 case VK_IMAGE_ASPECT_DEPTH_BIT
:
96 return &image
->depth_surface
;
97 case VK_IMAGE_ASPECT_STENCIL_BIT
:
98 return &image
->stencil_surface
;
103 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
104 * image's memory requirements (that is, the image's size and alignment).
106 * Exactly one bit must be set in \a aspect.
109 make_surface(const struct anv_device
*dev
,
110 struct anv_image
*image
,
111 const struct anv_image_create_info
*anv_info
,
112 VkImageAspectFlags aspect
)
114 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
117 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
118 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
119 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
120 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
123 isl_tiling_flags_t tiling_flags
= anv_info
->isl_tiling_flags
;
124 if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
)
125 tiling_flags
= ISL_TILING_LINEAR_BIT
;
127 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
129 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
132 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
133 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
134 .format
= anv_get_isl_format(&dev
->info
, vk_info
->format
,
135 aspect
, vk_info
->tiling
),
136 .width
= image
->extent
.width
,
137 .height
= image
->extent
.height
,
138 .depth
= image
->extent
.depth
,
139 .levels
= vk_info
->mipLevels
,
140 .array_len
= vk_info
->arrayLayers
,
141 .samples
= vk_info
->samples
,
143 .min_pitch
= anv_info
->stride
,
144 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
145 .tiling_flags
= tiling_flags
);
147 /* isl_surf_init() will fail only if provided invalid input. Invalid input
148 * is illegal in Vulkan.
152 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
153 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
154 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
160 * Parameter @a format is required and overrides VkImageCreateInfo::format.
162 static VkImageUsageFlags
163 anv_image_get_full_usage(const VkImageCreateInfo
*info
,
164 VkImageAspectFlags aspects
)
166 VkImageUsageFlags usage
= info
->usage
;
168 if (info
->samples
> 1 &&
169 (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)) {
170 /* Meta will resolve the image by binding it as a texture. */
171 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
174 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
175 /* Meta will transfer from the image by binding it as a texture. */
176 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
179 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
180 /* For non-clear transfer operations, meta will transfer to the image by
181 * binding it as a color attachment, even if the image format is not
184 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
186 if (aspects
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
187 /* vkCmdClearDepthStencilImage() only requires that
188 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
189 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
190 * clears the image, though, by binding it as a depthstencil
193 usage
|= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
;
201 anv_image_create(VkDevice _device
,
202 const struct anv_image_create_info
*create_info
,
203 const VkAllocationCallbacks
* alloc
,
206 ANV_FROM_HANDLE(anv_device
, device
, _device
);
207 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
208 struct anv_image
*image
= NULL
;
211 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
213 anv_assert(pCreateInfo
->mipLevels
> 0);
214 anv_assert(pCreateInfo
->arrayLayers
> 0);
215 anv_assert(pCreateInfo
->samples
> 0);
216 anv_assert(pCreateInfo
->extent
.width
> 0);
217 anv_assert(pCreateInfo
->extent
.height
> 0);
218 anv_assert(pCreateInfo
->extent
.depth
> 0);
220 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
221 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
223 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
225 memset(image
, 0, sizeof(*image
));
226 image
->type
= pCreateInfo
->imageType
;
227 image
->extent
= pCreateInfo
->extent
;
228 image
->vk_format
= pCreateInfo
->format
;
229 image
->aspects
= vk_format_aspects(image
->vk_format
);
230 image
->levels
= pCreateInfo
->mipLevels
;
231 image
->array_size
= pCreateInfo
->arrayLayers
;
232 image
->samples
= pCreateInfo
->samples
;
233 image
->usage
= anv_image_get_full_usage(pCreateInfo
, image
->aspects
);
234 image
->tiling
= pCreateInfo
->tiling
;
237 for_each_bit(b
, image
->aspects
) {
238 r
= make_surface(device
, image
, create_info
, (1 << b
));
243 *pImage
= anv_image_to_handle(image
);
249 anv_free2(&device
->alloc
, alloc
, image
);
255 anv_CreateImage(VkDevice device
,
256 const VkImageCreateInfo
*pCreateInfo
,
257 const VkAllocationCallbacks
*pAllocator
,
260 return anv_image_create(device
,
261 &(struct anv_image_create_info
) {
262 .vk_info
= pCreateInfo
,
263 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
270 anv_DestroyImage(VkDevice _device
, VkImage _image
,
271 const VkAllocationCallbacks
*pAllocator
)
273 ANV_FROM_HANDLE(anv_device
, device
, _device
);
275 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
279 anv_surface_get_subresource_layout(struct anv_image
*image
,
280 struct anv_surface
*surface
,
281 const VkImageSubresource
*subresource
,
282 VkSubresourceLayout
*layout
)
284 /* If we are on a non-zero mip level or array slice, we need to
285 * calculate a real offset.
287 anv_assert(subresource
->mipLevel
== 0);
288 anv_assert(subresource
->arrayLayer
== 0);
290 layout
->offset
= surface
->offset
;
291 layout
->rowPitch
= surface
->isl
.row_pitch
;
292 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
293 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
294 layout
->size
= surface
->isl
.size
;
297 void anv_GetImageSubresourceLayout(
300 const VkImageSubresource
* pSubresource
,
301 VkSubresourceLayout
* pLayout
)
303 ANV_FROM_HANDLE(anv_image
, image
, _image
);
305 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
307 switch (pSubresource
->aspectMask
) {
308 case VK_IMAGE_ASPECT_COLOR_BIT
:
309 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
310 pSubresource
, pLayout
);
312 case VK_IMAGE_ASPECT_DEPTH_BIT
:
313 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
314 pSubresource
, pLayout
);
316 case VK_IMAGE_ASPECT_STENCIL_BIT
:
317 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
318 pSubresource
, pLayout
);
321 assert(!"Invalid image aspect");
326 anv_validate_CreateImageView(VkDevice _device
,
327 const VkImageViewCreateInfo
*pCreateInfo
,
328 const VkAllocationCallbacks
*pAllocator
,
331 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
332 const VkImageSubresourceRange
*subresource
;
334 /* Validate structure type before dereferencing it. */
336 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
337 subresource
= &pCreateInfo
->subresourceRange
;
339 /* Validate viewType is in range before using it. */
340 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
341 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
343 /* Validate format is in range before using it. */
344 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
345 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
347 /* Validate channel swizzles. */
348 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
349 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
350 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
351 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
352 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
353 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
354 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
355 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
357 /* Validate subresource. */
358 assert(subresource
->aspectMask
!= 0);
359 assert(subresource
->levelCount
> 0);
360 assert(subresource
->layerCount
> 0);
361 assert(subresource
->baseMipLevel
< image
->levels
);
362 assert(subresource
->baseMipLevel
+ anv_get_levelCount(image
, subresource
) <= image
->levels
);
363 assert(subresource
->baseArrayLayer
< image
->array_size
);
364 assert(subresource
->baseArrayLayer
+ anv_get_layerCount(image
, subresource
) <= image
->array_size
);
367 MAYBE_UNUSED
const VkImageAspectFlags view_format_aspects
=
368 vk_format_aspects(pCreateInfo
->format
);
370 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
371 | VK_IMAGE_ASPECT_STENCIL_BIT
;
373 /* Validate format. */
374 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
375 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
376 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
377 assert(view_format_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
378 } else if (subresource
->aspectMask
& ds_flags
) {
379 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
381 assert(pCreateInfo
->format
== image
->vk_format
);
383 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
384 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
385 assert(view_format_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
388 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
389 /* FINISHME: Is it legal to have an R8 view of S8? */
390 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
391 assert(view_format_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
394 assert(!"bad VkImageSubresourceRange::aspectFlags");
397 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
400 static struct anv_state
401 alloc_surface_state(struct anv_device
*device
,
402 struct anv_cmd_buffer
*cmd_buffer
)
405 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
407 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
411 static enum isl_channel_select
412 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
413 struct anv_format_swizzle format_swizzle
)
415 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
419 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
420 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
421 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
422 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
423 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
424 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
426 unreachable("Invalid swizzle");
431 anv_image_view_init(struct anv_image_view
*iview
,
432 struct anv_device
*device
,
433 const VkImageViewCreateInfo
* pCreateInfo
,
434 struct anv_cmd_buffer
*cmd_buffer
,
435 VkImageUsageFlags usage_mask
)
437 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
438 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
440 assert(range
->layerCount
> 0);
441 assert(range
->baseMipLevel
< image
->levels
);
442 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
443 VK_IMAGE_USAGE_STORAGE_BIT
|
444 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
445 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
447 switch (image
->type
) {
449 unreachable("bad VkImageType");
450 case VK_IMAGE_TYPE_1D
:
451 case VK_IMAGE_TYPE_2D
:
452 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
454 case VK_IMAGE_TYPE_3D
:
455 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
456 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
460 struct anv_surface
*surface
=
461 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
463 iview
->image
= image
;
464 iview
->bo
= image
->bo
;
465 iview
->offset
= image
->offset
+ surface
->offset
;
467 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
468 iview
->vk_format
= pCreateInfo
->format
;
470 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
471 range
->aspectMask
, image
->tiling
);
473 iview
->base_layer
= range
->baseArrayLayer
;
474 iview
->base_mip
= range
->baseMipLevel
;
476 struct isl_view isl_view
= {
477 .format
= format
.isl_format
,
478 .base_level
= range
->baseMipLevel
,
479 .levels
= anv_get_levelCount(image
, range
),
480 .base_array_layer
= range
->baseArrayLayer
,
481 .array_len
= anv_get_layerCount(image
, range
),
483 remap_swizzle(pCreateInfo
->components
.r
,
484 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
485 remap_swizzle(pCreateInfo
->components
.g
,
486 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
487 remap_swizzle(pCreateInfo
->components
.b
,
488 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
489 remap_swizzle(pCreateInfo
->components
.a
,
490 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
494 iview
->extent
= (VkExtent3D
) {
495 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
496 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
497 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
500 isl_surf_usage_flags_t cube_usage
;
501 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
502 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
503 cube_usage
= ISL_SURF_USAGE_CUBE_BIT
;
508 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
509 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
511 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_TEXTURE_BIT
;
512 isl_surf_fill_state(&device
->isl_dev
,
513 iview
->sampler_surface_state
.map
,
514 .surf
= &surface
->isl
,
516 .mocs
= device
->default_mocs
);
518 if (!device
->info
.has_llc
)
519 anv_state_clflush(iview
->sampler_surface_state
);
521 iview
->sampler_surface_state
.alloc_size
= 0;
524 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
525 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
527 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_RENDER_TARGET_BIT
;
528 isl_surf_fill_state(&device
->isl_dev
,
529 iview
->color_rt_surface_state
.map
,
530 .surf
= &surface
->isl
,
532 .mocs
= device
->default_mocs
);
534 if (!device
->info
.has_llc
)
535 anv_state_clflush(iview
->color_rt_surface_state
);
537 iview
->color_rt_surface_state
.alloc_size
= 0;
540 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_STORAGE_BIT
) {
541 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
543 if (isl_has_matching_typed_storage_image_format(&device
->info
,
544 format
.isl_format
)) {
545 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_STORAGE_BIT
;
546 isl_surf_fill_state(&device
->isl_dev
,
547 iview
->storage_surface_state
.map
,
548 .surf
= &surface
->isl
,
550 .mocs
= device
->default_mocs
);
552 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
555 iview
->bo
->size
- iview
->offset
, 1);
558 isl_surf_fill_image_param(&device
->isl_dev
,
559 &iview
->storage_image_param
,
560 &surface
->isl
, &isl_view
);
562 if (!device
->info
.has_llc
)
563 anv_state_clflush(iview
->storage_surface_state
);
565 iview
->storage_surface_state
.alloc_size
= 0;
570 anv_CreateImageView(VkDevice _device
,
571 const VkImageViewCreateInfo
*pCreateInfo
,
572 const VkAllocationCallbacks
*pAllocator
,
575 ANV_FROM_HANDLE(anv_device
, device
, _device
);
576 struct anv_image_view
*view
;
578 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
579 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
583 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, ~0);
585 *pView
= anv_image_view_to_handle(view
);
591 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
592 const VkAllocationCallbacks
*pAllocator
)
594 ANV_FROM_HANDLE(anv_device
, device
, _device
);
595 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
597 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
598 anv_state_pool_free(&device
->surface_state_pool
,
599 iview
->color_rt_surface_state
);
602 if (iview
->sampler_surface_state
.alloc_size
> 0) {
603 anv_state_pool_free(&device
->surface_state_pool
,
604 iview
->sampler_surface_state
);
607 if (iview
->storage_surface_state
.alloc_size
> 0) {
608 anv_state_pool_free(&device
->surface_state_pool
,
609 iview
->storage_surface_state
);
612 anv_free2(&device
->alloc
, pAllocator
, iview
);
616 void anv_buffer_view_init(struct anv_buffer_view
*view
,
617 struct anv_device
*device
,
618 const VkBufferViewCreateInfo
* pCreateInfo
,
619 struct anv_cmd_buffer
*cmd_buffer
)
621 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
623 /* TODO: Handle the format swizzle? */
625 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
626 VK_IMAGE_ASPECT_COLOR_BIT
,
627 VK_IMAGE_TILING_LINEAR
);
628 view
->bo
= buffer
->bo
;
629 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
630 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
631 buffer
->size
- view
->offset
: pCreateInfo
->range
;
633 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
634 view
->surface_state
= alloc_surface_state(device
, cmd_buffer
);
636 anv_fill_buffer_surface_state(device
, view
->surface_state
,
638 view
->offset
, view
->range
,
639 isl_format_get_layout(view
->format
)->bs
);
641 view
->surface_state
= (struct anv_state
){ 0 };
644 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
645 view
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
647 enum isl_format storage_format
=
648 isl_has_matching_typed_storage_image_format(&device
->info
,
650 isl_lower_storage_image_format(&device
->info
, view
->format
) :
653 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
655 view
->offset
, view
->range
,
656 (storage_format
== ISL_FORMAT_RAW
? 1 :
657 isl_format_get_layout(storage_format
)->bs
));
659 isl_buffer_fill_image_param(&device
->isl_dev
,
660 &view
->storage_image_param
,
661 view
->format
, view
->range
);
663 view
->storage_surface_state
= (struct anv_state
){ 0 };
668 anv_CreateBufferView(VkDevice _device
,
669 const VkBufferViewCreateInfo
*pCreateInfo
,
670 const VkAllocationCallbacks
*pAllocator
,
673 ANV_FROM_HANDLE(anv_device
, device
, _device
);
674 struct anv_buffer_view
*view
;
676 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
677 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
679 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
681 anv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
683 *pView
= anv_buffer_view_to_handle(view
);
689 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
690 const VkAllocationCallbacks
*pAllocator
)
692 ANV_FROM_HANDLE(anv_device
, device
, _device
);
693 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
695 if (view
->surface_state
.alloc_size
> 0)
696 anv_state_pool_free(&device
->surface_state_pool
,
697 view
->surface_state
);
699 if (view
->storage_surface_state
.alloc_size
> 0)
700 anv_state_pool_free(&device
->surface_state_pool
,
701 view
->storage_surface_state
);
703 anv_free2(&device
->alloc
, pAllocator
, view
);
707 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
709 switch (aspect_mask
) {
710 case VK_IMAGE_ASPECT_COLOR_BIT
:
711 /* Dragons will eat you.
713 * Meta attaches all destination surfaces as color render targets. Guess
714 * what surface the Meta Dragons really want.
716 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
717 return &image
->depth_surface
;
718 } else if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
719 return &image
->stencil_surface
;
721 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
722 return &image
->color_surface
;
725 case VK_IMAGE_ASPECT_DEPTH_BIT
:
726 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
727 return &image
->depth_surface
;
728 case VK_IMAGE_ASPECT_STENCIL_BIT
:
729 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
730 return &image
->stencil_surface
;
731 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
732 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
733 * combined depth stencil formats. Specifically, it states:
735 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
736 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
738 * Image views with both depth and stencil aspects are only valid for
739 * render target attachments, in which case
740 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
741 * stencil surfaces from the underlying surface.
743 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
744 return &image
->depth_surface
;
746 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
747 return &image
->stencil_surface
;
750 unreachable("image does not have aspect");