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
;
48 cso_sanitize_callback sanitize_cb
;
53 static unsigned hash_key(const void *key
, unsigned key_size
)
55 unsigned *ikey
= (unsigned *)key
;
58 assert(key_size
% 4 == 0);
60 /* I'm sure this can be improved on:
62 for (i
= 0; i
< key_size
/4; i
++)
68 static unsigned hash_key(const unsigned char *p
, int n
)
75 if ((g
= (h
& 0xf0000000)) != 0)
83 unsigned cso_construct_key(void *item
, int item_size
)
85 return hash_key((item
), item_size
);
88 static struct cso_hash
*_cso_hash_for_type(struct cso_cache
*sc
, enum cso_cache_type type
)
90 struct cso_hash
*hash
= 0;
94 hash
= sc
->blend_hash
;
97 hash
= sc
->sampler_hash
;
99 case CSO_DEPTH_STENCIL_ALPHA
:
100 hash
= sc
->depth_stencil_hash
;
103 hash
= sc
->rasterizer_hash
;
105 case CSO_FRAGMENT_SHADER
:
108 case CSO_VERTEX_SHADER
:
116 static void delete_blend_state(void *state
, void *data
)
118 struct cso_blend
*cso
= (struct cso_blend
*)state
;
119 if (cso
->delete_state
)
120 cso
->delete_state(cso
->context
, cso
->data
);
124 static void delete_depth_stencil_state(void *state
, void *data
)
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
);
132 static void delete_sampler_state(void *state
, void *data
)
134 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
135 if (cso
->delete_state
)
136 cso
->delete_state(cso
->context
, cso
->data
);
140 static void delete_rasterizer_state(void *state
, void *data
)
142 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
148 static void delete_fs_state(void *state
, void *data
)
150 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
151 if (cso
->delete_state
)
152 cso
->delete_state(cso
->context
, cso
->data
);
156 static void delete_vs_state(void *state
, void *data
)
158 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
159 if (cso
->delete_state
)
160 cso
->delete_state(cso
->context
, cso
->data
);
165 static INLINE
void delete_cso(void *state
, enum cso_cache_type type
)
169 delete_blend_state(state
, 0);
172 delete_sampler_state(state
, 0);
174 case CSO_DEPTH_STENCIL_ALPHA
:
175 delete_depth_stencil_state(state
, 0);
178 delete_rasterizer_state(state
, 0);
180 case CSO_FRAGMENT_SHADER
:
181 delete_fs_state(state
, 0);
183 case CSO_VERTEX_SHADER
:
184 delete_vs_state(state
, 0);
193 static INLINE
void sanitize_hash(struct cso_cache
*sc
,
194 struct cso_hash
*hash
,
195 enum cso_cache_type type
,
199 sc
->sanitize_cb(hash
, type
, max_size
, sc
->sanitize_data
);
203 static INLINE
void sanitize_cb(struct cso_hash
*hash
, enum cso_cache_type type
,
204 int max_size
, void *user_data
)
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
;
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
);
224 cso_insert_state(struct cso_cache
*sc
,
225 unsigned hash_key
, enum cso_cache_type type
,
228 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
229 sanitize_hash(sc
, hash
, type
, sc
->max_size
);
231 return cso_hash_insert(hash
, hash_key
, state
);
235 cso_find_state(struct cso_cache
*sc
,
236 unsigned hash_key
, enum cso_cache_type type
)
238 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
240 return cso_hash_find(hash
, hash_key
);
244 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
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
)) {
257 iter
= cso_hash_iter_next(iter
);
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
)
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
))
272 iter
= cso_hash_iter_next(iter
);
277 void * cso_take_state(struct cso_cache
*sc
,
278 unsigned hash_key
, enum cso_cache_type type
)
280 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
281 return cso_hash_take(hash
, hash_key
);
284 struct cso_cache
*cso_cache_create(void)
286 struct cso_cache
*sc
= MALLOC_STRUCT(cso_cache
);
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;
303 void cso_for_each_state(struct cso_cache
*sc
, enum cso_cache_type type
,
304 cso_state_callback func
, void *user_data
)
306 struct cso_hash
*hash
= 0;
307 struct cso_hash_iter iter
;
311 hash
= sc
->blend_hash
;
314 hash
= sc
->sampler_hash
;
316 case CSO_DEPTH_STENCIL_ALPHA
:
317 hash
= sc
->depth_stencil_hash
;
320 hash
= sc
->rasterizer_hash
;
322 case CSO_FRAGMENT_SHADER
:
325 case CSO_VERTEX_SHADER
:
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
);
335 func(state
, user_data
);
340 void cso_cache_delete(struct cso_cache
*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);
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
);
364 void cso_set_maximum_cache_size(struct cso_cache
*sc
, int number
)
366 sc
->max_size
= number
;
368 sanitize_hash(sc
, sc
->blend_hash
, CSO_BLEND
, sc
->max_size
);
369 sanitize_hash(sc
, sc
->depth_stencil_hash
, CSO_DEPTH_STENCIL_ALPHA
,
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
);
377 int cso_maximum_cache_size(const struct cso_cache
*sc
)
382 void cso_cache_set_sanitize_callback(struct cso_cache
*sc
,
383 cso_sanitize_callback cb
,
386 sc
->sanitize_cb
= cb
;
387 sc
->sanitize_data
= user_data
;