[Mesa-dev] [PATCH] mesa/glsl: stop using GL shader type internally

Timothy Arceri t_arceri at yahoo.com.au
Tue Jun 14 00:59:43 UTC 2016


Ping.

On Sun, 2016-06-05 at 13:17 +1000, Timothy Arceri wrote:
> Instead use the internal gl_shader_stage enum everywhere. This
> makes things more consistent and gets rid of unnecessary
> conversions.
> Ideally it would be nice to remove the Type field from gl_shader
> altogether but currently it is used to differentiate between
> gl_shader and gl_shader_program in the ShaderObjects hash table.
> ---
>  src/compiler/glsl/builtin_functions.cpp      |  2 +-
>  src/compiler/glsl/link_varyings.cpp          |  2 +-
>  src/compiler/glsl/linker.cpp                 |  2 +-
>  src/compiler/glsl/standalone.cpp             |  2 +-
>  src/compiler/glsl/standalone_scaffolding.cpp |  7 +++----
>  src/compiler/glsl/standalone_scaffolding.h   |  2 +-
>  src/mesa/drivers/common/meta.c               | 12 +++++------
>  src/mesa/drivers/common/meta.h               |  5 -----
>  src/mesa/drivers/dri/i965/brw_link.cpp       |  5 ++---
>  src/mesa/drivers/dri/i965/brw_shader.h       |  3 ++-
>  src/mesa/main/dd.h                           |  2 +-
>  src/mesa/main/ff_fragment_shader.cpp         |  2 +-
>  src/mesa/main/shaderapi.c                    | 20 ++++++++++------
> ---
>  src/mesa/main/shaderobj.c                    |  6 ++----
>  src/mesa/state_tracker/st_glsl_to_nir.cpp    |  6 +++---
>  src/mesa/state_tracker/st_glsl_to_tgsi.cpp   | 30 ++++++++++++++--
> ------------
>  16 files changed, 51 insertions(+), 57 deletions(-)
> 
> diff --git a/src/compiler/glsl/builtin_functions.cpp
> b/src/compiler/glsl/builtin_functions.cpp
> index edd02bb..87d22e6 100644
> --- a/src/compiler/glsl/builtin_functions.cpp
> +++ b/src/compiler/glsl/builtin_functions.cpp
> @@ -948,7 +948,7 @@ builtin_builder::create_shader()
>      * GLSL utility code that could be linked against any stage, so
> just
>      * arbitrarily pick GL_VERTEX_SHADER.
>      */
> -   shader = _mesa_new_shader(NULL, 0, GL_VERTEX_SHADER);
> +   shader = _mesa_new_shader(NULL, 0, MESA_SHADER_VERTEX);
>     shader->symbols = new(mem_ctx) glsl_symbol_table;
>  
>     gl_ModelViewProjectionMatrix =
> diff --git a/src/compiler/glsl/link_varyings.cpp
> b/src/compiler/glsl/link_varyings.cpp
> index a286e77..2ff7d80 100644
> --- a/src/compiler/glsl/link_varyings.cpp
> +++ b/src/compiler/glsl/link_varyings.cpp
> @@ -2111,7 +2111,7 @@ assign_varying_locations(struct gl_context
> *ctx,
>            * within a patch and can be used as shared memory.
>            */
>           if (input_var || (prog->SeparateShader && consumer == NULL)
> ||
> -             producer->Type == GL_TESS_CONTROL_SHADER) {
> +             producer->Stage == MESA_SHADER_TESS_CTRL) {
>              matches.record(output_var, input_var);
>           }
>  
> diff --git a/src/compiler/glsl/linker.cpp
> b/src/compiler/glsl/linker.cpp
> index 43719f4..e0e09e9 100644
> --- a/src/compiler/glsl/linker.cpp
> +++ b/src/compiler/glsl/linker.cpp
> @@ -2237,7 +2237,7 @@ link_intrastage_shaders(void *mem_ctx,
>        return NULL;
>     }
>  
> -   gl_shader *linked = ctx->Driver.NewShader(NULL, 0, main->Type);
> +   gl_shader *linked = ctx->Driver.NewShader(NULL, 0,
> shader_list[0]->Stage);
>     linked->ir = new(linked) exec_list;
>     clone_ir_list(mem_ctx, linked->ir, main->ir);
>  
> diff --git a/src/compiler/glsl/standalone.cpp
> b/src/compiler/glsl/standalone.cpp
> index c3afbc3..9b77b3a 100644
> --- a/src/compiler/glsl/standalone.cpp
> +++ b/src/compiler/glsl/standalone.cpp
> @@ -419,7 +419,7 @@ standalone_compile_shader(const struct
> standalone_options *_options,
>              continue;
>  
>           shader->Program = rzalloc(shader, gl_program);
> -         init_gl_program(shader->Program, shader->Type);
> +         init_gl_program(shader->Program, shader->Stage);
>        }
>     }
>  
> diff --git a/src/compiler/glsl/standalone_scaffolding.cpp
> b/src/compiler/glsl/standalone_scaffolding.cpp
> index e7f6555..94938ef 100644
> --- a/src/compiler/glsl/standalone_scaffolding.cpp
> +++ b/src/compiler/glsl/standalone_scaffolding.cpp
> @@ -76,17 +76,16 @@ _mesa_shader_debug(struct gl_context *, GLenum,
> GLuint *,
>  }
>  
>  struct gl_shader *
> -_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
> +_mesa_new_shader(struct gl_context *ctx, GLuint name,
> gl_shader_stage stage)
>  {
>     struct gl_shader *shader;
>  
>     (void) ctx;
>  
> -   assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
> +   assert(stage == MESA_SHADER_FRAGMENT || stage ==
> MESA_SHADER_VERTEX);
>     shader = rzalloc(NULL, struct gl_shader);
>     if (shader) {
> -      shader->Type = type;
> -      shader->Stage = _mesa_shader_enum_to_shader_stage(type);
> +      shader->Stage = stage;
>        shader->Name = name;
>        shader->RefCount = 1;
>     }
> diff --git a/src/compiler/glsl/standalone_scaffolding.h
> b/src/compiler/glsl/standalone_scaffolding.h
> index 6cef784..cfac883 100644
> --- a/src/compiler/glsl/standalone_scaffolding.h
> +++ b/src/compiler/glsl/standalone_scaffolding.h
> @@ -47,7 +47,7 @@ _mesa_reference_program_(struct gl_context *ctx,
> struct gl_program **ptr,
>                         struct gl_shader *sh);
>  
>  extern "C" struct gl_shader *
> -_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type);
> +_mesa_new_shader(struct gl_context *ctx, GLuint name,
> gl_shader_stage stage);
>  
>  extern "C" void
>  _mesa_delete_shader(struct gl_context *ctx, struct gl_shader *sh);
> diff --git a/src/mesa/drivers/common/meta.c
> b/src/mesa/drivers/common/meta.c
> index 6dcbc8b..3c86305 100644
> --- a/src/mesa/drivers/common/meta.c
> +++ b/src/mesa/drivers/common/meta.c
> @@ -121,14 +121,14 @@ _mesa_meta_framebuffer_texture_image(struct
> gl_context *ctx,
>                               level, layer, false, __func__);
>  }
>  
> -struct gl_shader *
> -_mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum
> target,
> -                                     const GLcharARB *source)
> +static struct gl_shader *
> +meta_compile_shader_with_debug(struct gl_context *ctx,
> gl_shader_stage stage,
> +                               const GLcharARB *source)
>  {
>     const GLuint name = ~0;
>     struct gl_shader *sh;
>  
> -   sh = ctx->Driver.NewShader(ctx, name, target);
> +   sh = ctx->Driver.NewShader(ctx, name, stage);
>     sh->Source = strdup(source);
>     sh->CompileStatus = false;
>     _mesa_compile_shader(ctx, sh);
> @@ -183,9 +183,9 @@ _mesa_meta_compile_and_link_program(struct
> gl_context *ctx,
>     sh_prog->NumShaders = 2;
>     sh_prog->Shaders = malloc(2 * sizeof(struct gl_shader *));
>     sh_prog->Shaders[0] =
> -      _mesa_meta_compile_shader_with_debug(ctx, GL_VERTEX_SHADER,
> vs_source);
> +      meta_compile_shader_with_debug(ctx, MESA_SHADER_VERTEX,
> vs_source);
>     sh_prog->Shaders[1] =
> -      _mesa_meta_compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER,
> fs_source);
> +      meta_compile_shader_with_debug(ctx, MESA_SHADER_FRAGMENT,
> fs_source);
>  
>     _mesa_meta_link_program_with_debug(ctx, sh_prog);
>  
> diff --git a/src/mesa/drivers/common/meta.h
> b/src/mesa/drivers/common/meta.h
> index 0a7321c..ba83a6d 100644
> --- a/src/mesa/drivers/common/meta.h
> +++ b/src/mesa/drivers/common/meta.h
> @@ -577,11 +577,6 @@ _mesa_meta_DrawTex(struct gl_context *ctx,
> GLfloat x, GLfloat y, GLfloat z,
>  void
>  _mesa_meta_drawbuffers_from_bitfield(GLbitfield bits);
>  
> -struct gl_shader *
> -_mesa_meta_compile_shader_with_debug(struct gl_context *ctx, GLenum
> target,
> -                                     const GLcharARB *source);
> -
> -
>  void
>  _mesa_meta_link_program_with_debug(struct gl_context *ctx,
>                                     struct gl_shader_program
> *sh_prog);
> diff --git a/src/mesa/drivers/dri/i965/brw_link.cpp
> b/src/mesa/drivers/dri/i965/brw_link.cpp
> index b340da3..c8bea97 100644
> --- a/src/mesa/drivers/dri/i965/brw_link.cpp
> +++ b/src/mesa/drivers/dri/i965/brw_link.cpp
> @@ -189,14 +189,13 @@ process_glsl_ir(gl_shader_stage stage,
>  }
>  
>  extern "C" struct gl_shader *
> -brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
> +brw_new_shader(struct gl_context *ctx, GLuint name, gl_shader_stage
> stage)
>  {
>     struct brw_shader *shader;
>  
>     shader = rzalloc(NULL, struct brw_shader);
>     if (shader) {
> -      shader->base.Type = type;
> -      shader->base.Stage = _mesa_shader_enum_to_shader_stage(type);
> +      shader->base.Stage = stage;
>        shader->base.Name = name;
>        _mesa_init_shader(ctx, &shader->base);
>     }
> diff --git a/src/mesa/drivers/dri/i965/brw_shader.h
> b/src/mesa/drivers/dri/i965/brw_shader.h
> index 4da1364..4096791 100644
> --- a/src/mesa/drivers/dri/i965/brw_shader.h
> +++ b/src/mesa/drivers/dri/i965/brw_shader.h
> @@ -291,7 +291,8 @@ bool brw_cs_precompile(struct gl_context *ctx,
>                         struct gl_program *prog);
>  
>  GLboolean brw_link_shader(struct gl_context *ctx, struct
> gl_shader_program *prog);
> -struct gl_shader *brw_new_shader(struct gl_context *ctx, GLuint
> name, GLuint type);
> +struct gl_shader *brw_new_shader(struct gl_context *ctx, GLuint
> name,
> +                                 gl_shader_stage stage);
>  
>  int type_size_scalar(const struct glsl_type *type);
>  int type_size_vec4(const struct glsl_type *type);
> diff --git a/src/mesa/main/dd.h b/src/mesa/main/dd.h
> index 5710a34..bf0c6e1 100644
> --- a/src/mesa/main/dd.h
> +++ b/src/mesa/main/dd.h
> @@ -785,7 +785,7 @@ struct dd_function_table {
>      */
>     /*@{*/
>     struct gl_shader *(*NewShader)(struct gl_context *ctx,
> -                                  GLuint name, GLenum type);
> +                                  GLuint name, gl_shader_stage
> stage);
>     void (*UseProgram)(struct gl_context *ctx, struct
> gl_shader_program *shProg);
>     /*@}*/
>  
> diff --git a/src/mesa/main/ff_fragment_shader.cpp
> b/src/mesa/main/ff_fragment_shader.cpp
> index 4967eb3..d1c7444 100644
> --- a/src/mesa/main/ff_fragment_shader.cpp
> +++ b/src/mesa/main/ff_fragment_shader.cpp
> @@ -1199,7 +1199,7 @@ create_new_program(struct gl_context *ctx,
> struct state_key *key)
>     _mesa_glsl_parse_state *state;
>  
>     p.mem_ctx = ralloc_context(NULL);
> -   p.shader = ctx->Driver.NewShader(ctx, 0, GL_FRAGMENT_SHADER);
> +   p.shader = ctx->Driver.NewShader(ctx, 0, MESA_SHADER_FRAGMENT);
>     p.shader->ir = new(p.shader) exec_list;
>     state = new(p.shader) _mesa_glsl_parse_state(ctx,
> MESA_SHADER_FRAGMENT,
>  						p.shader);
> diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
> index 9351976..51f46b9 100644
> --- a/src/mesa/main/shaderapi.c
> +++ b/src/mesa/main/shaderapi.c
> @@ -264,7 +264,7 @@ attach_shader(struct gl_context *ctx, GLuint
> program, GLuint shader)
>           _mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
>           return;
>        } else if (same_type_disallowed &&
> -                 shProg->Shaders[i]->Type == sh->Type) {
> +                 shProg->Shaders[i]->Stage == sh->Stage) {
>          /* Shader with the same type is already attached to this
> program,
>           * OpenGL ES 2.0 and 3.0 specs say:
>           *
> @@ -307,7 +307,9 @@ create_shader(struct gl_context *ctx, GLenum
> type)
>  
>     _mesa_HashLockMutex(ctx->Shared->ShaderObjects);
>     name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
> -   sh = ctx->Driver.NewShader(ctx, name, type);
> +   sh = ctx->Driver.NewShader(ctx, name,
> +                              _mesa_shader_enum_to_shader_stage(type
> ));
> +   sh->Type = type;
>     _mesa_HashInsertLocked(ctx->Shared->ShaderObjects, name, sh);
>     _mesa_HashUnlockMutex(ctx->Shared->ShaderObjects);
>  
> @@ -429,11 +431,11 @@ detach_shader(struct gl_context *ctx, GLuint
> program, GLuint shader)
>  #ifdef DEBUG
>           /* sanity check - make sure the new list's entries are
> sensible */
>           for (j = 0; j < shProg->NumShaders; j++) {
> -            assert(shProg->Shaders[j]->Type == GL_VERTEX_SHADER ||
> -                   shProg->Shaders[j]->Type ==
> GL_TESS_CONTROL_SHADER ||
> -                   shProg->Shaders[j]->Type ==
> GL_TESS_EVALUATION_SHADER ||
> -                   shProg->Shaders[j]->Type == GL_GEOMETRY_SHADER ||
> -                   shProg->Shaders[j]->Type == GL_FRAGMENT_SHADER);
> +            assert(shProg->Shaders[j]->Stage == MESA_SHADER_VERTEX
> ||
> +                   shProg->Shaders[j]->Stage ==
> MESA_SHADER_TESS_CTRL ||
> +                   shProg->Shaders[j]->Stage ==
> MESA_SHADER_TESS_EVAL ||
> +                   shProg->Shaders[j]->Stage == MESA_SHADER_GEOMETRY
> ||
> +                   shProg->Shaders[j]->Stage ==
> MESA_SHADER_FRAGMENT);
>              assert(shProg->Shaders[j]->RefCount > 0);
>           }
>  #endif
> @@ -1065,9 +1067,9 @@ _mesa_link_program(struct gl_context *ctx,
> struct gl_shader_program *shProg)
>                     shProg->LinkStatus ? "Success" : "Failed");
>  
>        for (i = 0; i < shProg->NumShaders; i++) {
> -         printf(" shader %u, type 0x%x\n",
> +         printf(" shader %u, stage %u\n",
>                        shProg->Shaders[i]->Name,
> -                      shProg->Shaders[i]->Type);
> +                      shProg->Shaders[i]->Stage);
>        }
>     }
>  }
> diff --git a/src/mesa/main/shaderobj.c b/src/mesa/main/shaderobj.c
> index fc1e3e3..030c84f 100644
> --- a/src/mesa/main/shaderobj.c
> +++ b/src/mesa/main/shaderobj.c
> @@ -99,14 +99,12 @@ _mesa_init_shader(struct gl_context *ctx, struct
> gl_shader *shader)
>   * Called via ctx->Driver.NewShader()
>   */
>  struct gl_shader *
> -_mesa_new_shader(struct gl_context *ctx, GLuint name, GLenum type)
> +_mesa_new_shader(struct gl_context *ctx, GLuint name,
> gl_shader_stage stage)
>  {
>     struct gl_shader *shader;
> -   assert(_mesa_validate_shader_target(ctx, type));
>     shader = rzalloc(NULL, struct gl_shader);
>     if (shader) {
> -      shader->Type = type;
> -      shader->Stage = _mesa_shader_enum_to_shader_stage(type);
> +      shader->Stage = stage;
>        shader->Name = name;
>        _mesa_init_shader(ctx, shader);
>     }
> diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp
> b/src/mesa/state_tracker/st_glsl_to_nir.cpp
> index 98c075f..a880564 100644
> --- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
> +++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
> @@ -422,12 +422,12 @@ st_nir_get_mesa_program(struct gl_context *ctx,
>     struct st_vertex_program *stvp;
>     struct st_fragment_program *stfp;
>  
> -   switch (shader->Type) {
> -   case GL_VERTEX_SHADER:
> +   switch (shader->Stage) {
> +   case MESA_SHADER_VERTEX:
>        stvp = (struct st_vertex_program *)prog;
>        stvp->shader_program = shader_program;
>        break;
> -   case GL_FRAGMENT_SHADER:
> +   case MESA_SHADER_FRAGMENT:
>        stfp = (struct st_fragment_program *)prog;
>        stfp->shader_program = shader_program;
>        break;
> diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> index 48bc1b1..9349e39 100644
> --- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> +++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
> @@ -6449,7 +6449,7 @@ get_mesa_program_tgsi(struct gl_context *ctx,
>     GLenum target = _mesa_shader_stage_to_program(shader->Stage);
>     bool progress;
>     struct gl_shader_compiler_options *options =
> -         &ctx-
> >Const.ShaderCompilerOptions[_mesa_shader_enum_to_shader_stage(shader
> ->Type)];
> +         &ctx->Const.ShaderCompilerOptions[shader->Stage];
>     struct pipe_screen *pscreen = ctx->st->pipe->screen;
>     unsigned ptarget = st_shader_stage_to_ptarget(shader->Stage);
>  
> @@ -6544,8 +6544,8 @@ get_mesa_program_tgsi(struct gl_context *ctx,
>     /* Perform optimizations on the instructions in the
> glsl_to_tgsi_visitor. */
>     v->simplify_cmp();
>  
> -   if (shader->Type != GL_TESS_CONTROL_SHADER &&
> -       shader->Type != GL_TESS_EVALUATION_SHADER)
> +   if (shader->Stage != MESA_SHADER_TESS_CTRL &&
> +       shader->Stage != MESA_SHADER_TESS_EVAL)
>        v->copy_propagate();
>  
>     while (v->eliminate_dead_code());
> @@ -6581,7 +6581,7 @@ get_mesa_program_tgsi(struct gl_context *ctx,
>     shader->ir = NULL;
>  
>     /* This must be done before the uniform storage is associated. */
> -   if (shader->Type == GL_FRAGMENT_SHADER &&
> +   if (shader->Stage == MESA_SHADER_FRAGMENT &&
>         (prog->InputsRead & VARYING_BIT_POS ||
>          prog->SystemValuesRead & (1 << SYSTEM_VALUE_FRAG_COORD))) {
>        static const gl_state_index wposTransformState[STATE_LENGTH] =
> {
> @@ -6617,28 +6617,28 @@ get_mesa_program_tgsi(struct gl_context *ctx,
>     struct st_tesseval_program *sttep;
>     struct st_compute_program *stcp;
>  
> -   switch (shader->Type) {
> -   case GL_VERTEX_SHADER:
> +   switch (shader->Stage) {
> +   case MESA_SHADER_VERTEX:
>        stvp = (struct st_vertex_program *)prog;
>        stvp->glsl_to_tgsi = v;
>        break;
> -   case GL_FRAGMENT_SHADER:
> +   case MESA_SHADER_FRAGMENT:
>        stfp = (struct st_fragment_program *)prog;
>        stfp->glsl_to_tgsi = v;
>        break;
> -   case GL_GEOMETRY_SHADER:
> +   case MESA_SHADER_GEOMETRY:
>        stgp = (struct st_geometry_program *)prog;
>        stgp->glsl_to_tgsi = v;
>        break;
> -   case GL_TESS_CONTROL_SHADER:
> +   case MESA_SHADER_TESS_CTRL:
>        sttcp = (struct st_tessctrl_program *)prog;
>        sttcp->glsl_to_tgsi = v;
>        break;
> -   case GL_TESS_EVALUATION_SHADER:
> +   case MESA_SHADER_TESS_EVAL:
>        sttep = (struct st_tesseval_program *)prog;
>        sttep->glsl_to_tgsi = v;
>        break;
> -   case GL_COMPUTE_SHADER:
> +   case MESA_SHADER_COMPUTE:
>        stcp = (struct st_compute_program *)prog;
>        stcp->glsl_to_tgsi = v;
>        break;
> @@ -6661,9 +6661,9 @@ get_mesa_program(struct gl_context *ctx,
>        pscreen->get_shader_param(pscreen, ptarget,
> PIPE_SHADER_CAP_PREFERRED_IR);
>     if (preferred_ir == PIPE_SHADER_IR_NIR) {
>        /* TODO only for GLSL VS/FS for now: */
> -      switch (shader->Type) {
> -      case GL_VERTEX_SHADER:
> -      case GL_FRAGMENT_SHADER:
> +      switch (shader->Stage) {
> +      case MESA_SHADER_VERTEX:
> +      case MESA_SHADER_FRAGMENT:
>           return st_nir_get_mesa_program(ctx, shader_program,
> shader);
>        default:
>           break;
> @@ -6759,7 +6759,7 @@ st_link_shader(struct gl_context *ctx, struct
> gl_shader_program *prog,
>  
>        bool progress;
>        exec_list *ir = prog->_LinkedShaders[i]->ir;
> -      gl_shader_stage stage =
> _mesa_shader_enum_to_shader_stage(prog->_LinkedShaders[i]->Type);
> +      gl_shader_stage stage = prog->_LinkedShaders[i]->Stage;
>        const struct gl_shader_compiler_options *options =
>              &ctx->Const.ShaderCompilerOptions[stage];
>        unsigned ptarget = st_shader_stage_to_ptarget(stage);


More information about the mesa-dev mailing list