<div dir="ltr">On 20 October 2013 09:20, Marek Olšák <span dir="ltr"><<a href="mailto:maraeo@gmail.com" target="_blank">maraeo@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
From: Marek Olšák <<a href="mailto:marek.olsak@amd.com">marek.olsak@amd.com</a>><br>
<br>
This avoids a defect in lower_output_reads.<br>
<br>
The problem is lower_output_reads treats the gl_FragData array as a single<br>
variable. It first redirects all output writes to a temporary variable (array)<br>
and then writes the whole temporary variable to the output, generating<br>
assignments to all elements of gl_FragData.<br></blockquote><div><br></div><div>Can you go into more detail about why this is a problem?  At first glance it seems like it should be fine, because failing to assign to an element of gl_FragData is supposed to result in undefined data going to that fragment output.  So lowering to a shader that does assign to that element of gl_FragData seems like it should be harmless.  What am I missing here?<br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
BTW this pass can be modified to lower all arrays, not just inputs and outputs.<br>
The question is whether it is worth it.<br>
---<br>
 src/glsl/opt_dead_builtin_varyings.cpp | 163 ++++++++++++++++++++++++++-------<br>
 1 file changed, 132 insertions(+), 31 deletions(-)<br>
<br>
diff --git a/src/glsl/opt_dead_builtin_varyings.cpp b/src/glsl/opt_dead_builtin_varyings.cpp<br>
index 7e8cd43..72ff9df 100644<br>
--- a/src/glsl/opt_dead_builtin_varyings.cpp<br>
+++ b/src/glsl/opt_dead_builtin_varyings.cpp<br>
@@ -42,9 +42,11 @@<br>
  * If any texture coordinate slots can be eliminated, the gl_TexCoord array is<br>
  * broken down into separate vec4 variables with locations equal to<br>
  * VARYING_SLOT_TEX0 + i.<br>
+ *<br>
+ * The same is done for the gl_FragData fragment shader output.<br>
  */<br>
<br>
-#include "main/imports.h" /* for snprintf */<br>
+#include "main/core.h" /* for snprintf and ARRAY_SIZE */<br>
 #include "ir.h"<br>
 #include "ir_rvalue_visitor.h"<br>
 #include "ir_optimization.h"<br>
