anv: enable multiple planes per image/imageView
[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 #include "vk_util.h"
34
35 #include "vk_format_info.h"
36
37 /**
38 * Exactly one bit must be set in \a aspect.
39 */
40 static isl_surf_usage_flags_t
41 choose_isl_surf_usage(VkImageCreateFlags vk_create_flags,
42 VkImageUsageFlags vk_usage,
43 VkImageAspectFlags aspect)
44 {
45 isl_surf_usage_flags_t isl_usage = 0;
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_create_flags & 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_DEPTH_BIT;
66 break;
67 case VK_IMAGE_ASPECT_STENCIL_BIT:
68 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
69 break;
70 case VK_IMAGE_ASPECT_COLOR_BIT:
71 case VK_IMAGE_ASPECT_PLANE_0_BIT_KHR:
72 case VK_IMAGE_ASPECT_PLANE_1_BIT_KHR:
73 case VK_IMAGE_ASPECT_PLANE_2_BIT_KHR:
74 break;
75 default:
76 unreachable("bad VkImageAspect");
77 }
78
79 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
80 /* blorp implements transfers by sampling from the source image. */
81 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
82 }
83
84 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT &&
85 aspect == VK_IMAGE_ASPECT_COLOR_BIT) {
86 /* blorp implements transfers by rendering into the destination image.
87 * Only request this with color images, as we deal with depth/stencil
88 * formats differently. */
89 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
90 }
91
92 return isl_usage;
93 }
94
95 /**
96 * Exactly one bit must be set in \a aspect.
97 */
98 static struct anv_surface *
99 get_surface(struct anv_image *image, VkImageAspectFlags aspect)
100 {
101 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
102 return &image->planes[plane].surface;
103 }
104
105 static void
106 add_surface(struct anv_image *image, struct anv_surface *surf, uint32_t plane)
107 {
108 assert(surf->isl.size > 0); /* isl surface must be initialized */
109
110 if (image->disjoint) {
111 surf->offset = align_u32(image->planes[plane].size, surf->isl.alignment);
112 /* Plane offset is always 0 when it's disjoint. */
113 } else {
114 surf->offset = align_u32(image->size, surf->isl.alignment);
115 /* Determine plane's offset only once when the first surface is added. */
116 if (image->planes[plane].size == 0)
117 image->planes[plane].offset = image->size;
118 }
119
120 image->size = surf->offset + surf->isl.size;
121 image->planes[plane].size = (surf->offset + surf->isl.size) - image->planes[plane].offset;
122
123 image->alignment = MAX2(image->alignment, surf->isl.alignment);
124 image->planes[plane].alignment = MAX2(image->planes[plane].alignment,
125 surf->isl.alignment);
126 }
127
128
129 static bool
130 all_formats_ccs_e_compatible(const struct gen_device_info *devinfo,
131 const struct VkImageCreateInfo *vk_info)
132 {
133 enum isl_format format =
134 anv_get_isl_format(devinfo, vk_info->format,
135 VK_IMAGE_ASPECT_COLOR_BIT, vk_info->tiling);
136
137 if (!isl_format_supports_ccs_e(devinfo, format))
138 return false;
139
140 if (!(vk_info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
141 return true;
142
143 const VkImageFormatListCreateInfoKHR *fmt_list =
144 vk_find_struct_const(vk_info->pNext, IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
145
146 if (!fmt_list || fmt_list->viewFormatCount == 0)
147 return false;
148
149 for (uint32_t i = 0; i < fmt_list->viewFormatCount; i++) {
150 enum isl_format view_format =
151 anv_get_isl_format(devinfo, fmt_list->pViewFormats[i],
152 VK_IMAGE_ASPECT_COLOR_BIT, vk_info->tiling);
153
154 if (!isl_formats_are_ccs_e_compatible(devinfo, format, view_format))
155 return false;
156 }
157
158 return true;
159 }
160
161 /**
162 * For color images that have an auxiliary surface, request allocation for an
163 * additional buffer that mainly stores fast-clear values. Use of this buffer
164 * allows us to access the image's subresources while being aware of their
165 * fast-clear values in non-trivial cases (e.g., outside of a render pass in
166 * which a fast clear has occurred).
167 *
168 * For the purpose of discoverability, the algorithm used to manage this buffer
169 * is described here. A clear value in this buffer is updated when a fast clear
170 * is performed on a subresource. One of two synchronization operations is
171 * performed in order for a following memory access to use the fast-clear
172 * value:
173 * a. Copy the value from the buffer to the surface state object used for
174 * reading. This is done implicitly when the value is the clear value
175 * predetermined to be the default in other surface state objects. This
176 * is currently only done explicitly for the operation below.
177 * b. Do (a) and use the surface state object to resolve the subresource.
178 * This is only done during layout transitions for decent performance.
179 *
180 * With the above scheme, we can fast-clear whenever the hardware allows except
181 * for two cases in which synchronization becomes impossible or undesirable:
182 * * The subresource is in the GENERAL layout and is cleared to a value
183 * other than the special default value.
184 *
185 * Performing a synchronization operation in order to read from the
186 * subresource is undesirable in this case. Firstly, b) is not an option
187 * because a layout transition isn't required between a write and read of
188 * an image in the GENERAL layout. Secondly, it's undesirable to do a)
189 * explicitly because it would require large infrastructural changes. The
190 * Vulkan API supports us in deciding not to optimize this layout by
191 * stating that using this layout may cause suboptimal performance. NOTE:
192 * the auxiliary buffer must always be enabled to support a) implicitly.
193 *
194 *
195 * * For the given miplevel, only some of the layers are cleared at once.
196 *
197 * If the user clears each layer to a different value, then tries to
198 * render to multiple layers at once, we have no ability to perform a
199 * synchronization operation in between. a) is not helpful because the
200 * object can only hold one clear value. b) is not an option because a
201 * layout transition isn't required in this case.
202 */
203 static void
204 add_fast_clear_state_buffer(struct anv_image *image,
205 VkImageAspectFlagBits aspect,
206 uint32_t plane,
207 const struct anv_device *device)
208 {
209 assert(image && device);
210 assert(image->planes[plane].aux_surface.isl.size > 0 &&
211 image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT);
212
213 /* The offset to the buffer of clear values must be dword-aligned for GPU
214 * memcpy operations. It is located immediately after the auxiliary surface.
215 */
216
217 /* Tiled images are guaranteed to be 4K aligned, so the image alignment
218 * should also be dword-aligned.
219 */
220 assert(image->alignment % 4 == 0);
221
222 /* Auxiliary buffers should be a multiple of 4K, so the start of the clear
223 * values buffer should already be dword-aligned.
224 */
225 assert((image->planes[plane].offset + image->planes[plane].size) % 4 == 0);
226
227 /* This buffer should be at the very end of the plane. */
228 if (image->disjoint) {
229 assert(image->planes[plane].size ==
230 (image->planes[plane].offset + image->planes[plane].size));
231 } else {
232 assert(image->size ==
233 (image->planes[plane].offset + image->planes[plane].size));
234 }
235
236 const unsigned entry_size = anv_fast_clear_state_entry_size(device);
237 /* There's no padding between entries, so ensure that they're always a
238 * multiple of 32 bits in order to enable GPU memcpy operations.
239 */
240 assert(entry_size % 4 == 0);
241
242 const unsigned plane_state_size =
243 entry_size * anv_image_aux_levels(image, aspect);
244
245 image->planes[plane].fast_clear_state_offset =
246 image->planes[plane].offset + image->planes[plane].size;
247
248 image->planes[plane].size += plane_state_size;
249 image->size += plane_state_size;
250 }
251
252 /**
253 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
254 * image's memory requirements (that is, the image's size and alignment).
255 *
256 * Exactly one bit must be set in \a aspect.
257 */
258 static VkResult
259 make_surface(const struct anv_device *dev,
260 struct anv_image *image,
261 const struct anv_image_create_info *anv_info,
262 VkImageAspectFlags aspect)
263 {
264 const VkImageCreateInfo *vk_info = anv_info->vk_info;
265 bool ok UNUSED;
266
267 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
268 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
269 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
270 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
271 };
272
273 /* Translate the Vulkan tiling to an equivalent ISL tiling, then filter the
274 * result with an optionally provided ISL tiling argument.
275 */
276 isl_tiling_flags_t tiling_flags =
277 (vk_info->tiling == VK_IMAGE_TILING_LINEAR) ?
278 ISL_TILING_LINEAR_BIT : ISL_TILING_ANY_MASK;
279
280 if (anv_info->isl_tiling_flags)
281 tiling_flags &= anv_info->isl_tiling_flags;
282
283 assert(tiling_flags);
284
285 image->extent = anv_sanitize_image_extent(vk_info->imageType,
286 vk_info->extent);
287
288 const unsigned plane = anv_image_aspect_to_plane(image->aspects, aspect);
289 const struct anv_format_plane plane_format =
290 anv_get_format_plane(&dev->info, image->vk_format, aspect, image->tiling);
291 struct anv_surface *anv_surf = &image->planes[plane].surface;
292
293 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
294 * fall back to linear on Broadwell and earlier because we aren't
295 * guaranteed that we can handle offsets correctly. On Sky Lake, the
296 * horizontal and vertical alignments are sufficiently high that we can
297 * just use RENDER_SURFACE_STATE::X/Y Offset.
298 */
299 bool needs_shadow = false;
300 if (dev->info.gen <= 8 &&
301 (vk_info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT_KHR) &&
302 vk_info->tiling == VK_IMAGE_TILING_OPTIMAL) {
303 assert(isl_format_is_compressed(plane_format.isl_format));
304 tiling_flags = ISL_TILING_LINEAR_BIT;
305 needs_shadow = true;
306 }
307
308 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
309 .dim = vk_to_isl_surf_dim[vk_info->imageType],
310 .format = plane_format.isl_format,
311 .width = image->extent.width / plane_format.denominator_scales[0],
312 .height = image->extent.height / plane_format.denominator_scales[1],
313 .depth = image->extent.depth,
314 .levels = vk_info->mipLevels,
315 .array_len = vk_info->arrayLayers,
316 .samples = vk_info->samples,
317 .min_alignment = 0,
318 .row_pitch = anv_info->stride,
319 .usage = choose_isl_surf_usage(vk_info->flags, image->usage, aspect),
320 .tiling_flags = tiling_flags);
321
322 /* isl_surf_init() will fail only if provided invalid input. Invalid input
323 * is illegal in Vulkan.
324 */
325 assert(ok);
326
327 image->planes[plane].aux_usage = ISL_AUX_USAGE_NONE;
328
329 add_surface(image, anv_surf, plane);
330
331 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
332 * create an identical tiled shadow surface for use while texturing so we
333 * don't get garbage performance.
334 */
335 if (needs_shadow) {
336 assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
337 assert(tiling_flags == ISL_TILING_LINEAR_BIT);
338
339 ok = isl_surf_init(&dev->isl_dev, &image->planes[plane].shadow_surface.isl,
340 .dim = vk_to_isl_surf_dim[vk_info->imageType],
341 .format = plane_format.isl_format,
342 .width = image->extent.width,
343 .height = image->extent.height,
344 .depth = image->extent.depth,
345 .levels = vk_info->mipLevels,
346 .array_len = vk_info->arrayLayers,
347 .samples = vk_info->samples,
348 .min_alignment = 0,
349 .row_pitch = anv_info->stride,
350 .usage = choose_isl_surf_usage(image->usage, image->usage, aspect),
351 .tiling_flags = ISL_TILING_ANY_MASK);
352
353 /* isl_surf_init() will fail only if provided invalid input. Invalid input
354 * is illegal in Vulkan.
355 */
356 assert(ok);
357
358 add_surface(image, &image->planes[plane].shadow_surface, plane);
359 }
360
361 /* Add a HiZ surface to a depth buffer that will be used for rendering.
362 */
363 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
364 /* We don't advertise that depth buffers could be used as storage
365 * images.
366 */
367 assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
368
369 /* Allow the user to control HiZ enabling. Disable by default on gen7
370 * because resolves are not currently implemented pre-BDW.
371 */
372 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
373 /* It will never be used as an attachment, HiZ is pointless. */
374 } else if (dev->info.gen == 7) {
375 anv_perf_warn(dev->instance, image, "Implement gen7 HiZ");
376 } else if (vk_info->mipLevels > 1) {
377 anv_perf_warn(dev->instance, image, "Enable multi-LOD HiZ");
378 } else if (vk_info->arrayLayers > 1) {
379 anv_perf_warn(dev->instance, image,
380 "Implement multi-arrayLayer HiZ clears and resolves");
381 } else if (dev->info.gen == 8 && vk_info->samples > 1) {
382 anv_perf_warn(dev->instance, image, "Enable gen8 multisampled HiZ");
383 } else if (!unlikely(INTEL_DEBUG & DEBUG_NO_HIZ)) {
384 assert(image->planes[plane].aux_surface.isl.size == 0);
385 ok = isl_surf_get_hiz_surf(&dev->isl_dev,
386 &image->planes[plane].surface.isl,
387 &image->planes[plane].aux_surface.isl);
388 assert(ok);
389 add_surface(image, &image->planes[plane].aux_surface, plane);
390 image->planes[plane].aux_usage = ISL_AUX_USAGE_HIZ;
391 }
392 } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT) && vk_info->samples == 1) {
393 /* TODO: Disallow compression with :
394 *
395 * 1) non multiplanar images (We appear to hit a sampler bug with
396 * CCS & R16G16 format. Putting the clear state a page/4096bytes
397 * further fixes the issue).
398 *
399 * 2) alias images, because they might be aliases of images
400 * described in 1)
401 *
402 * 3) compression disabled by debug
403 */
404 const bool allow_compression =
405 image->n_planes == 1 &&
406 (vk_info->flags & VK_IMAGE_CREATE_ALIAS_BIT_KHR) == 0 &&
407 likely((INTEL_DEBUG & DEBUG_NO_RBC) == 0);
408
409 if (allow_compression) {
410 assert(image->planes[plane].aux_surface.isl.size == 0);
411 ok = isl_surf_get_ccs_surf(&dev->isl_dev,
412 &image->planes[plane].surface.isl,
413 &image->planes[plane].aux_surface.isl, 0);
414 if (ok) {
415
416 /* Disable CCS when it is not useful (i.e., when you can't render
417 * to the image with CCS enabled).
418 */
419 if (!isl_format_supports_rendering(&dev->info,
420 plane_format.isl_format)) {
421 /* While it may be technically possible to enable CCS for this
422 * image, we currently don't have things hooked up to get it
423 * working.
424 */
425 anv_perf_warn(dev->instance, image,
426 "This image format doesn't support rendering. "
427 "Not allocating an CCS buffer.");
428 image->planes[plane].aux_surface.isl.size = 0;
429 return VK_SUCCESS;
430 }
431
432 add_surface(image, &image->planes[plane].aux_surface, plane);
433 add_fast_clear_state_buffer(image, aspect, plane, dev);
434
435 /* For images created without MUTABLE_FORMAT_BIT set, we know that
436 * they will always be used with the original format. In
437 * particular, they will always be used with a format that
438 * supports color compression. If it's never used as a storage
439 * image, then it will only be used through the sampler or the as
440 * a render target. This means that it's safe to just leave
441 * compression on at all times for these formats.
442 */
443 if (!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
444 all_formats_ccs_e_compatible(&dev->info, vk_info)) {
445 image->planes[plane].aux_usage = ISL_AUX_USAGE_CCS_E;
446 }
447 }
448 }
449 } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT) && vk_info->samples > 1) {
450 assert(!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT));
451 assert(image->planes[plane].aux_surface.isl.size == 0);
452 ok = isl_surf_get_mcs_surf(&dev->isl_dev,
453 &image->planes[plane].surface.isl,
454 &image->planes[plane].aux_surface.isl);
455 if (ok) {
456 add_surface(image, &image->planes[plane].aux_surface, plane);
457 add_fast_clear_state_buffer(image, aspect, plane, dev);
458 image->planes[plane].aux_usage = ISL_AUX_USAGE_MCS;
459 }
460 }
461
462 assert((image->planes[plane].offset + image->planes[plane].size) == image->size);
463
464 /* Upper bound of the last surface should be smaller than the plane's
465 * size.
466 */
467 assert((MAX2(image->planes[plane].surface.offset,
468 image->planes[plane].aux_surface.offset) +
469 (image->planes[plane].aux_surface.isl.size > 0 ?
470 image->planes[plane].aux_surface.isl.size :
471 image->planes[plane].surface.isl.size)) <=
472 (image->planes[plane].offset + image->planes[plane].size));
473
474 if (image->planes[plane].aux_surface.isl.size) {
475 /* assert(image->planes[plane].fast_clear_state_offset == */
476 /* (image->planes[plane].aux_surface.offset + image->planes[plane].aux_surface.isl.size)); */
477 assert(image->planes[plane].fast_clear_state_offset <
478 (image->planes[plane].offset + image->planes[plane].size));
479 }
480
481 return VK_SUCCESS;
482 }
483
484 VkResult
485 anv_image_create(VkDevice _device,
486 const struct anv_image_create_info *create_info,
487 const VkAllocationCallbacks* alloc,
488 VkImage *pImage)
489 {
490 ANV_FROM_HANDLE(anv_device, device, _device);
491 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
492 struct anv_image *image = NULL;
493 VkResult r;
494
495 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
496
497 anv_assert(pCreateInfo->mipLevels > 0);
498 anv_assert(pCreateInfo->arrayLayers > 0);
499 anv_assert(pCreateInfo->samples > 0);
500 anv_assert(pCreateInfo->extent.width > 0);
501 anv_assert(pCreateInfo->extent.height > 0);
502 anv_assert(pCreateInfo->extent.depth > 0);
503
504 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
505 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
506 if (!image)
507 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
508
509 image->type = pCreateInfo->imageType;
510 image->extent = pCreateInfo->extent;
511 image->vk_format = pCreateInfo->format;
512 image->format = anv_get_format(pCreateInfo->format);
513 image->aspects = vk_format_aspects(image->vk_format);
514 image->levels = pCreateInfo->mipLevels;
515 image->array_size = pCreateInfo->arrayLayers;
516 image->samples = pCreateInfo->samples;
517 image->usage = pCreateInfo->usage;
518 image->tiling = pCreateInfo->tiling;
519
520 const struct anv_format *format = anv_get_format(image->vk_format);
521 assert(format != NULL);
522
523 image->n_planes = format->n_planes;
524
525 uint32_t b;
526 for_each_bit(b, image->aspects) {
527 r = make_surface(device, image, create_info, (1 << b));
528 if (r != VK_SUCCESS)
529 goto fail;
530 }
531
532 *pImage = anv_image_to_handle(image);
533
534 return VK_SUCCESS;
535
536 fail:
537 if (image)
538 vk_free2(&device->alloc, alloc, image);
539
540 return r;
541 }
542
543 VkResult
544 anv_CreateImage(VkDevice device,
545 const VkImageCreateInfo *pCreateInfo,
546 const VkAllocationCallbacks *pAllocator,
547 VkImage *pImage)
548 {
549 return anv_image_create(device,
550 &(struct anv_image_create_info) {
551 .vk_info = pCreateInfo,
552 },
553 pAllocator,
554 pImage);
555 }
556
557 void
558 anv_DestroyImage(VkDevice _device, VkImage _image,
559 const VkAllocationCallbacks *pAllocator)
560 {
561 ANV_FROM_HANDLE(anv_device, device, _device);
562 ANV_FROM_HANDLE(anv_image, image, _image);
563
564 if (!image)
565 return;
566
567 vk_free2(&device->alloc, pAllocator, image);
568 }
569
570 static void anv_image_bind_memory_plane(struct anv_device *device,
571 struct anv_image *image,
572 uint32_t plane,
573 struct anv_device_memory *memory,
574 uint32_t memory_offset)
575 {
576 if (!memory) {
577 image->planes[plane].bo = NULL;
578 image->planes[plane].bo_offset = 0;
579 }
580
581 image->planes[plane].bo = memory->bo;
582 image->planes[plane].bo_offset = memory_offset;
583 }
584
585 VkResult anv_BindImageMemory(
586 VkDevice _device,
587 VkImage _image,
588 VkDeviceMemory _memory,
589 VkDeviceSize memoryOffset)
590 {
591 ANV_FROM_HANDLE(anv_device, device, _device);
592 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
593 ANV_FROM_HANDLE(anv_image, image, _image);
594
595 uint32_t aspect_bit;
596 anv_foreach_image_aspect_bit(aspect_bit, image, image->aspects) {
597 uint32_t plane =
598 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
599 anv_image_bind_memory_plane(device, image, plane, mem, memoryOffset);
600 }
601
602 return VK_SUCCESS;
603 }
604
605 VkResult anv_BindImageMemory2KHR(
606 VkDevice _device,
607 uint32_t bindInfoCount,
608 const VkBindImageMemoryInfoKHR* pBindInfos)
609 {
610 ANV_FROM_HANDLE(anv_device, device, _device);
611
612 for (uint32_t i = 0; i < bindInfoCount; i++) {
613 const VkBindImageMemoryInfoKHR *bind_info = &pBindInfos[i];
614 ANV_FROM_HANDLE(anv_device_memory, mem, bind_info->memory);
615 ANV_FROM_HANDLE(anv_image, image, bind_info->image);
616 uint32_t aspect_bit;
617
618 anv_foreach_image_aspect_bit(aspect_bit, image, image->aspects) {
619 uint32_t plane =
620 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
621 anv_image_bind_memory_plane(device, image, plane,
622 mem, bind_info->memoryOffset);
623 }
624 }
625
626 return VK_SUCCESS;
627 }
628
629 static void
630 anv_surface_get_subresource_layout(struct anv_image *image,
631 struct anv_surface *surface,
632 const VkImageSubresource *subresource,
633 VkSubresourceLayout *layout)
634 {
635 /* If we are on a non-zero mip level or array slice, we need to
636 * calculate a real offset.
637 */
638 anv_assert(subresource->mipLevel == 0);
639 anv_assert(subresource->arrayLayer == 0);
640
641 layout->offset = surface->offset;
642 layout->rowPitch = surface->isl.row_pitch;
643 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
644 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
645 layout->size = surface->isl.size;
646 }
647
648 void anv_GetImageSubresourceLayout(
649 VkDevice device,
650 VkImage _image,
651 const VkImageSubresource* pSubresource,
652 VkSubresourceLayout* pLayout)
653 {
654 ANV_FROM_HANDLE(anv_image, image, _image);
655
656 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
657
658 anv_surface_get_subresource_layout(image,
659 get_surface(image,
660 pSubresource->aspectMask),
661 pSubresource, pLayout);
662 }
663
664 /**
665 * This function determines the optimal buffer to use for a given
666 * VkImageLayout and other pieces of information needed to make that
667 * determination. This does not determine the optimal buffer to use
668 * during a resolve operation.
669 *
670 * @param devinfo The device information of the Intel GPU.
671 * @param image The image that may contain a collection of buffers.
672 * @param plane The plane of the image to be accessed.
673 * @param layout The current layout of the image aspect(s).
674 *
675 * @return The primary buffer that should be used for the given layout.
676 */
677 enum isl_aux_usage
678 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
679 const struct anv_image * const image,
680 const VkImageAspectFlagBits aspect,
681 const VkImageLayout layout)
682 {
683 /* Validate the inputs. */
684
685 /* The devinfo is needed as the optimal buffer varies across generations. */
686 assert(devinfo != NULL);
687
688 /* The layout of a NULL image is not properly defined. */
689 assert(image != NULL);
690
691 /* The aspect must be exactly one of the image aspects. */
692 assert(_mesa_bitcount(aspect) == 1 && (aspect & image->aspects));
693
694 /* Determine the optimal buffer. */
695
696 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
697
698 /* If there is no auxiliary surface allocated, we must use the one and only
699 * main buffer.
700 */
701 if (image->planes[plane].aux_surface.isl.size == 0)
702 return ISL_AUX_USAGE_NONE;
703
704 /* All images that use an auxiliary surface are required to be tiled. */
705 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
706
707 /* Stencil has no aux */
708 assert(aspect != VK_IMAGE_ASPECT_STENCIL_BIT);
709
710 /* The following switch currently only handles depth stencil aspects.
711 * TODO: Handle the color aspect.
712 */
713 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT)
714 return image->planes[plane].aux_usage;
715
716 switch (layout) {
717
718 /* Invalid Layouts */
719 case VK_IMAGE_LAYOUT_RANGE_SIZE:
720 case VK_IMAGE_LAYOUT_MAX_ENUM:
721 unreachable("Invalid image layout.");
722
723 /* Undefined layouts
724 *
725 * The pre-initialized layout is equivalent to the undefined layout for
726 * optimally-tiled images. We can only do color compression (CCS or HiZ)
727 * on tiled images.
728 */
729 case VK_IMAGE_LAYOUT_UNDEFINED:
730 case VK_IMAGE_LAYOUT_PREINITIALIZED:
731 return ISL_AUX_USAGE_NONE;
732
733
734 /* Transfer Layouts
735 *
736 * This buffer could be a depth buffer used in a transfer operation. BLORP
737 * currently doesn't use HiZ for transfer operations so we must use the main
738 * buffer for this layout. TODO: Enable HiZ in BLORP.
739 */
740 case VK_IMAGE_LAYOUT_GENERAL:
741 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
742 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
743 return ISL_AUX_USAGE_NONE;
744
745
746 /* Sampling Layouts */
747 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
748 assert((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
749 /* Fall-through */
750 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
751 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL_KHR:
752 assert(aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
753 if (anv_can_sample_with_hiz(devinfo, image))
754 return ISL_AUX_USAGE_HIZ;
755 else
756 return ISL_AUX_USAGE_NONE;
757
758 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
759 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
760
761 /* On SKL+, the render buffer can be decompressed by the presentation
762 * engine. Support for this feature has not yet landed in the wider
763 * ecosystem. TODO: Update this code when support lands.
764 *
765 * From the BDW PRM, Vol 7, Render Target Resolve:
766 *
767 * If the MCS is enabled on a non-multisampled render target, the
768 * render target must be resolved before being used for other
769 * purposes (display, texture, CPU lock) The clear value from
770 * SURFACE_STATE is written into pixels in the render target
771 * indicated as clear in the MCS.
772 *
773 * Pre-SKL, the render buffer must be resolved before being used for
774 * presentation. We can infer that the auxiliary buffer is not used.
775 */
776 return ISL_AUX_USAGE_NONE;
777
778
779 /* Rendering Layouts */
780 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
781 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
782 unreachable("Color images are not yet supported.");
783
784 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
785 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR:
786 assert(aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
787 return ISL_AUX_USAGE_HIZ;
788
789 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
790 unreachable("VK_KHR_shared_presentable_image is unsupported");
791 }
792
793 /* If the layout isn't recognized in the exhaustive switch above, the
794 * VkImageLayout value is not defined in vulkan.h.
795 */
796 unreachable("layout is not a VkImageLayout enumeration member.");
797 }
798
799
800 static struct anv_state
801 alloc_surface_state(struct anv_device *device)
802 {
803 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
804 }
805
806 static enum isl_channel_select
807 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
808 struct isl_swizzle format_swizzle)
809 {
810 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
811 swizzle = component;
812
813 switch (swizzle) {
814 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
815 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
816 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
817 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
818 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
819 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
820 default:
821 unreachable("Invalid swizzle");
822 }
823 }
824
825 void
826 anv_image_fill_surface_state(struct anv_device *device,
827 const struct anv_image *image,
828 VkImageAspectFlagBits aspect,
829 const struct isl_view *view_in,
830 isl_surf_usage_flags_t view_usage,
831 enum isl_aux_usage aux_usage,
832 const union isl_color_value *clear_color,
833 enum anv_image_view_state_flags flags,
834 struct anv_surface_state *state_inout,
835 struct brw_image_param *image_param_out)
836 {
837 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
838
839 const struct anv_surface *surface = &image->planes[plane].surface,
840 *aux_surface = &image->planes[plane].aux_surface;
841
842 struct isl_view view = *view_in;
843 view.usage |= view_usage;
844
845 /* For texturing with VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL from a
846 * compressed surface with a shadow surface, we use the shadow instead of
847 * the primary surface. The shadow surface will be tiled, unlike the main
848 * surface, so it should get significantly better performance.
849 */
850 if (image->planes[plane].shadow_surface.isl.size > 0 &&
851 isl_format_is_compressed(view.format) &&
852 (flags & ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL)) {
853 assert(isl_format_is_compressed(surface->isl.format));
854 assert(surface->isl.tiling == ISL_TILING_LINEAR);
855 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
856 surface = &image->planes[plane].shadow_surface;
857 }
858
859 if (view_usage == ISL_SURF_USAGE_RENDER_TARGET_BIT)
860 view.swizzle = anv_swizzle_for_render(view.swizzle);
861
862 /* If this is a HiZ buffer we can sample from with a programmable clear
863 * value (SKL+), define the clear value to the optimal constant.
864 */
865 union isl_color_value default_clear_color = { .u32 = { 0, } };
866 if (device->info.gen >= 9 && aux_usage == ISL_AUX_USAGE_HIZ)
867 default_clear_color.f32[0] = ANV_HZ_FC_VAL;
868 if (!clear_color)
869 clear_color = &default_clear_color;
870
871 const uint64_t address = image->planes[plane].bo_offset + surface->offset;
872 const uint64_t aux_address = aux_usage == ISL_AUX_USAGE_NONE ?
873 0 : (image->planes[plane].bo_offset + aux_surface->offset);
874
875 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
876 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY) &&
877 !isl_has_matching_typed_storage_image_format(&device->info,
878 view.format)) {
879 /* In this case, we are a writeable storage buffer which needs to be
880 * lowered to linear. All tiling and offset calculations will be done in
881 * the shader.
882 */
883 assert(aux_usage == ISL_AUX_USAGE_NONE);
884 isl_buffer_fill_state(&device->isl_dev, state_inout->state.map,
885 .address = address,
886 .size = surface->isl.size,
887 .format = ISL_FORMAT_RAW,
888 .stride = 1,
889 .mocs = device->default_mocs);
890 state_inout->address = address,
891 state_inout->aux_address = 0;
892 } else {
893 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
894 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY)) {
895 /* Typed surface reads support a very limited subset of the shader
896 * image formats. Translate it into the closest format the hardware
897 * supports.
898 */
899 assert(aux_usage == ISL_AUX_USAGE_NONE);
900 view.format = isl_lower_storage_image_format(&device->info,
901 view.format);
902 }
903
904 const struct isl_surf *isl_surf = &surface->isl;
905
906 struct isl_surf tmp_surf;
907 uint32_t offset_B = 0, tile_x_sa = 0, tile_y_sa = 0;
908 if (isl_format_is_compressed(surface->isl.format) &&
909 !isl_format_is_compressed(view.format)) {
910 /* We're creating an uncompressed view of a compressed surface. This
911 * is allowed but only for a single level/layer.
912 */
913 assert(surface->isl.samples == 1);
914 assert(view.levels == 1);
915 assert(view.array_len == 1);
916
917 isl_surf_get_image_surf(&device->isl_dev, isl_surf,
918 view.base_level,
919 surface->isl.dim == ISL_SURF_DIM_3D ?
920 0 : view.base_array_layer,
921 surface->isl.dim == ISL_SURF_DIM_3D ?
922 view.base_array_layer : 0,
923 &tmp_surf,
924 &offset_B, &tile_x_sa, &tile_y_sa);
925
926 /* The newly created image represents the one subimage we're
927 * referencing with this view so it only has one array slice and
928 * miplevel.
929 */
930 view.base_array_layer = 0;
931 view.base_level = 0;
932
933 /* We're making an uncompressed view here. The image dimensions need
934 * to be scaled down by the block size.
935 */
936 const struct isl_format_layout *fmtl =
937 isl_format_get_layout(surface->isl.format);
938 tmp_surf.format = view.format;
939 tmp_surf.logical_level0_px.width =
940 DIV_ROUND_UP(tmp_surf.logical_level0_px.width, fmtl->bw);
941 tmp_surf.logical_level0_px.height =
942 DIV_ROUND_UP(tmp_surf.logical_level0_px.height, fmtl->bh);
943 tmp_surf.phys_level0_sa.width /= fmtl->bw;
944 tmp_surf.phys_level0_sa.height /= fmtl->bh;
945 tile_x_sa /= fmtl->bw;
946 tile_y_sa /= fmtl->bh;
947
948 isl_surf = &tmp_surf;
949
950 if (device->info.gen <= 8) {
951 assert(surface->isl.tiling == ISL_TILING_LINEAR);
952 assert(tile_x_sa == 0);
953 assert(tile_y_sa == 0);
954 }
955 }
956
957 isl_surf_fill_state(&device->isl_dev, state_inout->state.map,
958 .surf = isl_surf,
959 .view = &view,
960 .address = address + offset_B,
961 .clear_color = *clear_color,
962 .aux_surf = &aux_surface->isl,
963 .aux_usage = aux_usage,
964 .aux_address = aux_address,
965 .mocs = device->default_mocs,
966 .x_offset_sa = tile_x_sa,
967 .y_offset_sa = tile_y_sa);
968 state_inout->address = address + offset_B;
969 if (device->info.gen >= 8) {
970 state_inout->aux_address = aux_address;
971 } else {
972 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
973 * used to store other information. This should be ok, however,
974 * because surface buffer addresses are always 4K page alinged.
975 */
976 uint32_t *aux_addr_dw = state_inout->state.map +
977 device->isl_dev.ss.aux_addr_offset;
978 assert((aux_address & 0xfff) == 0);
979 assert(aux_address == (*aux_addr_dw & 0xfffff000));
980 state_inout->aux_address = *aux_addr_dw;
981 }
982 }
983
984 anv_state_flush(device, state_inout->state);
985
986 if (image_param_out) {
987 assert(view_usage == ISL_SURF_USAGE_STORAGE_BIT);
988 isl_surf_fill_image_param(&device->isl_dev, image_param_out,
989 &surface->isl, &view);
990 }
991 }
992
993 static VkImageAspectFlags
994 remap_aspect_flags(VkImageAspectFlags view_aspects)
995 {
996 if (view_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT) {
997 if (_mesa_bitcount(view_aspects) == 1)
998 return VK_IMAGE_ASPECT_COLOR_BIT;
999
1000 VkImageAspectFlags color_aspects = 0;
1001 for (uint32_t i = 0; i < _mesa_bitcount(view_aspects); i++)
1002 color_aspects |= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR << i;
1003 return color_aspects;
1004 }
1005 /* No special remapping needed for depth & stencil aspects. */
1006 return view_aspects;
1007 }
1008
1009 VkResult
1010 anv_CreateImageView(VkDevice _device,
1011 const VkImageViewCreateInfo *pCreateInfo,
1012 const VkAllocationCallbacks *pAllocator,
1013 VkImageView *pView)
1014 {
1015 ANV_FROM_HANDLE(anv_device, device, _device);
1016 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
1017 struct anv_image_view *iview;
1018
1019 iview = vk_zalloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
1020 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1021 if (iview == NULL)
1022 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1023
1024 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
1025
1026 assert(range->layerCount > 0);
1027 assert(range->baseMipLevel < image->levels);
1028
1029 const VkImageViewUsageCreateInfoKHR *usage_info =
1030 vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO_KHR);
1031 VkImageUsageFlags view_usage = usage_info ? usage_info->usage : image->usage;
1032 /* View usage should be a subset of image usage */
1033 assert((view_usage & ~image->usage) == 0);
1034 assert(view_usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
1035 VK_IMAGE_USAGE_STORAGE_BIT |
1036 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1037 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
1038 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
1039
1040 switch (image->type) {
1041 default:
1042 unreachable("bad VkImageType");
1043 case VK_IMAGE_TYPE_1D:
1044 case VK_IMAGE_TYPE_2D:
1045 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
1046 break;
1047 case VK_IMAGE_TYPE_3D:
1048 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
1049 <= anv_minify(image->extent.depth, range->baseMipLevel));
1050 break;
1051 }
1052
1053 /* First expand aspects to the image's ones (for example
1054 * VK_IMAGE_ASPECT_COLOR_BIT will be converted to
1055 * VK_IMAGE_ASPECT_PLANE_0_BIT_KHR | VK_IMAGE_ASPECT_PLANE_1_BIT_KHR |
1056 * VK_IMAGE_ASPECT_PLANE_2_BIT_KHR for an image of format
1057 * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR.
1058 */
1059 VkImageAspectFlags expanded_aspects =
1060 anv_image_expand_aspects(image, range->aspectMask);
1061
1062 iview->image = image;
1063
1064 /* Remap the expanded aspects for the image view. For example if only
1065 * VK_IMAGE_ASPECT_PLANE_1_BIT_KHR was given in range->aspectMask, we will
1066 * convert it to VK_IMAGE_ASPECT_COLOR_BIT since from the point of view of
1067 * the image view, it only has a single plane.
1068 */
1069 iview->aspect_mask = remap_aspect_flags(expanded_aspects);
1070 iview->n_planes = anv_image_aspect_get_planes(iview->aspect_mask);
1071 iview->vk_format = pCreateInfo->format;
1072
1073 iview->extent = (VkExtent3D) {
1074 .width = anv_minify(image->extent.width , range->baseMipLevel),
1075 .height = anv_minify(image->extent.height, range->baseMipLevel),
1076 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
1077 };
1078
1079 /* Now go through the underlying image selected planes (computed in
1080 * expanded_aspects) and map them to planes in the image view.
1081 */
1082 uint32_t iaspect_bit, vplane = 0;
1083 anv_foreach_image_aspect_bit(iaspect_bit, image, expanded_aspects) {
1084 uint32_t iplane =
1085 anv_image_aspect_to_plane(expanded_aspects, 1UL << iaspect_bit);
1086 VkImageAspectFlags vplane_aspect =
1087 anv_plane_to_aspect(iview->aspect_mask, vplane);
1088 struct anv_format_plane format =
1089 anv_get_format_plane(&device->info, pCreateInfo->format,
1090 vplane_aspect, image->tiling);
1091
1092 iview->planes[vplane].image_plane = iplane;
1093
1094 iview->planes[vplane].isl = (struct isl_view) {
1095 .format = format.isl_format,
1096 .base_level = range->baseMipLevel,
1097 .levels = anv_get_levelCount(image, range),
1098 .base_array_layer = range->baseArrayLayer,
1099 .array_len = anv_get_layerCount(image, range),
1100 .swizzle = {
1101 .r = remap_swizzle(pCreateInfo->components.r,
1102 VK_COMPONENT_SWIZZLE_R, format.swizzle),
1103 .g = remap_swizzle(pCreateInfo->components.g,
1104 VK_COMPONENT_SWIZZLE_G, format.swizzle),
1105 .b = remap_swizzle(pCreateInfo->components.b,
1106 VK_COMPONENT_SWIZZLE_B, format.swizzle),
1107 .a = remap_swizzle(pCreateInfo->components.a,
1108 VK_COMPONENT_SWIZZLE_A, format.swizzle),
1109 },
1110 };
1111
1112 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
1113 iview->planes[vplane].isl.base_array_layer = 0;
1114 iview->planes[vplane].isl.array_len = iview->extent.depth;
1115 }
1116
1117 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
1118 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
1119 iview->planes[vplane].isl.usage = ISL_SURF_USAGE_CUBE_BIT;
1120 } else {
1121 iview->planes[vplane].isl.usage = 0;
1122 }
1123
1124 if (view_usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
1125 (view_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
1126 !(iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT))) {
1127 iview->planes[vplane].optimal_sampler_surface_state.state = alloc_surface_state(device);
1128 iview->planes[vplane].general_sampler_surface_state.state = alloc_surface_state(device);
1129
1130 enum isl_aux_usage general_aux_usage =
1131 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1132 VK_IMAGE_LAYOUT_GENERAL);
1133 enum isl_aux_usage optimal_aux_usage =
1134 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1135 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1136
1137 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1138 &iview->planes[vplane].isl,
1139 ISL_SURF_USAGE_TEXTURE_BIT,
1140 optimal_aux_usage, NULL,
1141 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL,
1142 &iview->planes[vplane].optimal_sampler_surface_state,
1143 NULL);
1144
1145 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1146 &iview->planes[vplane].isl,
1147 ISL_SURF_USAGE_TEXTURE_BIT,
1148 general_aux_usage, NULL,
1149 0,
1150 &iview->planes[vplane].general_sampler_surface_state,
1151 NULL);
1152 }
1153
1154 /* NOTE: This one needs to go last since it may stomp isl_view.format */
1155 if (view_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1156 iview->planes[vplane].storage_surface_state.state = alloc_surface_state(device);
1157 iview->planes[vplane].writeonly_storage_surface_state.state = alloc_surface_state(device);
1158
1159 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1160 &iview->planes[vplane].isl,
1161 ISL_SURF_USAGE_STORAGE_BIT,
1162 ISL_AUX_USAGE_NONE, NULL,
1163 0,
1164 &iview->planes[vplane].storage_surface_state,
1165 &iview->planes[vplane].storage_image_param);
1166
1167 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1168 &iview->planes[vplane].isl,
1169 ISL_SURF_USAGE_STORAGE_BIT,
1170 ISL_AUX_USAGE_NONE, NULL,
1171 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY,
1172 &iview->planes[vplane].writeonly_storage_surface_state,
1173 NULL);
1174 }
1175
1176 vplane++;
1177 }
1178
1179 *pView = anv_image_view_to_handle(iview);
1180
1181 return VK_SUCCESS;
1182 }
1183
1184 void
1185 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
1186 const VkAllocationCallbacks *pAllocator)
1187 {
1188 ANV_FROM_HANDLE(anv_device, device, _device);
1189 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
1190
1191 if (!iview)
1192 return;
1193
1194 for (uint32_t plane = 0; plane < iview->n_planes; plane++) {
1195 if (iview->planes[plane].optimal_sampler_surface_state.state.alloc_size > 0) {
1196 anv_state_pool_free(&device->surface_state_pool,
1197 iview->planes[plane].optimal_sampler_surface_state.state);
1198 }
1199
1200 if (iview->planes[plane].general_sampler_surface_state.state.alloc_size > 0) {
1201 anv_state_pool_free(&device->surface_state_pool,
1202 iview->planes[plane].general_sampler_surface_state.state);
1203 }
1204
1205 if (iview->planes[plane].storage_surface_state.state.alloc_size > 0) {
1206 anv_state_pool_free(&device->surface_state_pool,
1207 iview->planes[plane].storage_surface_state.state);
1208 }
1209
1210 if (iview->planes[plane].writeonly_storage_surface_state.state.alloc_size > 0) {
1211 anv_state_pool_free(&device->surface_state_pool,
1212 iview->planes[plane].writeonly_storage_surface_state.state);
1213 }
1214 }
1215
1216 vk_free2(&device->alloc, pAllocator, iview);
1217 }
1218
1219
1220 VkResult
1221 anv_CreateBufferView(VkDevice _device,
1222 const VkBufferViewCreateInfo *pCreateInfo,
1223 const VkAllocationCallbacks *pAllocator,
1224 VkBufferView *pView)
1225 {
1226 ANV_FROM_HANDLE(anv_device, device, _device);
1227 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
1228 struct anv_buffer_view *view;
1229
1230 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
1231 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1232 if (!view)
1233 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1234
1235 /* TODO: Handle the format swizzle? */
1236
1237 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
1238 VK_IMAGE_ASPECT_COLOR_BIT,
1239 VK_IMAGE_TILING_LINEAR);
1240 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
1241 view->bo = buffer->bo;
1242 view->offset = buffer->offset + pCreateInfo->offset;
1243 view->range = anv_buffer_get_range(buffer, pCreateInfo->offset,
1244 pCreateInfo->range);
1245 view->range = align_down_npot_u32(view->range, format_bs);
1246
1247 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
1248 view->surface_state = alloc_surface_state(device);
1249
1250 anv_fill_buffer_surface_state(device, view->surface_state,
1251 view->format,
1252 view->offset, view->range, format_bs);
1253 } else {
1254 view->surface_state = (struct anv_state){ 0 };
1255 }
1256
1257 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
1258 view->storage_surface_state = alloc_surface_state(device);
1259 view->writeonly_storage_surface_state = alloc_surface_state(device);
1260
1261 enum isl_format storage_format =
1262 isl_has_matching_typed_storage_image_format(&device->info,
1263 view->format) ?
1264 isl_lower_storage_image_format(&device->info, view->format) :
1265 ISL_FORMAT_RAW;
1266
1267 anv_fill_buffer_surface_state(device, view->storage_surface_state,
1268 storage_format,
1269 view->offset, view->range,
1270 (storage_format == ISL_FORMAT_RAW ? 1 :
1271 isl_format_get_layout(storage_format)->bpb / 8));
1272
1273 /* Write-only accesses should use the original format. */
1274 anv_fill_buffer_surface_state(device, view->writeonly_storage_surface_state,
1275 view->format,
1276 view->offset, view->range,
1277 isl_format_get_layout(view->format)->bpb / 8);
1278
1279 isl_buffer_fill_image_param(&device->isl_dev,
1280 &view->storage_image_param,
1281 view->format, view->range);
1282 } else {
1283 view->storage_surface_state = (struct anv_state){ 0 };
1284 view->writeonly_storage_surface_state = (struct anv_state){ 0 };
1285 }
1286
1287 *pView = anv_buffer_view_to_handle(view);
1288
1289 return VK_SUCCESS;
1290 }
1291
1292 void
1293 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
1294 const VkAllocationCallbacks *pAllocator)
1295 {
1296 ANV_FROM_HANDLE(anv_device, device, _device);
1297 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
1298
1299 if (!view)
1300 return;
1301
1302 if (view->surface_state.alloc_size > 0)
1303 anv_state_pool_free(&device->surface_state_pool,
1304 view->surface_state);
1305
1306 if (view->storage_surface_state.alloc_size > 0)
1307 anv_state_pool_free(&device->surface_state_pool,
1308 view->storage_surface_state);
1309
1310 if (view->writeonly_storage_surface_state.alloc_size > 0)
1311 anv_state_pool_free(&device->surface_state_pool,
1312 view->writeonly_storage_surface_state);
1313
1314 vk_free2(&device->alloc, pAllocator, view);
1315 }
1316
1317 const struct anv_surface *
1318 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
1319 VkImageAspectFlags aspect_mask)
1320 {
1321 VkImageAspectFlags sanitized_mask;
1322
1323 switch (aspect_mask) {
1324 case VK_IMAGE_ASPECT_COLOR_BIT:
1325 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1326 sanitized_mask = VK_IMAGE_ASPECT_COLOR_BIT;
1327 break;
1328 case VK_IMAGE_ASPECT_DEPTH_BIT:
1329 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
1330 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1331 break;
1332 case VK_IMAGE_ASPECT_STENCIL_BIT:
1333 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
1334 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1335 break;
1336 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
1337 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1338 * combined depth stencil formats. Specifically, it states:
1339 *
1340 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1341 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1342 *
1343 * Image views with both depth and stencil aspects are only valid for
1344 * render target attachments, in which case
1345 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1346 * stencil surfaces from the underlying surface.
1347 */
1348 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1349 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1350 } else {
1351 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
1352 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1353 }
1354 break;
1355 case VK_IMAGE_ASPECT_PLANE_0_BIT_KHR:
1356 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
1357 sanitized_mask = VK_IMAGE_ASPECT_PLANE_0_BIT_KHR;
1358 break;
1359 case VK_IMAGE_ASPECT_PLANE_1_BIT_KHR:
1360 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
1361 sanitized_mask = VK_IMAGE_ASPECT_PLANE_1_BIT_KHR;
1362 break;
1363 case VK_IMAGE_ASPECT_PLANE_2_BIT_KHR:
1364 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
1365 sanitized_mask = VK_IMAGE_ASPECT_PLANE_2_BIT_KHR;
1366 break;
1367 default:
1368 unreachable("image does not have aspect");
1369 return NULL;
1370 }
1371
1372 uint32_t plane = anv_image_aspect_to_plane(image->aspects, sanitized_mask);
1373 return &image->planes[plane].surface;
1374 }