[Mesa-dev] [PATCH 4/6] mesa: Use shared code for converting shader targets to short strings.

Eric Anholt eric at anholt.net
Mon Jun 17 16:10:48 PDT 2013


We were duplicating this code all over the place, and they all would need
updating for the next set of shader targets.
---
 src/glsl/glsl_parser_extras.cpp            | 35 ++++++++++++++++++++++++++++++
 src/glsl/glsl_parser_extras.h              |  3 +++
 src/glsl/link_varyings.cpp                 | 15 ++++++++-----
 src/glsl/linker.cpp                        |  4 ++--
 src/mesa/drivers/dri/i965/brw_shader.cpp   |  9 ++++----
 src/mesa/main/shaderapi.c                  | 17 ++-------------
 src/mesa/main/uniform_query.cpp            |  9 ++------
 src/mesa/program/ir_to_mesa.cpp            |  5 +----
 src/mesa/state_tracker/st_glsl_to_tgsi.cpp |  7 ++----
 9 files changed, 61 insertions(+), 43 deletions(-)

diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp
index 9862714..7b827ba 100644
--- a/src/glsl/glsl_parser_extras.cpp
+++ b/src/glsl/glsl_parser_extras.cpp
@@ -302,6 +302,41 @@ _mesa_glsl_parse_state::process_version_directive(YYLTYPE *locp, int version,
    }
 }
 
