i965/vec4: split VEC4_OPCODE_FROM_DOUBLE into one opcode per destination's type
[mesa.git] / src / compiler / nir / nir_lower_samplers.c
index 9c912129f09d2b1fd96226b4ed1bb851f8035d9d..0c4e91b000fac1968fb78fc518b567802d753c30 100644 (file)
@@ -25,8 +25,7 @@
 
 #include "nir.h"
 #include "nir_builder.h"
-#include "program/hash_table.h"
-#include "glsl/ir_uniform.h"
+#include "compiler/glsl/ir_uniform.h"
 
 #include "main/compiler.h"
 #include "main/mtypes.h"
@@ -52,7 +51,7 @@ calc_sampler_offsets(nir_deref *tail, nir_tex_instr *instr,
 
       calc_sampler_offsets(tail->child, instr, array_elements,
                            indirect, b, location);
-      instr->sampler_index += deref_array->base_offset * *array_elements;
+      instr->texture_index += deref_array->base_offset * *array_elements;
 
       if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
          nir_ssa_def *mul =
@@ -87,26 +86,32 @@ calc_sampler_offsets(nir_deref *tail, nir_tex_instr *instr,
    }
 }
 
-static void
+static bool
 lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_program,
-              gl_shader_stage stage, nir_builder *builder)
+              gl_shader_stage stage, nir_builder *b)
 {
-   if (instr->sampler == NULL)
-      return;
+   if (instr->texture == NULL)
+      return false;
+
+   /* In GLSL, we only fill out the texture field.  The sampler is inferred */
+   assert(instr->sampler == NULL);
 
-   instr->sampler_index = 0;
-   unsigned location = instr->sampler->var->data.location;
+   instr->texture_index = 0;
+   unsigned location = instr->texture->var->data.location;
    unsigned array_elements = 1;
    nir_ssa_def *indirect = NULL;
 
-   builder->cursor = nir_before_instr(&instr->instr);
-   calc_sampler_offsets(&instr->sampler->deref, instr, &array_elements,
-                        &indirect, builder, &location);
+   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));
+
       /* First, we have to resize the array of texture sources */
       nir_tex_src *new_srcs = rzalloc_array(instr, nir_tex_src,
-                                            instr->num_srcs + 1);
+                                            instr->num_srcs + 2);
 
       for (unsigned i = 0; i < instr->num_srcs; i++) {
          new_srcs[i].src_type = instr->src[i].src_type;
@@ -120,68 +125,63 @@ lower_sampler(nir_tex_instr *instr, const struct gl_shader_program *shader_progr
       /* Now we can go ahead and move the source over to being a
        * first-class texture source.
        */
-      instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
+      instr->src[instr->num_srcs].src_type = nir_tex_src_texture_offset;
       instr->num_srcs++;
       nir_instr_rewrite_src(&instr->instr,
                             &instr->src[instr->num_srcs - 1].src,
                             nir_src_for_ssa(indirect));
 
-      instr->sampler_array_size = array_elements;
-   }
+      instr->src[instr->num_srcs].src_type = nir_tex_src_sampler_offset;
+      instr->num_srcs++;
+      nir_instr_rewrite_src(&instr->instr,
+                            &instr->src[instr->num_srcs - 1].src,
+                            nir_src_for_ssa(indirect));
 
-   if (location > shader_program->NumUniformStorage - 1 ||
-       !shader_program->UniformStorage[location].opaque[stage].active) {
-      assert(!"cannot return a sampler");
-      return;
+      instr->texture_array_size = array_elements;
    }
 
-   instr->sampler_index +=
-      shader_program->UniformStorage[location].opaque[stage].index;
+   assert(location < shader_program->data->NumUniformStorage &&
+          shader_program->data->UniformStorage[location].opaque[stage].active);
 
-   instr->sampler = NULL;
-}
+   instr->texture_index +=
+      shader_program->data->UniformStorage[location].opaque[stage].index;
 
-typedef struct {
-   nir_builder builder;
-   const struct gl_shader_program *shader_program;
-   gl_shader_stage stage;
-} lower_state;
+   instr->sampler_index = instr->texture_index;
 
-static bool
-lower_block_cb(nir_block *block, void *_state)
-{
-   lower_state *state = (lower_state *) _state;
-
-   nir_foreach_instr(block, instr) {
-      if (instr->type == nir_instr_type_tex) {
-         nir_tex_instr *tex_instr = nir_instr_as_tex(instr);
-         lower_sampler(tex_instr, state->shader_program, state->stage,
-                       &state->builder);
-      }
-   }
+   instr->texture = NULL;
 
    return true;
 }
 
-static void
+static bool
 lower_impl(nir_function_impl *impl, const struct gl_shader_program *shader_program,
            gl_shader_stage stage)
 {
-   lower_state state;
-
-   nir_builder_init(&state.builder, impl);
-   state.shader_program = shader_program;
-   state.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);
+      }
+   }
 
-   nir_foreach_block(impl, lower_block_cb, &state);
+   return progress;
 }
 
-void
+bool
 nir_lower_samplers(nir_shader *shader,
                    const struct gl_shader_program *shader_program)
 {
-   nir_foreach_function(shader, function) {
+   bool progress = false;
+
+   nir_foreach_function(function, shader) {
       if (function->impl)
-         lower_impl(function->impl, shader_program, shader->stage);
+         progress |= lower_impl(function->impl, shader_program, shader->stage);
    }
+
+   return progress;
 }