<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Oct 3, 2017 at 4:54 PM, Jason Ekstrand <span dir="ltr"><<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">On Tue, Oct 3, 2017 at 9:29 AM, Lionel Landwerlin <span dir="ltr"><<a href="mailto:lionel.g.landwerlin@intel.com" target="_blank">lionel.g.landwerlin@intel.com</a><wbr>></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_conversio<wbr>n 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" target="_blank">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_appl<wbr>y_pipeline_layout.c |  62 ++-<br>
 src/intel/vulkan/anv_nir_lowe<wbr>r_ycbcr_textures.c  | 468 +++++++++++++++++++++++<br>
 src/intel/vulkan/anv_<wbr>pipeline.c                  |   2 +<br>
 src/intel/vulkan/anv_private.<wbr>h                   |  16 +-<br>
 6 files changed, 545 insertions(+), 7 deletions(-)<br>
 create mode 100644 src/intel/vulkan/anv_nir_lower<wbr>_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_att<wbr>achments.c \<br>
        vulkan/anv_nir_lower_multiview<wbr>.c \<br>
        vulkan/anv_nir_lower_push_cons<wbr>tants.c \<br>
+       vulkan/anv_nir_lower_ycbcr_te<wbr>xtures.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(n<wbr>ir_shader *shader);<br>
<br>
 bool anv_nir_lower_multiview(nir_sh<wbr>ader *shader, uint32_t view_mask);<br>
