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(vk_info
->format
, aspect
, vk_info
->tiling
),
135 .width
= image
->extent
.width
,
136 .height
= image
->extent
.height
,
137 .depth
= image
->extent
.depth
,
138 .levels
= vk_info
->mipLevels
,
139 .array_len
= vk_info
->arrayLayers
,
140 .samples
= vk_info
->samples
,
142 .min_pitch
= anv_info
->stride
,
143 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
144 .tiling_flags
= tiling_flags
);
146 /* isl_surf_init() will fail only if provided invalid input. Invalid input
147 * is illegal in Vulkan.
151 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
152 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
153 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
159 * Parameter @a format is required and overrides VkImageCreateInfo::format.
161 static VkImageUsageFlags
162 anv_image_get_full_usage(const VkImageCreateInfo
*info
,
163 VkImageAspectFlags aspects
)
165 VkImageUsageFlags usage
= info
->usage
;
167 if (info
->samples
> 1 &&
168 (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)) {
169 /* Meta will resolve the image by binding it as a texture. */
170 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
173 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
174 /* Meta will transfer from the image by binding it as a texture. */
175 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
178 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
179 /* For non-clear transfer operations, meta will transfer to the image by
180 * binding it as a color attachment, even if the image format is not
183 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
185 if (aspects
& (VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
)) {
186 /* vkCmdClearDepthStencilImage() only requires that
187 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
188 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
189 * clears the image, though, by binding it as a depthstencil
192 usage
|= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
;
200 anv_image_create(VkDevice _device
,
201 const struct anv_image_create_info
*create_info
,
202 const VkAllocationCallbacks
* alloc
,
205 ANV_FROM_HANDLE(anv_device
, device
, _device
);
206 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
207 struct anv_image
*image
= NULL
;
210 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
212 anv_assert(pCreateInfo
->mipLevels
> 0);
213 anv_assert(pCreateInfo
->arrayLayers
> 0);
214 anv_assert(pCreateInfo
->samples
> 0);
215 anv_assert(pCreateInfo
->extent
.width
> 0);
216 anv_assert(pCreateInfo
->extent
.height
> 0);
217 anv_assert(pCreateInfo
->extent
.depth
> 0);
219 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
220 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
222 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
224 memset(image
, 0, sizeof(*image
));
225 image
->type
= pCreateInfo
->imageType
;
226 image
->extent
= pCreateInfo
->extent
;
227 image
->vk_format
= pCreateInfo
->format
;
228 image
->aspects
= vk_format_aspects(image
->vk_format
);
229 image
->levels
= pCreateInfo
->mipLevels
;
230 image
->array_size
= pCreateInfo
->arrayLayers
;
231 image
->samples
= pCreateInfo
->samples
;
232 image
->usage
= anv_image_get_full_usage(pCreateInfo
, image
->aspects
);
233 image
->tiling
= pCreateInfo
->tiling
;
236 for_each_bit(b
, image
->aspects
) {
237 r
= make_surface(device
, image
, create_info
, (1 << b
));
242 *pImage
= anv_image_to_handle(image
);
248 anv_free2(&device
->alloc
, alloc
, image
);
254 anv_CreateImage(VkDevice device
,
255 const VkImageCreateInfo
*pCreateInfo
,
256 const VkAllocationCallbacks
*pAllocator
,
259 return anv_image_create(device
,
260 &(struct anv_image_create_info
) {
261 .vk_info
= pCreateInfo
,
262 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
269 anv_DestroyImage(VkDevice _device
, VkImage _image
,
270 const VkAllocationCallbacks
*pAllocator
)
272 ANV_FROM_HANDLE(anv_device
, device
, _device
);
274 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
278 anv_surface_get_subresource_layout(struct anv_image
*image
,
279 struct anv_surface
*surface
,
280 const VkImageSubresource
*subresource
,
281 VkSubresourceLayout
*layout
)
283 /* If we are on a non-zero mip level or array slice, we need to
284 * calculate a real offset.
286 anv_assert(subresource
->mipLevel
== 0);
287 anv_assert(subresource
->arrayLayer
== 0);
289 layout
->offset
= surface
->offset
;
290 layout
->rowPitch
= surface
->isl
.row_pitch
;
291 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
292 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
293 layout
->size
= surface
->isl
.size
;
296 void anv_GetImageSubresourceLayout(
299 const VkImageSubresource
* pSubresource
,
300 VkSubresourceLayout
* pLayout
)
302 ANV_FROM_HANDLE(anv_image
, image
, _image
);
304 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
306 switch (pSubresource
->aspectMask
) {
307 case VK_IMAGE_ASPECT_COLOR_BIT
:
308 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
309 pSubresource
, pLayout
);
311 case VK_IMAGE_ASPECT_DEPTH_BIT
:
312 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
313 pSubresource
, pLayout
);
315 case VK_IMAGE_ASPECT_STENCIL_BIT
:
316 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
317 pSubresource
, pLayout
);
320 assert(!"Invalid image aspect");
325 anv_validate_CreateImageView(VkDevice _device
,
326 const VkImageViewCreateInfo
*pCreateInfo
,
327 const VkAllocationCallbacks
*pAllocator
,
330 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
331 const VkImageSubresourceRange
*subresource
;
333 /* Validate structure type before dereferencing it. */
335 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
336 subresource
= &pCreateInfo
->subresourceRange
;
338 /* Validate viewType is in range before using it. */
339 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
340 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
342 /* Validate format is in range before using it. */
343 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
344 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
346 /* Validate channel swizzles. */
347 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
348 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
349 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
350 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
351 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
352 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
353 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
354 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
356 /* Validate subresource. */
357 assert(subresource
->aspectMask
!= 0);
358 assert(subresource
->levelCount
> 0);
359 assert(subresource
->layerCount
> 0);
360 assert(subresource
->baseMipLevel
< image
->levels
);
361 assert(subresource
->baseMipLevel
+ anv_get_levelCount(image
, subresource
) <= image
->levels
);
362 assert(subresource
->baseArrayLayer
< image
->array_size
);
363 assert(subresource
->baseArrayLayer
+ anv_get_layerCount(image
, subresource
) <= image
->array_size
);
366 MAYBE_UNUSED
const VkImageAspectFlags view_format_aspects
=
367 vk_format_aspects(pCreateInfo
->format
);
369 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
370 | VK_IMAGE_ASPECT_STENCIL_BIT
;
372 /* Validate format. */
373 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
374 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
375 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
376 assert(view_format_aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
377 } else if (subresource
->aspectMask
& ds_flags
) {
378 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
380 assert(pCreateInfo
->format
== image
->vk_format
);
382 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
383 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
384 assert(view_format_aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
387 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
388 /* FINISHME: Is it legal to have an R8 view of S8? */
389 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
390 assert(view_format_aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
393 assert(!"bad VkImageSubresourceRange::aspectFlags");
396 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
399 static struct anv_state
400 alloc_surface_state(struct anv_device
*device
,
401 struct anv_cmd_buffer
*cmd_buffer
)
404 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
406 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
410 static enum isl_channel_select
411 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
412 struct anv_format_swizzle format_swizzle
)
414 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
418 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
419 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
420 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
421 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
422 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
423 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
425 unreachable("Invalid swizzle");
430 anv_image_view_init(struct anv_image_view
*iview
,
431 struct anv_device
*device
,
432 const VkImageViewCreateInfo
* pCreateInfo
,
433 struct anv_cmd_buffer
*cmd_buffer
,
434 VkImageUsageFlags usage_mask
)
436 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
437 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
439 assert(range
->layerCount
> 0);
440 assert(range
->baseMipLevel
< image
->levels
);
441 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
442 VK_IMAGE_USAGE_STORAGE_BIT
|
443 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
444 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
446 switch (image
->type
) {
448 unreachable("bad VkImageType");
449 case VK_IMAGE_TYPE_1D
:
450 case VK_IMAGE_TYPE_2D
:
451 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
453 case VK_IMAGE_TYPE_3D
:
454 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
455 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
459 struct anv_surface
*surface
=
460 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
462 iview
->image
= image
;
463 iview
->bo
= image
->bo
;
464 iview
->offset
= image
->offset
+ surface
->offset
;
466 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
467 iview
->vk_format
= pCreateInfo
->format
;
469 struct anv_format format
=
470 anv_get_format(pCreateInfo
->format
, range
->aspectMask
, image
->tiling
);
472 iview
->base_layer
= range
->baseArrayLayer
;
473 iview
->base_mip
= range
->baseMipLevel
;
475 struct isl_view isl_view
= {
476 .format
= format
.isl_format
,
477 .base_level
= range
->baseMipLevel
,
478 .levels
= anv_get_levelCount(image
, range
),
479 .base_array_layer
= range
->baseArrayLayer
,
480 .array_len
= anv_get_layerCount(image
, range
),
482 remap_swizzle(pCreateInfo
->components
.r
,
483 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
484 remap_swizzle(pCreateInfo
->components
.g
,
485 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
486 remap_swizzle(pCreateInfo
->components
.b
,
487 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
488 remap_swizzle(pCreateInfo
->components
.a
,
489 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
493 iview
->extent
= (VkExtent3D
) {
494 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
495 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
496 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
499 isl_surf_usage_flags_t cube_usage
;
500 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
501 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
502 cube_usage
= ISL_SURF_USAGE_CUBE_BIT
;
507 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
508 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
510 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_TEXTURE_BIT
;
511 isl_surf_fill_state(&device
->isl_dev
,
512 iview
->sampler_surface_state
.map
,
513 .surf
= &surface
->isl
,
515 .mocs
= device
->default_mocs
);
517 if (!device
->info
.has_llc
)
518 anv_state_clflush(iview
->sampler_surface_state
);
520 iview
->sampler_surface_state
.alloc_size
= 0;
523 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
524 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
526 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_RENDER_TARGET_BIT
;
527 isl_surf_fill_state(&device
->isl_dev
,
528 iview
->color_rt_surface_state
.map
,
529 .surf
= &surface
->isl
,
531 .mocs
= device
->default_mocs
);
533 if (!device
->info
.has_llc
)
534 anv_state_clflush(iview
->color_rt_surface_state
);
536 iview
->color_rt_surface_state
.alloc_size
= 0;
539 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_STORAGE_BIT
) {
540 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
542 if (isl_has_matching_typed_storage_image_format(&device
->info
,
543 format
.isl_format
)) {
544 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_STORAGE_BIT
;
545 isl_surf_fill_state(&device
->isl_dev
,
546 iview
->storage_surface_state
.map
,
547 .surf
= &surface
->isl
,
549 .mocs
= device
->default_mocs
);
551 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
554 iview
->bo
->size
- iview
->offset
, 1);
557 isl_surf_fill_image_param(&device
->isl_dev
,
558 &iview
->storage_image_param
,
559 &surface
->isl
, &isl_view
);
561 if (!device
->info
.has_llc
)
562 anv_state_clflush(iview
->storage_surface_state
);
564 iview
->storage_surface_state
.alloc_size
= 0;
569 anv_CreateImageView(VkDevice _device
,
570 const VkImageViewCreateInfo
*pCreateInfo
,
571 const VkAllocationCallbacks
*pAllocator
,
574 ANV_FROM_HANDLE(anv_device
, device
, _device
);
575 struct anv_image_view
*view
;
577 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
578 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
580 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
582 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, ~0);
584 *pView
= anv_image_view_to_handle(view
);
590 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
591 const VkAllocationCallbacks
*pAllocator
)
593 ANV_FROM_HANDLE(anv_device
, device
, _device
);
594 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
596 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
597 anv_state_pool_free(&device
->surface_state_pool
,
598 iview
->color_rt_surface_state
);
601 if (iview
->sampler_surface_state
.alloc_size
> 0) {
602 anv_state_pool_free(&device
->surface_state_pool
,
603 iview
->sampler_surface_state
);
606 if (iview
->storage_surface_state
.alloc_size
> 0) {
607 anv_state_pool_free(&device
->surface_state_pool
,
608 iview
->storage_surface_state
);
611 anv_free2(&device
->alloc
, pAllocator
, iview
);
615 void anv_buffer_view_init(struct anv_buffer_view
*view
,
616 struct anv_device
*device
,
617 const VkBufferViewCreateInfo
* pCreateInfo
,
618 struct anv_cmd_buffer
*cmd_buffer
)
620 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
622 /* TODO: Handle the format swizzle? */
624 view
->format
= anv_get_isl_format(pCreateInfo
->format
,
625 VK_IMAGE_ASPECT_COLOR_BIT
,
626 VK_IMAGE_TILING_LINEAR
);
627 view
->bo
= buffer
->bo
;
628 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
629 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
630 buffer
->size
- view
->offset
: pCreateInfo
->range
;
632 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
633 view
->surface_state
= alloc_surface_state(device
, cmd_buffer
);
635 anv_fill_buffer_surface_state(device
, view
->surface_state
,
637 view
->offset
, view
->range
,
638 isl_format_get_layout(view
->format
)->bs
);
640 view
->surface_state
= (struct anv_state
){ 0 };
643 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
644 view
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
646 enum isl_format storage_format
=
647 isl_has_matching_typed_storage_image_format(&device
->info
,
649 isl_lower_storage_image_format(&device
->info
, view
->format
) :
652 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
654 view
->offset
, view
->range
,
655 (storage_format
== ISL_FORMAT_RAW
? 1 :
656 isl_format_get_layout(storage_format
)->bs
));
658 isl_buffer_fill_image_param(&device
->isl_dev
,
659 &view
->storage_image_param
,
660 view
->format
, view
->range
);
662 view
->storage_surface_state
= (struct anv_state
){ 0 };
667 anv_CreateBufferView(VkDevice _device
,
668 const VkBufferViewCreateInfo
*pCreateInfo
,
669 const VkAllocationCallbacks
*pAllocator
,
672 ANV_FROM_HANDLE(anv_device
, device
, _device
);
673 struct anv_buffer_view
*view
;
675 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
676 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
678 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
680 anv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
682 *pView
= anv_buffer_view_to_handle(view
);
688 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
689 const VkAllocationCallbacks
*pAllocator
)
691 ANV_FROM_HANDLE(anv_device
, device
, _device
);
692 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
694 if (view
->surface_state
.alloc_size
> 0)
695 anv_state_pool_free(&device
->surface_state_pool
,
696 view
->surface_state
);
698 if (view
->storage_surface_state
.alloc_size
> 0)
699 anv_state_pool_free(&device
->surface_state_pool
,
700 view
->storage_surface_state
);
702 anv_free2(&device
->alloc
, pAllocator
, view
);
706 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
708 switch (aspect_mask
) {
709 case VK_IMAGE_ASPECT_COLOR_BIT
:
710 /* Dragons will eat you.
712 * Meta attaches all destination surfaces as color render targets. Guess
713 * what surface the Meta Dragons really want.
715 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
716 return &image
->depth_surface
;
717 } else if (image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
718 return &image
->stencil_surface
;
720 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
721 return &image
->color_surface
;
724 case VK_IMAGE_ASPECT_DEPTH_BIT
:
725 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
726 return &image
->depth_surface
;
727 case VK_IMAGE_ASPECT_STENCIL_BIT
:
728 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
729 return &image
->stencil_surface
;
730 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
731 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
732 * combined depth stencil formats. Specifically, it states:
734 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
735 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
737 * Image views with both depth and stencil aspects are only valid for
738 * render target attachments, in which case
739 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
740 * stencil surfaces from the underlying surface.
742 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
743 return &image
->depth_surface
;
745 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
746 return &image
->stencil_surface
;
749 unreachable("image does not have aspect");