<div dir="ltr">On 27 October 2013 12:58, 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Sun, Oct 27, 2013 at 4:19 PM, Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>> wrote:<br>
> On 20 October 2013 09:20, Marek Olšák <<a href="mailto:maraeo@gmail.com">maraeo@gmail.com</a>> wrote:<br>
>><br>
>> 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<br>
>> (array)<br>
>> and then writes the whole temporary variable to the output, generating<br>
>> assignments to all elements of gl_FragData.<br>
><br>
><br>
> Can you go into more detail about why this is a problem?  At first glance it<br>
> seems like it should be fine, because failing to assign to an element of<br>
> gl_FragData is supposed to result in undefined data going to that fragment<br>
> output.  So lowering to a shader that does assign to that element of<br>
> gl_FragData seems like it should be harmless.  What am I missing here?<br>
<br>
</div>Thanks for the review. The problem is drivers cannot eliminate useless<br>
writes to gl_FragData, and each enabled gl_FragData output decreases<br>
performance. The GLSL compiler cannot eliminate the writes either,<br>
because gl_FragData is an array.<br>
<br>
Maybe the GLSL compiler should resize arrays based on which elements<br>
are written, so that unused elements are not declared, but this is not<br>
enough for gl_FragData, where the i-th output can be written, but<br>
(i-1)-th output doesn't have to be.<br></blockquote><div><br></div><div>Ah, ok.  When I saw the word "defect", I misunderstood you to be fixing a correctness-of-rendering bug.  As a performance optimization, I get it now.<br>
<br></div><div>For driver back-ends that don't need lower_output_reads (such as i915 and i965), this optimization isn't needed.  Would you mind adding a flag to ShaderCompilerOptions so that tgsi-based drivers can opt in to this new optimization?  I want to make sure that the code generation of i965 and i915 isn't affected.<br>
<br>With that change (and the other fixes we talked about), this patch is:<br><br>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div><div class="h5"><br>
><br>
>><br>
>><br>
>> BTW this pass can be modified to lower all arrays, not just inputs and<br>
>> outputs.<br>
>> The question is whether it is worth it.<br>
>> ---<br>
>>  src/glsl/opt_dead_builtin_varyings.cpp | 163<br>
>> ++++++++++++++++++++++++++-------<br>
>>  1 file changed, 132 insertions(+), 31 deletions(-)<br>
>><br>
>> diff --git a/src/glsl/opt_dead_builtin_varyings.cpp<br>
>> 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<br>
>> 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 =<br>
>> 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>
>> {<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>
>> {<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>
>> {<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>
>> {<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,<br>
>> 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 +<br>
>> 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,<br>
>> 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,<br>
>> ARRAY_SIZE(this->new_fragdata),<br>
>> +                       FRAG_RESULT_DATA0, "FragData", mode_str,<br>
>> +                       info->fragdata_usage, (1 << MAX_DRAW_BUFFERS) -<br>
>> 1);<br>
>>        }<br>
>><br>
>>        /* Create dummy variables which will replace set-but-unused color<br>
>> 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,<br>
>> 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<br>
>> 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>
><br>
><br>
> This should say "gl_FragData[i] occurrence"<br>
<br>
</div></div>Will fix.<br>
<div><div class="h5"><br>
><br>
>><br>
>> +         ir_dereference_array *const da =<br>
>> (*rvalue)->as_dereference_array();<br>
>> +<br>
>> +         if (da && da->variable_referenced() ==<br>
>> this->info->fragdata_array) {<br>
>> +            unsigned i =<br>
>> da->array_index->as_constant()->get_uint_component(0);<br>
>> +<br>
>> +            *rvalue = new(ctx)<br>
>> ir_dereference_variable(this->new_fragdata[i]);<br>
>> +            return;<br>
>> +         }<br>
>> +      }<br>
>> +<br>
>>        /* Replace set-but-unused color and fog outputs with dummy<br>
>> variables. */<br>
>>        ir_dereference_variable *const dv =<br>
>> (*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<br>
>> 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>
><br>
><br>
> 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,<br>
> 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>
> Does this need to be changed so that when EXT_separate_shader_objects is<br>
> present, we still go ahead and lower gl_FragData?<br>
<br>
</div></div>Good point. Yes, we want to always lower gl_FragData.<br>
<span class="HOEnZb"><font color="#888888"><br>
Marek<br>
</font></span></blockquote></div><br></div></div>