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