blob: d97e27dd6b57812f2babddcf9935a68ef10d6361 [file] [log] [blame]
Brian0ee15e02007-10-30 12:23:22 -06001/**************************************************************************
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
35struct cache_item
36{
37 GLuint hash;
38 void *key;
39 struct gl_program *program;
40 struct cache_item *next;
41};
42
43struct 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 */
54static GLuint
55hash_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 */
78static void
79rehash(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
102static void
103clear_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
124struct 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
141void
142_mesa_delete_program_cache(GLcontext *ctx, struct gl_program_cache *cache)
143{
144 clear_cache(ctx, cache);
145 _mesa_free(cache->items);
146}
147
148
149struct gl_program *
150_mesa_search_program_cache(const struct gl_program_cache *cache,
151 const void *key, GLuint keysize)
152{
153 const GLuint hash = hash_key(key, keysize);
154 struct cache_item *c;
155
156 for (c = cache->items[hash % cache->size]; c; c = c->next) {
157 if (c->hash == hash && memcmp(c->key, key, keysize) == 0)
158 return c->program;
159 }
160
161 return NULL;
162}
163
164
165void
166_mesa_program_cache_insert(GLcontext *ctx,
167 struct gl_program_cache *cache,
168 const void *key, GLuint keysize,
169 struct gl_program *program)
170{
171 const GLuint hash = hash_key(key, keysize);
172 struct cache_item *c = CALLOC_STRUCT(cache_item);
173
174 c->hash = hash;
175
176 c->key = _mesa_malloc(keysize);
177 memcpy(c->key, key, keysize);
178
179 c->program = program;
180
181 if (cache->n_items > cache->size * 1.5) {
182 if (cache->size < 1000)
183 rehash(cache);
184 else
185 clear_cache(ctx, cache);
186 }
187
188 cache->n_items++;
189 c->next = cache->items[hash % cache->size];
190 cache->items[hash % cache->size] = c;
191}