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