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