Merge branch 'upstream-gallium-0.1' into darktama-gallium-0.1
[mesa.git] / src / mesa / shader / prog_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "main/glheader.h"
30 #include "main/mtypes.h"
31 #include "main/imports.h"
32 #include "shader/prog_cache.h"
33
34
35 struct cache_item
36 {
37 GLuint hash;
38 void *key;
39 struct gl_program *program;
40 struct cache_item *next;
41 };
42
43 struct gl_program_cache
44 {
45 struct cache_item **items;
46 GLuint size, n_items;
47 };
48
49
50
51 /**
52 * Compute hash index from state key.
53 */
54 static GLuint
55 hash_key(const void *key, GLuint key_size)
56 {
57 const GLuint *ikey = (const GLuint *) key;
58 GLuint hash = 0, i;
59
60 assert(key_size >= 4);
61
62 /* Make a slightly better attempt at a hash function:
63 */
64 for (i = 0; i < key_size / sizeof(*ikey); i++)
65 {
66 hash += ikey[i];
67 hash += (hash << 10);
68 hash ^= (hash >> 6);
69 }
70
71 return hash;
72 }
73
74
75 /**
76 * Rebuild/expand the hash table to accomodate more entries
77 */
78 static void
79 rehash(struct gl_program_cache *cache)
80 {
81 struct cache_item **items;
82 struct cache_item *c, *next;
83 GLuint size, i;
84
85 size = cache->size * 3;
86 items = (struct cache_item**) _mesa_malloc(size * sizeof(*items));
87 _mesa_memset(items, 0, size * sizeof(*items));
88
89 for (i = 0; i < cache->size; i++)
90 for (c = cache->items[i]; c; c = next) {
91 next = c->next;
92 c->next = items[c->hash % size];
93 items[c->hash % size] = c;
94 }
95
96 _mesa_free(cache->items);
97 cache->items = items;
98 cache->size = size;
99 }
100
101
102 static void
103 clear_cache(GLcontext *ctx, struct gl_program_cache *cache)
104 {
105 struct cache_item *c, *next;
106 GLuint i;
107
108 for (i = 0; i < cache->size; i++) {
109 for (c = cache->items[i]; c; c = next) {
110 next = c->next;
111 _mesa_free(c->key);
112 ctx->Driver.DeleteProgram(ctx, c->program);
113 _mesa_free(c);
114 }
115 cache->items[i] = NULL;
116 }
117
118
119 cache->n_items = 0;
120 }
121
122
123
124 struct gl_program_cache *
125 _mesa_new_program_cache(void)
126 {
127 struct gl_program_cache *cache = CALLOC_STRUCT(gl_program_cache);
128 if (cache) {
129 cache->size = 17;
130 cache->items = (struct cache_item **)
131 _mesa_calloc(cache->size * sizeof(struct cache_item));
132 if (!cache->items) {
133 _mesa_free(cache);
134 return NULL;
135 }
136 }
137 return cache;
138 }
139
140
141 void
142 _mesa_delete_program_cache(GLcontext *ctx, struct gl_program_cache *cache)
143 {
144 clear_cache(ctx, cache);
145 _mesa_free(cache->items);
146 _mesa_free(cache);
147 }
148
149
150 struct gl_program *
151 _mesa_search_program_cache(const struct gl_program_cache *cache,
152 const void *key, GLuint keysize)
153 {
154 const GLuint hash = hash_key(key, keysize);
155 struct cache_item *c;
156
157 for (c = cache->items[hash % cache->size]; c; c = c->next) {
158 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
159 return c->program;
160 }
161
162 return NULL;
163 }
164
165
166 void
167 _mesa_program_cache_insert(GLcontext *ctx,
168 struct gl_program_cache *cache,
169 const void *key, GLuint keysize,
170 struct gl_program *program)
171 {
172 const GLuint hash = hash_key(key, keysize);
173 struct cache_item *c = CALLOC_STRUCT(cache_item);
174
175 c->hash = hash;
176
177 c->key = _mesa_malloc(keysize);
178 memcpy(c->key, key, keysize);
179
180 c->program = program;
181
182 if (cache->n_items > cache->size * 1.5) {
183 if (cache->size < 1000)
184 rehash(cache);
185 else
186 clear_cache(ctx, cache);
187 }
188
189 cache->n_items++;
190 c->next = cache->items[hash % cache->size];
191 cache->items[hash % cache->size] = c;
192 }