2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30 #include "anv_private.h"
32 /* FIXME: We shouldn't be using the actual hardware enum values here. They
33 * change across gens. Once we get that fixed, this include needs to go.
35 #include "gen8_pack.h"
37 static const uint8_t anv_halign
[] = {
43 static const uint8_t anv_valign
[] = {
49 static const uint8_t anv_surf_type_from_image_type
[] = {
50 [VK_IMAGE_TYPE_1D
] = SURFTYPE_1D
,
51 [VK_IMAGE_TYPE_2D
] = SURFTYPE_2D
,
52 [VK_IMAGE_TYPE_3D
] = SURFTYPE_3D
,
55 static const struct anv_image_view_info
56 anv_image_view_info_table
[] = {
57 #define INFO(s, ...) { .surface_type = s, __VA_ARGS__ }
58 [VK_IMAGE_VIEW_TYPE_1D
] = INFO(SURFTYPE_1D
),
59 [VK_IMAGE_VIEW_TYPE_2D
] = INFO(SURFTYPE_2D
),
60 [VK_IMAGE_VIEW_TYPE_3D
] = INFO(SURFTYPE_3D
),
61 [VK_IMAGE_VIEW_TYPE_CUBE
] = INFO(SURFTYPE_CUBE
, .is_cube
= 1),
62 [VK_IMAGE_VIEW_TYPE_1D_ARRAY
] = INFO(SURFTYPE_1D
, .is_array
= 1),
63 [VK_IMAGE_VIEW_TYPE_2D_ARRAY
] = INFO(SURFTYPE_2D
, .is_array
= 1),
64 [VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
] = INFO(SURFTYPE_CUBE
, .is_array
= 1, .is_cube
= 1),
68 struct anv_image_view_info
69 anv_image_view_info_for_vk_image_view_type(VkImageViewType type
)
71 return anv_image_view_info_table
[type
];
75 * The \a format argument is required and overrides any format found in struct
76 * anv_image_create_info. Exactly one bit must be set in \a aspect.
78 static isl_surf_usage_flags_t
79 choose_isl_surf_usage(const struct anv_image_create_info
*info
,
80 VkImageAspectFlags aspect
)
82 const VkImageCreateInfo
*vk_info
= info
->vk_info
;
83 isl_surf_usage_flags_t isl_flags
= 0;
85 /* FINISHME: Support aux surfaces */
86 isl_flags
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
88 if (vk_info
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
89 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
91 if (vk_info
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
92 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
94 if (vk_info
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
95 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
97 if (vk_info
->flags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
98 isl_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
100 if (vk_info
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
103 unreachable("bad VkImageAspect");
104 case VK_IMAGE_ASPECT_DEPTH_BIT
:
105 isl_flags
|= ISL_SURF_USAGE_DEPTH_BIT
;
107 case VK_IMAGE_ASPECT_STENCIL_BIT
:
108 isl_flags
|= ISL_SURF_USAGE_STENCIL_BIT
;
113 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
114 /* Meta implements transfers by sampling from the source image. */
115 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
118 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
119 /* Meta implements transfers by rendering into the destination image. */
120 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
127 * Exactly one bit must be set in \a aspect.
129 static struct anv_surface
*
130 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
134 unreachable("bad VkImageAspect");
135 case VK_IMAGE_ASPECT_COLOR_BIT
:
136 return &image
->color_surface
;
137 case VK_IMAGE_ASPECT_DEPTH_BIT
:
138 return &image
->depth_surface
;
139 case VK_IMAGE_ASPECT_STENCIL_BIT
:
140 return &image
->stencil_surface
;
145 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
146 * image's memory requirements (that is, the image's size and alignment).
148 * Exactly one bit must be set in \a aspect.
151 make_surface(const struct anv_device
*dev
,
152 struct anv_image
*image
,
153 const struct anv_image_create_info
*anv_info
,
154 VkImageAspectFlags aspect
)
156 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
159 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
160 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
161 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
162 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
165 isl_tiling_flags_t tiling_flags
= anv_info
->isl_tiling_flags
;
166 if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
)
167 tiling_flags
&= ISL_TILING_LINEAR_BIT
;
169 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
171 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
172 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
173 .format
= anv_get_isl_format(vk_info
->format
, aspect
),
174 .width
= vk_info
->extent
.width
,
175 .height
= vk_info
->extent
.height
,
176 .depth
= vk_info
->extent
.depth
,
177 .levels
= vk_info
->mipLevels
,
178 .array_len
= vk_info
->arrayLayers
,
179 .samples
= vk_info
->samples
,
182 .usage
= choose_isl_surf_usage(anv_info
, aspect
),
183 .tiling_flags
= tiling_flags
);
185 /* isl_surf_init() will fail only if provided invalid input. Invalid input
186 * is illegal in Vulkan.
190 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
191 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
192 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
197 static VkImageUsageFlags
198 anv_image_get_full_usage(const VkImageCreateInfo
*info
)
200 VkImageUsageFlags usage
= info
->usage
;
202 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
203 /* Meta will transfer from the image by binding it as a texture. */
204 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
207 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
208 /* Meta will transfer to the image by binding it as a color attachment,
209 * even if the image format is not a color format.
211 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
218 anv_image_create(VkDevice _device
,
219 const struct anv_image_create_info
*create_info
,
220 const VkAllocationCallbacks
* alloc
,
223 ANV_FROM_HANDLE(anv_device
, device
, _device
);
224 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
225 struct anv_image
*image
= NULL
;
228 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
230 anv_assert(pCreateInfo
->mipLevels
> 0);
231 anv_assert(pCreateInfo
->arrayLayers
> 0);
232 anv_assert(pCreateInfo
->samples
== VK_SAMPLE_COUNT_1_BIT
);
233 anv_assert(pCreateInfo
->extent
.width
> 0);
234 anv_assert(pCreateInfo
->extent
.height
> 0);
235 anv_assert(pCreateInfo
->extent
.depth
> 0);
237 /* TODO(chadv): How should we validate inputs? */
238 const uint8_t surf_type
=
239 anv_surf_type_from_image_type
[pCreateInfo
->imageType
];
241 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
242 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
244 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
246 memset(image
, 0, sizeof(*image
));
247 image
->type
= pCreateInfo
->imageType
;
248 image
->extent
= pCreateInfo
->extent
;
249 image
->format
= anv_format_for_vk_format(pCreateInfo
->format
);
250 image
->levels
= pCreateInfo
->mipLevels
;
251 image
->array_size
= pCreateInfo
->arrayLayers
;
252 image
->usage
= anv_image_get_full_usage(pCreateInfo
);
253 image
->surface_type
= surf_type
;
255 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
256 image
->needs_nonrt_surface_state
= true;
259 if (image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
260 image
->needs_color_rt_surface_state
= true;
263 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
264 image
->needs_storage_surface_state
= true;
267 if (likely(anv_format_is_color(image
->format
))) {
268 r
= make_surface(device
, image
, create_info
,
269 VK_IMAGE_ASPECT_COLOR_BIT
);
273 if (image
->format
->depth_format
) {
274 r
= make_surface(device
, image
, create_info
,
275 VK_IMAGE_ASPECT_DEPTH_BIT
);
280 if (image
->format
->has_stencil
) {
281 r
= make_surface(device
, image
, create_info
,
282 VK_IMAGE_ASPECT_STENCIL_BIT
);
288 *pImage
= anv_image_to_handle(image
);
294 anv_free2(&device
->alloc
, alloc
, image
);
300 anv_CreateImage(VkDevice device
,
301 const VkImageCreateInfo
*pCreateInfo
,
302 const VkAllocationCallbacks
*pAllocator
,
305 return anv_image_create(device
,
306 &(struct anv_image_create_info
) {
307 .vk_info
= pCreateInfo
,
308 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
315 anv_DestroyImage(VkDevice _device
, VkImage _image
,
316 const VkAllocationCallbacks
*pAllocator
)
318 ANV_FROM_HANDLE(anv_device
, device
, _device
);
320 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
324 anv_surface_get_subresource_layout(struct anv_image
*image
,
325 struct anv_surface
*surface
,
326 const VkImageSubresource
*subresource
,
327 VkSubresourceLayout
*layout
)
329 /* If we are on a non-zero mip level or array slice, we need to
330 * calculate a real offset.
332 anv_assert(subresource
->mipLevel
== 0);
333 anv_assert(subresource
->arrayLayer
== 0);
335 layout
->offset
= surface
->offset
;
336 layout
->rowPitch
= surface
->isl
.row_pitch
;
337 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
338 layout
->size
= surface
->isl
.size
;
341 void anv_GetImageSubresourceLayout(
344 const VkImageSubresource
* pSubresource
,
345 VkSubresourceLayout
* pLayout
)
347 ANV_FROM_HANDLE(anv_image
, image
, _image
);
349 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
351 switch (pSubresource
->aspectMask
) {
352 case VK_IMAGE_ASPECT_COLOR_BIT
:
353 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
354 pSubresource
, pLayout
);
356 case VK_IMAGE_ASPECT_DEPTH_BIT
:
357 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
358 pSubresource
, pLayout
);
360 case VK_IMAGE_ASPECT_STENCIL_BIT
:
361 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
362 pSubresource
, pLayout
);
365 assert(!"Invalid image aspect");
370 anv_validate_CreateImageView(VkDevice _device
,
371 const VkImageViewCreateInfo
*pCreateInfo
,
372 const VkAllocationCallbacks
*pAllocator
,
375 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
376 const VkImageSubresourceRange
*subresource
;
377 const struct anv_image_view_info
*view_info
;
378 const struct anv_format
*view_format_info
;
380 /* Validate structure type before dereferencing it. */
382 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
383 subresource
= &pCreateInfo
->subresourceRange
;
385 /* Validate viewType is in range before using it. */
386 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
387 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
388 view_info
= &anv_image_view_info_table
[pCreateInfo
->viewType
];
390 /* Validate format is in range before using it. */
391 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
392 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
393 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
395 /* Validate channel swizzles. */
396 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
397 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
398 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
399 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
400 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
401 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
402 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
403 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
405 /* Validate subresource. */
406 assert(subresource
->aspectMask
!= 0);
407 assert(subresource
->levelCount
> 0);
408 assert(subresource
->layerCount
> 0);
409 assert(subresource
->baseMipLevel
< image
->levels
);
410 assert(subresource
->baseMipLevel
+ subresource
->levelCount
<= image
->levels
);
411 assert(subresource
->baseArrayLayer
< image
->array_size
);
412 assert(subresource
->baseArrayLayer
+ subresource
->layerCount
<= image
->array_size
);
415 if (view_info
->is_cube
) {
416 assert(subresource
->baseArrayLayer
% 6 == 0);
417 assert(subresource
->layerCount
% 6 == 0);
420 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
421 | VK_IMAGE_ASPECT_STENCIL_BIT
;
423 /* Validate format. */
424 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
425 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
426 assert(!image
->format
->depth_format
);
427 assert(!image
->format
->has_stencil
);
428 assert(!view_format_info
->depth_format
);
429 assert(!view_format_info
->has_stencil
);
430 assert(view_format_info
->isl_layout
->bs
==
431 image
->format
->isl_layout
->bs
);
432 } else if (subresource
->aspectMask
& ds_flags
) {
433 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
435 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
436 assert(image
->format
->depth_format
);
437 assert(view_format_info
->depth_format
);
438 assert(view_format_info
->isl_layout
->bs
==
439 image
->format
->isl_layout
->bs
);
442 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
443 /* FINISHME: Is it legal to have an R8 view of S8? */
444 assert(image
->format
->has_stencil
);
445 assert(view_format_info
->has_stencil
);
448 assert(!"bad VkImageSubresourceRange::aspectFlags");
451 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
455 anv_image_view_init(struct anv_image_view
*iview
,
456 struct anv_device
*device
,
457 const VkImageViewCreateInfo
* pCreateInfo
,
458 struct anv_cmd_buffer
*cmd_buffer
)
460 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
461 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
463 assert(range
->layerCount
> 0);
464 assert(range
->baseMipLevel
< image
->levels
);
465 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
466 VK_IMAGE_USAGE_STORAGE_BIT
|
467 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
468 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
470 switch (image
->type
) {
472 unreachable("bad VkImageType");
473 case VK_IMAGE_TYPE_1D
:
474 case VK_IMAGE_TYPE_2D
:
475 assert(range
->baseArrayLayer
+ range
->layerCount
- 1 <= image
->array_size
);
477 case VK_IMAGE_TYPE_3D
:
478 assert(range
->baseArrayLayer
+ range
->layerCount
- 1
479 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
483 switch (device
->info
.gen
) {
485 if (device
->info
.is_haswell
)
486 gen75_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
488 gen7_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
491 gen8_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
494 gen9_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
497 unreachable("unsupported gen\n");
502 anv_CreateImageView(VkDevice _device
,
503 const VkImageViewCreateInfo
*pCreateInfo
,
504 const VkAllocationCallbacks
*pAllocator
,
507 ANV_FROM_HANDLE(anv_device
, device
, _device
);
508 struct anv_image_view
*view
;
510 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
511 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
513 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
515 anv_image_view_init(view
, device
, pCreateInfo
, NULL
);
517 *pView
= anv_image_view_to_handle(view
);
523 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
524 const VkAllocationCallbacks
*pAllocator
)
526 ANV_FROM_HANDLE(anv_device
, device
, _device
);
527 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
529 if (iview
->image
->needs_color_rt_surface_state
) {
530 anv_state_pool_free(&device
->surface_state_pool
,
531 iview
->color_rt_surface_state
);
534 if (iview
->image
->needs_nonrt_surface_state
) {
535 anv_state_pool_free(&device
->surface_state_pool
,
536 iview
->nonrt_surface_state
);
539 if (iview
->image
->needs_storage_surface_state
) {
540 anv_state_pool_free(&device
->surface_state_pool
,
541 iview
->storage_surface_state
);
544 anv_free2(&device
->alloc
, pAllocator
, iview
);
548 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
550 switch (aspect_mask
) {
551 case VK_IMAGE_ASPECT_COLOR_BIT
:
552 /* Dragons will eat you.
554 * Meta attaches all destination surfaces as color render targets. Guess
555 * what surface the Meta Dragons really want.
557 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
558 anv_finishme("combined depth stencil formats");
559 return &image
->depth_surface
;
560 } else if (image
->format
->depth_format
) {
561 return &image
->depth_surface
;
562 } else if (image
->format
->has_stencil
) {
563 return &image
->stencil_surface
;
565 return &image
->color_surface
;
568 case VK_IMAGE_ASPECT_DEPTH_BIT
:
569 assert(image
->format
->depth_format
);
570 return &image
->depth_surface
;
571 case VK_IMAGE_ASPECT_STENCIL_BIT
:
572 assert(image
->format
->has_stencil
);
573 return &image
->stencil_surface
;
574 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
575 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
576 /* FINISHME: The Vulkan spec (git a511ba2) requires support for combined
577 * depth stencil formats. Specifically, it states:
579 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
580 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
582 anv_finishme("combined depthstencil aspect");
583 return &image
->depth_surface
;
584 } else if (image
->format
->depth_format
) {
585 return &image
->depth_surface
;
586 } else if (image
->format
->has_stencil
) {
587 return &image
->stencil_surface
;
591 unreachable("image does not have aspect");
597 anv_image_view_fill_image_param(struct anv_device
*device
,
598 struct anv_image_view
*view
,
599 struct brw_image_param
*param
)
601 memset(param
, 0, sizeof *param
);
602 anv_finishme("Actually fill out brw_image_param");