anv/image: Set correct base_array_layer and array_len for storage images
[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 const 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 if (image->type == VK_IMAGE_TYPE_3D) {
496 isl_view.base_array_layer = 0;
497 isl_view.array_len = iview->extent.depth;
498 }
499 isl_surf_fill_state(&device->isl_dev,
500 iview->storage_surface_state.map,
501 .surf = &surface->isl,
502 .view = &isl_view,
503 .mocs = device->default_mocs);
504 } else {
505 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
506 ISL_FORMAT_RAW,
507 iview->offset,
508 iview->bo->size - iview->offset, 1);
509 }
510
511 isl_surf_fill_image_param(&device->isl_dev,
512 &iview->storage_image_param,
513 &surface->isl, &isl_view);
514
515 if (!device->info.has_llc)
516 anv_state_clflush(iview->storage_surface_state);
517 } else {
518 iview->storage_surface_state.alloc_size = 0;
519 }
520 }
521
522 VkResult
523 anv_CreateImageView(VkDevice _device,
524 const VkImageViewCreateInfo *pCreateInfo,
525 const VkAllocationCallbacks *pAllocator,
526 VkImageView *pView)
527 {
528 ANV_FROM_HANDLE(anv_device, device, _device);
529 struct anv_image_view *view;
530
531 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
532 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
533 if (view == NULL)
534 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
535
536 anv_image_view_init(view, device, pCreateInfo, NULL, ~0);
537
538 *pView = anv_image_view_to_handle(view);
539
540 return VK_SUCCESS;
541 }
542
543 void
544 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
545 const VkAllocationCallbacks *pAllocator)
546 {
547 ANV_FROM_HANDLE(anv_device, device, _device);
548 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
549
550 if (iview->color_rt_surface_state.alloc_size > 0) {
551 anv_state_pool_free(&device->surface_state_pool,
552 iview->color_rt_surface_state);
553 }
554
555 if (iview->sampler_surface_state.alloc_size > 0) {
556 anv_state_pool_free(&device->surface_state_pool,
557 iview->sampler_surface_state);
558 }
559
560 if (iview->storage_surface_state.alloc_size > 0) {
561 anv_state_pool_free(&device->surface_state_pool,
562 iview->storage_surface_state);
563 }
564
565 anv_free2(&device->alloc, pAllocator, iview);
566 }
567
568
569 void anv_buffer_view_init(struct anv_buffer_view *view,
570 struct anv_device *device,
571 const VkBufferViewCreateInfo* pCreateInfo,
572 struct anv_cmd_buffer *cmd_buffer)
573 {
574 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
575
576 /* TODO: Handle the format swizzle? */
577
578 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
579 VK_IMAGE_ASPECT_COLOR_BIT,
580 VK_IMAGE_TILING_LINEAR);
581 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
582 view->bo = buffer->bo;
583 view->offset = buffer->offset + pCreateInfo->offset;
584 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
585 buffer->size - pCreateInfo->offset : pCreateInfo->range;
586 view->range = align_down_npot_u32(view->range, format_bs);
587
588 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
589 view->surface_state = alloc_surface_state(device, cmd_buffer);
590
591 anv_fill_buffer_surface_state(device, view->surface_state,
592 view->format,
593 view->offset, view->range, format_bs);
594 } else {
595 view->surface_state = (struct anv_state){ 0 };
596 }
597
598 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
599 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
600
601 enum isl_format storage_format =
602 isl_has_matching_typed_storage_image_format(&device->info,
603 view->format) ?
604 isl_lower_storage_image_format(&device->info, view->format) :
605 ISL_FORMAT_RAW;
606
607 anv_fill_buffer_surface_state(device, view->storage_surface_state,
608 storage_format,
609 view->offset, view->range,
610 (storage_format == ISL_FORMAT_RAW ? 1 :
611 isl_format_get_layout(storage_format)->bpb / 8));
612
613 isl_buffer_fill_image_param(&device->isl_dev,
614 &view->storage_image_param,
615 view->format, view->range);
616 } else {
617 view->storage_surface_state = (struct anv_state){ 0 };
618 }
619 }
620
621 VkResult
622 anv_CreateBufferView(VkDevice _device,
623 const VkBufferViewCreateInfo *pCreateInfo,
624 const VkAllocationCallbacks *pAllocator,
625 VkBufferView *pView)
626 {
627 ANV_FROM_HANDLE(anv_device, device, _device);
628 struct anv_buffer_view *view;
629
630 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
631 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
632 if (!view)
633 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
634
635 anv_buffer_view_init(view, device, pCreateInfo, NULL);
636
637 *pView = anv_buffer_view_to_handle(view);
638
639 return VK_SUCCESS;
640 }
641
642 void
643 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
644 const VkAllocationCallbacks *pAllocator)
645 {
646 ANV_FROM_HANDLE(anv_device, device, _device);
647 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
648
649 if (view->surface_state.alloc_size > 0)
650 anv_state_pool_free(&device->surface_state_pool,
651 view->surface_state);
652
653 if (view->storage_surface_state.alloc_size > 0)
654 anv_state_pool_free(&device->surface_state_pool,
655 view->storage_surface_state);
656
657 anv_free2(&device->alloc, pAllocator, view);
658 }
659
660 const struct anv_surface *
661 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
662 VkImageAspectFlags aspect_mask)
663 {
664 switch (aspect_mask) {
665 case VK_IMAGE_ASPECT_COLOR_BIT:
666 /* Dragons will eat you.
667 *
668 * Meta attaches all destination surfaces as color render targets. Guess
669 * what surface the Meta Dragons really want.
670 */
671 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
672 return &image->depth_surface;
673 } else if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
674 return &image->stencil_surface;
675 } else {
676 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
677 return &image->color_surface;
678 }
679 break;
680 case VK_IMAGE_ASPECT_DEPTH_BIT:
681 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
682 return &image->depth_surface;
683 case VK_IMAGE_ASPECT_STENCIL_BIT:
684 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
685 return &image->stencil_surface;
686 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
687 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
688 * combined depth stencil formats. Specifically, it states:
689 *
690 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
691 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
692 *
693 * Image views with both depth and stencil aspects are only valid for
694 * render target attachments, in which case
695 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
696 * stencil surfaces from the underlying surface.
697 */
698 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
699 return &image->depth_surface;
700 } else {
701 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
702 return &image->stencil_surface;
703 }
704 default:
705 unreachable("image does not have aspect");
706 return NULL;
707 }
708 }