1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <zackr@vmware.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 return &sc
->hashes
[type
];
88 static void delete_blend_state(void *state
, UNUSED
void *data
)
90 struct cso_blend
*cso
= (struct cso_blend
*)state
;
91 if (cso
->delete_state
)
92 cso
->delete_state(cso
->context
, cso
->data
);
96 static void delete_depth_stencil_state(void *state
, UNUSED
void *data
)
98 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
99 if (cso
->delete_state
)
100 cso
->delete_state(cso
->context
, cso
->data
);
104 static void delete_sampler_state(void *state
, UNUSED
void *data
)
106 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
107 if (cso
->delete_state
)
108 cso
->delete_state(cso
->context
, cso
->data
);
112 static void delete_rasterizer_state(void *state
, UNUSED
void *data
)
114 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
115 if (cso
->delete_state
)
116 cso
->delete_state(cso
->context
, cso
->data
);
120 static void delete_velements(void *state
, UNUSED
void *data
)
122 struct cso_velements
*cso
= (struct cso_velements
*)state
;
123 if (cso
->delete_state
)
124 cso
->delete_state(cso
->context
, cso
->data
);
128 static inline void delete_cso(void *state
, enum cso_cache_type type
)
132 delete_blend_state(state
, 0);
135 delete_sampler_state(state
, 0);
137 case CSO_DEPTH_STENCIL_ALPHA
:
138 delete_depth_stencil_state(state
, 0);
141 delete_rasterizer_state(state
, 0);
144 delete_velements(state
, 0);
153 static inline void sanitize_hash(struct cso_cache
*sc
,
154 struct cso_hash
*hash
,
155 enum cso_cache_type type
,
159 sc
->sanitize_cb(hash
, type
, max_size
, sc
->sanitize_data
);
163 static inline void sanitize_cb(struct cso_hash
*hash
, enum cso_cache_type type
,
164 int max_size
, UNUSED
void *user_data
)
166 /* if we're approach the maximum size, remove fourth of the entries
167 * otherwise every subsequent call will go through the same */
168 int hash_size
= cso_hash_size(hash
);
169 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
170 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
171 if (hash_size
> max_size
)
172 to_remove
+= hash_size
- max_size
;
174 /*remove elements until we're good */
175 /*fixme: currently we pick the nodes to remove at random*/
176 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
177 void *cso
= cso_hash_take(hash
, cso_hash_iter_key(iter
));
178 delete_cso(cso
, type
);
184 cso_insert_state(struct cso_cache
*sc
,
185 unsigned hash_key
, enum cso_cache_type type
,
188 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
189 sanitize_hash(sc
, hash
, type
, sc
->max_size
);
191 return cso_hash_insert(hash
, hash_key
, state
);
195 cso_find_state(struct cso_cache
*sc
,
196 unsigned hash_key
, enum cso_cache_type type
)
198 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
200 return cso_hash_find(hash
, hash_key
);
204 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
209 struct cso_hash_iter iter
= cso_hash_find(hash
, hash_key
);
210 while (!cso_hash_iter_is_null(iter
)) {
211 void *iter_data
= cso_hash_iter_data(iter
);
212 if (!memcmp(iter_data
, templ
, size
)) {
217 iter
= cso_hash_iter_next(iter
);
223 struct cso_hash_iter
cso_find_state_template(struct cso_cache
*sc
,
224 unsigned hash_key
, enum cso_cache_type type
,
225 void *templ
, unsigned size
)
227 struct cso_hash_iter iter
= cso_find_state(sc
, hash_key
, type
);
228 while (!cso_hash_iter_is_null(iter
)) {
229 void *iter_data
= cso_hash_iter_data(iter
);
230 if (!memcmp(iter_data
, templ
, size
))
232 iter
= cso_hash_iter_next(iter
);
237 void * cso_take_state(struct cso_cache
*sc
,
238 unsigned hash_key
, enum cso_cache_type type
)
240 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
241 return cso_hash_take(hash
, hash_key
);
244 struct cso_cache
*cso_cache_create(void)
246 struct cso_cache
*sc
= MALLOC_STRUCT(cso_cache
);
252 for (i
= 0; i
< CSO_CACHE_MAX
; i
++)
253 cso_hash_init(&sc
->hashes
[i
]);
255 sc
->sanitize_cb
= sanitize_cb
;
256 sc
->sanitize_data
= 0;
261 void cso_for_each_state(struct cso_cache
*sc
, enum cso_cache_type type
,
262 cso_state_callback func
, void *user_data
)
264 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
265 struct cso_hash_iter iter
;
267 iter
= cso_hash_first_node(hash
);
268 while (!cso_hash_iter_is_null(iter
)) {
269 void *state
= cso_hash_iter_data(iter
);
270 iter
= cso_hash_iter_next(iter
);
272 func(state
, user_data
);
277 void cso_cache_delete(struct cso_cache
*sc
)
285 /* delete driver data */
286 cso_for_each_state(sc
, CSO_BLEND
, delete_blend_state
, 0);
287 cso_for_each_state(sc
, CSO_DEPTH_STENCIL_ALPHA
, delete_depth_stencil_state
, 0);
288 cso_for_each_state(sc
, CSO_RASTERIZER
, delete_rasterizer_state
, 0);
289 cso_for_each_state(sc
, CSO_SAMPLER
, delete_sampler_state
, 0);
290 cso_for_each_state(sc
, CSO_VELEMENTS
, delete_velements
, 0);
292 for (i
= 0; i
< CSO_CACHE_MAX
; i
++)
293 cso_hash_deinit(&sc
->hashes
[i
]);
298 void cso_set_maximum_cache_size(struct cso_cache
*sc
, int number
)
302 sc
->max_size
= number
;
304 for (i
= 0; i
< CSO_CACHE_MAX
; i
++)
305 sanitize_hash(sc
, &sc
->hashes
[i
], i
, sc
->max_size
);
308 int cso_maximum_cache_size(const struct cso_cache
*sc
)
313 void cso_cache_set_sanitize_callback(struct cso_cache
*sc
,
314 cso_sanitize_callback cb
,
317 sc
->sanitize_cb
= cb
;
318 sc
->sanitize_data
= user_data
;