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 "cso_cache.h"
35 static unsigned hash_key(const void *key
, unsigned key_size
)
37 unsigned *ikey
= (unsigned *)key
;
40 assert(key_size
% 4 == 0);
42 /* I'm sure this can be improved on:
44 for (i
= 0; i
< key_size
/4; i
++)
50 static unsigned hash_key(const unsigned char *p
, int n
)
57 if ((g
= (h
& 0xf0000000)) != 0)
65 unsigned cso_construct_key(void *item
, int item_size
)
67 return hash_key((item
), item_size
);
70 static struct cso_hash
*_cso_hash_for_type(struct cso_cache
*sc
, enum cso_cache_type type
)
72 struct cso_hash
*hash
= 0;
76 hash
= sc
->blend_hash
;
79 hash
= sc
->sampler_hash
;
81 case CSO_DEPTH_STENCIL_ALPHA
:
82 hash
= sc
->depth_stencil_hash
;
85 hash
= sc
->rasterizer_hash
;
87 case CSO_FRAGMENT_SHADER
:
90 case CSO_VERTEX_SHADER
:
98 static int _cso_size_for_type(enum cso_cache_type type
)
102 return sizeof(struct pipe_blend_state
);
104 return sizeof(struct pipe_sampler_state
);
105 case CSO_DEPTH_STENCIL_ALPHA
:
106 return sizeof(struct pipe_depth_stencil_alpha_state
);
108 return sizeof(struct pipe_rasterizer_state
);
109 case CSO_FRAGMENT_SHADER
:
110 return sizeof(struct pipe_shader_state
);
111 case CSO_VERTEX_SHADER
:
112 return sizeof(struct pipe_shader_state
);
118 cso_insert_state(struct cso_cache
*sc
,
119 unsigned hash_key
, enum cso_cache_type type
,
122 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
123 return cso_hash_insert(hash
, hash_key
, state
);
127 cso_find_state(struct cso_cache
*sc
,
128 unsigned hash_key
, enum cso_cache_type type
)
130 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
132 return cso_hash_find(hash
, hash_key
);
136 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
141 struct cso_hash_iter iter
= cso_hash_find(hash
, hash_key
);
142 while (!cso_hash_iter_is_null(iter
)) {
143 void *iter_data
= cso_hash_iter_data(iter
);
144 if (!memcmp(iter_data
, templ
, size
)) {
145 /* Return the payload:
147 return (unsigned char *)iter_data
+ size
;
149 iter
= cso_hash_iter_next(iter
);
155 struct cso_hash_iter
cso_find_state_template(struct cso_cache
*sc
,
156 unsigned hash_key
, enum cso_cache_type type
,
159 struct cso_hash_iter iter
= cso_find_state(sc
, hash_key
, type
);
160 int size
= _cso_size_for_type(type
);
161 while (!cso_hash_iter_is_null(iter
)) {
162 void *iter_data
= cso_hash_iter_data(iter
);
163 if (!memcmp(iter_data
, templ
, size
))
165 iter
= cso_hash_iter_next(iter
);
170 void * cso_take_state(struct cso_cache
*sc
,
171 unsigned hash_key
, enum cso_cache_type type
)
173 struct cso_hash
*hash
= _cso_hash_for_type(sc
, type
);
174 return cso_hash_take(hash
, hash_key
);
177 struct cso_cache
*cso_cache_create(void)
179 struct cso_cache
*sc
= malloc(sizeof(struct cso_cache
));
181 sc
->blend_hash
= cso_hash_create();
182 sc
->sampler_hash
= cso_hash_create();
183 sc
->depth_stencil_hash
= cso_hash_create();
184 sc
->rasterizer_hash
= cso_hash_create();
185 sc
->fs_hash
= cso_hash_create();
186 sc
->vs_hash
= cso_hash_create();
191 void cso_cache_delete(struct cso_cache
*sc
)
194 cso_hash_delete(sc
->blend_hash
);
195 cso_hash_delete(sc
->sampler_hash
);
196 cso_hash_delete(sc
->depth_stencil_hash
);
197 cso_hash_delete(sc
->rasterizer_hash
);
198 cso_hash_delete(sc
->fs_hash
);
199 cso_hash_delete(sc
->vs_hash
);