<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">All of these changed were made :)<br>
      <br>
      On 04/10/17 22:32, Jason Ekstrand wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAOFGe95SUuVDcO4-ki7yJ8G7F6Yf5QEaMh6ujw_ho_BuiKvi_Q@mail.gmail.com">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <div dir="ltr"><br>
        <div class="gmail_extra"><br>
          <div class="gmail_quote">On Wed, Oct 4, 2017 at 10:34 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"><span
                class="">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>
              </span>v2: Add new file to meson build (Lionel)<br>
                  Use nir_frcp() rather than (1.0f / x) (Jason)<br>
                  Reuse nir_tex_instr_dest_size() rather than
              handwritten one (Jason)<br>
                  Return progress (Jason)<br>
                  Account for array of samplers (Jason)<br>
              <span class=""><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>
              </span> src/intel/vulkan/anv_nir_<wbr>apply_pipeline_layout.c
              |  61 ++-<br>
               src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c  |
              469 +++++++++++++++++++++++<br>
              <span class=""> src/intel/vulkan/anv_pipeline.<wbr>c     
                            |   2 +<br>
                 src/intel/vulkan/anv_private.h                   |  16
                +-<br>
              </span> src/intel/vulkan/meson.build                   
               |   1 +<br>
               7 files changed, 547 insertions(+), 6 deletions(-)<br>
              <span class=""> 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>
              </span>index 5b450b45cdf..8ac0a119dac 100644<br>
              <span class="">--- 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>
              </span>+bool anv_nir_lower_ycbcr_textures(<wbr>nir_shader
              *shader,<br>
              <span class="">+                                  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>
              </span>index 428cfdf42d1..28cbb98c563 100644<br>
              <div>
                <div class="h5">--- 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>
                </div>
              </div>
              @@ -186,6 +195,46 @@ cleanup_tex_deref(nir_tex_<wbr>instr
              *tex, nir_deref_var *deref)<br>
              <div>
                <div class="h5">    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>
                </div>
              </div>
              @@ -198,17 +247,21 @@ lower_tex(nir_tex_instr *tex, struct
              apply_pipeline_layout_state *state)<br>
              <span class="">    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>
              </span>index 00000000000..d2253db9053<br>
              --- /dev/null<br>
              +++ b/src/intel/vulkan/anv_nir_<wbr>lower_ycbcr_textures.c<br>
              @@ -0,0 +1,469 @@<br>
              <div>
                <div class="h5">+/*<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>
                </div>
              </div>
              +                      nir_frcp(b, nir_imm_float(b, 219.0f
              * pow(2, bpc - 8))));<br>
              <span class="">+   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>
              </span>+                      nir_frcp(b, nir_imm_float(b,
              224.0f * pow(2, bpc - 8))));<br>
              <div>
                <div class="h5">+   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>
                </div>
              </div>
              <span class="">+/* TODO: we should probably replace this
                with a push constant/uniform. */<br>
                +static nir_ssa_def *<br>
              </span>+get_texture_size(struct ycbcr_state *state,
              nir_deref_var *texture)<br>
              <span class="">+{<br>
                +   if (state->image_size)<br>
                +      return state->image_size;<br>
              </span><span class="">+<br>
                +   nir_builder *b = state->builder;<br>
                +   const struct glsl_type *type =
                nir_deref_tail(&texture-><wbr>deref)->type;<br>
              </span>+   nir_tex_instr *tex =
              nir_tex_instr_create(b-><wbr>shader, 0);<br>
              <span class="">+<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>
              </span>+                     nir_tex_instr_dest_size(tex),
              32, NULL);<br>
              <span class="">+   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>
              </span><span class="">+                           
                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>
              </span>+   nir_ssa_def *image_size =
              get_texture_size(state,<br>
              <span class="">+                                         
                    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>
              </span><span class="">+      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>
              </span>+            assert(old_tex->src[i].src.is_<wbr>ssa);<br>
              <div>
                <div class="h5">+            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>
                </div>
              </div>
              +try_lower_tex_ycbcr(struct anv_pipeline *pipeline,<br>
              <span class="">+                    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>
              </span>+   unsigned texture_index = tex->texture_index;<br>
              +   if (tex->texture->deref.child) {<br>
              +      assert(tex->texture->deref.<wbr>child->deref_type
              == nir_deref_type_array);<br>
            </blockquote>
            <div><br>
            </div>
            <div>This assert should be safe.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +      nir_deref_array *deref_array =
              nir_deref_as_array(tex-><wbr>texture->deref.child);<br>
              +      assert(deref_array->deref_<wbr>array_type ==
              nir_deref_array_type_direct);<br>
            </blockquote>
            <div><br>
            </div>
            <div>This one isn't.  At this point, we haven't done enough
              checks to know for 100% certain whether or not we have a
              ycbcr conversion.  We need to either do the
              "sampler->conversion" check above this or just bail if
              we see an indirect deref.  I think I like the later better
              than the former.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +      texture_index += MIN2(deref_array->base_offset,
              binding->hw_binding_size - 1);<br>
              <span class="">+   }<br>
                +   const struct anv_sampler *sampler =<br>
              </span>+      binding->immutable_samplers[<wbr>texture_index];<br>
              <span class="">+<br>
                +   if (sampler->conversion == NULL)<br>
                +      return;<br>
                +<br>
              </span><span class="">+   struct ycbcr_state state = {<br>
                +      .builder = builder,<br>
                +      .origin_tex = tex,<br>
                +      .conversion = sampler->conversion,<br>
              </span>
              <div>
                <div class="h5">+   };<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>
                </div>
              </div>
              +bool<br>
              <span class="">+anv_nir_lower_ycbcr_textures(<wbr>nir_shader
                *shader, struct anv_pipeline *pipeline)<br>
              </span>+{<br>
              +   bool progress = false;<br>
              <span class="">+<br>
                +   nir_foreach_function(function, shader) {<br>
                +      if (!function->impl)<br>
                +         continue;<br>
                +<br>
              </span>+      bool function_progress = false;<br>
              <span class="">+      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>
              </span>+            try_lower_tex_ycbcr(pipeline,
              &builder, tex);<br>
            </blockquote>
            <div><br>
            </div>
            <div>We probably want try_lower_tex_ycbcr to return a
              boolean too.  It's got a lot of early returns.  As is,
              this will report progress if it ever sees a texture
              instruction.<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0 0 0
              .8ex;border-left:1px #ccc solid;padding-left:1ex">
              +<br>
              +            function_progress = true;<br>
              +         }<br>
              +      }<br>
              +<br>
              +      if (function_progress) {<br>
              +         nir_metadata_preserve(<wbr>function->impl,<br>
              +                               nir_metadata_block_index |<br>
              +                               nir_metadata_dominance);<br>
              +      }<br>
              +<br>
              +      progress |= function_progress;<br>
              +   }<br>
              +<br>
              +   return progress;<br>
              <span class="">+}<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>
              </span>index 2b3b5a1810a..1292084fed9 100644<br>
              --- a/src/intel/vulkan/anv_<wbr>private.h<br>
              +++ b/src/intel/vulkan/anv_<wbr>private.h<br>
              @@ -2560,9 +2560,21 @@ void anv_fill_buffer_surface_state(<wbr>struct
              anv_device *device,<br>
              <span class="">                                   
                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>diff --git a/src/intel/vulkan/meson.build
              b/src/intel/vulkan/meson.build<br>
              index 6ce092e05ce..ff24e304ef5 100644<br>
              --- a/src/intel/vulkan/meson.build<br>
              +++ b/src/intel/vulkan/meson.build<br>
              @@ -101,6 +101,7 @@ libanv_files = files(<br>
                 'anv_nir_lower_input_<wbr>attachments.c',<br>
                 'anv_nir_lower_multiview.c',<br>
                 'anv_nir_lower_push_constants.<wbr>c',<br>
              +  'anv_nir_lower_ycbcr_textures.<wbr>c',<br>
                 'anv_pass.c',<br>
                 'anv_pipeline.c',<br>
                 'anv_pipeline_cache.c',<br>
              <div class="HOEnZb">
                <div class="h5">--<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>
                </div>
              </div>
            </blockquote>
          </div>
          <br>
        </div>
      </div>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>