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 image
->aspects
|= VK_IMAGE_ASPECT_COLOR_BIT
;
237 r
= make_surface(device
, image
, create_info
,
238 VK_IMAGE_ASPECT_COLOR_BIT
);
242 if (image
->format
->has_depth
) {
243 image
->aspects
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
244 r
= make_surface(device
, image
, create_info
,
245 VK_IMAGE_ASPECT_DEPTH_BIT
);
250 if (image
->format
->has_stencil
) {
251 image
->aspects
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
252 r
= make_surface(device
, image
, create_info
,
253 VK_IMAGE_ASPECT_STENCIL_BIT
);
259 *pImage
= anv_image_to_handle(image
);
265 anv_free2(&device
->alloc
, alloc
, image
);
271 anv_CreateImage(VkDevice device
,
272 const VkImageCreateInfo
*pCreateInfo
,
273 const VkAllocationCallbacks
*pAllocator
,
276 return anv_image_create(device
,
277 &(struct anv_image_create_info
) {
278 .vk_info
= pCreateInfo
,
279 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
286 anv_DestroyImage(VkDevice _device
, VkImage _image
,
287 const VkAllocationCallbacks
*pAllocator
)
289 ANV_FROM_HANDLE(anv_device
, device
, _device
);
291 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
295 anv_surface_get_subresource_layout(struct anv_image
*image
,
296 struct anv_surface
*surface
,
297 const VkImageSubresource
*subresource
,
298 VkSubresourceLayout
*layout
)
300 /* If we are on a non-zero mip level or array slice, we need to
301 * calculate a real offset.
303 anv_assert(subresource
->mipLevel
== 0);
304 anv_assert(subresource
->arrayLayer
== 0);
306 layout
->offset
= surface
->offset
;
307 layout
->rowPitch
= surface
->isl
.row_pitch
;
308 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
309 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
310 layout
->size
= surface
->isl
.size
;
313 void anv_GetImageSubresourceLayout(
316 const VkImageSubresource
* pSubresource
,
317 VkSubresourceLayout
* pLayout
)
319 ANV_FROM_HANDLE(anv_image
, image
, _image
);
321 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
323 switch (pSubresource
->aspectMask
) {
324 case VK_IMAGE_ASPECT_COLOR_BIT
:
325 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
326 pSubresource
, pLayout
);
328 case VK_IMAGE_ASPECT_DEPTH_BIT
:
329 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
330 pSubresource
, pLayout
);
332 case VK_IMAGE_ASPECT_STENCIL_BIT
:
333 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
334 pSubresource
, pLayout
);
337 assert(!"Invalid image aspect");
342 anv_validate_CreateImageView(VkDevice _device
,
343 const VkImageViewCreateInfo
*pCreateInfo
,
344 const VkAllocationCallbacks
*pAllocator
,
347 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
348 const VkImageSubresourceRange
*subresource
;
349 MAYBE_UNUSED
const struct anv_format
*view_format_info
;
351 /* Validate structure type before dereferencing it. */
353 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
354 subresource
= &pCreateInfo
->subresourceRange
;
356 /* Validate viewType is in range before using it. */
357 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
358 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
360 /* Validate format is in range before using it. */
361 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
362 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
363 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
365 /* Validate channel swizzles. */
366 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
367 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
368 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
369 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
370 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
371 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
372 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
373 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
375 /* Validate subresource. */
376 assert(subresource
->aspectMask
!= 0);
377 assert(subresource
->levelCount
> 0);
378 assert(subresource
->layerCount
> 0);
379 assert(subresource
->baseMipLevel
< image
->levels
);
380 assert(subresource
->baseMipLevel
+ anv_get_levelCount(image
, subresource
) <= image
->levels
);
381 assert(subresource
->baseArrayLayer
< image
->array_size
);
382 assert(subresource
->baseArrayLayer
+ anv_get_layerCount(image
, subresource
) <= image
->array_size
);
385 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
386 | VK_IMAGE_ASPECT_STENCIL_BIT
;
388 /* Validate format. */
389 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
390 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
391 assert(!image
->format
->has_depth
);
392 assert(!image
->format
->has_stencil
);
393 assert(!view_format_info
->has_depth
);
394 assert(!view_format_info
->has_stencil
);
395 assert(view_format_info
->isl_layout
->bs
==
396 image
->format
->isl_layout
->bs
);
397 } else if (subresource
->aspectMask
& ds_flags
) {
398 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
400 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
401 assert(image
->format
->has_depth
);
402 assert(view_format_info
->has_depth
);
403 assert(view_format_info
->isl_layout
->bs
==
404 image
->format
->isl_layout
->bs
);
407 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
408 /* FINISHME: Is it legal to have an R8 view of S8? */
409 assert(image
->format
->has_stencil
);
410 assert(view_format_info
->has_stencil
);
413 assert(!"bad VkImageSubresourceRange::aspectFlags");
416 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
419 static struct anv_state
420 alloc_surface_state(struct anv_device
*device
,
421 struct anv_cmd_buffer
*cmd_buffer
)
424 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
426 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
430 static enum isl_channel_select
431 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
432 struct anv_format_swizzle format_swizzle
)
434 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
438 case VK_COMPONENT_SWIZZLE_ZERO
:
439 return ISL_CHANNEL_SELECT_ZERO
;
440 case VK_COMPONENT_SWIZZLE_ONE
:
441 return ISL_CHANNEL_SELECT_ONE
;
442 case VK_COMPONENT_SWIZZLE_R
:
443 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.r
;
444 case VK_COMPONENT_SWIZZLE_G
:
445 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.g
;
446 case VK_COMPONENT_SWIZZLE_B
:
447 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.b
;
448 case VK_COMPONENT_SWIZZLE_A
:
449 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.a
;
451 unreachable("Invalid swizzle");
456 anv_image_view_init(struct anv_image_view
*iview
,
457 struct anv_device
*device
,
458 const VkImageViewCreateInfo
* pCreateInfo
,
459 struct anv_cmd_buffer
*cmd_buffer
,
460 VkImageUsageFlags usage_mask
)
462 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
463 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
465 assert(range
->layerCount
> 0);
466 assert(range
->baseMipLevel
< image
->levels
);
467 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
468 VK_IMAGE_USAGE_STORAGE_BIT
|
469 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
470 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
472 switch (image
->type
) {
474 unreachable("bad VkImageType");
475 case VK_IMAGE_TYPE_1D
:
476 case VK_IMAGE_TYPE_2D
:
477 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
479 case VK_IMAGE_TYPE_3D
:
480 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
481 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
485 struct anv_surface
*surface
=
486 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
488 iview
->image
= image
;
489 iview
->bo
= image
->bo
;
490 iview
->offset
= image
->offset
+ surface
->offset
;
492 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
493 iview
->vk_format
= pCreateInfo
->format
;
495 struct anv_format_swizzle swizzle
;
496 enum isl_format format
= anv_get_isl_format(pCreateInfo
->format
,
498 image
->tiling
, &swizzle
);
500 iview
->base_layer
= range
->baseArrayLayer
;
501 iview
->base_mip
= range
->baseMipLevel
;
503 struct isl_view isl_view
= {
505 .base_level
= range
->baseMipLevel
,
506 .levels
= anv_get_levelCount(image
, range
),
507 .base_array_layer
= range
->baseArrayLayer
,
508 .array_len
= anv_get_layerCount(image
, range
),
510 remap_swizzle(pCreateInfo
->components
.r
,
511 VK_COMPONENT_SWIZZLE_R
, swizzle
),
512 remap_swizzle(pCreateInfo
->components
.g
,
513 VK_COMPONENT_SWIZZLE_G
, swizzle
),
514 remap_swizzle(pCreateInfo
->components
.b
,
515 VK_COMPONENT_SWIZZLE_B
, swizzle
),
516 remap_swizzle(pCreateInfo
->components
.a
,
517 VK_COMPONENT_SWIZZLE_A
, swizzle
),
521 iview
->extent
= (VkExtent3D
) {
522 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
523 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
524 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
527 isl_surf_usage_flags_t cube_usage
;
528 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
529 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
530 cube_usage
= ISL_SURF_USAGE_CUBE_BIT
;
535 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
536 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
538 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_TEXTURE_BIT
;
539 isl_surf_fill_state(&device
->isl_dev
,
540 iview
->sampler_surface_state
.map
,
541 .surf
= &surface
->isl
,
543 .mocs
= device
->default_mocs
);
545 if (!device
->info
.has_llc
)
546 anv_state_clflush(iview
->sampler_surface_state
);
548 iview
->sampler_surface_state
.alloc_size
= 0;
551 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
552 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
554 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_RENDER_TARGET_BIT
;
555 isl_surf_fill_state(&device
->isl_dev
,
556 iview
->color_rt_surface_state
.map
,
557 .surf
= &surface
->isl
,
559 .mocs
= device
->default_mocs
);
561 if (!device
->info
.has_llc
)
562 anv_state_clflush(iview
->color_rt_surface_state
);
564 iview
->color_rt_surface_state
.alloc_size
= 0;
567 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_STORAGE_BIT
) {
568 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
570 if (isl_has_matching_typed_storage_image_format(&device
->info
, format
)) {
571 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_STORAGE_BIT
;
572 isl_surf_fill_state(&device
->isl_dev
,
573 iview
->storage_surface_state
.map
,
574 .surf
= &surface
->isl
,
576 .mocs
= device
->default_mocs
);
578 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
581 iview
->bo
->size
- iview
->offset
, 1);
584 isl_surf_fill_image_param(&device
->isl_dev
,
585 &iview
->storage_image_param
,
586 &surface
->isl
, &isl_view
);
588 if (!device
->info
.has_llc
)
589 anv_state_clflush(iview
->storage_surface_state
);
591 iview
->storage_surface_state
.alloc_size
= 0;
596 anv_CreateImageView(VkDevice _device
,
597 const VkImageViewCreateInfo
*pCreateInfo
,
598 const VkAllocationCallbacks
*pAllocator
,
601 ANV_FROM_HANDLE(anv_device
, device
, _device
);
602 struct anv_image_view
*view
;
604 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
605 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
607 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
609 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, ~0);
611 *pView
= anv_image_view_to_handle(view
);
617 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
618 const VkAllocationCallbacks
*pAllocator
)
620 ANV_FROM_HANDLE(anv_device
, device
, _device
);
621 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
623 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
624 anv_state_pool_free(&device
->surface_state_pool
,
625 iview
->color_rt_surface_state
);
628 if (iview
->sampler_surface_state
.alloc_size
> 0) {
629 anv_state_pool_free(&device
->surface_state_pool
,
630 iview
->sampler_surface_state
);
633 if (iview
->storage_surface_state
.alloc_size
> 0) {
634 anv_state_pool_free(&device
->surface_state_pool
,
635 iview
->storage_surface_state
);
638 anv_free2(&device
->alloc
, pAllocator
, iview
);
642 void anv_buffer_view_init(struct anv_buffer_view
*view
,
643 struct anv_device
*device
,
644 const VkBufferViewCreateInfo
* pCreateInfo
,
645 struct anv_cmd_buffer
*cmd_buffer
)
647 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
649 /* TODO: Handle the format swizzle? */
651 view
->format
= anv_get_isl_format(pCreateInfo
->format
,
652 VK_IMAGE_ASPECT_COLOR_BIT
,
653 VK_IMAGE_TILING_LINEAR
, NULL
);
654 view
->bo
= buffer
->bo
;
655 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
656 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
657 buffer
->size
- view
->offset
: pCreateInfo
->range
;
659 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
660 view
->surface_state
= alloc_surface_state(device
, cmd_buffer
);
662 anv_fill_buffer_surface_state(device
, view
->surface_state
,
664 view
->offset
, view
->range
,
665 isl_format_get_layout(view
->format
)->bs
);
667 view
->surface_state
= (struct anv_state
){ 0 };
670 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
671 view
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
673 enum isl_format storage_format
=
674 isl_has_matching_typed_storage_image_format(&device
->info
,
676 isl_lower_storage_image_format(&device
->info
, view
->format
) :
679 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
681 view
->offset
, view
->range
,
682 (storage_format
== ISL_FORMAT_RAW
? 1 :
683 isl_format_get_layout(storage_format
)->bs
));
685 isl_buffer_fill_image_param(&device
->isl_dev
,
686 &view
->storage_image_param
,
687 view
->format
, view
->range
);
689 view
->storage_surface_state
= (struct anv_state
){ 0 };
694 anv_CreateBufferView(VkDevice _device
,
695 const VkBufferViewCreateInfo
*pCreateInfo
,
696 const VkAllocationCallbacks
*pAllocator
,
699 ANV_FROM_HANDLE(anv_device
, device
, _device
);
700 struct anv_buffer_view
*view
;
702 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
703 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
705 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
707 anv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
709 *pView
= anv_buffer_view_to_handle(view
);
715 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
716 const VkAllocationCallbacks
*pAllocator
)
718 ANV_FROM_HANDLE(anv_device
, device
, _device
);
719 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
721 if (view
->surface_state
.alloc_size
> 0)
722 anv_state_pool_free(&device
->surface_state_pool
,
723 view
->surface_state
);
725 if (view
->storage_surface_state
.alloc_size
> 0)
726 anv_state_pool_free(&device
->surface_state_pool
,
727 view
->storage_surface_state
);
729 anv_free2(&device
->alloc
, pAllocator
, view
);
733 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
735 switch (aspect_mask
) {
736 case VK_IMAGE_ASPECT_COLOR_BIT
:
737 /* Dragons will eat you.
739 * Meta attaches all destination surfaces as color render targets. Guess
740 * what surface the Meta Dragons really want.
742 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
743 return &image
->depth_surface
;
744 } else if (image
->format
->has_depth
) {
745 return &image
->depth_surface
;
746 } else if (image
->format
->has_stencil
) {
747 return &image
->stencil_surface
;
749 return &image
->color_surface
;
752 case VK_IMAGE_ASPECT_DEPTH_BIT
:
753 assert(image
->format
->has_depth
);
754 return &image
->depth_surface
;
755 case VK_IMAGE_ASPECT_STENCIL_BIT
:
756 assert(image
->format
->has_stencil
);
757 return &image
->stencil_surface
;
758 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
759 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
760 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
761 * combined depth stencil formats. Specifically, it states:
763 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
764 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
766 * Image views with both depth and stencil aspects are only valid for
767 * render target attachments, in which case
768 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
769 * stencil surfaces from the underlying surface.
771 return &image
->depth_surface
;
772 } else if (image
->format
->has_depth
) {
773 return &image
->depth_surface
;
774 } else if (image
->format
->has_stencil
) {
775 return &image
->stencil_surface
;
779 unreachable("image does not have aspect");