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 * The \a format argument is required and overrides any format found in struct
34 * anv_image_create_info. Exactly one bit must be set in \a aspect.
36 static isl_surf_usage_flags_t
37 choose_isl_surf_usage(VkImageUsageFlags vk_usage
,
38 VkImageAspectFlags aspect
)
40 isl_surf_usage_flags_t isl_flags
= 0;
42 /* FINISHME: Support aux surfaces */
43 isl_flags
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
45 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
46 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
48 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
49 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
51 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
52 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
54 if (vk_usage
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
55 isl_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
57 if (vk_usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
60 unreachable("bad VkImageAspect");
61 case VK_IMAGE_ASPECT_DEPTH_BIT
:
62 isl_flags
|= ISL_SURF_USAGE_DEPTH_BIT
;
64 case VK_IMAGE_ASPECT_STENCIL_BIT
:
65 isl_flags
|= ISL_SURF_USAGE_STENCIL_BIT
;
70 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
71 /* Meta implements transfers by sampling from the source image. */
72 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
75 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
76 /* Meta implements transfers by rendering into the destination image. */
77 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
84 * Exactly one bit must be set in \a aspect.
86 static struct anv_surface
*
87 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
91 unreachable("bad VkImageAspect");
92 case VK_IMAGE_ASPECT_COLOR_BIT
:
93 return &image
->color_surface
;
94 case VK_IMAGE_ASPECT_DEPTH_BIT
:
95 return &image
->depth_surface
;
96 case VK_IMAGE_ASPECT_STENCIL_BIT
:
97 return &image
->stencil_surface
;
102 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
103 * image's memory requirements (that is, the image's size and alignment).
105 * Exactly one bit must be set in \a aspect.
108 make_surface(const struct anv_device
*dev
,
109 struct anv_image
*image
,
110 const struct anv_image_create_info
*anv_info
,
111 VkImageAspectFlags aspect
)
113 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
116 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
117 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
118 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
119 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
122 isl_tiling_flags_t tiling_flags
= anv_info
->isl_tiling_flags
;
123 if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
)
124 tiling_flags
&= ISL_TILING_LINEAR_BIT
;
126 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
128 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
129 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
130 .format
= anv_get_isl_format(vk_info
->format
, aspect
,
131 vk_info
->tiling
, NULL
),
132 .width
= vk_info
->extent
.width
,
133 .height
= vk_info
->extent
.height
,
134 .depth
= vk_info
->extent
.depth
,
135 .levels
= vk_info
->mipLevels
,
136 .array_len
= vk_info
->arrayLayers
,
137 .samples
= vk_info
->samples
,
140 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
141 .tiling_flags
= tiling_flags
);
143 /* isl_surf_init() will fail only if provided invalid input. Invalid input
144 * is illegal in Vulkan.
148 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
149 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
150 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
155 static VkImageUsageFlags
156 anv_image_get_full_usage(const VkImageCreateInfo
*info
)
158 VkImageUsageFlags usage
= info
->usage
;
160 if (info
->samples
> 1 &&
161 (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)) {
162 /* Meta will resolve the image by binding it as a texture. */
163 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
166 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
167 /* Meta will transfer from the image by binding it as a texture. */
168 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
171 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
172 /* Meta will transfer to the image by binding it as a color attachment,
173 * even if the image format is not a color format.
175 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
182 anv_image_create(VkDevice _device
,
183 const struct anv_image_create_info
*create_info
,
184 const VkAllocationCallbacks
* alloc
,
187 ANV_FROM_HANDLE(anv_device
, device
, _device
);
188 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
189 struct anv_image
*image
= NULL
;
192 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
194 anv_assert(pCreateInfo
->mipLevels
> 0);
195 anv_assert(pCreateInfo
->arrayLayers
> 0);
196 anv_assert(pCreateInfo
->samples
> 0);
197 anv_assert(pCreateInfo
->extent
.width
> 0);
198 anv_assert(pCreateInfo
->extent
.height
> 0);
199 anv_assert(pCreateInfo
->extent
.depth
> 0);
201 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
202 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
204 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
206 memset(image
, 0, sizeof(*image
));
207 image
->type
= pCreateInfo
->imageType
;
208 image
->extent
= pCreateInfo
->extent
;
209 image
->vk_format
= pCreateInfo
->format
;
210 image
->format
= anv_format_for_vk_format(pCreateInfo
->format
);
211 image
->levels
= pCreateInfo
->mipLevels
;
212 image
->array_size
= pCreateInfo
->arrayLayers
;
213 image
->samples
= pCreateInfo
->samples
;
214 image
->usage
= anv_image_get_full_usage(pCreateInfo
);
215 image
->tiling
= pCreateInfo
->tiling
;
217 if (likely(anv_format_is_color(image
->format
))) {
218 r
= make_surface(device
, image
, create_info
,
219 VK_IMAGE_ASPECT_COLOR_BIT
);
223 if (image
->format
->has_depth
) {
224 r
= make_surface(device
, image
, create_info
,
225 VK_IMAGE_ASPECT_DEPTH_BIT
);
230 if (image
->format
->has_stencil
) {
231 r
= make_surface(device
, image
, create_info
,
232 VK_IMAGE_ASPECT_STENCIL_BIT
);
238 *pImage
= anv_image_to_handle(image
);
244 anv_free2(&device
->alloc
, alloc
, image
);
250 anv_CreateImage(VkDevice device
,
251 const VkImageCreateInfo
*pCreateInfo
,
252 const VkAllocationCallbacks
*pAllocator
,
255 return anv_image_create(device
,
256 &(struct anv_image_create_info
) {
257 .vk_info
= pCreateInfo
,
258 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
265 anv_DestroyImage(VkDevice _device
, VkImage _image
,
266 const VkAllocationCallbacks
*pAllocator
)
268 ANV_FROM_HANDLE(anv_device
, device
, _device
);
270 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
274 anv_surface_get_subresource_layout(struct anv_image
*image
,
275 struct anv_surface
*surface
,
276 const VkImageSubresource
*subresource
,
277 VkSubresourceLayout
*layout
)
279 /* If we are on a non-zero mip level or array slice, we need to
280 * calculate a real offset.
282 anv_assert(subresource
->mipLevel
== 0);
283 anv_assert(subresource
->arrayLayer
== 0);
285 layout
->offset
= surface
->offset
;
286 layout
->rowPitch
= surface
->isl
.row_pitch
;
287 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
288 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
289 layout
->size
= surface
->isl
.size
;
292 void anv_GetImageSubresourceLayout(
295 const VkImageSubresource
* pSubresource
,
296 VkSubresourceLayout
* pLayout
)
298 ANV_FROM_HANDLE(anv_image
, image
, _image
);
300 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
302 switch (pSubresource
->aspectMask
) {
303 case VK_IMAGE_ASPECT_COLOR_BIT
:
304 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
305 pSubresource
, pLayout
);
307 case VK_IMAGE_ASPECT_DEPTH_BIT
:
308 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
309 pSubresource
, pLayout
);
311 case VK_IMAGE_ASPECT_STENCIL_BIT
:
312 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
313 pSubresource
, pLayout
);
316 assert(!"Invalid image aspect");
321 anv_validate_CreateImageView(VkDevice _device
,
322 const VkImageViewCreateInfo
*pCreateInfo
,
323 const VkAllocationCallbacks
*pAllocator
,
326 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
327 const VkImageSubresourceRange
*subresource
;
328 const struct anv_format
*view_format_info
;
330 /* Validate structure type before dereferencing it. */
332 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
333 subresource
= &pCreateInfo
->subresourceRange
;
335 /* Validate viewType is in range before using it. */
336 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
337 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
339 /* Validate format is in range before using it. */
340 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
341 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
342 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
344 /* Validate channel swizzles. */
345 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
346 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
347 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
348 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
349 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
350 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
351 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
352 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
354 /* Validate subresource. */
355 assert(subresource
->aspectMask
!= 0);
356 assert(subresource
->levelCount
> 0);
357 assert(subresource
->layerCount
> 0);
358 assert(subresource
->baseMipLevel
< image
->levels
);
359 assert(subresource
->baseMipLevel
+ subresource
->levelCount
<= image
->levels
);
360 assert(subresource
->baseArrayLayer
< image
->array_size
);
361 assert(subresource
->baseArrayLayer
+ subresource
->layerCount
<= image
->array_size
);
364 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
365 | VK_IMAGE_ASPECT_STENCIL_BIT
;
367 /* Validate format. */
368 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
369 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
370 assert(!image
->format
->has_depth
);
371 assert(!image
->format
->has_stencil
);
372 assert(!view_format_info
->has_depth
);
373 assert(!view_format_info
->has_stencil
);
374 assert(view_format_info
->isl_layout
->bs
==
375 image
->format
->isl_layout
->bs
);
376 } else if (subresource
->aspectMask
& ds_flags
) {
377 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
379 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
380 assert(image
->format
->has_depth
);
381 assert(view_format_info
->has_depth
);
382 assert(view_format_info
->isl_layout
->bs
==
383 image
->format
->isl_layout
->bs
);
386 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
387 /* FINISHME: Is it legal to have an R8 view of S8? */
388 assert(image
->format
->has_stencil
);
389 assert(view_format_info
->has_stencil
);
392 assert(!"bad VkImageSubresourceRange::aspectFlags");
395 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
399 anv_fill_image_surface_state(struct anv_device
*device
, struct anv_state state
,
400 struct anv_image_view
*iview
,
401 const VkImageViewCreateInfo
*pCreateInfo
,
402 VkImageUsageFlagBits usage
)
404 switch (device
->info
.gen
) {
406 if (device
->info
.is_haswell
)
407 gen75_fill_image_surface_state(device
, state
.map
, iview
,
410 gen7_fill_image_surface_state(device
, state
.map
, iview
,
414 gen8_fill_image_surface_state(device
, state
.map
, iview
,
418 gen9_fill_image_surface_state(device
, state
.map
, iview
,
422 unreachable("unsupported gen\n");
425 if (!device
->info
.has_llc
)
426 anv_state_clflush(state
);
429 static struct anv_state
430 alloc_surface_state(struct anv_device
*device
,
431 struct anv_cmd_buffer
*cmd_buffer
)
434 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
436 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
441 has_matching_storage_typed_format(const struct anv_device
*device
,
442 enum isl_format format
)
444 return (isl_format_get_layout(format
)->bs
<= 4 ||
445 (isl_format_get_layout(format
)->bs
<= 8 &&
446 (device
->info
.gen
>= 8 || device
->info
.is_haswell
)) ||
447 device
->info
.gen
>= 9);
450 static VkComponentSwizzle
451 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
452 struct anv_format_swizzle format_swizzle
)
454 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
458 case VK_COMPONENT_SWIZZLE_ZERO
:
459 return VK_COMPONENT_SWIZZLE_ZERO
;
460 case VK_COMPONENT_SWIZZLE_ONE
:
461 return VK_COMPONENT_SWIZZLE_ONE
;
462 case VK_COMPONENT_SWIZZLE_R
:
463 return VK_COMPONENT_SWIZZLE_R
+ format_swizzle
.r
;
464 case VK_COMPONENT_SWIZZLE_G
:
465 return VK_COMPONENT_SWIZZLE_R
+ format_swizzle
.g
;
466 case VK_COMPONENT_SWIZZLE_B
:
467 return VK_COMPONENT_SWIZZLE_R
+ format_swizzle
.b
;
468 case VK_COMPONENT_SWIZZLE_A
:
469 return VK_COMPONENT_SWIZZLE_R
+ format_swizzle
.a
;
471 unreachable("Invalid swizzle");
476 anv_image_view_init(struct anv_image_view
*iview
,
477 struct anv_device
*device
,
478 const VkImageViewCreateInfo
* pCreateInfo
,
479 struct anv_cmd_buffer
*cmd_buffer
,
482 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
483 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
484 VkImageViewCreateInfo mCreateInfo
;
485 memcpy(&mCreateInfo
, pCreateInfo
, sizeof(VkImageViewCreateInfo
));
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 iview
->format
= anv_get_isl_format(pCreateInfo
->format
, iview
->aspect_mask
,
519 image
->tiling
, &swizzle
);
520 iview
->swizzle
.r
= remap_swizzle(pCreateInfo
->components
.r
,
521 VK_COMPONENT_SWIZZLE_R
, swizzle
);
522 iview
->swizzle
.g
= remap_swizzle(pCreateInfo
->components
.g
,
523 VK_COMPONENT_SWIZZLE_G
, swizzle
);
524 iview
->swizzle
.b
= remap_swizzle(pCreateInfo
->components
.b
,
525 VK_COMPONENT_SWIZZLE_B
, swizzle
);
526 iview
->swizzle
.a
= remap_swizzle(pCreateInfo
->components
.a
,
527 VK_COMPONENT_SWIZZLE_A
, swizzle
);
529 iview
->base_layer
= range
->baseArrayLayer
;
530 iview
->base_mip
= range
->baseMipLevel
;
532 if (!isl_format_is_compressed(iview
->format
) &&
533 isl_format_is_compressed(image
->format
->isl_format
)) {
534 /* Scale the ImageView extent by the backing Image. This is used
535 * internally when an uncompressed ImageView is created on a
536 * compressed Image. The ImageView can therefore be used for copying
537 * data from a source Image to a destination Image.
539 const struct isl_format_layout
* isl_layout
= image
->format
->isl_layout
;
541 iview
->level_0_extent
.depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
);
542 iview
->level_0_extent
.depth
= DIV_ROUND_UP(iview
->level_0_extent
.depth
, isl_layout
->bd
);
544 iview
->level_0_extent
.height
= isl_surf_get_array_pitch_el_rows(&surface
->isl
) * image
->array_size
;
545 iview
->level_0_extent
.width
= isl_surf_get_row_pitch_el(&surface
->isl
);
546 mCreateInfo
.subresourceRange
.baseMipLevel
= 0;
547 mCreateInfo
.subresourceRange
.baseArrayLayer
= 0;
549 iview
->level_0_extent
.width
= image
->extent
.width
;
550 iview
->level_0_extent
.height
= image
->extent
.height
;
551 iview
->level_0_extent
.depth
= image
->extent
.depth
;
554 iview
->extent
= (VkExtent3D
) {
555 .width
= anv_minify(iview
->level_0_extent
.width
, range
->baseMipLevel
),
556 .height
= anv_minify(iview
->level_0_extent
.height
, range
->baseMipLevel
),
557 .depth
= anv_minify(iview
->level_0_extent
.depth
, range
->baseMipLevel
),
560 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
561 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
563 anv_fill_image_surface_state(device
, iview
->sampler_surface_state
,
565 VK_IMAGE_USAGE_SAMPLED_BIT
);
567 iview
->sampler_surface_state
.alloc_size
= 0;
570 if (image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
571 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
573 anv_fill_image_surface_state(device
, iview
->color_rt_surface_state
,
575 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
);
577 iview
->color_rt_surface_state
.alloc_size
= 0;
580 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
581 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
583 if (has_matching_storage_typed_format(device
, iview
->format
))
584 anv_fill_image_surface_state(device
, iview
->storage_surface_state
,
586 VK_IMAGE_USAGE_STORAGE_BIT
);
588 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
591 iview
->bo
->size
- iview
->offset
, 1);
594 iview
->storage_surface_state
.alloc_size
= 0;
599 anv_CreateImageView(VkDevice _device
,
600 const VkImageViewCreateInfo
*pCreateInfo
,
601 const VkAllocationCallbacks
*pAllocator
,
604 ANV_FROM_HANDLE(anv_device
, device
, _device
);
605 struct anv_image_view
*view
;
607 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
608 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
610 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
612 anv_image_view_init(view
, device
, pCreateInfo
, NULL
, 0);
614 *pView
= anv_image_view_to_handle(view
);
620 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
621 const VkAllocationCallbacks
*pAllocator
)
623 ANV_FROM_HANDLE(anv_device
, device
, _device
);
624 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
626 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
627 anv_state_pool_free(&device
->surface_state_pool
,
628 iview
->color_rt_surface_state
);
631 if (iview
->sampler_surface_state
.alloc_size
> 0) {
632 anv_state_pool_free(&device
->surface_state_pool
,
633 iview
->sampler_surface_state
);
636 if (iview
->storage_surface_state
.alloc_size
> 0) {
637 anv_state_pool_free(&device
->surface_state_pool
,
638 iview
->storage_surface_state
);
641 anv_free2(&device
->alloc
, pAllocator
, iview
);
645 anv_CreateBufferView(VkDevice _device
,
646 const VkBufferViewCreateInfo
*pCreateInfo
,
647 const VkAllocationCallbacks
*pAllocator
,
650 ANV_FROM_HANDLE(anv_device
, device
, _device
);
651 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
652 struct anv_buffer_view
*view
;
654 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
655 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
657 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
659 const struct anv_format
*format
=
660 anv_format_for_vk_format(pCreateInfo
->format
);
662 view
->format
= format
->isl_format
;
663 view
->bo
= buffer
->bo
;
664 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
665 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
666 buffer
->size
- view
->offset
: pCreateInfo
->range
;
668 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
669 view
->surface_state
=
670 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
672 anv_fill_buffer_surface_state(device
, view
->surface_state
,
674 view
->offset
, view
->range
,
675 format
->isl_layout
->bs
);
677 view
->surface_state
= (struct anv_state
){ 0 };
680 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
681 view
->storage_surface_state
=
682 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
684 enum isl_format storage_format
=
685 has_matching_storage_typed_format(device
, view
->format
) ?
686 isl_lower_storage_image_format(&device
->isl_dev
, view
->format
) :
689 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
691 view
->offset
, view
->range
,
692 (storage_format
== ISL_FORMAT_RAW
? 1 :
693 format
->isl_layout
->bs
));
696 view
->storage_surface_state
= (struct anv_state
){ 0 };
699 *pView
= anv_buffer_view_to_handle(view
);
705 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
706 const VkAllocationCallbacks
*pAllocator
)
708 ANV_FROM_HANDLE(anv_device
, device
, _device
);
709 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
711 if (view
->surface_state
.alloc_size
> 0)
712 anv_state_pool_free(&device
->surface_state_pool
,
713 view
->surface_state
);
715 if (view
->storage_surface_state
.alloc_size
> 0)
716 anv_state_pool_free(&device
->surface_state_pool
,
717 view
->storage_surface_state
);
719 anv_free2(&device
->alloc
, pAllocator
, view
);
723 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
725 switch (aspect_mask
) {
726 case VK_IMAGE_ASPECT_COLOR_BIT
:
727 /* Dragons will eat you.
729 * Meta attaches all destination surfaces as color render targets. Guess
730 * what surface the Meta Dragons really want.
732 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
733 return &image
->depth_surface
;
734 } else if (image
->format
->has_depth
) {
735 return &image
->depth_surface
;
736 } else if (image
->format
->has_stencil
) {
737 return &image
->stencil_surface
;
739 return &image
->color_surface
;
742 case VK_IMAGE_ASPECT_DEPTH_BIT
:
743 assert(image
->format
->has_depth
);
744 return &image
->depth_surface
;
745 case VK_IMAGE_ASPECT_STENCIL_BIT
:
746 assert(image
->format
->has_stencil
);
747 return &image
->stencil_surface
;
748 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
749 if (image
->format
->has_depth
&& image
->format
->has_stencil
) {
750 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
751 * combined depth stencil formats. Specifically, it states:
753 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
754 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
756 * Image views with both depth and stencil aspects are only valid for
757 * render target attachments, in which case
758 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
759 * stencil surfaces from the underlying surface.
761 return &image
->depth_surface
;
762 } else if (image
->format
->has_depth
) {
763 return &image
->depth_surface
;
764 } else if (image
->format
->has_stencil
) {
765 return &image
->stencil_surface
;
769 unreachable("image does not have aspect");
775 image_param_defaults(struct brw_image_param
*param
)
777 memset(param
, 0, sizeof *param
);
778 /* Set the swizzling shifts to all-ones to effectively disable swizzling --
779 * See emit_address_calculation() in brw_fs_surface_builder.cpp for a more
780 * detailed explanation of these parameters.
782 param
->swizzling
[0] = 0xff;
783 param
->swizzling
[1] = 0xff;
787 anv_image_view_fill_image_param(struct anv_device
*device
,
788 struct anv_image_view
*view
,
789 struct brw_image_param
*param
)
791 image_param_defaults(param
);
793 const struct isl_surf
*surf
= &view
->image
->color_surface
.isl
;
794 const int cpp
= isl_format_get_layout(surf
->format
)->bs
;
795 const struct isl_extent3d image_align_sa
=
796 isl_surf_get_image_alignment_sa(surf
);
798 param
->size
[0] = view
->extent
.width
;
799 param
->size
[1] = view
->extent
.height
;
800 if (surf
->dim
== ISL_SURF_DIM_3D
) {
801 param
->size
[2] = view
->extent
.depth
;
803 param
->size
[2] = surf
->logical_level0_px
.array_len
- view
->base_layer
;
806 isl_surf_get_image_offset_el(surf
, view
->base_mip
, view
->base_layer
, 0,
807 ¶m
->offset
[0], ¶m
->offset
[1]);
809 param
->stride
[0] = cpp
;
810 param
->stride
[1] = surf
->row_pitch
/ cpp
;
812 if (device
->info
.gen
< 9 && surf
->dim
== ISL_SURF_DIM_3D
) {
813 param
->stride
[2] = util_align_npot(param
->size
[0], image_align_sa
.w
);
814 param
->stride
[3] = util_align_npot(param
->size
[1], image_align_sa
.h
);
816 param
->stride
[2] = 0;
817 param
->stride
[3] = isl_surf_get_array_pitch_el_rows(surf
);
820 switch (surf
->tiling
) {
821 case ISL_TILING_LINEAR
:
822 /* image_param_defaults is good enough */
826 /* An X tile is a rectangular block of 512x8 bytes. */
827 param
->tiling
[0] = util_logbase2(512 / cpp
);
828 param
->tiling
[1] = util_logbase2(8);
830 if (device
->isl_dev
.has_bit6_swizzling
) {
831 /* Right shifts required to swizzle bits 9 and 10 of the memory
832 * address with bit 6.
834 param
->swizzling
[0] = 3;
835 param
->swizzling
[1] = 4;
840 /* The layout of a Y-tiled surface in memory isn't really fundamentally
841 * different to the layout of an X-tiled surface, we simply pretend that
842 * the surface is broken up in a number of smaller 16Bx32 tiles, each
843 * one arranged in X-major order just like is the case for X-tiling.
845 param
->tiling
[0] = util_logbase2(16 / cpp
);
846 param
->tiling
[1] = util_logbase2(32);
848 if (device
->isl_dev
.has_bit6_swizzling
) {
849 /* Right shift required to swizzle bit 9 of the memory address with
852 param
->swizzling
[0] = 3;
853 param
->swizzling
[1] = 0xff;
858 assert(!"Unhandled storage image tiling");
861 /* 3D textures are arranged in 2D in memory with 2^lod slices per row. The
862 * address calculation algorithm (emit_address_calculation() in
863 * brw_fs_surface_builder.cpp) handles this as a sort of tiling with
864 * modulus equal to the LOD.
866 param
->tiling
[2] = (device
->info
.gen
< 9 && surf
->dim
== ISL_SURF_DIM_3D
?
871 anv_buffer_view_fill_image_param(struct anv_device
*device
,
872 struct anv_buffer_view
*view
,
873 struct brw_image_param
*param
)
875 image_param_defaults(param
);
877 param
->stride
[0] = isl_format_layouts
[view
->format
].bs
;
878 param
->size
[0] = view
->range
/ param
->stride
[0];