Chris Wilson wrote:
Only use the active part of the state key and ignore all the texture units
beyond Const.MaxTextureUnits. For instance on my i915, this reduces
sizeof(struct state_key) from 196 to 100 bytes.

Actually, we can do better than that. If we use key->nr_enabled_units instead of ctx->Const.MaxTextureUnits we can ignore the disabled texture units. When no textures are enabled, the key size is only 4 bytes. With unit[0] enabled, the key size is 16 bytes.

Here's the patch. I'll commit it later after glean/etc testing if there's no concerns.

-Brian
diff --git a/src/mesa/main/texenvprogram.c b/src/mesa/main/texenvprogram.c
index d5343d4..fdbc14c 100644
--- a/src/mesa/main/texenvprogram.c
+++ b/src/mesa/main/texenvprogram.c
@@ -99,6 +99,7 @@ struct state_key {
    GLuint fog_mode:2;          /**< FOG_x */
    GLuint inputs_available:12;
 
+   /* NOTE: This array of structs must be last! (see "keySize" below) */
    struct {
       GLuint enabled:1;
       GLuint source_index:3;   /**< TEXTURE_x_INDEX */
@@ -368,11 +369,12 @@ static GLbitfield get_fp_input_mask( GLcontext *ctx )
  * Examine current texture environment state and generate a unique
  * key to identify it.
  */
-static void make_state_key( GLcontext *ctx,  struct state_key *key )
+static GLuint make_state_key( GLcontext *ctx,  struct state_key *key )
 {
    GLuint i, j;
    GLbitfield inputs_referenced = FRAG_BIT_COL0;
    const GLbitfield inputs_available = get_fp_input_mask( ctx );
+   GLuint keySize;
 
    memset(key, 0, sizeof(*key));
 
@@ -390,7 +392,7 @@ static void make_state_key( GLcontext *ctx,  struct 
state_key *key )
 
       key->unit[i].enabled = 1;
       key->enabled_units |= (1<<i);
-      key->nr_enabled_units = i+1;
+      key->nr_enabled_units = i + 1;
       inputs_referenced |= FRAG_BIT_TEX(i);
 
       key->unit[i].source_index =
@@ -443,8 +445,15 @@ static void make_state_key( GLcontext *ctx,  struct 
state_key *key )
    }
 
    key->inputs_available = (inputs_available & inputs_referenced);
+
+   /* compute size of state key, ignoring unused texture units */
+   keySize = sizeof(*key) - sizeof(key->unit)
+      + key->nr_enabled_units * sizeof(key->unit[0]);
+
+   return keySize;
 }
 
+
 /**
  * Use uregs to represent registers internally, translate to Mesa's
  * expected formats on emit.  
@@ -1390,7 +1399,7 @@ create_new_program(GLcontext *ctx, struct state_key *key,
       GLboolean needbumpstage = GL_FALSE;
 
       /* Zeroth pass - bump map textures first */
-      for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
+      for (unit = 0; unit < key->nr_enabled_units; unit++)
         if (key->unit[unit].enabled &&
              key->unit[unit].ModeRGB == MODE_BUMP_ENVMAP_ATI) {
            needbumpstage = GL_TRUE;
@@ -1403,7 +1412,7 @@ create_new_program(GLcontext *ctx, struct state_key *key,
        * all referenced texture sources and emit texld instructions
        * for each:
        */
-      for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
+      for (unit = 0; unit < key->nr_enabled_units; unit++)
         if (key->unit[unit].enabled) {
            load_texunit_sources( &p, unit );
            p.last_tex_stage = unit;
@@ -1411,7 +1420,7 @@ create_new_program(GLcontext *ctx, struct state_key *key,
 
       /* Second pass - emit combine instructions to build final color:
        */
-      for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++)
+      for (unit = 0; unit < key->nr_enabled_units; unit++)
         if (key->unit[unit].enabled) {
            p.src_previous = emit_texenv( &p, unit );
             reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
@@ -1502,12 +1511,13 @@ _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
 {
    struct gl_fragment_program *prog;
    struct state_key key;
+   GLuint keySize;
        
-   make_state_key(ctx, &key);
+   keySize = make_state_key(ctx, &key);
       
    prog = (struct gl_fragment_program *)
       _mesa_search_program_cache(ctx->FragmentProgram.Cache,
-                                 &key, sizeof(key));
+                                 &key, keySize);
 
    if (!prog) {
       prog = (struct gl_fragment_program *) 
@@ -1516,7 +1526,7 @@ _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
       create_new_program(ctx, &key, prog);
 
       _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
-                                 &key, sizeof(key), &prog->Base);
+                                 &key, keySize, &prog->Base);
    }
 
    return prog;
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Mesa3d-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mesa3d-dev

Reply via email to