@@ -60,10 +62,14 @@ namespace {<br>
 class varying_info_visitor : public ir_hierarchical_visitor {<br>
 public:<br>
    /* "mode" can be either ir_var_shader_in or ir_var_shader_out */<br>
-   varying_info_visitor(ir_variable_mode mode)<br>
+   varying_info_visitor(ir_variable_mode mode, bool find_frag_outputs = false)<br>
       : lower_texcoord_array(true),<br>
         texcoord_array(NULL),<br>
         texcoord_usage(0),<br>
+        find_frag_outputs(find_frag_outputs),<br>
+        lower_fragdata_array(true),<br>
+        fragdata_array(NULL),<br>
+        fragdata_usage(0),<br>
         color_usage(0),<br>
         tfeedback_color_usage(0),<br>
         fog(NULL),<br>
@@ -79,8 +85,27 @@ public:<br>
    {<br>
       ir_variable *var = ir->variable_referenced();<br>
<br>
-      if (var && var->mode == this->mode &&<br>
-          var->location == VARYING_SLOT_TEX0) {<br>
+      if (!var || var->mode != this->mode)<br>
+         return visit_continue;<br>
+<br>
+      if (this->find_frag_outputs && var->location == FRAG_RESULT_DATA0) {<br>
+         this->fragdata_array = var;<br>
+<br>
+         ir_constant *index = ir->array_index->as_constant();<br>
+         if (index == NULL) {<br>
+            /* This is variable indexing. */<br>
+            this->fragdata_usage |= (1 << var->type->array_size()) - 1;<br>
+            this->lower_fragdata_array = false;<br>
+         }<br>
+         else {<br>
+            this->fragdata_usage |= 1 << index->get_uint_component(0);<br>
+         }<br>
+<br>
+         /* Don't visit the leaves of ir_dereference_array. */<br>
+         return visit_continue_with_parent;<br>
+      }<br>
+<br>
+      if (!this->find_frag_outputs && var->location == VARYING_SLOT_TEX0) {<br>
          this->texcoord_array = var;<br>
<br>
          ir_constant *index = ir->array_index->as_constant();<br>
@@ -105,8 +130,17 @@ public:<br>
    {<br>
       ir_variable *var = ir->variable_referenced();<br>
<br>
-      if (var->mode == this->mode && var->type->is_array() &&<br>
-          var->location == VARYING_SLOT_TEX0) {<br>
+      if (var->mode != this->mode || !var->type->is_array())<br>
+         return visit_continue;<br>
+<br>
+      if (this->find_frag_outputs && var->location == FRAG_RESULT_DATA0) {<br>
+         /* This is a whole array dereference. */<br>
+         this->fragdata_usage |= (1 << var->type->array_size()) - 1;<br>
+         this->lower_fragdata_array = false;<br>
+         return visit_continue;<br>
+      }<br>
+<br>
+      if (!this->find_frag_outputs && var->location == VARYING_SLOT_TEX0) {<br>
          /* This is a whole array dereference like "gl_TexCoord = x;",<br>
           * there's probably no point in lowering that.<br>
           */<br>
@@ -121,6 +155,10 @@ public:<br>
       if (var->mode != this->mode)<br>
          return visit_continue;<br>
<br>
+      /* Nothing to do here for fragment outputs. */<br>
+      if (this->find_frag_outputs)<br>
+         return visit_continue;<br>
+<br>
       /* Handle colors and fog. */<br>
       switch (var->location) {<br>
       case VARYING_SLOT_COL0:<br>
@@ -185,12 +223,20 @@ public:<br>
       if (!this->texcoord_array) {<br>
          this->lower_texcoord_array = false;<br>
       }<br>
+      if (!this->fragdata_array) {<br>
+         this->lower_fragdata_array = false;<br>
+      }<br>
    }<br>
<br>
    bool lower_texcoord_array;<br>
    ir_variable *texcoord_array;<br>
    unsigned texcoord_usage; /* bitmask */<br>
<br>
+   bool find_frag_outputs; /* false if it's looking for varyings */<br>
+   bool lower_fragdata_array;<br>
+   ir_variable *fragdata_array;<br>
+   unsigned fragdata_usage; /* bitmask */<br>
+<br>
    ir_variable *color[2];<br>
    ir_variable *backcolor[2];<br>
    unsigned color_usage; /* bitmask */<br>
@@ -222,6 +268,7 @@ public:<br>
    {<br>
       void *const ctx = ir;<br>
<br>
+      memset(this->new_fragdata, 0, sizeof(this->new_fragdata));<br>
       memset(this->new_texcoord, 0, sizeof(this->new_texcoord));<br>
       memset(this->new_color, 0, sizeof(this->new_color));<br>
       memset(this->new_backcolor, 0, sizeof(this->new_backcolor));<br>
@@ -236,31 +283,16 @@ public:<br>
        * occurences of gl_TexCoord will be replaced with.<br>
        */<br>
       if (info->lower_texcoord_array) {<br>
-         for (int i = MAX_TEXTURE_COORD_UNITS-1; i >= 0; i--) {<br>
-            if (info->texcoord_usage & (1 << i)) {<br>
-               char name[32];<br>
-<br>
-               if (!(external_texcoord_usage & (1 << i))) {<br>
-                  /* This varying is unused in the next stage. Declare<br>
-                   * a temporary instead of an output. */<br>
-                  snprintf(name, 32, "gl_%s_TexCoord%i_dummy", mode_str, i);<br>
-                  this->new_texcoord[i] =<br>
-                     new (ctx) ir_variable(glsl_type::vec4_type, name,<br>
-                                           ir_var_temporary);<br>
-               }<br>
-               else {<br>
-                  snprintf(name, 32, "gl_%s_TexCoord%i", mode_str, i);<br>
-                  this->new_texcoord[i] =<br>
-                     new(ctx) ir_variable(glsl_type::vec4_type, name,<br>
-                                          info->mode);<br>
-                  this->new_texcoord[i]->location = VARYING_SLOT_TEX0 + i;<br>
-                  this->new_texcoord[i]->explicit_location = true;<br>
-                  this->new_texcoord[i]->explicit_index = 0;<br>
-               }<br>
-<br>
-               ir->head->insert_before(new_texcoord[i]);<br>
-            }<br>
-         }<br>
+         prepare_array(ir, this->new_texcoord, ARRAY_SIZE(this->new_texcoord),<br>
+                       VARYING_SLOT_TEX0, "TexCoord", mode_str,<br>
+                       info->texcoord_usage, external_texcoord_usage);<br>
+      }<br>
+<br>
+      /* Handle gl_FragData in the same way like gl_TexCoord. */<br>
+      if (info->lower_fragdata_array) {<br>
+         prepare_array(ir, this->new_fragdata, ARRAY_SIZE(this->new_fragdata),<br>
+                       FRAG_RESULT_DATA0, "FragData", mode_str,<br>
+                       info->fragdata_usage, (1 << MAX_DRAW_BUFFERS) - 1);<br>
       }<br>
<br>
       /* Create dummy variables which will replace set-but-unused color and<br>
@@ -301,6 +333,41 @@ public:<br>
       visit_list_elements(this, ir);<br>
    }<br>
<br>
+   void prepare_array(exec_list *ir,<br>
+                      struct ir_variable **new_var,<br>
+                      int max_elements, unsigned start_location,<br>
+                      const char *var_name, const char *mode_str,<br>
+                      unsigned usage, unsigned external_usage)<br>
+   {<br>
+      void *const ctx = ir;<br>
+<br>
+      for (int i = max_elements-1; i >= 0; i--) {<br>
+         if (usage & (1 << i)) {<br>
+            char name[32];<br>
+<br>
+            if (!(external_usage & (1 << i))) {<br>
+               /* This varying is unused in the next stage. Declare<br>
+                * a temporary instead of an output. */<br>
+               snprintf(name, 32, "gl_%s_%s%i_dummy", mode_str, var_name, i);<br>
+               new_var[i] =<br>
+                  new (ctx) ir_variable(glsl_type::vec4_type, name,<br>
+                                        ir_var_temporary);<br>
+            }<br>
+            else {<br>
+               snprintf(name, 32, "gl_%s_%s%i", mode_str, var_name, i);<br>
+               new_var[i] =<br>
+                  new(ctx) ir_variable(glsl_type::vec4_type, name,<br>
+                                       this->info->mode);<br>
+               new_var[i]->location = start_location + i;<br>
+               new_var[i]->explicit_location = true;<br>
+               new_var[i]->explicit_index = 0;<br>
+            }<br>
+<br>
+            ir->head->insert_before(new_var[i]);<br>
+         }<br>
+      }<br>
+   }<br>
+<br>
    virtual ir_visitor_status visit(ir_variable *var)<br>
    {<br>
       /* Remove the gl_TexCoord array. */<br>
@@ -309,6 +376,12 @@ public:<br>
          var->remove();<br>
       }<br>
<br>
+      /* Remove the gl_FragData array. */<br>
+      if (this->info->lower_fragdata_array &&<br>
+          var == this->info->fragdata_array) {<br>
+         var->remove();<br>
+      }<br>
+<br>
       /* Replace set-but-unused color and fog outputs with dummy variables. */<br>
       for (int i = 0; i < 2; i++) {<br>
          if (var == this->info->color[i] && this->new_color[i]) {<br>
@@ -350,6 +423,19 @@ public:<br>
          }<br>
       }<br>
<br>
+      /* Same for gl_FragData. */<br>
+      if (this->info->lower_fragdata_array) {<br>
+         /* gl_TexCoord[i] occurence */<br></blockquote><div><br></div><div>This should say "gl_FragData[i] occurrence"<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

+         ir_dereference_array *const da = (*rvalue)->as_dereference_array();<br>
+<br>
+         if (da && da->variable_referenced() == this->info->fragdata_array) {<br>
+            unsigned i = da->array_index->as_constant()->get_uint_component(0);<br>
+<br>
+            *rvalue = new(ctx) ir_dereference_variable(this->new_fragdata[i]);<br>
+            return;<br>
+         }<br>
+      }<br>
+<br>
       /* Replace set-but-unused color and fog outputs with dummy variables. */<br>
       ir_dereference_variable *const dv = (*rvalue)->as_dereference_variable();<br>
       if (!dv)<br>
@@ -392,6 +478,7 @@ public:<br>
<br>
 private:<br>
    const varying_info_visitor *info;<br>
+   ir_variable *new_fragdata[MAX_DRAW_BUFFERS];<br>
    ir_variable *new_texcoord[MAX_TEXTURE_COORD_UNITS];<br>
    ir_variable *new_color[2];<br>
    ir_variable *new_backcolor[2];<br>
@@ -408,6 +495,15 @@ lower_texcoord_array(exec_list *ir, const varying_info_visitor *info)<br>
                             1 | 2, true);<br>
 }<br>
<br>
+static void<br>
+lower_fragdata_array(exec_list *ir)<br>
+{<br>
+   varying_info_visitor info(ir_var_shader_out, true);<br>
+   info.get(ir, 0, NULL);<br>
+<br>
+   replace_varyings_visitor(ir, &info, 0, 0, 0);<br>
+}<br>
+<br>
<br>
 void<br>
 do_dead_builtin_varyings(struct gl_context *ctx,<br>
@@ -487,4 +583,9 @@ do_dead_builtin_varyings(struct gl_context *ctx,<br>
                                producer_info.color_usage,<br>
                                producer_info.has_fog);<br>
    }<br></blockquote><div><br></div><div>At the top of the do_dead_builtin_varyings() function there's this logic:<br><br>   /* This optimization has no effect with the core context and GLES2, because<br>    * the built-in varyings we're eliminating here are not available there.<br>
    *<br>    * EXT_separate_shader_objects doesn't allow this optimization,<br>    * because a program object can be bound partially (e.g. only one<br>    * stage of a program object can be bound).<br>    */<br>   if (ctx->API == API_OPENGL_CORE ||<br>
       ctx->API == API_OPENGLES2 ||<br>       ctx->Extensions.EXT_separate_shader_objects) {<br>      return;<br>   }<br><br></div><div>Does this need to be changed so that when EXT_separate_shader_objects is present, we still go ahead and lower gl_FragData? <br>
</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+<br>
+   /* Finally, lower the gl_FragData array to separate variables. */<br>
+   if (consumer->Type == GL_FRAGMENT_SHADER) {<br>
+      lower_fragdata_array(consumer->ir);<br>
+   }<br>
 }<br>
<span class=""><font color="#888888">--<br>
1.8.1.2<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>