[Mesa-dev] [PATCH 4/4] mesa: Dynamically allocate the storage for program local parameters.

Eric Anholt eric at anholt.net
Wed Nov 13 15:11:53 PST 2013


The array was 64kb per struct gl_program, plus we statically stored a copy
of one on disk for _mesa_DummyProgram.  Given that most struct gl_programs
we generate are for GLSL shaders that don't have local parameters, this
was a waste.

Since you can store and fetch parameters beyond what the program actually
uses, we do have to do a late allocation if necessary at
GetProgramLocalParameter time.

Reduces peak memory usage in the dota2 trace I made by 76MB (4.5%)
---
 src/mesa/main/arbprogram.c       |  6 ++++++
 src/mesa/main/mtypes.h           | 11 +++++++++--
 src/mesa/program/program.c       | 12 +++++++++++-
 src/mesa/program/program_parse.y |  7 +++++++
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/mesa/main/arbprogram.c b/src/mesa/main/arbprogram.c
index 51a2993..8bd3f0b 100644
--- a/src/mesa/main/arbprogram.c
+++ b/src/mesa/main/arbprogram.c
@@ -265,6 +265,12 @@ get_local_param_pointer(struct gl_context *ctx, const char *func,
       return GL_FALSE;
    }
 
+   if (!prog->LocalParams) {
+      prog->LocalParams = calloc(maxParams, sizeof(float[4]));
+      if (!prog->LocalParams)
+         return GL_FALSE;
+   }
+
    *param = prog->LocalParams[index];
    return GL_TRUE;
 }
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 0f470da..67c4996 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -2035,8 +2035,15 @@ struct gl_program
 
    /** Named parameters, constants, etc. from program text */
    struct gl_program_parameter_list *Parameters;
-   /** Numbered local parameters */
-   GLfloat LocalParams[MAX_PROGRAM_LOCAL_PARAMS][4];
+
+   /**
+    * Local parameters used by the program.
+    *
+    * It's dynamically allocated because it is rarely used (just
+    * assembly-style programs), and MAX_PROGRAM_LOCAL_PARAMS entries once it's
+    * allocated.
+    */
+   GLfloat (*LocalParams)[4];
 
    /** Map from sampler unit to texture unit (set by glUniform1i()) */
    GLubyte SamplerUnits[MAX_SAMPLERS];
diff --git a/src/mesa/program/program.c b/src/mesa/program/program.c
index b7b5140..01f8c6f 100644
--- a/src/mesa/program/program.c
+++ b/src/mesa/program/program.c
@@ -349,6 +349,7 @@ _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
       return;
 
    free(prog->String);
+   free(prog->LocalParams);
 
    if (prog->Instructions) {
       _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
@@ -477,7 +478,16 @@ _mesa_clone_program(struct gl_context *ctx, const struct gl_program *prog)
 
    if (prog->Parameters)
       clone->Parameters = _mesa_clone_parameter_list(prog->Parameters);
-   memcpy(clone->LocalParams, prog->LocalParams, sizeof(clone->LocalParams));
+   if (prog->LocalParams) {
+      clone->LocalParams = malloc(MAX_PROGRAM_LOCAL_PARAMS *
+                                  sizeof(float[4]));
+      if (!clone->LocalParams) {
+         _mesa_reference_program(ctx, &clone, NULL);
+         return NULL;
+      }
+      memcpy(clone->LocalParams, prog->LocalParams,
+             MAX_PROGRAM_LOCAL_PARAMS * sizeof(float[4]));
+   }
    clone->IndirectRegisterFiles = prog->IndirectRegisterFiles;
    clone->NumInstructions = prog->NumInstructions;
    clone->NumTemporaries = prog->NumTemporaries;
diff --git a/src/mesa/program/program_parse.y b/src/mesa/program/program_parse.y
index a76db4e..03c0a3d 100644
--- a/src/mesa/program/program_parse.y
+++ b/src/mesa/program/program_parse.y
@@ -25,6 +25,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "main/macros.h"
 #include "main/mtypes.h"
 #include "main/imports.h"
 #include "program/program.h"
@@ -2559,6 +2560,12 @@ initialize_symbol_from_param(struct gl_program *prog,
    param_var->type = at_param;
    param_var->param_binding_type = PROGRAM_STATE_VAR;
 
+   /* Dynamically allocate LocalParams, since it's a large array to have
+    * statically in every gl_program otherwise.
+    */
+   if (state_tokens[1] == STATE_LOCAL && !prog->LocalParams)
+      prog->LocalParams = calloc(MAX_PROGRAM_LOCAL_PARAMS, sizeof(float[4]));
+
    /* If we are adding a STATE_ENV or STATE_LOCAL that has multiple elements,
     * we need to unroll it and call add_state_reference() for each row
     */
-- 
1.8.4.rc3



More information about the mesa-dev mailing list