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 *hashes[CSO_CACHE_MAX];
43 cso_sanitize_callback sanitize_cb;
48 static unsigned hash_key(const void *key, unsigned key_size)
50 unsigned *ikey = (unsigned *)key;
53 assert(key_size % 4 == 0);
55 /* I'm sure this can be improved on:
57 for (i = 0; i < key_size/4; i++)
63 static unsigned hash_key(const unsigned char *p, int n)
70 if ((g = (h & 0xf0000000)) != 0)
78 unsigned cso_construct_key(void *item, int item_size)
80 return hash_key((item), item_size);
83 static INLINE struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type)
85 struct cso_hash *hash;
86 hash = sc->hashes[type];
90 static void delete_blend_state(void *state, void *data)
92 struct cso_blend *cso = (struct cso_blend *)state;
93 if (cso->delete_state)
94 cso->delete_state(cso->context, cso->data);
98 static void delete_depth_stencil_state(void *state, void *data)
100 struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
101 if (cso->delete_state)
102 cso->delete_state(cso->context, cso->data);
106 static void delete_sampler_state(void *state, void *data)
108 struct cso_sampler *cso = (struct cso_sampler *)state;
109 if (cso->delete_state)
110 cso->delete_state(cso->context, cso->data);
114 static void delete_rasterizer_state(void *state, void *data)
116 struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
117 if (cso->delete_state)
118 cso->delete_state(cso->context, cso->data);
122 static void delete_fs_state(void *state, void *data)
124 struct cso_fragment_shader *cso = (struct cso_fragment_shader *)state;
125 if (cso->delete_state)
126 cso->delete_state(cso->context, cso->data);
130 static void delete_vs_state(void *state, void *data)
132 struct cso_vertex_shader *cso = (struct cso_vertex_shader *)state;
133 if (cso->delete_state)
134 cso->delete_state(cso->context, cso->data);
138 static void delete_velements(void *state, void *data)
140 struct cso_velements *cso = (struct cso_velements *)state;
141 if (cso->delete_state)
142 cso->delete_state(cso->context, cso->data);
146 static INLINE void delete_cso(void *state, enum cso_cache_type type)
150 delete_blend_state(state, 0);
153 delete_sampler_state(state, 0);
155 case CSO_DEPTH_STENCIL_ALPHA:
156 delete_depth_stencil_state(state, 0);
159 delete_rasterizer_state(state, 0);
161 case CSO_FRAGMENT_SHADER:
162 delete_fs_state(state, 0);
164 case CSO_VERTEX_SHADER:
165 delete_vs_state(state, 0);
168 delete_velements(state, 0);
177 static INLINE void sanitize_hash(struct cso_cache *sc,
178 struct cso_hash *hash,
179 enum cso_cache_type type,
183 sc->sanitize_cb(hash, type, max_size, sc->sanitize_data);
187 static INLINE void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
188 int max_size, void *user_data)
190 /* if we're approach the maximum size, remove fourth of the entries
191 * otherwise every subsequent call will go through the same */
192 int hash_size = cso_hash_size(hash);
193 int max_entries = (max_size > hash_size) ? max_size : hash_size;
194 int to_remove = (max_size < max_entries) * max_entries/4;
195 if (hash_size > max_size)
196 to_remove += hash_size - max_size;
198 /*remove elements until we're good */
199 /*fixme: currently we pick the nodes to remove at random*/
200 struct cso_hash_iter iter = cso_hash_first_node(hash);
201 void *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
202 delete_cso(cso, type);
208 cso_insert_state(struct cso_cache *sc,
209 unsigned hash_key, enum cso_cache_type type,
212 struct cso_hash *hash = _cso_hash_for_type(sc, type);
213 sanitize_hash(sc, hash, type, sc->max_size);
215 return cso_hash_insert(hash, hash_key, state);
219 cso_find_state(struct cso_cache *sc,
220 unsigned hash_key, enum cso_cache_type type)
222 struct cso_hash *hash = _cso_hash_for_type(sc, type);
224 return cso_hash_find(hash, hash_key);
228 void *cso_hash_find_data_from_template( struct cso_hash *hash,
233 struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
234 while (!cso_hash_iter_is_null(iter)) {
235 void *iter_data = cso_hash_iter_data(iter);
236 if (!memcmp(iter_data, templ, size)) {
241 iter = cso_hash_iter_next(iter);
247 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
248 unsigned hash_key, enum cso_cache_type type,
249 void *templ, unsigned size)
251 struct cso_hash_iter iter = cso_find_state(sc, hash_key, type);
252 while (!cso_hash_iter_is_null(iter)) {
253 void *iter_data = cso_hash_iter_data(iter);
254 if (!memcmp(iter_data, templ, size))
256 iter = cso_hash_iter_next(iter);
261 void * cso_take_state(struct cso_cache *sc,
262 unsigned hash_key, enum cso_cache_type type)
264 struct cso_hash *hash = _cso_hash_for_type(sc, type);
265 return cso_hash_take(hash, hash_key);
268 struct cso_cache *cso_cache_create(void)
270 struct cso_cache *sc = MALLOC_STRUCT(cso_cache);
276 for (i = 0; i < CSO_CACHE_MAX; i++)
277 sc->hashes[i] = cso_hash_create();
279 sc->sanitize_cb = sanitize_cb;
280 sc->sanitize_data = 0;
285 void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
286 cso_state_callback func, void *user_data)
288 struct cso_hash *hash = _cso_hash_for_type(sc, type);
289 struct cso_hash_iter iter;
291 iter = cso_hash_first_node(hash);
292 while (!cso_hash_iter_is_null(iter)) {
293 void *state = cso_hash_iter_data(iter);
294 iter = cso_hash_iter_next(iter);
296 func(state, user_data);
301 void cso_cache_delete(struct cso_cache *sc)
309 /* delete driver data */
310 cso_for_each_state(sc, CSO_BLEND, delete_blend_state, 0);
311 cso_for_each_state(sc, CSO_DEPTH_STENCIL_ALPHA, delete_depth_stencil_state, 0);
312 cso_for_each_state(sc, CSO_FRAGMENT_SHADER, delete_fs_state, 0);
313 cso_for_each_state(sc, CSO_VERTEX_SHADER, delete_vs_state, 0);
314 cso_for_each_state(sc, CSO_RASTERIZER, delete_rasterizer_state, 0);
315 cso_for_each_state(sc, CSO_SAMPLER, delete_sampler_state, 0);
316 cso_for_each_state(sc, CSO_VELEMENTS, delete_velements, 0);
318 for (i = 0; i < CSO_CACHE_MAX; i++)
319 cso_hash_delete(sc->hashes[i]);
324 void cso_set_maximum_cache_size(struct cso_cache *sc, int number)
328 sc->max_size = number;
330 for (i = 0; i < CSO_CACHE_MAX; i++)
331 sanitize_hash(sc, sc->hashes[i], i, sc->max_size);
334 int cso_maximum_cache_size(const struct cso_cache *sc)
339 void cso_cache_set_sanitize_callback(struct cso_cache *sc,
340 cso_sanitize_callback cb,
343 sc->sanitize_cb = cb;
344 sc->sanitize_data = user_data;