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"
33 * Exactly one bit must be set in \a aspect.
35 static isl_surf_usage_flags_t
36 choose_isl_surf_usage(VkImageUsageFlags vk_usage
,
37 VkImageAspectFlags aspect
)
39 isl_surf_usage_flags_t isl_usage
= 0;
41 /* FINISHME: Support aux surfaces */
42 isl_usage
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
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 if (vk_usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
59 unreachable("bad VkImageAspect");
60 case VK_IMAGE_ASPECT_DEPTH_BIT
:
61 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
63 case VK_IMAGE_ASPECT_STENCIL_BIT
:
64 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
69 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
70 /* Meta implements transfers by sampling from the source image. */
71 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
74 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
75 /* Meta implements transfers by rendering into the destination image. */
76 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
83 * Exactly one bit must be set in \a aspect.
85 static struct anv_surface
*
86 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
90 unreachable("bad VkImageAspect");
91 case VK_IMAGE_ASPECT_COLOR_BIT
:
92 return &image
->color_surface
;
93 case VK_IMAGE_ASPECT_DEPTH_BIT
:
94 return &image
->depth_surface
;
95 case VK_IMAGE_ASPECT_STENCIL_BIT
:
96 return &image
->stencil_surface
;
101 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
102 * image's memory requirements (that is, the image's size and alignment).
104 * Exactly one bit must be set in \a aspect.
107 make_surface(const struct anv_device
*dev
,
108 struct anv_image
*image
,
109 const struct anv_image_create_info
*anv_info
,
110 VkImageAspectFlags aspect
)
112 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
115 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
116 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
117 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
118 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
121 isl_tiling_flags_t tiling_flags
= anv_info
->isl_tiling_flags
;
122 if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
)
123 tiling_flags
= ISL_TILING_LINEAR_BIT
;
125 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
127 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
130 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
131 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
132 .format
= anv_get_isl_format(vk_info
->format
, aspect
,
133 vk_info
->tiling
, NULL
),
134 .width
= image
->extent
.width
,
135 .height
= image
->extent
.height
,
136 .depth
= image
->extent
.depth
,
137 .levels
= vk_info
->mipLevels
,
138 .array_len
= vk_info
->arrayLayers
,
139 .samples
= vk_info
->samples
,
141 .min_pitch
= anv_info
->stride
,
142 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
143 .tiling_flags
= tiling_flags
);
145 /* isl_surf_init() will fail only if provided invalid input. Invalid input
146 * is illegal in Vulkan.
150 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
151 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
152 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
158 * Parameter @a format is required and overrides VkImageCreateInfo::format.
160 static VkImageUsageFlags
161 anv_image_get_full_usage(const VkImageCreateInfo
*info
,
162 const struct anv_format
*format
)
164 VkImageUsageFlags usage
= info
->usage
;
166 if (info
->samples
> 1 &&
167 (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)) {
168 /* Meta will resolve the image by binding it as a texture. */
169 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
172 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
173 /* Meta will transfer from the image by binding it as a texture. */
174 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
177 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
178 /* For non-clear transfer operations, meta will transfer to the image by
179 * binding it as a color attachment, even if the image format is not
182 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
184 if (anv_format_is_depth_or_stencil(format
)) {
185 /* vkCmdClearDepthStencilImage() only requires that
186 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
187 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
188 * clears the image, though, by binding it as a depthstencil
191 usage
|= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
;
199 anv_image_create(VkDevice _device
,
200 const struct anv_image_create_info
*create_info
,
201 const VkAllocationCallbacks
* alloc
,
204 ANV_FROM_HANDLE(anv_device
, device
, _device
);
205 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
206 struct anv_image
*image
= NULL
;
207 const struct anv_format
*format
= anv_format_for_vk_format(pCreateInfo
->format
);
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
->format
= 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
, format
);
233 image
->tiling
= pCreateInfo
->tiling
;
235 if (likely(anv_format_is_color(format
))) {
236 r
= make_surface(device
, image
, create_info
,
237 VK_IMAGE_ASPECT_COLOR_BIT
);
241 if (image
->format
->has_depth
) {
242 r
= make_surface(device
, image
, create_info
,
243 VK_IMAGE_ASPECT_DEPTH_BIT
);
248 if (image
->format
->has_stencil
) {
249 r
= make_surface(device
, image
, create_info
,
250 VK_IMAGE_ASPECT_STENCIL_BIT
);
256 *pImage
= anv_image_to_handle(image
);
262 anv_free2(&device
->alloc
, alloc
, image
);
268 anv_CreateImage(VkDevice device
,
269 const VkImageCreateInfo
*pCreateInfo
,
270 const VkAllocationCallbacks
*pAllocator
,
273 return anv_image_create(device
,
274 &(struct anv_image_create_info
) {
275 .vk_info
= pCreateInfo
,
276 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
283 anv_DestroyImage(VkDevice _device
, VkImage _image
,
284 const VkAllocationCallbacks
*pAllocator
)
286 ANV_FROM_HANDLE(anv_device
, device
, _device
);
288 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
292 anv_surface_get_subresource_layout(struct anv_image
*image
,
293 struct anv_surface
*surface
,
294 const VkImageSubresource
*subresource
,
295 VkSubresourceLayout
*layout
)
297 /* If we are on a non-zero mip level or array slice, we need to
298 * calculate a real offset.
300 anv_assert(subresource
->mipLevel
== 0);
301 anv_assert(subresource
->arrayLayer
== 0);
303 layout
->offset
= surface
->offset
;
304 layout
->rowPitch
= surface
->isl
.row_pitch
;
305 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
306 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
307 layout
->size
= surface
->isl
.size
;
310 void anv_GetImageSubresourceLayout(
313 const VkImageSubresource
* pSubresource
,
314 VkSubresourceLayout
* pLayout
)
316 ANV_FROM_HANDLE(anv_image
, image
, _image
);
318 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
320 switch (pSubresource
->aspectMask
) {
321 case VK_IMAGE_ASPECT_COLOR_BIT
:
322 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
323 pSubresource
, pLayout
);
325 case VK_IMAGE_ASPECT_DEPTH_BIT
:
326 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
327 pSubresource
, pLayout
);
329 case VK_IMAGE_ASPECT_STENCIL_BIT
:
330 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
331 pSubresource
, pLayout
);
334 assert(!"Invalid image aspect");
339 anv_validate_CreateImageView(VkDevice _device
,
340 const VkImageViewCreateInfo
*pCreateInfo
,
341 const VkAllocationCallbacks
*pAllocator
,
344 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
345 const VkImageSubresourceRange
*subresource
;
346 const struct anv_format
*view_format_info
;
348 /* Validate structure type before dereferencing it. */
350 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
351 subresource
= &pCreateInfo
->subresourceRange
;
353 /* Validate viewType is in range before using it. */
354 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
355 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
357 /* Validate format is in range before using it. */
358 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
359 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
360 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
362 /* Validate channel swizzles. */
363 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
364 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
365 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
366 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
367 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
368 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
369 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
370 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
372 /* Validate subresource. */
373 assert(subresource
->aspectMask
!= 0);
374 assert(subresource
->levelCount
> 0);
375 assert(subresource
->layerCount
> 0);
376 assert(subresource
->baseMipLevel
< image
->levels
);
377 assert(subresource
->baseMipLevel
+ anv_get_levelCount(image
, subresource
) <= image
->levels
);
378 assert(subresource
->baseArrayLayer
< image
->array_size
);
379 assert(subresource
->baseArrayLayer
+ anv_get_layerCount(image
, subresource
) <= image
->array_size
);
382 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
383 | VK_IMAGE_ASPECT_STENCIL_BIT
;
385 /* Validate format. */
386 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
387 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
388 assert(!image
->format
->has_depth
);
389 assert(!image
->format
->has_stencil
);
390 assert(!view_format_info
->has_depth
);
391 assert(!view_format_info
->has_stencil
);
392 assert(view_format_info
->isl_layout
->bs
==
393 image
->format
->isl_layout
->bs
);
394 } else if (subresource
->aspectMask
& ds_flags
) {
395 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
397 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
398 assert(image
->format
->has_depth
);
399 assert(view_format_info
->has_depth
);
400 assert(view_format_info
->isl_layout
->bs
==
401 image
->format
->isl_layout
->bs
);
404 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
405 /* FINISHME: Is it legal to have an R8 view of S8? */
406 assert(image
->format
->has_stencil
);
407 assert(view_format_info
->has_stencil
);
410 assert(!"bad VkImageSubresourceRange::aspectFlags");
413 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
416 static struct anv_state
417 alloc_surface_state(struct anv_device
*device
,
418 struct anv_cmd_buffer
*cmd_buffer
)
421 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
423 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
428 has_matching_storage_typed_format(const struct anv_device
*device
,
429 enum isl_format format
)
431 return (isl_format_get_layout(format
)->bs
<= 4 ||
432 (isl_format_get_layout(format
)->bs
<= 8 &&
433 (device
->info
.gen
>= 8 || device
->info
.is_haswell
)) ||
434 device
->info
.gen
>= 9);
437 static enum isl_channel_select
438 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
439 struct anv_format_swizzle format_swizzle
)
441 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
445 case VK_COMPONENT_SWIZZLE_ZERO
:
446 return ISL_CHANNEL_SELECT_ZERO
;
447 case VK_COMPONENT_SWIZZLE_ONE
:
448 return ISL_CHANNEL_SELECT_ONE
;
449 case VK_COMPONENT_SWIZZLE_R
:
450 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.r
;
451 case VK_COMPONENT_SWIZZLE_G
:
452 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.g
;
453 case VK_COMPONENT_SWIZZLE_B
:
454 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.b
;
455 case VK_COMPONENT_SWIZZLE_A
:
456 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.a
;
458 unreachable("Invalid swizzle");
463 anv_image_view_init(struct anv_image_view
*iview
,
464 struct anv_device
*device
,
465 const VkImageViewCreateInfo
* pCreateInfo
,
466 struct anv_cmd_buffer
*cmd_buffer
,
467 VkImageUsageFlags usage_mask
)
469 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
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 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_swizzle swizzle
;
503 enum isl_format format
= anv_get_isl_format(pCreateInfo
->format
,
505 image
->tiling
, &swizzle
);
507 iview
->base_layer
= range
->baseArrayLayer
;
508 iview
->base_mip
= range
->baseMipLevel
;
510 struct isl_view isl_view
= {
512 .base_level
= range
->baseMipLevel
,
513 .levels
= anv_get_levelCount(image
, range
),
514 .base_array_layer
= range
->baseArrayLayer
,
515 .array_len
= anv_get_layerCount(image
, range
),
517 remap_swizzle(pCreateInfo
->components
.r
,
518 VK_COMPONENT_SWIZZLE_R
, swizzle
),
519 remap_swizzle(pCreateInfo
->components
.g
,
520 VK_COMPONENT_SWIZZLE_G
, swizzle
),
521 remap_swizzle(pCreateInfo
->components
.b
,
522 VK_COMPONENT_SWIZZLE_B
, swizzle
),
523 remap_swizzle(pCreateInfo
->components
.a
,
524 VK_COMPONENT_SWIZZLE_A
, swizzle
),
528 iview
->extent
= (VkExtent3D
) {
529 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
530 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
531 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
534 isl_surf_usage_flags_t cube_usage
;
535 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
536 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
537 cube_usage
= ISL_SURF_USAGE_CUBE_BIT
;
542 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
543 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
545 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_TEXTURE_BIT
;
546 isl_surf_fill_state(&device
->isl_dev
,
547 iview
->sampler_surface_state
.map
,
548 .surf
= &surface
->isl
,
550 .mocs
= device
->default_mocs
);
552 if (!device
->info
.has_llc
)
553 anv_state_clflush(iview
->sampler_surface_state
);
555 iview
->sampler_surface_state
.alloc_size
= 0;
558 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
559 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
561 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_RENDER_TARGET_BIT
;
562 isl_surf_fill_state(&device
->isl_dev
,
563 iview
->color_rt_surface_state
.map
,
564 .surf
= &surface
->isl
,
566 .mocs
= device
->default_mocs
);
568 if (!device
->info
.has_llc
)
569 anv_state_clflush(iview
->color_rt_surface_state
);
571 iview
->color_rt_surface_state
.alloc_size
= 0;
574 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_STORAGE_BIT
) {
575 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
577 if (has_matching_storage_typed_format(device
, format
)) {
578 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_STORAGE_BIT
;
579 isl_surf_fill_state(&device
->isl_dev
,
580 iview
->storage_surface_state
.map
,
581 .surf
= &surface
->isl
,
583 .mocs
= device
->default_mocs
);
585 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
588 iview
->bo
->size
- iview
->offset
, 1);
591 isl_surf_fill_image_param(&device
->isl_dev
,
592 &iview
->storage_image_param
,
593 &surface
->isl
, &isl_view
);
595 if (!device
->info
.has_llc
)
596 anv_state_clflush(iview
->storage_surface_state
);
598 iview
->storage_surface_state
.alloc_size
= 0;
603 anv_CreateImageView(VkDevice _device
,
604 const VkImageViewCreateInfo
*pCreateInfo
,
605 const VkAllocationCallbacks
*pAllocator
,
608 ANV_FROM_HANDLE(anv_device
, device
, _device
);
609 struct anv_image_view
*view
;
611 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
612 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
614 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
616 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, ~0);
618 *pView
= anv_image_view_to_handle(view
);
624 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
625 const VkAllocationCallbacks
*pAllocator
)
627 ANV_FROM_HANDLE(anv_device
, device
, _device
);
628 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
630 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
631 anv_state_pool_free(&device
->surface_state_pool
,
632 iview
->color_rt_surface_state
);
635 if (iview
->sampler_surface_state
.alloc_size
> 0) {
636 anv_state_pool_free(&device
->surface_state_pool
,
637 iview
->sampler_surface_state
);
640 if (iview
->storage_surface_state
.alloc_size
> 0) {
641 anv_state_pool_free(&device
->surface_state_pool
,
642 iview
->storage_surface_state
);
645 anv_free2(&device
->alloc
, pAllocator
, iview
);
649 void anv_buffer_view_init(struct anv_buffer_view
*view
,
650 struct anv_device
*device
,
651 const VkBufferViewCreateInfo
* pCreateInfo
,
652 struct anv_cmd_buffer
*cmd_buffer
)
654 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
656 const struct anv_format
*format
=
657 anv_format_for_vk_format(pCreateInfo
->format
);
659 view
->format
= format
->isl_format
;
660 view
->bo
= buffer
->bo
;
661 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
662 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
663 buffer
->size
- view
->offset
: pCreateInfo
->range
;
665 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
666 view
->surface_state
= alloc_surface_state(device
, cmd_buffer
);
668 anv_fill_buffer_surface_state(device
, view
->surface_state
,
670 view
->offset
, view
->range
,
671 format
->isl_layout
->bs
);
673 view
->surface_state
= (struct anv_state
){ 0 };
676 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
677 view
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
679 enum isl_format storage_format
=
680 has_matching_storage_typed_format(device
, view
->format
) ?
681 isl_lower_storage_image_format(&device
->isl_dev
, view
->format
) :
684 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
686 view
->offset
, view
->range
,
687 (storage_format
== ISL_FORMAT_RAW
? 1 :
688 format
->isl_layout
->bs
));
690 isl_buffer_fill_image_param(&device
->isl_dev
,
691 &view
->storage_image_param
,
692 view
->format
, view
->range
);
694 view
->storage_surface_state
= (struct anv_state
){ 0 };
699 anv_CreateBufferView(VkDevice _device
,
700 const VkBufferViewCreateInfo
*pCreateInfo
,
701 const VkAllocationCallbacks
*pAllocator
,
704 ANV_FROM_HANDLE(anv_device
, device
, _device
);
705 struct anv_buffer_view
*view
;
707 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
708 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
710 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
712 anv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
714 *pView
= anv_buffer_view_to_handle(view
);
720 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
721 const VkAllocationCallbacks
*pAllocator
)
723 ANV_FROM_HANDLE(anv_device
, device
, _device
);
724 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
726 if (view
->surface_state
.alloc_size
> 0)
727 anv_state_pool_free(&device
->surface_state_pool
,
728 view
->surface_state
);
730 if (view
->storage_surface_state
.alloc_size
> 0)
731 anv_state_pool_free(&device
->surface_state_pool
,
732 view
->storage_surface_state
);
734 anv_free2(&device
->alloc
, pAllocator
, view
);
738 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
740 switch (aspect_mask
) {
741 case VK_IMAGE_ASPECT_COLOR_BIT
:
742 /* Dragons will eat you.
744 * Meta attaches all destination surfaces as color render targets. Guess
745 * what surface the Meta Dragons really want.
747 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
748 return &image
->depth_surface
;
749 } else if (image
->format
->has_depth
) {
750 return &image
->depth_surface
;
751 } else if (image
->format
->has_stencil
) {
752 return &image
->stencil_surface
;
754 return &image
->color_surface
;
757 case VK_IMAGE_ASPECT_DEPTH_BIT
:
758 assert(image
->format
->has_depth
);
759 return &image
->depth_surface
;
760 case VK_IMAGE_ASPECT_STENCIL_BIT
:
761 assert(image
->format
->has_stencil
);
762 return &image
->stencil_surface
;
763 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
764 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
765 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
766 * combined depth stencil formats. Specifically, it states:
768 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
769 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
771 * Image views with both depth and stencil aspects are only valid for
772 * render target attachments, in which case
773 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
774 * stencil surfaces from the underlying surface.
776 return &image
->depth_surface
;
777 } else if (image
->format
->has_depth
) {
778 return &image
->depth_surface
;
779 } else if (image
->format
->has_stencil
) {
780 return &image
->stencil_surface
;
784 unreachable("image does not have aspect");