anv: Use the YCbCr format info from common code
authorJason Ekstrand <jason@jlekstrand.net>
Wed, 31 Mar 2021 04:44:25 +0000 (23:44 -0500)
committerMarge Bot <emma+marge@anholt.net>
Mon, 16 Jan 2023 14:10:21 +0000 (14:10 +0000)
We still maintain our own table of formats but all of the conversion and
sampling info we pull from common code.

Acked-by: Alejandro PiƱeiro <apinheiro@igalia.com>
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19950>

src/intel/vulkan/anv_formats.c
src/intel/vulkan/anv_image.c
src/intel/vulkan/anv_nir_lower_ycbcr_textures.c
src/intel/vulkan/anv_private.h
src/intel/vulkan/genX_state.c

index cf0f437..bf14ad7 100644 (file)
@@ -51,7 +51,6 @@
    [VK_ENUM_OFFSET(__vk_fmt)] = { \
       .planes = { \
          { .isl_format = __hw_fmt, .swizzle = __swizzle, \
-           .denominator_scales = { 1, 1, }, \
            .aspect = VK_IMAGE_ASPECT_COLOR_BIT, \
          }, \
       }, \
@@ -66,7 +65,6 @@
    [VK_ENUM_OFFSET(__vk_fmt)] = { \
       .planes = { \
          { .isl_format = __hw_fmt, .swizzle = RGBA, \
-           .denominator_scales = { 1, 1, }, \
            .aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \
          }, \
       }, \
@@ -78,7 +76,6 @@
    [VK_ENUM_OFFSET(__vk_fmt)] = { \
       .planes = { \
          { .isl_format = __hw_fmt, .swizzle = RGBA, \
-           .denominator_scales = { 1, 1, }, \
            .aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \
          }, \
       }, \
    [VK_ENUM_OFFSET(__vk_fmt)] = { \
       .planes = { \
          { .isl_format = __fmt1, .swizzle = RGBA, \
-           .denominator_scales = { 1, 1, }, \
            .aspect = VK_IMAGE_ASPECT_DEPTH_BIT, \
          }, \
          { .isl_format = __fmt2, .swizzle = RGBA, \
-           .denominator_scales = { 1, 1, }, \
            .aspect = VK_IMAGE_ASPECT_STENCIL_BIT, \
          }, \
       }, \
       .vk_format = VK_FORMAT_UNDEFINED, \
    }
 
-#define y_plane(__plane, __hw_fmt, __swizzle, __ycbcr_swizzle, dhs, dvs) \
+#define ycbcr_plane(__plane, __hw_fmt, __swizzle) \
    { .isl_format = __hw_fmt, \
      .swizzle = __swizzle, \
-     .ycbcr_swizzle = __ycbcr_swizzle, \
-     .denominator_scales = { dhs, dvs, }, \
-     .has_chroma = false, \
-     .aspect = VK_IMAGE_ASPECT_PLANE_0_BIT, /* Y plane is always plane 0 */ \
-   }
-
-#define chroma_plane(__plane, __hw_fmt, __swizzle, __ycbcr_swizzle, dhs, dvs) \
-   { .isl_format = __hw_fmt, \
-     .swizzle = __swizzle, \
-     .ycbcr_swizzle = __ycbcr_swizzle, \
-     .denominator_scales = { dhs, dvs, }, \
-     .has_chroma = true, \
      .aspect = VK_IMAGE_ASPECT_PLANE_ ## __plane ## _BIT, \
    }
 
@@ -341,27 +324,27 @@ static const struct anv_format _4444_formats[] = {
 
 static const struct anv_format ycbcr_formats[] = {
    ycbcr_fmt(VK_FORMAT_G8B8G8R8_422_UNORM, 1,
-             y_plane(0, ISL_FORMAT_YCRCB_SWAPUV, RGBA, _ISL_SWIZZLE(BLUE, GREEN, RED, ZERO), 1, 1)),
+             ycbcr_plane(0, ISL_FORMAT_YCRCB_SWAPUV, RGBA)),
    ycbcr_fmt(VK_FORMAT_B8G8R8G8_422_UNORM, 1,
-             y_plane(0, ISL_FORMAT_YCRCB_SWAPUVY, RGBA, _ISL_SWIZZLE(BLUE, GREEN, RED, ZERO), 1, 1)),
+             ycbcr_plane(0, ISL_FORMAT_YCRCB_SWAPUVY, RGBA)),
    ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, 3,
-             y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 2),
-             chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 2)),
+             ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(2, ISL_FORMAT_R8_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, 2,
-             y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 2)),
+             ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM, 3,
-             y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 1),
-             chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 1)),
+             ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(2, ISL_FORMAT_R8_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G8_B8R8_2PLANE_422_UNORM, 2,
-             y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 1)),
+             ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R8G8_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM, 3,
-             y_plane(0, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(2, ISL_FORMAT_R8_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 1, 1)),
+             ycbcr_plane(0, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R8_UNORM, RGBA),
+             ycbcr_plane(2, ISL_FORMAT_R8_UNORM, RGBA)),
 
    fmt_unsupported(VK_FORMAT_R10X6_UNORM_PACK16),
    fmt_unsupported(VK_FORMAT_R10X6G10X6_UNORM_2PACK16),
