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