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
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 "brw_state.h"
50 #define FILE_DEBUG_FLAG DEBUG_STATE
53 hash_key(struct brw_cache_item
*item
)
55 GLuint
*ikey
= (GLuint
*)item
->key
;
56 GLuint hash
= item
->cache_id
, i
;
58 assert(item
->key_size
% 4 == 0);
60 /* I'm sure this can be improved on:
62 for (i
= 0; i
< item
->key_size
/4; i
++) {
64 hash
= (hash
<< 5) | (hash
>> 27);
72 * Marks a new buffer as being chosen for the given cache id.
75 update_cache_last(struct brw_cache
*cache
, enum brw_cache_id cache_id
,
78 if (bo
== cache
->last_bo
[cache_id
])
79 return; /* no change */
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
;
88 brw_cache_item_equals(const struct brw_cache_item
*a
,
89 const struct brw_cache_item
*b
)
91 return a
->cache_id
== b
->cache_id
&&
93 a
->key_size
== b
->key_size
&&
94 (memcmp(a
->key
, b
->key
, a
->key_size
) == 0);
97 static struct brw_cache_item
*
98 search_cache(struct brw_cache
*cache
, GLuint hash
,
99 struct brw_cache_item
*lookup
)
101 struct brw_cache_item
*c
;
106 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
)
109 fprintf(stderr
, "bucket %d/%d = %d/%d items\n", hash
% cache
->size
,
110 cache
->size
, bucketcount
, cache
->n_items
);
113 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
114 if (brw_cache_item_equals(lookup
, c
))
123 rehash(struct brw_cache
*cache
)
125 struct brw_cache_item
**items
;
126 struct brw_cache_item
*c
, *next
;
129 size
= cache
->size
* 3;
130 items
= (struct brw_cache_item
**) calloc(1, size
* sizeof(*items
));
132 for (i
= 0; i
< cache
->size
; i
++)
133 for (c
= cache
->items
[i
]; c
; c
= next
) {
135 c
->next
= items
[c
->hash
% size
];
136 items
[c
->hash
% size
] = c
;
140 cache
->items
= items
;
146 * Returns the buffer object matching cache_id and key, or NULL.
149 brw_search_cache(struct brw_cache
*cache
,
150 enum brw_cache_id cache_id
,
151 const void *key
, GLuint key_size
,
154 struct brw_cache_item
*item
;
155 struct brw_cache_item lookup
;
158 lookup
.cache_id
= cache_id
;
160 lookup
.key_size
= key_size
;
161 hash
= hash_key(&lookup
);
164 item
= search_cache(cache
, hash
, &lookup
);
170 *(void **)aux_return
= (void *)((char *)item
->key
+ item
->key_size
);
172 update_cache_last(cache
, cache_id
, item
->bo
);
174 drm_intel_bo_reference(item
->bo
);
180 brw_upload_cache(struct brw_cache
*cache
,
181 enum brw_cache_id cache_id
,
190 struct brw_cache_item
*item
= CALLOC_STRUCT(brw_cache_item
);
195 item
->cache_id
= cache_id
;
197 item
->key_size
= key_size
;
198 hash
= hash_key(item
);
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);
206 /* Set up the memory containing the key and aux_data */
207 tmp
= malloc(key_size
+ aux_size
);
209 memcpy(tmp
, key
, key_size
);
210 memcpy(tmp
+ key_size
, aux
, aux_size
);
215 drm_intel_bo_reference(bo
);
217 if (cache
->n_items
> cache
->size
* 1.5)
221 item
->next
= cache
->items
[hash
];
222 cache
->items
[hash
] = item
;
226 *(void **)aux_return
= (void *)((char *)item
->key
+ item
->key_size
);
229 DBG("upload %s: %d bytes to cache id %d\n",
230 cache
->name
[cache_id
],
231 data_size
, cache_id
);
233 /* Copy data to the buffer */
234 drm_intel_bo_subdata(bo
, 0, data_size
, data
);
236 update_cache_last(cache
, cache_id
, bo
);
242 brw_init_cache_id(struct brw_cache
*cache
,
244 enum brw_cache_id id
)
246 cache
->name
[id
] = strdup(name
);
251 brw_init_caches(struct brw_context
*brw
)
253 struct brw_cache
*cache
= &brw
->cache
;
259 cache
->items
= (struct brw_cache_item
**)
260 calloc(1, cache
->size
* sizeof(struct brw_cache_item
));
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
);
270 brw_init_cache_id(cache
, "SF_UNIT", BRW_SF_UNIT
);
272 brw_init_cache_id(cache
, "VS_UNIT", BRW_VS_UNIT
);
274 brw_init_cache_id(cache
, "VS_PROG", BRW_VS_PROG
);
276 brw_init_cache_id(cache
, "CLIP_UNIT", BRW_CLIP_UNIT
);
278 brw_init_cache_id(cache
, "CLIP_PROG", BRW_CLIP_PROG
);
279 brw_init_cache_id(cache
, "CLIP_VP", BRW_CLIP_VP
);
281 brw_init_cache_id(cache
, "GS_UNIT", BRW_GS_UNIT
);
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
);
290 brw_clear_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
292 struct brw_cache_item
*c
, *next
;
295 DBG("%s\n", __FUNCTION__
);
297 for (i
= 0; i
< cache
->size
; i
++) {
298 for (c
= cache
->items
[i
]; c
; c
= next
) {
300 drm_intel_bo_unreference(c
->bo
);
301 free((void *)c
->key
);
304 cache
->items
[i
] = NULL
;
309 brw
->state
.dirty
.mesa
|= ~0;
310 brw
->state
.dirty
.brw
|= ~0;
311 brw
->state
.dirty
.cache
|= ~0;
315 brw_state_cache_check_size(struct brw_context
*brw
)
317 /* un-tuned guess. Each object is generally a page, so 1000 of them is 4 MB of
320 if (brw
->cache
.n_items
> 1000)
321 brw_clear_cache(brw
, &brw
->cache
);
326 brw_destroy_cache(struct brw_context
*brw
, struct brw_cache
*cache
)
330 DBG("%s\n", __FUNCTION__
);
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
]);
344 brw_destroy_caches(struct brw_context
*brw
)
346 brw_destroy_cache(brw
, &brw
->cache
);