9c32e94124c41591fd3929d24a99e67d3a6f63dc
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /* Authors: Zack Rusin <zack@tungstengraphics.com>
29 */
30
31 #include "pipe/p_util.h"
32
33 #include "cso_cache.h"
34 #include "cso_hash.h"
35
36 #if 1
37 static unsigned hash_key(const void *key, unsigned key_size)
38 {
39 unsigned *ikey = (unsigned *)key;
40 unsigned hash = 0, i;
41
42 assert(key_size % 4 == 0);
43
44 /* I'm sure this can be improved on:
45 */
46 for (i = 0; i < key_size/4; i++)
47 hash ^= ikey[i];
48
49 return hash;
50 }
51 #else
52 static unsigned hash_key(const unsigned char *p, int n)
53 {
54 unsigned h = 0;
55 unsigned g;
56
57 while (n--) {
58 h = (h << 4) + *p++;
59 if ((g = (h & 0xf0000000)) != 0)
60 h ^= g >> 23;
61 h &= ~g;
62 }
63 return h;
64 }
65 #endif
66
67 unsigned cso_construct_key(void *item, int item_size)
68 {
69 return hash_key((item), item_size);
70 }
71
72 static struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type)
73 {
74 struct cso_hash *hash = 0;
75
76 switch(type) {
77 case CSO_BLEND:
78 hash = sc->blend_hash;
79 break;
80 case CSO_SAMPLER:
81 hash = sc->sampler_hash;
82 break;
83 case CSO_DEPTH_STENCIL_ALPHA:
84 hash = sc->depth_stencil_hash;
85 break;
86 case CSO_RASTERIZER:
87 hash = sc->rasterizer_hash;
88 break;
89 case CSO_FRAGMENT_SHADER:
90 hash = sc->fs_hash;
91 break;
92 case CSO_VERTEX_SHADER:
93 hash = sc->vs_hash;
94 break;
95 }
96
97 return hash;
98 }
99
100 static int _cso_size_for_type(enum cso_cache_type type)
101 {
102 switch(type) {
103 case CSO_BLEND:
104 return sizeof(struct pipe_blend_state);
105 case CSO_SAMPLER:
106 return sizeof(struct pipe_sampler_state);
107 case CSO_DEPTH_STENCIL_ALPHA:
108 return sizeof(struct pipe_depth_stencil_alpha_state);
109 case CSO_RASTERIZER:
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);
115 }
116 return 0;
117 }
118
119 struct cso_hash_iter
120 cso_insert_state(struct cso_cache *sc,
121 unsigned hash_key, enum cso_cache_type type,
122 void *state)
123 {
124 struct cso_hash *hash = _cso_hash_for_type(sc, type);
125 return cso_hash_insert(hash, hash_key, state);
126 }
127
128 struct cso_hash_iter
129 cso_find_state(struct cso_cache *sc,
130 unsigned hash_key, enum cso_cache_type type)
131 {
132 struct cso_hash *hash = _cso_hash_for_type(sc, type);
133
134 return cso_hash_find(hash, hash_key);
135 }
136
137
138 void *cso_hash_find_data_from_template( struct cso_hash *hash,
139 unsigned hash_key,
140 void *templ,
141 int size )
142 {
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:
148 */
149 return (unsigned char *)iter_data + size;
150 }
151 iter = cso_hash_iter_next(iter);
152 }
153 return NULL;
154 }
155
156
157 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
158 unsigned hash_key, enum cso_cache_type type,
159 void *templ)
160 {
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))
166 return iter;
167 iter = cso_hash_iter_next(iter);
168 }
169 return iter;
170 }
171
172 void * cso_take_state(struct cso_cache *sc,
173 unsigned hash_key, enum cso_cache_type type)
174 {
175 struct cso_hash *hash = _cso_hash_for_type(sc, type);
176 return cso_hash_take(hash, hash_key);
177 }
178
179 struct cso_cache *cso_cache_create(void)
180 {
181 struct cso_cache *sc = MALLOC_STRUCT(cso_cache);
182
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();
189
190 return sc;
191 }
192
193 void cso_cache_delete(struct cso_cache *sc)
194 {
195 assert(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);
202 FREE(sc);
203 }