anv/gen12: Write GFX_AUX_TABLE base address register
[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 #include "drm-uapi/drm_fourcc.h"
31
32 #include "anv_private.h"
33 #include "util/debug.h"
34 #include "vk_util.h"
35 #include "util/u_math.h"
36
37 #include "vk_format_info.h"
38
39 static isl_surf_usage_flags_t
40 choose_isl_surf_usage(VkImageCreateFlags vk_create_flags,
41 VkImageUsageFlags vk_usage,
42 isl_surf_usage_flags_t isl_extra_usage,
43 VkImageAspectFlagBits aspect)
44 {
45 isl_surf_usage_flags_t isl_usage = isl_extra_usage;
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:
72 case VK_IMAGE_ASPECT_PLANE_1_BIT:
73 case VK_IMAGE_ASPECT_PLANE_2_BIT:
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 static isl_tiling_flags_t
96 choose_isl_tiling_flags(const struct anv_image_create_info *anv_info,
97 const struct isl_drm_modifier_info *isl_mod_info,
98 bool legacy_scanout)
99 {
100 const VkImageCreateInfo *base_info = anv_info->vk_info;
101 isl_tiling_flags_t flags = 0;
102
103 switch (base_info->tiling) {
104 default:
105 unreachable("bad VkImageTiling");
106 case VK_IMAGE_TILING_OPTIMAL:
107 flags = ISL_TILING_ANY_MASK;
108 break;
109 case VK_IMAGE_TILING_LINEAR:
110 flags = ISL_TILING_LINEAR_BIT;
111 break;
112 }
113
114 if (anv_info->isl_tiling_flags)
115 flags &= anv_info->isl_tiling_flags;
116
117 if (legacy_scanout)
118 flags &= ISL_TILING_LINEAR_BIT | ISL_TILING_X_BIT;
119
120 if (isl_mod_info)
121 flags &= 1 << isl_mod_info->tiling;
122
123 assert(flags);
124
125 return flags;
126 }
127
128 static struct anv_surface *
129 get_surface(struct anv_image *image, VkImageAspectFlagBits aspect)
130 {
131 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
132 return &image->planes[plane].surface;
133 }
134
135 static void
136 add_surface(struct anv_image *image, struct anv_surface *surf, uint32_t plane)
137 {
138 assert(surf->isl.size_B > 0); /* isl surface must be initialized */
139
140 if (image->disjoint) {
141 surf->offset = align_u32(image->planes[plane].size,
142 surf->isl.alignment_B);
143 /* Plane offset is always 0 when it's disjoint. */
144 } else {
145 surf->offset = align_u32(image->size, surf->isl.alignment_B);
146 /* Determine plane's offset only once when the first surface is added. */
147 if (image->planes[plane].size == 0)
148 image->planes[plane].offset = image->size;
149 }
150
151 image->size = surf->offset + surf->isl.size_B;
152 image->planes[plane].size = (surf->offset + surf->isl.size_B) - image->planes[plane].offset;
153
154 image->alignment = MAX2(image->alignment, surf->isl.alignment_B);
155 image->planes[plane].alignment = MAX2(image->planes[plane].alignment,
156 surf->isl.alignment_B);
157 }
158
159
160 static bool
161 all_formats_ccs_e_compatible(const struct gen_device_info *devinfo,
162 const VkImageFormatListCreateInfoKHR *fmt_list,
163 struct anv_image *image)
164 {
165 enum isl_format format =
166 anv_get_isl_format(devinfo, image->vk_format,
167 VK_IMAGE_ASPECT_COLOR_BIT, image->tiling);
168
169 if (!isl_format_supports_ccs_e(devinfo, format))
170 return false;
171
172 if (!(image->create_flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
173 return true;
174
175 if (!fmt_list || fmt_list->viewFormatCount == 0)
176 return false;
177
178 for (uint32_t i = 0; i < fmt_list->viewFormatCount; i++) {
179 enum isl_format view_format =
180 anv_get_isl_format(devinfo, fmt_list->pViewFormats[i],
181 VK_IMAGE_ASPECT_COLOR_BIT, image->tiling);
182
183 if (!isl_formats_are_ccs_e_compatible(devinfo, format, view_format))
184 return false;
185 }
186
187 return true;
188 }
189
190 /**
191 * For color images that have an auxiliary surface, request allocation for an
192 * additional buffer that mainly stores fast-clear values. Use of this buffer
193 * allows us to access the image's subresources while being aware of their
194 * fast-clear values in non-trivial cases (e.g., outside of a render pass in
195 * which a fast clear has occurred).
196 *
197 * In order to avoid having multiple clear colors for a single plane of an
198 * image (hence a single RENDER_SURFACE_STATE), we only allow fast-clears on
199 * the first slice (level 0, layer 0). At the time of our testing (Jan 17,
200 * 2018), there were no known applications which would benefit from fast-
201 * clearing more than just the first slice.
202 *
203 * The fast clear portion of the image is laid out in the following order:
204 *
205 * * 1 or 4 dwords (depending on hardware generation) for the clear color
206 * * 1 dword for the anv_fast_clear_type of the clear color
207 * * On gen9+, 1 dword per level and layer of the image (3D levels count
208 * multiple layers) in level-major order for compression state.
209 *
210 * For the purpose of discoverability, the algorithm used to manage
211 * compression and fast-clears is described here:
212 *
213 * * On a transition from UNDEFINED or PREINITIALIZED to a defined layout,
214 * all of the values in the fast clear portion of the image are initialized
215 * to default values.
216 *
217 * * On fast-clear, the clear value is written into surface state and also
218 * into the buffer and the fast clear type is set appropriately. Both
219 * setting the fast-clear value in the buffer and setting the fast-clear
220 * type happen from the GPU using MI commands.
221 *
222 * * Whenever a render or blorp operation is performed with CCS_E, we call
223 * genX(cmd_buffer_mark_image_written) to set the compression state to
224 * true (which is represented by UINT32_MAX).
225 *
226 * * On pipeline barrier transitions, the worst-case transition is computed
227 * from the image layouts. The command streamer inspects the fast clear
228 * type and compression state dwords and constructs a predicate. The
229 * worst-case resolve is performed with the given predicate and the fast
230 * clear and compression state is set accordingly.
231 *
232 * See anv_layout_to_aux_usage and anv_layout_to_fast_clear_type functions for
233 * details on exactly what is allowed in what layouts.
234 *
235 * On gen7-9, we do not have a concept of indirect clear colors in hardware.
236 * In order to deal with this, we have to do some clear color management.
237 *
238 * * For LOAD_OP_LOAD at the top of a renderpass, we have to copy the clear
239 * value from the buffer into the surface state with MI commands.
240 *
241 * * For any blorp operations, we pass the address to the clear value into
242 * blorp and it knows to copy the clear color.
243 */
244 static void
245 add_aux_state_tracking_buffer(struct anv_image *image,
246 uint32_t plane,
247 const struct anv_device *device)
248 {
249 assert(image && device);
250 assert(image->planes[plane].aux_surface.isl.size_B > 0 &&
251 image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
252
253 /* Compressed images must be tiled and therefore everything should be 4K
254 * aligned. The CCS has the same alignment requirements. This is good
255 * because we need at least dword-alignment for MI_LOAD/STORE operations.
256 */
257 assert(image->alignment % 4 == 0);
258 assert((image->planes[plane].offset + image->planes[plane].size) % 4 == 0);
259
260 /* This buffer should be at the very end of the plane. */
261 if (image->disjoint) {
262 assert(image->planes[plane].size ==
263 (image->planes[plane].offset + image->planes[plane].size));
264 } else {
265 assert(image->size ==
266 (image->planes[plane].offset + image->planes[plane].size));
267 }
268
269 const unsigned clear_color_state_size = device->info.gen >= 10 ?
270 device->isl_dev.ss.clear_color_state_size :
271 device->isl_dev.ss.clear_value_size;
272
273 /* Clear color and fast clear type */
274 unsigned state_size = clear_color_state_size + 4;
275
276 /* We only need to track compression on CCS_E surfaces. */
277 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E) {
278 if (image->type == VK_IMAGE_TYPE_3D) {
279 for (uint32_t l = 0; l < image->levels; l++)
280 state_size += anv_minify(image->extent.depth, l) * 4;
281 } else {
282 state_size += image->levels * image->array_size * 4;
283 }
284 }
285
286 image->planes[plane].fast_clear_state_offset =
287 image->planes[plane].offset + image->planes[plane].size;
288
289 image->planes[plane].size += state_size;
290 image->size += state_size;
291 }
292
293 /**
294 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
295 * image's memory requirements (that is, the image's size and alignment).
296 */
297 static VkResult
298 make_surface(const struct anv_device *dev,
299 struct anv_image *image,
300 uint32_t stride,
301 isl_tiling_flags_t tiling_flags,
302 isl_surf_usage_flags_t isl_extra_usage_flags,
303 VkImageAspectFlagBits aspect)
304 {
305 bool ok;
306
307 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
308 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
309 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
310 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
311 };
312
313 image->extent = anv_sanitize_image_extent(image->type, image->extent);
314
315 const unsigned plane = anv_image_aspect_to_plane(image->aspects, aspect);
316 const struct anv_format_plane plane_format =
317 anv_get_format_plane(&dev->info, image->vk_format, aspect, image->tiling);
318 struct anv_surface *anv_surf = &image->planes[plane].surface;
319
320 const isl_surf_usage_flags_t usage =
321 choose_isl_surf_usage(image->create_flags, image->usage,
322 isl_extra_usage_flags, aspect);
323
324 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
325 * fall back to linear on Broadwell and earlier because we aren't
326 * guaranteed that we can handle offsets correctly. On Sky Lake, the
327 * horizontal and vertical alignments are sufficiently high that we can
328 * just use RENDER_SURFACE_STATE::X/Y Offset.
329 */
330 bool needs_shadow = false;
331 isl_surf_usage_flags_t shadow_usage = 0;
332 if (dev->info.gen <= 8 &&
333 (image->create_flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
334 image->tiling == VK_IMAGE_TILING_OPTIMAL) {
335 assert(isl_format_is_compressed(plane_format.isl_format));
336 tiling_flags = ISL_TILING_LINEAR_BIT;
337 needs_shadow = true;
338 shadow_usage = ISL_SURF_USAGE_TEXTURE_BIT |
339 (usage & ISL_SURF_USAGE_CUBE_BIT);
340 }
341
342 if (dev->info.gen <= 7 &&
343 aspect == VK_IMAGE_ASPECT_STENCIL_BIT &&
344 (image->stencil_usage & VK_IMAGE_USAGE_SAMPLED_BIT)) {
345 needs_shadow = true;
346 shadow_usage = ISL_SURF_USAGE_TEXTURE_BIT |
347 (usage & ISL_SURF_USAGE_CUBE_BIT);
348 }
349
350 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
351 .dim = vk_to_isl_surf_dim[image->type],
352 .format = plane_format.isl_format,
353 .width = image->extent.width / plane_format.denominator_scales[0],
354 .height = image->extent.height / plane_format.denominator_scales[1],
355 .depth = image->extent.depth,
356 .levels = image->levels,
357 .array_len = image->array_size,
358 .samples = image->samples,
359 .min_alignment_B = 0,
360 .row_pitch_B = stride,
361 .usage = usage,
362 .tiling_flags = tiling_flags);
363
364 if (!ok)
365 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
366
367 image->planes[plane].aux_usage = ISL_AUX_USAGE_NONE;
368
369 add_surface(image, anv_surf, plane);
370
371 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
372 * create an identical tiled shadow surface for use while texturing so we
373 * don't get garbage performance. If we're on gen7 and the image contains
374 * stencil, then we need to maintain a shadow because we can't texture from
375 * W-tiled images.
376 */
377 if (needs_shadow) {
378 ok = isl_surf_init(&dev->isl_dev, &image->planes[plane].shadow_surface.isl,
379 .dim = vk_to_isl_surf_dim[image->type],
380 .format = plane_format.isl_format,
381 .width = image->extent.width,
382 .height = image->extent.height,
383 .depth = image->extent.depth,
384 .levels = image->levels,
385 .array_len = image->array_size,
386 .samples = image->samples,
387 .min_alignment_B = 0,
388 .row_pitch_B = stride,
389 .usage = shadow_usage,
390 .tiling_flags = ISL_TILING_ANY_MASK);
391
392 /* isl_surf_init() will fail only if provided invalid input. Invalid input
393 * is illegal in Vulkan.
394 */
395 assert(ok);
396
397 add_surface(image, &image->planes[plane].shadow_surface, plane);
398 }
399
400 /* Add a HiZ surface to a depth buffer that will be used for rendering.
401 */
402 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
403 /* We don't advertise that depth buffers could be used as storage
404 * images.
405 */
406 assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
407
408 /* Allow the user to control HiZ enabling. Disable by default on gen7
409 * because resolves are not currently implemented pre-BDW.
410 */
411 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
412 /* It will never be used as an attachment, HiZ is pointless. */
413 } else if (dev->info.gen == 7) {
414 anv_perf_warn(dev->instance, image, "Implement gen7 HiZ");
415 } else if (image->levels > 1) {
416 anv_perf_warn(dev->instance, image, "Enable multi-LOD HiZ");
417 } else if (image->array_size > 1) {
418 anv_perf_warn(dev->instance, image,
419 "Implement multi-arrayLayer HiZ clears and resolves");
420 } else if (dev->info.gen == 8 && image->samples > 1) {
421 anv_perf_warn(dev->instance, image, "Enable gen8 multisampled HiZ");
422 } else if (!unlikely(INTEL_DEBUG & DEBUG_NO_HIZ)) {
423 assert(image->planes[plane].aux_surface.isl.size_B == 0);
424 ok = isl_surf_get_hiz_surf(&dev->isl_dev,
425 &image->planes[plane].surface.isl,
426 &image->planes[plane].aux_surface.isl);
427 assert(ok);
428 add_surface(image, &image->planes[plane].aux_surface, plane);
429 image->planes[plane].aux_usage = ISL_AUX_USAGE_HIZ;
430 }
431 } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) && image->samples == 1) {
432 /* TODO: Disallow compression with :
433 *
434 * 1) non multiplanar images (We appear to hit a sampler bug with
435 * CCS & R16G16 format. Putting the clear state a page/4096bytes
436 * further fixes the issue).
437 *
438 * 2) alias images, because they might be aliases of images
439 * described in 1)
440 *
441 * 3) compression disabled by debug
442 */
443 const bool allow_compression =
444 image->n_planes == 1 &&
445 (image->create_flags & VK_IMAGE_CREATE_ALIAS_BIT) == 0 &&
446 likely((INTEL_DEBUG & DEBUG_NO_RBC) == 0);
447
448 if (allow_compression) {
449 assert(image->planes[plane].aux_surface.isl.size_B == 0);
450 ok = isl_surf_get_ccs_surf(&dev->isl_dev,
451 &image->planes[plane].surface.isl,
452 &image->planes[plane].aux_surface.isl, 0);
453 if (ok) {
454
455 /* Disable CCS when it is not useful (i.e., when you can't render
456 * to the image with CCS enabled).
457 */
458 if (!isl_format_supports_rendering(&dev->info,
459 plane_format.isl_format)) {
460 /* While it may be technically possible to enable CCS for this
461 * image, we currently don't have things hooked up to get it
462 * working.
463 */
464 anv_perf_warn(dev->instance, image,
465 "This image format doesn't support rendering. "
466 "Not allocating an CCS buffer.");
467 image->planes[plane].aux_surface.isl.size_B = 0;
468 return VK_SUCCESS;
469 }
470
471 add_surface(image, &image->planes[plane].aux_surface, plane);
472 add_aux_state_tracking_buffer(image, plane, dev);
473
474 /* For images created without MUTABLE_FORMAT_BIT set, we know that
475 * they will always be used with the original format. In
476 * particular, they will always be used with a format that
477 * supports color compression. If it's never used as a storage
478 * image, then it will only be used through the sampler or the as
479 * a render target. This means that it's safe to just leave
480 * compression on at all times for these formats.
481 */
482 if (!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
483 image->ccs_e_compatible) {
484 image->planes[plane].aux_usage = ISL_AUX_USAGE_CCS_E;
485 }
486 }
487 }
488 } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) && image->samples > 1) {
489 assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
490 assert(image->planes[plane].aux_surface.isl.size_B == 0);
491 ok = isl_surf_get_mcs_surf(&dev->isl_dev,
492 &image->planes[plane].surface.isl,
493 &image->planes[plane].aux_surface.isl);
494 if (ok) {
495 add_surface(image, &image->planes[plane].aux_surface, plane);
496 add_aux_state_tracking_buffer(image, plane, dev);
497 image->planes[plane].aux_usage = ISL_AUX_USAGE_MCS;
498 }
499 }
500
501 assert((image->planes[plane].offset + image->planes[plane].size) == image->size);
502
503 /* Upper bound of the last surface should be smaller than the plane's
504 * size.
505 */
506 assert((MAX2(image->planes[plane].surface.offset,
507 image->planes[plane].aux_surface.offset) +
508 (image->planes[plane].aux_surface.isl.size_B > 0 ?
509 image->planes[plane].aux_surface.isl.size_B :
510 image->planes[plane].surface.isl.size_B)) <=
511 (image->planes[plane].offset + image->planes[plane].size));
512
513 if (image->planes[plane].aux_surface.isl.size_B) {
514 /* assert(image->planes[plane].fast_clear_state_offset == */
515 /* (image->planes[plane].aux_surface.offset + image->planes[plane].aux_surface.isl.size_B)); */
516 assert(image->planes[plane].fast_clear_state_offset <
517 (image->planes[plane].offset + image->planes[plane].size));
518 }
519
520 return VK_SUCCESS;
521 }
522
523 static uint32_t
524 score_drm_format_mod(uint64_t modifier)
525 {
526 switch (modifier) {
527 case DRM_FORMAT_MOD_LINEAR: return 1;
528 case I915_FORMAT_MOD_X_TILED: return 2;
529 case I915_FORMAT_MOD_Y_TILED: return 3;
530 case I915_FORMAT_MOD_Y_TILED_CCS: return 4;
531 default: unreachable("bad DRM format modifier");
532 }
533 }
534
535 static const struct isl_drm_modifier_info *
536 choose_drm_format_mod(const struct anv_physical_device *device,
537 uint32_t modifier_count, const uint64_t *modifiers)
538 {
539 uint64_t best_mod = UINT64_MAX;
540 uint32_t best_score = 0;
541
542 for (uint32_t i = 0; i < modifier_count; ++i) {
543 uint32_t score = score_drm_format_mod(modifiers[i]);
544 if (score > best_score) {
545 best_mod = modifiers[i];
546 best_score = score;
547 }
548 }
549
550 if (best_score > 0)
551 return isl_drm_modifier_get_info(best_mod);
552 else
553 return NULL;
554 }
555
556 VkResult
557 anv_image_create(VkDevice _device,
558 const struct anv_image_create_info *create_info,
559 const VkAllocationCallbacks* alloc,
560 VkImage *pImage)
561 {
562 ANV_FROM_HANDLE(anv_device, device, _device);
563 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
564 const struct isl_drm_modifier_info *isl_mod_info = NULL;
565 struct anv_image *image = NULL;
566 VkResult r;
567
568 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
569
570 const struct wsi_image_create_info *wsi_info =
571 vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
572 if (wsi_info && wsi_info->modifier_count > 0) {
573 isl_mod_info = choose_drm_format_mod(&device->instance->physicalDevice,
574 wsi_info->modifier_count,
575 wsi_info->modifiers);
576 assert(isl_mod_info);
577 }
578
579 anv_assert(pCreateInfo->mipLevels > 0);
580 anv_assert(pCreateInfo->arrayLayers > 0);
581 anv_assert(pCreateInfo->samples > 0);
582 anv_assert(pCreateInfo->extent.width > 0);
583 anv_assert(pCreateInfo->extent.height > 0);
584 anv_assert(pCreateInfo->extent.depth > 0);
585
586 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
587 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
588 if (!image)
589 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
590
591 image->type = pCreateInfo->imageType;
592 image->extent = pCreateInfo->extent;
593 image->vk_format = pCreateInfo->format;
594 image->format = anv_get_format(pCreateInfo->format);
595 image->aspects = vk_format_aspects(image->vk_format);
596 image->levels = pCreateInfo->mipLevels;
597 image->array_size = pCreateInfo->arrayLayers;
598 image->samples = pCreateInfo->samples;
599 image->usage = pCreateInfo->usage;
600 image->create_flags = pCreateInfo->flags;
601 image->tiling = pCreateInfo->tiling;
602 image->disjoint = pCreateInfo->flags & VK_IMAGE_CREATE_DISJOINT_BIT;
603 image->needs_set_tiling = wsi_info && wsi_info->scanout;
604 image->drm_format_mod = isl_mod_info ? isl_mod_info->modifier :
605 DRM_FORMAT_MOD_INVALID;
606
607 if (image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
608 image->stencil_usage = pCreateInfo->usage;
609 const VkImageStencilUsageCreateInfoEXT *stencil_usage_info =
610 vk_find_struct_const(pCreateInfo->pNext,
611 IMAGE_STENCIL_USAGE_CREATE_INFO_EXT);
612 if (stencil_usage_info)
613 image->stencil_usage = stencil_usage_info->stencilUsage;
614 }
615
616 /* In case of external format, We don't know format yet,
617 * so skip the rest for now.
618 */
619 if (create_info->external_format) {
620 image->external_format = true;
621 *pImage = anv_image_to_handle(image);
622 return VK_SUCCESS;
623 }
624
625 const struct anv_format *format = anv_get_format(image->vk_format);
626 assert(format != NULL);
627
628 const isl_tiling_flags_t isl_tiling_flags =
629 choose_isl_tiling_flags(create_info, isl_mod_info,
630 image->needs_set_tiling);
631
632 image->n_planes = format->n_planes;
633
634 const VkImageFormatListCreateInfoKHR *fmt_list =
635 vk_find_struct_const(pCreateInfo->pNext,
636 IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
637
638 image->ccs_e_compatible =
639 all_formats_ccs_e_compatible(&device->info, fmt_list, image);
640
641 uint32_t b;
642 for_each_bit(b, image->aspects) {
643 r = make_surface(device, image, create_info->stride, isl_tiling_flags,
644 create_info->isl_extra_usage_flags, (1 << b));
645 if (r != VK_SUCCESS)
646 goto fail;
647 }
648
649 *pImage = anv_image_to_handle(image);
650
651 return VK_SUCCESS;
652
653 fail:
654 if (image)
655 vk_free2(&device->alloc, alloc, image);
656
657 return r;
658 }
659
660 static struct anv_image *
661 anv_swapchain_get_image(VkSwapchainKHR swapchain,
662 uint32_t index)
663 {
664 uint32_t n_images = index + 1;
665 VkImage *images = malloc(sizeof(*images) * n_images);
666 VkResult result = wsi_common_get_images(swapchain, &n_images, images);
667
668 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
669 free(images);
670 return NULL;
671 }
672
673 ANV_FROM_HANDLE(anv_image, image, images[index]);
674 free(images);
675
676 return image;
677 }
678
679 static VkResult
680 anv_image_from_swapchain(VkDevice device,
681 const VkImageCreateInfo *pCreateInfo,
682 const VkImageSwapchainCreateInfoKHR *swapchain_info,
683 const VkAllocationCallbacks *pAllocator,
684 VkImage *pImage)
685 {
686 struct anv_image *swapchain_image = anv_swapchain_get_image(swapchain_info->swapchain, 0);
687 assert(swapchain_image);
688
689 assert(swapchain_image->type == pCreateInfo->imageType);
690 assert(swapchain_image->vk_format == pCreateInfo->format);
691 assert(swapchain_image->extent.width == pCreateInfo->extent.width);
692 assert(swapchain_image->extent.height == pCreateInfo->extent.height);
693 assert(swapchain_image->extent.depth == pCreateInfo->extent.depth);
694 assert(swapchain_image->array_size == pCreateInfo->arrayLayers);
695 /* Color attachment is added by the wsi code. */
696 assert(swapchain_image->usage == (pCreateInfo->usage | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT));
697
698 VkImageCreateInfo local_create_info;
699 local_create_info = *pCreateInfo;
700 local_create_info.pNext = NULL;
701 /* The following parameters are implictly selected by the wsi code. */
702 local_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
703 local_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
704 local_create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
705
706 /* If the image has a particular modifier, specify that modifier. */
707 struct wsi_image_create_info local_wsi_info = {
708 .sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA,
709 .modifier_count = 1,
710 .modifiers = &swapchain_image->drm_format_mod,
711 };
712 if (swapchain_image->drm_format_mod != DRM_FORMAT_MOD_INVALID)
713 __vk_append_struct(&local_create_info, &local_wsi_info);
714
715 return anv_image_create(device,
716 &(struct anv_image_create_info) {
717 .vk_info = &local_create_info,
718 .external_format = swapchain_image->external_format,
719 },
720 pAllocator,
721 pImage);
722 }
723
724 VkResult
725 anv_CreateImage(VkDevice device,
726 const VkImageCreateInfo *pCreateInfo,
727 const VkAllocationCallbacks *pAllocator,
728 VkImage *pImage)
729 {
730 const struct VkExternalMemoryImageCreateInfo *create_info =
731 vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
732
733 if (create_info && (create_info->handleTypes &
734 VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID))
735 return anv_image_from_external(device, pCreateInfo, create_info,
736 pAllocator, pImage);
737
738 bool use_external_format = false;
739 const struct VkExternalFormatANDROID *ext_format =
740 vk_find_struct_const(pCreateInfo->pNext, EXTERNAL_FORMAT_ANDROID);
741
742 /* "If externalFormat is zero, the effect is as if the
743 * VkExternalFormatANDROID structure was not present. Otherwise, the image
744 * will have the specified external format."
745 */
746 if (ext_format && ext_format->externalFormat != 0)
747 use_external_format = true;
748
749 const VkNativeBufferANDROID *gralloc_info =
750 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
751 if (gralloc_info)
752 return anv_image_from_gralloc(device, pCreateInfo, gralloc_info,
753 pAllocator, pImage);
754
755 const VkImageSwapchainCreateInfoKHR *swapchain_info =
756 vk_find_struct_const(pCreateInfo->pNext, IMAGE_SWAPCHAIN_CREATE_INFO_KHR);
757 if (swapchain_info && swapchain_info->swapchain != VK_NULL_HANDLE)
758 return anv_image_from_swapchain(device, pCreateInfo, swapchain_info,
759 pAllocator, pImage);
760
761 return anv_image_create(device,
762 &(struct anv_image_create_info) {
763 .vk_info = pCreateInfo,
764 .external_format = use_external_format,
765 },
766 pAllocator,
767 pImage);
768 }
769
770 void
771 anv_DestroyImage(VkDevice _device, VkImage _image,
772 const VkAllocationCallbacks *pAllocator)
773 {
774 ANV_FROM_HANDLE(anv_device, device, _device);
775 ANV_FROM_HANDLE(anv_image, image, _image);
776
777 if (!image)
778 return;
779
780 for (uint32_t p = 0; p < image->n_planes; ++p) {
781 if (image->planes[p].bo_is_owned) {
782 assert(image->planes[p].address.bo != NULL);
783 anv_bo_cache_release(device, &device->bo_cache,
784 image->planes[p].address.bo);
785 }
786 }
787
788 vk_free2(&device->alloc, pAllocator, image);
789 }
790
791 static void anv_image_bind_memory_plane(struct anv_device *device,
792 struct anv_image *image,
793 uint32_t plane,
794 struct anv_device_memory *memory,
795 uint32_t memory_offset)
796 {
797 assert(!image->planes[plane].bo_is_owned);
798
799 if (!memory) {
800 image->planes[plane].address = ANV_NULL_ADDRESS;
801 return;
802 }
803
804 image->planes[plane].address = (struct anv_address) {
805 .bo = memory->bo,
806 .offset = memory_offset,
807 };
808 }
809
810 /* We are binding AHardwareBuffer. Get a description, resolve the
811 * format and prepare anv_image properly.
812 */
813 static void
814 resolve_ahw_image(struct anv_device *device,
815 struct anv_image *image,
816 struct anv_device_memory *mem)
817 {
818 #if defined(ANDROID) && ANDROID_API_LEVEL >= 26
819 assert(mem->ahw);
820 AHardwareBuffer_Desc desc;
821 AHardwareBuffer_describe(mem->ahw, &desc);
822
823 /* Check tiling. */
824 int i915_tiling = anv_gem_get_tiling(device, mem->bo->gem_handle);
825 VkImageTiling vk_tiling;
826 isl_tiling_flags_t isl_tiling_flags = 0;
827
828 switch (i915_tiling) {
829 case I915_TILING_NONE:
830 vk_tiling = VK_IMAGE_TILING_LINEAR;
831 isl_tiling_flags = ISL_TILING_LINEAR_BIT;
832 break;
833 case I915_TILING_X:
834 vk_tiling = VK_IMAGE_TILING_OPTIMAL;
835 isl_tiling_flags = ISL_TILING_X_BIT;
836 break;
837 case I915_TILING_Y:
838 vk_tiling = VK_IMAGE_TILING_OPTIMAL;
839 isl_tiling_flags = ISL_TILING_Y0_BIT;
840 break;
841 case -1:
842 default:
843 unreachable("Invalid tiling flags.");
844 }
845
846 assert(vk_tiling == VK_IMAGE_TILING_LINEAR ||
847 vk_tiling == VK_IMAGE_TILING_OPTIMAL);
848
849 /* Check format. */
850 VkFormat vk_format = vk_format_from_android(desc.format, desc.usage);
851 enum isl_format isl_fmt = anv_get_isl_format(&device->info,
852 vk_format,
853 VK_IMAGE_ASPECT_COLOR_BIT,
854 vk_tiling);
855 assert(isl_fmt != ISL_FORMAT_UNSUPPORTED);
856
857 /* Handle RGB(X)->RGBA fallback. */
858 switch (desc.format) {
859 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
860 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
861 if (isl_format_is_rgb(isl_fmt))
862 isl_fmt = isl_format_rgb_to_rgba(isl_fmt);
863 break;
864 }
865
866 /* Now we are able to fill anv_image fields properly and create
867 * isl_surface for it.
868 */
869 image->vk_format = vk_format;
870 image->format = anv_get_format(vk_format);
871 image->aspects = vk_format_aspects(image->vk_format);
872 image->n_planes = image->format->n_planes;
873 image->ccs_e_compatible = false;
874
875 uint32_t stride = desc.stride *
876 (isl_format_get_layout(isl_fmt)->bpb / 8);
877
878 uint32_t b;
879 for_each_bit(b, image->aspects) {
880 VkResult r = make_surface(device, image, stride, isl_tiling_flags,
881 ISL_SURF_USAGE_DISABLE_AUX_BIT, (1 << b));
882 assert(r == VK_SUCCESS);
883 }
884 #endif
885 }
886
887 VkResult anv_BindImageMemory(
888 VkDevice _device,
889 VkImage _image,
890 VkDeviceMemory _memory,
891 VkDeviceSize memoryOffset)
892 {
893 ANV_FROM_HANDLE(anv_device, device, _device);
894 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
895 ANV_FROM_HANDLE(anv_image, image, _image);
896
897 if (mem->ahw)
898 resolve_ahw_image(device, image, mem);
899
900 uint32_t aspect_bit;
901 anv_foreach_image_aspect_bit(aspect_bit, image, image->aspects) {
902 uint32_t plane =
903 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
904 anv_image_bind_memory_plane(device, image, plane, mem, memoryOffset);
905 }
906
907 return VK_SUCCESS;
908 }
909
910 VkResult anv_BindImageMemory2(
911 VkDevice _device,
912 uint32_t bindInfoCount,
913 const VkBindImageMemoryInfo* pBindInfos)
914 {
915 ANV_FROM_HANDLE(anv_device, device, _device);
916
917 for (uint32_t i = 0; i < bindInfoCount; i++) {
918 const VkBindImageMemoryInfo *bind_info = &pBindInfos[i];
919 ANV_FROM_HANDLE(anv_device_memory, mem, bind_info->memory);
920 ANV_FROM_HANDLE(anv_image, image, bind_info->image);
921
922 /* Resolve will alter the image's aspects, do this first. */
923 if (mem && mem->ahw)
924 resolve_ahw_image(device, image, mem);
925
926 VkImageAspectFlags aspects = image->aspects;
927 vk_foreach_struct_const(s, bind_info->pNext) {
928 switch (s->sType) {
929 case VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO: {
930 const VkBindImagePlaneMemoryInfo *plane_info =
931 (const VkBindImagePlaneMemoryInfo *) s;
932
933 aspects = plane_info->planeAspect;
934 break;
935 }
936 case VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_SWAPCHAIN_INFO_KHR: {
937 const VkBindImageMemorySwapchainInfoKHR *swapchain_info =
938 (const VkBindImageMemorySwapchainInfoKHR *) s;
939 struct anv_image *swapchain_image =
940 anv_swapchain_get_image(swapchain_info->swapchain,
941 swapchain_info->imageIndex);
942 assert(swapchain_image);
943 assert(image->aspects == swapchain_image->aspects);
944 assert(mem == NULL);
945
946 uint32_t aspect_bit;
947 anv_foreach_image_aspect_bit(aspect_bit, image, aspects) {
948 uint32_t plane =
949 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
950 struct anv_device_memory mem = {
951 .bo = swapchain_image->planes[plane].address.bo,
952 };
953 anv_image_bind_memory_plane(device, image, plane,
954 &mem, bind_info->memoryOffset);
955 }
956 break;
957 }
958 default:
959 anv_debug_ignored_stype(s->sType);
960 break;
961 }
962 }
963
964 /* VkBindImageMemorySwapchainInfoKHR requires memory to be
965 * VK_NULL_HANDLE. In such case, just carry one with the next bind
966 * item.
967 */
968 if (!mem)
969 continue;
970
971 uint32_t aspect_bit;
972 anv_foreach_image_aspect_bit(aspect_bit, image, aspects) {
973 uint32_t plane =
974 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
975 anv_image_bind_memory_plane(device, image, plane,
976 mem, bind_info->memoryOffset);
977 }
978 }
979
980 return VK_SUCCESS;
981 }
982
983 void anv_GetImageSubresourceLayout(
984 VkDevice device,
985 VkImage _image,
986 const VkImageSubresource* subresource,
987 VkSubresourceLayout* layout)
988 {
989 ANV_FROM_HANDLE(anv_image, image, _image);
990
991 const struct anv_surface *surface;
992 if (subresource->aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT &&
993 image->drm_format_mod != DRM_FORMAT_MOD_INVALID &&
994 isl_drm_modifier_has_aux(image->drm_format_mod))
995 surface = &image->planes[0].aux_surface;
996 else
997 surface = get_surface(image, subresource->aspectMask);
998
999 assert(__builtin_popcount(subresource->aspectMask) == 1);
1000
1001 layout->offset = surface->offset;
1002 layout->rowPitch = surface->isl.row_pitch_B;
1003 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
1004 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
1005
1006 if (subresource->mipLevel > 0 || subresource->arrayLayer > 0) {
1007 assert(surface->isl.tiling == ISL_TILING_LINEAR);
1008
1009 uint32_t offset_B;
1010 isl_surf_get_image_offset_B_tile_sa(&surface->isl,
1011 subresource->mipLevel,
1012 subresource->arrayLayer,
1013 0 /* logical_z_offset_px */,
1014 &offset_B, NULL, NULL);
1015 layout->offset += offset_B;
1016 layout->size = layout->rowPitch * anv_minify(image->extent.height,
1017 subresource->mipLevel);
1018 } else {
1019 layout->size = surface->isl.size_B;
1020 }
1021 }
1022
1023 /**
1024 * This function determines the optimal buffer to use for a given
1025 * VkImageLayout and other pieces of information needed to make that
1026 * determination. This does not determine the optimal buffer to use
1027 * during a resolve operation.
1028 *
1029 * @param devinfo The device information of the Intel GPU.
1030 * @param image The image that may contain a collection of buffers.
1031 * @param aspect The aspect of the image to be accessed.
1032 * @param layout The current layout of the image aspect(s).
1033 *
1034 * @return The primary buffer that should be used for the given layout.
1035 */
1036 enum isl_aux_usage
1037 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
1038 const struct anv_image * const image,
1039 const VkImageAspectFlagBits aspect,
1040 const VkImageLayout layout)
1041 {
1042 /* Validate the inputs. */
1043
1044 /* The devinfo is needed as the optimal buffer varies across generations. */
1045 assert(devinfo != NULL);
1046
1047 /* The layout of a NULL image is not properly defined. */
1048 assert(image != NULL);
1049
1050 /* The aspect must be exactly one of the image aspects. */
1051 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
1052
1053 /* Determine the optimal buffer. */
1054
1055 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
1056
1057 /* If there is no auxiliary surface allocated, we must use the one and only
1058 * main buffer.
1059 */
1060 if (image->planes[plane].aux_surface.isl.size_B == 0)
1061 return ISL_AUX_USAGE_NONE;
1062
1063 /* All images that use an auxiliary surface are required to be tiled. */
1064 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
1065
1066 /* Stencil has no aux */
1067 assert(aspect != VK_IMAGE_ASPECT_STENCIL_BIT);
1068
1069 switch (layout) {
1070
1071 /* Invalid Layouts */
1072 case VK_IMAGE_LAYOUT_RANGE_SIZE:
1073 case VK_IMAGE_LAYOUT_MAX_ENUM:
1074 unreachable("Invalid image layout.");
1075
1076 /* Undefined layouts
1077 *
1078 * The pre-initialized layout is equivalent to the undefined layout for
1079 * optimally-tiled images. We can only do color compression (CCS or HiZ)
1080 * on tiled images.
1081 */
1082 case VK_IMAGE_LAYOUT_UNDEFINED:
1083 case VK_IMAGE_LAYOUT_PREINITIALIZED:
1084 return ISL_AUX_USAGE_NONE;
1085
1086
1087 /* Transfer Layouts
1088 */
1089 case VK_IMAGE_LAYOUT_GENERAL:
1090 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
1091 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
1092 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
1093 /* This buffer could be a depth buffer used in a transfer operation.
1094 * BLORP currently doesn't use HiZ for transfer operations so we must
1095 * use the main buffer for this layout. TODO: Enable HiZ in BLORP.
1096 */
1097 assert(image->planes[plane].aux_usage == ISL_AUX_USAGE_HIZ);
1098 return ISL_AUX_USAGE_NONE;
1099 } else {
1100 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
1101 return image->planes[plane].aux_usage;
1102 }
1103
1104
1105 /* Sampling Layouts */
1106 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
1107 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
1108 assert((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1109 /* Fall-through */
1110 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
1111 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
1112 if (anv_can_sample_with_hiz(devinfo, image))
1113 return ISL_AUX_USAGE_HIZ;
1114 else
1115 return ISL_AUX_USAGE_NONE;
1116 } else {
1117 return image->planes[plane].aux_usage;
1118 }
1119
1120
1121 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
1122 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1123
1124 /* When handing the image off to the presentation engine, we need to
1125 * ensure that things are properly resolved. For images with no
1126 * modifier, we assume that they follow the old rules and always need
1127 * a full resolve because the PE doesn't understand any form of
1128 * compression. For images with modifiers, we use the aux usage from
1129 * the modifier.
1130 */
1131 const struct isl_drm_modifier_info *mod_info =
1132 isl_drm_modifier_get_info(image->drm_format_mod);
1133 return mod_info ? mod_info->aux_usage : ISL_AUX_USAGE_NONE;
1134 }
1135
1136
1137 /* Rendering Layouts */
1138 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1139 assert(aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
1140 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_NONE) {
1141 assert(image->samples == 1);
1142 return ISL_AUX_USAGE_CCS_D;
1143 } else {
1144 assert(image->planes[plane].aux_usage != ISL_AUX_USAGE_CCS_D);
1145 return image->planes[plane].aux_usage;
1146 }
1147
1148 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
1149 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
1150 assert(aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
1151 return ISL_AUX_USAGE_HIZ;
1152
1153 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
1154 unreachable("VK_KHR_shared_presentable_image is unsupported");
1155
1156 case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
1157 unreachable("VK_EXT_fragment_density_map is unsupported");
1158
1159 case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
1160 unreachable("VK_NV_shading_rate_image is unsupported");
1161 }
1162
1163 /* If the layout isn't recognized in the exhaustive switch above, the
1164 * VkImageLayout value is not defined in vulkan.h.
1165 */
1166 unreachable("layout is not a VkImageLayout enumeration member.");
1167 }
1168
1169 /**
1170 * This function returns the level of unresolved fast-clear support of the
1171 * given image in the given VkImageLayout.
1172 *
1173 * @param devinfo The device information of the Intel GPU.
1174 * @param image The image that may contain a collection of buffers.
1175 * @param aspect The aspect of the image to be accessed.
1176 * @param layout The current layout of the image aspect(s).
1177 */
1178 enum anv_fast_clear_type
1179 anv_layout_to_fast_clear_type(const struct gen_device_info * const devinfo,
1180 const struct anv_image * const image,
1181 const VkImageAspectFlagBits aspect,
1182 const VkImageLayout layout)
1183 {
1184 if (INTEL_DEBUG & DEBUG_NO_FAST_CLEAR)
1185 return ANV_FAST_CLEAR_NONE;
1186
1187 /* The aspect must be exactly one of the image aspects. */
1188 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
1189
1190 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
1191
1192 /* If there is no auxiliary surface allocated, there are no fast-clears */
1193 if (image->planes[plane].aux_surface.isl.size_B == 0)
1194 return ANV_FAST_CLEAR_NONE;
1195
1196 /* All images that use an auxiliary surface are required to be tiled. */
1197 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
1198
1199 /* Stencil has no aux */
1200 assert(aspect != VK_IMAGE_ASPECT_STENCIL_BIT);
1201
1202 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
1203 /* For depth images (with HiZ), the layout supports fast-clears if and
1204 * only if it supports HiZ. However, we only support fast-clears to the
1205 * default depth value.
1206 */
1207 enum isl_aux_usage aux_usage =
1208 anv_layout_to_aux_usage(devinfo, image, aspect, layout);
1209 return aux_usage == ISL_AUX_USAGE_HIZ ?
1210 ANV_FAST_CLEAR_DEFAULT_VALUE : ANV_FAST_CLEAR_NONE;
1211 }
1212
1213 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
1214
1215 /* We don't support MSAA fast-clears on Ivybridge or Bay Trail because they
1216 * lack the MI ALU which we need to determine the predicates.
1217 */
1218 if (devinfo->gen == 7 && !devinfo->is_haswell && image->samples > 1)
1219 return ANV_FAST_CLEAR_NONE;
1220
1221 switch (layout) {
1222 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
1223 return ANV_FAST_CLEAR_ANY;
1224
1225 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
1226 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1227 #ifndef NDEBUG
1228 /* We do not yet support any modifiers which support clear color so we
1229 * just always return NONE. One day, this will change.
1230 */
1231 const struct isl_drm_modifier_info *mod_info =
1232 isl_drm_modifier_get_info(image->drm_format_mod);
1233 assert(!mod_info || !mod_info->supports_clear_color);
1234 #endif
1235 return ANV_FAST_CLEAR_NONE;
1236 }
1237
1238 default:
1239 /* If the image has MCS or CCS_E enabled all the time then we can use
1240 * fast-clear as long as the clear color is the default value of zero
1241 * since this is the default value we program into every surface state
1242 * used for texturing.
1243 */
1244 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_MCS ||
1245 image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E)
1246 return ANV_FAST_CLEAR_DEFAULT_VALUE;
1247 else
1248 return ANV_FAST_CLEAR_NONE;
1249 }
1250 }
1251
1252
1253 static struct anv_state
1254 alloc_surface_state(struct anv_device *device)
1255 {
1256 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
1257 }
1258
1259 static enum isl_channel_select
1260 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
1261 struct isl_swizzle format_swizzle)
1262 {
1263 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
1264 swizzle = component;
1265
1266 switch (swizzle) {
1267 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
1268 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
1269 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
1270 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
1271 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
1272 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
1273 default:
1274 unreachable("Invalid swizzle");
1275 }
1276 }
1277
1278 void
1279 anv_image_fill_surface_state(struct anv_device *device,
1280 const struct anv_image *image,
1281 VkImageAspectFlagBits aspect,
1282 const struct isl_view *view_in,
1283 isl_surf_usage_flags_t view_usage,
1284 enum isl_aux_usage aux_usage,
1285 const union isl_color_value *clear_color,
1286 enum anv_image_view_state_flags flags,
1287 struct anv_surface_state *state_inout,
1288 struct brw_image_param *image_param_out)
1289 {
1290 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
1291
1292 const struct anv_surface *surface = &image->planes[plane].surface,
1293 *aux_surface = &image->planes[plane].aux_surface;
1294
1295 struct isl_view view = *view_in;
1296 view.usage |= view_usage;
1297
1298 /* For texturing with VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL from a
1299 * compressed surface with a shadow surface, we use the shadow instead of
1300 * the primary surface. The shadow surface will be tiled, unlike the main
1301 * surface, so it should get significantly better performance.
1302 */
1303 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
1304 isl_format_is_compressed(view.format) &&
1305 (flags & ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL)) {
1306 assert(isl_format_is_compressed(surface->isl.format));
1307 assert(surface->isl.tiling == ISL_TILING_LINEAR);
1308 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
1309 surface = &image->planes[plane].shadow_surface;
1310 }
1311
1312 /* For texturing from stencil on gen7, we have to sample from a shadow
1313 * surface because we don't support W-tiling in the sampler.
1314 */
1315 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
1316 aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
1317 assert(device->info.gen == 7);
1318 assert(view_usage & ISL_SURF_USAGE_TEXTURE_BIT);
1319 surface = &image->planes[plane].shadow_surface;
1320 }
1321
1322 if (view_usage == ISL_SURF_USAGE_RENDER_TARGET_BIT)
1323 view.swizzle = anv_swizzle_for_render(view.swizzle);
1324
1325 /* On Ivy Bridge and Bay Trail we do the swizzle in the shader */
1326 if (device->info.gen == 7 && !device->info.is_haswell)
1327 view.swizzle = ISL_SWIZZLE_IDENTITY;
1328
1329 /* If this is a HiZ buffer we can sample from with a programmable clear
1330 * value (SKL+), define the clear value to the optimal constant.
1331 */
1332 union isl_color_value default_clear_color = { .u32 = { 0, } };
1333 if (device->info.gen >= 9 && aux_usage == ISL_AUX_USAGE_HIZ)
1334 default_clear_color.f32[0] = ANV_HZ_FC_VAL;
1335 if (!clear_color)
1336 clear_color = &default_clear_color;
1337
1338 const struct anv_address address =
1339 anv_address_add(image->planes[plane].address, surface->offset);
1340
1341 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
1342 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY) &&
1343 !isl_has_matching_typed_storage_image_format(&device->info,
1344 view.format)) {
1345 /* In this case, we are a writeable storage buffer which needs to be
1346 * lowered to linear. All tiling and offset calculations will be done in
1347 * the shader.
1348 */
1349 assert(aux_usage == ISL_AUX_USAGE_NONE);
1350 isl_buffer_fill_state(&device->isl_dev, state_inout->state.map,
1351 .address = anv_address_physical(address),
1352 .size_B = surface->isl.size_B,
1353 .format = ISL_FORMAT_RAW,
1354 .swizzle = ISL_SWIZZLE_IDENTITY,
1355 .stride_B = 1,
1356 .mocs = anv_mocs_for_bo(device, address.bo));
1357 state_inout->address = address,
1358 state_inout->aux_address = ANV_NULL_ADDRESS;
1359 state_inout->clear_address = ANV_NULL_ADDRESS;
1360 } else {
1361 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
1362 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY)) {
1363 /* Typed surface reads support a very limited subset of the shader
1364 * image formats. Translate it into the closest format the hardware
1365 * supports.
1366 */
1367 assert(aux_usage == ISL_AUX_USAGE_NONE);
1368 view.format = isl_lower_storage_image_format(&device->info,
1369 view.format);
1370 }
1371
1372 const struct isl_surf *isl_surf = &surface->isl;
1373
1374 struct isl_surf tmp_surf;
1375 uint32_t offset_B = 0, tile_x_sa = 0, tile_y_sa = 0;
1376 if (isl_format_is_compressed(surface->isl.format) &&
1377 !isl_format_is_compressed(view.format)) {
1378 /* We're creating an uncompressed view of a compressed surface. This
1379 * is allowed but only for a single level/layer.
1380 */
1381 assert(surface->isl.samples == 1);
1382 assert(view.levels == 1);
1383 assert(view.array_len == 1);
1384
1385 isl_surf_get_image_surf(&device->isl_dev, isl_surf,
1386 view.base_level,
1387 surface->isl.dim == ISL_SURF_DIM_3D ?
1388 0 : view.base_array_layer,
1389 surface->isl.dim == ISL_SURF_DIM_3D ?
1390 view.base_array_layer : 0,
1391 &tmp_surf,
1392 &offset_B, &tile_x_sa, &tile_y_sa);
1393
1394 /* The newly created image represents the one subimage we're
1395 * referencing with this view so it only has one array slice and
1396 * miplevel.
1397 */
1398 view.base_array_layer = 0;
1399 view.base_level = 0;
1400
1401 /* We're making an uncompressed view here. The image dimensions need
1402 * to be scaled down by the block size.
1403 */
1404 const struct isl_format_layout *fmtl =
1405 isl_format_get_layout(surface->isl.format);
1406 tmp_surf.logical_level0_px =
1407 isl_surf_get_logical_level0_el(&tmp_surf);
1408 tmp_surf.phys_level0_sa = isl_surf_get_phys_level0_el(&tmp_surf);
1409 tmp_surf.format = view.format;
1410 tile_x_sa /= fmtl->bw;
1411 tile_y_sa /= fmtl->bh;
1412
1413 isl_surf = &tmp_surf;
1414
1415 if (device->info.gen <= 8) {
1416 assert(surface->isl.tiling == ISL_TILING_LINEAR);
1417 assert(tile_x_sa == 0);
1418 assert(tile_y_sa == 0);
1419 }
1420 }
1421
1422 state_inout->address = anv_address_add(address, offset_B);
1423
1424 struct anv_address aux_address = ANV_NULL_ADDRESS;
1425 if (aux_usage != ISL_AUX_USAGE_NONE) {
1426 aux_address = anv_address_add(image->planes[plane].address,
1427 aux_surface->offset);
1428 }
1429 state_inout->aux_address = aux_address;
1430
1431 struct anv_address clear_address = ANV_NULL_ADDRESS;
1432 if (device->info.gen >= 10 && aux_usage != ISL_AUX_USAGE_NONE) {
1433 if (aux_usage == ISL_AUX_USAGE_HIZ) {
1434 clear_address = (struct anv_address) {
1435 .bo = &device->hiz_clear_bo,
1436 .offset = 0,
1437 };
1438 } else {
1439 clear_address = anv_image_get_clear_color_addr(device, image, aspect);
1440 }
1441 }
1442 state_inout->clear_address = clear_address;
1443
1444 isl_surf_fill_state(&device->isl_dev, state_inout->state.map,
1445 .surf = isl_surf,
1446 .view = &view,
1447 .address = anv_address_physical(state_inout->address),
1448 .clear_color = *clear_color,
1449 .aux_surf = &aux_surface->isl,
1450 .aux_usage = aux_usage,
1451 .aux_address = anv_address_physical(aux_address),
1452 .clear_address = anv_address_physical(clear_address),
1453 .use_clear_address = !anv_address_is_null(clear_address),
1454 .mocs = anv_mocs_for_bo(device,
1455 state_inout->address.bo),
1456 .x_offset_sa = tile_x_sa,
1457 .y_offset_sa = tile_y_sa);
1458
1459 /* With the exception of gen8, the bottom 12 bits of the MCS base address
1460 * are used to store other information. This should be ok, however,
1461 * because the surface buffer addresses are always 4K page aligned.
1462 */
1463 uint32_t *aux_addr_dw = state_inout->state.map +
1464 device->isl_dev.ss.aux_addr_offset;
1465 assert((aux_address.offset & 0xfff) == 0);
1466 state_inout->aux_address.offset |= *aux_addr_dw & 0xfff;
1467
1468 if (device->info.gen >= 10 && clear_address.bo) {
1469 uint32_t *clear_addr_dw = state_inout->state.map +
1470 device->isl_dev.ss.clear_color_state_offset;
1471 assert((clear_address.offset & 0x3f) == 0);
1472 state_inout->clear_address.offset |= *clear_addr_dw & 0x3f;
1473 }
1474 }
1475
1476 if (image_param_out) {
1477 assert(view_usage == ISL_SURF_USAGE_STORAGE_BIT);
1478 isl_surf_fill_image_param(&device->isl_dev, image_param_out,
1479 &surface->isl, &view);
1480 }
1481 }
1482
1483 static VkImageAspectFlags
1484 remap_aspect_flags(VkImageAspectFlags view_aspects)
1485 {
1486 if (view_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1487 if (util_bitcount(view_aspects) == 1)
1488 return VK_IMAGE_ASPECT_COLOR_BIT;
1489
1490 VkImageAspectFlags color_aspects = 0;
1491 for (uint32_t i = 0; i < util_bitcount(view_aspects); i++)
1492 color_aspects |= VK_IMAGE_ASPECT_PLANE_0_BIT << i;
1493 return color_aspects;
1494 }
1495 /* No special remapping needed for depth & stencil aspects. */
1496 return view_aspects;
1497 }
1498
1499 static uint32_t
1500 anv_image_aspect_get_planes(VkImageAspectFlags aspect_mask)
1501 {
1502 uint32_t planes = 0;
1503
1504 if (aspect_mask & (VK_IMAGE_ASPECT_COLOR_BIT |
1505 VK_IMAGE_ASPECT_DEPTH_BIT |
1506 VK_IMAGE_ASPECT_STENCIL_BIT |
1507 VK_IMAGE_ASPECT_PLANE_0_BIT))
1508 planes++;
1509 if (aspect_mask & VK_IMAGE_ASPECT_PLANE_1_BIT)
1510 planes++;
1511 if (aspect_mask & VK_IMAGE_ASPECT_PLANE_2_BIT)
1512 planes++;
1513
1514 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0 &&
1515 (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0)
1516 planes++;
1517
1518 return planes;
1519 }
1520
1521 VkResult
1522 anv_CreateImageView(VkDevice _device,
1523 const VkImageViewCreateInfo *pCreateInfo,
1524 const VkAllocationCallbacks *pAllocator,
1525 VkImageView *pView)
1526 {
1527 ANV_FROM_HANDLE(anv_device, device, _device);
1528 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
1529 struct anv_image_view *iview;
1530
1531 iview = vk_zalloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
1532 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1533 if (iview == NULL)
1534 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1535
1536 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
1537
1538 assert(range->layerCount > 0);
1539 assert(range->baseMipLevel < image->levels);
1540
1541 /* Check if a conversion info was passed. */
1542 const struct anv_format *conv_format = NULL;
1543 const struct VkSamplerYcbcrConversionInfo *conv_info =
1544 vk_find_struct_const(pCreateInfo->pNext, SAMPLER_YCBCR_CONVERSION_INFO);
1545
1546 /* If image has an external format, the pNext chain must contain an instance of
1547 * VKSamplerYcbcrConversionInfo with a conversion object created with the same
1548 * external format as image."
1549 */
1550 assert(!image->external_format || conv_info);
1551
1552 if (conv_info) {
1553 ANV_FROM_HANDLE(anv_ycbcr_conversion, conversion, conv_info->conversion);
1554 conv_format = conversion->format;
1555 }
1556
1557 VkImageUsageFlags image_usage = 0;
1558 if (range->aspectMask & ~VK_IMAGE_ASPECT_STENCIL_BIT)
1559 image_usage |= image->usage;
1560 if (range->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
1561 image_usage |= image->stencil_usage;
1562
1563 const VkImageViewUsageCreateInfo *usage_info =
1564 vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO);
1565 VkImageUsageFlags view_usage = usage_info ? usage_info->usage : image_usage;
1566
1567 /* View usage should be a subset of image usage */
1568 assert((view_usage & ~image_usage) == 0);
1569 assert(view_usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
1570 VK_IMAGE_USAGE_STORAGE_BIT |
1571 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1572 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
1573 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
1574
1575 switch (image->type) {
1576 default:
1577 unreachable("bad VkImageType");
1578 case VK_IMAGE_TYPE_1D:
1579 case VK_IMAGE_TYPE_2D:
1580 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
1581 break;
1582 case VK_IMAGE_TYPE_3D:
1583 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
1584 <= anv_minify(image->extent.depth, range->baseMipLevel));
1585 break;
1586 }
1587
1588 /* First expand aspects to the image's ones (for example
1589 * VK_IMAGE_ASPECT_COLOR_BIT will be converted to
1590 * VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT |
1591 * VK_IMAGE_ASPECT_PLANE_2_BIT for an image of format
1592 * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM.
1593 */
1594 VkImageAspectFlags expanded_aspects =
1595 anv_image_expand_aspects(image, range->aspectMask);
1596
1597 iview->image = image;
1598
1599 /* Remap the expanded aspects for the image view. For example if only
1600 * VK_IMAGE_ASPECT_PLANE_1_BIT was given in range->aspectMask, we will
1601 * convert it to VK_IMAGE_ASPECT_COLOR_BIT since from the point of view of
1602 * the image view, it only has a single plane.
1603 */
1604 iview->aspect_mask = remap_aspect_flags(expanded_aspects);
1605 iview->n_planes = anv_image_aspect_get_planes(iview->aspect_mask);
1606 iview->vk_format = pCreateInfo->format;
1607
1608 /* "If image has an external format, format must be VK_FORMAT_UNDEFINED." */
1609 assert(!image->external_format || pCreateInfo->format == VK_FORMAT_UNDEFINED);
1610
1611 /* Format is undefined, this can happen when using external formats. Set
1612 * view format from the passed conversion info.
1613 */
1614 if (iview->vk_format == VK_FORMAT_UNDEFINED && conv_format)
1615 iview->vk_format = conv_format->vk_format;
1616
1617 iview->extent = (VkExtent3D) {
1618 .width = anv_minify(image->extent.width , range->baseMipLevel),
1619 .height = anv_minify(image->extent.height, range->baseMipLevel),
1620 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
1621 };
1622
1623 /* Now go through the underlying image selected planes (computed in
1624 * expanded_aspects) and map them to planes in the image view.
1625 */
1626 uint32_t iaspect_bit, vplane = 0;
1627 anv_foreach_image_aspect_bit(iaspect_bit, image, expanded_aspects) {
1628 uint32_t iplane =
1629 anv_image_aspect_to_plane(image->aspects, 1UL << iaspect_bit);
1630 VkImageAspectFlags vplane_aspect =
1631 anv_plane_to_aspect(iview->aspect_mask, vplane);
1632 struct anv_format_plane format =
1633 anv_get_format_plane(&device->info, iview->vk_format,
1634 vplane_aspect, image->tiling);
1635
1636 iview->planes[vplane].image_plane = iplane;
1637
1638 iview->planes[vplane].isl = (struct isl_view) {
1639 .format = format.isl_format,
1640 .base_level = range->baseMipLevel,
1641 .levels = anv_get_levelCount(image, range),
1642 .base_array_layer = range->baseArrayLayer,
1643 .array_len = anv_get_layerCount(image, range),
1644 .swizzle = {
1645 .r = remap_swizzle(pCreateInfo->components.r,
1646 VK_COMPONENT_SWIZZLE_R, format.swizzle),
1647 .g = remap_swizzle(pCreateInfo->components.g,
1648 VK_COMPONENT_SWIZZLE_G, format.swizzle),
1649 .b = remap_swizzle(pCreateInfo->components.b,
1650 VK_COMPONENT_SWIZZLE_B, format.swizzle),
1651 .a = remap_swizzle(pCreateInfo->components.a,
1652 VK_COMPONENT_SWIZZLE_A, format.swizzle),
1653 },
1654 };
1655
1656 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
1657 iview->planes[vplane].isl.base_array_layer = 0;
1658 iview->planes[vplane].isl.array_len = iview->extent.depth;
1659 }
1660
1661 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
1662 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
1663 iview->planes[vplane].isl.usage = ISL_SURF_USAGE_CUBE_BIT;
1664 } else {
1665 iview->planes[vplane].isl.usage = 0;
1666 }
1667
1668 if (view_usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
1669 (view_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
1670 !(iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT))) {
1671 iview->planes[vplane].optimal_sampler_surface_state.state = alloc_surface_state(device);
1672 iview->planes[vplane].general_sampler_surface_state.state = alloc_surface_state(device);
1673
1674 enum isl_aux_usage general_aux_usage =
1675 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1676 VK_IMAGE_LAYOUT_GENERAL);
1677 enum isl_aux_usage optimal_aux_usage =
1678 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1679 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1680
1681 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1682 &iview->planes[vplane].isl,
1683 ISL_SURF_USAGE_TEXTURE_BIT,
1684 optimal_aux_usage, NULL,
1685 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL,
1686 &iview->planes[vplane].optimal_sampler_surface_state,
1687 NULL);
1688
1689 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1690 &iview->planes[vplane].isl,
1691 ISL_SURF_USAGE_TEXTURE_BIT,
1692 general_aux_usage, NULL,
1693 0,
1694 &iview->planes[vplane].general_sampler_surface_state,
1695 NULL);
1696 }
1697
1698 /* NOTE: This one needs to go last since it may stomp isl_view.format */
1699 if (view_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1700 iview->planes[vplane].storage_surface_state.state = alloc_surface_state(device);
1701 iview->planes[vplane].writeonly_storage_surface_state.state = alloc_surface_state(device);
1702
1703 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1704 &iview->planes[vplane].isl,
1705 ISL_SURF_USAGE_STORAGE_BIT,
1706 ISL_AUX_USAGE_NONE, NULL,
1707 0,
1708 &iview->planes[vplane].storage_surface_state,
1709 &iview->planes[vplane].storage_image_param);
1710
1711 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1712 &iview->planes[vplane].isl,
1713 ISL_SURF_USAGE_STORAGE_BIT,
1714 ISL_AUX_USAGE_NONE, NULL,
1715 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY,
1716 &iview->planes[vplane].writeonly_storage_surface_state,
1717 NULL);
1718 }
1719
1720 vplane++;
1721 }
1722
1723 *pView = anv_image_view_to_handle(iview);
1724
1725 return VK_SUCCESS;
1726 }
1727
1728 void
1729 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
1730 const VkAllocationCallbacks *pAllocator)
1731 {
1732 ANV_FROM_HANDLE(anv_device, device, _device);
1733 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
1734
1735 if (!iview)
1736 return;
1737
1738 for (uint32_t plane = 0; plane < iview->n_planes; plane++) {
1739 if (iview->planes[plane].optimal_sampler_surface_state.state.alloc_size > 0) {
1740 anv_state_pool_free(&device->surface_state_pool,
1741 iview->planes[plane].optimal_sampler_surface_state.state);
1742 }
1743
1744 if (iview->planes[plane].general_sampler_surface_state.state.alloc_size > 0) {
1745 anv_state_pool_free(&device->surface_state_pool,
1746 iview->planes[plane].general_sampler_surface_state.state);
1747 }
1748
1749 if (iview->planes[plane].storage_surface_state.state.alloc_size > 0) {
1750 anv_state_pool_free(&device->surface_state_pool,
1751 iview->planes[plane].storage_surface_state.state);
1752 }
1753
1754 if (iview->planes[plane].writeonly_storage_surface_state.state.alloc_size > 0) {
1755 anv_state_pool_free(&device->surface_state_pool,
1756 iview->planes[plane].writeonly_storage_surface_state.state);
1757 }
1758 }
1759
1760 vk_free2(&device->alloc, pAllocator, iview);
1761 }
1762
1763
1764 VkResult
1765 anv_CreateBufferView(VkDevice _device,
1766 const VkBufferViewCreateInfo *pCreateInfo,
1767 const VkAllocationCallbacks *pAllocator,
1768 VkBufferView *pView)
1769 {
1770 ANV_FROM_HANDLE(anv_device, device, _device);
1771 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
1772 struct anv_buffer_view *view;
1773
1774 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
1775 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1776 if (!view)
1777 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1778
1779 /* TODO: Handle the format swizzle? */
1780
1781 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
1782 VK_IMAGE_ASPECT_COLOR_BIT,
1783 VK_IMAGE_TILING_LINEAR);
1784 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
1785 view->range = anv_buffer_get_range(buffer, pCreateInfo->offset,
1786 pCreateInfo->range);
1787 view->range = align_down_npot_u32(view->range, format_bs);
1788
1789 view->address = anv_address_add(buffer->address, pCreateInfo->offset);
1790
1791 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
1792 view->surface_state = alloc_surface_state(device);
1793
1794 anv_fill_buffer_surface_state(device, view->surface_state,
1795 view->format,
1796 view->address, view->range, format_bs);
1797 } else {
1798 view->surface_state = (struct anv_state){ 0 };
1799 }
1800
1801 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
1802 view->storage_surface_state = alloc_surface_state(device);
1803 view->writeonly_storage_surface_state = alloc_surface_state(device);
1804
1805 enum isl_format storage_format =
1806 isl_has_matching_typed_storage_image_format(&device->info,
1807 view->format) ?
1808 isl_lower_storage_image_format(&device->info, view->format) :
1809 ISL_FORMAT_RAW;
1810
1811 anv_fill_buffer_surface_state(device, view->storage_surface_state,
1812 storage_format,
1813 view->address, view->range,
1814 (storage_format == ISL_FORMAT_RAW ? 1 :
1815 isl_format_get_layout(storage_format)->bpb / 8));
1816
1817 /* Write-only accesses should use the original format. */
1818 anv_fill_buffer_surface_state(device, view->writeonly_storage_surface_state,
1819 view->format,
1820 view->address, view->range,
1821 isl_format_get_layout(view->format)->bpb / 8);
1822
1823 isl_buffer_fill_image_param(&device->isl_dev,
1824 &view->storage_image_param,
1825 view->format, view->range);
1826 } else {
1827 view->storage_surface_state = (struct anv_state){ 0 };
1828 view->writeonly_storage_surface_state = (struct anv_state){ 0 };
1829 }
1830
1831 *pView = anv_buffer_view_to_handle(view);
1832
1833 return VK_SUCCESS;
1834 }
1835
1836 void
1837 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
1838 const VkAllocationCallbacks *pAllocator)
1839 {
1840 ANV_FROM_HANDLE(anv_device, device, _device);
1841 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
1842
1843 if (!view)
1844 return;
1845
1846 if (view->surface_state.alloc_size > 0)
1847 anv_state_pool_free(&device->surface_state_pool,
1848 view->surface_state);
1849
1850 if (view->storage_surface_state.alloc_size > 0)
1851 anv_state_pool_free(&device->surface_state_pool,
1852 view->storage_surface_state);
1853
1854 if (view->writeonly_storage_surface_state.alloc_size > 0)
1855 anv_state_pool_free(&device->surface_state_pool,
1856 view->writeonly_storage_surface_state);
1857
1858 vk_free2(&device->alloc, pAllocator, view);
1859 }
1860
1861 const struct anv_surface *
1862 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
1863 VkImageAspectFlags aspect_mask)
1864 {
1865 VkImageAspectFlags sanitized_mask;
1866
1867 switch (aspect_mask) {
1868 case VK_IMAGE_ASPECT_COLOR_BIT:
1869 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1870 sanitized_mask = VK_IMAGE_ASPECT_COLOR_BIT;
1871 break;
1872 case VK_IMAGE_ASPECT_DEPTH_BIT:
1873 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
1874 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1875 break;
1876 case VK_IMAGE_ASPECT_STENCIL_BIT:
1877 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
1878 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1879 break;
1880 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
1881 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1882 * combined depth stencil formats. Specifically, it states:
1883 *
1884 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1885 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1886 *
1887 * Image views with both depth and stencil aspects are only valid for
1888 * render target attachments, in which case
1889 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1890 * stencil surfaces from the underlying surface.
1891 */
1892 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1893 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1894 } else {
1895 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
1896 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1897 }
1898 break;
1899 case VK_IMAGE_ASPECT_PLANE_0_BIT:
1900 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1901 sanitized_mask = VK_IMAGE_ASPECT_PLANE_0_BIT;
1902 break;
1903 case VK_IMAGE_ASPECT_PLANE_1_BIT:
1904 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1905 sanitized_mask = VK_IMAGE_ASPECT_PLANE_1_BIT;
1906 break;
1907 case VK_IMAGE_ASPECT_PLANE_2_BIT:
1908 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1909 sanitized_mask = VK_IMAGE_ASPECT_PLANE_2_BIT;
1910 break;
1911 default:
1912 unreachable("image does not have aspect");
1913 return NULL;
1914 }
1915
1916 uint32_t plane = anv_image_aspect_to_plane(image->aspects, sanitized_mask);
1917 return &image->planes[plane].surface;
1918 }