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