glsl: Defer initialization of built-in functions until they're needed.
authorKenneth Graunke <kenneth@whitecape.org>
Tue, 20 Sep 2011 01:30:15 +0000 (18:30 -0700)
committerKenneth Graunke <kenneth@whitecape.org>
Sat, 24 Sep 2011 00:12:47 +0000 (17:12 -0700)
Very simple shaders don't actually use GLSL built-ins.  For example:
- gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
- gl_FragColor = vec4(0.0);
Both of the shaders used by _mesa_meta_glsl_Clear() also qualify.

By waiting to initialize the built-ins until the first time we need to
look for a signature, we can avoid the overhead entirely in these cases.

Makes piglit run roughly 18% faster (255 vs. 312 seconds).

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Eric Anholt <eric@anholt.net>
src/glsl/ast_function.cpp
src/glsl/ast_to_hir.cpp
src/glsl/builtins/tools/generate_builtins.py
src/glsl/glsl_parser_extras.cpp

index ca45934a478ec311ab740545ff3bd3a6a51f754e..fc0d7497d5383cedb0768d732d4345fd5108661c 100644 (file)
@@ -117,6 +117,7 @@ match_function_by_name(exec_list *instructions, const char *name,
       /* The current shader doesn't contain a matching function or signature.
        * Before giving up, look for the prototype in the built-in functions.
        */
+      _mesa_glsl_initialize_functions(state);
       for (unsigned i = 0; i < state->num_builtins_to_link; i++) {
         ir_function *builtin;
         builtin = state->builtins_to_link[i]->symbols->get_function(name);
index ce29d5a87aa0ed1842e75dd1b346bd415e0cfbce..91a2231605d1468ee218fe4938de910cf7277214 100644 (file)
@@ -60,7 +60,6 @@ void
 _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
 {
    _mesa_glsl_initialize_variables(instructions, state);
-   _mesa_glsl_initialize_functions(state);
 
    state->symbols->language_version = state->language_version;
 
index 17d528c2180992ce8a054c5567b8b55f788adb22..8ce2b70c500c2b5b98fe2e2cc5c659029c1cc038 100755 (executable)
@@ -228,12 +228,14 @@ _mesa_read_profile(struct _mesa_glsl_parse_state *state,
 void
 _mesa_glsl_initialize_functions(struct _mesa_glsl_parse_state *state)
 {
+   /* If we've already initialized the built-ins, bail early. */
+   if (state->num_builtins_to_link > 0)
+      return;
+
    if (builtin_mem_ctx == NULL) {
       builtin_mem_ctx = ralloc_context(NULL); // "GLSL built-in functions"
       memset(&builtin_profiles, 0, sizeof(builtin_profiles));
    }
-
-   state->num_builtins_to_link = 0;
 """
 
     i = 0
index 8faddc578d477f2feedfaadec89b6a8b8bef4d4b..a9075b2b1be02dc3a4c188b558a5988d3bb37acc 100644 (file)
@@ -52,6 +52,8 @@ _mesa_glsl_parse_state::_mesa_glsl_parse_state(struct gl_context *ctx,
    this->error = false;
    this->loop_or_switch_nesting = NULL;
 
+   this->num_builtins_to_link = 0;
+
    /* Set default language version and extensions */
    this->language_version = 110;
    this->es_shader = false;