1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/glheader.h"
30 #include "main/mtypes.h"
31 #include "main/imports.h"
32 #include "shader/prog_cache.h"
33 #include "shader/program.h"
40 struct gl_program
*program
;
41 struct cache_item
*next
;
44 struct gl_program_cache
46 struct cache_item
**items
;
47 struct cache_item
*last
;
54 * Compute hash index from state key.
57 hash_key(const void *key
, GLuint key_size
)
59 const GLuint
*ikey
= (const GLuint
*) key
;
62 assert(key_size
>= 4);
64 /* Make a slightly better attempt at a hash function:
66 for (i
= 0; i
< key_size
/ sizeof(*ikey
); i
++)
78 * Rebuild/expand the hash table to accomodate more entries
81 rehash(struct gl_program_cache
*cache
)
83 struct cache_item
**items
;
84 struct cache_item
*c
, *next
;
89 size
= cache
->size
* 3;
90 items
= (struct cache_item
**) _mesa_malloc(size
* sizeof(*items
));
91 _mesa_memset(items
, 0, size
* sizeof(*items
));
93 for (i
= 0; i
< cache
->size
; i
++)
94 for (c
= cache
->items
[i
]; c
; c
= next
) {
96 c
->next
= items
[c
->hash
% size
];
97 items
[c
->hash
% size
] = c
;
100 _mesa_free(cache
->items
);
101 cache
->items
= items
;
107 clear_cache(GLcontext
*ctx
, struct gl_program_cache
*cache
)
109 struct cache_item
*c
, *next
;
114 for (i
= 0; i
< cache
->size
; i
++) {
115 for (c
= cache
->items
[i
]; c
; c
= next
) {
118 _mesa_reference_program(ctx
, &c
->program
, NULL
);
121 cache
->items
[i
] = NULL
;
130 struct gl_program_cache
*
131 _mesa_new_program_cache(void)
133 struct gl_program_cache
*cache
= CALLOC_STRUCT(gl_program_cache
);
136 cache
->items
= (struct cache_item
**)
137 _mesa_calloc(cache
->size
* sizeof(struct cache_item
));
148 _mesa_delete_program_cache(GLcontext
*ctx
, struct gl_program_cache
*cache
)
150 clear_cache(ctx
, cache
);
151 _mesa_free(cache
->items
);
157 _mesa_search_program_cache(struct gl_program_cache
*cache
,
158 const void *key
, GLuint keysize
)
161 memcmp(cache
->last
->key
, key
, keysize
) == 0) {
162 return cache
->last
->program
;
165 const GLuint hash
= hash_key(key
, keysize
);
166 struct cache_item
*c
;
168 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
169 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0) {
181 _mesa_program_cache_insert(GLcontext
*ctx
,
182 struct gl_program_cache
*cache
,
183 const void *key
, GLuint keysize
,
184 struct gl_program
*program
)
186 const GLuint hash
= hash_key(key
, keysize
);
187 struct cache_item
*c
= CALLOC_STRUCT(cache_item
);
191 c
->key
= _mesa_malloc(keysize
);
192 memcpy(c
->key
, key
, keysize
);
194 c
->program
= program
; /* no refcount change */
196 if (cache
->n_items
> cache
->size
* 1.5) {
197 if (cache
->size
< 1000)
200 clear_cache(ctx
, cache
);
204 c
->next
= cache
->items
[hash
% cache
->size
];
205 cache
->items
[hash
% cache
->size
] = c
;