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
);
128 switch (vk_info
->imageType
) {
129 case VK_IMAGE_TYPE_1D
:
130 extent
= (VkExtent3D
) { vk_info
->extent
.width
, 1, 1 };
132 case VK_IMAGE_TYPE_2D
:
133 extent
= (VkExtent3D
) { vk_info
->extent
.width
, vk_info
->extent
.height
, 1 };
135 case VK_IMAGE_TYPE_3D
:
136 extent
= vk_info
->extent
;
139 unreachable("invalid image type");
142 image
->extent
= extent
;
144 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
145 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
146 .format
= anv_get_isl_format(vk_info
->format
, aspect
,
147 vk_info
->tiling
, NULL
),
148 .width
= extent
.width
,
149 .height
= extent
.height
,
150 .depth
= extent
.depth
,
151 .levels
= vk_info
->mipLevels
,
152 .array_len
= vk_info
->arrayLayers
,
153 .samples
= vk_info
->samples
,
156 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
157 .tiling_flags
= tiling_flags
);
159 /* isl_surf_init() will fail only if provided invalid input. Invalid input
160 * is illegal in Vulkan.
164 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
165 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
166 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
172 * Parameter @a format is required and overrides VkImageCreateInfo::format.
174 static VkImageUsageFlags
175 anv_image_get_full_usage(const VkImageCreateInfo
*info
,
176 const struct anv_format
*format
)
178 VkImageUsageFlags usage
= info
->usage
;
180 if (info
->samples
> 1 &&
181 (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)) {
182 /* Meta will resolve the image by binding it as a texture. */
183 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
186 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
187 /* Meta will transfer from the image by binding it as a texture. */
188 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
191 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
192 /* For non-clear transfer operations, meta will transfer to the image by
193 * binding it as a color attachment, even if the image format is not
196 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
198 if (anv_format_is_depth_or_stencil(format
)) {
199 /* vkCmdClearDepthStencilImage() only requires that
200 * VK_IMAGE_USAGE_TRANSFER_SRC_BIT be set. In particular, it does
201 * not require VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT. Meta
202 * clears the image, though, by binding it as a depthstencil
205 usage
|= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
;
213 anv_image_create(VkDevice _device
,
214 const struct anv_image_create_info
*create_info
,
215 const VkAllocationCallbacks
* alloc
,
218 ANV_FROM_HANDLE(anv_device
, device
, _device
);
219 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
220 struct anv_image
*image
= NULL
;
221 const struct anv_format
*format
= anv_format_for_vk_format(pCreateInfo
->format
);
224 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
226 anv_assert(pCreateInfo
->mipLevels
> 0);
227 anv_assert(pCreateInfo
->arrayLayers
> 0);
228 anv_assert(pCreateInfo
->samples
> 0);
229 anv_assert(pCreateInfo
->extent
.width
> 0);
230 anv_assert(pCreateInfo
->extent
.height
> 0);
231 anv_assert(pCreateInfo
->extent
.depth
> 0);
233 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
234 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
236 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
238 memset(image
, 0, sizeof(*image
));
239 image
->type
= pCreateInfo
->imageType
;
240 image
->extent
= pCreateInfo
->extent
;
241 image
->vk_format
= pCreateInfo
->format
;
242 image
->format
= format
;
243 image
->levels
= pCreateInfo
->mipLevels
;
244 image
->array_size
= pCreateInfo
->arrayLayers
;
245 image
->samples
= pCreateInfo
->samples
;
246 image
->usage
= anv_image_get_full_usage(pCreateInfo
, format
);
247 image
->tiling
= pCreateInfo
->tiling
;
249 if (likely(anv_format_is_color(format
))) {
250 r
= make_surface(device
, image
, create_info
,
251 VK_IMAGE_ASPECT_COLOR_BIT
);
255 if (image
->format
->has_depth
) {
256 r
= make_surface(device
, image
, create_info
,
257 VK_IMAGE_ASPECT_DEPTH_BIT
);
262 if (image
->format
->has_stencil
) {
263 r
= make_surface(device
, image
, create_info
,
264 VK_IMAGE_ASPECT_STENCIL_BIT
);
270 *pImage
= anv_image_to_handle(image
);
276 anv_free2(&device
->alloc
, alloc
, image
);
282 anv_CreateImage(VkDevice device
,
283 const VkImageCreateInfo
*pCreateInfo
,
284 const VkAllocationCallbacks
*pAllocator
,
287 return anv_image_create(device
,
288 &(struct anv_image_create_info
) {
289 .vk_info
= pCreateInfo
,
290 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
297 anv_DestroyImage(VkDevice _device
, VkImage _image
,
298 const VkAllocationCallbacks
*pAllocator
)
300 ANV_FROM_HANDLE(anv_device
, device
, _device
);
302 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
306 anv_surface_get_subresource_layout(struct anv_image
*image
,
307 struct anv_surface
*surface
,
308 const VkImageSubresource
*subresource
,
309 VkSubresourceLayout
*layout
)
311 /* If we are on a non-zero mip level or array slice, we need to
312 * calculate a real offset.
314 anv_assert(subresource
->mipLevel
== 0);
315 anv_assert(subresource
->arrayLayer
== 0);
317 layout
->offset
= surface
->offset
;
318 layout
->rowPitch
= surface
->isl
.row_pitch
;
319 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
320 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
321 layout
->size
= surface
->isl
.size
;
324 void anv_GetImageSubresourceLayout(
327 const VkImageSubresource
* pSubresource
,
328 VkSubresourceLayout
* pLayout
)
330 ANV_FROM_HANDLE(anv_image
, image
, _image
);
332 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
334 switch (pSubresource
->aspectMask
) {
335 case VK_IMAGE_ASPECT_COLOR_BIT
:
336 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
337 pSubresource
, pLayout
);
339 case VK_IMAGE_ASPECT_DEPTH_BIT
:
340 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
341 pSubresource
, pLayout
);
343 case VK_IMAGE_ASPECT_STENCIL_BIT
:
344 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
345 pSubresource
, pLayout
);
348 assert(!"Invalid image aspect");
353 anv_validate_CreateImageView(VkDevice _device
,
354 const VkImageViewCreateInfo
*pCreateInfo
,
355 const VkAllocationCallbacks
*pAllocator
,
358 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
359 const VkImageSubresourceRange
*subresource
;
360 const struct anv_format
*view_format_info
;
362 /* Validate structure type before dereferencing it. */
364 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
365 subresource
= &pCreateInfo
->subresourceRange
;
367 /* Validate viewType is in range before using it. */
368 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
369 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
371 /* Validate format is in range before using it. */
372 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
373 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
374 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
376 /* Validate channel swizzles. */
377 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
378 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
379 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
380 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
381 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
382 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
383 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
384 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
386 /* Validate subresource. */
387 assert(subresource
->aspectMask
!= 0);
388 assert(subresource
->levelCount
> 0);
389 assert(subresource
->layerCount
> 0);
390 assert(subresource
->baseMipLevel
< image
->levels
);
391 assert(subresource
->baseMipLevel
+ subresource
->levelCount
<= image
->levels
);
392 assert(subresource
->baseArrayLayer
< image
->array_size
);
393 assert(subresource
->baseArrayLayer
+ subresource
->layerCount
<= image
->array_size
);
396 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
397 | VK_IMAGE_ASPECT_STENCIL_BIT
;
399 /* Validate format. */
400 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
401 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
402 assert(!image
->format
->has_depth
);
403 assert(!image
->format
->has_stencil
);
404 assert(!view_format_info
->has_depth
);
405 assert(!view_format_info
->has_stencil
);
406 assert(view_format_info
->isl_layout
->bs
==
407 image
->format
->isl_layout
->bs
);
408 } else if (subresource
->aspectMask
& ds_flags
) {
409 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
411 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
412 assert(image
->format
->has_depth
);
413 assert(view_format_info
->has_depth
);
414 assert(view_format_info
->isl_layout
->bs
==
415 image
->format
->isl_layout
->bs
);
418 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
419 /* FINISHME: Is it legal to have an R8 view of S8? */
420 assert(image
->format
->has_stencil
);
421 assert(view_format_info
->has_stencil
);
424 assert(!"bad VkImageSubresourceRange::aspectFlags");
427 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
430 static struct anv_state
431 alloc_surface_state(struct anv_device
*device
,
432 struct anv_cmd_buffer
*cmd_buffer
)
435 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
437 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
442 has_matching_storage_typed_format(const struct anv_device
*device
,
443 enum isl_format format
)
445 return (isl_format_get_layout(format
)->bs
<= 4 ||
446 (isl_format_get_layout(format
)->bs
<= 8 &&
447 (device
->info
.gen
>= 8 || device
->info
.is_haswell
)) ||
448 device
->info
.gen
>= 9);
451 static enum isl_channel_select
452 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
453 struct anv_format_swizzle format_swizzle
)
455 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
459 case VK_COMPONENT_SWIZZLE_ZERO
:
460 return ISL_CHANNEL_SELECT_ZERO
;
461 case VK_COMPONENT_SWIZZLE_ONE
:
462 return ISL_CHANNEL_SELECT_ONE
;
463 case VK_COMPONENT_SWIZZLE_R
:
464 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.r
;
465 case VK_COMPONENT_SWIZZLE_G
:
466 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.g
;
467 case VK_COMPONENT_SWIZZLE_B
:
468 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.b
;
469 case VK_COMPONENT_SWIZZLE_A
:
470 return ISL_CHANNEL_SELECT_RED
+ format_swizzle
.a
;
472 unreachable("Invalid swizzle");
477 anv_image_view_init(struct anv_image_view
*iview
,
478 struct anv_device
*device
,
479 const VkImageViewCreateInfo
* pCreateInfo
,
480 struct anv_cmd_buffer
*cmd_buffer
,
482 VkImageUsageFlags usage_mask
)
484 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
485 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
487 assert(range
->layerCount
> 0);
488 assert(range
->baseMipLevel
< image
->levels
);
489 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
490 VK_IMAGE_USAGE_STORAGE_BIT
|
491 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
492 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
494 switch (image
->type
) {
496 unreachable("bad VkImageType");
497 case VK_IMAGE_TYPE_1D
:
498 case VK_IMAGE_TYPE_2D
:
499 assert(range
->baseArrayLayer
+ range
->layerCount
- 1 <= image
->array_size
);
501 case VK_IMAGE_TYPE_3D
:
502 assert(range
->baseArrayLayer
+ range
->layerCount
- 1
503 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
507 struct anv_surface
*surface
=
508 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
510 iview
->image
= image
;
511 iview
->bo
= image
->bo
;
512 iview
->offset
= image
->offset
+ surface
->offset
+ offset
;
514 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
515 iview
->vk_format
= pCreateInfo
->format
;
517 struct anv_format_swizzle swizzle
;
518 enum isl_format format
= anv_get_isl_format(pCreateInfo
->format
,
520 image
->tiling
, &swizzle
);
522 iview
->base_layer
= range
->baseArrayLayer
;
523 iview
->base_mip
= range
->baseMipLevel
;
525 struct isl_view isl_view
= {
527 .base_level
= range
->baseMipLevel
,
528 .levels
= range
->levelCount
,
529 .base_array_layer
= range
->baseArrayLayer
,
530 .array_len
= range
->layerCount
,
532 remap_swizzle(pCreateInfo
->components
.r
,
533 VK_COMPONENT_SWIZZLE_R
, swizzle
),
534 remap_swizzle(pCreateInfo
->components
.g
,
535 VK_COMPONENT_SWIZZLE_G
, swizzle
),
536 remap_swizzle(pCreateInfo
->components
.b
,
537 VK_COMPONENT_SWIZZLE_B
, swizzle
),
538 remap_swizzle(pCreateInfo
->components
.a
,
539 VK_COMPONENT_SWIZZLE_A
, swizzle
),
543 struct isl_extent4d level0_extent_px
;
545 if (!isl_format_is_compressed(format
) &&
546 isl_format_is_compressed(image
->format
->isl_format
)) {
547 /* Scale the ImageView extent by the backing Image. This is used
548 * internally when an uncompressed ImageView is created on a
549 * compressed Image. The ImageView can therefore be used for copying
550 * data from a source Image to a destination Image.
552 const struct isl_format_layout
* isl_layout
= image
->format
->isl_layout
;
554 level0_extent_px
.depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
);
555 level0_extent_px
.depth
= DIV_ROUND_UP(level0_extent_px
.depth
, isl_layout
->bd
);
557 level0_extent_px
.height
= isl_surf_get_array_pitch_el_rows(&surface
->isl
) * image
->array_size
;
558 level0_extent_px
.width
= isl_surf_get_row_pitch_el(&surface
->isl
);
559 isl_view
.base_level
= 0;
560 isl_view
.base_array_layer
= 0;
562 level0_extent_px
.width
= image
->extent
.width
;
563 level0_extent_px
.height
= image
->extent
.height
;
564 level0_extent_px
.depth
= image
->extent
.depth
;
567 iview
->extent
= (VkExtent3D
) {
568 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
569 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
570 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
573 isl_surf_usage_flags_t cube_usage
;
574 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
575 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
576 cube_usage
= ISL_SURF_USAGE_CUBE_BIT
;
581 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
582 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
584 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_TEXTURE_BIT
;
585 isl_surf_fill_state(&device
->isl_dev
,
586 iview
->sampler_surface_state
.map
,
587 .surf
= &surface
->isl
,
589 .mocs
= device
->default_mocs
,
590 .level0_extent_px
= level0_extent_px
);
592 if (!device
->info
.has_llc
)
593 anv_state_clflush(iview
->sampler_surface_state
);
595 iview
->sampler_surface_state
.alloc_size
= 0;
598 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
599 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
601 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_RENDER_TARGET_BIT
;
602 isl_surf_fill_state(&device
->isl_dev
,
603 iview
->color_rt_surface_state
.map
,
604 .surf
= &surface
->isl
,
606 .mocs
= device
->default_mocs
,
607 .level0_extent_px
= level0_extent_px
);
609 if (!device
->info
.has_llc
)
610 anv_state_clflush(iview
->color_rt_surface_state
);
612 iview
->color_rt_surface_state
.alloc_size
= 0;
615 if (image
->usage
& usage_mask
& VK_IMAGE_USAGE_STORAGE_BIT
) {
616 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
618 if (has_matching_storage_typed_format(device
, format
)) {
619 isl_view
.usage
= cube_usage
| ISL_SURF_USAGE_STORAGE_BIT
;
620 isl_surf_fill_state(&device
->isl_dev
,
621 iview
->storage_surface_state
.map
,
622 .surf
= &surface
->isl
,
624 .mocs
= device
->default_mocs
,
625 .level0_extent_px
= level0_extent_px
);
627 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
630 iview
->bo
->size
- iview
->offset
, 1);
633 if (!device
->info
.has_llc
)
634 anv_state_clflush(iview
->storage_surface_state
);
636 iview
->storage_surface_state
.alloc_size
= 0;
641 anv_CreateImageView(VkDevice _device
,
642 const VkImageViewCreateInfo
*pCreateInfo
,
643 const VkAllocationCallbacks
*pAllocator
,
646 ANV_FROM_HANDLE(anv_device
, device
, _device
);
647 struct anv_image_view
*view
;
649 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
650 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
652 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
654 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, 0, ~0);
656 *pView
= anv_image_view_to_handle(view
);
662 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
663 const VkAllocationCallbacks
*pAllocator
)
665 ANV_FROM_HANDLE(anv_device
, device
, _device
);
666 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
668 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
669 anv_state_pool_free(&device
->surface_state_pool
,
670 iview
->color_rt_surface_state
);
673 if (iview
->sampler_surface_state
.alloc_size
> 0) {
674 anv_state_pool_free(&device
->surface_state_pool
,
675 iview
->sampler_surface_state
);
678 if (iview
->storage_surface_state
.alloc_size
> 0) {
679 anv_state_pool_free(&device
->surface_state_pool
,
680 iview
->storage_surface_state
);
683 anv_free2(&device
->alloc
, pAllocator
, iview
);
687 anv_CreateBufferView(VkDevice _device
,
688 const VkBufferViewCreateInfo
*pCreateInfo
,
689 const VkAllocationCallbacks
*pAllocator
,
692 ANV_FROM_HANDLE(anv_device
, device
, _device
);
693 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
694 struct anv_buffer_view
*view
;
696 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
697 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
699 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
701 const struct anv_format
*format
=
702 anv_format_for_vk_format(pCreateInfo
->format
);
704 view
->format
= format
->isl_format
;
705 view
->bo
= buffer
->bo
;
706 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
707 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
708 buffer
->size
- view
->offset
: pCreateInfo
->range
;
710 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
711 view
->surface_state
=
712 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
714 anv_fill_buffer_surface_state(device
, view
->surface_state
,
716 view
->offset
, view
->range
,
717 format
->isl_layout
->bs
);
719 view
->surface_state
= (struct anv_state
){ 0 };
722 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
723 view
->storage_surface_state
=
724 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
726 enum isl_format storage_format
=
727 has_matching_storage_typed_format(device
, view
->format
) ?
728 isl_lower_storage_image_format(&device
->isl_dev
, view
->format
) :
731 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
733 view
->offset
, view
->range
,
734 (storage_format
== ISL_FORMAT_RAW
? 1 :
735 format
->isl_layout
->bs
));
738 view
->storage_surface_state
= (struct anv_state
){ 0 };
741 *pView
= anv_buffer_view_to_handle(view
);
747 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
748 const VkAllocationCallbacks
*pAllocator
)
750 ANV_FROM_HANDLE(anv_device
, device
, _device
);
751 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
753 if (view
->surface_state
.alloc_size
> 0)
754 anv_state_pool_free(&device
->surface_state_pool
,
755 view
->surface_state
);
757 if (view
->storage_surface_state
.alloc_size
> 0)
758 anv_state_pool_free(&device
->surface_state_pool
,
759 view
->storage_surface_state
);
761 anv_free2(&device
->alloc
, pAllocator
, view
);
765 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
767 switch (aspect_mask
) {
768 case VK_IMAGE_ASPECT_COLOR_BIT
:
769 /* Dragons will eat you.
771 * Meta attaches all destination surfaces as color render targets. Guess
772 * what surface the Meta Dragons really want.
774 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
775 return &image
->depth_surface
;
776 } else if (image
->format
->has_depth
) {
777 return &image
->depth_surface
;
778 } else if (image
->format
->has_stencil
) {
779 return &image
->stencil_surface
;
781 return &image
->color_surface
;
784 case VK_IMAGE_ASPECT_DEPTH_BIT
:
785 assert(image
->format
->has_depth
);
786 return &image
->depth_surface
;
787 case VK_IMAGE_ASPECT_STENCIL_BIT
:
788 assert(image
->format
->has_stencil
);
789 return &image
->stencil_surface
;
790 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
791 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
792 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
793 * combined depth stencil formats. Specifically, it states:
795 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
796 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
798 * Image views with both depth and stencil aspects are only valid for
799 * render target attachments, in which case
800 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
801 * stencil surfaces from the underlying surface.
803 return &image
->depth_surface
;
804 } else if (image
->format
->has_depth
) {
805 return &image
->depth_surface
;
806 } else if (image
->format
->has_stencil
) {
807 return &image
->stencil_surface
;
811 unreachable("image does not have aspect");
817 image_param_defaults(struct brw_image_param
*param
)
819 memset(param
, 0, sizeof *param
);
820 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
821 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
822 * detailed explanation of these parameters.
824 param
->swizzling
[0] = 0xff;
825 param
->swizzling
[1] = 0xff;
829 anv_image_view_fill_image_param(struct anv_device
*device
,
830 struct anv_image_view
*view
,
831 struct brw_image_param
*param
)
833 image_param_defaults(param
);
835 const struct isl_surf
*surf
= &view
->image
->color_surface
.isl
;
836 const int cpp
= isl_format_get_layout(surf
->format
)->bs
;
837 const struct isl_extent3d image_align_sa
=
838 isl_surf_get_image_alignment_sa(surf
);
840 param
->size
[0] = view
->extent
.width
;
841 param
->size
[1] = view
->extent
.height
;
842 if (surf
->dim
== ISL_SURF_DIM_3D
) {
843 param
->size
[2] = view
->extent
.depth
;
845 param
->size
[2] = surf
->logical_level0_px
.array_len
- view
->base_layer
;
848 isl_surf_get_image_offset_el(surf
, view
->base_mip
, view
->base_layer
, 0,
849 ¶m
->offset
[0], ¶m
->offset
[1]);
851 param
->stride
[0] = cpp
;
852 param
->stride
[1] = surf
->row_pitch
/ cpp
;
854 if (device
->info
.gen
< 9 && surf
->dim
== ISL_SURF_DIM_3D
) {
855 param
->stride
[2] = util_align_npot(param
->size
[0], image_align_sa
.w
);
856 param
->stride
[3] = util_align_npot(param
->size
[1], image_align_sa
.h
);
858 param
->stride
[2] = 0;
859 param
->stride
[3] = isl_surf_get_array_pitch_el_rows(surf
);
862 switch (surf
->tiling
) {
863 case ISL_TILING_LINEAR
:
864 /* image_param_defaults is good enough */
868 /* An X tile is a rectangular block of 512x8 bytes. */
869 param
->tiling
[0] = util_logbase2(512 / cpp
);
870 param
->tiling
[1] = util_logbase2(8);
872 if (device
->isl_dev
.has_bit6_swizzling
) {
873 /* Right shifts required to swizzle bits 9 and 10 of the memory
874 * address with bit 6.
876 param
->swizzling
[0] = 3;
877 param
->swizzling
[1] = 4;
882 /* The layout of a Y-tiled surface in memory isn't really fundamentally
883 * different to the layout of an X-tiled surface, we simply pretend that
884 * the surface is broken up in a number of smaller 16Bx32 tiles, each
885 * one arranged in X-major order just like is the case for X-tiling.
887 param
->tiling
[0] = util_logbase2(16 / cpp
);
888 param
->tiling
[1] = util_logbase2(32);
890 if (device
->isl_dev
.has_bit6_swizzling
) {
891 /* Right shift required to swizzle bit 9 of the memory address with
894 param
->swizzling
[0] = 3;
895 param
->swizzling
[1] = 0xff;
900 assert(!"Unhandled storage image tiling");
903 /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The
904 * address calculation algorithm (emit_address_calculation() in
905 * brw_fs_surface_builder.cpp) handles this as a sort of tiling with
906 * modulus equal to the LOD.
908 param
->tiling
[2] = (device
->info
.gen
< 9 && surf
->dim
== ISL_SURF_DIM_3D
?
913 anv_buffer_view_fill_image_param(struct anv_device
*device
,
914 struct anv_buffer_view
*view
,
915 struct brw_image_param
*param
)
917 image_param_defaults(param
);
919 param
->stride
[0] = isl_format_layouts
[view
->format
].bs
;
920 param
->size
[0] = view
->range
/ param
->stride
[0];