anv/image: Get rid of the usage hacks for meta
[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 /* FINISHME: Support aux surfaces */
45 isl_usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
46
47 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
48 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
49
50 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
51 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
52
53 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
54 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
55
56 if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
57 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
58
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.
62 */
63 switch (aspect) {
64 case VK_IMAGE_ASPECT_DEPTH_BIT:
65 isl_usage &= ~ISL_SURF_USAGE_DISABLE_AUX_BIT;
66 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
67 break;
68 case VK_IMAGE_ASPECT_STENCIL_BIT:
69 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
70 break;
71 case VK_IMAGE_ASPECT_COLOR_BIT:
72 break;
73 default:
74 unreachable("bad VkImageAspect");
75 }
76
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;
80 }
81
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;
85 }
86
87 return isl_usage;
88 }
89
90 /**
91 * Exactly one bit must be set in \a aspect.
92 */
93 static struct anv_surface *
94 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
95 {
96 switch (aspect) {
97 default:
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;
105 }
106 }
107
108 static void
109 add_surface(struct anv_image *image, struct anv_surface *surf)
110 {
111 assert(surf->isl.size > 0); /* isl surface must be initialized */
112
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);
116 }
117
118 /**
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).
121 *
122 * Exactly one bit must be set in \a aspect.
123 */
124 static VkResult
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)
129 {
130 const VkImageCreateInfo *vk_info = anv_info->vk_info;
131 bool ok UNUSED;
132
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,
137 };
138
139 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
140 * result with an optionally provided ISL tiling argument.
141 */
142 isl_tiling_flags_t tiling_flags =
143 (vk_info->tiling == VK_IMAGE_TILING_LINEAR) ?
144 ISL_TILING_LINEAR_BIT : ISL_TILING_ANY_MASK;
145
146 if (anv_info->isl_tiling_flags)
147 tiling_flags &= anv_info->isl_tiling_flags;
148
149 assert(tiling_flags);
150
151 struct anv_surface *anv_surf = get_surface(image, aspect);
152
153 image->extent = anv_sanitize_image_extent(vk_info->imageType,
154 vk_info->extent);
155
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);
159
160 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
161 .dim = vk_to_isl_surf_dim[vk_info->imageType],
162 .format = format,
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,
169 .min_alignment = 0,
170 .min_pitch = anv_info->stride,
171 .usage = choose_isl_surf_usage(image->usage, aspect),
172 .tiling_flags = tiling_flags);
173
174 /* isl_surf_init() will fail only if provided invalid input. Invalid input
175 * is illegal in Vulkan.
176 */
177 assert(ok);
178
179 add_surface(image, anv_surf);
180
181 /* Add a HiZ surface to a depth buffer that will be used for rendering.
182 */
183 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT &&
184 (image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
185
186 /* Allow the user to control HiZ enabling. Disable by default on gen7
187 * because resolves are not currently implemented pre-BDW.
188 */
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");
195 } else {
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);
199 }
200 }
201
202 return VK_SUCCESS;
203 }
204
205 VkResult
206 anv_image_create(VkDevice _device,
207 const struct anv_image_create_info *create_info,
208 const VkAllocationCallbacks* alloc,
209 VkImage *pImage)
210 {
211 ANV_FROM_HANDLE(anv_device, device, _device);
212 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
213 struct anv_image *image = NULL;
214 VkResult r;
215
216 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
217
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);
224
225 image = anv_alloc2(&device->alloc, alloc, sizeof(*image), 8,
226 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
227 if (!image)
228 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
229
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;
240
241 uint32_t b;
242 for_each_bit(b, image->aspects) {
243 r = make_surface(device, image, create_info, (1 << b));
244 if (r != VK_SUCCESS)
245 goto fail;
246 }
247
248 *pImage = anv_image_to_handle(image);
249
250 return VK_SUCCESS;
251
252 fail:
253 if (image)
254 anv_free2(&device->alloc, alloc, image);
255
256 return r;
257 }
258
259 VkResult
260 anv_CreateImage(VkDevice device,
261 const VkImageCreateInfo *pCreateInfo,
262 const VkAllocationCallbacks *pAllocator,
263 VkImage *pImage)
264 {
265 return anv_image_create(device,
266 &(struct anv_image_create_info) {
267 .vk_info = pCreateInfo,
268 },
269 pAllocator,
270 pImage);
271 }
272
273 void
274 anv_DestroyImage(VkDevice _device, VkImage _image,
275 const VkAllocationCallbacks *pAllocator)
276 {
277 ANV_FROM_HANDLE(anv_device, device, _device);
278
279 anv_free2(&device->alloc, pAllocator, anv_image_from_handle(_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->hiz_surface.offset) % 4096 == 0);
306 assert(image->hiz_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->hiz_surface.offset,
314 image->hiz_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->hiz_surface.isl.size);
324
325 anv_gem_munmap(map, image->hiz_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 struct anv_cmd_buffer *cmd_buffer)
381 {
382 if (cmd_buffer) {
383 return anv_cmd_buffer_alloc_surface_state(cmd_buffer);
384 } else {
385 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
386 }
387 }
388
389 static enum isl_channel_select
390 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
391 struct isl_swizzle format_swizzle)
392 {
393 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
394 swizzle = component;
395
396 switch (swizzle) {
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;
403 default:
404 unreachable("Invalid swizzle");
405 }
406 }
407
408 void
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)
413 {
414 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
415 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
416
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));
423
424 switch (image->type) {
425 default:
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);
430 break;
431 case VK_IMAGE_TYPE_3D:
432 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
433 <= anv_minify(image->extent.depth, range->baseMipLevel));
434 break;
435 }
436
437 const struct anv_surface *surface =
438 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
439
440 iview->image = image;
441 iview->bo = image->bo;
442 iview->offset = image->offset + surface->offset;
443
444 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
445 iview->vk_format = pCreateInfo->format;
446
447 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
448 range->aspectMask, image->tiling);
449
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),
456 .swizzle = {
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),
465 },
466 };
467
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),
472 };
473
474 if (image->type == VK_IMAGE_TYPE_3D) {
475 iview->isl.base_array_layer = 0;
476 iview->isl.array_len = iview->extent.depth;
477 }
478
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;
482 } else {
483 iview->isl.usage = 0;
484 }
485
486 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
487 iview->sampler_surface_state = alloc_surface_state(device, cmd_buffer);
488
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,
494 .view = &view,
495 .mocs = device->default_mocs);
496
497 if (!device->info.has_llc)
498 anv_state_clflush(iview->sampler_surface_state);
499 } else {
500 iview->sampler_surface_state.alloc_size = 0;
501 }
502
503 if (image->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
504 iview->color_rt_surface_state = alloc_surface_state(device, cmd_buffer);
505
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,
511 .view = &view,
512 .mocs = device->default_mocs);
513
514 if (!device->info.has_llc)
515 anv_state_clflush(iview->color_rt_surface_state);
516 } else {
517 iview->color_rt_surface_state.alloc_size = 0;
518 }
519
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);
523
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,
529 format.isl_format);
530 isl_surf_fill_state(&device->isl_dev,
531 iview->storage_surface_state.map,
532 .surf = &surface->isl,
533 .view = &view,
534 .mocs = device->default_mocs);
535 } else {
536 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
537 ISL_FORMAT_RAW,
538 iview->offset,
539 iview->bo->size - iview->offset, 1);
540 }
541
542 isl_surf_fill_image_param(&device->isl_dev,
543 &iview->storage_image_param,
544 &surface->isl, &iview->isl);
545
546 if (!device->info.has_llc)
547 anv_state_clflush(iview->storage_surface_state);
548 } else {
549 iview->storage_surface_state.alloc_size = 0;
550 }
551 }
552
553 VkResult
554 anv_CreateImageView(VkDevice _device,
555 const VkImageViewCreateInfo *pCreateInfo,
556 const VkAllocationCallbacks *pAllocator,
557 VkImageView *pView)
558 {
559 ANV_FROM_HANDLE(anv_device, device, _device);
560 struct anv_image_view *view;
561
562 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
563 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
564 if (view == NULL)
565 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
566
567 anv_image_view_init(view, device, pCreateInfo, NULL);
568
569 *pView = anv_image_view_to_handle(view);
570
571 return VK_SUCCESS;
572 }
573
574 void
575 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
576 const VkAllocationCallbacks *pAllocator)
577 {
578 ANV_FROM_HANDLE(anv_device, device, _device);
579 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
580
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);
584 }
585
586 if (iview->sampler_surface_state.alloc_size > 0) {
587 anv_state_pool_free(&device->surface_state_pool,
588 iview->sampler_surface_state);
589 }
590
591 if (iview->storage_surface_state.alloc_size > 0) {
592 anv_state_pool_free(&device->surface_state_pool,
593 iview->storage_surface_state);
594 }
595
596 anv_free2(&device->alloc, pAllocator, iview);
597 }
598
599
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)
604 {
605 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
606
607 /* TODO: Handle the format swizzle? */
608
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);
618
619 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
620 view->surface_state = alloc_surface_state(device, cmd_buffer);
621
622 anv_fill_buffer_surface_state(device, view->surface_state,
623 view->format,
624 view->offset, view->range, format_bs);
625 } else {
626 view->surface_state = (struct anv_state){ 0 };
627 }
628
629 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
630 view->storage_surface_state = alloc_surface_state(device, cmd_buffer);
631
632 enum isl_format storage_format =
633 isl_has_matching_typed_storage_image_format(&device->info,
634 view->format) ?
635 isl_lower_storage_image_format(&device->info, view->format) :
636 ISL_FORMAT_RAW;
637
638 anv_fill_buffer_surface_state(device, view->storage_surface_state,
639 storage_format,
640 view->offset, view->range,
641 (storage_format == ISL_FORMAT_RAW ? 1 :
642 isl_format_get_layout(storage_format)->bpb / 8));
643
644 isl_buffer_fill_image_param(&device->isl_dev,
645 &view->storage_image_param,
646 view->format, view->range);
647 } else {
648 view->storage_surface_state = (struct anv_state){ 0 };
649 }
650 }
651
652 VkResult
653 anv_CreateBufferView(VkDevice _device,
654 const VkBufferViewCreateInfo *pCreateInfo,
655 const VkAllocationCallbacks *pAllocator,
656 VkBufferView *pView)
657 {
658 ANV_FROM_HANDLE(anv_device, device, _device);
659 struct anv_buffer_view *view;
660
661 view = anv_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
662 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
663 if (!view)
664 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
665
666 anv_buffer_view_init(view, device, pCreateInfo, NULL);
667
668 *pView = anv_buffer_view_to_handle(view);
669
670 return VK_SUCCESS;
671 }
672
673 void
674 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
675 const VkAllocationCallbacks *pAllocator)
676 {
677 ANV_FROM_HANDLE(anv_device, device, _device);
678 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
679
680 if (view->surface_state.alloc_size > 0)
681 anv_state_pool_free(&device->surface_state_pool,
682 view->surface_state);
683
684 if (view->storage_surface_state.alloc_size > 0)
685 anv_state_pool_free(&device->surface_state_pool,
686 view->storage_surface_state);
687
688 anv_free2(&device->alloc, pAllocator, view);
689 }
690
691 const struct anv_surface *
692 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
693 VkImageAspectFlags aspect_mask)
694 {
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:
708 *
709 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
710 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
711 *
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.
716 */
717 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
718 return &image->depth_surface;
719 } else {
720 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
721 return &image->stencil_surface;
722 }
723 default:
724 unreachable("image does not have aspect");
725 return NULL;
726 }
727 }