[Mesa-dev] [PATCH v4 064/129] nir/lower_samplers: split out _legacy version for deref chains

Jason Ekstrand jason at jlekstrand.net
Fri Jun 1 05:03:54 UTC 2018


From: Rob Clark <robdclark at gmail.com>

To simplify the transition, and make things bisectable, split out a
legacy copy or lower_samplers.  This way the i965 and gallium drivers
can independently switch over to deref instructions.

Since the lower_samplers_as_deref pass is only used by gallium drivers,
it can be converted in lock-step with moving the lower_deref_instrs
pass, and so does not need a corresponding _legacy clone.

This legacy pass will be removed in a future commit.

Signed-off-by: Rob Clark <robdclark at gmail.com>
---
 src/compiler/Makefile.sources                    |   1 +
 src/compiler/glsl/gl_nir.h                       |   2 +
 src/compiler/glsl/gl_nir_lower_samplers_legacy.c | 163 +++++++++++++++++++++++
 src/compiler/glsl/meson.build                    |   1 +
 src/gallium/drivers/freedreno/ir3/ir3_cmdline.c  |   2 +-
 src/mesa/state_tracker/st_glsl_to_nir.cpp        |   2 +-
 6 files changed, 169 insertions(+), 2 deletions(-)
 create mode 100644 src/compiler/glsl/gl_nir_lower_samplers_legacy.c

diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources
index 57a3dae..971afdf 100644
--- a/src/compiler/Makefile.sources
+++ b/src/compiler/Makefile.sources
@@ -27,6 +27,7 @@ LIBGLSL_FILES = \
 	glsl/generate_ir.cpp \
 	glsl/gl_nir_lower_atomics.c \
 	glsl/gl_nir_lower_samplers.c \
+	glsl/gl_nir_lower_samplers_legacy.c \
 	glsl/gl_nir_lower_samplers_as_deref.c \
 	glsl/gl_nir.h \
 	glsl/glsl_parser_extras.cpp \
