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 "pipe/p_util.h"
32 #include "pipe/p_debug.h"
34 #include "cso_cache.h"
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
;
49 static unsigned hash_key(const void *key
, unsigned key_size
)
51 unsigned *ikey
= (unsigned *)key
;
54 assert(key_size
% 4 == 0);
56 /* I'm sure this can be improved on:
58 for (i
= 0; i
< key_size
/4; i
++)
64 static unsigned hash_key(const unsigned char *p
, int n
)
71 if ((g
= (h
& 0xf0000000)) != 0)
79 unsigned cso_construct_key(void *item
, int item_size
)
81 return hash_key((item
), item_size
);
84 static struct cso_hash
*_cso_hash_for_type(struct cso_cache
*sc
, enum cso_cache_type type
)
86 struct cso_hash
*hash
= 0;
90 hash
= sc
->blend_hash
;
93 hash
= sc
->sampler_hash
;
95 case CSO_DEPTH_STENCIL_ALPHA
:
96 hash
= sc
->depth_stencil_hash
;
99 hash
= sc
->rasterizer_hash
;
101 case CSO_FRAGMENT_SHADER
:
104 case CSO_VERTEX_SHADER
:
112 static int _cso_size_for_type(enum cso_cache_type type
)
116 return sizeof(struct pipe_blend_state
);
118 return sizeof(struct pipe_sampler_state
);
119 case CSO_DEPTH_STENCIL_ALPHA
:
120 return sizeof(struct pipe_depth_stencil_alpha_state
);
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
);
132 static void delete_blend_state(void *state
, void *data
)
134 struct cso_blend
*cso
= (struct cso_blend
*)state
;
135 if (cso
->delete_state
)
136 cso
->delete_state(cso
->context
, cso
->data
);
140 static void delete_depth_stencil_state(void *state
, void *data
)
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
);
148 static void delete_sampler_state(void *state
, void *data
)
150 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
151 if (cso
->delete_state
)
152 cso
->delete_state(cso
->context
, cso
->data
);
156 static void delete_rasterizer_state(void *state
, void *data
)
158 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
159 if (cso
->delete_state
)
160 cso
->delete_state(cso
->context
, cso
->data
);
164 static void delete_fs_state(void *state
, void *data
)
166 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
167 if (cso
->delete_state
)
168 cso
->delete_state(cso
->context
, cso
->data
);
172 static void delete_vs_state(void *state
, void *data
)
174 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
175 if (cso
->delete_state
)
176 cso
->delete_state(cso
->context
, cso
->data
);
181 static INLINE
void delete_cso(void *state
, enum cso_cache_type type
)
185 delete_blend_state(state
, 0);
188 delete_sampler_state(state
, 0);
190 case CSO_DEPTH_STENCIL_ALPHA
:
191 delete_depth_stencil_state(state
, 0);
194 delete_rasterizer_state(state
, 0);
196 case CSO_FRAGMENT_SHADER
:
197 delete_fs_state(state
, 0);
199 case CSO_VERTEX_SHADER
:
200 delete_vs_state(state
, 0);
208 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
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
;
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
);
229 cso_insert_state(struct cso_cache
*sc
,
230 unsigned hash_key
, enum cso_cache_type type
,
233 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
234 sanitize_hash(hash
, type
, sc
->max_size
);
236 return cso_hash_insert(hash
, hash_key
, state
);
240 cso_find_state(struct cso_cache
*sc
,
241 unsigned hash_key
, enum cso_cache_type type
)
243 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
245 return cso_hash_find(hash
, hash_key
);
249 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
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:
260 return (unsigned char *)iter_data
+ size
;
262 iter
= cso_hash_iter_next(iter
);
268 struct cso_hash_iter
cso_find_state_template(struct cso_cache
*sc
,
269 unsigned hash_key
, enum cso_cache_type type
,
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
))
278 iter
= cso_hash_iter_next(iter
);
283 void * cso_take_state(struct cso_cache
*sc
,
284 unsigned hash_key
, enum cso_cache_type type
)
286 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
287 return cso_hash_take(hash
, hash_key
);
290 struct cso_cache
*cso_cache_create(void)
292 struct cso_cache
*sc
= MALLOC_STRUCT(cso_cache
);
295 sc
->blend_hash
= cso_hash_create();
296 sc
->sampler_hash
= cso_hash_create();
297 sc
->depth_stencil_hash
= cso_hash_create();
298 sc
->rasterizer_hash
= cso_hash_create();
299 sc
->fs_hash
= cso_hash_create();
300 sc
->vs_hash
= cso_hash_create();
305 void cso_for_each_state(struct cso_cache
*sc
, enum cso_cache_type type
,
306 cso_state_callback func
, void *user_data
)
308 struct cso_hash
*hash
= 0;
309 struct cso_hash_iter iter
;
313 hash
= sc
->blend_hash
;
316 hash
= sc
->sampler_hash
;
318 case CSO_DEPTH_STENCIL_ALPHA
:
319 hash
= sc
->depth_stencil_hash
;
322 hash
= sc
->rasterizer_hash
;
324 case CSO_FRAGMENT_SHADER
:
327 case CSO_VERTEX_SHADER
:
332 iter
= cso_hash_first_node(hash
);
333 while (!cso_hash_iter_is_null(iter
)) {
334 void *state
= cso_hash_iter_data(iter
);
336 func(state
, user_data
);
338 iter
= cso_hash_iter_next(iter
);
342 void cso_cache_delete(struct cso_cache
*sc
)
345 /* delete driver data */
346 cso_for_each_state(sc
, CSO_BLEND
, delete_blend_state
, 0);
347 cso_for_each_state(sc
, CSO_DEPTH_STENCIL_ALPHA
, delete_depth_stencil_state
, 0);
348 cso_for_each_state(sc
, CSO_FRAGMENT_SHADER
, delete_fs_state
, 0);
349 cso_for_each_state(sc
, CSO_VERTEX_SHADER
, delete_vs_state
, 0);
350 cso_for_each_state(sc
, CSO_RASTERIZER
, delete_rasterizer_state
, 0);
351 cso_for_each_state(sc
, CSO_SAMPLER
, delete_sampler_state
, 0);
353 cso_hash_delete(sc
->blend_hash
);
354 cso_hash_delete(sc
->sampler_hash
);
355 cso_hash_delete(sc
->depth_stencil_hash
);
356 cso_hash_delete(sc
->rasterizer_hash
);
357 cso_hash_delete(sc
->fs_hash
);
358 cso_hash_delete(sc
->vs_hash
);
362 void cso_set_maximum_cache_size(struct cso_cache
*sc
, int number
)
364 sc
->max_size
= number
;
366 sanitize_hash(sc
->blend_hash
, CSO_BLEND
, sc
->max_size
);
367 sanitize_hash(sc
->depth_stencil_hash
, CSO_DEPTH_STENCIL_ALPHA
,
369 sanitize_hash(sc
->fs_hash
, CSO_FRAGMENT_SHADER
, sc
->max_size
);
370 sanitize_hash(sc
->vs_hash
, CSO_VERTEX_SHADER
, sc
->max_size
);
371 sanitize_hash(sc
->rasterizer_hash
, CSO_RASTERIZER
, sc
->max_size
);
372 sanitize_hash(sc
->sampler_hash
, CSO_SAMPLER
, sc
->max_size
);
375 int cso_maximum_cache_size(const struct cso_cache
*sc
)