blob: ed93af7f13ad10d202947709718e14400e23e633 [file] [log] [blame]
Brian0ee15e02007-10-30 12:23:22 -06001/**************************************************************************
2 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2003 VMware, Inc.
Brian0ee15e02007-10-30 12:23:22 -06004 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
José Fonseca87712852014-01-17 16:27:50 +000021 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Brian0ee15e02007-10-30 12:23:22 -060022 * 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.
25 *
26 **************************************************************************/
27
28
29#include "main/glheader.h"
30#include "main/mtypes.h"
31#include "main/imports.h"
Eric Anholte1cb12b2010-11-05 07:25:18 -070032#include "main/shaderobj.h"
Brian Paulec2b92f2010-06-10 23:02:41 -060033#include "program/prog_cache.h"
34#include "program/program.h"
Brian0ee15e02007-10-30 12:23:22 -060035
36
37struct cache_item
38{
39 GLuint hash;
Chris Forbesc4629ad2013-04-01 12:51:59 +130040 unsigned keysize;
Brian0ee15e02007-10-30 12:23:22 -060041 void *key;
42 struct gl_program *program;
43 struct cache_item *next;
44};
45
46struct gl_program_cache
47{
48 struct cache_item **items;
Keith Whitwelld63a36e2008-10-03 16:46:48 +010049 struct cache_item *last;
Brian0ee15e02007-10-30 12:23:22 -060050 GLuint size, n_items;
51};
52
53
54
55/**
56 * Compute hash index from state key.
57 */
58static GLuint
59hash_key(const void *key, GLuint key_size)
60{
61 const GLuint *ikey = (const GLuint *) key;
62 GLuint hash = 0, i;
63
64 assert(key_size >= 4);
65
66 /* Make a slightly better attempt at a hash function:
67 */
68 for (i = 0; i < key_size / sizeof(*ikey); i++)
69 {
70 hash += ikey[i];
71 hash += (hash << 10);
72 hash ^= (hash >> 6);
73 }
74
75 return hash;
76}
77
78
79/**
Zoë Blade05e7f7f2015-04-22 11:33:17 +010080 * Rebuild/expand the hash table to accommodate more entries
Brian0ee15e02007-10-30 12:23:22 -060081 */
82static void
83rehash(struct gl_program_cache *cache)
84{
85 struct cache_item **items;
86 struct cache_item *c, *next;
87 GLuint size, i;
88
Keith Whitwelld63a36e2008-10-03 16:46:48 +010089 cache->last = NULL;
90
Brian0ee15e02007-10-30 12:23:22 -060091 size = cache->size * 3;
Matt Turner2b7a9722012-09-03 19:44:00 -070092 items = malloc(size * sizeof(*items));
Kenneth Graunke26f8fad2010-02-18 23:51:00 -080093 memset(items, 0, size * sizeof(*items));
Brian0ee15e02007-10-30 12:23:22 -060094
95 for (i = 0; i < cache->size; i++)
96 for (c = cache->items[i]; c; c = next) {
97 next = c->next;
98 c->next = items[c->hash % size];
99 items[c->hash % size] = c;
100 }
101
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500102 free(cache->items);
Brian0ee15e02007-10-30 12:23:22 -0600103 cache->items = items;
104 cache->size = size;
105}
106
107
108static void
Eric Anholte1cb12b2010-11-05 07:25:18 -0700109clear_cache(struct gl_context *ctx, struct gl_program_cache *cache,
110 GLboolean shader)
Brian0ee15e02007-10-30 12:23:22 -0600111{
112 struct cache_item *c, *next;
113 GLuint i;
Keith Whitwelld63a36e2008-10-03 16:46:48 +0100114
115 cache->last = NULL;
Brian0ee15e02007-10-30 12:23:22 -0600116
117 for (i = 0; i < cache->size; i++) {
118 for (c = cache->items[i]; c; c = next) {
119 next = c->next;
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500120 free(c->key);
Eric Anholte1cb12b2010-11-05 07:25:18 -0700121 if (shader) {
122 _mesa_reference_shader_program(ctx,
123 (struct gl_shader_program **)&c->program,
124 NULL);
125 } else {
126 _mesa_reference_program(ctx, &c->program, NULL);
127 }
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500128 free(c);
Brian0ee15e02007-10-30 12:23:22 -0600129 }
130 cache->items[i] = NULL;
131 }
132
133
134 cache->n_items = 0;
135}
136
137
138
139struct gl_program_cache *
140_mesa_new_program_cache(void)
141{
142 struct gl_program_cache *cache = CALLOC_STRUCT(gl_program_cache);
143 if (cache) {
144 cache->size = 17;
Matt Turner2b7a9722012-09-03 19:44:00 -0700145 cache->items =
Dave Airlie021e84f2014-09-02 09:21:18 +1000146 calloc(cache->size, sizeof(struct cache_item *));
Brian0ee15e02007-10-30 12:23:22 -0600147 if (!cache->items) {
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500148 free(cache);
Brian0ee15e02007-10-30 12:23:22 -0600149 return NULL;
150 }
151 }
152 return cache;
153}
154
155
156void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400157_mesa_delete_program_cache(struct gl_context *ctx, struct gl_program_cache *cache)
Brian0ee15e02007-10-30 12:23:22 -0600158{
Eric Anholte1cb12b2010-11-05 07:25:18 -0700159 clear_cache(ctx, cache, GL_FALSE);
160 free(cache->items);
161 free(cache);
162}
163
164void
165_mesa_delete_shader_cache(struct gl_context *ctx,
166 struct gl_program_cache *cache)
167{
168 clear_cache(ctx, cache, GL_TRUE);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500169 free(cache->items);
170 free(cache);
Brian0ee15e02007-10-30 12:23:22 -0600171}
172
173
174struct gl_program *
Keith Whitwelld63a36e2008-10-03 16:46:48 +0100175_mesa_search_program_cache(struct gl_program_cache *cache,
Brian0ee15e02007-10-30 12:23:22 -0600176 const void *key, GLuint keysize)
177{
Chris Forbesc4629ad2013-04-01 12:51:59 +1300178 if (cache->last &&
179 cache->last->keysize == keysize &&
Keith Whitwelld63a36e2008-10-03 16:46:48 +0100180 memcmp(cache->last->key, key, keysize) == 0) {
181 return cache->last->program;
Brian0ee15e02007-10-30 12:23:22 -0600182 }
Keith Whitwelld63a36e2008-10-03 16:46:48 +0100183 else {
184 const GLuint hash = hash_key(key, keysize);
185 struct cache_item *c;
Brian0ee15e02007-10-30 12:23:22 -0600186
Keith Whitwelld63a36e2008-10-03 16:46:48 +0100187 for (c = cache->items[hash % cache->size]; c; c = c->next) {
Chris Forbesc4629ad2013-04-01 12:51:59 +1300188 if (c->hash == hash &&
189 c->keysize == keysize &&
190 memcmp(c->key, key, keysize) == 0) {
191
Keith Whitwelld63a36e2008-10-03 16:46:48 +0100192 cache->last = c;
193 return c->program;
194 }
195 }
196
197 return NULL;
198 }
Brian0ee15e02007-10-30 12:23:22 -0600199}
200
201
202void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400203_mesa_program_cache_insert(struct gl_context *ctx,
Brian0ee15e02007-10-30 12:23:22 -0600204 struct gl_program_cache *cache,
205 const void *key, GLuint keysize,
206 struct gl_program *program)
207{
208 const GLuint hash = hash_key(key, keysize);
209 struct cache_item *c = CALLOC_STRUCT(cache_item);
210
211 c->hash = hash;
212
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500213 c->key = malloc(keysize);
Brian0ee15e02007-10-30 12:23:22 -0600214 memcpy(c->key, key, keysize);
Chris Forbesc4629ad2013-04-01 12:51:59 +1300215 c->keysize = keysize;
Brian0ee15e02007-10-30 12:23:22 -0600216
Brian Paula56a59c2008-05-07 08:55:33 -0600217 c->program = program; /* no refcount change */
Brian0ee15e02007-10-30 12:23:22 -0600218
219 if (cache->n_items > cache->size * 1.5) {
220 if (cache->size < 1000)
221 rehash(cache);
222 else
Eric Anholte1cb12b2010-11-05 07:25:18 -0700223 clear_cache(ctx, cache, GL_FALSE);
224 }
225
226 cache->n_items++;
227 c->next = cache->items[hash % cache->size];
228 cache->items[hash % cache->size] = c;
229}
230
231void
232_mesa_shader_cache_insert(struct gl_context *ctx,
233 struct gl_program_cache *cache,
234 const void *key, GLuint keysize,
235 struct gl_shader_program *program)
236{
237 const GLuint hash = hash_key(key, keysize);
238 struct cache_item *c = CALLOC_STRUCT(cache_item);
239
240 c->hash = hash;
241
242 c->key = malloc(keysize);
243 memcpy(c->key, key, keysize);
Chris Forbesc4629ad2013-04-01 12:51:59 +1300244 c->keysize = keysize;
Eric Anholte1cb12b2010-11-05 07:25:18 -0700245
246 c->program = (struct gl_program *)program; /* no refcount change */
247
248 if (cache->n_items > cache->size * 1.5) {
249 if (cache->size < 1000)
250 rehash(cache);
251 else
252 clear_cache(ctx, cache, GL_TRUE);
Brian0ee15e02007-10-30 12:23:22 -0600253 }
254
255 cache->n_items++;
256 c->next = cache->items[hash % cache->size];
257 cache->items[hash % cache->size] = c;
258}