1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2003 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "main/glheader.h"
30bf215546Sopenharmony_ci#include "main/shader_types.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "main/shaderobj.h"
33bf215546Sopenharmony_ci#include "program/prog_cache.h"
34bf215546Sopenharmony_ci#include "program/program.h"
35bf215546Sopenharmony_ci#include "util/u_memory.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct cache_item
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   GLuint hash;
41bf215546Sopenharmony_ci   unsigned keysize;
42bf215546Sopenharmony_ci   void *key;
43bf215546Sopenharmony_ci   struct gl_program *program;
44bf215546Sopenharmony_ci   struct cache_item *next;
45bf215546Sopenharmony_ci};
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_cistruct gl_program_cache
48bf215546Sopenharmony_ci{
49bf215546Sopenharmony_ci   struct cache_item **items;
50bf215546Sopenharmony_ci   struct cache_item *last;
51bf215546Sopenharmony_ci   GLuint size, n_items;
52bf215546Sopenharmony_ci};
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci/**
57bf215546Sopenharmony_ci * Compute hash index from state key.
58bf215546Sopenharmony_ci */
59bf215546Sopenharmony_cistatic GLuint
60bf215546Sopenharmony_cihash_key(const void *key, GLuint key_size)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   const GLuint *ikey = (const GLuint *) key;
63bf215546Sopenharmony_ci   GLuint hash = 0, i;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   assert(key_size >= 4);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   /* Make a slightly better attempt at a hash function:
68bf215546Sopenharmony_ci    */
69bf215546Sopenharmony_ci   for (i = 0; i < key_size / sizeof(*ikey); i++)
70bf215546Sopenharmony_ci   {
71bf215546Sopenharmony_ci      hash += ikey[i];
72bf215546Sopenharmony_ci      hash += (hash << 10);
73bf215546Sopenharmony_ci      hash ^= (hash >> 6);
74bf215546Sopenharmony_ci   }
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   return hash;
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci/**
81bf215546Sopenharmony_ci * Rebuild/expand the hash table to accommodate more entries
82bf215546Sopenharmony_ci */
83bf215546Sopenharmony_cistatic void
84bf215546Sopenharmony_cirehash(struct gl_program_cache *cache)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   struct cache_item **items;
87bf215546Sopenharmony_ci   struct cache_item *c, *next;
88bf215546Sopenharmony_ci   GLuint size, i;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   cache->last = NULL;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   size = cache->size * 3;
93bf215546Sopenharmony_ci   items = malloc(size * sizeof(*items));
94bf215546Sopenharmony_ci   memset(items, 0, size * sizeof(*items));
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   for (i = 0; i < cache->size; i++)
97bf215546Sopenharmony_ci      for (c = cache->items[i]; c; c = next) {
98bf215546Sopenharmony_ci	 next = c->next;
99bf215546Sopenharmony_ci	 c->next = items[c->hash % size];
100bf215546Sopenharmony_ci	 items[c->hash % size] = c;
101bf215546Sopenharmony_ci      }
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   free(cache->items);
104bf215546Sopenharmony_ci   cache->items = items;
105bf215546Sopenharmony_ci   cache->size = size;
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cistatic void
110bf215546Sopenharmony_ciclear_cache(struct gl_context *ctx, struct gl_program_cache *cache,
111bf215546Sopenharmony_ci	    GLboolean shader)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   struct cache_item *c, *next;
114bf215546Sopenharmony_ci   GLuint i;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   cache->last = NULL;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   for (i = 0; i < cache->size; i++) {
119bf215546Sopenharmony_ci      for (c = cache->items[i]; c; c = next) {
120bf215546Sopenharmony_ci	 next = c->next;
121bf215546Sopenharmony_ci	 free(c->key);
122bf215546Sopenharmony_ci	 if (shader) {
123bf215546Sopenharmony_ci	    _mesa_reference_shader_program(ctx,
124bf215546Sopenharmony_ci					   (struct gl_shader_program **)&c->program,
125bf215546Sopenharmony_ci					   NULL);
126bf215546Sopenharmony_ci	 } else {
127bf215546Sopenharmony_ci	    _mesa_reference_program(ctx, &c->program, NULL);
128bf215546Sopenharmony_ci	 }
129bf215546Sopenharmony_ci	 FREE(c);
130bf215546Sopenharmony_ci      }
131bf215546Sopenharmony_ci      cache->items[i] = NULL;
132bf215546Sopenharmony_ci   }
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   cache->n_items = 0;
136bf215546Sopenharmony_ci}
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_cistruct gl_program_cache *
141bf215546Sopenharmony_ci_mesa_new_program_cache(void)
142bf215546Sopenharmony_ci{
143bf215546Sopenharmony_ci   struct gl_program_cache *cache = CALLOC_STRUCT(gl_program_cache);
144bf215546Sopenharmony_ci   if (cache) {
145bf215546Sopenharmony_ci      cache->size = 17;
146bf215546Sopenharmony_ci      cache->items =
147bf215546Sopenharmony_ci         calloc(cache->size, sizeof(struct cache_item *));
148bf215546Sopenharmony_ci      if (!cache->items) {
149bf215546Sopenharmony_ci         FREE(cache);
150bf215546Sopenharmony_ci         return NULL;
151bf215546Sopenharmony_ci      }
152bf215546Sopenharmony_ci   }
153bf215546Sopenharmony_ci   return cache;
154bf215546Sopenharmony_ci}
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_civoid
158bf215546Sopenharmony_ci_mesa_delete_program_cache(struct gl_context *ctx, struct gl_program_cache *cache)
159bf215546Sopenharmony_ci{
160bf215546Sopenharmony_ci   clear_cache(ctx, cache, GL_FALSE);
161bf215546Sopenharmony_ci   free(cache->items);
162bf215546Sopenharmony_ci   FREE(cache);
163bf215546Sopenharmony_ci}
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_civoid
166bf215546Sopenharmony_ci_mesa_delete_shader_cache(struct gl_context *ctx,
167bf215546Sopenharmony_ci			  struct gl_program_cache *cache)
168bf215546Sopenharmony_ci{
169bf215546Sopenharmony_ci   clear_cache(ctx, cache, GL_TRUE);
170bf215546Sopenharmony_ci   free(cache->items);
171bf215546Sopenharmony_ci   FREE(cache);
172bf215546Sopenharmony_ci}
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_cistruct gl_program *
176bf215546Sopenharmony_ci_mesa_search_program_cache(struct gl_program_cache *cache,
177bf215546Sopenharmony_ci                           const void *key, GLuint keysize)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci   if (cache->last &&
180bf215546Sopenharmony_ci       cache->last->keysize == keysize &&
181bf215546Sopenharmony_ci       memcmp(cache->last->key, key, keysize) == 0) {
182bf215546Sopenharmony_ci      return cache->last->program;
183bf215546Sopenharmony_ci   }
184bf215546Sopenharmony_ci   else {
185bf215546Sopenharmony_ci      const GLuint hash = hash_key(key, keysize);
186bf215546Sopenharmony_ci      struct cache_item *c;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci      for (c = cache->items[hash % cache->size]; c; c = c->next) {
189bf215546Sopenharmony_ci         if (c->hash == hash &&
190bf215546Sopenharmony_ci             c->keysize == keysize &&
191bf215546Sopenharmony_ci             memcmp(c->key, key, keysize) == 0) {
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci            cache->last = c;
194bf215546Sopenharmony_ci            return c->program;
195bf215546Sopenharmony_ci         }
196bf215546Sopenharmony_ci      }
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci      return NULL;
199bf215546Sopenharmony_ci   }
200bf215546Sopenharmony_ci}
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_civoid
204bf215546Sopenharmony_ci_mesa_program_cache_insert(struct gl_context *ctx,
205bf215546Sopenharmony_ci                           struct gl_program_cache *cache,
206bf215546Sopenharmony_ci                           const void *key, GLuint keysize,
207bf215546Sopenharmony_ci                           struct gl_program *program)
208bf215546Sopenharmony_ci{
209bf215546Sopenharmony_ci   const GLuint hash = hash_key(key, keysize);
210bf215546Sopenharmony_ci   struct cache_item *c = CALLOC_STRUCT(cache_item);
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   c->hash = hash;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   c->key = malloc(keysize);
215bf215546Sopenharmony_ci   memcpy(c->key, key, keysize);
216bf215546Sopenharmony_ci   c->keysize = keysize;
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   c->program = program;  /* no refcount change */
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci   if (cache->n_items > cache->size * 1.5) {
221bf215546Sopenharmony_ci      if (cache->size < 1000)
222bf215546Sopenharmony_ci	 rehash(cache);
223bf215546Sopenharmony_ci      else
224bf215546Sopenharmony_ci	 clear_cache(ctx, cache, GL_FALSE);
225bf215546Sopenharmony_ci   }
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   cache->n_items++;
228bf215546Sopenharmony_ci   c->next = cache->items[hash % cache->size];
229bf215546Sopenharmony_ci   cache->items[hash % cache->size] = c;
230bf215546Sopenharmony_ci}
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_civoid
233bf215546Sopenharmony_ci_mesa_shader_cache_insert(struct gl_context *ctx,
234bf215546Sopenharmony_ci			  struct gl_program_cache *cache,
235bf215546Sopenharmony_ci			  const void *key, GLuint keysize,
236bf215546Sopenharmony_ci			  struct gl_shader_program *program)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci   const GLuint hash = hash_key(key, keysize);
239bf215546Sopenharmony_ci   struct cache_item *c = CALLOC_STRUCT(cache_item);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   c->hash = hash;
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   c->key = malloc(keysize);
244bf215546Sopenharmony_ci   memcpy(c->key, key, keysize);
245bf215546Sopenharmony_ci   c->keysize = keysize;
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   c->program = (struct gl_program *)program;  /* no refcount change */
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   if (cache->n_items > cache->size * 1.5) {
250bf215546Sopenharmony_ci      if (cache->size < 1000)
251bf215546Sopenharmony_ci	 rehash(cache);
252bf215546Sopenharmony_ci      else
253bf215546Sopenharmony_ci	 clear_cache(ctx, cache, GL_TRUE);
254bf215546Sopenharmony_ci   }
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci   cache->n_items++;
257bf215546Sopenharmony_ci   c->next = cache->items[hash % cache->size];
258bf215546Sopenharmony_ci   cache->items[hash % cache->size] = c;
259bf215546Sopenharmony_ci}
260