@@ -390,23 +373,23 @@ static const struct anv_format ycbcr_formats[] = {
    fmt_unsupported(VK_FORMAT_B16G16R16G16_422_UNORM),
 
    ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, 3,
-             y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 2),
-             chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 2)),
+             ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(2, ISL_FORMAT_R16_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_420_UNORM, 2,
-             y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 2)),
+             ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM, 3,
-             y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 2, 1),
-             chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 2, 1)),
+             ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(2, ISL_FORMAT_R16_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G16_B16R16_2PLANE_422_UNORM, 2,
-             y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, RED, ZERO, ZERO), 2, 1)),
+             ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R16G16_UNORM, RGBA)),
    ycbcr_fmt(VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM, 3,
-             y_plane(0, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(GREEN, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(1, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(BLUE, ZERO, ZERO, ZERO), 1, 1),
-             chroma_plane(2, ISL_FORMAT_R16_UNORM, RGBA, _ISL_SWIZZLE(RED, ZERO, ZERO, ZERO), 1, 1)),
+             ycbcr_plane(0, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(1, ISL_FORMAT_R16_UNORM, RGBA),
+             ycbcr_plane(2, ISL_FORMAT_R16_UNORM, RGBA)),
 };
 
 #undef _fmt
@@ -684,10 +667,13 @@ anv_get_image_format_features2(const struct intel_device_info *devinfo,
       /* We can support cosited chroma locations when handle planes with our
        * own shader snippets.
        */
-      for (unsigned p = 0; p < anv_format->n_planes; p++) {
-         if (anv_format->planes[p].denominator_scales[0] > 1 ||
-             anv_format->planes[p].denominator_scales[1] > 1) {
-            flags |= VK_FORMAT_FEATURE_2_COSITED_CHROMA_SAMPLES_BIT;
+      const struct vk_format_ycbcr_info *ycbcr_info =
+         vk_format_get_ycbcr_info(vk_format);
+      assert(anv_format->n_planes == ycbcr_info->n_planes);
+      for (unsigned p = 0; p < ycbcr_info->n_planes; p++) {
+         if (ycbcr_info->planes[p].denominator_scales[0] > 1 ||
+             ycbcr_info->planes[p].denominator_scales[1] > 1) {
+            flags |= VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;
             break;
          }
       }
index f02f467..cb30e55 100644 (file)
@@ -827,11 +827,21 @@ add_primary_surface(struct anv_device *device,
    struct anv_surface *anv_surf = &image->planes[plane].primary_surface;
    bool ok;
 
+   uint32_t width = image->vk.extent.width;
+   uint32_t height = image->vk.extent.height;
+   const struct vk_format_ycbcr_info *ycbcr_info =
+      vk_format_get_ycbcr_info(image->vk.format);
+   if (ycbcr_info) {
+      assert(plane < ycbcr_info->n_planes);
+      width /= ycbcr_info->planes[plane].denominator_scales[0];
+      height /= ycbcr_info->planes[plane].denominator_scales[1];
+   }
+
    ok = isl_surf_init(&device->isl_dev, &anv_surf->isl,
       .dim = vk_to_isl_surf_dim[image->vk.image_type],
       .format = plane_format.isl_format,
-      .width = image->vk.extent.width / plane_format.denominator_scales[0],
-      .height = image->vk.extent.height / plane_format.denominator_scales[1],
+      .width = width,
+      .height = height,
       .depth = image->vk.extent.depth,
       .levels = image->vk.mip_levels,
       .array_len = image->vk.array_layers,
index 4e073e4..92109df 100644 (file)
@@ -26,6 +26,7 @@
 #include "nir/nir.h"
 #include "nir/nir_builder.h"
 #include "nir/nir_vulkan.h"
+#include "vk_format.h"
 
 struct ycbcr_state {
    nir_builder *builder;
@@ -33,6 +34,7 @@ struct ycbcr_state {
    nir_tex_instr *origin_tex;
    nir_deref_instr *tex_deref;
    const struct vk_ycbcr_conversion *conversion;
+   const struct vk_format_ycbcr_info *format_ycbcr_info;
 };
 
 /* TODO: we should probably replace this with a push constant/uniform. */
@@ -82,7 +84,7 @@ implicit_downsampled_coord(nir_builder *b,
 static nir_ssa_def *
 implicit_downsampled_coords(struct ycbcr_state *state,
                             nir_ssa_def *old_coords,
-                            const struct anv_format_plane *plane_format)
+                            const struct vk_format_ycbcr_plane *format_plane)
 {
    nir_builder *b = state->builder;
    const struct vk_ycbcr_conversion *conversion = state->conversion;
@@ -91,12 +93,12 @@ implicit_downsampled_coords(struct ycbcr_state *state,
    int c;
 
    for (c = 0; c < ARRAY_SIZE(conversion->chroma_offsets); c++) {
-      if (plane_format->denominator_scales[c] > 1 &&
+      if (format_plane->denominator_scales[c] > 1 &&
           conversion->chroma_offsets[c] == VK_CHROMA_LOCATION_COSITED_EVEN) {
          comp[c] = implicit_downsampled_coord(b,
                                               nir_channel(b, old_coords, c),
                                               nir_channel(b, image_size, c),
-                                              plane_format->denominator_scales[c]);
+                                              format_plane->denominator_scales[c]);
       } else {
          comp[c] = nir_channel(b, old_coords, c);
       }
@@ -115,8 +117,8 @@ create_plane_tex_instr_implicit(struct ycbcr_state *state,
 {
    nir_builder *b = state->builder;
    const struct vk_ycbcr_conversion *conversion = state->conversion;
-   const struct anv_format_plane *plane_format =
-      &anv_get_format(conversion->format)->planes[plane];
+   const struct vk_format_ycbcr_plane *format_plane =
+      &state->format_ycbcr_info->planes[plane];
    nir_tex_instr *old_tex = state->origin_tex;
    nir_tex_instr *tex = nir_tex_instr_create(b->shader, old_tex->num_srcs + 1);
 
@@ -125,12 +127,12 @@ create_plane_tex_instr_implicit(struct ycbcr_state *state,
 
       switch (old_tex->src[i].src_type) {
       case nir_tex_src_coord:
-         if (plane_format->has_chroma && conversion->chroma_reconstruction) {
+         if (format_plane->has_chroma && conversion->chroma_reconstruction) {
             assert(old_tex->src[i].src.is_ssa);
             tex->src[i].src =
                nir_src_for_ssa(implicit_downsampled_coords(state,
                                                            old_tex->src[i].src.ssa,
-                                                           plane_format));
+                                                           format_plane));
             break;
          }
          FALLTHROUGH;
@@ -163,16 +165,16 @@ create_plane_tex_instr_implicit(struct ycbcr_state *state,
 }
 
 static unsigned
-channel_to_component(enum isl_channel_select channel)
+swizzle_to_component(VkComponentSwizzle swizzle)
 {
-   switch (channel) {
-   case ISL_CHANNEL_SELECT_RED:
+   switch (swizzle) {
+   case VK_COMPONENT_SWIZZLE_R:
       return 0;
-   case ISL_CHANNEL_SELECT_GREEN:
+   case VK_COMPONENT_SWIZZLE_G:
       return 1;
-   case ISL_CHANNEL_SELECT_BLUE:
+   case VK_COMPONENT_SWIZZLE_B:
       return 2;
-   case ISL_CHANNEL_SELECT_ALPHA:
+   case VK_COMPONENT_SWIZZLE_A:
       return 3;
    default:
       unreachable("invalid channel");
@@ -180,24 +182,6 @@ channel_to_component(enum isl_channel_select channel)
    }
 }
 
-static enum isl_channel_select
-swizzle_channel(struct isl_swizzle swizzle, unsigned channel)
-{
-   switch (channel) {
-   case 0:
-      return swizzle.r;
-   case 1:
-      return swizzle.g;
-   case 2:
-      return swizzle.b;
-   case 3:
-      return swizzle.a;
-   default:
-      unreachable("invalid channel");
-      return 0;
-   }
-}
-
 static bool
 anv_nir_lower_ycbcr_textures_instr(nir_builder *builder,
                                    nir_instr *instr,
@@ -245,23 +229,28 @@ anv_nir_lower_ycbcr_textures_instr(nir_builder *builder,
    if (sampler->conversion == NULL)
       return false;
 
+   const struct vk_format_ycbcr_info *format_ycbcr_info =
+      vk_format_get_ycbcr_info(sampler->conversion->format);
+
    struct ycbcr_state state = {
       .builder = builder,
       .origin_tex = tex,
       .tex_deref = deref,
       .conversion = sampler->conversion,
+      .format_ycbcr_info = format_ycbcr_info,
    };
 
    builder->cursor = nir_before_instr(&tex->instr);
 
-   const struct anv_format *format = anv_get_format(state.conversion->format);
-   const struct isl_format_layout *y_isl_layout = NULL;
-   for (uint32_t p = 0; p < format->n_planes; p++) {
-      if (!format->planes[p].has_chroma)
-         y_isl_layout = isl_format_get_layout(format->planes[p].isl_format);
+   VkFormat y_format = VK_FORMAT_UNDEFINED;
+   for (uint32_t p = 0; p < format_ycbcr_info->n_planes; p++) {
+      if (!format_ycbcr_info->planes[p].has_chroma)
+         y_format = format_ycbcr_info->planes[p].format;
    }
-   assert(y_isl_layout != NULL);
-   uint8_t y_bpc = y_isl_layout->channels_array[0].bits;
+   assert(y_format != VK_FORMAT_UNDEFINED);
+   const struct util_format_description *y_format_desc =
+      util_format_description(vk_format_to_pipe_format(y_format));
+   uint8_t y_bpc = y_format_desc->channel[0].size;
 
    /* |ycbcr_comp| holds components in the order : Cr-Y-Cb */
    nir_ssa_def *zero = nir_imm_float(builder, 0.0f);
@@ -277,23 +266,23 @@ anv_nir_lower_ycbcr_textures_instr(nir_builder *builder,
     *
     *    R, G, B should respectively map to Cr, Y, Cb
     */
-   for (uint32_t p = 0; p < format->n_planes; p++) {
-      const struct anv_format_plane *plane_format = &format->planes[p];
+   for (uint32_t p = 0; p < format_ycbcr_info->n_planes; p++) {
+      const struct vk_format_ycbcr_plane *format_plane =
+         &format_ycbcr_info->planes[p];
       nir_ssa_def *plane_sample = create_plane_tex_instr_implicit(&state, p);
 
       for (uint32_t pc = 0; pc < 4; pc++) {
-         enum isl_channel_select ycbcr_swizzle =
-            swizzle_channel(plane_format->ycbcr_swizzle, pc);
-         if (ycbcr_swizzle == ISL_CHANNEL_SELECT_ZERO)
+         VkComponentSwizzle ycbcr_swizzle = format_plane->ycbcr_swizzle[pc];
+         if (ycbcr_swizzle == VK_COMPONENT_SWIZZLE_ZERO)
             continue;
 
-         unsigned ycbcr_component = channel_to_component(ycbcr_swizzle);
+         unsigned ycbcr_component = swizzle_to_component(ycbcr_swizzle);
          ycbcr_comp[ycbcr_component] = nir_channel(builder, plane_sample, pc);
 
          /* Also compute the number of bits for each component. */
-         const struct isl_format_layout *isl_layout =
-            isl_format_get_layout(plane_format->isl_format);
-         ycbcr_bpcs[ycbcr_component] = isl_layout->channels_array[pc].bits;
+         const struct util_format_description *plane_format_desc =
+            util_format_description(vk_format_to_pipe_format(format_plane->format));
+         ycbcr_bpcs[ycbcr_component] = plane_format_desc->channel[pc].size;
       }
    }
 
index aaf54e4..5ddb205 100644 (file)
@@ -3258,20 +3258,10 @@ struct anv_format_plane {
    enum isl_format isl_format:16;
    struct isl_swizzle swizzle;
 
-   /* Whether this plane contains chroma channels */
-   bool has_chroma;
-
-   /* For downscaling of YUV planes */
-   uint8_t denominator_scales[2];
-
-   /* How to map sampled ycbcr planes to a single 4 component element. */
-   struct isl_swizzle ycbcr_swizzle;
-
    /* What aspect is associated to this plane */
    VkImageAspectFlags aspect;
 };
 
-
 struct anv_format {
    struct anv_format_plane planes[3];
    VkFormat vk_format;
index da25a2a..e0e24c4 100644 (file)
@@ -938,6 +938,7 @@ VkResult genX(CreateSampler)(
          anv_get_format(sampler->conversion->format)->planes[0].isl_format :
          ISL_FORMAT_UNSUPPORTED;
       const bool isl_format_is_planar_yuv =
+         plane0_isl_format != ISL_FORMAT_UNSUPPORTED &&
          isl_format_is_yuv(plane0_isl_format) &&
          isl_format_is_planar(plane0_isl_format);