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
];
74 static isl_tiling_flags_t
75 choose_isl_tiling_flags(const struct anv_image_create_info
*anv_info
)
77 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
79 if (anv_info
->force_tiling
) {
80 return 1u << anv_info
->tiling
;
81 } else if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) {
82 return ISL_TILING_LINEAR_BIT
;
83 } else if (vk_info
->tiling
== VK_IMAGE_TILING_OPTIMAL
) {
84 return ISL_TILING_ANY_MASK
;
86 unreachable("bad anv_image_create_info");
92 * The \a format argument is required and overrides any format found in struct
93 * anv_image_create_info. Exactly one bit must be set in \a aspect.
95 static isl_surf_usage_flags_t
96 choose_isl_surf_usage(const struct anv_image_create_info
*info
,
97 VkImageAspectFlags aspect
)
99 const VkImageCreateInfo
*vk_info
= info
->vk_info
;
100 isl_surf_usage_flags_t isl_flags
= 0;
102 /* FINISHME: Support aux surfaces */
103 isl_flags
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
105 if (vk_info
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
106 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
108 if (vk_info
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
109 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
111 if (vk_info
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
112 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
114 if (vk_info
->flags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
115 isl_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
117 if (vk_info
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
120 unreachable("bad VkImageAspect");
121 case VK_IMAGE_ASPECT_DEPTH_BIT
:
122 isl_flags
|= ISL_SURF_USAGE_DEPTH_BIT
;
124 case VK_IMAGE_ASPECT_STENCIL_BIT
:
125 isl_flags
|= ISL_SURF_USAGE_STENCIL_BIT
;
130 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
131 /* Meta implements transfers by sampling from the source image. */
132 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
135 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
136 /* Meta implements transfers by rendering into the destination image. */
137 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
144 * The \a format argument is required and overrides any format in struct
145 * anv_image_create_info. Exactly one bit must be set in \a aspect.
148 anv_image_make_surface(const struct anv_device
*dev
,
149 const struct anv_image_create_info
*anv_info
,
150 VkImageAspectFlags aspect
,
151 uint64_t *inout_image_size
,
152 uint32_t *inout_image_alignment
,
153 struct anv_surface
*out_anv_surf
)
155 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
157 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
158 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
159 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
160 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
163 isl_surf_init(&dev
->isl_dev
, &out_anv_surf
->isl
,
164 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
165 .format
= anv_get_isl_format(vk_info
->format
, aspect
),
166 .width
= vk_info
->extent
.width
,
167 .height
= vk_info
->extent
.height
,
168 .depth
= vk_info
->extent
.depth
,
169 .levels
= vk_info
->mipLevels
,
170 .array_len
= vk_info
->arrayLayers
,
171 .samples
= vk_info
->samples
,
174 .usage
= choose_isl_surf_usage(anv_info
, aspect
),
175 .tiling_flags
= choose_isl_tiling_flags(anv_info
));
177 out_anv_surf
->offset
= align_u32(*inout_image_size
,
178 out_anv_surf
->isl
.alignment
);
180 *inout_image_size
= out_anv_surf
->offset
+ out_anv_surf
->isl
.size
;
181 *inout_image_alignment
= MAX(*inout_image_alignment
,
182 out_anv_surf
->isl
.alignment
);
187 static VkImageUsageFlags
188 anv_image_get_full_usage(const VkImageCreateInfo
*info
)
190 VkImageUsageFlags usage
= info
->usage
;
192 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
193 /* Meta will transfer from the image by binding it as a texture. */
194 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
197 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
198 /* Meta will transfer to the image by binding it as a color attachment,
199 * even if the image format is not a color format.
201 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
208 anv_image_create(VkDevice _device
,
209 const struct anv_image_create_info
*create_info
,
210 const VkAllocationCallbacks
* alloc
,
213 ANV_FROM_HANDLE(anv_device
, device
, _device
);
214 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
215 struct anv_image
*image
= NULL
;
218 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
220 anv_assert(pCreateInfo
->mipLevels
> 0);
221 anv_assert(pCreateInfo
->arrayLayers
> 0);
222 anv_assert(pCreateInfo
->samples
== VK_SAMPLE_COUNT_1_BIT
);
223 anv_assert(pCreateInfo
->extent
.width
> 0);
224 anv_assert(pCreateInfo
->extent
.height
> 0);
225 anv_assert(pCreateInfo
->extent
.depth
> 0);
227 /* TODO(chadv): How should we validate inputs? */
228 const uint8_t surf_type
=
229 anv_surf_type_from_image_type
[pCreateInfo
->imageType
];
231 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
232 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
234 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
236 memset(image
, 0, sizeof(*image
));
237 image
->type
= pCreateInfo
->imageType
;
238 image
->extent
= pCreateInfo
->extent
;
239 image
->format
= anv_format_for_vk_format(pCreateInfo
->format
);
240 image
->levels
= pCreateInfo
->mipLevels
;
241 image
->array_size
= pCreateInfo
->arrayLayers
;
242 image
->usage
= anv_image_get_full_usage(pCreateInfo
);
243 image
->surface_type
= surf_type
;
245 if (image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
246 VK_IMAGE_USAGE_STORAGE_BIT
)) {
247 image
->needs_nonrt_surface_state
= true;
250 if (image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
251 image
->needs_color_rt_surface_state
= true;
254 if (likely(anv_format_is_color(image
->format
))) {
255 r
= anv_image_make_surface(device
, create_info
,
256 VK_IMAGE_ASPECT_COLOR_BIT
,
257 &image
->size
, &image
->alignment
,
258 &image
->color_surface
);
262 if (image
->format
->depth_format
) {
263 r
= anv_image_make_surface(device
, create_info
,
264 VK_IMAGE_ASPECT_DEPTH_BIT
,
265 &image
->size
, &image
->alignment
,
266 &image
->depth_surface
);
271 if (image
->format
->has_stencil
) {
272 r
= anv_image_make_surface(device
, create_info
,
273 VK_IMAGE_ASPECT_STENCIL_BIT
,
274 &image
->size
, &image
->alignment
,
275 &image
->stencil_surface
);
281 *pImage
= anv_image_to_handle(image
);
287 anv_free2(&device
->alloc
, alloc
, image
);
293 anv_CreateImage(VkDevice device
,
294 const VkImageCreateInfo
*pCreateInfo
,
295 const VkAllocationCallbacks
*pAllocator
,
298 return anv_image_create(device
,
299 &(struct anv_image_create_info
) {
300 .vk_info
= pCreateInfo
,
307 anv_DestroyImage(VkDevice _device
, VkImage _image
,
308 const VkAllocationCallbacks
*pAllocator
)
310 ANV_FROM_HANDLE(anv_device
, device
, _device
);
312 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
316 anv_surface_get_subresource_layout(struct anv_image
*image
,
317 struct anv_surface
*surface
,
318 const VkImageSubresource
*subresource
,
319 VkSubresourceLayout
*layout
)
321 /* If we are on a non-zero mip level or array slice, we need to
322 * calculate a real offset.
324 anv_assert(subresource
->mipLevel
== 0);
325 anv_assert(subresource
->arrayLayer
== 0);
327 layout
->offset
= surface
->offset
;
328 layout
->rowPitch
= surface
->isl
.row_pitch
;
329 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
330 layout
->size
= surface
->isl
.size
;
333 void anv_GetImageSubresourceLayout(
336 const VkImageSubresource
* pSubresource
,
337 VkSubresourceLayout
* pLayout
)
339 ANV_FROM_HANDLE(anv_image
, image
, _image
);
341 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
343 switch (pSubresource
->aspectMask
) {
344 case VK_IMAGE_ASPECT_COLOR_BIT
:
345 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
346 pSubresource
, pLayout
);
348 case VK_IMAGE_ASPECT_DEPTH_BIT
:
349 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
350 pSubresource
, pLayout
);
352 case VK_IMAGE_ASPECT_STENCIL_BIT
:
353 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
354 pSubresource
, pLayout
);
357 assert(!"Invalid image aspect");
362 anv_validate_CreateImageView(VkDevice _device
,
363 const VkImageViewCreateInfo
*pCreateInfo
,
364 const VkAllocationCallbacks
*pAllocator
,
367 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
368 const VkImageSubresourceRange
*subresource
;
369 const struct anv_image_view_info
*view_info
;
370 const struct anv_format
*view_format_info
;
372 /* Validate structure type before dereferencing it. */
374 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
375 subresource
= &pCreateInfo
->subresourceRange
;
377 /* Validate viewType is in range before using it. */
378 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
379 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
380 view_info
= &anv_image_view_info_table
[pCreateInfo
->viewType
];
382 /* Validate format is in range before using it. */
383 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
384 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
385 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
387 /* Validate channel swizzles. */
388 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
389 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
390 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
391 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
392 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
393 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
394 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
395 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
397 /* Validate subresource. */
398 assert(subresource
->aspectMask
!= 0);
399 assert(subresource
->levelCount
> 0);
400 assert(subresource
->layerCount
> 0);
401 assert(subresource
->baseMipLevel
< image
->levels
);
402 assert(subresource
->baseMipLevel
+ subresource
->levelCount
<= image
->levels
);
403 assert(subresource
->baseArrayLayer
< image
->array_size
);
404 assert(subresource
->baseArrayLayer
+ subresource
->layerCount
<= image
->array_size
);
407 if (view_info
->is_cube
) {
408 assert(subresource
->baseArrayLayer
% 6 == 0);
409 assert(subresource
->layerCount
% 6 == 0);
412 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
413 | VK_IMAGE_ASPECT_STENCIL_BIT
;
415 /* Validate format. */
416 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
417 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
418 assert(!image
->format
->depth_format
);
419 assert(!image
->format
->has_stencil
);
420 assert(!view_format_info
->depth_format
);
421 assert(!view_format_info
->has_stencil
);
422 assert(view_format_info
->isl_layout
->bs
==
423 image
->format
->isl_layout
->bs
);
424 } else if (subresource
->aspectMask
& ds_flags
) {
425 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
427 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
428 assert(image
->format
->depth_format
);
429 assert(view_format_info
->depth_format
);
430 assert(view_format_info
->isl_layout
->bs
==
431 image
->format
->isl_layout
->bs
);
434 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
435 /* FINISHME: Is it legal to have an R8 view of S8? */
436 assert(image
->format
->has_stencil
);
437 assert(view_format_info
->has_stencil
);
440 assert(!"bad VkImageSubresourceRange::aspectFlags");
443 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
447 anv_image_view_init(struct anv_image_view
*iview
,
448 struct anv_device
*device
,
449 const VkImageViewCreateInfo
* pCreateInfo
,
450 struct anv_cmd_buffer
*cmd_buffer
)
452 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
453 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
455 assert(range
->layerCount
> 0);
456 assert(range
->baseMipLevel
< image
->levels
);
457 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
458 VK_IMAGE_USAGE_STORAGE_BIT
|
459 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
460 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
462 switch (image
->type
) {
464 unreachable("bad VkImageType");
465 case VK_IMAGE_TYPE_1D
:
466 case VK_IMAGE_TYPE_2D
:
467 assert(range
->baseArrayLayer
+ range
->layerCount
- 1 <= image
->array_size
);
469 case VK_IMAGE_TYPE_3D
:
470 assert(range
->baseArrayLayer
+ range
->layerCount
- 1
471 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
475 switch (device
->info
.gen
) {
477 if (device
->info
.is_haswell
)
478 gen75_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
480 gen7_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
483 gen8_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
486 gen9_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
489 unreachable("unsupported gen\n");
494 anv_CreateImageView(VkDevice _device
,
495 const VkImageViewCreateInfo
*pCreateInfo
,
496 const VkAllocationCallbacks
*pAllocator
,
499 ANV_FROM_HANDLE(anv_device
, device
, _device
);
500 struct anv_image_view
*view
;
502 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
503 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
505 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
507 anv_image_view_init(view
, device
, pCreateInfo
, NULL
);
509 *pView
= anv_image_view_to_handle(view
);
515 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
516 const VkAllocationCallbacks
*pAllocator
)
518 ANV_FROM_HANDLE(anv_device
, device
, _device
);
519 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
521 if (iview
->image
->needs_color_rt_surface_state
) {
522 anv_state_pool_free(&device
->surface_state_pool
,
523 iview
->color_rt_surface_state
);
526 if (iview
->image
->needs_nonrt_surface_state
) {
527 anv_state_pool_free(&device
->surface_state_pool
,
528 iview
->nonrt_surface_state
);
531 anv_free2(&device
->alloc
, pAllocator
, iview
);
535 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
537 switch (aspect_mask
) {
538 case VK_IMAGE_ASPECT_COLOR_BIT
:
539 /* Dragons will eat you.
541 * Meta attaches all destination surfaces as color render targets. Guess
542 * what surface the Meta Dragons really want.
544 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
545 anv_finishme("combined depth stencil formats");
546 return &image
->depth_surface
;
547 } else if (image
->format
->depth_format
) {
548 return &image
->depth_surface
;
549 } else if (image
->format
->has_stencil
) {
550 return &image
->stencil_surface
;
552 return &image
->color_surface
;
555 case VK_IMAGE_ASPECT_DEPTH_BIT
:
556 assert(image
->format
->depth_format
);
557 return &image
->depth_surface
;
558 case VK_IMAGE_ASPECT_STENCIL_BIT
:
559 assert(image
->format
->has_stencil
);
560 return &image
->stencil_surface
;
561 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
562 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
563 /* FINISHME: The Vulkan spec (git a511ba2) requires support for combined
564 * depth stencil formats. Specifically, it states:
566 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
567 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
569 anv_finishme("combined depthstencil aspect");
570 return &image
->depth_surface
;
571 } else if (image
->format
->depth_format
) {
572 return &image
->depth_surface
;
573 } else if (image
->format
->has_stencil
) {
574 return &image
->stencil_surface
;
578 unreachable("image does not have aspect");