diff --git a/src/compiler/glsl/gl_nir.h b/src/compiler/glsl/gl_nir.h
index 59d5f65..17cc5da 100644
--- a/src/compiler/glsl/gl_nir.h
+++ b/src/compiler/glsl/gl_nir.h
@@ -37,6 +37,8 @@ bool gl_nir_lower_atomics(nir_shader *shader,
 
 bool gl_nir_lower_samplers(nir_shader *shader,
                            const struct gl_shader_program *shader_program);
+bool gl_nir_lower_samplers_legacy(nir_shader *shader,
+                           const struct gl_shader_program *shader_program);
 bool gl_nir_lower_samplers_as_deref(nir_shader *shader,
                                     const struct gl_shader_program *shader_program);
 
diff --git a/src/compiler/glsl/gl_nir_lower_samplers_legacy.c b/src/compiler/glsl/gl_nir_lower_samplers_legacy.c
new file mode 100644
index 0000000..d213b9f
--- /dev/null
+++ b/src/compiler/glsl/gl_nir_lower_samplers_legacy.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
+ * Copyright © 2014 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "compiler/nir/nir.h"
+#include "compiler/nir/nir_builder.h"
+#include "gl_nir.h"
+#include "ir_uniform.h"
+
+#include "main/compiler.h"
+#include "main/mtypes.h"
+
+/* Calculate the sampler index based on array indicies and also
+ * calculate the base uniform location for struct members.
+ */
+static void
+calc_sampler_offsets(nir_deref *tail, nir_tex_instr *instr,
+                     unsigned *array_elements, nir_ssa_def **indirect,
+                     nir_builder *b, unsigned *location)
+{
+   if (tail->child == NULL)
+      return;
+
+   switch (tail->child->deref_type) {
+   case nir_deref_type_array: {
+      nir_deref_array *deref_array = nir_deref_as_array(tail->child);
+
+      assert(deref_array->deref_array_type != nir_deref_array_type_wildcard);
+
+      calc_sampler_offsets(tail->child, instr, array_elements,
+                           indirect, b, location);
+      instr->texture_index += deref_array->base_offset * *array_elements;
+
+      if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
+         nir_ssa_def *mul =
+            nir_imul(b, nir_imm_int(b, *array_elements),
+                     nir_ssa_for_src(b, deref_array->indirect, 1));
+
+         nir_instr_rewrite_src(&instr->instr, &deref_array->indirect,
+                               NIR_SRC_INIT);
+
+         if (*indirect) {
+            *indirect = nir_iadd(b, *indirect, mul);
+         } else {
+            *indirect = mul;
+         }
+      }
+
+      *array_elements *= glsl_get_length(tail->type);
+       break;
+   }
+
+   case nir_deref_type_struct: {
+      nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);
+      *location += glsl_get_record_location_offset(tail->type, deref_struct->index);
+      calc_sampler_offsets(tail->child, instr, array_elements,
+                           indirect, b, location);
+      break;
+   }
+
+   default:
+      unreachable("Invalid deref type");
+      break;
+   }
+}
+
+static bool
+lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_program,
+              gl_shader_stage stage, nir_builder *b)
+{
+   if (instr->texture == NULL)
+      return false;
+
+   instr->texture_index = 0;
+   unsigned location = instr->texture->var->data.location;
+   unsigned array_elements = 1;
+   nir_ssa_def *indirect = NULL;
+
+   b->cursor = nir_before_instr(&instr->instr);
+   calc_sampler_offsets(&instr->texture->deref, instr, &array_elements,
+                        &indirect, b, &location);
+
+   if (indirect) {
+      assert(array_elements >= 1);
+      indirect = nir_umin(b, indirect, nir_imm_int(b, array_elements - 1));
+
+      nir_tex_instr_add_src(instr, nir_tex_src_texture_offset,
+                            nir_src_for_ssa(indirect));
+      nir_tex_instr_add_src(instr, nir_tex_src_sampler_offset,
+                            nir_src_for_ssa(indirect));
+
+      instr->texture_array_size = array_elements;
+   }
+
+   assert(location < shader_program->data->NumUniformStorage &&
+          shader_program->data->UniformStorage[location].opaque[stage].active);
+
+   instr->texture_index +=
+      shader_program->data->UniformStorage[location].opaque[stage].index;
+
+   instr->sampler_index = instr->texture_index;
+
+   instr->texture = NULL;
+
+   return true;
+}
+
+static bool
+lower_impl(nir_function_impl *impl, const struct gl_shader_program *shader_program,
+           gl_shader_stage stage)
+{
+   nir_builder b;
+   nir_builder_init(&b, impl);
+   bool progress = false;
+
+   nir_foreach_block(block, impl) {
+      nir_foreach_instr(instr, block) {
+         if (instr->type == nir_instr_type_tex)
+            progress |= lower_sampler(nir_instr_as_tex(instr),
+                                      shader_program, stage, &b);
+      }
+   }
+
+   return progress;
+}
+
+bool
+gl_nir_lower_samplers_legacy(nir_shader *shader,
+                      const struct gl_shader_program *shader_program)
+{
+   bool progress = false;
+
+   nir_assert_lowered_derefs(shader, nir_lower_texture_derefs);
+
+   nir_foreach_function(function, shader) {
+      if (function->impl)
+         progress |= lower_impl(function->impl, shader_program,
+                                shader->info.stage);
+   }
+
+   return progress;
+}
diff --git a/src/compiler/glsl/meson.build b/src/compiler/glsl/meson.build
index 055a847..68f568c 100644
--- a/src/compiler/glsl/meson.build
+++ b/src/compiler/glsl/meson.build
@@ -68,6 +68,7 @@ files_libglsl = files(
   'generate_ir.cpp',
   'gl_nir_lower_atomics.c',
   'gl_nir_lower_samplers.c',
+  'gl_nir_lower_samplers_legacy.c',
   'gl_nir_lower_samplers_as_deref.c',
   'gl_nir.h',
   'glsl_parser_extras.cpp',
diff --git a/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c b/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
index ab2fbf6..1fa1a72 100644
--- a/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
+++ b/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
@@ -164,7 +164,7 @@ load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
 
 	NIR_PASS_V(nir, nir_lower_system_values);
 	NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size, 0);
-	NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
+	NIR_PASS_V(nir, gl_nir_lower_samplers_legacy, prog);
 
 	return nir;
 }
diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp b/src/mesa/state_tracker/st_glsl_to_nir.cpp
index f5c247c..aa8fb58 100644
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
@@ -818,7 +818,7 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
    if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
       NIR_PASS_V(nir, gl_nir_lower_samplers_as_deref, shader_program);
    else
-      NIR_PASS_V(nir, gl_nir_lower_samplers, shader_program);
+      NIR_PASS_V(nir, gl_nir_lower_samplers_legacy, shader_program);
 }
 
 } /* extern "C" */
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list