2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics 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 <keithw@vmware.com>
32 /** @file brw_state_cache.c
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.
40 * The inner workings are a simple hash table based on a CRC of the
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.
47 #include "main/imports.h"
48 #include "intel_batchbuffer.h"
49 #include "brw_state.h"
54 #include "brw_program.h"
56 #define FILE_DEBUG_FLAG DEBUG_STATE
59 hash_key(struct brw_cache_item
*item
)
61 GLuint
*ikey
= (GLuint
*)item
->key
;
62 GLuint hash
= item
->cache_id
, i
;
64 assert(item
->key_size
% 4 == 0);
66 /* I'm sure this can be improved on:
68 for (i
= 0; i
< item
->key_size
/4; i
++) {
70 hash
= (hash
<< 5) | (hash
>> 27);
77 brw_cache_item_equals(const struct brw_cache_item
*a
,
78 const struct brw_cache_item
*b
)
80 return a
->cache_id
== b
->cache_id
&&
82 a
->key_size
== b
->key_size
&&
83 (memcmp(a
->key
, b
->key
, a
->key_size
) == 0);
86 static struct brw_cache_item
*
87 search_cache(struct brw_cache
*cache
, GLuint hash
,
88 struct brw_cache_item
*lookup
)
90 struct brw_cache_item
*c
;
95 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
)
98 fprintf(stderr
, "bucket %d/%d = %d/%d items\n", hash
% cache
->size
,
99 cache
->size
, bucketcount
, cache
->n_items
);
102 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
103 if (brw_cache_item_equals(lookup
, c
))
112 rehash(struct brw_cache
*cache
)
114 struct brw_cache_item
**items
;
115 struct brw_cache_item
*c
, *next
;
118 size
= cache
->size
* 3;
119 items
= calloc(size
, sizeof(*items
));
121 for (i
= 0; i
< cache
->size
; i
++)
122 for (c
= cache
->items
[i
]; c
; c
= next
) {
124 c
->next
= items
[c
->hash
% size
];
125 items
[c
->hash
% size
] = c
;
129 cache
->items
= items
;
135 * Returns the buffer object matching cache_id and key, or NULL.
138 brw_search_cache(struct brw_cache
*cache
,
139 enum brw_cache_id cache_id
,
140 const void *key
, GLuint key_size
,
141 uint32_t *inout_offset
, void *inout_aux
)
143 struct brw_context
*brw
= cache
->brw
;
144 struct brw_cache_item
*item
;
145 struct brw_cache_item lookup
;
148 lookup
.cache_id
= cache_id
;
150 lookup
.key_size
= key_size
;
151 hash
= hash_key(&lookup
);
154 item
= search_cache(cache
, hash
, &lookup
);
159 void *aux
= ((char *) item
->key
) + item
->key_size
;
161 if (item
->offset
!= *inout_offset
|| aux
!= *((void **) inout_aux
)) {
162 brw
->ctx
.NewDriverState
|= (1 << cache_id
);
163 *inout_offset
= item
->offset
;
164 *((void **) inout_aux
) = aux
;
171 brw_cache_new_bo(struct brw_cache
*cache
, uint32_t new_size
)
173 struct brw_context
*brw
= cache
->brw
;
174 drm_intel_bo
*new_bo
;
176 new_bo
= drm_intel_bo_alloc(brw
->bufmgr
, "program cache", new_size
, 64);
178 drm_intel_gem_bo_map_unsynchronized(new_bo
);
180 /* Copy any existing data that needs to be saved. */
181 if (cache
->next_offset
!= 0) {
183 memcpy(new_bo
->virtual, cache
->bo
->virtual, cache
->next_offset
);
185 drm_intel_bo_map(cache
->bo
, false);
186 drm_intel_bo_subdata(new_bo
, 0, cache
->next_offset
,
188 drm_intel_bo_unmap(cache
->bo
);
193 drm_intel_bo_unmap(cache
->bo
);
194 drm_intel_bo_unreference(cache
->bo
);
196 cache
->bo_used_by_gpu
= false;
198 /* Since we have a new BO in place, we need to signal the units
199 * that depend on it (state base address on gen5+, or unit state before).
201 brw
->ctx
.NewDriverState
|= BRW_NEW_PROGRAM_CACHE
;
205 * Attempts to find an item in the cache with identical data.
207 static const struct brw_cache_item
*
208 brw_lookup_prog(const struct brw_cache
*cache
,
209 enum brw_cache_id cache_id
,
210 const void *data
, unsigned data_size
)
212 const struct brw_context
*brw
= cache
->brw
;
214 const struct brw_cache_item
*item
;
216 for (i
= 0; i
< cache
->size
; i
++) {
217 for (item
= cache
->items
[i
]; item
; item
= item
->next
) {
220 if (item
->cache_id
!= cache_id
|| item
->size
!= data_size
)
224 drm_intel_bo_map(cache
->bo
, false);
225 ret
= memcmp(cache
->bo
->virtual + item
->offset
, data
, item
->size
);
227 drm_intel_bo_unmap(cache
->bo
);
239 brw_alloc_item_data(struct brw_cache
*cache
, uint32_t size
)
242 struct brw_context
*brw
= cache
->brw
;
244 /* Allocate space in the cache BO for our new program. */
245 if (cache
->next_offset
+ size
> cache
->bo
->size
) {
246 uint32_t new_size
= cache
->bo
->size
* 2;
248 while (cache
->next_offset
+ size
> new_size
)
251 brw_cache_new_bo(cache
, new_size
);
254 /* If we would block on writing to an in-use program BO, just
257 if (!brw
->has_llc
&& cache
->bo_used_by_gpu
) {
258 perf_debug("Copying busy program cache buffer.\n");
259 brw_cache_new_bo(cache
, cache
->bo
->size
);
262 offset
= cache
->next_offset
;
264 /* Programs are always 64-byte aligned, so set up the next one now */
265 cache
->next_offset
= ALIGN(offset
+ size
, 64);
271 brw_upload_cache(struct brw_cache
*cache
,
272 enum brw_cache_id cache_id
,
279 uint32_t *out_offset
,
282 struct brw_context
*brw
= cache
->brw
;
283 struct brw_cache_item
*item
= CALLOC_STRUCT(brw_cache_item
);
284 const struct brw_cache_item
*matching_data
=
285 brw_lookup_prog(cache
, cache_id
, data
, data_size
);
289 item
->cache_id
= cache_id
;
290 item
->size
= data_size
;
292 item
->key_size
= key_size
;
293 item
->aux_size
= aux_size
;
294 hash
= hash_key(item
);
297 /* If we can find a matching prog in the cache already, then reuse the
298 * existing stuff without creating new copy into the underlying buffer
299 * object. This is notably useful for programs generating shaders at
300 * runtime, where multiple shaders may compile to the same thing in our
304 item
->offset
= matching_data
->offset
;
306 item
->offset
= brw_alloc_item_data(cache
, data_size
);
308 /* Copy data to the buffer */
310 memcpy((char *)cache
->bo
->virtual + item
->offset
, data
, data_size
);
312 drm_intel_bo_subdata(cache
->bo
, item
->offset
, data_size
, data
);
316 /* Set up the memory containing the key and aux_data */
317 tmp
= malloc(key_size
+ aux_size
);
319 memcpy(tmp
, key
, key_size
);
320 memcpy(tmp
+ key_size
, aux
, aux_size
);
324 if (cache
->n_items
> cache
->size
* 1.5f
)
328 item
->next
= cache
->items
[hash
];
329 cache
->items
[hash
] = item
;
332 *out_offset
= item
->offset
;
333 *(void **)out_aux
= (void *)((char *)item
->key
+ item
->key_size
);
334 cache
->brw
->ctx
.NewDriverState
|= 1 << cache_id
;
338 brw_init_caches(struct brw_context
*brw
)
340 struct brw_cache
*cache
= &brw
->cache
;
347 calloc(cache
->size
, sizeof(struct brw_cache_item
*));
349 cache
->bo
= drm_intel_bo_alloc(brw
->bufmgr
,
353 drm_intel_gem_bo_map_unsynchronized(cache
->bo
);
357 brw_clear_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
359 struct brw_cache_item
*c
, *next
;
362 DBG("%s\n", __func__
);
364 for (i
= 0; i
< cache
->size
; i
++) {
365 for (c
= cache
->items
[i
]; c
; c
= next
) {
367 if (c
->cache_id
== BRW_CACHE_VS_PROG
||
368 c
->cache_id
== BRW_CACHE_GS_PROG
||
369 c
->cache_id
== BRW_CACHE_FS_PROG
||
370 c
->cache_id
== BRW_CACHE_CS_PROG
) {
371 const void *item_aux
= c
->key
+ c
->key_size
;
372 brw_stage_prog_data_free(item_aux
);
374 free((void *)c
->key
);
377 cache
->items
[i
] = NULL
;
382 /* Start putting programs into the start of the BO again, since
383 * we'll never find the old results.
385 cache
->next_offset
= 0;
387 /* We need to make sure that the programs get regenerated, since
388 * any offsets leftover in brw_context will no longer be valid.
390 brw
->NewGLState
|= ~0;
391 brw
->ctx
.NewDriverState
|= ~0ull;
392 intel_batchbuffer_flush(brw
);
396 brw_state_cache_check_size(struct brw_context
*brw
)
398 /* un-tuned guess. Each object is generally a page, so 2000 of them is 8 MB of
401 if (brw
->cache
.n_items
> 2000) {
402 perf_debug("Exceeded state cache size limit. Clearing the set "
403 "of compiled programs, which will trigger recompiles\n");
404 brw_clear_cache(brw
, &brw
->cache
);
410 brw_destroy_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
413 DBG("%s\n", __func__
);
416 drm_intel_bo_unmap(cache
->bo
);
417 drm_intel_bo_unreference(cache
->bo
);
419 brw_clear_cache(brw
, cache
);
427 brw_destroy_caches(struct brw_context
*brw
)
429 brw_destroy_cache(brw
, &brw
->cache
);