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"
39 struct gl_program
*program
;
40 struct cache_item
*next
;
43 struct gl_program_cache
45 struct cache_item
**items
;
52 * Compute hash index from state key.
55 hash_key(const void *key
, GLuint key_size
)
57 const GLuint
*ikey
= (const GLuint
*) key
;
60 assert(key_size
>= 4);
62 /* Make a slightly better attempt at a hash function:
64 for (i
= 0; i
< key_size
/ sizeof(*ikey
); i
++)
76 * Rebuild/expand the hash table to accomodate more entries
79 rehash(struct gl_program_cache
*cache
)
81 struct cache_item
**items
;
82 struct cache_item
*c
, *next
;
85 size
= cache
->size
* 3;
86 items
= (struct cache_item
**) _mesa_malloc(size
* sizeof(*items
));
87 _mesa_memset(items
, 0, size
* sizeof(*items
));
89 for (i
= 0; i
< cache
->size
; i
++)
90 for (c
= cache
->items
[i
]; c
; c
= next
) {
92 c
->next
= items
[c
->hash
% size
];
93 items
[c
->hash
% size
] = c
;
96 _mesa_free(cache
->items
);
103 clear_cache(GLcontext
*ctx
, struct gl_program_cache
*cache
)
105 struct cache_item
*c
, *next
;
108 for (i
= 0; i
< cache
->size
; i
++) {
109 for (c
= cache
->items
[i
]; c
; c
= next
) {
112 ctx
->Driver
.DeleteProgram(ctx
, c
->program
);
115 cache
->items
[i
] = NULL
;
124 struct gl_program_cache
*
125 _mesa_new_program_cache(void)
127 struct gl_program_cache
*cache
= CALLOC_STRUCT(gl_program_cache
);
130 cache
->items
= (struct cache_item
**)
131 _mesa_calloc(cache
->size
* sizeof(struct cache_item
));
142 _mesa_delete_program_cache(GLcontext
*ctx
, struct gl_program_cache
*cache
)
144 clear_cache(ctx
, cache
);
145 _mesa_free(cache
->items
);
151 _mesa_search_program_cache(const struct gl_program_cache
*cache
,
152 const void *key
, GLuint keysize
)
154 const GLuint hash
= hash_key(key
, keysize
);
155 struct cache_item
*c
;
157 for (c
= cache
->items
[hash
% cache
->size
]; c
; c
= c
->next
) {
158 if (c
->hash
== hash
&& memcmp(c
->key
, key
, keysize
) == 0)
167 _mesa_program_cache_insert(GLcontext
*ctx
,
168 struct gl_program_cache
*cache
,
169 const void *key
, GLuint keysize
,
170 struct gl_program
*program
)
172 const GLuint hash
= hash_key(key
, keysize
);
173 struct cache_item
*c
= CALLOC_STRUCT(cache_item
);
177 c
->key
= _mesa_malloc(keysize
);
178 memcpy(c
->key
, key
, keysize
);
180 c
->program
= program
;
182 if (cache
->n_items
> cache
->size
* 1.5) {
183 if (cache
->size
< 1000)
186 clear_cache(ctx
, cache
);
190 c
->next
= cache
->items
[hash
% cache
->size
];
191 cache
->items
[hash
% cache
->size
] = c
;