anv/image: Add INPUT_ATTACHMENT to the list of required usages
[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 #include <sys/mman.h>
30
31 #include "anv_private.h"
32 #include "util/debug.h"
33
34 #include "vk_format_info.h"
35
36 /**
37 * Exactly one bit must be set in \a aspect.
38 */
39 static isl_surf_usage_flags_t
40 choose_isl_surf_usage(VkImageUsageFlags vk_usage,
41 VkImageAspectFlags aspect)
42 {
43 isl_surf_usage_flags_t isl_usage = 0;
44
45 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
46 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
47
48 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
49 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
50
51 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
52 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
53
54 if (vk_usage & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
55 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
56
57 /* Even if we're only using it for transfer operations, clears to depth and
58 * stencil images happen as depth and stencil so they need the right ISL
59 * usage bits or else things will fall apart.
60 */
61 switch (aspect) {
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 case VK_IMAGE_ASPECT_COLOR_BIT:
69 break;
70 default:
71 unreachable("bad VkImageAspect");
72 }
73
74 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
75 /* blorp implements transfers by sampling from the source image. */
76 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
77 }
78
79 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT &&
80 aspect == VK_IMAGE_ASPECT_COLOR_BIT) {
81 /* blorp implements transfers by rendering into the destination image.
82 * Only request this with color images, as we deal with depth/stencil
83 * formats differently. */
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 = MAX2(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 .row_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 /* We don't advertise that depth buffers could be used as storage
185 * images.
186 */
187 assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
188
189 /* Allow the user to control HiZ enabling. Disable by default on gen7
190 * because resolves are not currently implemented pre-BDW.
191 */
192 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
193 /* It will never be used as an attachment, HiZ is pointless. */
194 } else if (dev->info.gen == 7) {
195 anv_perf_warn("Implement gen7 HiZ");
196 } else if (vk_info->mipLevels > 1) {
197 anv_perf_warn("Enable multi-LOD HiZ");
198 } else if (vk_info->arrayLayers > 1) {
199 anv_perf_warn("Implement multi-arrayLayer HiZ clears and resolves");
200 } else if (dev->info.gen == 8 && vk_info->samples > 1) {
201 anv_perf_warn("Enable gen8 multisampled HiZ");
202 } else if (!unlikely(INTEL_DEBUG & DEBUG_NO_HIZ)) {
203 assert(image->aux_surface.isl.size == 0);
204 ok = isl_surf_get_hiz_surf(&dev->isl_dev, &image->depth_surface.isl,
205 &image->aux_surface.isl);
206 assert(ok);
207 add_surface(image, &image->aux_surface);
208 image->aux_usage = ISL_AUX_USAGE_HIZ;
209 }
210 } else if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && vk_info->samples == 1) {
211 if (!unlikely(INTEL_DEBUG & DEBUG_NO_RBC)) {
212 assert(image->aux_surface.isl.size == 0);
213 ok = isl_surf_get_ccs_surf(&dev->isl_dev, &anv_surf->isl,
214 &image->aux_surface.isl);
215 if (ok) {
216 add_surface(image, &image->aux_surface);
217
218 /* For images created without MUTABLE_FORMAT_BIT set, we know that
219 * they will always be used with the original format. In
220 * particular, they will always be used with a format that
221 * supports color compression. If it's never used as a storage
222 * image, then it will only be used through the sampler or the as
223 * a render target. This means that it's safe to just leave
224 * compression on at all times for these formats.
225 */
226 if (!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
227 !(vk_info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) &&
228 isl_format_supports_ccs_e(&dev->info, format)) {
229 image->aux_usage = ISL_AUX_USAGE_CCS_E;
230 }
231 }
232 }
233 } else if (aspect == VK_IMAGE_ASPECT_COLOR_BIT && vk_info->samples > 1) {
234 assert(image->aux_surface.isl.size == 0);
235 assert(!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT));
236 ok = isl_surf_get_mcs_surf(&dev->isl_dev, &anv_surf->isl,
237 &image->aux_surface.isl);
238 if (ok) {
239 add_surface(image, &image->aux_surface);
240 image->aux_usage = ISL_AUX_USAGE_MCS;
241 }
242 }
243
244 return VK_SUCCESS;
245 }
246
247 VkResult
248 anv_image_create(VkDevice _device,
249 const struct anv_image_create_info *create_info,
250 const VkAllocationCallbacks* alloc,
251 VkImage *pImage)
252 {
253 ANV_FROM_HANDLE(anv_device, device, _device);
254 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
255 struct anv_image *image = NULL;
256 VkResult r;
257
258 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
259
260 anv_assert(pCreateInfo->mipLevels > 0);
261 anv_assert(pCreateInfo->arrayLayers > 0);
262 anv_assert(pCreateInfo->samples > 0);
263 anv_assert(pCreateInfo->extent.width > 0);
264 anv_assert(pCreateInfo->extent.height > 0);
265 anv_assert(pCreateInfo->extent.depth > 0);
266
267 image = vk_alloc2(&device->alloc, alloc, sizeof(*image), 8,
268 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
269 if (!image)
270 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
271
272 memset(image, 0, sizeof(*image));
273 image->type = pCreateInfo->imageType;
274 image->extent = pCreateInfo->extent;
275 image->vk_format = pCreateInfo->format;
276 image->aspects = vk_format_aspects(image->vk_format);
277 image->levels = pCreateInfo->mipLevels;
278 image->array_size = pCreateInfo->arrayLayers;
279 image->samples = pCreateInfo->samples;
280 image->usage = pCreateInfo->usage;
281 image->tiling = pCreateInfo->tiling;
282 image->aux_usage = ISL_AUX_USAGE_NONE;
283
284 uint32_t b;
285 for_each_bit(b, image->aspects) {
286 r = make_surface(device, image, create_info, (1 << b));
287 if (r != VK_SUCCESS)
288 goto fail;
289 }
290
291 *pImage = anv_image_to_handle(image);
292
293 return VK_SUCCESS;
294
295 fail:
296 if (image)
297 vk_free2(&device->alloc, alloc, image);
298
299 return r;
300 }
301
302 VkResult
303 anv_CreateImage(VkDevice device,
304 const VkImageCreateInfo *pCreateInfo,
305 const VkAllocationCallbacks *pAllocator,
306 VkImage *pImage)
307 {
308 return anv_image_create(device,
309 &(struct anv_image_create_info) {
310 .vk_info = pCreateInfo,
311 },
312 pAllocator,
313 pImage);
314 }
315
316 void
317 anv_DestroyImage(VkDevice _device, VkImage _image,
318 const VkAllocationCallbacks *pAllocator)
319 {
320 ANV_FROM_HANDLE(anv_device, device, _device);
321 ANV_FROM_HANDLE(anv_image, image, _image);
322
323 if (!image)
324 return;
325
326 vk_free2(&device->alloc, pAllocator, image);
327 }
328
329 VkResult anv_BindImageMemory(
330 VkDevice _device,
331 VkImage _image,
332 VkDeviceMemory _memory,
333 VkDeviceSize memoryOffset)
334 {
335 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
336 ANV_FROM_HANDLE(anv_image, image, _image);
337
338 if (mem == NULL) {
339 image->bo = NULL;
340 image->offset = 0;
341 return VK_SUCCESS;
342 }
343
344 image->bo = mem->bo;
345 image->offset = memoryOffset;
346
347 return VK_SUCCESS;
348 }
349
350 static void
351 anv_surface_get_subresource_layout(struct anv_image *image,
352 struct anv_surface *surface,
353 const VkImageSubresource *subresource,
354 VkSubresourceLayout *layout)
355 {
356 /* If we are on a non-zero mip level or array slice, we need to
357 * calculate a real offset.
358 */
359 anv_assert(subresource->mipLevel == 0);
360 anv_assert(subresource->arrayLayer == 0);
361
362 layout->offset = surface->offset;
363 layout->rowPitch = surface->isl.row_pitch;
364 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
365 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
366 layout->size = surface->isl.size;
367 }
368
369 void anv_GetImageSubresourceLayout(
370 VkDevice device,
371 VkImage _image,
372 const VkImageSubresource* pSubresource,
373 VkSubresourceLayout* pLayout)
374 {
375 ANV_FROM_HANDLE(anv_image, image, _image);
376
377 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
378
379 switch (pSubresource->aspectMask) {
380 case VK_IMAGE_ASPECT_COLOR_BIT:
381 anv_surface_get_subresource_layout(image, &image->color_surface,
382 pSubresource, pLayout);
383 break;
384 case VK_IMAGE_ASPECT_DEPTH_BIT:
385 anv_surface_get_subresource_layout(image, &image->depth_surface,
386 pSubresource, pLayout);
387 break;
388 case VK_IMAGE_ASPECT_STENCIL_BIT:
389 anv_surface_get_subresource_layout(image, &image->stencil_surface,
390 pSubresource, pLayout);
391 break;
392 default:
393 assert(!"Invalid image aspect");
394 }
395 }
396
397 /**
398 * This function determines the optimal buffer to use for a given
399 * VkImageLayout and other pieces of information needed to make that
400 * determination. This does not determine the optimal buffer to use
401 * during a resolve operation.
402 *
403 * @param devinfo The device information of the Intel GPU.
404 * @param image The image that may contain a collection of buffers.
405 * @param aspects The aspect(s) of the image to be accessed.
406 * @param layout The current layout of the image aspect(s).
407 *
408 * @return The primary buffer that should be used for the given layout.
409 */
410 enum isl_aux_usage
411 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
412 const struct anv_image * const image,
413 const VkImageAspectFlags aspects,
414 const VkImageLayout layout)
415 {
416 /* Validate the inputs. */
417
418 /* The devinfo is needed as the optimal buffer varies across generations. */
419 assert(devinfo != NULL);
420
421 /* The layout of a NULL image is not properly defined. */
422 assert(image != NULL);
423
424 /* The aspects must be a subset of the image aspects. */
425 assert(aspects & image->aspects && aspects <= image->aspects);
426
427 /* Determine the optimal buffer. */
428
429 /* If there is no auxiliary surface allocated, we must use the one and only
430 * main buffer.
431 */
432 if (image->aux_surface.isl.size == 0)
433 return ISL_AUX_USAGE_NONE;
434
435 /* All images that use an auxiliary surface are required to be tiled. */
436 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
437
438 /* On BDW+, when clearing the stencil aspect of a depth stencil image,
439 * the HiZ buffer allows us to record the clear with a relatively small
440 * number of packets. Prior to BDW, the HiZ buffer provides no known benefit
441 * to the stencil aspect.
442 */
443 if (devinfo->gen < 8 && aspects == VK_IMAGE_ASPECT_STENCIL_BIT)
444 return ISL_AUX_USAGE_NONE;
445
446 const bool color_aspect = aspects == VK_IMAGE_ASPECT_COLOR_BIT;
447
448 /* The following switch currently only handles depth stencil aspects.
449 * TODO: Handle the color aspect.
450 */
451 if (color_aspect)
452 return image->aux_usage;
453
454 switch (layout) {
455
456 /* Invalid Layouts */
457 case VK_IMAGE_LAYOUT_RANGE_SIZE:
458 case VK_IMAGE_LAYOUT_MAX_ENUM:
459 unreachable("Invalid image layout.");
460
461 /* Undefined layouts
462 *
463 * The pre-initialized layout is equivalent to the undefined layout for
464 * optimally-tiled images. We can only do color compression (CCS or HiZ)
465 * on tiled images.
466 */
467 case VK_IMAGE_LAYOUT_UNDEFINED:
468 case VK_IMAGE_LAYOUT_PREINITIALIZED:
469 return ISL_AUX_USAGE_NONE;
470
471
472 /* Transfer Layouts
473 *
474 * This buffer could be a depth buffer used in a transfer operation. BLORP
475 * currently doesn't use HiZ for transfer operations so we must use the main
476 * buffer for this layout. TODO: Enable HiZ in BLORP.
477 */
478 case VK_IMAGE_LAYOUT_GENERAL:
479 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
480 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
481 return ISL_AUX_USAGE_NONE;
482
483
484 /* Sampling Layouts */
485 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
486 assert(!color_aspect);
487 /* Fall-through */
488 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
489 if (anv_can_sample_with_hiz(devinfo, aspects, image->samples))
490 return ISL_AUX_USAGE_HIZ;
491 else
492 return ISL_AUX_USAGE_NONE;
493
494 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
495 assert(color_aspect);
496
497 /* On SKL+, the render buffer can be decompressed by the presentation
498 * engine. Support for this feature has not yet landed in the wider
499 * ecosystem. TODO: Update this code when support lands.
500 *
501 * From the BDW PRM, Vol 7, Render Target Resolve:
502 *
503 * If the MCS is enabled on a non-multisampled render target, the
504 * render target must be resolved before being used for other
505 * purposes (display, texture, CPU lock) The clear value from
506 * SURFACE_STATE is written into pixels in the render target
507 * indicated as clear in the MCS.
508 *
509 * Pre-SKL, the render buffer must be resolved before being used for
510 * presentation. We can infer that the auxiliary buffer is not used.
511 */
512 return ISL_AUX_USAGE_NONE;
513
514
515 /* Rendering Layouts */
516 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
517 assert(color_aspect);
518 unreachable("Color images are not yet supported.");
519
520 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
521 assert(!color_aspect);
522 return ISL_AUX_USAGE_HIZ;
523
524 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
525 unreachable("VK_KHR_shared_presentable_image is unsupported");
526 }
527
528 /* If the layout isn't recognized in the exhaustive switch above, the
529 * VkImageLayout value is not defined in vulkan.h.
530 */
531 unreachable("layout is not a VkImageLayout enumeration member.");
532 }
533
534
535 static struct anv_state
536 alloc_surface_state(struct anv_device *device)
537 {
538 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
539 }
540
541 static enum isl_channel_select
542 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
543 struct isl_swizzle format_swizzle)
544 {
545 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
546 swizzle = component;
547
548 switch (swizzle) {
549 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
550 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
551 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
552 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
553 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
554 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
555 default:
556 unreachable("Invalid swizzle");
557 }
558 }
559
560
561 VkResult
562 anv_CreateImageView(VkDevice _device,
563 const VkImageViewCreateInfo *pCreateInfo,
564 const VkAllocationCallbacks *pAllocator,
565 VkImageView *pView)
566 {
567 ANV_FROM_HANDLE(anv_device, device, _device);
568 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
569 struct anv_image_view *iview;
570
571 iview = vk_alloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
572 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
573 if (iview == NULL)
574 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
575
576 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
577
578 assert(range->layerCount > 0);
579 assert(range->baseMipLevel < image->levels);
580 assert(image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
581 VK_IMAGE_USAGE_STORAGE_BIT |
582 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
583 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
584 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
585
586 switch (image->type) {
587 default:
588 unreachable("bad VkImageType");
589 case VK_IMAGE_TYPE_1D:
590 case VK_IMAGE_TYPE_2D:
591 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
592 break;
593 case VK_IMAGE_TYPE_3D:
594 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
595 <= anv_minify(image->extent.depth, range->baseMipLevel));
596 break;
597 }
598
599 const struct anv_surface *surface =
600 anv_image_get_surface_for_aspect_mask(image, range->aspectMask);
601
602 iview->image = image;
603 iview->bo = image->bo;
604 iview->offset = image->offset + surface->offset;
605
606 iview->aspect_mask = pCreateInfo->subresourceRange.aspectMask;
607 iview->vk_format = pCreateInfo->format;
608
609 struct anv_format format = anv_get_format(&device->info, pCreateInfo->format,
610 range->aspectMask, image->tiling);
611
612 iview->isl = (struct isl_view) {
613 .format = format.isl_format,
614 .base_level = range->baseMipLevel,
615 .levels = anv_get_levelCount(image, range),
616 .base_array_layer = range->baseArrayLayer,
617 .array_len = anv_get_layerCount(image, range),
618 .swizzle = {
619 .r = remap_swizzle(pCreateInfo->components.r,
620 VK_COMPONENT_SWIZZLE_R, format.swizzle),
621 .g = remap_swizzle(pCreateInfo->components.g,
622 VK_COMPONENT_SWIZZLE_G, format.swizzle),
623 .b = remap_swizzle(pCreateInfo->components.b,
624 VK_COMPONENT_SWIZZLE_B, format.swizzle),
625 .a = remap_swizzle(pCreateInfo->components.a,
626 VK_COMPONENT_SWIZZLE_A, format.swizzle),
627 },
628 };
629
630 iview->extent = (VkExtent3D) {
631 .width = anv_minify(image->extent.width , range->baseMipLevel),
632 .height = anv_minify(image->extent.height, range->baseMipLevel),
633 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
634 };
635
636 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
637 iview->isl.base_array_layer = 0;
638 iview->isl.array_len = iview->extent.depth;
639 }
640
641 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
642 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
643 iview->isl.usage = ISL_SURF_USAGE_CUBE_BIT;
644 } else {
645 iview->isl.usage = 0;
646 }
647
648 /* Input attachment surfaces for color are allocated and filled
649 * out at BeginRenderPass time because they need compression information.
650 * Compression is not yet enabled for depth textures and stencil doesn't
651 * allow compression so we can just use the texture surface state from the
652 * view.
653 */
654 if (image->usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
655 (image->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
656 !(iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT))) {
657 iview->sampler_surface_state = alloc_surface_state(device);
658 iview->no_aux_sampler_surface_state = alloc_surface_state(device);
659
660 /* Sampling is performed in one of two buffer configurations in anv: with
661 * an auxiliary buffer or without it. Sampler states aren't always needed
662 * for both configurations, but are currently created unconditionally for
663 * simplicity.
664 *
665 * TODO: Consider allocating each surface state only when necessary.
666 */
667
668 /* Create a sampler state with the optimal aux_usage for sampling. This
669 * may use the aux_buffer.
670 */
671 const enum isl_aux_usage surf_usage =
672 anv_layout_to_aux_usage(&device->info, image, iview->aspect_mask,
673 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
674
675 /* If this is a HiZ buffer we can sample from with a programmable clear
676 * value (SKL+), define the clear value to the optimal constant.
677 */
678 const float red_clear_color = surf_usage == ISL_AUX_USAGE_HIZ &&
679 device->info.gen >= 9 ?
680 ANV_HZ_FC_VAL : 0.0f;
681
682 struct isl_view view = iview->isl;
683 view.usage |= ISL_SURF_USAGE_TEXTURE_BIT;
684 isl_surf_fill_state(&device->isl_dev,
685 iview->sampler_surface_state.map,
686 .surf = &surface->isl,
687 .view = &view,
688 .clear_color.f32 = { red_clear_color,},
689 .aux_surf = &image->aux_surface.isl,
690 .aux_usage = surf_usage,
691 .mocs = device->default_mocs);
692
693 /* Create a sampler state that only uses the main buffer. */
694 isl_surf_fill_state(&device->isl_dev,
695 iview->no_aux_sampler_surface_state.map,
696 .surf = &surface->isl,
697 .view = &view,
698 .mocs = device->default_mocs);
699
700 anv_state_flush(device, iview->sampler_surface_state);
701 anv_state_flush(device, iview->no_aux_sampler_surface_state);
702 } else {
703 iview->sampler_surface_state.alloc_size = 0;
704 iview->no_aux_sampler_surface_state.alloc_size = 0;
705 }
706
707 /* NOTE: This one needs to go last since it may stomp isl_view.format */
708 if (image->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
709 iview->storage_surface_state = alloc_surface_state(device);
710 iview->writeonly_storage_surface_state = alloc_surface_state(device);
711
712 struct isl_view view = iview->isl;
713 view.usage |= ISL_SURF_USAGE_STORAGE_BIT;
714
715 /* Write-only accesses always used a typed write instruction and should
716 * therefore use the real format.
717 */
718 isl_surf_fill_state(&device->isl_dev,
719 iview->writeonly_storage_surface_state.map,
720 .surf = &surface->isl,
721 .view = &view,
722 .aux_surf = &image->aux_surface.isl,
723 .aux_usage = image->aux_usage,
724 .mocs = device->default_mocs);
725
726 if (isl_has_matching_typed_storage_image_format(&device->info,
727 format.isl_format)) {
728 /* Typed surface reads support a very limited subset of the shader
729 * image formats. Translate it into the closest format the hardware
730 * supports.
731 */
732 view.format = isl_lower_storage_image_format(&device->info,
733 format.isl_format);
734
735 isl_surf_fill_state(&device->isl_dev,
736 iview->storage_surface_state.map,
737 .surf = &surface->isl,
738 .view = &view,
739 .aux_surf = &image->aux_surface.isl,
740 .aux_usage = image->aux_usage,
741 .mocs = device->default_mocs);
742 } else {
743 anv_fill_buffer_surface_state(device, iview->storage_surface_state,
744 ISL_FORMAT_RAW,
745 iview->offset,
746 iview->bo->size - iview->offset, 1);
747 }
748
749 isl_surf_fill_image_param(&device->isl_dev,
750 &iview->storage_image_param,
751 &surface->isl, &iview->isl);
752
753 anv_state_flush(device, iview->storage_surface_state);
754 anv_state_flush(device, iview->writeonly_storage_surface_state);
755 } else {
756 iview->storage_surface_state.alloc_size = 0;
757 iview->writeonly_storage_surface_state.alloc_size = 0;
758 }
759
760 *pView = anv_image_view_to_handle(iview);
761
762 return VK_SUCCESS;
763 }
764
765 void
766 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
767 const VkAllocationCallbacks *pAllocator)
768 {
769 ANV_FROM_HANDLE(anv_device, device, _device);
770 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
771
772 if (!iview)
773 return;
774
775 if (iview->sampler_surface_state.alloc_size > 0) {
776 anv_state_pool_free(&device->surface_state_pool,
777 iview->sampler_surface_state);
778 }
779
780 if (iview->no_aux_sampler_surface_state.alloc_size > 0) {
781 anv_state_pool_free(&device->surface_state_pool,
782 iview->no_aux_sampler_surface_state);
783 }
784
785 if (iview->storage_surface_state.alloc_size > 0) {
786 anv_state_pool_free(&device->surface_state_pool,
787 iview->storage_surface_state);
788 }
789
790 if (iview->writeonly_storage_surface_state.alloc_size > 0) {
791 anv_state_pool_free(&device->surface_state_pool,
792 iview->writeonly_storage_surface_state);
793 }
794
795 vk_free2(&device->alloc, pAllocator, iview);
796 }
797
798
799 VkResult
800 anv_CreateBufferView(VkDevice _device,
801 const VkBufferViewCreateInfo *pCreateInfo,
802 const VkAllocationCallbacks *pAllocator,
803 VkBufferView *pView)
804 {
805 ANV_FROM_HANDLE(anv_device, device, _device);
806 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
807 struct anv_buffer_view *view;
808
809 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
810 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
811 if (!view)
812 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
813
814 /* TODO: Handle the format swizzle? */
815
816 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
817 VK_IMAGE_ASPECT_COLOR_BIT,
818 VK_IMAGE_TILING_LINEAR);
819 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
820 view->bo = buffer->bo;
821 view->offset = buffer->offset + pCreateInfo->offset;
822 view->range = anv_buffer_get_range(buffer, pCreateInfo->offset,
823 pCreateInfo->range);
824 view->range = align_down_npot_u32(view->range, format_bs);
825
826 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
827 view->surface_state = alloc_surface_state(device);
828
829 anv_fill_buffer_surface_state(device, view->surface_state,
830 view->format,
831 view->offset, view->range, format_bs);
832 } else {
833 view->surface_state = (struct anv_state){ 0 };
834 }
835
836 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
837 view->storage_surface_state = alloc_surface_state(device);
838 view->writeonly_storage_surface_state = alloc_surface_state(device);
839
840 enum isl_format storage_format =
841 isl_has_matching_typed_storage_image_format(&device->info,
842 view->format) ?
843 isl_lower_storage_image_format(&device->info, view->format) :
844 ISL_FORMAT_RAW;
845
846 anv_fill_buffer_surface_state(device, view->storage_surface_state,
847 storage_format,
848 view->offset, view->range,
849 (storage_format == ISL_FORMAT_RAW ? 1 :
850 isl_format_get_layout(storage_format)->bpb / 8));
851
852 /* Write-only accesses should use the original format. */
853 anv_fill_buffer_surface_state(device, view->writeonly_storage_surface_state,
854 view->format,
855 view->offset, view->range,
856 isl_format_get_layout(view->format)->bpb / 8);
857
858 isl_buffer_fill_image_param(&device->isl_dev,
859 &view->storage_image_param,
860 view->format, view->range);
861 } else {
862 view->storage_surface_state = (struct anv_state){ 0 };
863 view->writeonly_storage_surface_state = (struct anv_state){ 0 };
864 }
865
866 *pView = anv_buffer_view_to_handle(view);
867
868 return VK_SUCCESS;
869 }
870
871 void
872 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
873 const VkAllocationCallbacks *pAllocator)
874 {
875 ANV_FROM_HANDLE(anv_device, device, _device);
876 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
877
878 if (!view)
879 return;
880
881 if (view->surface_state.alloc_size > 0)
882 anv_state_pool_free(&device->surface_state_pool,
883 view->surface_state);
884
885 if (view->storage_surface_state.alloc_size > 0)
886 anv_state_pool_free(&device->surface_state_pool,
887 view->storage_surface_state);
888
889 if (view->writeonly_storage_surface_state.alloc_size > 0)
890 anv_state_pool_free(&device->surface_state_pool,
891 view->writeonly_storage_surface_state);
892
893 vk_free2(&device->alloc, pAllocator, view);
894 }
895
896 const struct anv_surface *
897 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
898 VkImageAspectFlags aspect_mask)
899 {
900 switch (aspect_mask) {
901 case VK_IMAGE_ASPECT_COLOR_BIT:
902 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
903 return &image->color_surface;
904 case VK_IMAGE_ASPECT_DEPTH_BIT:
905 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
906 return &image->depth_surface;
907 case VK_IMAGE_ASPECT_STENCIL_BIT:
908 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
909 return &image->stencil_surface;
910 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
911 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
912 * combined depth stencil formats. Specifically, it states:
913 *
914 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
915 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
916 *
917 * Image views with both depth and stencil aspects are only valid for
918 * render target attachments, in which case
919 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
920 * stencil surfaces from the underlying surface.
921 */
922 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
923 return &image->depth_surface;
924 } else {
925 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
926 return &image->stencil_surface;
927 }
928 default:
929 unreachable("image does not have aspect");
930 return NULL;
931 }
932 }