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