[Mesa-dev] [PATCH 2/2] tgsi/scan: turn a huge if-else-if.. chain into a switch statement

Nicolai Hähnle nhaehnle at gmail.com
Mon Nov 7 11:41:33 UTC 2016


On 05.11.2016 18:38, Marek Olšák wrote:
> From: Marek Olšák <marek.olsak at amd.com>

For the series:

Reviewed-by: Nicolai Hähnle <nicolai.haehnle at amd.com>

>
> ---
>  src/gallium/auxiliary/tgsi/tgsi_scan.c | 44 +++++++++++++++++++++++-----------
>  1 file changed, 30 insertions(+), 14 deletions(-)
>
> diff --git a/src/gallium/auxiliary/tgsi/tgsi_scan.c b/src/gallium/auxiliary/tgsi/tgsi_scan.c
> index 26cb2be..40a1340 100644
> --- a/src/gallium/auxiliary/tgsi/tgsi_scan.c
> +++ b/src/gallium/auxiliary/tgsi/tgsi_scan.c
> @@ -448,62 +448,72 @@ scan_declaration(struct tgsi_shader_info *info,
>           info->output_array_last[array_id] = fulldecl->Range.Last;
>           break;
>        }
>        info->array_max[file] = MAX2(info->array_max[file], array_id);
>     }
>
>     for (reg = fulldecl->Range.First; reg <= fulldecl->Range.Last; reg++) {
>        unsigned semName = fulldecl->Semantic.Name;
>        unsigned semIndex = fulldecl->Semantic.Index +
>           (reg - fulldecl->Range.First);
> +      int buffer;
> +      unsigned index, target, type;
>
>        /* only first 32 regs will appear in this bitfield */
>        info->file_mask[file] |= (1 << reg);
>        info->file_count[file]++;
>        info->file_max[file] = MAX2(info->file_max[file], (int)reg);
>
> -      if (file == TGSI_FILE_CONSTANT) {
> -         int buffer = 0;
> +      switch (file) {
> +      case TGSI_FILE_CONSTANT:
> +         buffer = 0;
>
>           if (fulldecl->Declaration.Dimension)
>              buffer = fulldecl->Dim.Index2D;
>
>           info->const_file_max[buffer] =
>              MAX2(info->const_file_max[buffer], (int)reg);
>           info->const_buffers_declared |= 1u << buffer;
> -      } else if (file == TGSI_FILE_IMAGE) {
> +         break;
> +
> +      case TGSI_FILE_IMAGE:
>           info->images_declared |= 1u << reg;
>           if (fulldecl->Image.Resource == TGSI_TEXTURE_BUFFER)
>              info->images_buffers |= 1 << reg;
> -      } else if (file == TGSI_FILE_BUFFER) {
> +         break;
> +
> +      case TGSI_FILE_BUFFER:
>           info->shader_buffers_declared |= 1u << reg;
> -      } else if (file == TGSI_FILE_INPUT) {
> +         break;
> +
> +      case TGSI_FILE_INPUT:
>           info->input_semantic_name[reg] = (ubyte) semName;
>           info->input_semantic_index[reg] = (ubyte) semIndex;
>           info->input_interpolate[reg] = (ubyte)fulldecl->Interp.Interpolate;
>           info->input_interpolate_loc[reg] = (ubyte)fulldecl->Interp.Location;
>           info->input_cylindrical_wrap[reg] = (ubyte)fulldecl->Interp.CylindricalWrap;
>
>           /* Vertex shaders can have inputs with holes between them. */
>           info->num_inputs = MAX2(info->num_inputs, reg + 1);
>
>           if (semName == TGSI_SEMANTIC_PRIMID)
>              info->uses_primid = TRUE;
>           else if (procType == PIPE_SHADER_FRAGMENT) {
>              if (semName == TGSI_SEMANTIC_POSITION)
>                 info->reads_position = TRUE;
>              else if (semName == TGSI_SEMANTIC_FACE)
>                 info->uses_frontface = TRUE;
>           }
> -      }
> -      else if (file == TGSI_FILE_SYSTEM_VALUE) {
> -         unsigned index = fulldecl->Range.First;
> +         break;
> +
> +      case TGSI_FILE_SYSTEM_VALUE:
> +         index = fulldecl->Range.First;
>
>           info->system_value_semantic_name[index] = semName;
>           info->num_system_values = MAX2(info->num_system_values, index + 1);
>
>           switch (semName) {
>           case TGSI_SEMANTIC_INSTANCEID:
>              info->uses_instanceid = TRUE;
>              break;
>           case TGSI_SEMANTIC_VERTEXID:
>              info->uses_vertexid = TRUE;
> @@ -523,22 +533,23 @@ scan_declaration(struct tgsi_shader_info *info,
>           case TGSI_SEMANTIC_POSITION:
>              info->reads_position = TRUE;
>              break;
>           case TGSI_SEMANTIC_FACE:
>              info->uses_frontface = TRUE;
>              break;
>           case TGSI_SEMANTIC_SAMPLEMASK:
>              info->reads_samplemask = TRUE;
>              break;
>           }
> -      }
> -      else if (file == TGSI_FILE_OUTPUT) {
> +         break;
> +
> +      case TGSI_FILE_OUTPUT:
>           info->output_semantic_name[reg] = (ubyte) semName;
>           info->output_semantic_index[reg] = (ubyte) semIndex;
>           info->num_outputs = MAX2(info->num_outputs, reg + 1);
>
>           if (semName == TGSI_SEMANTIC_COLOR)
>              info->colors_written |= 1 << semIndex;
>
>           if (procType == PIPE_SHADER_VERTEX ||
>               procType == PIPE_SHADER_GEOMETRY ||
>               procType == PIPE_SHADER_TESS_CTRL ||
> @@ -571,37 +582,42 @@ scan_declaration(struct tgsi_shader_info *info,
>                 info->writes_samplemask = TRUE;
>                 break;
>              }
>           }
>
>           if (procType == PIPE_SHADER_VERTEX) {
>              if (semName == TGSI_SEMANTIC_EDGEFLAG) {
>                 info->writes_edgeflag = TRUE;
>              }
>           }
> -      } else if (file == TGSI_FILE_SAMPLER) {
> +         break;
> +
> +      case TGSI_FILE_SAMPLER:
>           STATIC_ASSERT(sizeof(info->samplers_declared) * 8 >= PIPE_MAX_SAMPLERS);
>           info->samplers_declared |= 1u << reg;
> -      } else if (file == TGSI_FILE_SAMPLER_VIEW) {
> -         unsigned target = fulldecl->SamplerView.Resource;
> -         unsigned type = fulldecl->SamplerView.ReturnTypeX;
> +         break;
> +
> +      case TGSI_FILE_SAMPLER_VIEW:
> +         target = fulldecl->SamplerView.Resource;
> +         type = fulldecl->SamplerView.ReturnTypeX;
>
>           assert(target < TGSI_TEXTURE_UNKNOWN);
>           if (info->sampler_targets[reg] == TGSI_TEXTURE_UNKNOWN) {
>              /* Save sampler target for this sampler index */
>              info->sampler_targets[reg] = target;
>              info->sampler_type[reg] = type;
>           } else {
>              /* if previously declared, make sure targets agree */
>              assert(info->sampler_targets[reg] == target);
>              assert(info->sampler_type[reg] == type);
>           }
> +         break;
>        }
>     }
>  }
>
>
>  static void
>  scan_immediate(struct tgsi_shader_info *info)
>  {
>     uint reg = info->immediate_count++;
>     uint file = TGSI_FILE_IMMEDIATE;
>


More information about the mesa-dev mailing list