i965: Remove dead entrypoints to state cache, rename the one that's left.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_state_cache.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 /** @file brw_state_cache.c
33 *
34 * This file implements a simple static state cache for 965. The
35 * consumers can query the hash table of state using a cache_id,
36 * opaque key data, and receive the corresponding state buffer object
37 * of state (plus associated auxiliary data) in return. Objects in
38 * the cache may not have relocations (pointers to other BOs) in them.
39 *
40 * The inner workings are a simple hash table based on a CRC of the
41 * key data.
42 *
43 * Replacement is not implemented. Instead, when the cache gets too
44 * big we throw out all of the cache data and let it get regenerated.
45 */
46
47 #include "main/imports.h"
48 #include "brw_state.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_STATE
51
52 static GLuint
53 hash_key(struct brw_cache_item *item)
54 {
55 GLuint *ikey = (GLuint *)item->key;
56 GLuint hash = item->cache_id, i;
57
58 assert(item->key_size % 4 == 0);
59
60 /* I'm sure this can be improved on:
61 */
62 for (i = 0; i < item->key_size/4; i++) {
63 hash ^= ikey[i];
64 hash = (hash << 5) | (hash >> 27);
65 }
66
67 return hash;
68 }
69
70
71 /**
72 * Marks a new buffer as being chosen for the given cache id.
73 */
74 static void
75 update_cache_last(struct brw_cache *cache, enum brw_cache_id cache_id,
76 drm_intel_bo *bo)
77 {
78 if (bo == cache->last_bo[cache_id])
79 return; /* no change */
80
81 drm_intel_bo_unreference(cache->last_bo[cache_id]);
82 cache->last_bo[cache_id] = bo;
83 drm_intel_bo_reference(cache->last_bo[cache_id]);
84 cache->brw->state.dirty.cache |= 1 << cache_id;
85 }
86
87 static int
88 brw_cache_item_equals(const struct brw_cache_item *a,
89 const struct brw_cache_item *b)
90 {
91 return a->cache_id == b->cache_id &&
92 a->hash == b->hash &&
93 a->key_size == b->key_size &&
94 (memcmp(a->key, b->key, a->key_size) == 0);
95 }
96
97 static struct brw_cache_item *
98 search_cache(struct brw_cache *cache, GLuint hash,
99 struct brw_cache_item *lookup)
100 {
101 struct brw_cache_item *c;
102
103 #if 0
104 int bucketcount = 0;
105
106 for (c = cache->items[hash % cache->size]; c; c = c->next)
107 bucketcount++;
108
109 fprintf(stderr, "bucket %d/%d = %d/%d items\n", hash % cache->size,
110 cache->size, bucketcount, cache->n_items);
111 #endif
112
113 for (c = cache->items[hash % cache->size]; c; c = c->next) {
114 if (brw_cache_item_equals(lookup, c))
115 return c;
116 }
117
118 return NULL;
119 }
120
121
122 static void
123 rehash(struct brw_cache *cache)
124 {
125 struct brw_cache_item **items;
126 struct brw_cache_item *c, *next;
127 GLuint size, i;
128
129 size = cache->size * 3;
130 items = (struct brw_cache_item**) calloc(1, size * sizeof(*items));
131
132 for (i = 0; i < cache->size; i++)
133 for (c = cache->items[i]; c; c = next) {
134 next = c->next;
135 c->next = items[c->hash % size];
136 items[c->hash % size] = c;
137 }
138
139 FREE(cache->items);
140 cache->items = items;
141 cache->size = size;
142 }
143
144
145 /**
146 * Returns the buffer object matching cache_id and key, or NULL.
147 */
148 drm_intel_bo *
149 brw_search_cache(struct brw_cache *cache,
150 enum brw_cache_id cache_id,
151 const void *key, GLuint key_size,
152 void *aux_return)
153 {
154 struct brw_cache_item *item;
155 struct brw_cache_item lookup;
156 GLuint hash;
157
158 lookup.cache_id = cache_id;
159 lookup.key = key;
160 lookup.key_size = key_size;
161 hash = hash_key(&lookup);
162 lookup.hash = hash;
163
164 item = search_cache(cache, hash, &lookup);
165
166 if (item == NULL)
167 return NULL;
168
169 if (aux_return)
170 *(void **)aux_return = (void *)((char *)item->key + item->key_size);
171
172 update_cache_last(cache, cache_id, item->bo);
173
174 drm_intel_bo_reference(item->bo);
175 return item->bo;
176 }
177
178
179 drm_intel_bo *
180 brw_upload_cache(struct brw_cache *cache,
181 enum brw_cache_id cache_id,
182 const void *key,
183 GLuint key_size,
184 const void *data,
185 GLuint data_size,
186 const void *aux,
187 GLuint aux_size,
188 void *aux_return)
189 {
190 struct brw_cache_item *item = CALLOC_STRUCT(brw_cache_item);
191 GLuint hash;
192 void *tmp;
193 drm_intel_bo *bo;
194
195 item->cache_id = cache_id;
196 item->key = key;
197 item->key_size = key_size;
198 hash = hash_key(item);
199 item->hash = hash;
200
201 /* Create the buffer object to contain the data */
202 bo = drm_intel_bo_alloc(cache->brw->intel.bufmgr,
203 cache->name[cache_id], data_size, 1 << 6);
204
205
206 /* Set up the memory containing the key and aux_data */
207 tmp = malloc(key_size + aux_size);
208
209 memcpy(tmp, key, key_size);
210 memcpy(tmp + key_size, aux, aux_size);
211
212 item->key = tmp;
213
214 item->bo = bo;
215 drm_intel_bo_reference(bo);
216
217 if (cache->n_items > cache->size * 1.5)
218 rehash(cache);
219
220 hash %= cache->size;
221 item->next = cache->items[hash];
222 cache->items[hash] = item;
223 cache->n_items++;
224
225 if (aux_return) {
226 *(void **)aux_return = (void *)((char *)item->key + item->key_size);
227 }
228
229 DBG("upload %s: %d bytes to cache id %d\n",
230 cache->name[cache_id],
231 data_size, cache_id);
232
233 /* Copy data to the buffer */
234 drm_intel_bo_subdata(bo, 0, data_size, data);
235
236 update_cache_last(cache, cache_id, bo);
237
238 return bo;
239 }
240
241 static void
242 brw_init_cache_id(struct brw_cache *cache,
243 const char *name,
244 enum brw_cache_id id)
245 {
246 cache->name[id] = strdup(name);
247 }
248
249
250 void
251 brw_init_caches(struct brw_context *brw)
252 {
253 struct brw_cache *cache = &brw->cache;
254
255 cache->brw = brw;
256
257 cache->size = 7;
258 cache->n_items = 0;
259 cache->items = (struct brw_cache_item **)
260 calloc(1, cache->size * sizeof(struct brw_cache_item));
261
262 brw_init_cache_id(cache, "CC_VP", BRW_CC_VP);
263 brw_init_cache_id(cache, "CC_UNIT", BRW_CC_UNIT);
264 brw_init_cache_id(cache, "WM_PROG", BRW_WM_PROG);
265 brw_init_cache_id(cache, "SAMPLER", BRW_SAMPLER);
266 brw_init_cache_id(cache, "WM_UNIT", BRW_WM_UNIT);
267 brw_init_cache_id(cache, "SF_PROG", BRW_SF_PROG);
268 brw_init_cache_id(cache, "SF_VP", BRW_SF_VP);
269
270 brw_init_cache_id(cache, "SF_UNIT", BRW_SF_UNIT);
271
272 brw_init_cache_id(cache, "VS_UNIT", BRW_VS_UNIT);
273
274 brw_init_cache_id(cache, "VS_PROG", BRW_VS_PROG);
275
276 brw_init_cache_id(cache, "CLIP_UNIT", BRW_CLIP_UNIT);
277
278 brw_init_cache_id(cache, "CLIP_PROG", BRW_CLIP_PROG);
279 brw_init_cache_id(cache, "CLIP_VP", BRW_CLIP_VP);
280
281 brw_init_cache_id(cache, "GS_UNIT", BRW_GS_UNIT);
282
283 brw_init_cache_id(cache, "GS_PROG", BRW_GS_PROG);
284 brw_init_cache_id(cache, "BLEND_STATE", BRW_BLEND_STATE);
285 brw_init_cache_id(cache, "COLOR_CALC_STATE", BRW_COLOR_CALC_STATE);
286 brw_init_cache_id(cache, "DEPTH_STENCIL_STATE", BRW_DEPTH_STENCIL_STATE);
287 }
288
289 static void
290 brw_clear_cache(struct brw_context *brw, struct brw_cache *cache)
291 {
292 struct brw_cache_item *c, *next;
293 GLuint i;
294
295 DBG("%s\n", __FUNCTION__);
296
297 for (i = 0; i < cache->size; i++) {
298 for (c = cache->items[i]; c; c = next) {
299 next = c->next;
300 drm_intel_bo_unreference(c->bo);
301 free((void *)c->key);
302 free(c);
303 }
304 cache->items[i] = NULL;
305 }
306
307 cache->n_items = 0;
308
309 brw->state.dirty.mesa |= ~0;
310 brw->state.dirty.brw |= ~0;
311 brw->state.dirty.cache |= ~0;
312 }
313
314 void
315 brw_state_cache_check_size(struct brw_context *brw)
316 {
317 /* un-tuned guess. Each object is generally a page, so 1000 of them is 4 MB of
318 * state cache.
319 */
320 if (brw->cache.n_items > 1000)
321 brw_clear_cache(brw, &brw->cache);
322 }
323
324
325 static void
326 brw_destroy_cache(struct brw_context *brw, struct brw_cache *cache)
327 {
328 GLuint i;
329
330 DBG("%s\n", __FUNCTION__);
331
332 brw_clear_cache(brw, cache);
333 for (i = 0; i < BRW_MAX_CACHE; i++) {
334 drm_intel_bo_unreference(cache->last_bo[i]);
335 free(cache->name[i]);
336 }
337 free(cache->items);
338 cache->items = NULL;
339 cache->size = 0;
340 }
341
342
343 void
344 brw_destroy_caches(struct brw_context *brw)
345 {
346 brw_destroy_cache(brw, &brw->cache);
347 }