mesa/st: rewrite src var when lowering tex_src_plane
authorPierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Thu, 18 Jul 2019 13:04:05 +0000 (15:04 +0200)
committerMarek Olšák <marek.olsak@amd.com>
Tue, 23 Jul 2019 19:00:43 +0000 (15:00 -0400)
The assign_extra_samplers() adds the needed extra samplers but they need
to be used in the nir_tex_instr.
Otherwise the plane information is simply lost and all nir_tex_instr use the
same sampler.
Here's an example of the bug:

NIR before st_nir_lower_tex_src_plane:
vec1 32 ssa_8 = load_const (0x00000000 /* 0.000000 */)
vec4 32 ssa_9 = tex ssa_0 (texture_deref), ssa_0 (sampler_deref), ssa_5 (coord), ssa_8 (plane)
vec1 32 ssa_10 = load_const (0x00000001 /* 0.000000 */)
vec4 32 ssa_11 = tex ssa_0 (texture_deref), ssa_0 (sampler_deref), ssa_5 (coord), ssa_10 (plane)

After:

vec4 32 ssa_9 = tex ssa_0 (texture_deref), ssa_0 (sampler_deref), ssa_5 (coord)
vec4 32 ssa_11 = tex ssa_0 (texture_deref), ssa_0 (sampler_deref), ssa_5 (coord)

This fixes the following piglit test for radeonsi + NIR:
  - ext_image_dma_buf_import-sample_nv12
  - ext_image_dma_buf_import-sample_yuv420
  - ext_image_dma_buf_import-sample_yvu420

Reviewed-by: Eric Anholt <eric@anholt.net>
Signed-off-by: Marek Olšák <marek.olsak@amd.com>
src/mesa/state_tracker/st_nir_lower_tex_src_plane.c

index 99f595a72b383f5ad255cf479435245b4e4b0289..090e5bf87bdd9434d7e0917f9aac3c7ead90b42a 100644 (file)
@@ -31,6 +31,7 @@
 
 #include "util/u_string.h"
 #include "compiler/nir/nir.h"
+#include "compiler/nir/nir_builder.h"
 #include "st_nir.h"
 
 typedef struct {
@@ -103,7 +104,7 @@ assign_extra_samplers(lower_tex_src_state *state, unsigned free_slots)
 }
 
 static void
-lower_tex_src_plane_block(lower_tex_src_state *state, nir_block *block)
+lower_tex_src_plane_block(nir_builder *b, lower_tex_src_state *state, nir_block *block)
 {
    nir_foreach_instr(instr, block) {
       if (instr->type != nir_instr_type_tex)
@@ -129,6 +130,28 @@ lower_tex_src_plane_block(lower_tex_src_state *state, nir_block *block)
                state->sampler_map[y_samp][plane[0].i32 - 1];
 
          state->shader->info.textures_used |= 1u << tex->texture_index;
+
+         /* For drivers using PIPE_CAP_NIR_SAMPLERS_AS_DEREF, we need
+          * to reference the correct sampler nir variable.
+          */
+         int tex_index = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
+         int samp_index = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
+         if (tex_index >= 0 && samp_index >= 0) {
+            b->cursor = nir_before_instr(&tex->instr);
+
+            nir_variable* samp = find_sampler(state, plane[0].i32);
+            assert(samp);
+
+            nir_deref_instr *tex_deref_instr = nir_build_deref_var(b, samp);
+            nir_ssa_def *tex_deref = &tex_deref_instr->dest.ssa;
+
+            nir_instr_rewrite_src(&tex->instr,
+                                  &tex->src[tex_index].src,
+                                  nir_src_for_ssa(tex_deref));
+            nir_instr_rewrite_src(&tex->instr,
+                                  &tex->src[samp_index].src,
+                                  nir_src_for_ssa(tex_deref));
+         }
       }
 
       nir_tex_instr_remove_src(tex, plane_index);
@@ -138,8 +161,11 @@ lower_tex_src_plane_block(lower_tex_src_state *state, nir_block *block)
 static void
 lower_tex_src_plane_impl(lower_tex_src_state *state, nir_function_impl *impl)
 {
+   nir_builder b;
+   nir_builder_init(&b, impl);
+
    nir_foreach_block(block, impl) {
-      lower_tex_src_plane_block(state, block);
+      lower_tex_src_plane_block(&b, state, block);
    }
 
    nir_metadata_preserve(impl, nir_metadata_block_index |