Code reorganization: placeholder for state-trackers.
[mesa.git] / src / gallium / aux / 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 "cso_cache.h"
32 #include "cso_hash.h"
33
34 #if 1
35 static unsigned hash_key(const void *key, unsigned key_size)
36 {
37 unsigned *ikey = (unsigned *)key;
38 unsigned hash = 0, i;
39
40 assert(key_size % 4 == 0);
41
42 /* I'm sure this can be improved on:
43 */
44 for (i = 0; i < key_size/4; i++)
45 hash ^= ikey[i];
46
47 return hash;
48 }
49 #else
50 static unsigned hash_key(const unsigned char *p, int n)
51 {
52 unsigned h = 0;
53 unsigned g;
54
55 while (n--) {
56 h = (h << 4) + *p++;
57 if ((g = (h & 0xf0000000)) != 0)
58 h ^= g >> 23;
59 h &= ~g;
60 }
61 return h;
62 }
63 #endif
64
65 unsigned cso_construct_key(void *item, int item_size)
66 {
67 return hash_key((item), item_size);
68 }
69
70 static struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type)
71 {
72 struct cso_hash *hash = 0;
73
74 switch(type) {
75 case CSO_BLEND:
76 hash = sc->blend_hash;
77 break;
78 case CSO_SAMPLER:
79 hash = sc->sampler_hash;
80 break;
81 case CSO_DEPTH_STENCIL_ALPHA:
82 hash = sc->depth_stencil_hash;
83 break;
84 case CSO_RASTERIZER:
85 hash = sc->rasterizer_hash;
86 break;
87 case CSO_FRAGMENT_SHADER:
88 hash = sc->fs_hash;
89 break;
90 case CSO_VERTEX_SHADER:
91 hash = sc->vs_hash;
92 break;
93 }
94
95 return hash;
96 }
97
98 static int _cso_size_for_type(enum cso_cache_type type)
99 {
100 switch(type) {
101 case CSO_BLEND:
102 return sizeof(struct pipe_blend_state);
103 case CSO_SAMPLER:
104 return sizeof(struct pipe_sampler_state);
105 case CSO_DEPTH_STENCIL_ALPHA:
106 return sizeof(struct pipe_depth_stencil_alpha_state);
107 case CSO_RASTERIZER:
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);
113 }
114 return 0;
115 }
116
117 struct cso_hash_iter
118 cso_insert_state(struct cso_cache *sc,
119 unsigned hash_key, enum cso_cache_type type,
120 void *state)
121 {
122 struct cso_hash *hash = _cso_hash_for_type(sc, type);
123 return cso_hash_insert(hash, hash_key, state);
124 }
125
126 struct cso_hash_iter
127 cso_find_state(struct cso_cache *sc,
128 unsigned hash_key, enum cso_cache_type type)
129 {
130 struct cso_hash *hash = _cso_hash_for_type(sc, type);
131
132 return cso_hash_find(hash, hash_key);
133 }
134
135 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
136 unsigned hash_key, enum cso_cache_type type,
137 void *templ)
138 {
139 struct cso_hash_iter iter = cso_find_state(sc, hash_key, type);
140 int size = _cso_size_for_type(type);
141 while (!cso_hash_iter_is_null(iter)) {
142 void *iter_data = cso_hash_iter_data(iter);
143 if (!memcmp(iter_data, templ, size))
144 return iter;
145 iter = cso_hash_iter_next(iter);
146 }
147 return iter;
148 }
149
150 void * cso_take_state(struct cso_cache *sc,
151 unsigned hash_key, enum cso_cache_type type)
152 {
153 struct cso_hash *hash = _cso_hash_for_type(sc, type);
154 return cso_hash_take(hash, hash_key);
155 }
156
157 struct cso_cache *cso_cache_create(void)
158 {
159 struct cso_cache *sc = malloc(sizeof(struct cso_cache));
160
161 sc->blend_hash = cso_hash_create();
162 sc->sampler_hash = cso_hash_create();
163 sc->depth_stencil_hash = cso_hash_create();
164 sc->rasterizer_hash = cso_hash_create();
165 sc->fs_hash = cso_hash_create();
166 sc->vs_hash = cso_hash_create();
167
168 return sc;
169 }
170
171 void cso_cache_delete(struct cso_cache *sc)
172 {
173 assert(sc);
174 cso_hash_delete(sc->blend_hash);
175 cso_hash_delete(sc->sampler_hash);
176 cso_hash_delete(sc->depth_stencil_hash);
177 cso_hash_delete(sc->rasterizer_hash);
178 cso_hash_delete(sc->fs_hash);
179 cso_hash_delete(sc->vs_hash);
180 free(sc);
181 }