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
;
202 brw
->batch
.state_base_address_emitted
= false;
206 * Attempts to find an item in the cache with identical data.
208 static const struct brw_cache_item
*
209 brw_lookup_prog(const struct brw_cache
*cache
,
210 enum brw_cache_id cache_id
,
211 const void *data
, unsigned data_size
)
213 const struct brw_context
*brw
= cache
->brw
;
215 const struct brw_cache_item
*item
;
217 for (i
= 0; i
< cache
->size
; i
++) {
218 for (item
= cache
->items
[i
]; item
; item
= item
->next
) {
221 if (item
->cache_id
!= cache_id
|| item
->size
!= data_size
)
225 drm_intel_bo_map(cache
->bo
, false);
226 ret
= memcmp(cache
->bo
->virtual + item
->offset
, data
, item
->size
);
228 drm_intel_bo_unmap(cache
->bo
);
240 brw_alloc_item_data(struct brw_cache
*cache
, uint32_t size
)
243 struct brw_context
*brw
= cache
->brw
;
245 /* Allocate space in the cache BO for our new program. */
246 if (cache
->next_offset
+ size
> cache
->bo
->size
) {
247 uint32_t new_size
= cache
->bo
->size
* 2;
249 while (cache
->next_offset
+ size
> new_size
)
252 brw_cache_new_bo(cache
, new_size
);
255 /* If we would block on writing to an in-use program BO, just
258 if (!brw
->has_llc
&& cache
->bo_used_by_gpu
) {
259 perf_debug("Copying busy program cache buffer.\n");
260 brw_cache_new_bo(cache
, cache
->bo
->size
);
263 offset
= cache
->next_offset
;
265 /* Programs are always 64-byte aligned, so set up the next one now */
266 cache
->next_offset
= ALIGN(offset
+ size
, 64);
272 brw_upload_cache(struct brw_cache
*cache
,
273 enum brw_cache_id cache_id
,
280 uint32_t *out_offset
,
283 struct brw_context
*brw
= cache
->brw
;
284 struct brw_cache_item
*item
= CALLOC_STRUCT(brw_cache_item
);
285 const struct brw_cache_item
*matching_data
=
286 brw_lookup_prog(cache
, cache_id
, data
, data_size
);
290 item
->cache_id
= cache_id
;
291 item
->size
= data_size
;
293 item
->key_size
= key_size
;
294 item
->aux_size
= aux_size
;
295 hash
= hash_key(item
);
298 /* If we can find a matching prog in the cache already, then reuse the
299 * existing stuff without creating new copy into the underlying buffer
300 * object. This is notably useful for programs generating shaders at
301 * runtime, where multiple shaders may compile to the same thing in our
305 item
->offset
= matching_data
->offset
;
307 item
->offset
= brw_alloc_item_data(cache
, data_size
);
309 /* Copy data to the buffer */
311 memcpy((char *)cache
->bo
->virtual + item
->offset
, data
, data_size
);
313 drm_intel_bo_subdata(cache
->bo
, item
->offset
, data_size
, data
);
317 /* Set up the memory containing the key and aux_data */
318 tmp
= malloc(key_size
+ aux_size
);
320 memcpy(tmp
, key
, key_size
);
321 memcpy(tmp
+ key_size
, aux
, aux_size
);
325 if (cache
->n_items
> cache
->size
* 1.5f
)
329 item
->next
= cache
->items
[hash
];
330 cache
->items
[hash
] = item
;
333 *out_offset
= item
->offset
;
334 *(void **)out_aux
= (void *)((char *)item
->key
+ item
->key_size
);
335 cache
->brw
->ctx
.NewDriverState
|= 1 << cache_id
;
339 brw_init_caches(struct brw_context
*brw
)
341 struct brw_cache
*cache
= &brw
->cache
;
348 calloc(cache
->size
, sizeof(struct brw_cache_item
*));
350 cache
->bo
= drm_intel_bo_alloc(brw
->bufmgr
,
354 drm_intel_gem_bo_map_unsynchronized(cache
->bo
);
358 brw_clear_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
360 struct brw_cache_item
*c
, *next
;
363 DBG("%s\n", __func__
);
365 for (i
= 0; i
< cache
->size
; i
++) {
366 for (c
= cache
->items
[i
]; c
; c
= next
) {
368 if (c
->cache_id
== BRW_CACHE_VS_PROG
||
369 c
->cache_id
== BRW_CACHE_GS_PROG
||
370 c
->cache_id
== BRW_CACHE_FS_PROG
||
371 c
->cache_id
== BRW_CACHE_CS_PROG
) {
372 const void *item_aux
= c
->key
+ c
->key_size
;
373 brw_stage_prog_data_free(item_aux
);
375 free((void *)c
->key
);
378 cache
->items
[i
] = NULL
;
383 /* Start putting programs into the start of the BO again, since
384 * we'll never find the old results.
386 cache
->next_offset
= 0;
388 /* We need to make sure that the programs get regenerated, since
389 * any offsets leftover in brw_context will no longer be valid.
391 brw
->NewGLState
= ~0;
392 brw
->ctx
.NewDriverState
= ~0ull;
393 brw
->state
.pipelines
[BRW_RENDER_PIPELINE
].mesa
= ~0;
394 brw
->state
.pipelines
[BRW_RENDER_PIPELINE
].brw
= ~0ull;
395 brw
->state
.pipelines
[BRW_COMPUTE_PIPELINE
].mesa
= ~0;
396 brw
->state
.pipelines
[BRW_COMPUTE_PIPELINE
].brw
= ~0ull;
398 /* Also, NULL out any stale program pointers. */
399 brw
->vs
.prog_data
= NULL
;
400 brw
->vs
.base
.prog_data
= NULL
;
401 brw
->tcs
.prog_data
= NULL
;
402 brw
->tcs
.base
.prog_data
= NULL
;
403 brw
->tes
.prog_data
= NULL
;
404 brw
->tes
.base
.prog_data
= NULL
;
405 brw
->gs
.prog_data
= NULL
;
406 brw
->gs
.base
.prog_data
= NULL
;
407 brw
->wm
.prog_data
= NULL
;
408 brw
->wm
.base
.prog_data
= NULL
;
409 brw
->cs
.prog_data
= NULL
;
410 brw
->cs
.base
.prog_data
= NULL
;
412 intel_batchbuffer_flush(brw
);
416 brw_state_cache_check_size(struct brw_context
*brw
)
418 /* un-tuned guess. Each object is generally a page, so 2000 of them is 8 MB of
421 if (brw
->cache
.n_items
> 2000) {
422 perf_debug("Exceeded state cache size limit. Clearing the set "
423 "of compiled programs, which will trigger recompiles\n");
424 brw_clear_cache(brw
, &brw
->cache
);
430 brw_destroy_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
433 DBG("%s\n", __func__
);
436 drm_intel_bo_unmap(cache
->bo
);
437 drm_intel_bo_unreference(cache
->bo
);
439 brw_clear_cache(brw
, cache
);
447 brw_destroy_caches(struct brw_context
*brw
)
449 brw_destroy_cache(brw
, &brw
->cache
);