anv: fix null pointer dereference
[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 image->disjoint = pCreateInfo->flags & VK_IMAGE_CREATE_DISJOINT_BIT_KHR;
520
521 const struct anv_format *format = anv_get_format(image->vk_format);
522 assert(format != NULL);
523
524 image->n_planes = format->n_planes;
525
526 uint32_t b;
527 for_each_bit(b, image->aspects) {
528 r = make_surface(device, image, create_info, (1 << b));
529 if (r != VK_SUCCESS)
530 goto fail;
531 }
532
533 *pImage = anv_image_to_handle(image);
534
535 return VK_SUCCESS;
536
537 fail:
538 if (image)
539 vk_free2(&device->alloc, alloc, image);
540
541 return r;
542 }
543
544 VkResult
545 anv_CreateImage(VkDevice device,
546 const VkImageCreateInfo *pCreateInfo,
547 const VkAllocationCallbacks *pAllocator,
548 VkImage *pImage)
549 {
550 return anv_image_create(device,
551 &(struct anv_image_create_info) {
552 .vk_info = pCreateInfo,
553 },
554 pAllocator,
555 pImage);
556 }
557
558 void
559 anv_DestroyImage(VkDevice _device, VkImage _image,
560 const VkAllocationCallbacks *pAllocator)
561 {
562 ANV_FROM_HANDLE(anv_device, device, _device);
563 ANV_FROM_HANDLE(anv_image, image, _image);
564
565 if (!image)
566 return;
567
568 vk_free2(&device->alloc, pAllocator, image);
569 }
570
571 static void anv_image_bind_memory_plane(struct anv_device *device,
572 struct anv_image *image,
573 uint32_t plane,
574 struct anv_device_memory *memory,
575 uint32_t memory_offset)
576 {
577 if (!memory) {
578 image->planes[plane].bo = NULL;
579 image->planes[plane].bo_offset = 0;
580 return;
581 }
582
583 image->planes[plane].bo = memory->bo;
584 image->planes[plane].bo_offset = memory_offset;
585 }
586
587 VkResult anv_BindImageMemory(
588 VkDevice _device,
589 VkImage _image,
590 VkDeviceMemory _memory,
591 VkDeviceSize memoryOffset)
592 {
593 ANV_FROM_HANDLE(anv_device, device, _device);
594 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
595 ANV_FROM_HANDLE(anv_image, image, _image);
596
597 uint32_t aspect_bit;
598 anv_foreach_image_aspect_bit(aspect_bit, image, image->aspects) {
599 uint32_t plane =
600 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
601 anv_image_bind_memory_plane(device, image, plane, mem, memoryOffset);
602 }
603
604 return VK_SUCCESS;
605 }
606
607 VkResult anv_BindImageMemory2KHR(
608 VkDevice _device,
609 uint32_t bindInfoCount,
610 const VkBindImageMemoryInfoKHR* pBindInfos)
611 {
612 ANV_FROM_HANDLE(anv_device, device, _device);
613
614 for (uint32_t i = 0; i < bindInfoCount; i++) {
615 const VkBindImageMemoryInfoKHR *bind_info = &pBindInfos[i];
616 ANV_FROM_HANDLE(anv_device_memory, mem, bind_info->memory);
617 ANV_FROM_HANDLE(anv_image, image, bind_info->image);
618 VkImageAspectFlags aspects = image->aspects;
619
620 vk_foreach_struct_const(s, bind_info->pNext) {
621 switch (s->sType) {
622 case VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO_KHR: {
623 const VkBindImagePlaneMemoryInfoKHR *plane_info =
624 (const VkBindImagePlaneMemoryInfoKHR *) s;
625
626 aspects = plane_info->planeAspect;
627 break;
628 }
629 default:
630 anv_debug_ignored_stype(s->sType);
631 break;
632 }
633 }
634
635 uint32_t aspect_bit;
636 anv_foreach_image_aspect_bit(aspect_bit, image, aspects) {
637 uint32_t plane =
638 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
639 anv_image_bind_memory_plane(device, image, plane,
640 mem, bind_info->memoryOffset);
641 }
642 }
643
644 return VK_SUCCESS;
645 }
646
647 static void
648 anv_surface_get_subresource_layout(struct anv_image *image,
649 struct anv_surface *surface,
650 const VkImageSubresource *subresource,
651 VkSubresourceLayout *layout)
652 {
653 /* If we are on a non-zero mip level or array slice, we need to
654 * calculate a real offset.
655 */
656 anv_assert(subresource->mipLevel == 0);
657 anv_assert(subresource->arrayLayer == 0);
658
659 layout->offset = surface->offset;
660 layout->rowPitch = surface->isl.row_pitch;
661 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
662 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
663 layout->size = surface->isl.size;
664 }
665
666 void anv_GetImageSubresourceLayout(
667 VkDevice device,
668 VkImage _image,
669 const VkImageSubresource* pSubresource,
670 VkSubresourceLayout* pLayout)
671 {
672 ANV_FROM_HANDLE(anv_image, image, _image);
673
674 assert(__builtin_popcount(pSubresource->aspectMask) == 1);
675
676 anv_surface_get_subresource_layout(image,
677 get_surface(image,
678 pSubresource->aspectMask),
679 pSubresource, pLayout);
680 }
681
682 /**
683 * This function determines the optimal buffer to use for a given
684 * VkImageLayout and other pieces of information needed to make that
685 * determination. This does not determine the optimal buffer to use
686 * during a resolve operation.
687 *
688 * @param devinfo The device information of the Intel GPU.
689 * @param image The image that may contain a collection of buffers.
690 * @param plane The plane of the image to be accessed.
691 * @param layout The current layout of the image aspect(s).
692 *
693 * @return The primary buffer that should be used for the given layout.
694 */
695 enum isl_aux_usage
696 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
697 const struct anv_image * const image,
698 const VkImageAspectFlagBits aspect,
699 const VkImageLayout layout)
700 {
701 /* Validate the inputs. */
702
703 /* The devinfo is needed as the optimal buffer varies across generations. */
704 assert(devinfo != NULL);
705
706 /* The layout of a NULL image is not properly defined. */
707 assert(image != NULL);
708
709 /* The aspect must be exactly one of the image aspects. */
710 assert(_mesa_bitcount(aspect) == 1 && (aspect & image->aspects));
711
712 /* Determine the optimal buffer. */
713
714 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
715
716 /* If there is no auxiliary surface allocated, we must use the one and only
717 * main buffer.
718 */
719 if (image->planes[plane].aux_surface.isl.size == 0)
720 return ISL_AUX_USAGE_NONE;
721
722 /* All images that use an auxiliary surface are required to be tiled. */
723 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
724
725 /* Stencil has no aux */
726 assert(aspect != VK_IMAGE_ASPECT_STENCIL_BIT);
727
728 /* The following switch currently only handles depth stencil aspects.
729 * TODO: Handle the color aspect.
730 */
731 if (image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT)
732 return image->planes[plane].aux_usage;
733
734 switch (layout) {
735
736 /* Invalid Layouts */
737 case VK_IMAGE_LAYOUT_RANGE_SIZE:
738 case VK_IMAGE_LAYOUT_MAX_ENUM:
739 unreachable("Invalid image layout.");
740
741 /* Undefined layouts
742 *
743 * The pre-initialized layout is equivalent to the undefined layout for
744 * optimally-tiled images. We can only do color compression (CCS or HiZ)
745 * on tiled images.
746 */
747 case VK_IMAGE_LAYOUT_UNDEFINED:
748 case VK_IMAGE_LAYOUT_PREINITIALIZED:
749 return ISL_AUX_USAGE_NONE;
750
751
752 /* Transfer Layouts
753 *
754 * This buffer could be a depth buffer used in a transfer operation. BLORP
755 * currently doesn't use HiZ for transfer operations so we must use the main
756 * buffer for this layout. TODO: Enable HiZ in BLORP.
757 */
758 case VK_IMAGE_LAYOUT_GENERAL:
759 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
760 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
761 return ISL_AUX_USAGE_NONE;
762
763
764 /* Sampling Layouts */
765 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
766 assert((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
767 /* Fall-through */
768 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
769 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL_KHR:
770 assert(aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
771 if (anv_can_sample_with_hiz(devinfo, image))
772 return ISL_AUX_USAGE_HIZ;
773 else
774 return ISL_AUX_USAGE_NONE;
775
776 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
777 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
778
779 /* On SKL+, the render buffer can be decompressed by the presentation
780 * engine. Support for this feature has not yet landed in the wider
781 * ecosystem. TODO: Update this code when support lands.
782 *
783 * From the BDW PRM, Vol 7, Render Target Resolve:
784 *
785 * If the MCS is enabled on a non-multisampled render target, the
786 * render target must be resolved before being used for other
787 * purposes (display, texture, CPU lock) The clear value from
788 * SURFACE_STATE is written into pixels in the render target
789 * indicated as clear in the MCS.
790 *
791 * Pre-SKL, the render buffer must be resolved before being used for
792 * presentation. We can infer that the auxiliary buffer is not used.
793 */
794 return ISL_AUX_USAGE_NONE;
795
796
797 /* Rendering Layouts */
798 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
799 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
800 unreachable("Color images are not yet supported.");
801
802 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
803 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR:
804 assert(aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
805 return ISL_AUX_USAGE_HIZ;
806
807 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
808 unreachable("VK_KHR_shared_presentable_image is unsupported");
809 }
810
811 /* If the layout isn't recognized in the exhaustive switch above, the
812 * VkImageLayout value is not defined in vulkan.h.
813 */
814 unreachable("layout is not a VkImageLayout enumeration member.");
815 }
816
817
818 static struct anv_state
819 alloc_surface_state(struct anv_device *device)
820 {
821 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
822 }
823
824 static enum isl_channel_select
825 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
826 struct isl_swizzle format_swizzle)
827 {
828 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
829 swizzle = component;
830
831 switch (swizzle) {
832 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
833 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
834 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
835 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
836 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
837 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
838 default:
839 unreachable("Invalid swizzle");
840 }
841 }
842
843 void
844 anv_image_fill_surface_state(struct anv_device *device,
845 const struct anv_image *image,
846 VkImageAspectFlagBits aspect,
847 const struct isl_view *view_in,
848 isl_surf_usage_flags_t view_usage,
849 enum isl_aux_usage aux_usage,
850 const union isl_color_value *clear_color,
851 enum anv_image_view_state_flags flags,
852 struct anv_surface_state *state_inout,
853 struct brw_image_param *image_param_out)
854 {
855 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
856
857 const struct anv_surface *surface = &image->planes[plane].surface,
858 *aux_surface = &image->planes[plane].aux_surface;
859
860 struct isl_view view = *view_in;
861 view.usage |= view_usage;
862
863 /* For texturing with VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL from a
864 * compressed surface with a shadow surface, we use the shadow instead of
865 * the primary surface. The shadow surface will be tiled, unlike the main
866 * surface, so it should get significantly better performance.
867 */
868 if (image->planes[plane].shadow_surface.isl.size > 0 &&
869 isl_format_is_compressed(view.format) &&
870 (flags & ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL)) {
871 assert(isl_format_is_compressed(surface->isl.format));
872 assert(surface->isl.tiling == ISL_TILING_LINEAR);
873 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
874 surface = &image->planes[plane].shadow_surface;
875 }
876
877 if (view_usage == ISL_SURF_USAGE_RENDER_TARGET_BIT)
878 view.swizzle = anv_swizzle_for_render(view.swizzle);
879
880 /* If this is a HiZ buffer we can sample from with a programmable clear
881 * value (SKL+), define the clear value to the optimal constant.
882 */
883 union isl_color_value default_clear_color = { .u32 = { 0, } };
884 if (device->info.gen >= 9 && aux_usage == ISL_AUX_USAGE_HIZ)
885 default_clear_color.f32[0] = ANV_HZ_FC_VAL;
886 if (!clear_color)
887 clear_color = &default_clear_color;
888
889 const uint64_t address = image->planes[plane].bo_offset + surface->offset;
890 const uint64_t aux_address = aux_usage == ISL_AUX_USAGE_NONE ?
891 0 : (image->planes[plane].bo_offset + aux_surface->offset);
892
893 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
894 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY) &&
895 !isl_has_matching_typed_storage_image_format(&device->info,
896 view.format)) {
897 /* In this case, we are a writeable storage buffer which needs to be
898 * lowered to linear. All tiling and offset calculations will be done in
899 * the shader.
900 */
901 assert(aux_usage == ISL_AUX_USAGE_NONE);
902 isl_buffer_fill_state(&device->isl_dev, state_inout->state.map,
903 .address = address,
904 .size = surface->isl.size,
905 .format = ISL_FORMAT_RAW,
906 .stride = 1,
907 .mocs = device->default_mocs);
908 state_inout->address = address,
909 state_inout->aux_address = 0;
910 } else {
911 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
912 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY)) {
913 /* Typed surface reads support a very limited subset of the shader
914 * image formats. Translate it into the closest format the hardware
915 * supports.
916 */
917 assert(aux_usage == ISL_AUX_USAGE_NONE);
918 view.format = isl_lower_storage_image_format(&device->info,
919 view.format);
920 }
921
922 const struct isl_surf *isl_surf = &surface->isl;
923
924 struct isl_surf tmp_surf;
925 uint32_t offset_B = 0, tile_x_sa = 0, tile_y_sa = 0;
926 if (isl_format_is_compressed(surface->isl.format) &&
927 !isl_format_is_compressed(view.format)) {
928 /* We're creating an uncompressed view of a compressed surface. This
929 * is allowed but only for a single level/layer.
930 */
931 assert(surface->isl.samples == 1);
932 assert(view.levels == 1);
933 assert(view.array_len == 1);
934
935 isl_surf_get_image_surf(&device->isl_dev, isl_surf,
936 view.base_level,
937 surface->isl.dim == ISL_SURF_DIM_3D ?
938 0 : view.base_array_layer,
939 surface->isl.dim == ISL_SURF_DIM_3D ?
940 view.base_array_layer : 0,
941 &tmp_surf,
942 &offset_B, &tile_x_sa, &tile_y_sa);
943
944 /* The newly created image represents the one subimage we're
945 * referencing with this view so it only has one array slice and
946 * miplevel.
947 */
948 view.base_array_layer = 0;
949 view.base_level = 0;
950
951 /* We're making an uncompressed view here. The image dimensions need
952 * to be scaled down by the block size.
953 */
954 const struct isl_format_layout *fmtl =
955 isl_format_get_layout(surface->isl.format);
956 tmp_surf.format = view.format;
957 tmp_surf.logical_level0_px.width =
958 DIV_ROUND_UP(tmp_surf.logical_level0_px.width, fmtl->bw);
959 tmp_surf.logical_level0_px.height =
960 DIV_ROUND_UP(tmp_surf.logical_level0_px.height, fmtl->bh);
961 tmp_surf.phys_level0_sa.width /= fmtl->bw;
962 tmp_surf.phys_level0_sa.height /= fmtl->bh;
963 tile_x_sa /= fmtl->bw;
964 tile_y_sa /= fmtl->bh;
965
966 isl_surf = &tmp_surf;
967
968 if (device->info.gen <= 8) {
969 assert(surface->isl.tiling == ISL_TILING_LINEAR);
970 assert(tile_x_sa == 0);
971 assert(tile_y_sa == 0);
972 }
973 }
974
975 isl_surf_fill_state(&device->isl_dev, state_inout->state.map,
976 .surf = isl_surf,
977 .view = &view,
978 .address = address + offset_B,
979 .clear_color = *clear_color,
980 .aux_surf = &aux_surface->isl,
981 .aux_usage = aux_usage,
982 .aux_address = aux_address,
983 .mocs = device->default_mocs,
984 .x_offset_sa = tile_x_sa,
985 .y_offset_sa = tile_y_sa);
986 state_inout->address = address + offset_B;
987 if (device->info.gen >= 8) {
988 state_inout->aux_address = aux_address;
989 } else {
990 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
991 * used to store other information. This should be ok, however,
992 * because surface buffer addresses are always 4K page alinged.
993 */
994 uint32_t *aux_addr_dw = state_inout->state.map +
995 device->isl_dev.ss.aux_addr_offset;
996 assert((aux_address & 0xfff) == 0);
997 assert(aux_address == (*aux_addr_dw & 0xfffff000));
998 state_inout->aux_address = *aux_addr_dw;
999 }
1000 }
1001
1002 anv_state_flush(device, state_inout->state);
1003
1004 if (image_param_out) {
1005 assert(view_usage == ISL_SURF_USAGE_STORAGE_BIT);
1006 isl_surf_fill_image_param(&device->isl_dev, image_param_out,
1007 &surface->isl, &view);
1008 }
1009 }
1010
1011 static VkImageAspectFlags
1012 remap_aspect_flags(VkImageAspectFlags view_aspects)
1013 {
1014 if (view_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT) {
1015 if (_mesa_bitcount(view_aspects) == 1)
1016 return VK_IMAGE_ASPECT_COLOR_BIT;
1017
1018 VkImageAspectFlags color_aspects = 0;
1019 for (uint32_t i = 0; i < _mesa_bitcount(view_aspects); i++)
1020 color_aspects |= VK_IMAGE_ASPECT_PLANE_0_BIT_KHR << i;
1021 return color_aspects;
1022 }
1023 /* No special remapping needed for depth & stencil aspects. */
1024 return view_aspects;
1025 }
1026
1027 VkResult
1028 anv_CreateImageView(VkDevice _device,
1029 const VkImageViewCreateInfo *pCreateInfo,
1030 const VkAllocationCallbacks *pAllocator,
1031 VkImageView *pView)
1032 {
1033 ANV_FROM_HANDLE(anv_device, device, _device);
1034 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
1035 struct anv_image_view *iview;
1036
1037 iview = vk_zalloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
1038 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1039 if (iview == NULL)
1040 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1041
1042 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
1043
1044 assert(range->layerCount > 0);
1045 assert(range->baseMipLevel < image->levels);
1046
1047 const VkImageViewUsageCreateInfoKHR *usage_info =
1048 vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO_KHR);
1049 VkImageUsageFlags view_usage = usage_info ? usage_info->usage : image->usage;
1050 /* View usage should be a subset of image usage */
1051 assert((view_usage & ~image->usage) == 0);
1052 assert(view_usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
1053 VK_IMAGE_USAGE_STORAGE_BIT |
1054 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1055 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
1056 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
1057
1058 switch (image->type) {
1059 default:
1060 unreachable("bad VkImageType");
1061 case VK_IMAGE_TYPE_1D:
1062 case VK_IMAGE_TYPE_2D:
1063 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
1064 break;
1065 case VK_IMAGE_TYPE_3D:
1066 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
1067 <= anv_minify(image->extent.depth, range->baseMipLevel));
1068 break;
1069 }
1070
1071 /* First expand aspects to the image's ones (for example
1072 * VK_IMAGE_ASPECT_COLOR_BIT will be converted to
1073 * VK_IMAGE_ASPECT_PLANE_0_BIT_KHR | VK_IMAGE_ASPECT_PLANE_1_BIT_KHR |
1074 * VK_IMAGE_ASPECT_PLANE_2_BIT_KHR for an image of format
1075 * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR.
1076 */
1077 VkImageAspectFlags expanded_aspects =
1078 anv_image_expand_aspects(image, range->aspectMask);
1079
1080 iview->image = image;
1081
1082 /* Remap the expanded aspects for the image view. For example if only
1083 * VK_IMAGE_ASPECT_PLANE_1_BIT_KHR was given in range->aspectMask, we will
1084 * convert it to VK_IMAGE_ASPECT_COLOR_BIT since from the point of view of
1085 * the image view, it only has a single plane.
1086 */
1087 iview->aspect_mask = remap_aspect_flags(expanded_aspects);
1088 iview->n_planes = anv_image_aspect_get_planes(iview->aspect_mask);
1089 iview->vk_format = pCreateInfo->format;
1090
1091 iview->extent = (VkExtent3D) {
1092 .width = anv_minify(image->extent.width , range->baseMipLevel),
1093 .height = anv_minify(image->extent.height, range->baseMipLevel),
1094 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
1095 };
1096
1097 /* Now go through the underlying image selected planes (computed in
1098 * expanded_aspects) and map them to planes in the image view.
1099 */
1100 uint32_t iaspect_bit, vplane = 0;
1101 anv_foreach_image_aspect_bit(iaspect_bit, image, expanded_aspects) {
1102 uint32_t iplane =
1103 anv_image_aspect_to_plane(expanded_aspects, 1UL << iaspect_bit);
1104 VkImageAspectFlags vplane_aspect =
1105 anv_plane_to_aspect(iview->aspect_mask, vplane);
1106 struct anv_format_plane format =
1107 anv_get_format_plane(&device->info, pCreateInfo->format,
1108 vplane_aspect, image->tiling);
1109
1110 iview->planes[vplane].image_plane = iplane;
1111
1112 iview->planes[vplane].isl = (struct isl_view) {
1113 .format = format.isl_format,
1114 .base_level = range->baseMipLevel,
1115 .levels = anv_get_levelCount(image, range),
1116 .base_array_layer = range->baseArrayLayer,
1117 .array_len = anv_get_layerCount(image, range),
1118 .swizzle = {
1119 .r = remap_swizzle(pCreateInfo->components.r,
1120 VK_COMPONENT_SWIZZLE_R, format.swizzle),
1121 .g = remap_swizzle(pCreateInfo->components.g,
1122 VK_COMPONENT_SWIZZLE_G, format.swizzle),
1123 .b = remap_swizzle(pCreateInfo->components.b,
1124 VK_COMPONENT_SWIZZLE_B, format.swizzle),
1125 .a = remap_swizzle(pCreateInfo->components.a,
1126 VK_COMPONENT_SWIZZLE_A, format.swizzle),
1127 },
1128 };
1129
1130 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
1131 iview->planes[vplane].isl.base_array_layer = 0;
1132 iview->planes[vplane].isl.array_len = iview->extent.depth;
1133 }
1134
1135 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
1136 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
1137 iview->planes[vplane].isl.usage = ISL_SURF_USAGE_CUBE_BIT;
1138 } else {
1139 iview->planes[vplane].isl.usage = 0;
1140 }
1141
1142 if (view_usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
1143 (view_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
1144 !(iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT))) {
1145 iview->planes[vplane].optimal_sampler_surface_state.state = alloc_surface_state(device);
1146 iview->planes[vplane].general_sampler_surface_state.state = alloc_surface_state(device);
1147
1148 enum isl_aux_usage general_aux_usage =
1149 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1150 VK_IMAGE_LAYOUT_GENERAL);
1151 enum isl_aux_usage optimal_aux_usage =
1152 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1153 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1154
1155 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1156 &iview->planes[vplane].isl,
1157 ISL_SURF_USAGE_TEXTURE_BIT,
1158 optimal_aux_usage, NULL,
1159 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL,
1160 &iview->planes[vplane].optimal_sampler_surface_state,
1161 NULL);
1162
1163 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1164 &iview->planes[vplane].isl,
1165 ISL_SURF_USAGE_TEXTURE_BIT,
1166 general_aux_usage, NULL,
1167 0,
1168 &iview->planes[vplane].general_sampler_surface_state,
1169 NULL);
1170 }
1171
1172 /* NOTE: This one needs to go last since it may stomp isl_view.format */
1173 if (view_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1174 iview->planes[vplane].storage_surface_state.state = alloc_surface_state(device);
1175 iview->planes[vplane].writeonly_storage_surface_state.state = alloc_surface_state(device);
1176
1177 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1178 &iview->planes[vplane].isl,
1179 ISL_SURF_USAGE_STORAGE_BIT,
1180 ISL_AUX_USAGE_NONE, NULL,
1181 0,
1182 &iview->planes[vplane].storage_surface_state,
1183 &iview->planes[vplane].storage_image_param);
1184
1185 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1186 &iview->planes[vplane].isl,
1187 ISL_SURF_USAGE_STORAGE_BIT,
1188 ISL_AUX_USAGE_NONE, NULL,
1189 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY,
1190 &iview->planes[vplane].writeonly_storage_surface_state,
1191 NULL);
1192 }
1193
1194 vplane++;
1195 }
1196
1197 *pView = anv_image_view_to_handle(iview);
1198
1199 return VK_SUCCESS;
1200 }
1201
1202 void
1203 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
1204 const VkAllocationCallbacks *pAllocator)
1205 {
1206 ANV_FROM_HANDLE(anv_device, device, _device);
1207 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
1208
1209 if (!iview)
1210 return;
1211
1212 for (uint32_t plane = 0; plane < iview->n_planes; plane++) {
1213 if (iview->planes[plane].optimal_sampler_surface_state.state.alloc_size > 0) {
1214 anv_state_pool_free(&device->surface_state_pool,
1215 iview->planes[plane].optimal_sampler_surface_state.state);
1216 }
1217
1218 if (iview->planes[plane].general_sampler_surface_state.state.alloc_size > 0) {
1219 anv_state_pool_free(&device->surface_state_pool,
1220 iview->planes[plane].general_sampler_surface_state.state);
1221 }
1222
1223 if (iview->planes[plane].storage_surface_state.state.alloc_size > 0) {
1224 anv_state_pool_free(&device->surface_state_pool,
1225 iview->planes[plane].storage_surface_state.state);
1226 }
1227
1228 if (iview->planes[plane].writeonly_storage_surface_state.state.alloc_size > 0) {
1229 anv_state_pool_free(&device->surface_state_pool,
1230 iview->planes[plane].writeonly_storage_surface_state.state);
1231 }
1232 }
1233
1234 vk_free2(&device->alloc, pAllocator, iview);
1235 }
1236
1237
1238 VkResult
1239 anv_CreateBufferView(VkDevice _device,
1240 const VkBufferViewCreateInfo *pCreateInfo,
1241 const VkAllocationCallbacks *pAllocator,
1242 VkBufferView *pView)
1243 {
1244 ANV_FROM_HANDLE(anv_device, device, _device);
1245 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
1246 struct anv_buffer_view *view;
1247
1248 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
1249 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1250 if (!view)
1251 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1252
1253 /* TODO: Handle the format swizzle? */
1254
1255 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
1256 VK_IMAGE_ASPECT_COLOR_BIT,
1257 VK_IMAGE_TILING_LINEAR);
1258 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
1259 view->bo = buffer->bo;
1260 view->offset = buffer->offset + pCreateInfo->offset;
1261 view->range = anv_buffer_get_range(buffer, pCreateInfo->offset,
1262 pCreateInfo->range);
1263 view->range = align_down_npot_u32(view->range, format_bs);
1264
1265 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
1266 view->surface_state = alloc_surface_state(device);
1267
1268 anv_fill_buffer_surface_state(device, view->surface_state,
1269 view->format,
1270 view->offset, view->range, format_bs);
1271 } else {
1272 view->surface_state = (struct anv_state){ 0 };
1273 }
1274
1275 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
1276 view->storage_surface_state = alloc_surface_state(device);
1277 view->writeonly_storage_surface_state = alloc_surface_state(device);
1278
1279 enum isl_format storage_format =
1280 isl_has_matching_typed_storage_image_format(&device->info,
1281 view->format) ?
1282 isl_lower_storage_image_format(&device->info, view->format) :
1283 ISL_FORMAT_RAW;
1284
1285 anv_fill_buffer_surface_state(device, view->storage_surface_state,
1286 storage_format,
1287 view->offset, view->range,
1288 (storage_format == ISL_FORMAT_RAW ? 1 :
1289 isl_format_get_layout(storage_format)->bpb / 8));
1290
1291 /* Write-only accesses should use the original format. */
1292 anv_fill_buffer_surface_state(device, view->writeonly_storage_surface_state,
1293 view->format,
1294 view->offset, view->range,
1295 isl_format_get_layout(view->format)->bpb / 8);
1296
1297 isl_buffer_fill_image_param(&device->isl_dev,
1298 &view->storage_image_param,
1299 view->format, view->range);
1300 } else {
1301 view->storage_surface_state = (struct anv_state){ 0 };
1302 view->writeonly_storage_surface_state = (struct anv_state){ 0 };
1303 }
1304
1305 *pView = anv_buffer_view_to_handle(view);
1306
1307 return VK_SUCCESS;
1308 }
1309
1310 void
1311 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
1312 const VkAllocationCallbacks *pAllocator)
1313 {
1314 ANV_FROM_HANDLE(anv_device, device, _device);
1315 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
1316
1317 if (!view)
1318 return;
1319
1320 if (view->surface_state.alloc_size > 0)
1321 anv_state_pool_free(&device->surface_state_pool,
1322 view->surface_state);
1323
1324 if (view->storage_surface_state.alloc_size > 0)
1325 anv_state_pool_free(&device->surface_state_pool,
1326 view->storage_surface_state);
1327
1328 if (view->writeonly_storage_surface_state.alloc_size > 0)
1329 anv_state_pool_free(&device->surface_state_pool,
1330 view->writeonly_storage_surface_state);
1331
1332 vk_free2(&device->alloc, pAllocator, view);
1333 }
1334
1335 const struct anv_surface *
1336 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
1337 VkImageAspectFlags aspect_mask)
1338 {
1339 VkImageAspectFlags sanitized_mask;
1340
1341 switch (aspect_mask) {
1342 case VK_IMAGE_ASPECT_COLOR_BIT:
1343 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1344 sanitized_mask = VK_IMAGE_ASPECT_COLOR_BIT;
1345 break;
1346 case VK_IMAGE_ASPECT_DEPTH_BIT:
1347 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
1348 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1349 break;
1350 case VK_IMAGE_ASPECT_STENCIL_BIT:
1351 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
1352 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1353 break;
1354 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
1355 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1356 * combined depth stencil formats. Specifically, it states:
1357 *
1358 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1359 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1360 *
1361 * Image views with both depth and stencil aspects are only valid for
1362 * render target attachments, in which case
1363 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1364 * stencil surfaces from the underlying surface.
1365 */
1366 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1367 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1368 } else {
1369 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
1370 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1371 }
1372 break;
1373 case VK_IMAGE_ASPECT_PLANE_0_BIT_KHR:
1374 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
1375 sanitized_mask = VK_IMAGE_ASPECT_PLANE_0_BIT_KHR;
1376 break;
1377 case VK_IMAGE_ASPECT_PLANE_1_BIT_KHR:
1378 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
1379 sanitized_mask = VK_IMAGE_ASPECT_PLANE_1_BIT_KHR;
1380 break;
1381 case VK_IMAGE_ASPECT_PLANE_2_BIT_KHR:
1382 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT) == 0);
1383 sanitized_mask = VK_IMAGE_ASPECT_PLANE_2_BIT_KHR;
1384 break;
1385 default:
1386 unreachable("image does not have aspect");
1387 return NULL;
1388 }
1389
1390 uint32_t plane = anv_image_aspect_to_plane(image->aspects, sanitized_mask);
1391 return &image->planes[plane].surface;
1392 }