anv/image: Rename hiz_surface to aux_surface
[mesa.git] / src / intel / vulkan / anv_image.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31 #include "util/debug.h"
32
33 #include "vk_format_info.h"
34
35 /**
36 * Exactly one bit must be set in \a aspect.
37 */
38 static isl_surf_usage_flags_t
39 choose_isl_surf_usage(VkImageUsageFlags vk_usage,
40 VkImageAspectFlags aspect)
41 {
42 isl_surf_usage_flags_t isl_usage = 0;
43
44 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
45 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
46
47 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
48 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
49
50 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
51 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
52
53 if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
54 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
55
56 /* Even if we're only using it for transfer operations, clears to depth and
57 * stencil images happen as depth and stencil so they need the right ISL
58 * usage bits or else things will fall apart.
59 */
60 switch (aspect) {
61 case VK_IMAGE_ASPECT_DEPTH_BIT:
62 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
63 break;
64 case VK_IMAGE_ASPECT_STENCIL_BIT:
65 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
66 break;
67 case VK_IMAGE_ASPECT_COLOR_BIT:
68 break;
69 default:
70 unreachable("bad VkImageAspect");
71 }
72
73 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
74 /* blorp implements transfers by sampling from the source image. */
75 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
76 }
77
78 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
79 /* blorp implements transfers by rendering into the destination image. */
80 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
81 }
82
83 return isl_usage;
84 }
85
86 /**
87 * Exactly one bit must be set in \a aspect.
88 */
89 static struct anv_surface *
90 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
91 {
92 switch (aspect) {
93 default:
94 unreachable("bad VkImageAspect");
95 case VK_IMAGE_ASPECT_COLOR_BIT:
96 return &image->color_surface;
97 case VK_IMAGE_ASPECT_DEPTH_BIT:
98 return &image->depth_surface;
99 case VK_IMAGE_ASPECT_STENCIL_BIT:
100 return &image->stencil_surface;
101 }
102 }
103
104 static void
105 add_surface(struct anv_image *image, struct anv_surface *surf)
106 {
107 assert(surf->isl.size > 0); /* isl surface must be initialized */
108
109 surf->offset = align_u32(image->size, surf->isl.alignment);
110 image->size = surf->offset + surf->isl.size;
111 image->alignment = MAX2(image->alignment, surf->isl.alignment);
112 }
113
114 /**
115 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
116 * image's memory requirements (that is, the image's size and alignment).
117 *
118 * Exactly one bit must be set in \a aspect.
119 */
120 static VkResult
121 make_surface(const struct anv_device *dev,
122 struct anv_image *image,
123 const struct anv_image_create_info *anv_info,
124 VkImageAspectFlags aspect)
125 {
126 const VkImageCreateInfo *vk_info = anv_info->vk_info;
127 bool ok UNUSED;
128
129 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
130 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
131 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
132 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
133 };
134
135 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
136 * result with an optionally provided ISL tiling argument.
137 */
138 isl_tiling_flags_t tiling_flags =
139 (vk_info->tiling == VK_IMAGE_TILING_LINEAR) ?
140 ISL_TILING_LINEAR_BIT : ISL_TILING_ANY_MASK;
141
142 if (anv_info->isl_tiling_flags)
143 tiling_flags &= anv_info->isl_tiling_flags;
144
145 assert(tiling_flags);
146
147 struct anv_surface *anv_surf = get_surface(image, aspect);
148
149 image->extent = anv_sanitize_image_extent(vk_info->imageType,
150 vk_info->extent);
151
152 enum isl_format format = anv_get_isl_format(&dev->info, vk_info->format,
153 aspect, vk_info->tiling);
154 assert(format != ISL_FORMAT_UNSUPPORTED);
155
156 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
157 .dim = vk_to_isl_surf_dim[vk_info->imageType],
158 .format = format,
159 .width = image->extent.width,
160 .height = image->extent.height,
161 .depth = image->extent.depth,
162 .levels = vk_info->mipLevels,
163 .array_len = vk_info->arrayLayers,
164 .samples = vk_info->samples,
165 .min_alignment = 0,
166 .min_pitch = anv_info->stride,
167 .usage = choose_isl_surf_usage(image->usage, aspect),
168 .tiling_flags = tiling_flags);
169
170 /* isl_surf_init() will fail only if provided invalid input. Invalid input
171 * is illegal in Vulkan.
172 */
173 assert(ok);
174
175 add_surface(image, anv_surf);
176
177 /* Add a HiZ surface to a depth buffer that will be used for rendering.
178 */
179 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT &&
180 (image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
181
182 /* Allow the user to control HiZ enabling. Disable by default on gen7
183 * because resolves are not currently implemented pre-BDW.
184 */
185 if (!env_var_as_boolean("INTEL_VK_HIZ", dev->info.gen >= 8)) {
186 anv_finishme("Implement gen7 HiZ");
187 } else if (vk_info->mipLevels > 1) {
188 anv_finishme("Test multi-LOD HiZ");
189 } else if (dev->info.gen == 8 && vk_info->samples > 1) {
190 anv_finishme("Test gen8 multisampled HiZ");
191 } else {
192 isl_surf_get_hiz_surf(&dev->isl_dev, &image->depth_surface.isl,
193 &image->aux_surface.isl);
194 add_surface(image, &image->aux_surface);
195 }
196 }
197
198 return VK_SUCCESS;
199 }
200
201 VkResult
202 anv_image_create(VkDevice _device,
203 const struct anv_image_create_info *create_info,
204 const VkAllocationCallbacks* alloc,
205 VkImage *pImage)
206 {
207 ANV_FROM_HANDLE(anv_device, device, _device);
208 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
209 struct anv_image *image = NULL;
210 VkResult r;
211
212 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
213
214 anv_assert(pCreateInfo->mipLevels > 0);
215 anv_assert(pCreateInfo->arrayLayers > 0);
216 anv_assert(pCreateInfo->samples > 0);
217 anv_assert(pCreateInfo->extent.width > 0);
218 anv_assert(pCreateInfo->extent.height > 0);
219 anv_assert(pCreateInfo->extent.depth > 0);
220
221 image = vk_alloc2(&device->alloc, alloc, sizeof(*image), 8,
222 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
223 if (!image)
224 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
225
226 memset(image, 0, sizeof(*image));
227 image->type = pCreateInfo->imageType;
228 image->extent = pCreateInfo->extent;
229 image->vk_format = pCreateInfo->format;
230 image->aspects = vk_format_aspects(image->vk_format);
231 image->levels = pCreateInfo->mipLevels;
232 image->array_size = pCreateInfo->arrayLayers;
233 image->samples = pCreateInfo->samples;
234 image->usage = pCreateInfo->usage;
235 image->tiling = pCreateInfo->tiling;
236
237 uint32_t b;
238 for_each_bit(b, image->aspects) {
239 r = make_surface(device, image, create_info, (1 << b));
240 if (r != VK_SUCCESS)
241 goto fail;
242 }
243
244 *pImage = anv_image_to_handle(image);
245
246 return VK_SUCCESS;
247
248 fail:
249 if (image)
250 vk_free2(&device->alloc, alloc, image);
251
252 return r;
253 }
254
255 VkResult
256 anv_CreateImage(VkDevice device,
257 const VkImageCreateInfo *pCreateInfo,
258 const VkAllocationCallbacks *pAllocator,
259 VkImage *pImage)
260 {
261 return anv_image_create(device,
262 &(struct anv_image_create_info) {
263 .vk_info = pCreateInfo,
264 },
265 pAllocator,
266 pImage);
267 }
268
269 void
270 anv_DestroyImage(VkDevice _device, VkImage _image,
271 const VkAllocationCallbacks *pAllocator)
272 {
273 ANV_FROM_HANDLE(anv_device, device, _device);
274 ANV_FROM_HANDLE(anv_image, image, _image);
275
276 if (!image)
277 return;
278
279 vk_free2(&device->alloc, pAllocator, image);
280 }
281
282 VkResult anv_BindImageMemory(
283 VkDevice _device,
284 VkImage _image,
285 VkDeviceMemory _memory,
286 VkDeviceSize memoryOffset)
287 {
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);
291
292 if (mem) {
293 image->bo = &mem->bo;
294 image->offset = memoryOffset;
295 } else {
296 image->bo = NULL;
297 image->offset = 0;
298 }
299
300 if (anv_image_has_hiz(image)) {
301
302 /* The offset and size must be a multiple of 4K or else the
303 * anv_gem_mmap call below will return NULL.
304 */
305 assert((image->offset + image->aux_surface.offset) % 4096 == 0);
306 assert(image->aux_surface.isl.size % 4096 == 0);
307
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.
311 */
312 void *map = anv_gem_mmap(device, image->bo->gem_handle,
313 image->offset + image->aux_surface.offset,
314 image->aux_surface.isl.size,
315 device->info.has_llc ? 0 : I915_MMAP_WC);
316
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.
319 */
320 if (map == NULL)
321 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
322
323 memset(map, 0, image->aux_surface.isl.size);
324
325 anv_gem_munmap(map, image->aux_surface.isl.size);
326 }
327
328 return VK_SUCCESS;
329 }
330
331 static void
332 anv_surface_get_subresource_layout(struct anv_image *image,
333 struct anv_surface *surface,
334 const VkImageSubresource *subresource,
335 VkSubresourceLayout *layout)
336 {
337 /* If we are on a non-zero mip level or array slice, we need to
338 * calculate a real offset.
339 */
340 anv_assert(subresource->mipLevel == 0);
341 anv_assert(subresource->arrayLayer == 0);
342
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;
348 }
349
350 void anv_GetImageSubresourceLayout(
351 VkDevice device,
352 VkImage _image,
353 const VkImageSubresource* pSubresource,
354 VkSubresourceLayout* pLayout)
355 {
356 ANV_FROM_HANDLE(anv_image, image, _image);
357
358 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
359
360 switch (pSubresource->aspectMask) {
361 case VK_IMAGE_ASPECT_COLOR_BIT:
362 anv_surface_get_subresource_layout(image, &image->color_surface,
363 pSubresource, pLayout);
364 break;
365 case VK_IMAGE_ASPECT_DEPTH_BIT:
366 anv_surface_get_subresource_layout(image, &image->depth_surface,
367 pSubresource, pLayout);
368 break;
369 case VK_IMAGE_ASPECT_STENCIL_BIT:
370 anv_surface_get_subresource_layout(image, &image->stencil_surface,
371 pSubresource, pLayout);
372 break;
373 default:
374 assert(!"Invalid image aspect");
375 }
376 }
377
378 static struct anv_state
379 alloc_surface_state(struct anv_device *device)
380 {
381 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
382 }
383
384 static enum isl_channel_select
385 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
386 struct isl_swizzle format_swizzle)
387 {
388 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
389 swizzle = component;
390
391 switch (swizzle) {
392 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
393 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
394 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
395 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
396 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
397 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
398 default:
399 unreachable("Invalid swizzle");
400 }
401 }
402
403
404 VkResult
405 anv_CreateImageView(VkDevice _device,
406 const VkImageViewCreateInfo *pCreateInfo,
407 const VkAllocationCallbacks *pAllocator,
408 VkImageView *pView)
409 {
410 ANV_FROM_HANDLE(anv_device, device, _device);
411 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
412 struct anv_image_view *iview;
413
414 iview = vk_alloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
415 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
416 if (iview == NULL)
417 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
418
419 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
420
421 assert(range->layerCount > 0);
422 assert(range->baseMipLevel < image->levels);
423 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
424 VK_IMAGE_USAGE_STORAGE_BIT |
425 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
426 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
427
428 switch (image->type) {
429 default:
430 unreachable("bad VkImageType");
431 case VK_IMAGE_TYPE_1D:
432 case VK_IMAGE_TYPE_2D:
433 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
434 break;
435 case VK_IMAGE_TYPE_3D:
436 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
437 <= anv_minify(image->extent.depth, range->baseMipLevel));
438 break;
439 }
440
441 const struct anv_surface *surface =
442 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
443
444 iview->image = image;
445 iview->bo = image->bo;
446 iview->offset = image->offset + surface->offset;
447
448 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
449 iview->vk_format = pCreateInfo->format;
450
451 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
452 range->aspectMask, image->tiling);
453
454 iview->isl = (struct isl_view) {
455 .format = format.isl_format,
456 .base_level = range->baseMipLevel,
457 .levels = anv_get_levelCount(image, range),
458 .base_array_layer = range->baseArrayLayer,
459 .array_len = anv_get_layerCount(image, range),
460 .swizzle = {
461 .r = remap_swizzle(pCreateInfo->components.r,
462 VK_COMPONENT_SWIZZLE_R, format.swizzle),
463 .g = remap_swizzle(pCreateInfo->components.g,
464 VK_COMPONENT_SWIZZLE_G, format.swizzle),
465 .b = remap_swizzle(pCreateInfo->components.b,
466 VK_COMPONENT_SWIZZLE_B, format.swizzle),
467 .a = remap_swizzle(pCreateInfo->components.a,
468 VK_COMPONENT_SWIZZLE_A, format.swizzle),
469 },
470 };
471
472 iview->extent = (VkExtent3D) {
473 .width = anv_minify(image->extent.width , range->baseMipLevel),
474 .height = anv_minify(image->extent.height, range->baseMipLevel),
475 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
476 };
477
478 if (image->type == VK_IMAGE_TYPE_3D) {
479 iview->isl.base_array_layer = 0;
480 iview->isl.array_len = iview->extent.depth;
481 }
482
483 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
484 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
485 iview->isl.usage = ISL_SURF_USAGE_CUBE_BIT;
486 } else {
487 iview->isl.usage = 0;
488 }
489
490 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
491 iview->sampler_surface_state = alloc_surface_state(device);
492
493 struct isl_view view = iview->isl;
494 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
495 isl_surf_fill_state(&device->isl_dev,
496 iview->sampler_surface_state.map,
497 .surf = &surface->isl,
498 .view = &view,
499 .mocs = device->default_mocs);
500
501 if (!device->info.has_llc)
502 anv_state_clflush(iview->sampler_surface_state);
503 } else {
504 iview->sampler_surface_state.alloc_size = 0;
505 }
506
507 /* NOTE: This one needs to go last since it may stomp isl_view.format */
508 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
509 iview->storage_surface_state = alloc_surface_state(device);
510
511 if (isl_has_matching_typed_storage_image_format(&device->info,
512 format.isl_format)) {
513 struct isl_view view = iview->isl;
514 view.usage |= ISL_SURF_USAGE_STORAGE_BIT;
515 view.format = isl_lower_storage_image_format(&device->info,
516 format.isl_format);
517 isl_surf_fill_state(&device->isl_dev,
518 iview->storage_surface_state.map,
519 .surf = &surface->isl,
520 .view = &view,
521 .mocs = device->default_mocs);
522 } else {
523 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
524 ISL_FORMAT_RAW,
525 iview->offset,
526 iview->bo->size - iview->offset, 1);
527 }
528
529 isl_surf_fill_image_param(&device->isl_dev,
530 &iview->storage_image_param,
531 &surface->isl, &iview->isl);
532
533 if (!device->info.has_llc)
534 anv_state_clflush(iview->storage_surface_state);
535 } else {
536 iview->storage_surface_state.alloc_size = 0;
537 }
538
539 *pView = anv_image_view_to_handle(iview);
540
541 return VK_SUCCESS;
542 }
543
544 void
545 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
546 const VkAllocationCallbacks *pAllocator)
547 {
548 ANV_FROM_HANDLE(anv_device, device, _device);
549 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
550
551 if (!iview)
552 return;
553
554 if (iview->sampler_surface_state.alloc_size > 0) {
555 anv_state_pool_free(&device->surface_state_pool,
556 iview->sampler_surface_state);
557 }
558
559 if (iview->storage_surface_state.alloc_size > 0) {
560 anv_state_pool_free(&device->surface_state_pool,
561 iview->storage_surface_state);
562 }
563
564 vk_free2(&device->alloc, pAllocator, iview);
565 }
566
567
568 VkResult
569 anv_CreateBufferView(VkDevice _device,
570 const VkBufferViewCreateInfo *pCreateInfo,
571 const VkAllocationCallbacks *pAllocator,
572 VkBufferView *pView)
573 {
574 ANV_FROM_HANDLE(anv_device, device, _device);
575 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
576 struct anv_buffer_view *view;
577
578 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
579 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
580 if (!view)
581 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
582
583 /* TODO: Handle the format swizzle? */
584
585 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
586 VK_IMAGE_ASPECT_COLOR_BIT,
587 VK_IMAGE_TILING_LINEAR);
588 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
589 view->bo = buffer->bo;
590 view->offset = buffer->offset + pCreateInfo->offset;
591 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
592 buffer->size - pCreateInfo->offset : pCreateInfo->range;
593 view->range = align_down_npot_u32(view->range, format_bs);
594
595 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
596 view->surface_state = alloc_surface_state(device);
597
598 anv_fill_buffer_surface_state(device, view->surface_state,
599 view->format,
600 view->offset, view->range, format_bs);
601 } else {
602 view->surface_state = (struct anv_state){ 0 };
603 }
604
605 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
606 view->storage_surface_state = alloc_surface_state(device);
607
608 enum isl_format storage_format =
609 isl_has_matching_typed_storage_image_format(&device->info,
610 view->format) ?
611 isl_lower_storage_image_format(&device->info, view->format) :
612 ISL_FORMAT_RAW;
613
614 anv_fill_buffer_surface_state(device, view->storage_surface_state,
615 storage_format,
616 view->offset, view->range,
617 (storage_format == ISL_FORMAT_RAW ? 1 :
618 isl_format_get_layout(storage_format)->bpb / 8));
619
620 isl_buffer_fill_image_param(&device->isl_dev,
621 &view->storage_image_param,
622 view->format, view->range);
623 } else {
624 view->storage_surface_state = (struct anv_state){ 0 };
625 }
626
627 *pView = anv_buffer_view_to_handle(view);
628
629 return VK_SUCCESS;
630 }
631
632 void
633 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
634 const VkAllocationCallbacks *pAllocator)
635 {
636 ANV_FROM_HANDLE(anv_device, device, _device);
637 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
638
639 if (!view)
640 return;
641
642 if (view->surface_state.alloc_size > 0)
643 anv_state_pool_free(&device->surface_state_pool,
644 view->surface_state);
645
646 if (view->storage_surface_state.alloc_size > 0)
647 anv_state_pool_free(&device->surface_state_pool,
648 view->storage_surface_state);
649
650 vk_free2(&device->alloc, pAllocator, view);
651 }
652
653 const struct anv_surface *
654 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
655 VkImageAspectFlags aspect_mask)
656 {
657 switch (aspect_mask) {
658 case VK_IMAGE_ASPECT_COLOR_BIT:
659 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
660 return &image->color_surface;
661 case VK_IMAGE_ASPECT_DEPTH_BIT:
662 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
663 return &image->depth_surface;
664 case VK_IMAGE_ASPECT_STENCIL_BIT:
665 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
666 return &image->stencil_surface;
667 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
668 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
669 * combined depth stencil formats. Specifically, it states:
670 *
671 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
672 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
673 *
674 * Image views with both depth and stencil aspects are only valid for
675 * render target attachments, in which case
676 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
677 * stencil surfaces from the underlying surface.
678 */
679 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
680 return &image->depth_surface;
681 } else {
682 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
683 return &image->stencil_surface;
684 }
685 default:
686 unreachable("image does not have aspect");
687 return NULL;
688 }
689 }