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"
38 * The \a format argument is required and overrides any format found in struct
39 * anv_image_create_info. Exactly one bit must be set in \a aspect.
41 static isl_surf_usage_flags_t
42 choose_isl_surf_usage(const struct anv_image_create_info
*info
,
43 VkImageAspectFlags aspect
)
45 const VkImageCreateInfo
*vk_info
= info
->vk_info
;
46 isl_surf_usage_flags_t isl_flags
= 0;
48 /* FINISHME: Support aux surfaces */
49 isl_flags
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
51 if (vk_info
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
52 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
54 if (vk_info
->usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
55 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
57 if (vk_info
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
58 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
60 if (vk_info
->flags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
61 isl_flags
|= ISL_SURF_USAGE_CUBE_BIT
;
63 if (vk_info
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
66 unreachable("bad VkImageAspect");
67 case VK_IMAGE_ASPECT_DEPTH_BIT
:
68 isl_flags
|= ISL_SURF_USAGE_DEPTH_BIT
;
70 case VK_IMAGE_ASPECT_STENCIL_BIT
:
71 isl_flags
|= ISL_SURF_USAGE_STENCIL_BIT
;
76 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
77 /* Meta implements transfers by sampling from the source image. */
78 isl_flags
|= ISL_SURF_USAGE_TEXTURE_BIT
;
81 if (vk_info
->usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
82 /* Meta implements transfers by rendering into the destination image. */
83 isl_flags
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
90 * Exactly one bit must be set in \a aspect.
92 static struct anv_surface
*
93 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
97 unreachable("bad VkImageAspect");
98 case VK_IMAGE_ASPECT_COLOR_BIT
:
99 return &image
->color_surface
;
100 case VK_IMAGE_ASPECT_DEPTH_BIT
:
101 return &image
->depth_surface
;
102 case VK_IMAGE_ASPECT_STENCIL_BIT
:
103 return &image
->stencil_surface
;
108 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
109 * image's memory requirements (that is, the image's size and alignment).
111 * Exactly one bit must be set in \a aspect.
114 make_surface(const struct anv_device
*dev
,
115 struct anv_image
*image
,
116 const struct anv_image_create_info
*anv_info
,
117 VkImageAspectFlags aspect
)
119 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
122 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
123 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
124 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
125 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
128 isl_tiling_flags_t tiling_flags
= anv_info
->isl_tiling_flags
;
129 if (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
)
130 tiling_flags
&= ISL_TILING_LINEAR_BIT
;
132 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
134 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
135 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
136 .format
= anv_get_isl_format(vk_info
->format
, aspect
),
137 .width
= vk_info
->extent
.width
,
138 .height
= vk_info
->extent
.height
,
139 .depth
= vk_info
->extent
.depth
,
140 .levels
= vk_info
->mipLevels
,
141 .array_len
= vk_info
->arrayLayers
,
142 .samples
= vk_info
->samples
,
145 .usage
= choose_isl_surf_usage(anv_info
, aspect
),
146 .tiling_flags
= tiling_flags
);
148 /* isl_surf_init() will fail only if provided invalid input. Invalid input
149 * is illegal in Vulkan.
153 anv_surf
->offset
= align_u32(image
->size
, anv_surf
->isl
.alignment
);
154 image
->size
= anv_surf
->offset
+ anv_surf
->isl
.size
;
155 image
->alignment
= MAX(image
->alignment
, anv_surf
->isl
.alignment
);
160 static VkImageUsageFlags
161 anv_image_get_full_usage(const VkImageCreateInfo
*info
)
163 VkImageUsageFlags usage
= info
->usage
;
165 if (usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
166 /* Meta will transfer from the image by binding it as a texture. */
167 usage
|= VK_IMAGE_USAGE_SAMPLED_BIT
;
170 if (usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
171 /* Meta will transfer to the image by binding it as a color attachment,
172 * even if the image format is not a color format.
174 usage
|= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
181 anv_image_create(VkDevice _device
,
182 const struct anv_image_create_info
*create_info
,
183 const VkAllocationCallbacks
* alloc
,
186 ANV_FROM_HANDLE(anv_device
, device
, _device
);
187 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
188 struct anv_image
*image
= NULL
;
191 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
193 anv_assert(pCreateInfo
->mipLevels
> 0);
194 anv_assert(pCreateInfo
->arrayLayers
> 0);
195 anv_assert(pCreateInfo
->samples
== VK_SAMPLE_COUNT_1_BIT
);
196 anv_assert(pCreateInfo
->extent
.width
> 0);
197 anv_assert(pCreateInfo
->extent
.height
> 0);
198 anv_assert(pCreateInfo
->extent
.depth
> 0);
200 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
201 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
203 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
205 memset(image
, 0, sizeof(*image
));
206 image
->type
= pCreateInfo
->imageType
;
207 image
->extent
= pCreateInfo
->extent
;
208 image
->format
= anv_format_for_vk_format(pCreateInfo
->format
);
209 image
->levels
= pCreateInfo
->mipLevels
;
210 image
->array_size
= pCreateInfo
->arrayLayers
;
211 image
->usage
= anv_image_get_full_usage(pCreateInfo
);
213 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
214 image
->needs_nonrt_surface_state
= true;
217 if (image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
218 image
->needs_color_rt_surface_state
= true;
221 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
222 image
->needs_storage_surface_state
= true;
225 if (likely(anv_format_is_color(image
->format
))) {
226 r
= make_surface(device
, image
, create_info
,
227 VK_IMAGE_ASPECT_COLOR_BIT
);
231 if (image
->format
->depth_format
) {
232 r
= make_surface(device
, image
, create_info
,
233 VK_IMAGE_ASPECT_DEPTH_BIT
);
238 if (image
->format
->has_stencil
) {
239 r
= make_surface(device
, image
, create_info
,
240 VK_IMAGE_ASPECT_STENCIL_BIT
);
246 *pImage
= anv_image_to_handle(image
);
252 anv_free2(&device
->alloc
, alloc
, image
);
258 anv_CreateImage(VkDevice device
,
259 const VkImageCreateInfo
*pCreateInfo
,
260 const VkAllocationCallbacks
*pAllocator
,
263 return anv_image_create(device
,
264 &(struct anv_image_create_info
) {
265 .vk_info
= pCreateInfo
,
266 .isl_tiling_flags
= ISL_TILING_ANY_MASK
,
273 anv_DestroyImage(VkDevice _device
, VkImage _image
,
274 const VkAllocationCallbacks
*pAllocator
)
276 ANV_FROM_HANDLE(anv_device
, device
, _device
);
278 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
282 anv_surface_get_subresource_layout(struct anv_image
*image
,
283 struct anv_surface
*surface
,
284 const VkImageSubresource
*subresource
,
285 VkSubresourceLayout
*layout
)
287 /* If we are on a non-zero mip level or array slice, we need to
288 * calculate a real offset.
290 anv_assert(subresource
->mipLevel
== 0);
291 anv_assert(subresource
->arrayLayer
== 0);
293 layout
->offset
= surface
->offset
;
294 layout
->rowPitch
= surface
->isl
.row_pitch
;
295 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
296 layout
->size
= surface
->isl
.size
;
299 void anv_GetImageSubresourceLayout(
302 const VkImageSubresource
* pSubresource
,
303 VkSubresourceLayout
* pLayout
)
305 ANV_FROM_HANDLE(anv_image
, image
, _image
);
307 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
309 switch (pSubresource
->aspectMask
) {
310 case VK_IMAGE_ASPECT_COLOR_BIT
:
311 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
312 pSubresource
, pLayout
);
314 case VK_IMAGE_ASPECT_DEPTH_BIT
:
315 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
316 pSubresource
, pLayout
);
318 case VK_IMAGE_ASPECT_STENCIL_BIT
:
319 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
320 pSubresource
, pLayout
);
323 assert(!"Invalid image aspect");
328 anv_validate_CreateImageView(VkDevice _device
,
329 const VkImageViewCreateInfo
*pCreateInfo
,
330 const VkAllocationCallbacks
*pAllocator
,
333 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
334 const VkImageSubresourceRange
*subresource
;
335 const struct anv_format
*view_format_info
;
337 /* Validate structure type before dereferencing it. */
339 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
);
340 subresource
= &pCreateInfo
->subresourceRange
;
342 /* Validate viewType is in range before using it. */
343 assert(pCreateInfo
->viewType
>= VK_IMAGE_VIEW_TYPE_BEGIN_RANGE
);
344 assert(pCreateInfo
->viewType
<= VK_IMAGE_VIEW_TYPE_END_RANGE
);
346 /* Validate format is in range before using it. */
347 assert(pCreateInfo
->format
>= VK_FORMAT_BEGIN_RANGE
);
348 assert(pCreateInfo
->format
<= VK_FORMAT_END_RANGE
);
349 view_format_info
= anv_format_for_vk_format(pCreateInfo
->format
);
351 /* Validate channel swizzles. */
352 assert(pCreateInfo
->components
.r
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
353 assert(pCreateInfo
->components
.r
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
354 assert(pCreateInfo
->components
.g
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
355 assert(pCreateInfo
->components
.g
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
356 assert(pCreateInfo
->components
.b
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
357 assert(pCreateInfo
->components
.b
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
358 assert(pCreateInfo
->components
.a
>= VK_COMPONENT_SWIZZLE_BEGIN_RANGE
);
359 assert(pCreateInfo
->components
.a
<= VK_COMPONENT_SWIZZLE_END_RANGE
);
361 /* Validate subresource. */
362 assert(subresource
->aspectMask
!= 0);
363 assert(subresource
->levelCount
> 0);
364 assert(subresource
->layerCount
> 0);
365 assert(subresource
->baseMipLevel
< image
->levels
);
366 assert(subresource
->baseMipLevel
+ subresource
->levelCount
<= image
->levels
);
367 assert(subresource
->baseArrayLayer
< image
->array_size
);
368 assert(subresource
->baseArrayLayer
+ subresource
->layerCount
<= image
->array_size
);
371 const VkImageAspectFlags ds_flags
= VK_IMAGE_ASPECT_DEPTH_BIT
372 | VK_IMAGE_ASPECT_STENCIL_BIT
;
374 /* Validate format. */
375 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_COLOR_BIT
) {
376 assert(subresource
->aspectMask
== VK_IMAGE_ASPECT_COLOR_BIT
);
377 assert(!image
->format
->depth_format
);
378 assert(!image
->format
->has_stencil
);
379 assert(!view_format_info
->depth_format
);
380 assert(!view_format_info
->has_stencil
);
381 assert(view_format_info
->isl_layout
->bs
==
382 image
->format
->isl_layout
->bs
);
383 } else if (subresource
->aspectMask
& ds_flags
) {
384 assert((subresource
->aspectMask
& ~ds_flags
) == 0);
386 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
387 assert(image
->format
->depth_format
);
388 assert(view_format_info
->depth_format
);
389 assert(view_format_info
->isl_layout
->bs
==
390 image
->format
->isl_layout
->bs
);
393 if (subresource
->aspectMask
& VK_IMAGE_ASPECT_STENCIL_BIT
) {
394 /* FINISHME: Is it legal to have an R8 view of S8? */
395 assert(image
->format
->has_stencil
);
396 assert(view_format_info
->has_stencil
);
399 assert(!"bad VkImageSubresourceRange::aspectFlags");
402 return anv_CreateImageView(_device
, pCreateInfo
, pAllocator
, pView
);
406 anv_image_view_init(struct anv_image_view
*iview
,
407 struct anv_device
*device
,
408 const VkImageViewCreateInfo
* pCreateInfo
,
409 struct anv_cmd_buffer
*cmd_buffer
)
411 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
412 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
414 assert(range
->layerCount
> 0);
415 assert(range
->baseMipLevel
< image
->levels
);
416 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
417 VK_IMAGE_USAGE_STORAGE_BIT
|
418 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
419 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
421 switch (image
->type
) {
423 unreachable("bad VkImageType");
424 case VK_IMAGE_TYPE_1D
:
425 case VK_IMAGE_TYPE_2D
:
426 assert(range
->baseArrayLayer
+ range
->layerCount
- 1 <= image
->array_size
);
428 case VK_IMAGE_TYPE_3D
:
429 assert(range
->baseArrayLayer
+ range
->layerCount
- 1
430 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
434 switch (device
->info
.gen
) {
436 if (device
->info
.is_haswell
)
437 gen75_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
439 gen7_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
442 gen8_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
445 gen9_image_view_init(iview
, device
, pCreateInfo
, cmd_buffer
);
448 unreachable("unsupported gen\n");
453 anv_CreateImageView(VkDevice _device
,
454 const VkImageViewCreateInfo
*pCreateInfo
,
455 const VkAllocationCallbacks
*pAllocator
,
458 ANV_FROM_HANDLE(anv_device
, device
, _device
);
459 struct anv_image_view
*view
;
461 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
462 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
464 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
466 anv_image_view_init(view
, device
, pCreateInfo
, NULL
);
468 *pView
= anv_image_view_to_handle(view
);
474 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
475 const VkAllocationCallbacks
*pAllocator
)
477 ANV_FROM_HANDLE(anv_device
, device
, _device
);
478 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
480 if (iview
->image
->needs_color_rt_surface_state
) {
481 anv_state_pool_free(&device
->surface_state_pool
,
482 iview
->color_rt_surface_state
);
485 if (iview
->image
->needs_nonrt_surface_state
) {
486 anv_state_pool_free(&device
->surface_state_pool
,
487 iview
->nonrt_surface_state
);
490 if (iview
->image
->needs_storage_surface_state
) {
491 anv_state_pool_free(&device
->surface_state_pool
,
492 iview
->storage_surface_state
);
495 anv_free2(&device
->alloc
, pAllocator
, iview
);
499 anv_CreateBufferView(VkDevice _device
,
500 const VkBufferViewCreateInfo
*pCreateInfo
,
501 const VkAllocationCallbacks
*pAllocator
,
504 ANV_FROM_HANDLE(anv_device
, device
, _device
);
505 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
506 struct anv_buffer_view
*view
;
508 /* TODO: Storage texel buffers */
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 view
->bo
= buffer
->bo
;
516 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
518 view
->surface_state
=
519 anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
521 const struct anv_format
*format
=
522 anv_format_for_vk_format(pCreateInfo
->format
);
524 anv_fill_buffer_surface_state(device
, view
->surface_state
.map
, format
,
525 view
->offset
, pCreateInfo
->range
,
526 format
->isl_layout
->bpb
/ 8);
528 *pView
= anv_buffer_view_to_handle(view
);
534 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
535 const VkAllocationCallbacks
*pAllocator
)
537 ANV_FROM_HANDLE(anv_device
, device
, _device
);
538 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
540 anv_state_pool_free(&device
->surface_state_pool
, view
->surface_state
);
541 anv_free2(&device
->alloc
, pAllocator
, view
);
545 anv_image_get_surface_for_aspect_mask(struct anv_image
*image
, VkImageAspectFlags aspect_mask
)
547 switch (aspect_mask
) {
548 case VK_IMAGE_ASPECT_COLOR_BIT
:
549 /* Dragons will eat you.
551 * Meta attaches all destination surfaces as color render targets. Guess
552 * what surface the Meta Dragons really want.
554 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
555 anv_finishme("combined depth stencil formats");
556 return &image
->depth_surface
;
557 } else if (image
->format
->depth_format
) {
558 return &image
->depth_surface
;
559 } else if (image
->format
->has_stencil
) {
560 return &image
->stencil_surface
;
562 return &image
->color_surface
;
565 case VK_IMAGE_ASPECT_DEPTH_BIT
:
566 assert(image
->format
->depth_format
);
567 return &image
->depth_surface
;
568 case VK_IMAGE_ASPECT_STENCIL_BIT
:
569 assert(image
->format
->has_stencil
);
570 return &image
->stencil_surface
;
571 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
572 if (image
->format
->depth_format
&& image
->format
->has_stencil
) {
573 /* FINISHME: The Vulkan spec (git a511ba2) requires support for combined
574 * depth stencil formats. Specifically, it states:
576 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
577 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
579 anv_finishme("combined depthstencil aspect");
580 return &image
->depth_surface
;
581 } else if (image
->format
->depth_format
) {
582 return &image
->depth_surface
;
583 } else if (image
->format
->has_stencil
) {
584 return &image
->stencil_surface
;
588 unreachable("image does not have aspect");
594 anv_image_view_fill_image_param(struct anv_device
*device
,
595 struct anv_image_view
*view
,
596 struct brw_image_param
*param
)
598 memset(param
, 0, sizeof *param
);
599 anv_finishme("Actually fill out brw_image_param");