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_cache_delete(struct cso_cache
*sc
)
196 cso_hash_delete(sc
->blend_hash
);
197 cso_hash_delete(sc
->sampler_hash
);
198 cso_hash_delete(sc
->depth_stencil_hash
);
199 cso_hash_delete(sc
->rasterizer_hash
);
200 cso_hash_delete(sc
->fs_hash
);
201 cso_hash_delete(sc
->vs_hash
);