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