anv: Enable fast clears on gen7-8
[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 /* Allow the user to control HiZ enabling. Disable by default on gen7
181 * because resolves are not currently implemented pre-BDW.
182 */
183 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
184 /* It will never be used as an attachment, HiZ is pointless. */
185 } else 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 assert(image->aux_surface.isl.size == 0);
193 isl_surf_get_hiz_surf(&dev->isl_dev, &image->depth_surface.isl,
194 &image->aux_surface.isl);
195 add_surface(image, &image->aux_surface);
196 }
197 } else if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && vk_info->samples == 1) {
198 if (!unlikely(INTEL_DEBUG & DEBUG_NO_RBC)) {
199 assert(image->aux_surface.isl.size == 0);
200 ok = isl_surf_get_ccs_surf(&dev->isl_dev, &anv_surf->isl,
201 &image->aux_surface.isl);
202 if (ok) {
203 add_surface(image, &image->aux_surface);
204
205 /* For images created without MUTABLE_FORMAT_BIT set, we know that
206 * they will always be used with the original format. In
207 * particular, they will always be used with a format that
208 * supports color compression. This means that it's safe to just
209 * leave compression on at all times for these formats.
210 */
211 if (!(vk_info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) &&
212 isl_format_supports_lossless_compression(&dev->info, format)) {
213 if (vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
214 /*
215 * For now, we leave compression off for anything that may
216 * be used as a storage image. This is because accessing
217 * storage images may involve ccs-incompatible views or even
218 * untyped messages which don't support compression at all.
219 */
220 anv_finishme("Enable CCS for storage images");
221 } else {
222 image->aux_usage = ISL_AUX_USAGE_CCS_E;
223 }
224 }
225 }
226 }
227 }
228
229 return VK_SUCCESS;
230 }
231
232 VkResult
233 anv_image_create(VkDevice _device,
234 const struct anv_image_create_info *create_info,
235 const VkAllocationCallbacks* alloc,
236 VkImage *pImage)
237 {
238 ANV_FROM_HANDLE(anv_device, device, _device);
239 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
240 struct anv_image *image = NULL;
241 VkResult r;
242
243 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
244
245 anv_assert(pCreateInfo->mipLevels > 0);
246 anv_assert(pCreateInfo->arrayLayers > 0);
247 anv_assert(pCreateInfo->samples > 0);
248 anv_assert(pCreateInfo->extent.width > 0);
249 anv_assert(pCreateInfo->extent.height > 0);
250 anv_assert(pCreateInfo->extent.depth > 0);
251
252 image = vk_alloc2(&device->alloc, alloc, sizeof(*image), 8,
253 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
254 if (!image)
255 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
256
257 memset(image, 0, sizeof(*image));
258 image->type = pCreateInfo->imageType;
259 image->extent = pCreateInfo->extent;
260 image->vk_format = pCreateInfo->format;
261 image->aspects = vk_format_aspects(image->vk_format);
262 image->levels = pCreateInfo->mipLevels;
263 image->array_size = pCreateInfo->arrayLayers;
264 image->samples = pCreateInfo->samples;
265 image->usage = pCreateInfo->usage;
266 image->tiling = pCreateInfo->tiling;
267 image->aux_usage = ISL_AUX_USAGE_NONE;
268
269 uint32_t b;
270 for_each_bit(b, image->aspects) {
271 r = make_surface(device, image, create_info, (1 << b));
272 if (r != VK_SUCCESS)
273 goto fail;
274 }
275
276 *pImage = anv_image_to_handle(image);
277
278 return VK_SUCCESS;
279
280 fail:
281 if (image)
282 vk_free2(&device->alloc, alloc, image);
283
284 return r;
285 }
286
287 VkResult
288 anv_CreateImage(VkDevice device,
289 const VkImageCreateInfo *pCreateInfo,
290 const VkAllocationCallbacks *pAllocator,
291 VkImage *pImage)
292 {
293 return anv_image_create(device,
294 &(struct anv_image_create_info) {
295 .vk_info = pCreateInfo,
296 },
297 pAllocator,
298 pImage);
299 }
300
301 void
302 anv_DestroyImage(VkDevice _device, VkImage _image,
303 const VkAllocationCallbacks *pAllocator)
304 {
305 ANV_FROM_HANDLE(anv_device, device, _device);
306 ANV_FROM_HANDLE(anv_image, image, _image);
307
308 if (!image)
309 return;
310
311 vk_free2(&device->alloc, pAllocator, image);
312 }
313
314 VkResult anv_BindImageMemory(
315 VkDevice _device,
316 VkImage _image,
317 VkDeviceMemory _memory,
318 VkDeviceSize memoryOffset)
319 {
320 ANV_FROM_HANDLE(anv_device, device, _device);
321 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
322 ANV_FROM_HANDLE(anv_image, image, _image);
323
324 if (mem) {
325 image->bo = &mem->bo;
326 image->offset = memoryOffset;
327 } else {
328 image->bo = NULL;
329 image->offset = 0;
330 }
331
332 if (image->aux_surface.isl.size > 0) {
333
334 /* The offset and size must be a multiple of 4K or else the
335 * anv_gem_mmap call below will return NULL.
336 */
337 assert((image->offset + image->aux_surface.offset) % 4096 == 0);
338 assert(image->aux_surface.isl.size % 4096 == 0);
339
340 /* Auxiliary surfaces need to have their memory cleared to 0 before they
341 * can be used. For CCS surfaces, this puts them in the "resolved"
342 * state so they can be used with CCS enabled before we ever touch it
343 * from the GPU. For HiZ, we need something valid or else we may get
344 * GPU hangs on some hardware and 0 works fine.
345 */
346 void *map = anv_gem_mmap(device, image->bo->gem_handle,
347 image->offset + image->aux_surface.offset,
348 image->aux_surface.isl.size,
349 device->info.has_llc ? 0 : I915_MMAP_WC);
350
351 /* If anv_gem_mmap returns NULL, it's likely that the kernel was
352 * not able to find space on the host to create a proper mapping.
353 */
354 if (map == NULL)
355 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
356
357 memset(map, 0, image->aux_surface.isl.size);
358
359 anv_gem_munmap(map, image->aux_surface.isl.size);
360 }
361
362 return VK_SUCCESS;
363 }
364
365 static void
366 anv_surface_get_subresource_layout(struct anv_image *image,
367 struct anv_surface *surface,
368 const VkImageSubresource *subresource,
369 VkSubresourceLayout *layout)
370 {
371 /* If we are on a non-zero mip level or array slice, we need to
372 * calculate a real offset.
373 */
374 anv_assert(subresource->mipLevel == 0);
375 anv_assert(subresource->arrayLayer == 0);
376
377 layout->offset = surface->offset;
378 layout->rowPitch = surface->isl.row_pitch;
379 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
380 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
381 layout->size = surface->isl.size;
382 }
383
384 void anv_GetImageSubresourceLayout(
385 VkDevice device,
386 VkImage _image,
387 const VkImageSubresource* pSubresource,
388 VkSubresourceLayout* pLayout)
389 {
390 ANV_FROM_HANDLE(anv_image, image, _image);
391
392 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
393
394 switch (pSubresource->aspectMask) {
395 case VK_IMAGE_ASPECT_COLOR_BIT:
396 anv_surface_get_subresource_layout(image, &image->color_surface,
397 pSubresource, pLayout);
398 break;
399 case VK_IMAGE_ASPECT_DEPTH_BIT:
400 anv_surface_get_subresource_layout(image, &image->depth_surface,
401 pSubresource, pLayout);
402 break;
403 case VK_IMAGE_ASPECT_STENCIL_BIT:
404 anv_surface_get_subresource_layout(image, &image->stencil_surface,
405 pSubresource, pLayout);
406 break;
407 default:
408 assert(!"Invalid image aspect");
409 }
410 }
411
412 static struct anv_state
413 alloc_surface_state(struct anv_device *device)
414 {
415 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
416 }
417
418 static enum isl_channel_select
419 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
420 struct isl_swizzle format_swizzle)
421 {
422 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
423 swizzle = component;
424
425 switch (swizzle) {
426 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
427 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
428 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
429 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
430 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
431 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
432 default:
433 unreachable("Invalid swizzle");
434 }
435 }
436
437
438 VkResult
439 anv_CreateImageView(VkDevice _device,
440 const VkImageViewCreateInfo *pCreateInfo,
441 const VkAllocationCallbacks *pAllocator,
442 VkImageView *pView)
443 {
444 ANV_FROM_HANDLE(anv_device, device, _device);
445 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
446 struct anv_image_view *iview;
447
448 iview = vk_alloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
449 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
450 if (iview == NULL)
451 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
452
453 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
454
455 assert(range->layerCount > 0);
456 assert(range->baseMipLevel < image->levels);
457 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
458 VK_IMAGE_USAGE_STORAGE_BIT |
459 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
460 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
461
462 switch (image->type) {
463 default:
464 unreachable("bad VkImageType");
465 case VK_IMAGE_TYPE_1D:
466 case VK_IMAGE_TYPE_2D:
467 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
468 break;
469 case VK_IMAGE_TYPE_3D:
470 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
471 <= anv_minify(image->extent.depth, range->baseMipLevel));
472 break;
473 }
474
475 const struct anv_surface *surface =
476 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
477
478 iview->image = image;
479 iview->bo = image->bo;
480 iview->offset = image->offset + surface->offset;
481
482 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
483 iview->vk_format = pCreateInfo->format;
484
485 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
486 range->aspectMask, image->tiling);
487
488 iview->isl = (struct isl_view) {
489 .format = format.isl_format,
490 .base_level = range->baseMipLevel,
491 .levels = anv_get_levelCount(image, range),
492 .base_array_layer = range->baseArrayLayer,
493 .array_len = anv_get_layerCount(image, range),
494 .swizzle = {
495 .r = remap_swizzle(pCreateInfo->components.r,
496 VK_COMPONENT_SWIZZLE_R, format.swizzle),
497 .g = remap_swizzle(pCreateInfo->components.g,
498 VK_COMPONENT_SWIZZLE_G, format.swizzle),
499 .b = remap_swizzle(pCreateInfo->components.b,
500 VK_COMPONENT_SWIZZLE_B, format.swizzle),
501 .a = remap_swizzle(pCreateInfo->components.a,
502 VK_COMPONENT_SWIZZLE_A, format.swizzle),
503 },
504 };
505
506 iview->extent = (VkExtent3D) {
507 .width = anv_minify(image->extent.width , range->baseMipLevel),
508 .height = anv_minify(image->extent.height, range->baseMipLevel),
509 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
510 };
511
512 if (image->type == VK_IMAGE_TYPE_3D) {
513 iview->isl.base_array_layer = 0;
514 iview->isl.array_len = iview->extent.depth;
515 }
516
517 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
518 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
519 iview->isl.usage = ISL_SURF_USAGE_CUBE_BIT;
520 } else {
521 iview->isl.usage = 0;
522 }
523
524 /* Input attachment surfaces for color or depth are allocated and filled
525 * out at BeginRenderPass time because they need compression information.
526 * Stencil image do not support compression so we just use the texture
527 * surface from the image view.
528 */
529 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
530 (image->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
531 (iview->aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT))) {
532 iview->sampler_surface_state = alloc_surface_state(device);
533
534 struct isl_view view = iview->isl;
535 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
536 isl_surf_fill_state(&device->isl_dev,
537 iview->sampler_surface_state.map,
538 .surf = &surface->isl,
539 .view = &view,
540 .aux_surf = &image->aux_surface.isl,
541 .aux_usage = image->aux_usage,
542 .mocs = device->default_mocs);
543
544 if (!device->info.has_llc)
545 anv_state_clflush(iview->sampler_surface_state);
546 } else {
547 iview->sampler_surface_state.alloc_size = 0;
548 }
549
550 /* NOTE: This one needs to go last since it may stomp isl_view.format */
551 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
552 iview->storage_surface_state = alloc_surface_state(device);
553
554 if (isl_has_matching_typed_storage_image_format(&device->info,
555 format.isl_format)) {
556 struct isl_view view = iview->isl;
557 view.usage |= ISL_SURF_USAGE_STORAGE_BIT;
558 view.format = isl_lower_storage_image_format(&device->info,
559 format.isl_format);
560 isl_surf_fill_state(&device->isl_dev,
561 iview->storage_surface_state.map,
562 .surf = &surface->isl,
563 .view = &view,
564 .aux_surf = &image->aux_surface.isl,
565 .aux_usage = image->aux_usage,
566 .mocs = device->default_mocs);
567 } else {
568 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
569 ISL_FORMAT_RAW,
570 iview->offset,
571 iview->bo->size - iview->offset, 1);
572 }
573
574 isl_surf_fill_image_param(&device->isl_dev,
575 &iview->storage_image_param,
576 &surface->isl, &iview->isl);
577
578 if (!device->info.has_llc)
579 anv_state_clflush(iview->storage_surface_state);
580 } else {
581 iview->storage_surface_state.alloc_size = 0;
582 }
583
584 *pView = anv_image_view_to_handle(iview);
585
586 return VK_SUCCESS;
587 }
588
589 void
590 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
591 const VkAllocationCallbacks *pAllocator)
592 {
593 ANV_FROM_HANDLE(anv_device, device, _device);
594 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
595
596 if (!iview)
597 return;
598
599 if (iview->sampler_surface_state.alloc_size > 0) {
600 anv_state_pool_free(&device->surface_state_pool,
601 iview->sampler_surface_state);
602 }
603
604 if (iview->storage_surface_state.alloc_size > 0) {
605 anv_state_pool_free(&device->surface_state_pool,
606 iview->storage_surface_state);
607 }
608
609 vk_free2(&device->alloc, pAllocator, iview);
610 }
611
612
613 VkResult
614 anv_CreateBufferView(VkDevice _device,
615 const VkBufferViewCreateInfo *pCreateInfo,
616 const VkAllocationCallbacks *pAllocator,
617 VkBufferView *pView)
618 {
619 ANV_FROM_HANDLE(anv_device, device, _device);
620 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
621 struct anv_buffer_view *view;
622
623 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
624 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
625 if (!view)
626 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
627
628 /* TODO: Handle the format swizzle? */
629
630 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
631 VK_IMAGE_ASPECT_COLOR_BIT,
632 VK_IMAGE_TILING_LINEAR);
633 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
634 view->bo = buffer->bo;
635 view->offset = buffer->offset + pCreateInfo->offset;
636 view->range = pCreateInfo->range == VK_WHOLE_SIZE ?
637 buffer->size - pCreateInfo->offset : pCreateInfo->range;
638 view->range = align_down_npot_u32(view->range, format_bs);
639
640 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
641 view->surface_state = alloc_surface_state(device);
642
643 anv_fill_buffer_surface_state(device, view->surface_state,
644 view->format,
645 view->offset, view->range, format_bs);
646 } else {
647 view->surface_state = (struct anv_state){ 0 };
648 }
649
650 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
651 view->storage_surface_state = alloc_surface_state(device);
652
653 enum isl_format storage_format =
654 isl_has_matching_typed_storage_image_format(&device->info,
655 view->format) ?
656 isl_lower_storage_image_format(&device->info, view->format) :
657 ISL_FORMAT_RAW;
658
659 anv_fill_buffer_surface_state(device, view->storage_surface_state,
660 storage_format,
661 view->offset, view->range,
662 (storage_format == ISL_FORMAT_RAW ? 1 :
663 isl_format_get_layout(storage_format)->bpb / 8));
664
665 isl_buffer_fill_image_param(&device->isl_dev,
666 &view->storage_image_param,
667 view->format, view->range);
668 } else {
669 view->storage_surface_state = (struct anv_state){ 0 };
670 }
671
672 *pView = anv_buffer_view_to_handle(view);
673
674 return VK_SUCCESS;
675 }
676
677 void
678 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
679 const VkAllocationCallbacks *pAllocator)
680 {
681 ANV_FROM_HANDLE(anv_device, device, _device);
682 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
683
684 if (!view)
685 return;
686
687 if (view->surface_state.alloc_size > 0)
688 anv_state_pool_free(&device->surface_state_pool,
689 view->surface_state);
690
691 if (view->storage_surface_state.alloc_size > 0)
692 anv_state_pool_free(&device->surface_state_pool,
693 view->storage_surface_state);
694
695 vk_free2(&device->alloc, pAllocator, view);
696 }
697
698 const struct anv_surface *
699 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
700 VkImageAspectFlags aspect_mask)
701 {
702 switch (aspect_mask) {
703 case VK_IMAGE_ASPECT_COLOR_BIT:
704 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
705 return &image->color_surface;
706 case VK_IMAGE_ASPECT_DEPTH_BIT:
707 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
708 return &image->depth_surface;
709 case VK_IMAGE_ASPECT_STENCIL_BIT:
710 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
711 return &image->stencil_surface;
712 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
713 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
714 * combined depth stencil formats. Specifically, it states:
715 *
716 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
717 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
718 *
719 * Image views with both depth and stencil aspects are only valid for
720 * render target attachments, in which case
721 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
722 * stencil surfaces from the underlying surface.
723 */
724 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
725 return &image->depth_surface;
726 } else {
727 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
728 return &image->stencil_surface;
729 }
730 default:
731 unreachable("image does not have aspect");
732 return NULL;
733 }
734 }