glx: Sync <GL/glxext.h> with Khronos
[mesa.git] / src / mesa / state_tracker / st_nir_lower_builtin.c
index b4da9017d5cf7afeb624b828a1d0eb9f0a73bcf3..f6f9bf15278cc5d21460b2a18b5ec53aaff9c94a 100644 (file)
  *
  * with appropriate substitutions in the uniform variables list:
  *
- *    decl_var uniform INTERP_QUALIFIER_NONE gl_FogParameters gl_Fog (0, 0)
+ *    decl_var uniform INTERP_MODE_NONE gl_FogParameters gl_Fog (0, 0)
  *
  * would become:
  *
- *    decl_var uniform INTERP_QUALIFIER_NONE vec4 state.fog.color (0, 0)
- *    decl_var uniform INTERP_QUALIFIER_NONE vec4 state.fog.params (0, 1)
+ *    decl_var uniform INTERP_MODE_NONE vec4 state.fog.color (0, 0)
+ *    decl_var uniform INTERP_MODE_NONE vec4 state.fog.params (0, 1)
  *
  * See in particular 'struct gl_builtin_uniform_element'.
  */
 
 #include "compiler/nir/nir.h"
 #include "compiler/nir/nir_builder.h"
+#include "compiler/nir/nir_deref.h"
 #include "st_nir.h"
 #include "compiler/glsl/ir.h"
 #include "uniforms.h"
@@ -70,45 +71,42 @@ typedef struct {
 } lower_builtin_state;
 
 static const struct gl_builtin_uniform_element *
-get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_var *deref)
+get_element(const struct gl_builtin_uniform_desc *desc, nir_deref_path *path)
 {
-   nir_deref *tail = &deref->deref;
+   int idx = 1;
+
+   assert(path->path[0]->deref_type == nir_deref_type_var);
 
    if ((desc->num_elements == 1) && (desc->elements[0].field == NULL))
       return NULL;
 
    /* we handle arrays in get_variable(): */
-   if (tail->child->deref_type == nir_deref_type_array)
-      tail = tail->child;
+   if (path->path[idx]->deref_type == nir_deref_type_array)
+      idx++;
 
    /* don't need to deal w/ non-struct or array of non-struct: */
-   if (!tail->child)
+   if (!path->path[idx])
       return NULL;
 
-   if (tail->child->deref_type != nir_deref_type_struct)
+   if (path->path[idx]->deref_type != nir_deref_type_struct)
       return NULL;
 
-   nir_deref_struct *deref_struct = nir_deref_as_struct(tail->child);
-
-   assert(deref_struct->index < desc->num_elements);
+   assert(path->path[idx]->strct.index < desc->num_elements);
 
-   return &desc->elements[deref_struct->index];
+   return &desc->elements[path->path[idx]->strct.index ];
 }
 
 static nir_variable *
-get_variable(lower_builtin_state *state, nir_deref_var *deref,
+get_variable(lower_builtin_state *state, nir_deref_path *path,
              const struct gl_builtin_uniform_element *element)
 {
    nir_shader *shader = state->shader;
-   int tokens[STATE_LENGTH];
+   gl_state_index16 tokens[STATE_LENGTH];
+   int idx = 1;
 
    memcpy(tokens, element->tokens, sizeof(tokens));
 
-   if (deref->deref.child->deref_type == nir_deref_type_array) {
-      nir_deref_array *darr = nir_deref_as_array(deref->deref.child);
-
-      assert(darr->deref_array_type == nir_deref_array_type_direct);
-
+   if (path->path[idx]->deref_type == nir_deref_type_array) {
       /* we need to fixup the array index slot: */
       switch (tokens[0]) {
       case STATE_MODELVIEW_MATRIX:
@@ -121,16 +119,19 @@ get_variable(lower_builtin_state *state, nir_deref_var *deref,
       case STATE_TEXGEN:
       case STATE_TEXENV_COLOR:
       case STATE_CLIPPLANE:
-         tokens[1] = darr->base_offset;
+         tokens[1] = nir_src_as_uint(path->path[idx]->arr.index);
          break;
       }
    }
 
-   char *name = _mesa_program_state_string((gl_state_index *)tokens);
+   char *name = _mesa_program_state_string(tokens);
 
-   nir_foreach_variable(var, &shader->uniforms)
-      if (strcmp(var->name, name) == 0)
+   nir_foreach_variable(var, &shader->uniforms) {
+      if (strcmp(var->name, name) == 0) {
+         free(name);
          return var;
+      }
+   }
 
    /* variable doesn't exist yet, so create it: */
    nir_variable *var =
@@ -150,6 +151,7 @@ static bool
 lower_builtin_block(lower_builtin_state *state, nir_block *block)
 {
    nir_builder *b = &state->builder;
+   bool progress = false;
 
    nir_foreach_instr_safe(instr, block) {
       if (instr->type != nir_instr_type_intrinsic)
@@ -157,10 +159,11 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
 
       nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
 
-      if (intrin->intrinsic != nir_intrinsic_load_var)
+      if (intrin->intrinsic != nir_intrinsic_load_deref)
          continue;
 
-      nir_variable *var = intrin->variables[0]->var;
+      nir_variable *var =
+         nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[0]));
       if (var->data.mode != nir_var_uniform)
          continue;
 
@@ -175,12 +178,16 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
       if (!desc)
          continue;
 
-      const struct gl_builtin_uniform_element *element =
-         get_element(desc, intrin->variables[0]);
+      nir_deref_path path;
+      nir_deref_path_init(&path, nir_src_as_deref(intrin->src[0]), NULL);
+
+      const struct gl_builtin_uniform_element *element = get_element(desc, &path);
 
       /* matrix elements (array_deref) do not need special handling: */
-      if (!element)
+      if (!element) {
+         nir_deref_path_finish(&path);
          continue;
+      }
 
       /* remove existing var from uniform list: */
       exec_node_remove(&var->node);
@@ -189,8 +196,8 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
        */
       exec_node_self_link(&var->node);
 
-      nir_variable *new_var =
-         get_variable(state, intrin->variables[0], element);
+      nir_variable *new_var = get_variable(state, &path, element);
+      nir_deref_path_finish(&path);
 
       b->cursor = nir_before_instr(instr);
 
@@ -202,7 +209,7 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
          swiz[i] = GET_SWZ(element->swizzle, i);
          assert(swiz[i] <= SWIZZLE_W);
       }
-      def = nir_swizzle(b, def, swiz, intrin->num_components, true);
+      def = nir_swizzle(b, def, swiz, intrin->num_components);
 
       /* and rewrite uses of original instruction: */
       assert(intrin->dest.is_ssa);
@@ -213,10 +220,12 @@ lower_builtin_block(lower_builtin_state *state, nir_block *block)
        * to remove'd var.  And we have to remove the original uniform
        * var since we don't want it to get uniform space allocated.
        */
-      exec_node_remove(&intrin->instr.node);
+      nir_instr_remove(&intrin->instr);
+
+      progress = true;
    }
 
-   return true;
+   return progress;
 }
 
 static void
@@ -225,10 +234,14 @@ lower_builtin_impl(lower_builtin_state *state, nir_function_impl *impl)
    nir_builder_init(&state->builder, impl);
    state->mem_ctx = ralloc_parent(impl);
 
+   bool progress = false;
    nir_foreach_block(block, impl) {
-      lower_builtin_block(state, block);
+      progress |= lower_builtin_block(state, block);
    }
 
+   if (progress)
+      nir_remove_dead_derefs_impl(impl);
+
    nir_metadata_preserve(impl, nir_metadata_block_index |
                                nir_metadata_dominance);
 }