anv: add create_flags as part of anv_image
[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_fourcc.h>
31
32 #include "anv_private.h"
33 #include "util/debug.h"
34 #include "vk_util.h"
35 #include "util/u_math.h"
36
37 #include "vk_format_info.h"
38
39 static isl_surf_usage_flags_t
40 choose_isl_surf_usage(VkImageCreateFlags vk_create_flags,
41 VkImageUsageFlags vk_usage,
42 isl_surf_usage_flags_t isl_extra_usage,
43 VkImageAspectFlagBits aspect)
44 {
45 isl_surf_usage_flags_t isl_usage = isl_extra_usage;
46
47 if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
48 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
49
50 if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
51 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
52
53 if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
54 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
55
56 if (vk_create_flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
57 isl_usage |= ISL_SURF_USAGE_CUBE_BIT;
58
59 /* Even if we're only using it for transfer operations, clears to depth and
60 * stencil images happen as depth and stencil so they need the right ISL
61 * usage bits or else things will fall apart.
62 */
63 switch (aspect) {
64 case VK_IMAGE_ASPECT_DEPTH_BIT:
65 isl_usage |= ISL_SURF_USAGE_DEPTH_BIT;
66 break;
67 case VK_IMAGE_ASPECT_STENCIL_BIT:
68 isl_usage |= ISL_SURF_USAGE_STENCIL_BIT;
69 break;
70 case VK_IMAGE_ASPECT_COLOR_BIT:
71 case VK_IMAGE_ASPECT_PLANE_0_BIT:
72 case VK_IMAGE_ASPECT_PLANE_1_BIT:
73 case VK_IMAGE_ASPECT_PLANE_2_BIT:
74 break;
75 default:
76 unreachable("bad VkImageAspect");
77 }
78
79 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
80 /* blorp implements transfers by sampling from the source image. */
81 isl_usage |= ISL_SURF_USAGE_TEXTURE_BIT;
82 }
83
84 if (vk_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT &&
85 aspect == VK_IMAGE_ASPECT_COLOR_BIT) {
86 /* blorp implements transfers by rendering into the destination image.
87 * Only request this with color images, as we deal with depth/stencil
88 * formats differently. */
89 isl_usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
90 }
91
92 return isl_usage;
93 }
94
95 static isl_tiling_flags_t
96 choose_isl_tiling_flags(const struct anv_image_create_info *anv_info,
97 const struct isl_drm_modifier_info *isl_mod_info,
98 bool legacy_scanout)
99 {
100 const VkImageCreateInfo *base_info = anv_info->vk_info;
101 isl_tiling_flags_t flags = 0;
102
103 switch (base_info->tiling) {
104 default:
105 unreachable("bad VkImageTiling");
106 case VK_IMAGE_TILING_OPTIMAL:
107 flags = ISL_TILING_ANY_MASK;
108 break;
109 case VK_IMAGE_TILING_LINEAR:
110 flags = ISL_TILING_LINEAR_BIT;
111 break;
112 }
113
114 if (anv_info->isl_tiling_flags)
115 flags &= anv_info->isl_tiling_flags;
116
117 if (legacy_scanout)
118 flags &= ISL_TILING_LINEAR_BIT | ISL_TILING_X_BIT;
119
120 if (isl_mod_info)
121 flags &= 1 << isl_mod_info->tiling;
122
123 assert(flags);
124
125 return flags;
126 }
127
128 static struct anv_surface *
129 get_surface(struct anv_image *image, VkImageAspectFlagBits aspect)
130 {
131 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
132 return &image->planes[plane].surface;
133 }
134
135 static void
136 add_surface(struct anv_image *image, struct anv_surface *surf, uint32_t plane)
137 {
138 assert(surf->isl.size_B > 0); /* isl surface must be initialized */
139
140 if (image->disjoint) {
141 surf->offset = align_u32(image->planes[plane].size,
142 surf->isl.alignment_B);
143 /* Plane offset is always 0 when it's disjoint. */
144 } else {
145 surf->offset = align_u32(image->size, surf->isl.alignment_B);
146 /* Determine plane's offset only once when the first surface is added. */
147 if (image->planes[plane].size == 0)
148 image->planes[plane].offset = image->size;
149 }
150
151 image->size = surf->offset + surf->isl.size_B;
152 image->planes[plane].size = (surf->offset + surf->isl.size_B) - image->planes[plane].offset;
153
154 image->alignment = MAX2(image->alignment, surf->isl.alignment_B);
155 image->planes[plane].alignment = MAX2(image->planes[plane].alignment,
156 surf->isl.alignment_B);
157 }
158
159
160 static bool
161 all_formats_ccs_e_compatible(const struct gen_device_info *devinfo,
162 const struct VkImageCreateInfo *vk_info)
163 {
164 enum isl_format format =
165 anv_get_isl_format(devinfo, vk_info->format,
166 VK_IMAGE_ASPECT_COLOR_BIT, vk_info->tiling);
167
168 if (!isl_format_supports_ccs_e(devinfo, format))
169 return false;
170
171 if (!(vk_info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))
172 return true;
173
174 const VkImageFormatListCreateInfoKHR *fmt_list =
175 vk_find_struct_const(vk_info->pNext, IMAGE_FORMAT_LIST_CREATE_INFO_KHR);
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, vk_info->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 image->planes[plane].fast_clear_state_offset =
289 image->planes[plane].offset + image->planes[plane].size;
290
291 image->planes[plane].size += state_size;
292 image->size += state_size;
293 }
294
295 /**
296 * Initialize the anv_image::*_surface selected by \a aspect. Then update the
297 * image's memory requirements (that is, the image's size and alignment).
298 */
299 static VkResult
300 make_surface(const struct anv_device *dev,
301 struct anv_image *image,
302 const struct anv_image_create_info *anv_info,
303 isl_tiling_flags_t tiling_flags,
304 VkImageAspectFlagBits aspect)
305 {
306 const VkImageCreateInfo *vk_info = anv_info->vk_info;
307 bool ok;
308
309 static const enum isl_surf_dim vk_to_isl_surf_dim[] = {
310 [VK_IMAGE_TYPE_1D] = ISL_SURF_DIM_1D,
311 [VK_IMAGE_TYPE_2D] = ISL_SURF_DIM_2D,
312 [VK_IMAGE_TYPE_3D] = ISL_SURF_DIM_3D,
313 };
314
315 image->extent = anv_sanitize_image_extent(vk_info->imageType,
316 vk_info->extent);
317
318 const unsigned plane = anv_image_aspect_to_plane(image->aspects, aspect);
319 const struct anv_format_plane plane_format =
320 anv_get_format_plane(&dev->info, image->vk_format, aspect, image->tiling);
321 struct anv_surface *anv_surf = &image->planes[plane].surface;
322
323 const isl_surf_usage_flags_t usage =
324 choose_isl_surf_usage(vk_info->flags, image->usage,
325 anv_info->isl_extra_usage_flags, aspect);
326
327 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
328 * fall back to linear on Broadwell and earlier because we aren't
329 * guaranteed that we can handle offsets correctly. On Sky Lake, the
330 * horizontal and vertical alignments are sufficiently high that we can
331 * just use RENDER_SURFACE_STATE::X/Y Offset.
332 */
333 bool needs_shadow = false;
334 if (dev->info.gen <= 8 &&
335 (vk_info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
336 vk_info->tiling == VK_IMAGE_TILING_OPTIMAL) {
337 assert(isl_format_is_compressed(plane_format.isl_format));
338 tiling_flags = ISL_TILING_LINEAR_BIT;
339 needs_shadow = true;
340 }
341
342 ok = isl_surf_init(&dev->isl_dev, &anv_surf->isl,
343 .dim = vk_to_isl_surf_dim[vk_info->imageType],
344 .format = plane_format.isl_format,
345 .width = image->extent.width / plane_format.denominator_scales[0],
346 .height = image->extent.height / plane_format.denominator_scales[1],
347 .depth = image->extent.depth,
348 .levels = vk_info->mipLevels,
349 .array_len = vk_info->arrayLayers,
350 .samples = vk_info->samples,
351 .min_alignment_B = 0,
352 .row_pitch_B = anv_info->stride,
353 .usage = usage,
354 .tiling_flags = tiling_flags);
355
356 if (!ok)
357 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
358
359 image->planes[plane].aux_usage = ISL_AUX_USAGE_NONE;
360
361 add_surface(image, anv_surf, plane);
362
363 /* If an image is created as BLOCK_TEXEL_VIEW_COMPATIBLE, then we need to
364 * create an identical tiled shadow surface for use while texturing so we
365 * don't get garbage performance.
366 */
367 if (needs_shadow) {
368 assert(aspect == VK_IMAGE_ASPECT_COLOR_BIT);
369 assert(tiling_flags == ISL_TILING_LINEAR_BIT);
370
371 ok = isl_surf_init(&dev->isl_dev, &image->planes[plane].shadow_surface.isl,
372 .dim = vk_to_isl_surf_dim[vk_info->imageType],
373 .format = plane_format.isl_format,
374 .width = image->extent.width,
375 .height = image->extent.height,
376 .depth = image->extent.depth,
377 .levels = vk_info->mipLevels,
378 .array_len = vk_info->arrayLayers,
379 .samples = vk_info->samples,
380 .min_alignment_B = 0,
381 .row_pitch_B = anv_info->stride,
382 .usage = usage,
383 .tiling_flags = ISL_TILING_ANY_MASK);
384
385 /* isl_surf_init() will fail only if provided invalid input. Invalid input
386 * is illegal in Vulkan.
387 */
388 assert(ok);
389
390 add_surface(image, &image->planes[plane].shadow_surface, plane);
391 }
392
393 /* Add a HiZ surface to a depth buffer that will be used for rendering.
394 */
395 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
396 /* We don't advertise that depth buffers could be used as storage
397 * images.
398 */
399 assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
400
401 /* Allow the user to control HiZ enabling. Disable by default on gen7
402 * because resolves are not currently implemented pre-BDW.
403 */
404 if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
405 /* It will never be used as an attachment, HiZ is pointless. */
406 } else if (dev->info.gen == 7) {
407 anv_perf_warn(dev->instance, image, "Implement gen7 HiZ");
408 } else if (vk_info->mipLevels > 1) {
409 anv_perf_warn(dev->instance, image, "Enable multi-LOD HiZ");
410 } else if (vk_info->arrayLayers > 1) {
411 anv_perf_warn(dev->instance, image,
412 "Implement multi-arrayLayer HiZ clears and resolves");
413 } else if (dev->info.gen == 8 && vk_info->samples > 1) {
414 anv_perf_warn(dev->instance, image, "Enable gen8 multisampled HiZ");
415 } else if (!unlikely(INTEL_DEBUG & DEBUG_NO_HIZ)) {
416 assert(image->planes[plane].aux_surface.isl.size_B == 0);
417 ok = isl_surf_get_hiz_surf(&dev->isl_dev,
418 &image->planes[plane].surface.isl,
419 &image->planes[plane].aux_surface.isl);
420 assert(ok);
421 add_surface(image, &image->planes[plane].aux_surface, plane);
422 image->planes[plane].aux_usage = ISL_AUX_USAGE_HIZ;
423 }
424 } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) && vk_info->samples == 1) {
425 /* TODO: Disallow compression with :
426 *
427 * 1) non multiplanar images (We appear to hit a sampler bug with
428 * CCS & R16G16 format. Putting the clear state a page/4096bytes
429 * further fixes the issue).
430 *
431 * 2) alias images, because they might be aliases of images
432 * described in 1)
433 *
434 * 3) compression disabled by debug
435 */
436 const bool allow_compression =
437 image->n_planes == 1 &&
438 (vk_info->flags & VK_IMAGE_CREATE_ALIAS_BIT) == 0 &&
439 likely((INTEL_DEBUG & DEBUG_NO_RBC) == 0);
440
441 if (allow_compression) {
442 assert(image->planes[plane].aux_surface.isl.size_B == 0);
443 ok = isl_surf_get_ccs_surf(&dev->isl_dev,
444 &image->planes[plane].surface.isl,
445 &image->planes[plane].aux_surface.isl, 0);
446 if (ok) {
447
448 /* Disable CCS when it is not useful (i.e., when you can't render
449 * to the image with CCS enabled).
450 */
451 if (!isl_format_supports_rendering(&dev->info,
452 plane_format.isl_format)) {
453 /* While it may be technically possible to enable CCS for this
454 * image, we currently don't have things hooked up to get it
455 * working.
456 */
457 anv_perf_warn(dev->instance, image,
458 "This image format doesn't support rendering. "
459 "Not allocating an CCS buffer.");
460 image->planes[plane].aux_surface.isl.size_B = 0;
461 return VK_SUCCESS;
462 }
463
464 add_surface(image, &image->planes[plane].aux_surface, plane);
465 add_aux_state_tracking_buffer(image, plane, dev);
466
467 /* For images created without MUTABLE_FORMAT_BIT set, we know that
468 * they will always be used with the original format. In
469 * particular, they will always be used with a format that
470 * supports color compression. If it's never used as a storage
471 * image, then it will only be used through the sampler or the as
472 * a render target. This means that it's safe to just leave
473 * compression on at all times for these formats.
474 */
475 if (!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
476 all_formats_ccs_e_compatible(&dev->info, vk_info)) {
477 image->planes[plane].aux_usage = ISL_AUX_USAGE_CCS_E;
478 }
479 }
480 }
481 } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) && vk_info->samples > 1) {
482 assert(!(vk_info->usage & VK_IMAGE_USAGE_STORAGE_BIT));
483 assert(image->planes[plane].aux_surface.isl.size_B == 0);
484 ok = isl_surf_get_mcs_surf(&dev->isl_dev,
485 &image->planes[plane].surface.isl,
486 &image->planes[plane].aux_surface.isl);
487 if (ok) {
488 add_surface(image, &image->planes[plane].aux_surface, plane);
489 add_aux_state_tracking_buffer(image, plane, dev);
490 image->planes[plane].aux_usage = ISL_AUX_USAGE_MCS;
491 }
492 }
493
494 assert((image->planes[plane].offset + image->planes[plane].size) == image->size);
495
496 /* Upper bound of the last surface should be smaller than the plane's
497 * size.
498 */
499 assert((MAX2(image->planes[plane].surface.offset,
500 image->planes[plane].aux_surface.offset) +
501 (image->planes[plane].aux_surface.isl.size_B > 0 ?
502 image->planes[plane].aux_surface.isl.size_B :
503 image->planes[plane].surface.isl.size_B)) <=
504 (image->planes[plane].offset + image->planes[plane].size));
505
506 if (image->planes[plane].aux_surface.isl.size_B) {
507 /* assert(image->planes[plane].fast_clear_state_offset == */
508 /* (image->planes[plane].aux_surface.offset + image->planes[plane].aux_surface.isl.size_B)); */
509 assert(image->planes[plane].fast_clear_state_offset <
510 (image->planes[plane].offset + image->planes[plane].size));
511 }
512
513 return VK_SUCCESS;
514 }
515
516 static uint32_t
517 score_drm_format_mod(uint64_t modifier)
518 {
519 switch (modifier) {
520 case DRM_FORMAT_MOD_LINEAR: return 1;
521 case I915_FORMAT_MOD_X_TILED: return 2;
522 case I915_FORMAT_MOD_Y_TILED: return 3;
523 case I915_FORMAT_MOD_Y_TILED_CCS: return 4;
524 default: unreachable("bad DRM format modifier");
525 }
526 }
527
528 static const struct isl_drm_modifier_info *
529 choose_drm_format_mod(const struct anv_physical_device *device,
530 uint32_t modifier_count, const uint64_t *modifiers)
531 {
532 uint64_t best_mod = UINT64_MAX;
533 uint32_t best_score = 0;
534
535 for (uint32_t i = 0; i < modifier_count; ++i) {
536 uint32_t score = score_drm_format_mod(modifiers[i]);
537 if (score > best_score) {
538 best_mod = modifiers[i];
539 best_score = score;
540 }
541 }
542
543 if (best_score > 0)
544 return isl_drm_modifier_get_info(best_mod);
545 else
546 return NULL;
547 }
548
549 VkResult
550 anv_image_create(VkDevice _device,
551 const struct anv_image_create_info *create_info,
552 const VkAllocationCallbacks* alloc,
553 VkImage *pImage)
554 {
555 ANV_FROM_HANDLE(anv_device, device, _device);
556 const VkImageCreateInfo *pCreateInfo = create_info->vk_info;
557 const struct isl_drm_modifier_info *isl_mod_info = NULL;
558 struct anv_image *image = NULL;
559 VkResult r;
560
561 assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
562
563 const struct wsi_image_create_info *wsi_info =
564 vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA);
565 if (wsi_info && wsi_info->modifier_count > 0) {
566 isl_mod_info = choose_drm_format_mod(&device->instance->physicalDevice,
567 wsi_info->modifier_count,
568 wsi_info->modifiers);
569 assert(isl_mod_info);
570 }
571
572 anv_assert(pCreateInfo->mipLevels > 0);
573 anv_assert(pCreateInfo->arrayLayers > 0);
574 anv_assert(pCreateInfo->samples > 0);
575 anv_assert(pCreateInfo->extent.width > 0);
576 anv_assert(pCreateInfo->extent.height > 0);
577 anv_assert(pCreateInfo->extent.depth > 0);
578
579 image = vk_zalloc2(&device->alloc, alloc, sizeof(*image), 8,
580 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
581 if (!image)
582 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
583
584 image->type = pCreateInfo->imageType;
585 image->extent = pCreateInfo->extent;
586 image->vk_format = pCreateInfo->format;
587 image->format = anv_get_format(pCreateInfo->format);
588 image->aspects = vk_format_aspects(image->vk_format);
589 image->levels = pCreateInfo->mipLevels;
590 image->array_size = pCreateInfo->arrayLayers;
591 image->samples = pCreateInfo->samples;
592 image->usage = pCreateInfo->usage;
593 image->create_flags = pCreateInfo->flags;
594 image->tiling = pCreateInfo->tiling;
595 image->disjoint = pCreateInfo->flags & VK_IMAGE_CREATE_DISJOINT_BIT;
596 image->needs_set_tiling = wsi_info && wsi_info->scanout;
597 image->drm_format_mod = isl_mod_info ? isl_mod_info->modifier :
598 DRM_FORMAT_MOD_INVALID;
599
600 const struct anv_format *format = anv_get_format(image->vk_format);
601 assert(format != NULL);
602
603 const isl_tiling_flags_t isl_tiling_flags =
604 choose_isl_tiling_flags(create_info, isl_mod_info,
605 image->needs_set_tiling);
606
607 image->n_planes = format->n_planes;
608
609 uint32_t b;
610 for_each_bit(b, image->aspects) {
611 r = make_surface(device, image, create_info, isl_tiling_flags,
612 (1 << b));
613 if (r != VK_SUCCESS)
614 goto fail;
615 }
616
617 *pImage = anv_image_to_handle(image);
618
619 return VK_SUCCESS;
620
621 fail:
622 if (image)
623 vk_free2(&device->alloc, alloc, image);
624
625 return r;
626 }
627
628 VkResult
629 anv_CreateImage(VkDevice device,
630 const VkImageCreateInfo *pCreateInfo,
631 const VkAllocationCallbacks *pAllocator,
632 VkImage *pImage)
633 {
634 const VkNativeBufferANDROID *gralloc_info =
635 vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
636
637 if (gralloc_info)
638 return anv_image_from_gralloc(device, pCreateInfo, gralloc_info,
639 pAllocator, pImage);
640
641 return anv_image_create(device,
642 &(struct anv_image_create_info) {
643 .vk_info = pCreateInfo,
644 },
645 pAllocator,
646 pImage);
647 }
648
649 void
650 anv_DestroyImage(VkDevice _device, VkImage _image,
651 const VkAllocationCallbacks *pAllocator)
652 {
653 ANV_FROM_HANDLE(anv_device, device, _device);
654 ANV_FROM_HANDLE(anv_image, image, _image);
655
656 if (!image)
657 return;
658
659 for (uint32_t p = 0; p < image->n_planes; ++p) {
660 if (image->planes[p].bo_is_owned) {
661 assert(image->planes[p].address.bo != NULL);
662 anv_bo_cache_release(device, &device->bo_cache,
663 image->planes[p].address.bo);
664 }
665 }
666
667 vk_free2(&device->alloc, pAllocator, image);
668 }
669
670 static void anv_image_bind_memory_plane(struct anv_device *device,
671 struct anv_image *image,
672 uint32_t plane,
673 struct anv_device_memory *memory,
674 uint32_t memory_offset)
675 {
676 assert(!image->planes[plane].bo_is_owned);
677
678 if (!memory) {
679 image->planes[plane].address = ANV_NULL_ADDRESS;
680 return;
681 }
682
683 image->planes[plane].address = (struct anv_address) {
684 .bo = memory->bo,
685 .offset = memory_offset,
686 };
687 }
688
689 VkResult anv_BindImageMemory(
690 VkDevice _device,
691 VkImage _image,
692 VkDeviceMemory _memory,
693 VkDeviceSize memoryOffset)
694 {
695 ANV_FROM_HANDLE(anv_device, device, _device);
696 ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
697 ANV_FROM_HANDLE(anv_image, image, _image);
698
699 uint32_t aspect_bit;
700 anv_foreach_image_aspect_bit(aspect_bit, image, image->aspects) {
701 uint32_t plane =
702 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
703 anv_image_bind_memory_plane(device, image, plane, mem, memoryOffset);
704 }
705
706 return VK_SUCCESS;
707 }
708
709 VkResult anv_BindImageMemory2(
710 VkDevice _device,
711 uint32_t bindInfoCount,
712 const VkBindImageMemoryInfo* pBindInfos)
713 {
714 ANV_FROM_HANDLE(anv_device, device, _device);
715
716 for (uint32_t i = 0; i < bindInfoCount; i++) {
717 const VkBindImageMemoryInfo *bind_info = &pBindInfos[i];
718 ANV_FROM_HANDLE(anv_device_memory, mem, bind_info->memory);
719 ANV_FROM_HANDLE(anv_image, image, bind_info->image);
720 VkImageAspectFlags aspects = image->aspects;
721
722 vk_foreach_struct_const(s, bind_info->pNext) {
723 switch (s->sType) {
724 case VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO: {
725 const VkBindImagePlaneMemoryInfo *plane_info =
726 (const VkBindImagePlaneMemoryInfo *) s;
727
728 aspects = plane_info->planeAspect;
729 break;
730 }
731 default:
732 anv_debug_ignored_stype(s->sType);
733 break;
734 }
735 }
736
737 uint32_t aspect_bit;
738 anv_foreach_image_aspect_bit(aspect_bit, image, aspects) {
739 uint32_t plane =
740 anv_image_aspect_to_plane(image->aspects, 1UL << aspect_bit);
741 anv_image_bind_memory_plane(device, image, plane,
742 mem, bind_info->memoryOffset);
743 }
744 }
745
746 return VK_SUCCESS;
747 }
748
749 void anv_GetImageSubresourceLayout(
750 VkDevice device,
751 VkImage _image,
752 const VkImageSubresource* subresource,
753 VkSubresourceLayout* layout)
754 {
755 ANV_FROM_HANDLE(anv_image, image, _image);
756
757 const struct anv_surface *surface;
758 if (subresource->aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT_KHR &&
759 image->drm_format_mod != DRM_FORMAT_MOD_INVALID &&
760 isl_drm_modifier_has_aux(image->drm_format_mod))
761 surface = &image->planes[0].aux_surface;
762 else
763 surface = get_surface(image, subresource->aspectMask);
764
765 assert(__builtin_popcount(subresource->aspectMask) == 1);
766
767 layout->offset = surface->offset;
768 layout->rowPitch = surface->isl.row_pitch_B;
769 layout->depthPitch = isl_surf_get_array_pitch(&surface->isl);
770 layout->arrayPitch = isl_surf_get_array_pitch(&surface->isl);
771
772 if (subresource->mipLevel > 0 || subresource->arrayLayer > 0) {
773 assert(surface->isl.tiling == ISL_TILING_LINEAR);
774
775 uint32_t offset_B;
776 isl_surf_get_image_offset_B_tile_sa(&surface->isl,
777 subresource->mipLevel,
778 subresource->arrayLayer,
779 0 /* logical_z_offset_px */,
780 &offset_B, NULL, NULL);
781 layout->offset += offset_B;
782 layout->size = layout->rowPitch * anv_minify(image->extent.height,
783 subresource->mipLevel);
784 } else {
785 layout->size = surface->isl.size_B;
786 }
787 }
788
789 /**
790 * This function determines the optimal buffer to use for a given
791 * VkImageLayout and other pieces of information needed to make that
792 * determination. This does not determine the optimal buffer to use
793 * during a resolve operation.
794 *
795 * @param devinfo The device information of the Intel GPU.
796 * @param image The image that may contain a collection of buffers.
797 * @param aspect The aspect of the image to be accessed.
798 * @param layout The current layout of the image aspect(s).
799 *
800 * @return The primary buffer that should be used for the given layout.
801 */
802 enum isl_aux_usage
803 anv_layout_to_aux_usage(const struct gen_device_info * const devinfo,
804 const struct anv_image * const image,
805 const VkImageAspectFlagBits aspect,
806 const VkImageLayout layout)
807 {
808 /* Validate the inputs. */
809
810 /* The devinfo is needed as the optimal buffer varies across generations. */
811 assert(devinfo != NULL);
812
813 /* The layout of a NULL image is not properly defined. */
814 assert(image != NULL);
815
816 /* The aspect must be exactly one of the image aspects. */
817 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
818
819 /* Determine the optimal buffer. */
820
821 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
822
823 /* If there is no auxiliary surface allocated, we must use the one and only
824 * main buffer.
825 */
826 if (image->planes[plane].aux_surface.isl.size_B == 0)
827 return ISL_AUX_USAGE_NONE;
828
829 /* All images that use an auxiliary surface are required to be tiled. */
830 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
831
832 /* Stencil has no aux */
833 assert(aspect != VK_IMAGE_ASPECT_STENCIL_BIT);
834
835 switch (layout) {
836
837 /* Invalid Layouts */
838 case VK_IMAGE_LAYOUT_RANGE_SIZE:
839 case VK_IMAGE_LAYOUT_MAX_ENUM:
840 unreachable("Invalid image layout.");
841
842 /* Undefined layouts
843 *
844 * The pre-initialized layout is equivalent to the undefined layout for
845 * optimally-tiled images. We can only do color compression (CCS or HiZ)
846 * on tiled images.
847 */
848 case VK_IMAGE_LAYOUT_UNDEFINED:
849 case VK_IMAGE_LAYOUT_PREINITIALIZED:
850 return ISL_AUX_USAGE_NONE;
851
852
853 /* Transfer Layouts
854 */
855 case VK_IMAGE_LAYOUT_GENERAL:
856 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
857 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
858 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
859 /* This buffer could be a depth buffer used in a transfer operation.
860 * BLORP currently doesn't use HiZ for transfer operations so we must
861 * use the main buffer for this layout. TODO: Enable HiZ in BLORP.
862 */
863 assert(image->planes[plane].aux_usage == ISL_AUX_USAGE_HIZ);
864 return ISL_AUX_USAGE_NONE;
865 } else {
866 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
867 return image->planes[plane].aux_usage;
868 }
869
870
871 /* Sampling Layouts */
872 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
873 case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
874 assert((image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
875 /* Fall-through */
876 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
877 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
878 if (anv_can_sample_with_hiz(devinfo, image))
879 return ISL_AUX_USAGE_HIZ;
880 else
881 return ISL_AUX_USAGE_NONE;
882 } else {
883 return image->planes[plane].aux_usage;
884 }
885
886
887 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
888 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
889
890 /* When handing the image off to the presentation engine, we need to
891 * ensure that things are properly resolved. For images with no
892 * modifier, we assume that they follow the old rules and always need
893 * a full resolve because the PE doesn't understand any form of
894 * compression. For images with modifiers, we use the aux usage from
895 * the modifier.
896 */
897 const struct isl_drm_modifier_info *mod_info =
898 isl_drm_modifier_get_info(image->drm_format_mod);
899 return mod_info ? mod_info->aux_usage : ISL_AUX_USAGE_NONE;
900 }
901
902
903 /* Rendering Layouts */
904 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
905 assert(aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
906 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_NONE) {
907 assert(image->samples == 1);
908 return ISL_AUX_USAGE_CCS_D;
909 } else {
910 assert(image->planes[plane].aux_usage != ISL_AUX_USAGE_CCS_D);
911 return image->planes[plane].aux_usage;
912 }
913
914 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
915 case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
916 assert(aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
917 return ISL_AUX_USAGE_HIZ;
918
919 case VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR:
920 unreachable("VK_KHR_shared_presentable_image is unsupported");
921
922 case VK_IMAGE_LAYOUT_FRAGMENT_DENSITY_MAP_OPTIMAL_EXT:
923 unreachable("VK_EXT_fragment_density_map is unsupported");
924
925 case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
926 unreachable("VK_NV_shading_rate_image is unsupported");
927 }
928
929 /* If the layout isn't recognized in the exhaustive switch above, the
930 * VkImageLayout value is not defined in vulkan.h.
931 */
932 unreachable("layout is not a VkImageLayout enumeration member.");
933 }
934
935 /**
936 * This function returns the level of unresolved fast-clear support of the
937 * given image in the given VkImageLayout.
938 *
939 * @param devinfo The device information of the Intel GPU.
940 * @param image The image that may contain a collection of buffers.
941 * @param aspect The aspect of the image to be accessed.
942 * @param layout The current layout of the image aspect(s).
943 */
944 enum anv_fast_clear_type
945 anv_layout_to_fast_clear_type(const struct gen_device_info * const devinfo,
946 const struct anv_image * const image,
947 const VkImageAspectFlagBits aspect,
948 const VkImageLayout layout)
949 {
950 /* The aspect must be exactly one of the image aspects. */
951 assert(util_bitcount(aspect) == 1 && (aspect & image->aspects));
952
953 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
954
955 /* If there is no auxiliary surface allocated, there are no fast-clears */
956 if (image->planes[plane].aux_surface.isl.size_B == 0)
957 return ANV_FAST_CLEAR_NONE;
958
959 /* All images that use an auxiliary surface are required to be tiled. */
960 assert(image->tiling == VK_IMAGE_TILING_OPTIMAL);
961
962 /* Stencil has no aux */
963 assert(aspect != VK_IMAGE_ASPECT_STENCIL_BIT);
964
965 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
966 /* For depth images (with HiZ), the layout supports fast-clears if and
967 * only if it supports HiZ. However, we only support fast-clears to the
968 * default depth value.
969 */
970 enum isl_aux_usage aux_usage =
971 anv_layout_to_aux_usage(devinfo, image, aspect, layout);
972 return aux_usage == ISL_AUX_USAGE_HIZ ?
973 ANV_FAST_CLEAR_DEFAULT_VALUE : ANV_FAST_CLEAR_NONE;
974 }
975
976 assert(image->aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV);
977
978 /* We don't support MSAA fast-clears on Ivybridge or Bay Trail because they
979 * lack the MI ALU which we need to determine the predicates.
980 */
981 if (devinfo->gen == 7 && !devinfo->is_haswell && image->samples > 1)
982 return ANV_FAST_CLEAR_NONE;
983
984 switch (layout) {
985 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
986 return ANV_FAST_CLEAR_ANY;
987
988 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR: {
989 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
990 #ifndef NDEBUG
991 /* We do not yet support any modifiers which support clear color so we
992 * just always return NONE. One day, this will change.
993 */
994 const struct isl_drm_modifier_info *mod_info =
995 isl_drm_modifier_get_info(image->drm_format_mod);
996 assert(!mod_info || !mod_info->supports_clear_color);
997 #endif
998 return ANV_FAST_CLEAR_NONE;
999 }
1000
1001 default:
1002 /* If the image has MCS or CCS_E enabled all the time then we can use
1003 * fast-clear as long as the clear color is the default value of zero
1004 * since this is the default value we program into every surface state
1005 * used for texturing.
1006 */
1007 if (image->planes[plane].aux_usage == ISL_AUX_USAGE_MCS ||
1008 image->planes[plane].aux_usage == ISL_AUX_USAGE_CCS_E)
1009 return ANV_FAST_CLEAR_DEFAULT_VALUE;
1010 else
1011 return ANV_FAST_CLEAR_NONE;
1012 }
1013 }
1014
1015
1016 static struct anv_state
1017 alloc_surface_state(struct anv_device *device)
1018 {
1019 return anv_state_pool_alloc(&device->surface_state_pool, 64, 64);
1020 }
1021
1022 static enum isl_channel_select
1023 remap_swizzle(VkComponentSwizzle swizzle, VkComponentSwizzle component,
1024 struct isl_swizzle format_swizzle)
1025 {
1026 if (swizzle == VK_COMPONENT_SWIZZLE_IDENTITY)
1027 swizzle = component;
1028
1029 switch (swizzle) {
1030 case VK_COMPONENT_SWIZZLE_ZERO: return ISL_CHANNEL_SELECT_ZERO;
1031 case VK_COMPONENT_SWIZZLE_ONE: return ISL_CHANNEL_SELECT_ONE;
1032 case VK_COMPONENT_SWIZZLE_R: return format_swizzle.r;
1033 case VK_COMPONENT_SWIZZLE_G: return format_swizzle.g;
1034 case VK_COMPONENT_SWIZZLE_B: return format_swizzle.b;
1035 case VK_COMPONENT_SWIZZLE_A: return format_swizzle.a;
1036 default:
1037 unreachable("Invalid swizzle");
1038 }
1039 }
1040
1041 void
1042 anv_image_fill_surface_state(struct anv_device *device,
1043 const struct anv_image *image,
1044 VkImageAspectFlagBits aspect,
1045 const struct isl_view *view_in,
1046 isl_surf_usage_flags_t view_usage,
1047 enum isl_aux_usage aux_usage,
1048 const union isl_color_value *clear_color,
1049 enum anv_image_view_state_flags flags,
1050 struct anv_surface_state *state_inout,
1051 struct brw_image_param *image_param_out)
1052 {
1053 uint32_t plane = anv_image_aspect_to_plane(image->aspects, aspect);
1054
1055 const struct anv_surface *surface = &image->planes[plane].surface,
1056 *aux_surface = &image->planes[plane].aux_surface;
1057
1058 struct isl_view view = *view_in;
1059 view.usage |= view_usage;
1060
1061 /* For texturing with VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL from a
1062 * compressed surface with a shadow surface, we use the shadow instead of
1063 * the primary surface. The shadow surface will be tiled, unlike the main
1064 * surface, so it should get significantly better performance.
1065 */
1066 if (image->planes[plane].shadow_surface.isl.size_B > 0 &&
1067 isl_format_is_compressed(view.format) &&
1068 (flags & ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL)) {
1069 assert(isl_format_is_compressed(surface->isl.format));
1070 assert(surface->isl.tiling == ISL_TILING_LINEAR);
1071 assert(image->planes[plane].shadow_surface.isl.tiling != ISL_TILING_LINEAR);
1072 surface = &image->planes[plane].shadow_surface;
1073 }
1074
1075 if (view_usage == ISL_SURF_USAGE_RENDER_TARGET_BIT)
1076 view.swizzle = anv_swizzle_for_render(view.swizzle);
1077
1078 /* If this is a HiZ buffer we can sample from with a programmable clear
1079 * value (SKL+), define the clear value to the optimal constant.
1080 */
1081 union isl_color_value default_clear_color = { .u32 = { 0, } };
1082 if (device->info.gen >= 9 && aux_usage == ISL_AUX_USAGE_HIZ)
1083 default_clear_color.f32[0] = ANV_HZ_FC_VAL;
1084 if (!clear_color)
1085 clear_color = &default_clear_color;
1086
1087 const struct anv_address address =
1088 anv_address_add(image->planes[plane].address, surface->offset);
1089
1090 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
1091 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY) &&
1092 !isl_has_matching_typed_storage_image_format(&device->info,
1093 view.format)) {
1094 /* In this case, we are a writeable storage buffer which needs to be
1095 * lowered to linear. All tiling and offset calculations will be done in
1096 * the shader.
1097 */
1098 assert(aux_usage == ISL_AUX_USAGE_NONE);
1099 isl_buffer_fill_state(&device->isl_dev, state_inout->state.map,
1100 .address = anv_address_physical(address),
1101 .size_B = surface->isl.size_B,
1102 .format = ISL_FORMAT_RAW,
1103 .stride_B = 1,
1104 .mocs = anv_mocs_for_bo(device, address.bo));
1105 state_inout->address = address,
1106 state_inout->aux_address = ANV_NULL_ADDRESS;
1107 state_inout->clear_address = ANV_NULL_ADDRESS;
1108 } else {
1109 if (view_usage == ISL_SURF_USAGE_STORAGE_BIT &&
1110 !(flags & ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY)) {
1111 /* Typed surface reads support a very limited subset of the shader
1112 * image formats. Translate it into the closest format the hardware
1113 * supports.
1114 */
1115 assert(aux_usage == ISL_AUX_USAGE_NONE);
1116 view.format = isl_lower_storage_image_format(&device->info,
1117 view.format);
1118 }
1119
1120 const struct isl_surf *isl_surf = &surface->isl;
1121
1122 struct isl_surf tmp_surf;
1123 uint32_t offset_B = 0, tile_x_sa = 0, tile_y_sa = 0;
1124 if (isl_format_is_compressed(surface->isl.format) &&
1125 !isl_format_is_compressed(view.format)) {
1126 /* We're creating an uncompressed view of a compressed surface. This
1127 * is allowed but only for a single level/layer.
1128 */
1129 assert(surface->isl.samples == 1);
1130 assert(view.levels == 1);
1131 assert(view.array_len == 1);
1132
1133 isl_surf_get_image_surf(&device->isl_dev, isl_surf,
1134 view.base_level,
1135 surface->isl.dim == ISL_SURF_DIM_3D ?
1136 0 : view.base_array_layer,
1137 surface->isl.dim == ISL_SURF_DIM_3D ?
1138 view.base_array_layer : 0,
1139 &tmp_surf,
1140 &offset_B, &tile_x_sa, &tile_y_sa);
1141
1142 /* The newly created image represents the one subimage we're
1143 * referencing with this view so it only has one array slice and
1144 * miplevel.
1145 */
1146 view.base_array_layer = 0;
1147 view.base_level = 0;
1148
1149 /* We're making an uncompressed view here. The image dimensions need
1150 * to be scaled down by the block size.
1151 */
1152 const struct isl_format_layout *fmtl =
1153 isl_format_get_layout(surface->isl.format);
1154 tmp_surf.format = view.format;
1155 tmp_surf.logical_level0_px.width =
1156 DIV_ROUND_UP(tmp_surf.logical_level0_px.width, fmtl->bw);
1157 tmp_surf.logical_level0_px.height =
1158 DIV_ROUND_UP(tmp_surf.logical_level0_px.height, fmtl->bh);
1159 tmp_surf.phys_level0_sa.width /= fmtl->bw;
1160 tmp_surf.phys_level0_sa.height /= fmtl->bh;
1161 tile_x_sa /= fmtl->bw;
1162 tile_y_sa /= fmtl->bh;
1163
1164 isl_surf = &tmp_surf;
1165
1166 if (device->info.gen <= 8) {
1167 assert(surface->isl.tiling == ISL_TILING_LINEAR);
1168 assert(tile_x_sa == 0);
1169 assert(tile_y_sa == 0);
1170 }
1171 }
1172
1173 state_inout->address = anv_address_add(address, offset_B);
1174
1175 struct anv_address aux_address = ANV_NULL_ADDRESS;
1176 if (aux_usage != ISL_AUX_USAGE_NONE) {
1177 aux_address = anv_address_add(image->planes[plane].address,
1178 aux_surface->offset);
1179 }
1180 state_inout->aux_address = aux_address;
1181
1182 struct anv_address clear_address = ANV_NULL_ADDRESS;
1183 if (device->info.gen >= 10 && aux_usage != ISL_AUX_USAGE_NONE) {
1184 if (aux_usage == ISL_AUX_USAGE_HIZ) {
1185 clear_address = (struct anv_address) {
1186 .bo = &device->hiz_clear_bo,
1187 .offset = 0,
1188 };
1189 } else {
1190 clear_address = anv_image_get_clear_color_addr(device, image, aspect);
1191 }
1192 }
1193 state_inout->clear_address = clear_address;
1194
1195 isl_surf_fill_state(&device->isl_dev, state_inout->state.map,
1196 .surf = isl_surf,
1197 .view = &view,
1198 .address = anv_address_physical(state_inout->address),
1199 .clear_color = *clear_color,
1200 .aux_surf = &aux_surface->isl,
1201 .aux_usage = aux_usage,
1202 .aux_address = anv_address_physical(aux_address),
1203 .clear_address = anv_address_physical(clear_address),
1204 .use_clear_address = !anv_address_is_null(clear_address),
1205 .mocs = anv_mocs_for_bo(device,
1206 state_inout->address.bo),
1207 .x_offset_sa = tile_x_sa,
1208 .y_offset_sa = tile_y_sa);
1209
1210 /* With the exception of gen8, the bottom 12 bits of the MCS base address
1211 * are used to store other information. This should be ok, however,
1212 * because the surface buffer addresses are always 4K page aligned.
1213 */
1214 uint32_t *aux_addr_dw = state_inout->state.map +
1215 device->isl_dev.ss.aux_addr_offset;
1216 assert((aux_address.offset & 0xfff) == 0);
1217 state_inout->aux_address.offset |= *aux_addr_dw & 0xfff;
1218
1219 if (device->info.gen >= 10 && clear_address.bo) {
1220 uint32_t *clear_addr_dw = state_inout->state.map +
1221 device->isl_dev.ss.clear_color_state_offset;
1222 assert((clear_address.offset & 0x3f) == 0);
1223 state_inout->clear_address.offset |= *clear_addr_dw & 0x3f;
1224 }
1225 }
1226
1227 anv_state_flush(device, state_inout->state);
1228
1229 if (image_param_out) {
1230 assert(view_usage == ISL_SURF_USAGE_STORAGE_BIT);
1231 isl_surf_fill_image_param(&device->isl_dev, image_param_out,
1232 &surface->isl, &view);
1233 }
1234 }
1235
1236 static VkImageAspectFlags
1237 remap_aspect_flags(VkImageAspectFlags view_aspects)
1238 {
1239 if (view_aspects & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) {
1240 if (util_bitcount(view_aspects) == 1)
1241 return VK_IMAGE_ASPECT_COLOR_BIT;
1242
1243 VkImageAspectFlags color_aspects = 0;
1244 for (uint32_t i = 0; i < util_bitcount(view_aspects); i++)
1245 color_aspects |= VK_IMAGE_ASPECT_PLANE_0_BIT << i;
1246 return color_aspects;
1247 }
1248 /* No special remapping needed for depth & stencil aspects. */
1249 return view_aspects;
1250 }
1251
1252 static uint32_t
1253 anv_image_aspect_get_planes(VkImageAspectFlags aspect_mask)
1254 {
1255 uint32_t planes = 0;
1256
1257 if (aspect_mask & (VK_IMAGE_ASPECT_COLOR_BIT |
1258 VK_IMAGE_ASPECT_DEPTH_BIT |
1259 VK_IMAGE_ASPECT_STENCIL_BIT |
1260 VK_IMAGE_ASPECT_PLANE_0_BIT))
1261 planes++;
1262 if (aspect_mask & VK_IMAGE_ASPECT_PLANE_1_BIT)
1263 planes++;
1264 if (aspect_mask & VK_IMAGE_ASPECT_PLANE_2_BIT)
1265 planes++;
1266
1267 if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0 &&
1268 (aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0)
1269 planes++;
1270
1271 return planes;
1272 }
1273
1274 VkResult
1275 anv_CreateImageView(VkDevice _device,
1276 const VkImageViewCreateInfo *pCreateInfo,
1277 const VkAllocationCallbacks *pAllocator,
1278 VkImageView *pView)
1279 {
1280 ANV_FROM_HANDLE(anv_device, device, _device);
1281 ANV_FROM_HANDLE(anv_image, image, pCreateInfo->image);
1282 struct anv_image_view *iview;
1283
1284 iview = vk_zalloc2(&device->alloc, pAllocator, sizeof(*iview), 8,
1285 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1286 if (iview == NULL)
1287 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1288
1289 const VkImageSubresourceRange *range = &pCreateInfo->subresourceRange;
1290
1291 assert(range->layerCount > 0);
1292 assert(range->baseMipLevel < image->levels);
1293
1294 const VkImageViewUsageCreateInfo *usage_info =
1295 vk_find_struct_const(pCreateInfo, IMAGE_VIEW_USAGE_CREATE_INFO);
1296 VkImageUsageFlags view_usage = usage_info ? usage_info->usage : image->usage;
1297 /* View usage should be a subset of image usage */
1298 assert((view_usage & ~image->usage) == 0);
1299 assert(view_usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
1300 VK_IMAGE_USAGE_STORAGE_BIT |
1301 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1302 VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
1303 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT));
1304
1305 switch (image->type) {
1306 default:
1307 unreachable("bad VkImageType");
1308 case VK_IMAGE_TYPE_1D:
1309 case VK_IMAGE_TYPE_2D:
1310 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1 <= image->array_size);
1311 break;
1312 case VK_IMAGE_TYPE_3D:
1313 assert(range->baseArrayLayer + anv_get_layerCount(image, range) - 1
1314 <= anv_minify(image->extent.depth, range->baseMipLevel));
1315 break;
1316 }
1317
1318 /* First expand aspects to the image's ones (for example
1319 * VK_IMAGE_ASPECT_COLOR_BIT will be converted to
1320 * VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT |
1321 * VK_IMAGE_ASPECT_PLANE_2_BIT for an image of format
1322 * VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM_KHR.
1323 */
1324 VkImageAspectFlags expanded_aspects =
1325 anv_image_expand_aspects(image, range->aspectMask);
1326
1327 iview->image = image;
1328
1329 /* Remap the expanded aspects for the image view. For example if only
1330 * VK_IMAGE_ASPECT_PLANE_1_BIT was given in range->aspectMask, we will
1331 * convert it to VK_IMAGE_ASPECT_COLOR_BIT since from the point of view of
1332 * the image view, it only has a single plane.
1333 */
1334 iview->aspect_mask = remap_aspect_flags(expanded_aspects);
1335 iview->n_planes = anv_image_aspect_get_planes(iview->aspect_mask);
1336 iview->vk_format = pCreateInfo->format;
1337
1338 iview->extent = (VkExtent3D) {
1339 .width = anv_minify(image->extent.width , range->baseMipLevel),
1340 .height = anv_minify(image->extent.height, range->baseMipLevel),
1341 .depth = anv_minify(image->extent.depth , range->baseMipLevel),
1342 };
1343
1344 /* Now go through the underlying image selected planes (computed in
1345 * expanded_aspects) and map them to planes in the image view.
1346 */
1347 uint32_t iaspect_bit, vplane = 0;
1348 anv_foreach_image_aspect_bit(iaspect_bit, image, expanded_aspects) {
1349 uint32_t iplane =
1350 anv_image_aspect_to_plane(image->aspects, 1UL << iaspect_bit);
1351 VkImageAspectFlags vplane_aspect =
1352 anv_plane_to_aspect(iview->aspect_mask, vplane);
1353 struct anv_format_plane format =
1354 anv_get_format_plane(&device->info, pCreateInfo->format,
1355 vplane_aspect, image->tiling);
1356
1357 iview->planes[vplane].image_plane = iplane;
1358
1359 iview->planes[vplane].isl = (struct isl_view) {
1360 .format = format.isl_format,
1361 .base_level = range->baseMipLevel,
1362 .levels = anv_get_levelCount(image, range),
1363 .base_array_layer = range->baseArrayLayer,
1364 .array_len = anv_get_layerCount(image, range),
1365 .swizzle = {
1366 .r = remap_swizzle(pCreateInfo->components.r,
1367 VK_COMPONENT_SWIZZLE_R, format.swizzle),
1368 .g = remap_swizzle(pCreateInfo->components.g,
1369 VK_COMPONENT_SWIZZLE_G, format.swizzle),
1370 .b = remap_swizzle(pCreateInfo->components.b,
1371 VK_COMPONENT_SWIZZLE_B, format.swizzle),
1372 .a = remap_swizzle(pCreateInfo->components.a,
1373 VK_COMPONENT_SWIZZLE_A, format.swizzle),
1374 },
1375 };
1376
1377 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_3D) {
1378 iview->planes[vplane].isl.base_array_layer = 0;
1379 iview->planes[vplane].isl.array_len = iview->extent.depth;
1380 }
1381
1382 if (pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE ||
1383 pCreateInfo->viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY) {
1384 iview->planes[vplane].isl.usage = ISL_SURF_USAGE_CUBE_BIT;
1385 } else {
1386 iview->planes[vplane].isl.usage = 0;
1387 }
1388
1389 if (view_usage & VK_IMAGE_USAGE_SAMPLED_BIT ||
1390 (view_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT &&
1391 !(iview->aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT))) {
1392 iview->planes[vplane].optimal_sampler_surface_state.state = alloc_surface_state(device);
1393 iview->planes[vplane].general_sampler_surface_state.state = alloc_surface_state(device);
1394
1395 enum isl_aux_usage general_aux_usage =
1396 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1397 VK_IMAGE_LAYOUT_GENERAL);
1398 enum isl_aux_usage optimal_aux_usage =
1399 anv_layout_to_aux_usage(&device->info, image, 1UL << iaspect_bit,
1400 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1401
1402 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1403 &iview->planes[vplane].isl,
1404 ISL_SURF_USAGE_TEXTURE_BIT,
1405 optimal_aux_usage, NULL,
1406 ANV_IMAGE_VIEW_STATE_TEXTURE_OPTIMAL,
1407 &iview->planes[vplane].optimal_sampler_surface_state,
1408 NULL);
1409
1410 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1411 &iview->planes[vplane].isl,
1412 ISL_SURF_USAGE_TEXTURE_BIT,
1413 general_aux_usage, NULL,
1414 0,
1415 &iview->planes[vplane].general_sampler_surface_state,
1416 NULL);
1417 }
1418
1419 /* NOTE: This one needs to go last since it may stomp isl_view.format */
1420 if (view_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1421 iview->planes[vplane].storage_surface_state.state = alloc_surface_state(device);
1422 iview->planes[vplane].writeonly_storage_surface_state.state = alloc_surface_state(device);
1423
1424 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1425 &iview->planes[vplane].isl,
1426 ISL_SURF_USAGE_STORAGE_BIT,
1427 ISL_AUX_USAGE_NONE, NULL,
1428 0,
1429 &iview->planes[vplane].storage_surface_state,
1430 &iview->planes[vplane].storage_image_param);
1431
1432 anv_image_fill_surface_state(device, image, 1ULL << iaspect_bit,
1433 &iview->planes[vplane].isl,
1434 ISL_SURF_USAGE_STORAGE_BIT,
1435 ISL_AUX_USAGE_NONE, NULL,
1436 ANV_IMAGE_VIEW_STATE_STORAGE_WRITE_ONLY,
1437 &iview->planes[vplane].writeonly_storage_surface_state,
1438 NULL);
1439 }
1440
1441 vplane++;
1442 }
1443
1444 *pView = anv_image_view_to_handle(iview);
1445
1446 return VK_SUCCESS;
1447 }
1448
1449 void
1450 anv_DestroyImageView(VkDevice _device, VkImageView _iview,
1451 const VkAllocationCallbacks *pAllocator)
1452 {
1453 ANV_FROM_HANDLE(anv_device, device, _device);
1454 ANV_FROM_HANDLE(anv_image_view, iview, _iview);
1455
1456 if (!iview)
1457 return;
1458
1459 for (uint32_t plane = 0; plane < iview->n_planes; plane++) {
1460 if (iview->planes[plane].optimal_sampler_surface_state.state.alloc_size > 0) {
1461 anv_state_pool_free(&device->surface_state_pool,
1462 iview->planes[plane].optimal_sampler_surface_state.state);
1463 }
1464
1465 if (iview->planes[plane].general_sampler_surface_state.state.alloc_size > 0) {
1466 anv_state_pool_free(&device->surface_state_pool,
1467 iview->planes[plane].general_sampler_surface_state.state);
1468 }
1469
1470 if (iview->planes[plane].storage_surface_state.state.alloc_size > 0) {
1471 anv_state_pool_free(&device->surface_state_pool,
1472 iview->planes[plane].storage_surface_state.state);
1473 }
1474
1475 if (iview->planes[plane].writeonly_storage_surface_state.state.alloc_size > 0) {
1476 anv_state_pool_free(&device->surface_state_pool,
1477 iview->planes[plane].writeonly_storage_surface_state.state);
1478 }
1479 }
1480
1481 vk_free2(&device->alloc, pAllocator, iview);
1482 }
1483
1484
1485 VkResult
1486 anv_CreateBufferView(VkDevice _device,
1487 const VkBufferViewCreateInfo *pCreateInfo,
1488 const VkAllocationCallbacks *pAllocator,
1489 VkBufferView *pView)
1490 {
1491 ANV_FROM_HANDLE(anv_device, device, _device);
1492 ANV_FROM_HANDLE(anv_buffer, buffer, pCreateInfo->buffer);
1493 struct anv_buffer_view *view;
1494
1495 view = vk_alloc2(&device->alloc, pAllocator, sizeof(*view), 8,
1496 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1497 if (!view)
1498 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1499
1500 /* TODO: Handle the format swizzle? */
1501
1502 view->format = anv_get_isl_format(&device->info, pCreateInfo->format,
1503 VK_IMAGE_ASPECT_COLOR_BIT,
1504 VK_IMAGE_TILING_LINEAR);
1505 const uint32_t format_bs = isl_format_get_layout(view->format)->bpb / 8;
1506 view->range = anv_buffer_get_range(buffer, pCreateInfo->offset,
1507 pCreateInfo->range);
1508 view->range = align_down_npot_u32(view->range, format_bs);
1509
1510 view->address = anv_address_add(buffer->address, pCreateInfo->offset);
1511
1512 if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) {
1513 view->surface_state = alloc_surface_state(device);
1514
1515 anv_fill_buffer_surface_state(device, view->surface_state,
1516 view->format,
1517 view->address, view->range, format_bs);
1518 } else {
1519 view->surface_state = (struct anv_state){ 0 };
1520 }
1521
1522 if (buffer->usage & VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) {
1523 view->storage_surface_state = alloc_surface_state(device);
1524 view->writeonly_storage_surface_state = alloc_surface_state(device);
1525
1526 enum isl_format storage_format =
1527 isl_has_matching_typed_storage_image_format(&device->info,
1528 view->format) ?
1529 isl_lower_storage_image_format(&device->info, view->format) :
1530 ISL_FORMAT_RAW;
1531
1532 anv_fill_buffer_surface_state(device, view->storage_surface_state,
1533 storage_format,
1534 view->address, view->range,
1535 (storage_format == ISL_FORMAT_RAW ? 1 :
1536 isl_format_get_layout(storage_format)->bpb / 8));
1537
1538 /* Write-only accesses should use the original format. */
1539 anv_fill_buffer_surface_state(device, view->writeonly_storage_surface_state,
1540 view->format,
1541 view->address, view->range,
1542 isl_format_get_layout(view->format)->bpb / 8);
1543
1544 isl_buffer_fill_image_param(&device->isl_dev,
1545 &view->storage_image_param,
1546 view->format, view->range);
1547 } else {
1548 view->storage_surface_state = (struct anv_state){ 0 };
1549 view->writeonly_storage_surface_state = (struct anv_state){ 0 };
1550 }
1551
1552 *pView = anv_buffer_view_to_handle(view);
1553
1554 return VK_SUCCESS;
1555 }
1556
1557 void
1558 anv_DestroyBufferView(VkDevice _device, VkBufferView bufferView,
1559 const VkAllocationCallbacks *pAllocator)
1560 {
1561 ANV_FROM_HANDLE(anv_device, device, _device);
1562 ANV_FROM_HANDLE(anv_buffer_view, view, bufferView);
1563
1564 if (!view)
1565 return;
1566
1567 if (view->surface_state.alloc_size > 0)
1568 anv_state_pool_free(&device->surface_state_pool,
1569 view->surface_state);
1570
1571 if (view->storage_surface_state.alloc_size > 0)
1572 anv_state_pool_free(&device->surface_state_pool,
1573 view->storage_surface_state);
1574
1575 if (view->writeonly_storage_surface_state.alloc_size > 0)
1576 anv_state_pool_free(&device->surface_state_pool,
1577 view->writeonly_storage_surface_state);
1578
1579 vk_free2(&device->alloc, pAllocator, view);
1580 }
1581
1582 const struct anv_surface *
1583 anv_image_get_surface_for_aspect_mask(const struct anv_image *image,
1584 VkImageAspectFlags aspect_mask)
1585 {
1586 VkImageAspectFlags sanitized_mask;
1587
1588 switch (aspect_mask) {
1589 case VK_IMAGE_ASPECT_COLOR_BIT:
1590 assert(image->aspects == VK_IMAGE_ASPECT_COLOR_BIT);
1591 sanitized_mask = VK_IMAGE_ASPECT_COLOR_BIT;
1592 break;
1593 case VK_IMAGE_ASPECT_DEPTH_BIT:
1594 assert(image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT);
1595 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1596 break;
1597 case VK_IMAGE_ASPECT_STENCIL_BIT:
1598 assert(image->aspects & VK_IMAGE_ASPECT_STENCIL_BIT);
1599 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1600 break;
1601 case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
1602 /* FINISHME: The Vulkan spec (git a511ba2) requires support for
1603 * combined depth stencil formats. Specifically, it states:
1604 *
1605 * At least one of ename:VK_FORMAT_D24_UNORM_S8_UINT or
1606 * ename:VK_FORMAT_D32_SFLOAT_S8_UINT must be supported.
1607 *
1608 * Image views with both depth and stencil aspects are only valid for
1609 * render target attachments, in which case
1610 * cmd_buffer_emit_depth_stencil() will pick out both the depth and
1611 * stencil surfaces from the underlying surface.
1612 */
1613 if (image->aspects & VK_IMAGE_ASPECT_DEPTH_BIT) {
1614 sanitized_mask = VK_IMAGE_ASPECT_DEPTH_BIT;
1615 } else {
1616 assert(image->aspects == VK_IMAGE_ASPECT_STENCIL_BIT);
1617 sanitized_mask = VK_IMAGE_ASPECT_STENCIL_BIT;
1618 }
1619 break;
1620 case VK_IMAGE_ASPECT_PLANE_0_BIT:
1621 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1622 sanitized_mask = VK_IMAGE_ASPECT_PLANE_0_BIT;
1623 break;
1624 case VK_IMAGE_ASPECT_PLANE_1_BIT:
1625 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1626 sanitized_mask = VK_IMAGE_ASPECT_PLANE_1_BIT;
1627 break;
1628 case VK_IMAGE_ASPECT_PLANE_2_BIT:
1629 assert((image->aspects & ~VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) == 0);
1630 sanitized_mask = VK_IMAGE_ASPECT_PLANE_2_BIT;
1631 break;
1632 default:
1633 unreachable("image does not have aspect");
1634 return NULL;
1635 }
1636
1637 uint32_t plane = anv_image_aspect_to_plane(image->aspects, sanitized_mask);
1638 return &image->planes[plane].surface;
1639 }