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"
33 #include "cso_cache.h"
37 static unsigned hash_key(const void *key
, unsigned key_size
)
39 unsigned *ikey
= (unsigned *)key
;
42 assert(key_size
% 4 == 0);
44 /* I'm sure this can be improved on:
46 for (i
= 0; i
< key_size
/4; i
++)
52 static unsigned hash_key(const unsigned char *p
, int n
)
59 if ((g
= (h
& 0xf0000000)) != 0)
67 unsigned cso_construct_key(void *item
, int item_size
)
69 return hash_key((item
), item_size
);
72 static struct cso_hash
*_cso_hash_for_type(struct cso_cache
*sc
, enum cso_cache_type type
)
74 struct cso_hash
*hash
= 0;
78 hash
= sc
->blend_hash
;
81 hash
= sc
->sampler_hash
;
83 case CSO_DEPTH_STENCIL_ALPHA
:
84 hash
= sc
->depth_stencil_hash
;
87 hash
= sc
->rasterizer_hash
;
89 case CSO_FRAGMENT_SHADER
:
92 case CSO_VERTEX_SHADER
:
100 static int _cso_size_for_type(enum cso_cache_type type
)
104 return sizeof(struct pipe_blend_state
);
106 return sizeof(struct pipe_sampler_state
);
107 case CSO_DEPTH_STENCIL_ALPHA
:
108 return sizeof(struct pipe_depth_stencil_alpha_state
);
110 return sizeof(struct pipe_rasterizer_state
);
111 case CSO_FRAGMENT_SHADER
:
112 return sizeof(struct pipe_shader_state
);
113 case CSO_VERTEX_SHADER
:
114 return sizeof(struct pipe_shader_state
);
120 cso_insert_state(struct cso_cache
*sc
,
121 unsigned hash_key
, enum cso_cache_type type
,
124 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
125 return cso_hash_insert(hash
, hash_key
, state
);
129 cso_find_state(struct cso_cache
*sc
,
130 unsigned hash_key
, enum cso_cache_type type
)
132 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
134 return cso_hash_find(hash
, hash_key
);
138 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
143 struct cso_hash_iter iter
= cso_hash_find(hash
, hash_key
);
144 while (!cso_hash_iter_is_null(iter
)) {
145 void *iter_data
= cso_hash_iter_data(iter
);
146 if (!memcmp(iter_data
, templ
, size
)) {
147 /* Return the payload:
149 return (unsigned char *)iter_data
+ size
;
151 iter
= cso_hash_iter_next(iter
);
157 struct cso_hash_iter
cso_find_state_template(struct cso_cache
*sc
,
158 unsigned hash_key
, enum cso_cache_type type
,
161 struct cso_hash_iter iter
= cso_find_state(sc
, hash_key
, type
);
162 int size
= _cso_size_for_type(type
);
163 while (!cso_hash_iter_is_null(iter
)) {
164 void *iter_data
= cso_hash_iter_data(iter
);
165 if (!memcmp(iter_data
, templ
, size
))
167 iter
= cso_hash_iter_next(iter
);
172 void * cso_take_state(struct cso_cache
*sc
,
173 unsigned hash_key
, enum cso_cache_type type
)
175 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
176 return cso_hash_take(hash
, hash_key
);
179 struct cso_cache
*cso_cache_create(void)
181 struct cso_cache
*sc
= MALLOC_STRUCT(cso_cache
);
183 sc
->blend_hash
= cso_hash_create();
184 sc
->sampler_hash
= cso_hash_create();
185 sc
->depth_stencil_hash
= cso_hash_create();
186 sc
->rasterizer_hash
= cso_hash_create();
187 sc
->fs_hash
= cso_hash_create();
188 sc
->vs_hash
= cso_hash_create();
193 void cso_for_each_state(struct cso_cache
*sc
, enum cso_cache_type type
,
194 void (*func
)(void *state
, void *user_data
), void *user_data
)
196 struct cso_hash
*hash
= 0;
197 struct cso_hash_iter iter
;
201 hash
= sc
->blend_hash
;
204 hash
= sc
->sampler_hash
;
206 case CSO_DEPTH_STENCIL_ALPHA
:
207 hash
= sc
->depth_stencil_hash
;
210 hash
= sc
->rasterizer_hash
;
212 case CSO_FRAGMENT_SHADER
:
215 case CSO_VERTEX_SHADER
:
220 iter
= cso_hash_first_node(hash
);
221 while (!cso_hash_iter_is_null(iter
)) {
222 void *state
= cso_hash_iter_data(iter
);
224 func(state
, user_data
);
226 iter
= cso_hash_iter_next(iter
);
230 static void delete_blend_state(void *state
, void *user_data
)
232 struct cso_blend
*cso
= (struct cso_blend
*)state
;
233 if (cso
->delete_state
&& cso
->data
!= &cso
->state
)
234 cso
->delete_state(cso
->context
, cso
->data
);
237 static void delete_depth_stencil_state(void *state
, void *pipe
)
239 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
240 if (cso
->delete_state
&& cso
->data
!= &cso
->state
)
241 cso
->delete_state(cso
->context
, cso
->data
);
244 static void delete_sampler_state(void *state
, void *pipe
)
246 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
247 if (cso
->delete_state
&& cso
->data
!= &cso
->state
)
248 cso
->delete_state(cso
->context
, cso
->data
);
251 static void delete_rasterizer_state(void *state
, void *pipe
)
253 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
254 if (cso
->delete_state
&& cso
->data
!= &cso
->state
)
255 cso
->delete_state(cso
->context
, cso
->data
);
258 static void delete_fs_state(void *state
, void *pipe
)
260 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
261 if (cso
->delete_state
&& cso
->data
!= &cso
->state
)
262 cso
->delete_state(cso
->context
, cso
->data
);
265 static void delete_vs_state(void *state
, void *pipe
)
267 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
268 if (cso
->delete_state
&& cso
->data
!= &cso
->state
)
269 cso
->delete_state(cso
->context
, cso
->data
);
272 void cso_cache_delete(struct cso_cache
*sc
)
275 /* delete driver data */
276 cso_for_each_state(sc
, CSO_BLEND
, delete_blend_state
, 0);
277 cso_for_each_state(sc
, CSO_DEPTH_STENCIL_ALPHA
, delete_depth_stencil_state
, 0);
278 cso_for_each_state(sc
, CSO_FRAGMENT_SHADER
, delete_fs_state
, 0);
279 cso_for_each_state(sc
, CSO_VERTEX_SHADER
, delete_vs_state
, 0);
280 cso_for_each_state(sc
, CSO_RASTERIZER
, delete_rasterizer_state
, 0);
281 cso_for_each_state(sc
, CSO_SAMPLER
, delete_sampler_state
, 0);
283 cso_hash_delete(sc
->blend_hash
);
284 cso_hash_delete(sc
->sampler_hash
);
285 cso_hash_delete(sc
->depth_stencil_hash
);
286 cso_hash_delete(sc
->rasterizer_hash
);
287 cso_hash_delete(sc
->fs_hash
);
288 cso_hash_delete(sc
->vs_hash
);