1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 /* Authors: Zack Rusin <zack@tungstengraphics.com>
31 #include "util/u_debug.h"
33 #include "util/u_memory.h"
35 #include "cso_cache.h"
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
;
49 cso_sanitize_callback sanitize_cb
;
54 static unsigned hash_key(const void *key
, unsigned key_size
)
56 unsigned *ikey
= (unsigned *)key
;
59 assert(key_size
% 4 == 0);
61 /* I'm sure this can be improved on:
63 for (i
= 0; i
< key_size
/4; i
++)
69 static unsigned hash_key(const unsigned char *p
, int n
)
76 if ((g
= (h
& 0xf0000000)) != 0)
84 unsigned cso_construct_key(void *item
, int item_size
)
86 return hash_key((item
), item_size
);
89 static struct cso_hash
*_cso_hash_for_type(struct cso_cache
*sc
, enum cso_cache_type type
)
91 struct cso_hash
*hash
= 0;
95 hash
= sc
->blend_hash
;
98 hash
= sc
->sampler_hash
;
100 case CSO_DEPTH_STENCIL_ALPHA
:
101 hash
= sc
->depth_stencil_hash
;
104 hash
= sc
->rasterizer_hash
;
106 case CSO_FRAGMENT_SHADER
:
109 case CSO_VERTEX_SHADER
:
113 hash
= sc
->velements_hash
;
120 static void delete_blend_state(void *state
, void *data
)
122 struct cso_blend
*cso
= (struct cso_blend
*)state
;
123 if (cso
->delete_state
)
124 cso
->delete_state(cso
->context
, cso
->data
);
128 static void delete_depth_stencil_state(void *state
, void *data
)
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
);
136 static void delete_sampler_state(void *state
, void *data
)
138 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
139 if (cso
->delete_state
)
140 cso
->delete_state(cso
->context
, cso
->data
);
144 static void delete_rasterizer_state(void *state
, void *data
)
146 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
147 if (cso
->delete_state
)
148 cso
->delete_state(cso
->context
, cso
->data
);
152 static void delete_fs_state(void *state
, void *data
)
154 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
155 if (cso
->delete_state
)
156 cso
->delete_state(cso
->context
, cso
->data
);
160 static void delete_vs_state(void *state
, void *data
)
162 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
163 if (cso
->delete_state
)
164 cso
->delete_state(cso
->context
, cso
->data
);
168 static void delete_velements(void *state
, void *data
)
170 struct cso_velements
*cso
= (struct cso_velements
*)state
;
171 if (cso
->delete_state
)
172 cso
->delete_state(cso
->context
, cso
->data
);
176 static INLINE
void delete_cso(void *state
, enum cso_cache_type type
)
180 delete_blend_state(state
, 0);
183 delete_sampler_state(state
, 0);
185 case CSO_DEPTH_STENCIL_ALPHA
:
186 delete_depth_stencil_state(state
, 0);
189 delete_rasterizer_state(state
, 0);
191 case CSO_FRAGMENT_SHADER
:
192 delete_fs_state(state
, 0);
194 case CSO_VERTEX_SHADER
:
195 delete_vs_state(state
, 0);
198 delete_velements(state
, 0);
207 static INLINE
void sanitize_hash(struct cso_cache
*sc
,
208 struct cso_hash
*hash
,
209 enum cso_cache_type type
,
213 sc
->sanitize_cb(hash
, type
, max_size
, sc
->sanitize_data
);
217 static INLINE
void sanitize_cb(struct cso_hash
*hash
, enum cso_cache_type type
,
218 int max_size
, void *user_data
)
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
;
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
);
238 cso_insert_state(struct cso_cache
*sc
,
239 unsigned hash_key
, enum cso_cache_type type
,
242 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
243 sanitize_hash(sc
, hash
, type
, sc
->max_size
);
245 return cso_hash_insert(hash
, hash_key
, state
);
249 cso_find_state(struct cso_cache
*sc
,
250 unsigned hash_key
, enum cso_cache_type type
)
252 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
254 return cso_hash_find(hash
, hash_key
);
258 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
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
)) {
271 iter
= cso_hash_iter_next(iter
);
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
)
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
))
286 iter
= cso_hash_iter_next(iter
);
291 void * cso_take_state(struct cso_cache
*sc
,
292 unsigned hash_key
, enum cso_cache_type type
)
294 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
295 return cso_hash_take(hash
, hash_key
);
298 struct cso_cache
*cso_cache_create(void)
300 struct cso_cache
*sc
= MALLOC_STRUCT(cso_cache
);
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;
318 void cso_for_each_state(struct cso_cache
*sc
, enum cso_cache_type type
,
319 cso_state_callback func
, void *user_data
)
321 struct cso_hash
*hash
= 0;
322 struct cso_hash_iter iter
;
326 hash
= sc
->blend_hash
;
329 hash
= sc
->sampler_hash
;
331 case CSO_DEPTH_STENCIL_ALPHA
:
332 hash
= sc
->depth_stencil_hash
;
335 hash
= sc
->rasterizer_hash
;
337 case CSO_FRAGMENT_SHADER
:
340 case CSO_VERTEX_SHADER
:
344 hash
= sc
->velements_hash
;
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
);
353 func(state
, user_data
);
358 void cso_cache_delete(struct cso_cache
*sc
)
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);
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
);
384 void cso_set_maximum_cache_size(struct cso_cache
*sc
, int number
)
386 sc
->max_size
= number
;
388 sanitize_hash(sc
, sc
->blend_hash
, CSO_BLEND
, sc
->max_size
);
389 sanitize_hash(sc
, sc
->depth_stencil_hash
, CSO_DEPTH_STENCIL_ALPHA
,
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
);
398 int cso_maximum_cache_size(const struct cso_cache
*sc
)
403 void cso_cache_set_sanitize_callback(struct cso_cache
*sc
,
404 cso_sanitize_callback cb
,
407 sc
->sanitize_cb
= cb
;
408 sc
->sanitize_data
= user_data
;