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