2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
32 /** @file brw_state_cache.c
34 * This file implements a simple static state cache for 965. The consumers
35 * can query the hash table of state using a cache_id, opaque key data,
36 * and list of buffers that will be used in relocations, and receive the
37 * corresponding state buffer object of state (plus associated auxiliary
40 * The inner workings are a simple hash table based on a CRC of the key data.
41 * The cache_id and relocation target buffers associated with the state
42 * buffer are included as auxiliary key data, but are not part of the hash
43 * value (this should be fixed, but will likely be fixed instead by making
44 * consumers use structured keys).
46 * Replacement is not implemented. Instead, when the cache gets too big, at
47 * a safe point (unlock) we throw out all of the cache data and let it
48 * regenerate for the next rendering operation.
50 * The reloc_buf pointers need to be included as key data, otherwise the
51 * non-unique values stuffed in the offset in key data through
52 * brw_cache_data() may result in successful probe for state buffers
53 * even when the buffer being referenced doesn't match. The result would be
54 * that the same state cache entry is used twice for different buffers,
55 * only one of the two buffers referenced gets put into the offset, and the
56 * incorrect program is run for the other instance.
59 #include "main/imports.h"
60 #include "brw_state.h"
61 #include "intel_batchbuffer.h"
64 #define FILE_DEBUG_FLAG DEBUG_STATE
67 hash_key(struct brw_cache_item
*item
)
69 GLuint
*ikey
= (GLuint
*)item
->key
;
70 GLuint hash
= item
->cache_id
, i
;
72 assert(item
->key_size
% 4 == 0);
74 /* I'm sure this can be improved on:
76 for (i
= 0; i
< item
->key_size
/4; i
++) {
78 hash
= (hash
<< 5) | (hash
>> 27);
81 /* Include the BO pointers as key data as well */
82 ikey
= (GLuint
*)item
->reloc_bufs
;
83 for (i
= 0; i
< item
->nr_reloc_bufs
* sizeof(drm_intel_bo
*) / 4; i
++) {
85 hash
= (hash
<< 5) | (hash
>> 27);
93 * Marks a new buffer as being chosen for the given cache id.
96 update_cache_last(struct brw_cache
*cache
, enum brw_cache_id cache_id
,
99 if (bo
== cache
->last_bo
[cache_id
])
100 return; /* no change */
102 drm_intel_bo_unreference(cache
->last_bo
[cache_id
]);
103 cache
->last_bo
[cache_id
] = bo
;
104 drm_intel_bo_reference(cache
->last_bo
[cache_id
]);
105 cache
->brw
->state
.dirty
.cache
|= 1 << cache_id
;
109 brw_cache_item_equals(const struct brw_cache_item
*a
,
110 const struct brw_cache_item
*b
)
112 return a
->cache_id
== b
->cache_id
&&
113 a
->hash
== b
->hash
&&
114 a
->key_size
== b
->key_size
&&
115 (memcmp(a
->key
, b
->key
, a
->key_size
) == 0) &&
116 a
->nr_reloc_bufs
== b
->nr_reloc_bufs
&&
117 (memcmp(a
->reloc_bufs
, b
->reloc_bufs
,
118 a
->nr_reloc_bufs
* sizeof(drm_intel_bo
*)) == 0);
121 static struct brw_cache_item
*
122 search_cache(struct brw_cache
*cache
, GLuint hash
,
123 struct brw_cache_item
*lookup
)
125 struct brw_cache_item
*c
;
130 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
)
133 fprintf(stderr
, "bucket %d/%d = %d/%d items\n", hash
% cache
->size
,
134 cache
->size
, bucketcount
, cache
->n_items
);
137 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
138 if (brw_cache_item_equals(lookup
, c
))
147 rehash(struct brw_cache
*cache
)
149 struct brw_cache_item
**items
;
150 struct brw_cache_item
*c
, *next
;
153 size
= cache
->size
* 3;
154 items
= (struct brw_cache_item
**) calloc(1, size
* sizeof(*items
));
156 for (i
= 0; i
< cache
->size
; i
++)
157 for (c
= cache
->items
[i
]; c
; c
= next
) {
159 c
->next
= items
[c
->hash
% size
];
160 items
[c
->hash
% size
] = c
;
164 cache
->items
= items
;
170 * Returns the buffer object matching cache_id and key, or NULL.
173 brw_search_cache(struct brw_cache
*cache
,
174 enum brw_cache_id cache_id
,
177 drm_intel_bo
**reloc_bufs
, GLuint nr_reloc_bufs
,
180 struct brw_cache_item
*item
;
181 struct brw_cache_item lookup
;
184 lookup
.cache_id
= cache_id
;
186 lookup
.key_size
= key_size
;
187 lookup
.reloc_bufs
= reloc_bufs
;
188 lookup
.nr_reloc_bufs
= nr_reloc_bufs
;
189 hash
= hash_key(&lookup
);
192 item
= search_cache(cache
, hash
, &lookup
);
198 *(void **)aux_return
= (void *)((char *)item
->key
+ item
->key_size
);
200 update_cache_last(cache
, cache_id
, item
->bo
);
202 drm_intel_bo_reference(item
->bo
);
208 brw_upload_cache_with_auxdata(struct brw_cache
*cache
,
209 enum brw_cache_id cache_id
,
212 drm_intel_bo
**reloc_bufs
,
213 GLuint nr_reloc_bufs
,
220 struct brw_cache_item
*item
= CALLOC_STRUCT(brw_cache_item
);
222 GLuint relocs_size
= nr_reloc_bufs
* sizeof(drm_intel_bo
*);
227 item
->cache_id
= cache_id
;
229 item
->key_size
= key_size
;
230 item
->reloc_bufs
= reloc_bufs
;
231 item
->nr_reloc_bufs
= nr_reloc_bufs
;
232 hash
= hash_key(item
);
235 /* Create the buffer object to contain the data */
236 bo
= drm_intel_bo_alloc(cache
->brw
->intel
.bufmgr
,
237 cache
->name
[cache_id
], data_size
, 1 << 6);
240 /* Set up the memory containing the key, aux_data, and reloc_bufs */
241 tmp
= malloc(key_size
+ aux_size
+ relocs_size
);
243 memcpy(tmp
, key
, key_size
);
244 memcpy(tmp
+ key_size
, aux
, aux_size
);
245 memcpy(tmp
+ key_size
+ aux_size
, reloc_bufs
, relocs_size
);
246 for (i
= 0; i
< nr_reloc_bufs
; i
++) {
247 if (reloc_bufs
[i
] != NULL
)
248 drm_intel_bo_reference(reloc_bufs
[i
]);
252 item
->reloc_bufs
= tmp
+ key_size
+ aux_size
;
255 drm_intel_bo_reference(bo
);
257 if (cache
->n_items
> cache
->size
* 1.5)
261 item
->next
= cache
->items
[hash
];
262 cache
->items
[hash
] = item
;
266 *(void **)aux_return
= (void *)((char *)item
->key
+ item
->key_size
);
269 DBG("upload %s: %d bytes to cache id %d\n",
270 cache
->name
[cache_id
],
271 data_size
, cache_id
);
273 /* Copy data to the buffer */
274 drm_intel_bo_subdata(bo
, 0, data_size
, data
);
276 update_cache_last(cache
, cache_id
, bo
);
282 brw_upload_cache(struct brw_cache
*cache
,
283 enum brw_cache_id cache_id
,
286 drm_intel_bo
**reloc_bufs
,
287 GLuint nr_reloc_bufs
,
291 return brw_upload_cache_with_auxdata(cache
, cache_id
,
293 reloc_bufs
, nr_reloc_bufs
,
300 * Wrapper around brw_cache_data_sz using the cache_id's canonical key size.
302 * If nr_reloc_bufs is nonzero, brw_search_cache()/brw_upload_cache() would be
303 * better to use, as the potentially changing offsets in the data-used-as-key
304 * will result in excessive cache misses.
306 * If aux data is involved, use search/upload instead.
310 brw_cache_data(struct brw_cache
*cache
,
311 enum brw_cache_id cache_id
,
316 struct brw_cache_item
*item
, lookup
;
319 lookup
.cache_id
= cache_id
;
321 lookup
.key_size
= data_size
;
322 lookup
.reloc_bufs
= NULL
;
323 lookup
.nr_reloc_bufs
= 0;
324 hash
= hash_key(&lookup
);
327 item
= search_cache(cache
, hash
, &lookup
);
329 update_cache_last(cache
, cache_id
, item
->bo
);
330 drm_intel_bo_reference(item
->bo
);
334 bo
= brw_upload_cache(cache
, cache_id
,
349 brw_init_cache_id(struct brw_cache
*cache
,
351 enum brw_cache_id id
)
353 cache
->name
[id
] = strdup(name
);
358 brw_init_non_surface_cache(struct brw_context
*brw
)
360 struct brw_cache
*cache
= &brw
->cache
;
366 cache
->items
= (struct brw_cache_item
**)
367 calloc(1, cache
->size
* sizeof(struct brw_cache_item
));
369 brw_init_cache_id(cache
, "CC_VP", BRW_CC_VP
);
370 brw_init_cache_id(cache
, "CC_UNIT", BRW_CC_UNIT
);
371 brw_init_cache_id(cache
, "WM_PROG", BRW_WM_PROG
);
372 brw_init_cache_id(cache
, "SAMPLER_DEFAULT_COLOR", BRW_SAMPLER_DEFAULT_COLOR
);
373 brw_init_cache_id(cache
, "SAMPLER", BRW_SAMPLER
);
374 brw_init_cache_id(cache
, "WM_UNIT", BRW_WM_UNIT
);
375 brw_init_cache_id(cache
, "SF_PROG", BRW_SF_PROG
);
376 brw_init_cache_id(cache
, "SF_VP", BRW_SF_VP
);
378 brw_init_cache_id(cache
, "SF_UNIT", BRW_SF_UNIT
);
380 brw_init_cache_id(cache
, "VS_UNIT", BRW_VS_UNIT
);
382 brw_init_cache_id(cache
, "VS_PROG", BRW_VS_PROG
);
384 brw_init_cache_id(cache
, "CLIP_UNIT", BRW_CLIP_UNIT
);
386 brw_init_cache_id(cache
, "CLIP_PROG", BRW_CLIP_PROG
);
387 brw_init_cache_id(cache
, "CLIP_VP", BRW_CLIP_VP
);
389 brw_init_cache_id(cache
, "GS_UNIT", BRW_GS_UNIT
);
391 brw_init_cache_id(cache
, "GS_PROG", BRW_GS_PROG
);
392 brw_init_cache_id(cache
, "BLEND_STATE", BRW_BLEND_STATE
);
393 brw_init_cache_id(cache
, "COLOR_CALC_STATE", BRW_COLOR_CALC_STATE
);
394 brw_init_cache_id(cache
, "DEPTH_STENCIL_STATE", BRW_DEPTH_STENCIL_STATE
);
398 brw_init_caches(struct brw_context
*brw
)
400 brw_init_non_surface_cache(brw
);
405 brw_clear_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
407 struct brw_cache_item
*c
, *next
;
410 DBG("%s\n", __FUNCTION__
);
412 for (i
= 0; i
< cache
->size
; i
++) {
413 for (c
= cache
->items
[i
]; c
; c
= next
) {
417 for (j
= 0; j
< c
->nr_reloc_bufs
; j
++)
418 drm_intel_bo_unreference(c
->reloc_bufs
[j
]);
419 drm_intel_bo_unreference(c
->bo
);
420 free((void *)c
->key
);
423 cache
->items
[i
] = NULL
;
428 brw
->state
.dirty
.mesa
|= ~0;
429 brw
->state
.dirty
.brw
|= ~0;
430 brw
->state
.dirty
.cache
|= ~0;
434 brw_state_cache_check_size(struct brw_context
*brw
)
436 DBG("%s (n_items=%d)\n", __FUNCTION__
, brw
->cache
.n_items
);
438 /* un-tuned guess. Each object is generally a page, so 1000 of them is 4 MB of
441 if (brw
->cache
.n_items
> 1000)
442 brw_clear_cache(brw
, &brw
->cache
);
447 brw_destroy_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
451 DBG("%s\n", __FUNCTION__
);
453 brw_clear_cache(brw
, cache
);
454 for (i
= 0; i
< BRW_MAX_CACHE
; i
++) {
455 drm_intel_bo_unreference(cache
->last_bo
[i
]);
456 free(cache
->name
[i
]);
465 brw_destroy_caches(struct brw_context
*brw
)
467 brw_destroy_cache(brw
, &brw
->cache
);