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_surf_type_from_image_type
[] = {
38 [VK_IMAGE_TYPE_1D
] = SURFTYPE_1D
,
39 [VK_IMAGE_TYPE_2D
] = SURFTYPE_2D
,
40 [VK_IMAGE_TYPE_3D
] = SURFTYPE_3D
,
44 * The \a format argument is required and overrides any format found in struct
45 * anv_image_create_info. Exactly one bit must be set in \a aspect.
47 static isl_surf_usage_flags_t
48 choose_isl_surf_usage(const struct anv_image_create_info
*info
,
49 VkImageAspectFlags aspect
)
51 const VkImageCreateInfo
*vk_info
= info
->vk_info
;
52 isl_surf_usage_flags_t isl_flags
= 0;
54 /* FINISHME: Support aux surfaces */
55 isl_flags
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
57 if (vk_info
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
58 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
60 if (vk_info
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
61 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
63 if (vk_info
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
64 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
66 if (vk_info
->flags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
67 isl_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
69 if (vk_info
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
72 unreachable("bad VkImageAspect");
73 case VK_IMAGE_ASPECT_DEPTH_BIT
:
74 isl_flags
|= ISL_SURF_USAGE_DEPTH_BIT
;
76 case VK_IMAGE_ASPECT_STENCIL_BIT
:
77 isl_flags
|= ISL_SURF_USAGE_STENCIL_BIT
;
82 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
83 /* Meta implements transfers by sampling from the source image. */
84 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
87 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
88 /* Meta implements transfers by rendering into the destination image. */
89 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
96 * Exactly one bit must be set in \a aspect.
98 static struct anv_surface
*
99 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
103 unreachable("bad VkImageAspect");
104 case VK_IMAGE_ASPECT_COLOR_BIT
:
105 return &image
->color_surface
;
106 case VK_IMAGE_ASPECT_DEPTH_BIT
:
107 return &image
->depth_surface
;
108 case VK_IMAGE_ASPECT_STENCIL_BIT
:
109 return &image
->stencil_surface
;
114 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
115 * image's memory requirements (that is, the image's size and alignment).
117 * Exactly one bit must be set in \a aspect.
120 make_surface(const struct anv_device
*dev
,
121 struct anv_image
*image
,
122 const struct anv_image_create_info
*anv_info
,
123 VkImageAspectFlags aspect
)
125 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
128 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
129 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
130 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
131 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
134 isl_tiling_flags_t tiling_flags
= anv_info
->isl_tiling_flags
;
135 if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
)
136 tiling_flags
&= ISL_TILING_LINEAR_BIT
;
138 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
140 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
141 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
142 .format
= anv_get_isl_format(vk_info
->format
, aspect
),
143 .width
= vk_info
->extent
.width
,
144 .height
= vk_info
->extent
.height
,
145 .depth
= vk_info
->extent
.depth
,
146 .levels
= vk_info
->mipLevels
,
147 .array_len
= vk_info
->arrayLayers
,
148 .samples
= vk_info
->samples
,
151 .usage
= choose_isl_surf_usage(anv_info
, aspect
),
152 .tiling_flags
= tiling_flags
);
154 /* isl_surf_init() will fail only if provided invalid input. Invalid input
155 * is illegal in Vulkan.
159 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
160 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
161 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
166 static VkImageUsageFlags
167 anv_image_get_full_usage(const VkImageCreateInfo
*info
)
169 VkImageUsageFlags usage
= info
->usage
;
171 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
172 /* Meta will transfer from the image by binding it as a texture. */
173 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
176 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
177 /* Meta will transfer to the image by binding it as a color attachment,
178 * even if the image format is not a color format.
180 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
187 anv_image_create(VkDevice _device
,
188 const struct anv_image_create_info
*create_info
,
189 const VkAllocationCallbacks
* alloc
,
192 ANV_FROM_HANDLE(anv_device
, device
, _device
);
193 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
194 struct anv_image
*image
= NULL
;
197 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
199 anv_assert(pCreateInfo
->mipLevels
> 0);
200 anv_assert(pCreateInfo
->arrayLayers
> 0);
201 anv_assert(pCreateInfo
->samples
== VK_SAMPLE_COUNT_1_BIT
);
202 anv_assert(pCreateInfo
->extent
.width
> 0);
203 anv_assert(pCreateInfo
->extent
.height
> 0);
204 anv_assert(pCreateInfo
->extent
.depth
> 0);
206 /* TODO(chadv): How should we validate inputs? */
207 const uint8_t surf_type
=
208 anv_surf_type_from_image_type
[pCreateInfo
->imageType
];
210 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
211 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
213 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
215 memset(image
, 0, sizeof(*image
));
216 image
->type
= pCreateInfo
->imageType
;
217 image
->extent
= pCreateInfo
->extent
;
218 image
->format
= anv_format_for_vk_format(pCreateInfo
->format
);
219 image
->levels
= pCreateInfo
->mipLevels
;
220 image
->array_size
= pCreateInfo
->arrayLayers
;
221 image
->usage
= anv_image_get_full_usage(pCreateInfo
);
222 image
->surface_type
= surf_type
;
224 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
225 image
->needs_nonrt_surface_state
= true;
228 if (image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
229 image
->needs_color_rt_surface_state
= true;
232 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
233 image
->needs_storage_surface_state
= true;
236 if (likely(anv_format_is_color(image
->format
))) {
237 r
= make_surface(device
, image
, create_info
,
238 VK_IMAGE_ASPECT_COLOR_BIT
);
242 if (image
->format
->depth_format
) {
243 r
= make_surface(device
, image
, create_info
,
244 VK_IMAGE_ASPECT_DEPTH_BIT
);
249 if (image
->format
->has_stencil
) {
250 r
= make_surface(device
, image
, create_info
,
251 VK_IMAGE_ASPECT_STENCIL_BIT
);
257 *pImage
= anv_image_to_handle(image
);
263 anv_free2(&device
->alloc
, alloc
, image
);
269 anv_CreateImage(VkDevice device
,
270 const VkImageCreateInfo
*pCreateInfo
,
271 const VkAllocationCallbacks
*pAllocator
,
274 return anv_image_create(device
,
275 &(struct anv_image_create_info
) {
276 .vk_info
= pCreateInfo
,
277 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
284 anv_DestroyImage(VkDevice _device
, VkImage _image
,
285 const VkAllocationCallbacks
*pAllocator
)
287 ANV_FROM_HANDLE(anv_device
, device
, _device
);
289 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
293 anv_surface_get_subresource_layout(struct anv_image
*image
,
294 struct anv_surface
*surface
,
295 const VkImageSubresource
*subresource
,
296 VkSubresourceLayout
*layout
)
298 /* If we are on a non-zero mip level or array slice, we need to
299 * calculate a real offset.
301 anv_assert(subresource
->mipLevel
== 0);
302 anv_assert(subresource
->arrayLayer
== 0);
304 layout
->offset
= surface
->offset
;
305 layout
->rowPitch
= surface
->isl
.row_pitch
;
306 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
307 layout
->size
= surface
->isl
.size
;
310 void anv_GetImageSubresourceLayout(
313 const VkImageSubresource
* pSubresource
,
314 VkSubresourceLayout
* pLayout
)
316 ANV_FROM_HANDLE(anv_image
, image
, _image
);
318 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
320 switch (pSubresource
->aspectMask
) {
321 case VK_IMAGE_ASPECT_COLOR_BIT
:
322 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
323 pSubresource
, pLayout
);
325 case VK_IMAGE_ASPECT_DEPTH_BIT
:
326 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
327 pSubresource
, pLayout
);
329 case VK_IMAGE_ASPECT_STENCIL_BIT
:
330 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
331 pSubresource
, pLayout
);
334 assert(!"Invalid image aspect");
339 anv_validate_CreateImageView(VkDevice _device
,
340 const VkImageViewCreateInfo
*pCreateInfo
,
341 const VkAllocationCallbacks
*pAllocator
,
344 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
345 const VkImageSubresourceRange
*subresource
;
346 const struct anv_format
*view_format_info
;
348 /* Validate structure type before dereferencing it. */
350 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
351 subresource
= &pCreateInfo
->subresourceRange
;
353 /* Validate viewType is in range before using it. */
354 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
355 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
357 /* Validate format is in range before using it. */
358 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
359 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
360 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
362 /* Validate channel swizzles. */
363 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
364 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
365 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
366 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
367 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
368 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
369 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
370 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
372 /* Validate subresource. */
373 assert(subresource
->aspectMask
!= 0);
374 assert(subresource
->levelCount
> 0);
375 assert(subresource
->layerCount
> 0);
376 assert(subresource
->baseMipLevel
< image
->levels
);
377 assert(subresource
->baseMipLevel
+ subresource
->levelCount
<= image
->levels
);
378 assert(subresource
->baseArrayLayer
< image
->array_size
);
379 assert(subresource
->baseArrayLayer
+ subresource
->layerCount
<= image
->array_size
);
382 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
383 | VK_IMAGE_ASPECT_STENCIL_BIT
;
385 /* Validate format. */
386 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
387 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
388 assert(!image
->format
->depth_format
);
389 assert(!image
->format
->has_stencil
);
390 assert(!view_format_info
->depth_format
);
391 assert(!view_format_info
->has_stencil
);
392 assert(view_format_info
->isl_layout
->bs
==
393 image
->format
->isl_layout
->bs
);
394 } else if (subresource
->aspectMask
& ds_flags
) {
395 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
397 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
398 assert(image
->format
->depth_format
);
399 assert(view_format_info
->depth_format
);
400 assert(view_format_info
->isl_layout
->bs
==
401 image
->format
->isl_layout
->bs
);
404 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
405 /* FINISHME: Is it legal to have an R8 view of S8? */
406 assert(image
->format
->has_stencil
);
407 assert(view_format_info
->has_stencil
);
410 assert(!"bad VkImageSubresourceRange::aspectFlags");
413 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
417 anv_image_view_init(struct anv_image_view
*iview
,
418 struct anv_device
*device
,
419 const VkImageViewCreateInfo
* pCreateInfo
,
420 struct anv_cmd_buffer
*cmd_buffer
)
422 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
423 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
425 assert(range
->layerCount
> 0);
426 assert(range
->baseMipLevel
< image
->levels
);
427 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
428 VK_IMAGE_USAGE_STORAGE_BIT
|
429 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
430 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
432 switch (image
->type
) {
434 unreachable("bad VkImageType");
435 case VK_IMAGE_TYPE_1D
:
436 case VK_IMAGE_TYPE_2D
:
437 assert(range
->baseArrayLayer
+ range
->layerCount
- 1 <= image
->array_size
);
439 case VK_IMAGE_TYPE_3D
:
440 assert(range
->baseArrayLayer
+ range
->layerCount
- 1
441 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
445 switch (device
->info
.gen
) {
447 if (device
->info
.is_haswell
)
448 gen75_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
450 gen7_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
453 gen8_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
456 gen9_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
459 unreachable("unsupported gen\n");
464 anv_CreateImageView(VkDevice _device
,
465 const VkImageViewCreateInfo
*pCreateInfo
,
466 const VkAllocationCallbacks
*pAllocator
,
469 ANV_FROM_HANDLE(anv_device
, device
, _device
);
470 struct anv_image_view
*view
;
472 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
473 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
475 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
477 anv_image_view_init(view
, device
, pCreateInfo
, NULL
);
479 *pView
= anv_image_view_to_handle(view
);
485 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
486 const VkAllocationCallbacks
*pAllocator
)
488 ANV_FROM_HANDLE(anv_device
, device
, _device
);
489 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
491 if (iview
->image
->needs_color_rt_surface_state
) {
492 anv_state_pool_free(&device
->surface_state_pool
,
493 iview
->color_rt_surface_state
);
496 if (iview
->image
->needs_nonrt_surface_state
) {
497 anv_state_pool_free(&device
->surface_state_pool
,
498 iview
->nonrt_surface_state
);
501 if (iview
->image
->needs_storage_surface_state
) {
502 anv_state_pool_free(&device
->surface_state_pool
,
503 iview
->storage_surface_state
);
506 anv_free2(&device
->alloc
, pAllocator
, iview
);
510 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
512 switch (aspect_mask
) {
513 case VK_IMAGE_ASPECT_COLOR_BIT
:
514 /* Dragons will eat you.
516 * Meta attaches all destination surfaces as color render targets. Guess
517 * what surface the Meta Dragons really want.
519 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
520 anv_finishme("combined depth stencil formats");
521 return &image
->depth_surface
;
522 } else if (image
->format
->depth_format
) {
523 return &image
->depth_surface
;
524 } else if (image
->format
->has_stencil
) {
525 return &image
->stencil_surface
;
527 return &image
->color_surface
;
530 case VK_IMAGE_ASPECT_DEPTH_BIT
:
531 assert(image
->format
->depth_format
);
532 return &image
->depth_surface
;
533 case VK_IMAGE_ASPECT_STENCIL_BIT
:
534 assert(image
->format
->has_stencil
);
535 return &image
->stencil_surface
;
536 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
537 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
538 /* FINISHME: The Vulkan spec (git a511ba2) requires support for combined
539 * depth stencil formats. Specifically, it states:
541 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
542 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
544 anv_finishme("combined depthstencil aspect");
545 return &image
->depth_surface
;
546 } else if (image
->format
->depth_format
) {
547 return &image
->depth_surface
;
548 } else if (image
->format
->has_stencil
) {
549 return &image
->stencil_surface
;
553 unreachable("image does not have aspect");
559 anv_image_view_fill_image_param(struct anv_device
*device
,
560 struct anv_image_view
*view
,
561 struct brw_image_param
*param
)
563 memset(param
, 0, sizeof *param
);
564 anv_finishme("Actually fill out brw_image_param");