<br>
+void anv_nir_lower_ycbcr_textures(n<wbr>ir_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_app<wbr>ly_pipeline_layout.c b/src/intel/vulkan/anv_nir_app<wbr>ly_pipeline_layout.c<br>
index 428cfdf42d1..7cd28debe09 100644<br>
--- a/src/intel/vulkan/anv_nir_app<wbr>ly_pipeline_layout.c<br>
+++ b/src/intel/vulkan/anv_nir_app<wbr>ly_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->dere<wbr>f.child);<br>
<br>
       if (deref_array->deref_array_type == nir_deref_array_type_indirect) {<br>
+         /* From VK_KHR_sampler_ycbcr_conversio<wbr>n:<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></div><div>Spurrious change?<br></div><div><div class="h5"><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->inst<wbr>r, &new_srcs[i].src, &tex->src[i].src);<br>
@@ -186,6 +194,46 @@ cleanup_tex_deref(nir_tex_inst<wbr>r *tex, nir_deref_var *deref)<br>
    nir_instr_rewrite_src(&tex->in<wbr>str, &deref_array->indirect, NIR_SRC_INIT);<br>
 }<br>
<br>
+static bool<br>
+has_tex_src_plane(nir_tex_ins<wbr>tr *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_tex<wbr>_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->sr<wbr>c[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->inst<wbr>r, &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.bindin<wbr>g;<br>
    unsigned hw_binding_size =<br>
       state->layout->set[set].layou<wbr>t->binding[binding].hw_binding<wbr>_size;<br>
-   tex->texture_index = state->set[set].surface_offset<wbr>s[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_offset<wbr>s[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.descri<wbr>ptor_set;<br>
       unsigned binding = tex->sampler->var->data.bindin<wbr>g;<br>
       unsigned hw_binding_size =<br>
          state->layout->set[set].layout<wbr>->binding[binding].hw_binding_<wbr>size;<br>
+      tex->sampler_index = state->set[set].sampler_offset<wbr>s[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_low<wbr>er_ycbcr_textures.c b/src/intel/vulkan/anv_nir_low<wbr>er_ycbcr_textures.c<br>
new file mode 100644<br>
index 00000000000..cae668cba2e<br>
--- /dev/null<br>
+++ b/src/intel/vulkan/anv_nir_low<wbr>er_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_FUL<wbr>L_KHR:<br>
+      return y_channel;<br>
+   case VK_SAMPLER_YCBCR_RANGE_ITU_NAR<wbr>ROW_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_FUL<wbr>L_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_NAR<wbr>ROW_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(VkS<wbr>amplerYcbcrModelConversionKHR model)<br>
+{<br>
+   switch (model) {<br>
+   case VK_SAMPLER_YCBCR_MODEL_CONVERS<wbr>ION_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_CONVERS<wbr>ION_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_CONVERS<wbr>ION_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_CONVERS<wbr>ION_YCBCR_IDENTITY_KHR)<br>
+      return expanded_channels;<br>
+<br>
+   const nir_const_value *conversion_matrix =<br>
+      ycbcr_model_to_rgb_matrix(conv<wbr>ersion->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></div><div>You don't need this.  Just use nir_tex_instr_dest_size().<br></div><span class=""><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></span><div>I don't really like the maybe_  Why not just call it "get_texture_size" or something like that.<br></div><span class=""><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></span><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 class="h5"><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->deref<wbr>)->type;<br>
+   const unsigned num_components =<br>
+      sampler_dim_components(glsl_ge<wbr>t_sampler_dim(type));<br>
+   nir_tex_instr *tex = nir_tex_instr_create(b->shader<wbr>, 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(typ<wbr>e);<br>
+   tex->is_shadow = glsl_sampler_type_is_shadow(ty<wbr>pe);<br>
+   tex->texture = nir_deref_var_clone(texture, tex);<br>
+   tex->dest_type = nir_type_int;<br>
+<br>
+   nir_ssa_dest_init(&tex-><wbr>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(ni<wbr>r_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></div><div>nir_frcp<br></div><div><div class="h5"><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(s<wbr>truct 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_sca<wbr>les[c] > 1 &&<br>
+          conversion->chroma_offsets[c] == VK_CHROMA_LOCATION_COSITED_EVE<wbr>N_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_scal<wbr>es[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></div><div>Is non-2D YCBCR allowed?  I guess it may make sense to allow arrays.</div><span class=""><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_implic<wbr>it(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[pl<wbr>ane];<br>
+   nir_tex_instr *old_tex = state->origin_tex;<br>
+   nir_tex_instr *tex = nir_tex_instr_create(b->shader<wbr>, 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_reconstruct<wbr>ion) {<br></blockquote><div><br></div></span><div>assert(old_tex->src[i].src.is_<wbr>ssa);<br></div><div><div class="h5"><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_down<wbr>sampled_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].<wbr>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->t<wbr>exture, tex);<br>
+<br>
+   tex->sampler_index = old_tex->sampler_index;<br>
+   tex->sampler = nir_deref_var_clone(old_tex->s<wbr>ampler, tex);<br>
+<br>
+   nir_ssa_dest_init(&tex-><wbr>instr, &tex->dest,<br>
+                     old_tex->dest.ssa.num_compone<wbr>nts,<br>
+                     nir_dest_bit_size(old_tex->de<wbr>st), 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_multipl<wbr>anar(struct anv_pipeline *pipeline,<br></blockquote><div><br></div></div></div><div>How about try_lower_tex_ycbcr?<br></div><div><div class="h5"><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->dat<wbr>a.descriptor_set].layout;<br>
+   const struct anv_descriptor_set_binding_lay<wbr>out *binding =<br>
+      &set_layout->binding[var->data<wbr>.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[te<wbr>x->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></blockquote></div></div></div></div></div></blockquote><div><br></div><div>What about arrays of samplers?  You're not allowed to indirect on them but they can, in theory, exist and have different samplers with different conversions.  I think we need to fish the array index out and take sampler[idx].conversion here instead.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+   };<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_chroma<wbr>)<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[0<wbr>].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_implici<wbr>t(&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_swi<wbr>zzle);<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_fo<wbr>rmat->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->m<wbr>apping); 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_CONVERS<wbr>ION_RGB_IDENTITY_KHR)<br>
+      result = convert_ycbcr(&state, result, swizzled_bpcs);<br>
+<br>
+   nir_ssa_def_rewrite_uses(&<wbr>tex->dest.ssa, nir_src_for_ssa(result));<br>
+   nir_instr_remove(&tex->instr)<wbr>;<br>
+}<br>
+<br>
+void<br>
+anv_nir_lower_ycbcr_textures(<wbr>nir_shader *shader, struct anv_pipeline *pipeline)<br></blockquote><div><br></div></div></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><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+   nir_foreach_function(<wbr>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_multipla<wbr>nar(pipeline, &builder, tex);<br>
+         }<br>
+      }<br></blockquote><div><br></div></span><div>We need to call nir_metadata_preserve here.<br></div><div><div class="h5"><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_pipelin<wbr>e.c b/src/intel/vulkan/anv_pipelin<wbr>e.c<br>
index 94e99d8437a..b35bad10503 100644<br>
--- a/src/intel/vulkan/anv_pipelin<wbr>e.c<br>
+++ b/src/intel/vulkan/anv_pipelin<wbr>e.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_private<wbr>.h b/src/intel/vulkan/anv_private<wbr>.h<br>
index 63b803df6ea..5e14f49b196 100644<br>
--- a/src/intel/vulkan/anv_private<wbr>.h<br>
+++ b/src/intel/vulkan/anv_private<wbr>.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>
+   <wbr>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="m_7310995667424482070HOEnZb"><font color="#888888">--<br>
2.14.2<br>
<br>
______________________________<wbr>_________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="https://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/<wbr>mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div></div></div><br></div></div>
</blockquote></div><br></div></div>