+/**
+ * The return code indicates whether creation of the VkImage should continue
+ * or fail, not whether the creation of the aux surface succeeded. If the aux
+ * surface is not required (for example, by neither hardware nor DRM format
+ * modifier), then this may return VK_SUCCESS when creation of the aux surface
+ * fails.
+ */
+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)
+{
+ VkImageAspectFlags aspect = plane_format.aspect;
+ bool ok;
+
+ /* The aux surface must not be already added. */
+ assert(image->planes[plane].aux_surface.isl.size_B == 0);
+
+ if ((isl_extra_usage_flags & ISL_SURF_USAGE_DISABLE_AUX_BIT))
+ return VK_SUCCESS;
+
+ if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
+ /* We don't advertise that depth buffers could be used as storage
+ * images.
+ */
+ assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
+
+ /* Allow the user to control HiZ enabling. Disable by default on gen7
+ * because resolves are not currently implemented pre-BDW.
+ */
+ if (!(image->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
+ /* It will never be used as an attachment, HiZ is pointless. */
+ return VK_SUCCESS;
+ }
+
+ if (device->info.gen == 7) {
+ anv_perf_warn(device, image, "Implement gen7 HiZ");
+ return VK_SUCCESS;
+ }
+
+ if (image->levels > 1) {
+ anv_perf_warn(device, image, "Enable multi-LOD HiZ");
+ return VK_SUCCESS;
+ }
+
+ if (device->info.gen == 8 && image->samples > 1) {
+ anv_perf_warn(device, image, "Enable gen8 multisampled HiZ");
+ return VK_SUCCESS;
+ }
+
+ if (unlikely(INTEL_DEBUG & DEBUG_NO_HIZ))
+ return VK_SUCCESS;
+
+ ok = isl_surf_get_hiz_surf(&device->isl_dev,
+ &image->planes[plane].surface.isl,
+ &image->planes[plane].aux_surface.isl);
+ assert(ok);
+ if (!isl_surf_supports_ccs(&device->isl_dev,
+ &image->planes[plane].surface.isl)) {
+ image->planes[plane].aux_usage = ISL_AUX_USAGE_HIZ;
+ } else if (image->usage & (VK_IMAGE_USAGE_SAMPLED_BIT |
+ VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) &&
+ image->samples == 1) {
+ /* If it's used as an input attachment or a texture and it's
+ * single-sampled (this is a requirement for HiZ+CCS write-through
+ * mode), use write-through mode so that we don't need to resolve
+ * before texturing. This will make depth testing a bit slower but
+ * texturing faster.
+ *
+ * TODO: This is a heuristic trade-off; we haven't tuned it at all.
+ */
+ assert(device->info.gen >= 12);
+ image->planes[plane].aux_usage = ISL_AUX_USAGE_HIZ_CCS_WT;
+ } else {
+ assert(device->info.gen >= 12);
+ image->planes[plane].aux_usage = ISL_AUX_USAGE_HIZ_CCS;
+ }
+ add_surface(image, &image->planes[plane].aux_surface, plane);
+ } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) && image->samples == 1) {
+ if (image->n_planes != 1) {
+ /* Multiplanar images seem to hit a sampler bug with CCS and R16G16
+ * format. (Putting the clear state a page/4096bytes further fixes
+ * the issue).
+ */
+ return VK_SUCCESS;
+ }
+
+ if ((image->create_flags & VK_IMAGE_CREATE_ALIAS_BIT)) {
+ /* The image may alias a plane of a multiplanar image. Above we ban
+ * CCS on multiplanar images.
+ */
+ return VK_SUCCESS;
+ }
+
+ if (!isl_format_supports_rendering(&device->info,
+ plane_format.isl_format)) {
+ /* Disable CCS because it is not useful (we can't render to the image
+ * with CCS enabled). While it may be technically possible to enable
+ * CCS for this case, we currently don't have things hooked up to get
+ * it working.
+ */
+ anv_perf_warn(device, image,
+ "This image format doesn't support rendering. "
+ "Not allocating an CCS buffer.");
+ return VK_SUCCESS;
+ }
+
+ if (unlikely(INTEL_DEBUG & DEBUG_NO_RBC))
+ return VK_SUCCESS;
+
+ ok = isl_surf_get_ccs_surf(&device->isl_dev,
+ &image->planes[plane].surface.isl,
+ &image->planes[plane].aux_surface.isl,
+ NULL, 0);
+ if (!ok)
+ return VK_SUCCESS;
+
+ /* Choose aux usage */
+ if (!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT) &&
+ anv_formats_ccs_e_compatible(&device->info,
+ image->create_flags,
+ image->vk_format,
+ image->tiling,
+ fmt_list)) {
+ /* For images created without MUTABLE_FORMAT_BIT set, we know that
+ * they will always be used with the original format. In particular,
+ * they will always be used with a format that supports color
+ * compression. If it's never used as a storage image, then it will
+ * only be used through the sampler or the as a render target. This
+ * means that it's safe to just leave compression on at all times for
+ * these formats.
+ */
+ image->planes[plane].aux_usage = ISL_AUX_USAGE_CCS_E;
+ } else if (device->info.gen >= 12) {
+ anv_perf_warn(device, image,
+ "The CCS_D aux mode is not yet handled on "
+ "Gen12+. Not allocating a CCS buffer.");
+ image->planes[plane].aux_surface.isl.size_B = 0;
+ return VK_SUCCESS;
+ } else {
+ image->planes[plane].aux_usage = ISL_AUX_USAGE_CCS_D;
+ }
+
+ if (!device->physical->has_implicit_ccs)
+ add_surface(image, &image->planes[plane].aux_surface, plane);
+
+ add_aux_state_tracking_buffer(image, plane, device);
+ } else if ((aspect & VK_IMAGE_ASPECT_ANY_COLOR_BIT_ANV) && image->samples > 1) {
+ assert(!(image->usage & VK_IMAGE_USAGE_STORAGE_BIT));
+ ok = isl_surf_get_mcs_surf(&device->isl_dev,
+ &image->planes[plane].surface.isl,
+ &image->planes[plane].aux_surface.isl);
+ if (!ok)
+ return VK_SUCCESS;
+
+ image->planes[plane].aux_usage = ISL_AUX_USAGE_MCS;
+ add_surface(image, &image->planes[plane].aux_surface, plane);
+ add_aux_state_tracking_buffer(image, plane, device);
+ }
+
+ return VK_SUCCESS;
+}
+