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"
31 #include "util/debug.h"
33 #include "vk_format_info.h"
36 * Exactly one bit must be set in \a aspect.
38 static isl_surf_usage_flags_t
39 choose_isl_surf_usage(VkImageUsageFlags vk_usage
,
40 VkImageAspectFlags aspect
)
42 isl_surf_usage_flags_t isl_usage
= 0;
44 /* FINISHME: Support aux surfaces */
45 isl_usage
|= ISL_SURF_USAGE_DISABLE_AUX_BIT
;
47 if (vk_usage
& VK_IMAGE_USAGE_SAMPLED_BIT
)
48 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
50 if (vk_usage
& VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
)
51 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
53 if (vk_usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
)
54 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
56 if (vk_usage
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
)
57 isl_usage
|= ISL_SURF_USAGE_CUBE_BIT
;
59 /* Even if we're only using it for transfer operations, clears to depth and
60 * stencil images happen as depth and stencil so they need the right ISL
61 * usage bits or else things will fall apart.
64 case VK_IMAGE_ASPECT_DEPTH_BIT
:
65 isl_usage
&= ~ISL_SURF_USAGE_DISABLE_AUX_BIT
;
66 isl_usage
|= ISL_SURF_USAGE_DEPTH_BIT
;
68 case VK_IMAGE_ASPECT_STENCIL_BIT
:
69 isl_usage
|= ISL_SURF_USAGE_STENCIL_BIT
;
71 case VK_IMAGE_ASPECT_COLOR_BIT
:
74 unreachable("bad VkImageAspect");
77 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_SRC_BIT
) {
78 /* blorp implements transfers by sampling from the source image. */
79 isl_usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
82 if (vk_usage
& VK_IMAGE_USAGE_TRANSFER_DST_BIT
) {
83 /* blorp implements transfers by rendering into the destination image. */
84 isl_usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
91 * Exactly one bit must be set in \a aspect.
93 static struct anv_surface
*
94 get_surface(struct anv_image
*image
, VkImageAspectFlags aspect
)
98 unreachable("bad VkImageAspect");
99 case VK_IMAGE_ASPECT_COLOR_BIT
:
100 return &image
->color_surface
;
101 case VK_IMAGE_ASPECT_DEPTH_BIT
:
102 return &image
->depth_surface
;
103 case VK_IMAGE_ASPECT_STENCIL_BIT
:
104 return &image
->stencil_surface
;
109 add_surface(struct anv_image
*image
, struct anv_surface
*surf
)
111 assert(surf
->isl
.size
> 0); /* isl surface must be initialized */
113 surf
->offset
= align_u32(image
->size
, surf
->isl
.alignment
);
114 image
->size
= surf
->offset
+ surf
->isl
.size
;
115 image
->alignment
= MAX(image
->alignment
, surf
->isl
.alignment
);
119 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
120 * image's memory requirements (that is, the image's size and alignment).
122 * Exactly one bit must be set in \a aspect.
125 make_surface(const struct anv_device
*dev
,
126 struct anv_image
*image
,
127 const struct anv_image_create_info
*anv_info
,
128 VkImageAspectFlags aspect
)
130 const VkImageCreateInfo
*vk_info
= anv_info
->vk_info
;
133 static const enum isl_surf_dim vk_to_isl_surf_dim
[] = {
134 [VK_IMAGE_TYPE_1D
] = ISL_SURF_DIM_1D
,
135 [VK_IMAGE_TYPE_2D
] = ISL_SURF_DIM_2D
,
136 [VK_IMAGE_TYPE_3D
] = ISL_SURF_DIM_3D
,
139 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
140 * result with an optionally provided ISL tiling argument.
142 isl_tiling_flags_t tiling_flags
=
143 (vk_info
->tiling
== VK_IMAGE_TILING_LINEAR
) ?
144 ISL_TILING_LINEAR_BIT
: ISL_TILING_ANY_MASK
;
146 if (anv_info
->isl_tiling_flags
)
147 tiling_flags
&= anv_info
->isl_tiling_flags
;
149 assert(tiling_flags
);
151 struct anv_surface
*anv_surf
= get_surface(image
, aspect
);
153 image
->extent
= anv_sanitize_image_extent(vk_info
->imageType
,
156 enum isl_format format
= anv_get_isl_format(&dev
->info
, vk_info
->format
,
157 aspect
, vk_info
->tiling
);
158 assert(format
!= ISL_FORMAT_UNSUPPORTED
);
160 ok
= isl_surf_init(&dev
->isl_dev
, &anv_surf
->isl
,
161 .dim
= vk_to_isl_surf_dim
[vk_info
->imageType
],
163 .width
= image
->extent
.width
,
164 .height
= image
->extent
.height
,
165 .depth
= image
->extent
.depth
,
166 .levels
= vk_info
->mipLevels
,
167 .array_len
= vk_info
->arrayLayers
,
168 .samples
= vk_info
->samples
,
170 .min_pitch
= anv_info
->stride
,
171 .usage
= choose_isl_surf_usage(image
->usage
, aspect
),
172 .tiling_flags
= tiling_flags
);
174 /* isl_surf_init() will fail only if provided invalid input. Invalid input
175 * is illegal in Vulkan.
179 add_surface(image
, anv_surf
);
181 /* Add a HiZ surface to a depth buffer that will be used for rendering.
183 if (aspect
== VK_IMAGE_ASPECT_DEPTH_BIT
&&
184 (image
->usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
186 /* Allow the user to control HiZ enabling. Disable by default on gen7
187 * because resolves are not currently implemented pre-BDW.
189 if (!env_var_as_boolean("INTEL_VK_HIZ", dev
->info
.gen
>= 8)) {
190 anv_finishme("Implement gen7 HiZ");
191 } else if (vk_info
->mipLevels
> 1) {
192 anv_finishme("Test multi-LOD HiZ");
193 } else if (dev
->info
.gen
== 8 && vk_info
->samples
> 1) {
194 anv_finishme("Test gen8 multisampled HiZ");
196 isl_surf_get_hiz_surf(&dev
->isl_dev
, &image
->depth_surface
.isl
,
197 &image
->hiz_surface
.isl
);
198 add_surface(image
, &image
->hiz_surface
);
206 anv_image_create(VkDevice _device
,
207 const struct anv_image_create_info
*create_info
,
208 const VkAllocationCallbacks
* alloc
,
211 ANV_FROM_HANDLE(anv_device
, device
, _device
);
212 const VkImageCreateInfo
*pCreateInfo
= create_info
->vk_info
;
213 struct anv_image
*image
= NULL
;
216 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
);
218 anv_assert(pCreateInfo
->mipLevels
> 0);
219 anv_assert(pCreateInfo
->arrayLayers
> 0);
220 anv_assert(pCreateInfo
->samples
> 0);
221 anv_assert(pCreateInfo
->extent
.width
> 0);
222 anv_assert(pCreateInfo
->extent
.height
> 0);
223 anv_assert(pCreateInfo
->extent
.depth
> 0);
225 image
= anv_alloc2(&device
->alloc
, alloc
, sizeof(*image
), 8,
226 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
228 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
230 memset(image
, 0, sizeof(*image
));
231 image
->type
= pCreateInfo
->imageType
;
232 image
->extent
= pCreateInfo
->extent
;
233 image
->vk_format
= pCreateInfo
->format
;
234 image
->aspects
= vk_format_aspects(image
->vk_format
);
235 image
->levels
= pCreateInfo
->mipLevels
;
236 image
->array_size
= pCreateInfo
->arrayLayers
;
237 image
->samples
= pCreateInfo
->samples
;
238 image
->usage
= pCreateInfo
->usage
;
239 image
->tiling
= pCreateInfo
->tiling
;
242 for_each_bit(b
, image
->aspects
) {
243 r
= make_surface(device
, image
, create_info
, (1 << b
));
248 *pImage
= anv_image_to_handle(image
);
254 anv_free2(&device
->alloc
, alloc
, image
);
260 anv_CreateImage(VkDevice device
,
261 const VkImageCreateInfo
*pCreateInfo
,
262 const VkAllocationCallbacks
*pAllocator
,
265 return anv_image_create(device
,
266 &(struct anv_image_create_info
) {
267 .vk_info
= pCreateInfo
,
274 anv_DestroyImage(VkDevice _device
, VkImage _image
,
275 const VkAllocationCallbacks
*pAllocator
)
277 ANV_FROM_HANDLE(anv_device
, device
, _device
);
279 anv_free2(&device
->alloc
, pAllocator
, anv_image_from_handle(_image
));
282 VkResult
anv_BindImageMemory(
285 VkDeviceMemory _memory
,
286 VkDeviceSize memoryOffset
)
288 ANV_FROM_HANDLE(anv_device
, device
, _device
);
289 ANV_FROM_HANDLE(anv_device_memory
, mem
, _memory
);
290 ANV_FROM_HANDLE(anv_image
, image
, _image
);
293 image
->bo
= &mem
->bo
;
294 image
->offset
= memoryOffset
;
300 if (anv_image_has_hiz(image
)) {
302 /* The offset and size must be a multiple of 4K or else the
303 * anv_gem_mmap call below will return NULL.
305 assert((image
->offset
+ image
->hiz_surface
.offset
) % 4096 == 0);
306 assert(image
->hiz_surface
.isl
.size
% 4096 == 0);
308 /* HiZ surfaces need to have their memory cleared to 0 before they
309 * can be used. If we let it have garbage data, it can cause GPU
310 * hangs on some hardware.
312 void *map
= anv_gem_mmap(device
, image
->bo
->gem_handle
,
313 image
->offset
+ image
->hiz_surface
.offset
,
314 image
->hiz_surface
.isl
.size
,
315 device
->info
.has_llc
? 0 : I915_MMAP_WC
);
317 /* If anv_gem_mmap returns NULL, it's likely that the kernel was
318 * not able to find space on the host to create a proper mapping.
321 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
323 memset(map
, 0, image
->hiz_surface
.isl
.size
);
325 anv_gem_munmap(map
, image
->hiz_surface
.isl
.size
);
332 anv_surface_get_subresource_layout(struct anv_image
*image
,
333 struct anv_surface
*surface
,
334 const VkImageSubresource
*subresource
,
335 VkSubresourceLayout
*layout
)
337 /* If we are on a non-zero mip level or array slice, we need to
338 * calculate a real offset.
340 anv_assert(subresource
->mipLevel
== 0);
341 anv_assert(subresource
->arrayLayer
== 0);
343 layout
->offset
= surface
->offset
;
344 layout
->rowPitch
= surface
->isl
.row_pitch
;
345 layout
->depthPitch
= isl_surf_get_array_pitch(&surface
->isl
);
346 layout
->arrayPitch
= isl_surf_get_array_pitch(&surface
->isl
);
347 layout
->size
= surface
->isl
.size
;
350 void anv_GetImageSubresourceLayout(
353 const VkImageSubresource
* pSubresource
,
354 VkSubresourceLayout
* pLayout
)
356 ANV_FROM_HANDLE(anv_image
, image
, _image
);
358 assert(__builtin_popcount(pSubresource
->aspectMask
) == 1);
360 switch (pSubresource
->aspectMask
) {
361 case VK_IMAGE_ASPECT_COLOR_BIT
:
362 anv_surface_get_subresource_layout(image
, &image
->color_surface
,
363 pSubresource
, pLayout
);
365 case VK_IMAGE_ASPECT_DEPTH_BIT
:
366 anv_surface_get_subresource_layout(image
, &image
->depth_surface
,
367 pSubresource
, pLayout
);
369 case VK_IMAGE_ASPECT_STENCIL_BIT
:
370 anv_surface_get_subresource_layout(image
, &image
->stencil_surface
,
371 pSubresource
, pLayout
);
374 assert(!"Invalid image aspect");
378 static struct anv_state
379 alloc_surface_state(struct anv_device
*device
,
380 struct anv_cmd_buffer
*cmd_buffer
)
383 return anv_cmd_buffer_alloc_surface_state(cmd_buffer
);
385 return anv_state_pool_alloc(&device
->surface_state_pool
, 64, 64);
389 static enum isl_channel_select
390 remap_swizzle(VkComponentSwizzle swizzle
, VkComponentSwizzle component
,
391 struct isl_swizzle format_swizzle
)
393 if (swizzle
== VK_COMPONENT_SWIZZLE_IDENTITY
)
397 case VK_COMPONENT_SWIZZLE_ZERO
: return ISL_CHANNEL_SELECT_ZERO
;
398 case VK_COMPONENT_SWIZZLE_ONE
: return ISL_CHANNEL_SELECT_ONE
;
399 case VK_COMPONENT_SWIZZLE_R
: return format_swizzle
.r
;
400 case VK_COMPONENT_SWIZZLE_G
: return format_swizzle
.g
;
401 case VK_COMPONENT_SWIZZLE_B
: return format_swizzle
.b
;
402 case VK_COMPONENT_SWIZZLE_A
: return format_swizzle
.a
;
404 unreachable("Invalid swizzle");
409 anv_image_view_init(struct anv_image_view
*iview
,
410 struct anv_device
*device
,
411 const VkImageViewCreateInfo
* pCreateInfo
,
412 struct anv_cmd_buffer
*cmd_buffer
)
414 ANV_FROM_HANDLE(anv_image
, image
, pCreateInfo
->image
);
415 const VkImageSubresourceRange
*range
= &pCreateInfo
->subresourceRange
;
417 assert(range
->layerCount
> 0);
418 assert(range
->baseMipLevel
< image
->levels
);
419 assert(image
->usage
& (VK_IMAGE_USAGE_SAMPLED_BIT
|
420 VK_IMAGE_USAGE_STORAGE_BIT
|
421 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
|
422 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
));
424 switch (image
->type
) {
426 unreachable("bad VkImageType");
427 case VK_IMAGE_TYPE_1D
:
428 case VK_IMAGE_TYPE_2D
:
429 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1 <= image
->array_size
);
431 case VK_IMAGE_TYPE_3D
:
432 assert(range
->baseArrayLayer
+ anv_get_layerCount(image
, range
) - 1
433 <= anv_minify(image
->extent
.depth
, range
->baseMipLevel
));
437 const struct anv_surface
*surface
=
438 anv_image_get_surface_for_aspect_mask(image
, range
->aspectMask
);
440 iview
->image
= image
;
441 iview
->bo
= image
->bo
;
442 iview
->offset
= image
->offset
+ surface
->offset
;
444 iview
->aspect_mask
= pCreateInfo
->subresourceRange
.aspectMask
;
445 iview
->vk_format
= pCreateInfo
->format
;
447 struct anv_format format
= anv_get_format(&device
->info
, pCreateInfo
->format
,
448 range
->aspectMask
, image
->tiling
);
450 iview
->isl
= (struct isl_view
) {
451 .format
= format
.isl_format
,
452 .base_level
= range
->baseMipLevel
,
453 .levels
= anv_get_levelCount(image
, range
),
454 .base_array_layer
= range
->baseArrayLayer
,
455 .array_len
= anv_get_layerCount(image
, range
),
457 .r
= remap_swizzle(pCreateInfo
->components
.r
,
458 VK_COMPONENT_SWIZZLE_R
, format
.swizzle
),
459 .g
= remap_swizzle(pCreateInfo
->components
.g
,
460 VK_COMPONENT_SWIZZLE_G
, format
.swizzle
),
461 .b
= remap_swizzle(pCreateInfo
->components
.b
,
462 VK_COMPONENT_SWIZZLE_B
, format
.swizzle
),
463 .a
= remap_swizzle(pCreateInfo
->components
.a
,
464 VK_COMPONENT_SWIZZLE_A
, format
.swizzle
),
468 iview
->extent
= (VkExtent3D
) {
469 .width
= anv_minify(image
->extent
.width
, range
->baseMipLevel
),
470 .height
= anv_minify(image
->extent
.height
, range
->baseMipLevel
),
471 .depth
= anv_minify(image
->extent
.depth
, range
->baseMipLevel
),
474 if (image
->type
== VK_IMAGE_TYPE_3D
) {
475 iview
->isl
.base_array_layer
= 0;
476 iview
->isl
.array_len
= iview
->extent
.depth
;
479 if (pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE
||
480 pCreateInfo
->viewType
== VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
) {
481 iview
->isl
.usage
= ISL_SURF_USAGE_CUBE_BIT
;
483 iview
->isl
.usage
= 0;
486 if (image
->usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
487 iview
->sampler_surface_state
= alloc_surface_state(device
, cmd_buffer
);
489 struct isl_view view
= iview
->isl
;
490 view
.usage
|= ISL_SURF_USAGE_TEXTURE_BIT
;
491 isl_surf_fill_state(&device
->isl_dev
,
492 iview
->sampler_surface_state
.map
,
493 .surf
= &surface
->isl
,
495 .mocs
= device
->default_mocs
);
497 if (!device
->info
.has_llc
)
498 anv_state_clflush(iview
->sampler_surface_state
);
500 iview
->sampler_surface_state
.alloc_size
= 0;
503 if (image
->usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
504 iview
->color_rt_surface_state
= alloc_surface_state(device
, cmd_buffer
);
506 struct isl_view view
= iview
->isl
;
507 view
.usage
|= ISL_SURF_USAGE_RENDER_TARGET_BIT
;
508 isl_surf_fill_state(&device
->isl_dev
,
509 iview
->color_rt_surface_state
.map
,
510 .surf
= &surface
->isl
,
512 .mocs
= device
->default_mocs
);
514 if (!device
->info
.has_llc
)
515 anv_state_clflush(iview
->color_rt_surface_state
);
517 iview
->color_rt_surface_state
.alloc_size
= 0;
520 /* NOTE: This one needs to go last since it may stomp isl_view.format */
521 if (image
->usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
522 iview
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
524 if (isl_has_matching_typed_storage_image_format(&device
->info
,
525 format
.isl_format
)) {
526 struct isl_view view
= iview
->isl
;
527 view
.usage
|= ISL_SURF_USAGE_STORAGE_BIT
;
528 view
.format
= isl_lower_storage_image_format(&device
->info
,
530 isl_surf_fill_state(&device
->isl_dev
,
531 iview
->storage_surface_state
.map
,
532 .surf
= &surface
->isl
,
534 .mocs
= device
->default_mocs
);
536 anv_fill_buffer_surface_state(device
, iview
->storage_surface_state
,
539 iview
->bo
->size
- iview
->offset
, 1);
542 isl_surf_fill_image_param(&device
->isl_dev
,
543 &iview
->storage_image_param
,
544 &surface
->isl
, &iview
->isl
);
546 if (!device
->info
.has_llc
)
547 anv_state_clflush(iview
->storage_surface_state
);
549 iview
->storage_surface_state
.alloc_size
= 0;
554 anv_CreateImageView(VkDevice _device
,
555 const VkImageViewCreateInfo
*pCreateInfo
,
556 const VkAllocationCallbacks
*pAllocator
,
559 ANV_FROM_HANDLE(anv_device
, device
, _device
);
560 struct anv_image_view
*view
;
562 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
563 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
565 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
567 anv_image_view_init(view
, device
, pCreateInfo
, NULL
);
569 *pView
= anv_image_view_to_handle(view
);
575 anv_DestroyImageView(VkDevice _device
, VkImageView _iview
,
576 const VkAllocationCallbacks
*pAllocator
)
578 ANV_FROM_HANDLE(anv_device
, device
, _device
);
579 ANV_FROM_HANDLE(anv_image_view
, iview
, _iview
);
581 if (iview
->color_rt_surface_state
.alloc_size
> 0) {
582 anv_state_pool_free(&device
->surface_state_pool
,
583 iview
->color_rt_surface_state
);
586 if (iview
->sampler_surface_state
.alloc_size
> 0) {
587 anv_state_pool_free(&device
->surface_state_pool
,
588 iview
->sampler_surface_state
);
591 if (iview
->storage_surface_state
.alloc_size
> 0) {
592 anv_state_pool_free(&device
->surface_state_pool
,
593 iview
->storage_surface_state
);
596 anv_free2(&device
->alloc
, pAllocator
, iview
);
600 void anv_buffer_view_init(struct anv_buffer_view
*view
,
601 struct anv_device
*device
,
602 const VkBufferViewCreateInfo
* pCreateInfo
,
603 struct anv_cmd_buffer
*cmd_buffer
)
605 ANV_FROM_HANDLE(anv_buffer
, buffer
, pCreateInfo
->buffer
);
607 /* TODO: Handle the format swizzle? */
609 view
->format
= anv_get_isl_format(&device
->info
, pCreateInfo
->format
,
610 VK_IMAGE_ASPECT_COLOR_BIT
,
611 VK_IMAGE_TILING_LINEAR
);
612 const uint32_t format_bs
= isl_format_get_layout(view
->format
)->bpb
/ 8;
613 view
->bo
= buffer
->bo
;
614 view
->offset
= buffer
->offset
+ pCreateInfo
->offset
;
615 view
->range
= pCreateInfo
->range
== VK_WHOLE_SIZE
?
616 buffer
->size
- pCreateInfo
->offset
: pCreateInfo
->range
;
617 view
->range
= align_down_npot_u32(view
->range
, format_bs
);
619 if (buffer
->usage
& VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
) {
620 view
->surface_state
= alloc_surface_state(device
, cmd_buffer
);
622 anv_fill_buffer_surface_state(device
, view
->surface_state
,
624 view
->offset
, view
->range
, format_bs
);
626 view
->surface_state
= (struct anv_state
){ 0 };
629 if (buffer
->usage
& VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
) {
630 view
->storage_surface_state
= alloc_surface_state(device
, cmd_buffer
);
632 enum isl_format storage_format
=
633 isl_has_matching_typed_storage_image_format(&device
->info
,
635 isl_lower_storage_image_format(&device
->info
, view
->format
) :
638 anv_fill_buffer_surface_state(device
, view
->storage_surface_state
,
640 view
->offset
, view
->range
,
641 (storage_format
== ISL_FORMAT_RAW
? 1 :
642 isl_format_get_layout(storage_format
)->bpb
/ 8));
644 isl_buffer_fill_image_param(&device
->isl_dev
,
645 &view
->storage_image_param
,
646 view
->format
, view
->range
);
648 view
->storage_surface_state
= (struct anv_state
){ 0 };
653 anv_CreateBufferView(VkDevice _device
,
654 const VkBufferViewCreateInfo
*pCreateInfo
,
655 const VkAllocationCallbacks
*pAllocator
,
658 ANV_FROM_HANDLE(anv_device
, device
, _device
);
659 struct anv_buffer_view
*view
;
661 view
= anv_alloc2(&device
->alloc
, pAllocator
, sizeof(*view
), 8,
662 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
664 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
666 anv_buffer_view_init(view
, device
, pCreateInfo
, NULL
);
668 *pView
= anv_buffer_view_to_handle(view
);
674 anv_DestroyBufferView(VkDevice _device
, VkBufferView bufferView
,
675 const VkAllocationCallbacks
*pAllocator
)
677 ANV_FROM_HANDLE(anv_device
, device
, _device
);
678 ANV_FROM_HANDLE(anv_buffer_view
, view
, bufferView
);
680 if (view
->surface_state
.alloc_size
> 0)
681 anv_state_pool_free(&device
->surface_state_pool
,
682 view
->surface_state
);
684 if (view
->storage_surface_state
.alloc_size
> 0)
685 anv_state_pool_free(&device
->surface_state_pool
,
686 view
->storage_surface_state
);
688 anv_free2(&device
->alloc
, pAllocator
, view
);
691 const struct anv_surface
*
692 anv_image_get_surface_for_aspect_mask(const struct anv_image
*image
,
693 VkImageAspectFlags aspect_mask
)
695 switch (aspect_mask
) {
696 case VK_IMAGE_ASPECT_COLOR_BIT
:
697 assert(image
->aspects
== VK_IMAGE_ASPECT_COLOR_BIT
);
698 return &image
->color_surface
;
699 case VK_IMAGE_ASPECT_DEPTH_BIT
:
700 assert(image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
);
701 return &image
->depth_surface
;
702 case VK_IMAGE_ASPECT_STENCIL_BIT
:
703 assert(image
->aspects
& VK_IMAGE_ASPECT_STENCIL_BIT
);
704 return &image
->stencil_surface
;
705 case VK_IMAGE_ASPECT_DEPTH_BIT
| VK_IMAGE_ASPECT_STENCIL_BIT
:
706 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
707 * combined depth stencil formats. Specifically, it states:
709 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
710 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
712 * Image views with both depth and stencil aspects are only valid for
713 * render target attachments, in which case
714 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
715 * stencil surfaces from the underlying surface.
717 if (image
->aspects
& VK_IMAGE_ASPECT_DEPTH_BIT
) {
718 return &image
->depth_surface
;
720 assert(image
->aspects
== VK_IMAGE_ASPECT_STENCIL_BIT
);
721 return &image
->stencil_surface
;
724 unreachable("image does not have aspect");