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