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_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 int _cso_size_for_type(enum cso_cache_type type
)
120 return sizeof(struct pipe_blend_state
);
122 return sizeof(struct pipe_sampler_state
);
123 case CSO_DEPTH_STENCIL_ALPHA
:
124 return sizeof(struct pipe_depth_stencil_alpha_state
);
126 return sizeof(struct pipe_rasterizer_state
);
127 case CSO_FRAGMENT_SHADER
:
128 return sizeof(struct pipe_shader_state
);
129 case CSO_VERTEX_SHADER
:
130 return sizeof(struct pipe_shader_state
);
136 static void delete_blend_state(void *state
, void *data
)
138 struct cso_blend
*cso
= (struct cso_blend
*)state
;
139 if (cso
->delete_state
)
140 cso
->delete_state(cso
->context
, cso
->data
);
144 static void delete_depth_stencil_state(void *state
, void *data
)
146 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
147 if (cso
->delete_state
)
148 cso
->delete_state(cso
->context
, cso
->data
);
152 static void delete_sampler_state(void *state
, void *data
)
154 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
155 if (cso
->delete_state
)
156 cso
->delete_state(cso
->context
, cso
->data
);
160 static void delete_rasterizer_state(void *state
, void *data
)
162 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
163 if (cso
->delete_state
)
164 cso
->delete_state(cso
->context
, cso
->data
);
168 static void delete_fs_state(void *state
, void *data
)
170 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
171 if (cso
->delete_state
)
172 cso
->delete_state(cso
->context
, cso
->data
);
176 static void delete_vs_state(void *state
, void *data
)
178 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
179 if (cso
->delete_state
)
180 cso
->delete_state(cso
->context
, cso
->data
);
185 static INLINE
void delete_cso(void *state
, enum cso_cache_type type
)
189 delete_blend_state(state
, 0);
192 delete_sampler_state(state
, 0);
194 case CSO_DEPTH_STENCIL_ALPHA
:
195 delete_depth_stencil_state(state
, 0);
198 delete_rasterizer_state(state
, 0);
200 case CSO_FRAGMENT_SHADER
:
201 delete_fs_state(state
, 0);
203 case CSO_VERTEX_SHADER
:
204 delete_vs_state(state
, 0);
213 static INLINE
void sanitize_hash(struct cso_cache
*sc
,
214 struct cso_hash
*hash
,
215 enum cso_cache_type type
,
219 sc
->sanitize_cb(hash
, type
, max_size
, sc
->sanitize_data
);
223 static INLINE
void sanitize_cb(struct cso_hash
*hash
, enum cso_cache_type type
,
224 int max_size
, void *user_data
)
226 /* if we're approach the maximum size, remove fourth of the entries
227 * otherwise every subsequent call will go through the same */
228 int hash_size
= cso_hash_size(hash
);
229 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
230 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
231 if (hash_size
> max_size
)
232 to_remove
+= hash_size
- max_size
;
234 /*remove elements until we're good */
235 /*fixme: currently we pick the nodes to remove at random*/
236 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
237 void *cso
= cso_hash_take(hash
, cso_hash_iter_key(iter
));
238 delete_cso(cso
, type
);
244 cso_insert_state(struct cso_cache
*sc
,
245 unsigned hash_key
, enum cso_cache_type type
,
248 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
249 sanitize_hash(sc
, hash
, type
, sc
->max_size
);
251 return cso_hash_insert(hash
, hash_key
, state
);
255 cso_find_state(struct cso_cache
*sc
,
256 unsigned hash_key
, enum cso_cache_type type
)
258 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
260 return cso_hash_find(hash
, hash_key
);
264 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
269 struct cso_hash_iter iter
= cso_hash_find(hash
, hash_key
);
270 while (!cso_hash_iter_is_null(iter
)) {
271 void *iter_data
= cso_hash_iter_data(iter
);
272 if (!memcmp(iter_data
, templ
, size
)) {
277 iter
= cso_hash_iter_next(iter
);
283 struct cso_hash_iter
cso_find_state_template(struct cso_cache
*sc
,
284 unsigned hash_key
, enum cso_cache_type type
,
287 struct cso_hash_iter iter
= cso_find_state(sc
, hash_key
, type
);
288 int size
= _cso_size_for_type(type
);
289 while (!cso_hash_iter_is_null(iter
)) {
290 void *iter_data
= cso_hash_iter_data(iter
);
291 if (!memcmp(iter_data
, templ
, size
))
293 iter
= cso_hash_iter_next(iter
);
298 void * cso_take_state(struct cso_cache
*sc
,
299 unsigned hash_key
, enum cso_cache_type type
)
301 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
302 return cso_hash_take(hash
, hash_key
);
305 struct cso_cache
*cso_cache_create(void)
307 struct cso_cache
*sc
= MALLOC_STRUCT(cso_cache
);
312 sc
->blend_hash
= cso_hash_create();
313 sc
->sampler_hash
= cso_hash_create();
314 sc
->depth_stencil_hash
= cso_hash_create();
315 sc
->rasterizer_hash
= cso_hash_create();
316 sc
->fs_hash
= cso_hash_create();
317 sc
->vs_hash
= cso_hash_create();
318 sc
->sanitize_cb
= sanitize_cb
;
319 sc
->sanitize_data
= 0;
324 void cso_for_each_state(struct cso_cache
*sc
, enum cso_cache_type type
,
325 cso_state_callback func
, void *user_data
)
327 struct cso_hash
*hash
= 0;
328 struct cso_hash_iter iter
;
332 hash
= sc
->blend_hash
;
335 hash
= sc
->sampler_hash
;
337 case CSO_DEPTH_STENCIL_ALPHA
:
338 hash
= sc
->depth_stencil_hash
;
341 hash
= sc
->rasterizer_hash
;
343 case CSO_FRAGMENT_SHADER
:
346 case CSO_VERTEX_SHADER
:
351 iter
= cso_hash_first_node(hash
);
352 while (!cso_hash_iter_is_null(iter
)) {
353 void *state
= cso_hash_iter_data(iter
);
354 iter
= cso_hash_iter_next(iter
);
356 func(state
, user_data
);
361 void cso_cache_delete(struct cso_cache
*sc
)
364 /* delete driver data */
365 cso_for_each_state(sc
, CSO_BLEND
, delete_blend_state
, 0);
366 cso_for_each_state(sc
, CSO_DEPTH_STENCIL_ALPHA
, delete_depth_stencil_state
, 0);
367 cso_for_each_state(sc
, CSO_FRAGMENT_SHADER
, delete_fs_state
, 0);
368 cso_for_each_state(sc
, CSO_VERTEX_SHADER
, delete_vs_state
, 0);
369 cso_for_each_state(sc
, CSO_RASTERIZER
, delete_rasterizer_state
, 0);
370 cso_for_each_state(sc
, CSO_SAMPLER
, delete_sampler_state
, 0);
372 cso_hash_delete(sc
->blend_hash
);
373 cso_hash_delete(sc
->sampler_hash
);
374 cso_hash_delete(sc
->depth_stencil_hash
);
375 cso_hash_delete(sc
->rasterizer_hash
);
376 cso_hash_delete(sc
->fs_hash
);
377 cso_hash_delete(sc
->vs_hash
);
381 void cso_set_maximum_cache_size(struct cso_cache
*sc
, int number
)
383 sc
->max_size
= number
;
385 sanitize_hash(sc
, sc
->blend_hash
, CSO_BLEND
, sc
->max_size
);
386 sanitize_hash(sc
, sc
->depth_stencil_hash
, CSO_DEPTH_STENCIL_ALPHA
,
388 sanitize_hash(sc
, sc
->fs_hash
, CSO_FRAGMENT_SHADER
, sc
->max_size
);
389 sanitize_hash(sc
, sc
->vs_hash
, CSO_VERTEX_SHADER
, sc
->max_size
);
390 sanitize_hash(sc
, sc
->rasterizer_hash
, CSO_RASTERIZER
, sc
->max_size
);
391 sanitize_hash(sc
, sc
->sampler_hash
, CSO_SAMPLER
, sc
->max_size
);
394 int cso_maximum_cache_size(const struct cso_cache
*sc
)
399 void cso_cache_set_sanitize_callback(struct cso_cache
*sc
,
400 cso_sanitize_callback cb
,
403 sc
->sanitize_cb
= cb
;
404 sc
->sanitize_data
= user_data
;