<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">On 04/10/17 00:54, Jason Ekstrand
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAOFGe95M0Pgkgww_RSGMZBySknSDy=xgh55sJJoFV3qBiXnAoA@mail.gmail.com">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">On Tue, Oct 3, 2017 at 9:29 AM,
            Lionel Landwerlin <span dir="ltr"><<a
                href="mailto:lionel.g.landwerlin@intel.com"
                target="_blank" moz-do-not-send="true">lionel.g.landwerlin@intel.com</a>></span>
            wrote:<br>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">This
              pass implements all the implicit conversions required by
              the<br>
              VK_KHR_sampler_ycbcr_<wbr>conversion specification.<br>
              <br>
              It also inserts plane sources onto sampling instructions
              that we then<br>
              let the pipeline layout pass deal with, when mapping
              things correctly<br>
              to descriptors.<br>
              <br>
              Signed-off-by: Lionel Landwerlin <<a
                href="mailto:lionel.g.landwerlin@intel.com"
                moz-do-not-send="true">lionel.g.landwerlin@intel.com</a><wbr>><br>
              ---<br>
               src/intel/Makefile.sources                       |   1 +<br>
               src/intel/vulkan/anv_nir.h                       |   3 +<br>
               src/intel/vulkan/anv_nir_<wbr>apply_pipeline_layout.c | 
              62 ++-<br>
               src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c  |
              468 +++++++++++++++++++++++<br>
               src/intel/vulkan/anv_pipeline.<wbr>c                  | 
               2 +<br>
               src/intel/vulkan/anv_private.h                   |  16 +-<br>
               6 files changed, 545 insertions(+), 7 deletions(-)<br>
               create mode 100644 src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c<br>
              <br>
              diff --git a/src/intel/Makefile.sources
              b/src/intel/Makefile.sources<br>
              index bca7a132b26..9672dcc252d 100644<br>
              --- a/src/intel/Makefile.sources<br>
              +++ b/src/intel/Makefile.sources<br>
              @@ -219,6 +219,7 @@ VULKAN_FILES := \<br>
                      vulkan/anv_nir_lower_input_<wbr>attachments.c \<br>
                      vulkan/anv_nir_lower_<wbr>multiview.c \<br>
                      vulkan/anv_nir_lower_push_<wbr>constants.c \<br>
              +       vulkan/anv_nir_lower_ycbcr_<wbr>textures.c \<br>
                      vulkan/anv_pass.c \<br>
                      vulkan/anv_pipeline.c \<br>
                      vulkan/anv_pipeline_cache.c \<br>
              diff --git a/src/intel/vulkan/anv_nir.h
              b/src/intel/vulkan/anv_nir.h<br>
              index 5b450b45cdf..0a06e3a1cf0 100644<br>
              --- a/src/intel/vulkan/anv_nir.h<br>
              +++ b/src/intel/vulkan/anv_nir.h<br>
              @@ -37,6 +37,9 @@ void anv_nir_lower_push_constants(<wbr>nir_shader
              *shader);<br>
              <br>
               bool anv_nir_lower_multiview(nir_<wbr>shader *shader,
              uint32_t view_mask);<br>
              <br>
              +void anv_nir_lower_ycbcr_textures(<wbr>nir_shader
              *shader,<br>
              +                                  struct anv_pipeline
              *pipeline);<br>
              +<br>
               void anv_nir_apply_pipeline_layout(<wbr>struct
              anv_pipeline *pipeline,<br>
                                                  nir_shader *shader,<br>
                                                  struct
              brw_stage_prog_data *prog_data,<br>
              diff --git a/src/intel/vulkan/anv_nir_<wbr>apply_pipeline_layout.c
              b/src/intel/vulkan/anv_nir_<wbr>apply_pipeline_layout.c<br>
              index 428cfdf42d1..7cd28debe09 100644<br>
              --- a/src/intel/vulkan/anv_nir_<wbr>apply_pipeline_layout.c<br>
              +++ b/src/intel/vulkan/anv_nir_<wbr>apply_pipeline_layout.c<br>
              @@ -131,7 +131,7 @@ lower_res_index_intrinsic(nir_<wbr>intrinsic_instr
              *intrin,<br>
               static void<br>
               lower_tex_deref(nir_tex_instr *tex, nir_deref_var *deref,<br>
                               unsigned *const_index, unsigned
              hw_binding_size,<br>
              -                nir_tex_src_type src_type,<br>
              +                nir_tex_src_type src_type, bool
              allow_indirect,<br>
                               struct apply_pipeline_layout_state
              *state)<br>
               {<br>
                  nir_builder *b = &state->builder;<br>
              @@ -141,6 +141,15 @@ lower_tex_deref(nir_tex_instr *tex,
              nir_deref_var *deref,<br>
                     nir_deref_array *deref_array =
              nir_deref_as_array(deref-><wbr>deref.child);<br>
              <br>
                     if (deref_array->deref_array_type ==
              nir_deref_array_type_indirect) {<br>
              +         /* From VK_KHR_sampler_ycbcr_<wbr>conversion:<br>
              +          *<br>
              +          * If sampler Y’CBCR conversion is enabled, the
              combined image<br>
              +          * sampler must be indexed only by constant
              integral expressions when<br>
              +          * aggregated into arrays in shader code,
              irrespective of the<br>
              +          * shaderSampledImageArrayDynamic<wbr>Indexing
              feature.<br>
              +          */<br>
              +         assert(allow_indirect);<br>
              +<br>
                        nir_ssa_def *index =<br>
                           nir_iadd(b, nir_imm_int(b,
              deref_array->base_offset),<br>
                                       nir_ssa_for_src(b,
              deref_array->indirect, 1));<br>
              @@ -150,7 +159,6 @@ lower_tex_deref(nir_tex_instr *tex,
              nir_deref_var *deref,<br>
              <br>
                        nir_tex_src *new_srcs = rzalloc_array(tex,
              nir_tex_src,<br>
                                                             
              tex->num_srcs + 1);<br>
              -<br>
            </blockquote>
            <div><br>
            </div>
            <div>Spurrious change?<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
                        for (unsigned i = 0; i < tex->num_srcs;
              i++) {<br>
                           new_srcs[i].src_type =
              tex->src[i].src_type;<br>
                           nir_instr_move_src(&tex-><wbr>instr,
              &new_srcs[i].src, &tex->src[i].src);<br>
              @@ -186,6 +194,46 @@ cleanup_tex_deref(nir_tex_<wbr>instr
              *tex, nir_deref_var *deref)<br>
                  nir_instr_rewrite_src(&tex-><wbr>instr,
              &deref_array->indirect, NIR_SRC_INIT);<br>
               }<br>
              <br>
              +static bool<br>
              +has_tex_src_plane(nir_tex_<wbr>instr *tex)<br>
              +{<br>
              +   for (unsigned i = 0; i < tex->num_srcs; i++) {<br>
              +      if (tex->src[i].src_type == nir_tex_src_plane)<br>
              +         return true;<br>
              +   }<br>
              +<br>
              +   return false;<br>
              +}<br>
              +<br>
              +static uint32_t<br>
              +extract_tex_src_plane(nir_<wbr>tex_instr *tex)<br>
              +{<br>
              +   nir_tex_src *new_srcs = rzalloc_array(tex,
              nir_tex_src, tex->num_srcs - 1);<br>
              +   unsigned plane = 0;<br>
              +<br>
              +   for (unsigned i = 0, w = 0; i < tex->num_srcs;
              i++) {<br>
              +      if (tex->src[i].src_type == nir_tex_src_plane) {<br>
              +         nir_const_value *const_plane =<br>
              +            nir_src_as_const_value(tex-><wbr>src[i].src);<br>
              +<br>
              +         /* Our color conversion lowering pass should
              only ever insert<br>
              +          * constants. */<br>
              +         assert(const_plane);<br>
              +         plane = const_plane->u32[0];<br>
              +      } else {<br>
              +         new_srcs[w].src_type = tex->src[i].src_type;<br>
              +         nir_instr_move_src(&tex-><wbr>instr,
              &new_srcs[w].src, &tex->src[i].src);<br>
              +         w++;<br>
              +      }<br>
              +   }<br>
              +<br>
              +   ralloc_free(tex->src);<br>
              +   tex->src = new_srcs;<br>
              +   tex->num_srcs--;<br>
              +<br>
              +   return plane;<br>
              +}<br>
              +<br>
               static void<br>
               lower_tex(nir_tex_instr *tex, struct
              apply_pipeline_layout_state *state)<br>
               {<br>
              @@ -198,17 +246,21 @@ lower_tex(nir_tex_instr *tex, struct
              apply_pipeline_layout_state *state)<br>
                  unsigned binding = tex->texture->var->data.<wbr>binding;<br>
                  unsigned hw_binding_size =<br>
                     state->layout->set[set].<wbr>layout->binding[binding].hw_<wbr>binding_size;<br>
              -   tex->texture_index = state->set[set].surface_<wbr>offsets[binding];<br>
              +   bool has_plane = has_tex_src_plane(tex);<br>
              +   unsigned plane = has_plane ?
              extract_tex_src_plane(tex) : 0;<br>
              +<br>
              +   tex->texture_index = state->set[set].surface_<wbr>offsets[binding]
              + plane;<br>
                  lower_tex_deref(tex, tex->texture,
              &tex->texture_index, hw_binding_size,<br>
              -                   nir_tex_src_texture_offset, state);<br>
              +                   nir_tex_src_texture_offset,
              !has_plane, state);<br>
              <br>
                  if (tex->sampler) {<br>
                     unsigned set = tex->sampler->var->data.<wbr>descriptor_set;<br>
                     unsigned binding =
              tex->sampler->var->data.<wbr>binding;<br>
                     unsigned hw_binding_size =<br>
                        state->layout->set[set].<wbr>layout->binding[binding].hw_<wbr>binding_size;<br>
              +      tex->sampler_index = state->set[set].sampler_<wbr>offsets[binding]
              + plane;<br>
                     lower_tex_deref(tex, tex->sampler,
              &tex->sampler_index, hw_binding_size,<br>
              -                      nir_tex_src_sampler_offset, state);<br>
              +                      nir_tex_src_sampler_offset,
              !has_plane, state);<br>
                  }<br>
              <br>
                  /* The backend only ever uses this to mark used
              surfaces.  We don't care<br>
              diff --git a/src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c
              b/src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c<br>
              new file mode 100644<br>
              index 00000000000..cae668cba2e<br>
              --- /dev/null<br>
              +++ b/src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c<br>
              @@ -0,0 +1,468 @@<br>
              +/*<br>
              + * Copyright © 2017 Intel Corporation<br>
              + *<br>
              + * Permission is hereby granted, free of charge, to any
              person obtaining a<br>
              + * copy of this software and associated documentation
              files (the "Software"),<br>
              + * to deal in the Software without restriction, including
              without limitation<br>
              + * the rights to use, copy, modify, merge, publish,
              distribute, sublicense,<br>
              + * and/or sell copies of the Software, and to permit
              persons to whom the<br>
              + * Software is furnished to do so, subject to the
              following conditions:<br>
              + *<br>
              + * The above copyright notice and this permission notice
              (including the next<br>
              + * paragraph) shall be included in all copies or
              substantial portions of the<br>
              + * Software.<br>
              + *<br>
              + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
              ANY KIND, EXPRESS OR<br>
              + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
              OF MERCHANTABILITY,<br>
              + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
              IN NO EVENT SHALL<br>
              + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
              CLAIM, DAMAGES OR OTHER<br>
              + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
              OTHERWISE, ARISING<br>
              + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
              USE OR OTHER DEALINGS<br>
              + * IN THE SOFTWARE.<br>
              + */<br>
              +<br>
              +#include "anv_nir.h"<br>
              +#include "anv_private.h"<br>
              +#include "nir.h"<br>
              +#include "nir/nir_builder.h"<br>
              +<br>
              +struct ycbcr_state {<br>
              +   nir_builder *builder;<br>
              +   nir_ssa_def *image_size;<br>
              +   nir_tex_instr *origin_tex;<br>
              +   struct anv_ycbcr_conversion *conversion;<br>
              +};<br>
              +<br>
              +static nir_ssa_def *<br>
              +y_range(nir_builder *b,<br>
              +        nir_ssa_def *y_channel,<br>
              +        int bpc,<br>
              +        VkSamplerYcbcrRangeKHR range)<br>
              +{<br>
              +   switch (range) {<br>
              +   case VK_SAMPLER_YCBCR_RANGE_ITU_<wbr>FULL_KHR:<br>
              +      return y_channel;<br>
              +   case VK_SAMPLER_YCBCR_RANGE_ITU_<wbr>NARROW_KHR:<br>
              +      return nir_fmul(b,<br>
              +                      nir_fadd(b,<br>
              +                               nir_fmul(b, y_channel,<br>
              +                                        nir_imm_float(b,
              pow(2, bpc) - 1)),<br>
              +                               nir_imm_float(b, -16.0f *
              pow(2, bpc - 8))),<br>
              +                      nir_imm_float(b, 1.0f / (219.0f *
              pow(2, bpc - 8))));<br>
              +   default:<br>
              +      unreachable("missing Ycbcr range");<br>
              +      return NULL;<br>
              +   }<br>
              +}<br>
              +<br>
              +static nir_ssa_def *<br>
              +chroma_range(nir_builder *b,<br>
              +             nir_ssa_def *chroma_channel,<br>
              +             int bpc,<br>
              +             VkSamplerYcbcrRangeKHR range)<br>
              +{<br>
              +   switch (range) {<br>
              +   case VK_SAMPLER_YCBCR_RANGE_ITU_<wbr>FULL_KHR:<br>
              +      return nir_fadd(b, chroma_channel,<br>
              +                      nir_imm_float(b, -pow(2, bpc - 1) /
              (pow(2, bpc) - 1.0f)));<br>
              +   case VK_SAMPLER_YCBCR_RANGE_ITU_<wbr>NARROW_KHR:<br>
              +      return nir_fmul(b,<br>
              +                      nir_fadd(b,<br>
              +                               nir_fmul(b,
              chroma_channel,<br>
              +                                        nir_imm_float(b,
              pow(2, bpc) - 1)),<br>
              +                               nir_imm_float(b, -128.0f *
              pow(2, bpc - 8))),<br>
              +                      nir_imm_float(b, 1.0f / (224.0f *
              pow(2, bpc - 8))));<br>
              +   default:<br>
              +      unreachable("missing Ycbcr range");<br>
              +      return NULL;<br>
              +   }<br>
              +}<br>
              +<br>
              +static const nir_const_value *<br>
              +ycbcr_model_to_rgb_matrix(<wbr>VkSamplerYcbcrModelConversionK<wbr>HR
              model)<br>
              +{<br>
              +   switch (model) {<br>
              +   case VK_SAMPLER_YCBCR_MODEL_<wbr>CONVERSION_YCBCR_601_KHR:
              {<br>
              +      static const nir_const_value bt601[3] = {<br>
              +         { .f32 = {  1.402f,             1.0f,  0.0f,   
                         0.0f } },<br>
              +         { .f32 = { -0.714136286201022f, 1.0f,
              -0.344136286201022f, 0.0f } },<br>
              +         { .f32 = {  0.0f,               1.0f,  1.772f, 
                         0.0f } }<br>
              +      };<br>
              +<br>
              +      return bt601;<br>
              +   }<br>
              +   case VK_SAMPLER_YCBCR_MODEL_<wbr>CONVERSION_YCBCR_709_KHR:
              {<br>
              +      static const nir_const_value bt709[3] = {<br>
              +         { .f32 = {  1.5748031496063f,   1.0f,  0.0,     
                        0.0f } },<br>
              +         { .f32 = { -0.468125209181067f, 1.0f,
              -0.187327487470334f, 0.0f } },<br>
              +         { .f32 = {  0.0f,               1.0f, 
              1.85563184264242f,  0.0f } }<br>
              +      };<br>
              +<br>
              +      return bt709;<br>
              +   }<br>
              +   case VK_SAMPLER_YCBCR_MODEL_<wbr>CONVERSION_YCBCR_2020_KHR:
              {<br>
              +      static const nir_const_value bt2020[3] = {<br>
              +         { .f32 = { 1.4746f,             1.0f,  0.0f,   
                         0.0f } },<br>
              +         { .f32 = { -0.571353126843658f, 1.0f,
              -0.164553126843658f, 0.0f } },<br>
              +         { .f32 = { 0.0f,                1.0f,  1.8814f, 
                        0.0f } }<br>
              +      };<br>
              +<br>
              +      return bt2020;<br>
              +   }<br>
              +   default:<br>
              +      unreachable("missing Ycbcr model");<br>
              +      return NULL;<br>
              +   }<br>
              +}<br>
              +<br>
              +static nir_ssa_def *<br>
              +convert_ycbcr(struct ycbcr_state *state,<br>
              +              nir_ssa_def *raw_channels,<br>
              +              uint32_t *bpcs)<br>
              +{<br>
              +   nir_builder *b = state->builder;<br>
              +   struct anv_ycbcr_conversion *conversion =
              state->conversion;<br>
              +<br>
              +   nir_ssa_def *expanded_channels =<br>
              +      nir_vec4(b,<br>
              +               chroma_range(b, nir_channel(b,
              raw_channels, 0),<br>
              +                            bpcs[0],
              conversion->ycbcr_range),<br>
              +               y_range(b, nir_channel(b, raw_channels,
              1),<br>
              +                       bpcs[1],
              conversion->ycbcr_range),<br>
              +               chroma_range(b, nir_channel(b,
              raw_channels, 2),<br>
              +                            bpcs[2],
              conversion->ycbcr_range),<br>
              +               nir_imm_float(b, 1.0f));<br>
              +<br>
              +   if (conversion->ycbcr_model ==
              VK_SAMPLER_YCBCR_MODEL_<wbr>CONVERSION_YCBCR_IDENTITY_KHR)<br>
              +      return expanded_channels;<br>
              +<br>
              +   const nir_const_value *conversion_matrix =<br>
              +      ycbcr_model_to_rgb_matrix(<wbr>conversion->ycbcr_model);<br>
              +<br>
              +   nir_ssa_def *converted_channels[] = {<br>
              +      nir_fdot4(b, expanded_channels, nir_build_imm(b, 4,
              32, conversion_matrix[0])),<br>
              +      nir_fdot4(b, expanded_channels, nir_build_imm(b, 4,
              32, conversion_matrix[1])),<br>
              +      nir_fdot4(b, expanded_channels, nir_build_imm(b, 4,
              32, conversion_matrix[2]))<br>
              +   };<br>
              +<br>
              +   return nir_vec4(b,<br>
              +                   converted_channels[0],
              converted_channels[1],<br>
              +                   converted_channels[2],
              nir_imm_float(b, 1.0f));<br>
              +}<br>
              +<br>
              +static unsigned<br>
              +sampler_dim_components(enum glsl_sampler_dim sampler_dim)<br>
              +{<br>
              +   switch (sampler_dim) {<br>
              +   case GLSL_SAMPLER_DIM_1D:<br>
              +   case GLSL_SAMPLER_DIM_BUF:<br>
              +      return 1;<br>
              +   case GLSL_SAMPLER_DIM_RECT:<br>
              +   case GLSL_SAMPLER_DIM_2D:<br>
              +      return 2;<br>
              +   case GLSL_SAMPLER_DIM_3D:<br>
              +      return 3;<br>
              +   case GLSL_SAMPLER_DIM_CUBE:<br>
              +      return 3;<br>
              +   default:<br>
              +      unreachable("unsupported sampler dimension");<br>
              +      return 0;<br>
              +   }<br>
              +}<br>
            </blockquote>
            <div><br>
            </div>
            <div>You don't need this.  Just use
              nir_tex_instr_dest_size().<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +<br>
              +/* TODO: we should probably replace this with a push
              constant/uniform. */<br>
              +static nir_ssa_def *<br>
              +maybe_load_image_size(struct ycbcr_state *state,
              nir_deref_var *texture)<br>
            </blockquote>
            <div><br>
            </div>
            <div>I don't really like the maybe_  Why not just call it
              "get_texture_size" or something like that.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +{<br>
              +   if (state->image_size)<br>
              +      return state->image_size;<br>
            </blockquote>
            <div><br>
            </div>
            <div>Don't worry too much about not re-emitting image_size. 
              NIR will CSE any duplicate txs operations you may have.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +<br>
              +   nir_builder *b = state->builder;<br>
              +   const struct glsl_type *type =
              nir_deref_tail(&texture-><wbr>deref)->type;<br>
              +   const unsigned num_components =<br>
              +      sampler_dim_components(glsl_<wbr>get_sampler_dim(type));<br>
              +   nir_tex_instr *tex = nir_tex_instr_create(b-><wbr>shader,
              0);<br>
              +<br>
              +   tex->op = nir_texop_txs;<br>
              +   tex->sampler_dim = glsl_get_sampler_dim(type);<br>
              +   tex->is_array = glsl_sampler_type_is_array(<wbr>type);<br>
              +   tex->is_shadow = glsl_sampler_type_is_shadow(<wbr>type);<br>
              +   tex->texture = nir_deref_var_clone(texture, tex);<br>
              +   tex->dest_type = nir_type_int;<br>
              +<br>
              +   nir_ssa_dest_init(&tex->instr,
              &tex->dest,<br>
              +                     num_components, 32, NULL);<br>
              +   nir_builder_instr_insert(b, &tex->instr);<br>
              +<br>
              +   state->image_size = nir_i2f32(b,
              &tex->dest.ssa);<br>
              +<br>
              +   return state->image_size;<br>
              +}<br>
              +<br>
              +static nir_ssa_def *<br>
              +implicit_downsampled_coord(<wbr>nir_builder *b,<br>
              +                           nir_ssa_def *value,<br>
              +                           nir_ssa_def *max_value,<br>
              +                           int div_scale)<br>
              +{<br>
              +   return nir_fadd(b,<br>
              +                   value,<br>
              +                   nir_fdiv(b,<br>
              +                            nir_imm_float(b, 1.0f),<br>
            </blockquote>
            <div><br>
            </div>
            <div>nir_frcp<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +                            nir_fmul(b,<br>
              +                                     nir_imm_float(b,
              div_scale),<br>
              +                                     max_value)));<br>
              +}<br>
              +<br>
              +static nir_ssa_def *<br>
              +implicit_downsampled_coords(<wbr>struct ycbcr_state
              *state,<br>
              +                            nir_ssa_def *old_coords,<br>
              +                            const struct anv_format_plane
              *plane_format)<br>
              +{<br>
              +   nir_builder *b = state->builder;<br>
              +   struct anv_ycbcr_conversion *conversion =
              state->conversion;<br>
              +   nir_ssa_def *image_size = maybe_load_image_size(state,<br>
              +                                                 
               state->origin_tex->texture);<br>
              +   nir_ssa_def *comp[4] = { NULL, };<br>
              +   int c;<br>
              +<br>
              +   for (c = 0; c < ARRAY_SIZE(conversion->chroma_<wbr>offsets);
              c++) {<br>
              +      if (plane_format->denominator_<wbr>scales[c]
              > 1 &&<br>
              +          conversion->chroma_offsets[c] ==
              VK_CHROMA_LOCATION_COSITED_<wbr>EVEN_KHR) {<br>
              +         comp[c] = implicit_downsampled_coord(b,<br>
              +                                             
              nir_channel(b, old_coords, c),<br>
              +                                             
              nir_channel(b, image_size, c),<br>
              +                                             
              plane_format->denominator_<wbr>scales[c]);<br>
              +      } else {<br>
              +         comp[c] = nir_channel(b, old_coords, c);<br>
              +      }<br>
              +   }<br>
              +<br>
              +   /* Leave other coordinates untouched */<br>
              +   for (; c < old_coords->num_components; c++)<br>
            </blockquote>
            <div><br>
            </div>
            <div>Is non-2D YCBCR allowed?  I guess it may make sense to
              allow arrays.</div>
          </div>
        </div>
      </div>
    </blockquote>
    <br>
    Nop :<br>
    <br>
    <ul style="box-sizing: border-box; margin: 0px 0px 0px 1.25em;
      padding: 0px; direction: ltr; font-size: 16px; line-height: 1.6;
      list-style-position: outside; font-family: "Noto Serif",
      "DejaVu Serif", serif; color: rgba(0, 0, 0, 0.8);
      font-style: normal; font-variant-ligatures: normal;
      font-variant-caps: normal; font-weight: normal; letter-spacing:
      normal; orphans: 2; text-align: left; text-indent: 0px;
      text-transform: none; white-space: normal; widows: 2;
      word-spacing: 0px; -webkit-text-stroke-width: 0px;
      background-color: rgb(248, 248, 247); text-decoration-style:
      initial; text-decoration-color: initial;">
      <li style="box-sizing: border-box; margin: 0px; padding: 0px;
        direction: ltr;">
        <p style="box-sizing: border-box; margin: 0px 0px 0.625em;
          padding: 0px; direction: ltr; font-family: inherit;
          font-weight: 400; font-size: 1em; line-height: 1.6;
          text-rendering: optimizeLegibility; letter-spacing: -0.01em;"><code
            style="box-sizing: border-box; font-family: "Droid Sans
            Mono", "DejaVu Sans Mono", monospace;
            font-size: 0.9375em; font-weight: 400; color: rgba(0, 0, 0,
            0.9); letter-spacing: 0px; padding: 0.1em 0.5ex;
            word-spacing: -0.15em; background-color: rgb(247, 247, 248);
            border-radius: 4px; line-height: 1.45; text-rendering:
            optimizeSpeed; word-wrap: break-word; font-style: normal
            !important;">imageType</code><span> </span><strong
            class="purple" style="box-sizing: border-box; font-weight:
            bold; line-height: inherit; letter-spacing: -0.005em; color:
            rgb(96, 0, 96);">must</strong><span> </span>be<span> </span><code
            style="box-sizing: border-box; font-family: "Droid Sans
            Mono", "DejaVu Sans Mono", monospace;
            font-size: 0.9375em; font-weight: 400; color: rgba(0, 0, 0,
            0.9); letter-spacing: 0px; padding: 0.1em 0.5ex;
            word-spacing: -0.15em; background-color: rgb(247, 247, 248);
            border-radius: 4px; line-height: 1.45; text-rendering:
            optimizeSpeed; word-wrap: break-word; font-style: normal
            !important;">VK_IMAGE_TYPE_2D</code></p>
      </li>
    </ul>
    <br>
    <blockquote type="cite"
cite="mid:CAOFGe95M0Pgkgww_RSGMZBySknSDy=xgh55sJJoFV3qBiXnAoA@mail.gmail.com">
      <div dir="ltr">
        <div class="gmail_extra">
          <div class="gmail_quote">
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +      comp[c] = nir_channel(b, old_coords, c);<br>
              +<br>
              +   return nir_vec(b, comp,
              old_coords->num_components);<br>
              +}<br>
              +<br>
              +static nir_ssa_def *<br>
              +create_plane_tex_instr_<wbr>implicit(struct ycbcr_state
              *state,<br>
              +                                uint32_t plane)<br>
              +{<br>
              +   nir_builder *b = state->builder;<br>
              +   struct anv_ycbcr_conversion *conversion =
              state->conversion;<br>
              +   const struct anv_format_plane *plane_format =<br>
              +      &conversion->format->planes[<wbr>plane];<br>
              +   nir_tex_instr *old_tex = state->origin_tex;<br>
              +   nir_tex_instr *tex = nir_tex_instr_create(b-><wbr>shader,
              old_tex->num_srcs + 1);<br>
              +<br>
              +   for (uint32_t i = 0; i < old_tex->num_srcs; i++)
              {<br>
              +      tex->src[i].src_type =
              old_tex->src[i].src_type;<br>
              +<br>
              +      switch (old_tex->src[i].src_type) {<br>
              +      case nir_tex_src_coord:<br>
              +         if (plane_format->has_chroma &&
              conversion->chroma_<wbr>reconstruction) {<br>
            </blockquote>
            <div><br>
            </div>
            <div>assert(old_tex->src[i].src.is_ssa);<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +            tex->src[i].src =<br>
              +               nir_src_for_ssa(implicit_<wbr>downsampled_coords(state,<br>
              +                                                         
               old_tex->src[i].src.ssa,<br>
              +                                                         
               plane_format));<br>
              +            break;<br>
              +         }<br>
              +         /* fall through */<br>
              +      default:<br>
              +         nir_src_copy(&tex->src[i].src,
              &old_tex->src[i].src, tex);<br>
              +         break;<br>
              +      }<br>
              +   }<br>
              +   tex->src[tex->num_srcs - 1].src =
              nir_src_for_ssa(nir_imm_int(b, plane));<br>
              +   tex->src[tex->num_srcs - 1].src_type =
              nir_tex_src_plane;<br>
              +<br>
              +   tex->sampler_dim = old_tex->sampler_dim;<br>
              +   tex->dest_type = old_tex->dest_type;<br>
              +<br>
              +   tex->op = old_tex->op;<br>
              +   tex->coord_components =
              old_tex->coord_components;<br>
              +   tex->is_new_style_shadow =
              old_tex->is_new_style_shadow;<br>
              +   tex->component = old_tex->component;<br>
              +<br>
              +   tex->texture_index = old_tex->texture_index;<br>
              +   tex->texture_array_size =
              old_tex->texture_array_size;<br>
              +   tex->texture = nir_deref_var_clone(old_tex-><wbr>texture,
              tex);<br>
              +<br>
              +   tex->sampler_index = old_tex->sampler_index;<br>
              +   tex->sampler = nir_deref_var_clone(old_tex-><wbr>sampler,
              tex);<br>
              +<br>
              +   nir_ssa_dest_init(&tex->instr,
              &tex->dest,<br>
              +                     old_tex->dest.ssa.num_<wbr>components,<br>
              +                     nir_dest_bit_size(old_tex-><wbr>dest),
              NULL);<br>
              +   nir_builder_instr_insert(b, &tex->instr);<br>
              +<br>
              +   return &tex->dest.ssa;<br>
              +}<br>
              +<br>
              +static unsigned<br>
              +channel_to_component(enum isl_channel_select channel)<br>
              +{<br>
              +   switch (channel) {<br>
              +   case ISL_CHANNEL_SELECT_RED:<br>
              +      return 0;<br>
              +   case ISL_CHANNEL_SELECT_GREEN:<br>
              +      return 1;<br>
              +   case ISL_CHANNEL_SELECT_BLUE:<br>
              +      return 2;<br>
              +   case ISL_CHANNEL_SELECT_ALPHA:<br>
              +      return 3;<br>
              +   default:<br>
              +      unreachable("invalid channel");<br>
              +      return 0;<br>
              +   }<br>
              +}<br>
              +<br>
              +static enum isl_channel_select<br>
              +swizzle_channel(struct isl_swizzle swizzle, unsigned
              channel)<br>
              +{<br>
              +   switch (channel) {<br>
              +   case 0:<br>
              +      return swizzle.r;<br>
              +   case 1:<br>
              +      return swizzle.g;<br>
              +   case 2:<br>
              +      return swizzle.b;<br>
              +   case 3:<br>
              +      return swizzle.a;<br>
              +   default:<br>
              +      unreachable("invalid channel");<br>
              +      return 0;<br>
              +   }<br>
              +}<br>
              +<br>
              +static void<br>
              +maybe_replace_texture_<wbr>multiplanar(struct
              anv_pipeline *pipeline,<br>
            </blockquote>
            <div><br>
            </div>
            <div>How about try_lower_tex_ycbcr?<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +                                  nir_builder *builder,<br>
              +                                  nir_tex_instr *tex)<br>
              +{<br>
              +   nir_variable *var = tex->texture->var;<br>
              +   const struct anv_descriptor_set_layout *set_layout =<br>
              +      pipeline->layout->set[var-><wbr>data.descriptor_set].layout;<br>
              +   const struct anv_descriptor_set_binding_<wbr>layout
              *binding =<br>
              +      &set_layout->binding[var-><wbr>data.binding];<br>
              +<br>
              +   if (tex->op == nir_texop_txs ||<br>
              +       tex->op == nir_texop_query_levels ||<br>
              +       tex->op == nir_texop_lod)<br>
              +      return;<br>
              +<br>
              +   if (binding->immutable_samplers == NULL)<br>
              +      return;<br>
              +<br>
              +   const struct anv_sampler *sampler =<br>
              +      binding->immutable_samplers[<wbr>tex->texture_index];<br>
              +<br>
              +   if (sampler->conversion == NULL)<br>
              +      return;<br>
              +<br>
              +   struct ycbcr_state state = {<br>
              +      .builder = builder,<br>
              +      .origin_tex = tex,<br>
              +      .conversion = sampler->conversion,<br>
              +   };<br>
              +<br>
              +   builder->cursor =
              nir_before_instr(&tex->instr);<br>
              +<br>
              +   const struct anv_format *format =
              state.conversion->format;<br>
              +   const struct isl_format_layout *y_isl_layout = NULL;<br>
              +   for (uint32_t p = 0; p < format->n_planes; p++)
              {<br>
              +      if (!format->planes[p].has_<wbr>chroma)<br>
              +         y_isl_layout = isl_format_get_layout(format-><wbr>planes[p].isl_format);<br>
              +   }<br>
              +   assert(y_isl_layout != NULL);<br>
              +   uint8_t y_bpc = y_isl_layout->channels_array[<wbr>0].bits;<br>
              +<br>
              +   /* |ycbcr_comp| holds components in the order :
              Cr-Y-Cb */<br>
              +   nir_ssa_def *ycbcr_comp[5] = { NULL, NULL, NULL,<br>
              +                                  /* Use extra 2 channels
              for following swizzle */<br>
              +                                  nir_imm_float(builder,
              1.0f),<br>
              +                                  nir_imm_float(builder,
              0.0f),<br>
              +   };<br>
              +   uint8_t ycbcr_bpcs[5];<br>
              +   memset(ycbcr_bpcs, y_bpc, sizeof(ycbcr_bpcs));<br>
              +<br>
              +   /* Go through all the planes and gather the samples
              into a |ycbcr_comp|<br>
              +    * while applying a swizzle required by the spec:<br>
              +    *<br>
              +    *    R, G, B should respectively map to Cr, Y, Cb<br>
              +    */<br>
              +   for (uint32_t p = 0; p < format->n_planes; p++)
              {<br>
              +      const struct anv_format_plane *plane_format =
              &format->planes[p];<br>
              +      nir_ssa_def *plane_sample = create_plane_tex_instr_<wbr>implicit(&state,
              p);<br>
              +<br>
              +      for (uint32_t pc = 0; pc < 4; pc++) {<br>
              +         enum isl_channel_select ycbcr_swizzle =<br>
              +            swizzle_channel(plane_format-><wbr>ycbcr_swizzle,
              pc);<br>
              +         if (ycbcr_swizzle == ISL_CHANNEL_SELECT_ZERO)<br>
              +            continue;<br>
              +<br>
              +         unsigned ycbcr_component =
              channel_to_component(ycbcr_<wbr>swizzle);<br>
              +         ycbcr_comp[ycbcr_component] =
              nir_channel(builder, plane_sample, pc);<br>
              +<br>
              +         /* Also compute the number of bits for each
              component. */<br>
              +         const struct isl_format_layout *isl_layout =<br>
              +            isl_format_get_layout(plane_<wbr>format->isl_format);<br>
              +         ycbcr_bpcs[ycbcr_component] =
              isl_layout->channels_array[pc]<wbr>.bits;<br>
              +      }<br>
              +   }<br>
              +<br>
              +   /* Now remaps components to the order specified by the
              conversion. */<br>
              +   nir_ssa_def *swizzled_comp[4] = { NULL, };<br>
              +   uint32_t swizzled_bpcs[4] = { 0, };<br>
              +<br>
              +   for (uint32_t i = 0; i <
              ARRAY_SIZE(state.conversion-><wbr>mapping); i++) {<br>
              +      /* Maps to components in |ycbcr_comp| */<br>
              +      static const uint32_t swizzle_mapping[] = {<br>
              +         [VK_COMPONENT_SWIZZLE_ZERO] = 4,<br>
              +         [VK_COMPONENT_SWIZZLE_ONE]  = 3,<br>
              +         [VK_COMPONENT_SWIZZLE_R]    = 0,<br>
              +         [VK_COMPONENT_SWIZZLE_G]    = 1,<br>
              +         [VK_COMPONENT_SWIZZLE_B]    = 2,<br>
              +         [VK_COMPONENT_SWIZZLE_A]    = 3,<br>
              +      };<br>
              +      const VkComponentSwizzle m =
              state.conversion->mapping[i];<br>
              +<br>
              +      if (m == VK_COMPONENT_SWIZZLE_IDENTITY) {<br>
              +         swizzled_comp[i] = ycbcr_comp[i];<br>
              +         swizzled_bpcs[i] = ycbcr_bpcs[i];<br>
              +      } else {<br>
              +         swizzled_comp[i] =
              ycbcr_comp[swizzle_mapping[m]]<wbr>;<br>
              +         swizzled_bpcs[i] =
              ycbcr_bpcs[swizzle_mapping[m]]<wbr>;<br>
              +      }<br>
              +   }<br>
              +<br>
              +   nir_ssa_def *result = nir_vec(builder, swizzled_comp,
              4);<br>
              +   if (state.conversion->ycbcr_model !=
              VK_SAMPLER_YCBCR_MODEL_<wbr>CONVERSION_RGB_IDENTITY_KHR)<br>
              +      result = convert_ycbcr(&state, result,
              swizzled_bpcs);<br>
              +<br>
              +   nir_ssa_def_rewrite_uses(&tex-<wbr>>dest.ssa,
              nir_src_for_ssa(result));<br>
              +   nir_instr_remove(&tex->instr);<br>
              +}<br>
              +<br>
              +void<br>
              +anv_nir_lower_ycbcr_textures(<wbr>nir_shader *shader,
              struct anv_pipeline *pipeline)<br>
            </blockquote>
            <div><br>
            </div>
            <div>Most NIR lowering passes return a boolean that
              indicates whether or not they actually changed anything. 
              We should probably do the same here.  It shouldn't be a
              big change.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +{<br>
              +   nir_foreach_function(function, shader) {<br>
              +      if (!function->impl)<br>
              +         continue;<br>
              +<br>
              +      nir_builder builder;<br>
              +      nir_builder_init(&builder, function->impl);<br>
              +<br>
              +      nir_foreach_block(block, function->impl) {<br>
              +         nir_foreach_instr_safe(instr, block) {<br>
              +            if (instr->type != nir_instr_type_tex)<br>
              +               continue;<br>
              +<br>
              +            nir_tex_instr *tex = nir_instr_as_tex(instr);<br>
              +            maybe_replace_texture_<wbr>multiplanar(pipeline,
              &builder, tex);<br>
              +         }<br>
              +      }<br>
            </blockquote>
            <div><br>
            </div>
            <div>We need to call nir_metadata_preserve here.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +   }<br>
              +}<br>
              diff --git a/src/intel/vulkan/anv_<wbr>pipeline.c
              b/src/intel/vulkan/anv_<wbr>pipeline.c<br>
              index 94e99d8437a..b35bad10503 100644<br>
              --- a/src/intel/vulkan/anv_<wbr>pipeline.c<br>
              +++ b/src/intel/vulkan/anv_<wbr>pipeline.c<br>
              @@ -376,6 +376,8 @@ anv_pipeline_compile(struct
              anv_pipeline *pipeline,<br>
                  if (nir == NULL)<br>
                     return NULL;<br>
              <br>
              +   NIR_PASS_V(nir, anv_nir_lower_ycbcr_textures,
              pipeline);<br>
              +<br>
                  NIR_PASS_V(nir, anv_nir_lower_push_constants);<br>
              <br>
                  if (stage != MESA_SHADER_COMPUTE)<br>
              diff --git a/src/intel/vulkan/anv_<wbr>private.h
              b/src/intel/vulkan/anv_<wbr>private.h<br>
              index 63b803df6ea..5e14f49b196 100644<br>
              --- a/src/intel/vulkan/anv_<wbr>private.h<br>
              +++ b/src/intel/vulkan/anv_<wbr>private.h<br>
              @@ -2567,9 +2567,21 @@ void anv_fill_buffer_surface_state(<wbr>struct
              anv_device *device,<br>
                                                  uint32_t offset,
              uint32_t range,<br>
                                                  uint32_t stride);<br>
              <br>
              +<br>
              +struct anv_ycbcr_conversion {<br>
              +   const struct anv_format *        format;<br>
              +   VkSamplerYcbcrModelConversionK<wbr>HR ycbcr_model;<br>
              +   VkSamplerYcbcrRangeKHR           ycbcr_range;<br>
              +   VkComponentSwizzle               mapping[4];<br>
              +   VkChromaLocationKHR              chroma_offsets[2];<br>
              +   VkFilter                         chroma_filter;<br>
              +   bool                           
               chroma_reconstruction;<br>
              +};<br>
              +<br>
               struct anv_sampler {<br>
              -   uint32_t state[3][4];<br>
              -   uint32_t n_planes;<br>
              +   uint32_t                     state[3][4];<br>
              +   uint32_t                     n_planes;<br>
              +   struct anv_ycbcr_conversion *conversion;<br>
               };<br>
              <br>
               struct anv_framebuffer {<br>
              <span class="HOEnZb"><font color="#888888">--<br>
                  2.14.2<br>
                  <br>
                  ______________________________<wbr>_________________<br>
                  mesa-dev mailing list<br>
                  <a href="mailto:mesa-dev@lists.freedesktop.org"
                    moz-do-not-send="true">mesa-dev@lists.freedesktop.org</a><br>
                  <a
                    href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev"
                    rel="noreferrer" target="_blank"
                    moz-do-not-send="true">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
                </font></span></blockquote>
          </div>
          <br>
        </div>
      </div>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>