[OPENGL]
[reactos.git] / reactos / dll / opengl / mesa / src / gallium / auxiliary / cso_cache / cso_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /* Authors: Zack Rusin <zack@tungstengraphics.com>
29 */
30
31 #include "util/u_debug.h"
32
33 #include "util/u_memory.h"
34
35 #include "cso_cache.h"
36 #include "cso_hash.h"
37
38
39 struct cso_cache {
40 struct cso_hash *hashes[CSO_CACHE_MAX];
41 int max_size;
42
43 cso_sanitize_callback sanitize_cb;
44 void *sanitize_data;
45 };
46
47 #if 1
48 static unsigned hash_key(const void *key, unsigned key_size)
49 {
50 unsigned *ikey = (unsigned *)key;
51 unsigned hash = 0, i;
52
53 assert(key_size % 4 == 0);
54
55 /* I'm sure this can be improved on:
56 */
57 for (i = 0; i < key_size/4; i++)
58 hash ^= ikey[i];
59
60 return hash;
61 }
62 #else
63 static unsigned hash_key(const unsigned char *p, int n)
64 {
65 unsigned h = 0;
66 unsigned g;
67
68 while (n--) {
69 h = (h << 4) + *p++;
70 if ((g = (h & 0xf0000000)) != 0)
71 h ^= g >> 23;
72 h &= ~g;
73 }
74 return h;
75 }
76 #endif
77
78 unsigned cso_construct_key(void *item, int item_size)
79 {
80 return hash_key((item), item_size);
81 }
82
83 static INLINE struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type)
84 {
85 struct cso_hash *hash;
86 hash = sc->hashes[type];
87 return hash;
88 }
89
90 static void delete_blend_state(void *state, void *data)
91 {
92 struct cso_blend *cso = (struct cso_blend *)state;
93 if (cso->delete_state)
94 cso->delete_state(cso->context, cso->data);
95 FREE(state);
96 }
97
98 static void delete_depth_stencil_state(void *state, void *data)
99 {
100 struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
101 if (cso->delete_state)
102 cso->delete_state(cso->context, cso->data);
103 FREE(state);
104 }
105
106 static void delete_sampler_state(void *state, void *data)
107 {
108 struct cso_sampler *cso = (struct cso_sampler *)state;
109 if (cso->delete_state)
110 cso->delete_state(cso->context, cso->data);
111 FREE(state);
112 }
113
114 static void delete_rasterizer_state(void *state, void *data)
115 {
116 struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
117 if (cso->delete_state)
118 cso->delete_state(cso->context, cso->data);
119 FREE(state);
120 }
121
122 static void delete_fs_state(void *state, void *data)
123 {
124 struct cso_fragment_shader *cso = (struct cso_fragment_shader *)state;
125 if (cso->delete_state)
126 cso->delete_state(cso->context, cso->data);
127 FREE(state);
128 }
129
130 static void delete_vs_state(void *state, void *data)
131 {
132 struct cso_vertex_shader *cso = (struct cso_vertex_shader *)state;
133 if (cso->delete_state)
134 cso->delete_state(cso->context, cso->data);
135 FREE(state);
136 }
137
138 static void delete_velements(void *state, void *data)
139 {
140 struct cso_velements *cso = (struct cso_velements *)state;
141 if (cso->delete_state)
142 cso->delete_state(cso->context, cso->data);
143 FREE(state);
144 }
145
146 static INLINE void delete_cso(void *state, enum cso_cache_type type)
147 {
148 switch (type) {
149 case CSO_BLEND:
150 delete_blend_state(state, 0);
151 break;
152 case CSO_SAMPLER:
153 delete_sampler_state(state, 0);
154 break;
155 case CSO_DEPTH_STENCIL_ALPHA:
156 delete_depth_stencil_state(state, 0);
157 break;
158 case CSO_RASTERIZER:
159 delete_rasterizer_state(state, 0);
160 break;
161 case CSO_FRAGMENT_SHADER:
162 delete_fs_state(state, 0);
163 break;
164 case CSO_VERTEX_SHADER:
165 delete_vs_state(state, 0);
166 break;
167 case CSO_VELEMENTS:
168 delete_velements(state, 0);
169 break;
170 default:
171 assert(0);
172 FREE(state);
173 }
174 }
175
176
177 static INLINE void sanitize_hash(struct cso_cache *sc,
178 struct cso_hash *hash,
179 enum cso_cache_type type,
180 int max_size)
181 {
182 if (sc->sanitize_cb)
183 sc->sanitize_cb(hash, type, max_size, sc->sanitize_data);
184 }
185
186
187 static INLINE void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
188 int max_size, void *user_data)
189 {
190 /* if we're approach the maximum size, remove fourth of the entries
191 * otherwise every subsequent call will go through the same */
192 int hash_size = cso_hash_size(hash);
193 int max_entries = (max_size > hash_size) ? max_size : hash_size;
194 int to_remove = (max_size < max_entries) * max_entries/4;
195 if (hash_size > max_size)
196 to_remove += hash_size - max_size;
197 while (to_remove) {
198 /*remove elements until we're good */
199 /*fixme: currently we pick the nodes to remove at random*/
200 struct cso_hash_iter iter = cso_hash_first_node(hash);
201 void *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
202 delete_cso(cso, type);
203 --to_remove;
204 }
205 }
206
207 struct cso_hash_iter
208 cso_insert_state(struct cso_cache *sc,
209 unsigned hash_key, enum cso_cache_type type,
210 void *state)
211 {
212 struct cso_hash *hash = _cso_hash_for_type(sc, type);
213 sanitize_hash(sc, hash, type, sc->max_size);
214
215 return cso_hash_insert(hash, hash_key, state);
216 }
217
218 struct cso_hash_iter
219 cso_find_state(struct cso_cache *sc,
220 unsigned hash_key, enum cso_cache_type type)
221 {
222 struct cso_hash *hash = _cso_hash_for_type(sc, type);
223
224 return cso_hash_find(hash, hash_key);
225 }
226
227
228 void *cso_hash_find_data_from_template( struct cso_hash *hash,
229 unsigned hash_key,
230 void *templ,
231 int size )
232 {
233 struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
234 while (!cso_hash_iter_is_null(iter)) {
235 void *iter_data = cso_hash_iter_data(iter);
236 if (!memcmp(iter_data, templ, size)) {
237 /* We found a match
238 */
239 return iter_data;
240 }
241 iter = cso_hash_iter_next(iter);
242 }
243 return NULL;
244 }
245
246
247 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
248 unsigned hash_key, enum cso_cache_type type,
249 void *templ, unsigned size)
250 {
251 struct cso_hash_iter iter = cso_find_state(sc, hash_key, type);
252 while (!cso_hash_iter_is_null(iter)) {
253 void *iter_data = cso_hash_iter_data(iter);
254 if (!memcmp(iter_data, templ, size))
255 return iter;
256 iter = cso_hash_iter_next(iter);
257 }
258 return iter;
259 }
260
261 void * cso_take_state(struct cso_cache *sc,
262 unsigned hash_key, enum cso_cache_type type)
263 {
264 struct cso_hash *hash = _cso_hash_for_type(sc, type);
265 return cso_hash_take(hash, hash_key);
266 }
267
268 struct cso_cache *cso_cache_create(void)
269 {
270 struct cso_cache *sc = MALLOC_STRUCT(cso_cache);
271 int i;
272 if (sc == NULL)
273 return NULL;
274
275 sc->max_size = 4096;
276 for (i = 0; i < CSO_CACHE_MAX; i++)
277 sc->hashes[i] = cso_hash_create();
278
279 sc->sanitize_cb = sanitize_cb;
280 sc->sanitize_data = 0;
281
282 return sc;
283 }
284
285 void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
286 cso_state_callback func, void *user_data)
287 {
288 struct cso_hash *hash = _cso_hash_for_type(sc, type);
289 struct cso_hash_iter iter;
290
291 iter = cso_hash_first_node(hash);
292 while (!cso_hash_iter_is_null(iter)) {
293 void *state = cso_hash_iter_data(iter);
294 iter = cso_hash_iter_next(iter);
295 if (state) {
296 func(state, user_data);
297 }
298 }
299 }
300
301 void cso_cache_delete(struct cso_cache *sc)
302 {
303 int i;
304 assert(sc);
305
306 if (!sc)
307 return;
308
309 /* delete driver data */
310 cso_for_each_state(sc, CSO_BLEND, delete_blend_state, 0);
311 cso_for_each_state(sc, CSO_DEPTH_STENCIL_ALPHA, delete_depth_stencil_state, 0);
312 cso_for_each_state(sc, CSO_FRAGMENT_SHADER, delete_fs_state, 0);
313 cso_for_each_state(sc, CSO_VERTEX_SHADER, delete_vs_state, 0);
314 cso_for_each_state(sc, CSO_RASTERIZER, delete_rasterizer_state, 0);
315 cso_for_each_state(sc, CSO_SAMPLER, delete_sampler_state, 0);
316 cso_for_each_state(sc, CSO_VELEMENTS, delete_velements, 0);
317
318 for (i = 0; i < CSO_CACHE_MAX; i++)
319 cso_hash_delete(sc->hashes[i]);
320
321 FREE(sc);
322 }
323
324 void cso_set_maximum_cache_size(struct cso_cache *sc, int number)
325 {
326 int i;
327
328 sc->max_size = number;
329
330 for (i = 0; i < CSO_CACHE_MAX; i++)
331 sanitize_hash(sc, sc->hashes[i], i, sc->max_size);
332 }
333
334 int cso_maximum_cache_size(const struct cso_cache *sc)
335 {
336 return sc->max_size;
337 }
338
339 void cso_cache_set_sanitize_callback(struct cso_cache *sc,
340 cso_sanitize_callback cb,
341 void *user_data)
342 {
343 sc->sanitize_cb = cb;
344 sc->sanitize_data = user_data;
345 }
346