+extern "C" {
+
+/**
+ * The most common use of _mesa_glsl_shader_target_name(), which is
+ * shared with C code in Mesa core to translate a GLenum to a short
+ * shader stage name in debug printouts.
+ *
+ * It recognizes the PROGRAM variants of the names so it can be used
+ * with a struct gl_program->Target, not just a struct
+ * gl_shader->Type.
+ */
+const char *
+_mesa_glsl_shader_target_name(GLenum type)
+{
+   switch (type) {
+   case GL_VERTEX_SHADER:
+   case GL_VERTEX_PROGRAM_ARB:
+      return "vertex";
+   case GL_FRAGMENT_SHADER:
+   case GL_FRAGMENT_PROGRAM_ARB:
+      return "fragment";
+   case GL_GEOMETRY_SHADER:
+      return "geometry";
+   default:
+      assert(!"Should not get here.");
+      return "unknown";
+   }
+}
+
+} /* extern "C" */
+
+/**
+ * Overloaded C++ variant usable within the compiler for translating
+ * our internal enum into short stage names.
+ */
 const char *
 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target)
 {
diff --git a/src/glsl/glsl_parser_extras.h b/src/glsl/glsl_parser_extras.h
index 95918de..7f478df 100644
--- a/src/glsl/glsl_parser_extras.h
+++ b/src/glsl/glsl_parser_extras.h
@@ -371,6 +371,9 @@ _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
 extern "C" {
 #endif
 
+extern const char *
+_mesa_glsl_shader_target_name(GLenum type);
+
 extern int glcpp_preprocess(void *ctx, const char **shader, char **info_log,
                       const struct gl_extensions *extensions, struct gl_context *gl_ctx);
 
diff --git a/src/glsl/link_varyings.cpp b/src/glsl/link_varyings.cpp
index 34e3440..4fdbdc1 100644
--- a/src/glsl/link_varyings.cpp
+++ b/src/glsl/link_varyings.cpp
@@ -31,6 +31,7 @@
 
 #include "main/mtypes.h"
 #include "glsl_symbol_table.h"
+#include "glsl_parser_extras.h"
 #include "ir_optimization.h"
 #include "linker.h"
 #include "link_varyings.h"
@@ -47,9 +48,10 @@ cross_validate_outputs_to_inputs(struct gl_shader_program *prog,
 				 gl_shader *producer, gl_shader *consumer)
 {
    glsl_symbol_table parameters;
-   /* FINISHME: Figure these out dynamically. */
-   const char *const producer_stage = "vertex";
-   const char *const consumer_stage = "fragment";
+   const char *const producer_stage =
+      _mesa_glsl_shader_target_name(producer->Type);
+   const char *const consumer_stage =
+      _mesa_glsl_shader_target_name(consumer->Type);
 
    /* Find all shader outputs in the "producer" stage.
     */
@@ -1135,8 +1137,11 @@ assign_varying_locations(struct gl_context *ctx,
                 * "glsl1-varying read but not written" in piglit.
                 */
 
-               linker_error(prog, "fragment shader varying %s not written "
-                            "by vertex shader\n.", var->name);
+               linker_error(prog, "%s shader varying %s not written "
+                            "by %s shader\n.",
+                            _mesa_glsl_shader_target_name(consumer->Type),
+			    var->name,
+                            _mesa_glsl_shader_target_name(producer->Type));
             }
 
             /* An 'in' variable is only really a shader input if its
diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp
index cd8d680..c168e47 100644
--- a/src/glsl/linker.cpp
+++ b/src/glsl/linker.cpp
@@ -66,6 +66,7 @@
 
 #include "main/core.h"
 #include "glsl_symbol_table.h"
+#include "glsl_parser_extras.h"
 #include "ir.h"
 #include "program.h"
 #include "program/hash_table.h"
@@ -1009,8 +1010,7 @@ link_intrastage_shaders(void *mem_ctx,
 
    if (main == NULL) {
       linker_error(prog, "%s shader lacks `main'\n",
-		   (shader_list[0]->Type == GL_VERTEX_SHADER)
-		   ? "vertex" : "fragment");
+		   _mesa_glsl_shader_target_name(shader_list[0]->Type));
       return NULL;
    }
 
diff --git a/src/mesa/drivers/dri/i965/brw_shader.cpp b/src/mesa/drivers/dri/i965/brw_shader.cpp
index 65df931..12986cc 100644
--- a/src/mesa/drivers/dri/i965/brw_shader.cpp
+++ b/src/mesa/drivers/dri/i965/brw_shader.cpp
@@ -28,6 +28,7 @@ extern "C" {
 }
 #include "brw_fs.h"
 #include "glsl/ir_optimization.h"
+#include "glsl/glsl_parser_extras.h"
 
 struct gl_shader *
 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
@@ -112,8 +113,6 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
    struct brw_context *brw = brw_context(ctx);
    struct intel_context *intel = &brw->intel;
    unsigned int stage;
-   static const char *target_strings[]
-      = { "vertex", "fragment", "geometry" };
 
    for (stage = 0; stage < ARRAY_SIZE(shProg->_LinkedShaders); stage++) {
       struct brw_shader *shader =
@@ -262,8 +261,8 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
 
       if (ctx->Shader.Flags & GLSL_DUMP) {
          printf("\n");
-         printf("GLSL IR for linked %s program %d:\n", target_strings[stage],
-                shProg->Name);
+         printf("GLSL IR for linked %s program %d:\n",
+                _mesa_glsl_shader_target_name(shader->base.Type), shProg->Name);
          _mesa_print_ir(shader->base.ir, NULL);
          printf("\n");
       }
@@ -276,7 +275,7 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
             continue;
 
          printf("GLSL %s shader %d source for linked program %d:\n",
-                target_strings[_mesa_shader_type_to_index(sh->Type)],
+                _mesa_glsl_shader_target_name(sh->Type),
                 i,
                 shProg->Name);
          printf("%s", sh->Source);
diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
index 8cb0276..88518ca 100644
--- a/src/mesa/main/shaderapi.c
+++ b/src/mesa/main/shaderapi.c
@@ -816,21 +816,8 @@ print_shader_info(const struct gl_shader_program *shProg)
 
    printf("Mesa: glUseProgram(%u)\n", shProg->Name);
    for (i = 0; i < shProg->NumShaders; i++) {
-      const char *s;
-      switch (shProg->Shaders[i]->Type) {
-      case GL_VERTEX_SHADER:
-         s = "vertex";
-         break;
-      case GL_FRAGMENT_SHADER:
-         s = "fragment";
-         break;
-      case GL_GEOMETRY_SHADER:
-         s = "geometry";
-         break;
-      default:
-         s = "";
-      }
-      printf("  %s shader %u, checksum %u\n", s, 
+      printf("  %s shader %u, checksum %u\n",
+             _mesa_glsl_shader_target_name(shProg->Shaders[i]->Type),
 	     shProg->Shaders[i]->Name,
 	     shProg->Shaders[i]->SourceChecksum);
    }
diff --git a/src/mesa/main/uniform_query.cpp b/src/mesa/main/uniform_query.cpp
index 296f80f..3c46004 100644
--- a/src/mesa/main/uniform_query.cpp
+++ b/src/mesa/main/uniform_query.cpp
@@ -33,6 +33,7 @@
 #include "program/hash_table.h"
 #include "../glsl/program.h"
 #include "../glsl/ir_uniform.h"
+#include "../glsl/glsl_parser_extras.h"
 #include "main/shaderapi.h"
 #include "main/shaderobj.h"
 #include "uniforms.h"
@@ -434,12 +435,6 @@ log_uniform(const void *values, enum glsl_base_type basicType,
 static void
 log_program_parameters(const struct gl_shader_program *shProg)
 {
-   static const char *stages[] = {
-      "vertex", "fragment", "geometry"
-   };
-
-   assert(Elements(stages) == MESA_SHADER_TYPES);
-
    for (unsigned i = 0; i < MESA_SHADER_TYPES; i++) {
       if (shProg->_LinkedShaders[i] == NULL)
 	 continue;
@@ -447,7 +442,7 @@ log_program_parameters(const struct gl_shader_program *shProg)
       const struct gl_program *const prog = shProg->_LinkedShaders[i]->Program;
 
       printf("Program %d %s shader parameters:\n",
-	     shProg->Name, stages[i]);
+             shProg->Name, _mesa_glsl_shader_target_name(prog->Target));
       for (unsigned j = 0; j < prog->Parameters->NumParameters; j++) {
 	 printf("%s: %p %f %f %f %f\n",
 		prog->Parameters->Parameters[j].Name,
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index 191e291..2739df7 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -2810,22 +2810,19 @@ get_mesa_program(struct gl_context *ctx,
    int i;
    struct gl_program *prog;
    GLenum target;
-   const char *target_string;
+   const char *target_string = _mesa_glsl_shader_target_name(shader->Type);
    struct gl_shader_compiler_options *options =
          &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
 
    switch (shader->Type) {
    case GL_VERTEX_SHADER:
       target = GL_VERTEX_PROGRAM_ARB;
-      target_string = "vertex";
       break;
    case GL_FRAGMENT_SHADER:
       target = GL_FRAGMENT_PROGRAM_ARB;
-      target_string = "fragment";
       break;
    case GL_GEOMETRY_SHADER:
       target = GL_GEOMETRY_PROGRAM_NV;
-      target_string = "geometry";
       break;
    default:
       assert(!"should not be reached");
diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
index 4a68882..32bc2b3 100644
--- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
@@ -4994,7 +4994,6 @@ get_mesa_program(struct gl_context *ctx,
    glsl_to_tgsi_visitor* v;
    struct gl_program *prog;
    GLenum target;
-   const char *target_string;
    bool progress;
    struct gl_shader_compiler_options *options =
          &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
@@ -5005,17 +5004,14 @@ get_mesa_program(struct gl_context *ctx,
    case GL_VERTEX_SHADER:
       target = GL_VERTEX_PROGRAM_ARB;
       ptarget = PIPE_SHADER_VERTEX;
-      target_string = "vertex";
       break;
    case GL_FRAGMENT_SHADER:
       target = GL_FRAGMENT_PROGRAM_ARB;
       ptarget = PIPE_SHADER_FRAGMENT;
-      target_string = "fragment";
       break;
    case GL_GEOMETRY_SHADER:
       target = GL_GEOMETRY_PROGRAM_NV;
       ptarget = PIPE_SHADER_GEOMETRY;
-      target_string = "geometry";
       break;
    default:
       assert(!"should not be reached");
@@ -5105,7 +5101,8 @@ get_mesa_program(struct gl_context *ctx,
 
    if (ctx->Shader.Flags & GLSL_DUMP) {
       printf("\n");
-      printf("GLSL IR for linked %s program %d:\n", target_string,
+      printf("GLSL IR for linked %s program %d:\n",
+             _mesa_glsl_shader_target_name(shader->Type),
              shader_program->Name);
       _mesa_print_ir(shader->ir, NULL);
       printf("\n");
-- 
1.8.3.rc0



More information about the mesa-dev mailing list