prog_cache: Add some support for shader_programs in prog_cache.
authorEric Anholt <eric@anholt.net>
Fri, 5 Nov 2010 14:25:18 +0000 (07:25 -0700)
committerEric Anholt <eric@anholt.net>
Fri, 11 Mar 2011 20:55:13 +0000 (12:55 -0800)
This is used in the upcoming fixed function shader_program generation,
and shader_program and ARB programs are together in this code until
both fragment and vertex ff get converted.

src/mesa/program/prog_cache.c
src/mesa/program/prog_cache.h

index 56ca59890de0ded4d83eafa3b4b4378960e7fb3a..2ccedb5d7d0d80547a9f0344f13bb5302a2c40e1 100644 (file)
@@ -29,6 +29,7 @@
 #include "main/glheader.h"
 #include "main/mtypes.h"
 #include "main/imports.h"
+#include "main/shaderobj.h"
 #include "program/prog_cache.h"
 #include "program/program.h"
 
@@ -104,7 +105,8 @@ rehash(struct gl_program_cache *cache)
 
 
 static void
-clear_cache(struct gl_context *ctx, struct gl_program_cache *cache)
+clear_cache(struct gl_context *ctx, struct gl_program_cache *cache,
+           GLboolean shader)
 {
    struct cache_item *c, *next;
    GLuint i;
@@ -115,7 +117,13 @@ clear_cache(struct gl_context *ctx, struct gl_program_cache *cache)
       for (c = cache->items[i]; c; c = next) {
         next = c->next;
         free(c->key);
-         _mesa_reference_program(ctx, &c->program, NULL);
+        if (shader) {
+           _mesa_reference_shader_program(ctx,
+                                          (struct gl_shader_program **)&c->program,
+                                          NULL);
+        } else {
+           _mesa_reference_program(ctx, &c->program, NULL);
+        }
         free(c);
       }
       cache->items[i] = NULL;
@@ -147,7 +155,16 @@ _mesa_new_program_cache(void)
 void
 _mesa_delete_program_cache(struct gl_context *ctx, struct gl_program_cache *cache)
 {
-   clear_cache(ctx, cache);
+   clear_cache(ctx, cache, GL_FALSE);
+   free(cache->items);
+   free(cache);
+}
+
+void
+_mesa_delete_shader_cache(struct gl_context *ctx,
+                         struct gl_program_cache *cache)
+{
+   clear_cache(ctx, cache, GL_TRUE);
    free(cache->items);
    free(cache);
 }
@@ -197,7 +214,35 @@ _mesa_program_cache_insert(struct gl_context *ctx,
       if (cache->size < 1000)
         rehash(cache);
       else 
-        clear_cache(ctx, cache);
+        clear_cache(ctx, cache, GL_FALSE);
+   }
+
+   cache->n_items++;
+   c->next = cache->items[hash % cache->size];
+   cache->items[hash % cache->size] = c;
+}
+
+void
+_mesa_shader_cache_insert(struct gl_context *ctx,
+                         struct gl_program_cache *cache,
+                         const void *key, GLuint keysize,
+                         struct gl_shader_program *program)
+{
+   const GLuint hash = hash_key(key, keysize);
+   struct cache_item *c = CALLOC_STRUCT(cache_item);
+
+   c->hash = hash;
+
+   c->key = malloc(keysize);
+   memcpy(c->key, key, keysize);
+
+   c->program = (struct gl_program *)program;  /* no refcount change */
+
+   if (cache->n_items > cache->size * 1.5) {
+      if (cache->size < 1000)
+        rehash(cache);
+      else
+        clear_cache(ctx, cache, GL_TRUE);
    }
 
    cache->n_items++;
index 0167334827952a76c03bcb3dbfd18590af9ef0a2..5d46bfc5cc931a8b239b923d92ac9a13a4f6ad06 100644 (file)
@@ -44,6 +44,9 @@ _mesa_new_program_cache(void);
 extern void
 _mesa_delete_program_cache(struct gl_context *ctx, struct gl_program_cache *pc);
 
+extern void
+_mesa_delete_shader_cache(struct gl_context *ctx,
+                         struct gl_program_cache *cache);
 
 extern struct gl_program *
 _mesa_search_program_cache(struct gl_program_cache *cache,
@@ -55,5 +58,11 @@ _mesa_program_cache_insert(struct gl_context *ctx,
                            const void *key, GLuint keysize,
                            struct gl_program *program);
 
+void
+_mesa_shader_cache_insert(struct gl_context *ctx,
+                         struct gl_program_cache *cache,
+                         const void *key, GLuint keysize,
+                         struct gl_shader_program *program);
+
 
 #endif /* PROG_CACHE_H */