Brian | 0ee15e0 | 2007-10-30 12:23:22 -0600 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 4 | * 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. |
| 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. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | |
| 28 | |
| 29 | #include "main/glheader.h" |
| 30 | #include "main/mtypes.h" |
| 31 | #include "main/imports.h" |
| 32 | #include "shader/prog_cache.h" |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame^] | 33 | #include "shader/program.h" |
Brian | 0ee15e0 | 2007-10-30 12:23:22 -0600 | [diff] [blame] | 34 | |
| 35 | |
| 36 | struct cache_item |
| 37 | { |
| 38 | GLuint hash; |
| 39 | void *key; |
| 40 | struct gl_program *program; |
| 41 | struct cache_item *next; |
| 42 | }; |
| 43 | |
| 44 | struct gl_program_cache |
| 45 | { |
| 46 | struct cache_item **items; |
| 47 | GLuint size, n_items; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Compute hash index from state key. |
| 54 | */ |
| 55 | static GLuint |
| 56 | hash_key(const void *key, GLuint key_size) |
| 57 | { |
| 58 | const GLuint *ikey = (const GLuint *) key; |
| 59 | GLuint hash = 0, i; |
| 60 | |
| 61 | assert(key_size >= 4); |
| 62 | |
| 63 | /* Make a slightly better attempt at a hash function: |
| 64 | */ |
| 65 | for (i = 0; i < key_size / sizeof(*ikey); i++) |
| 66 | { |
| 67 | hash += ikey[i]; |
| 68 | hash += (hash << 10); |
| 69 | hash ^= (hash >> 6); |
| 70 | } |
| 71 | |
| 72 | return hash; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Rebuild/expand the hash table to accomodate more entries |
| 78 | */ |
| 79 | static void |
| 80 | rehash(struct gl_program_cache *cache) |
| 81 | { |
| 82 | struct cache_item **items; |
| 83 | struct cache_item *c, *next; |
| 84 | GLuint size, i; |
| 85 | |
| 86 | size = cache->size * 3; |
| 87 | items = (struct cache_item**) _mesa_malloc(size * sizeof(*items)); |
| 88 | _mesa_memset(items, 0, size * sizeof(*items)); |
| 89 | |
| 90 | for (i = 0; i < cache->size; i++) |
| 91 | for (c = cache->items[i]; c; c = next) { |
| 92 | next = c->next; |
| 93 | c->next = items[c->hash % size]; |
| 94 | items[c->hash % size] = c; |
| 95 | } |
| 96 | |
| 97 | _mesa_free(cache->items); |
| 98 | cache->items = items; |
| 99 | cache->size = size; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static void |
| 104 | clear_cache(GLcontext *ctx, struct gl_program_cache *cache) |
| 105 | { |
| 106 | struct cache_item *c, *next; |
| 107 | GLuint i; |
| 108 | |
| 109 | for (i = 0; i < cache->size; i++) { |
| 110 | for (c = cache->items[i]; c; c = next) { |
| 111 | next = c->next; |
| 112 | _mesa_free(c->key); |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame^] | 113 | _mesa_reference_program(ctx, &c->program, NULL); |
Brian | 0ee15e0 | 2007-10-30 12:23:22 -0600 | [diff] [blame] | 114 | _mesa_free(c); |
| 115 | } |
| 116 | cache->items[i] = NULL; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | cache->n_items = 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | struct gl_program_cache * |
| 126 | _mesa_new_program_cache(void) |
| 127 | { |
| 128 | struct gl_program_cache *cache = CALLOC_STRUCT(gl_program_cache); |
| 129 | if (cache) { |
| 130 | cache->size = 17; |
| 131 | cache->items = (struct cache_item **) |
| 132 | _mesa_calloc(cache->size * sizeof(struct cache_item)); |
| 133 | if (!cache->items) { |
| 134 | _mesa_free(cache); |
| 135 | return NULL; |
| 136 | } |
| 137 | } |
| 138 | return cache; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void |
| 143 | _mesa_delete_program_cache(GLcontext *ctx, struct gl_program_cache *cache) |
| 144 | { |
| 145 | clear_cache(ctx, cache); |
| 146 | _mesa_free(cache->items); |
Brian | aec6009e | 2007-12-24 17:39:21 -0700 | [diff] [blame] | 147 | _mesa_free(cache); |
Brian | 0ee15e0 | 2007-10-30 12:23:22 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | struct gl_program * |
| 152 | _mesa_search_program_cache(const struct gl_program_cache *cache, |
| 153 | const void *key, GLuint keysize) |
| 154 | { |
| 155 | const GLuint hash = hash_key(key, keysize); |
| 156 | struct cache_item *c; |
| 157 | |
| 158 | for (c = cache->items[hash % cache->size]; c; c = c->next) { |
| 159 | if (c->hash == hash && memcmp(c->key, key, keysize) == 0) |
| 160 | return c->program; |
| 161 | } |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void |
| 168 | _mesa_program_cache_insert(GLcontext *ctx, |
| 169 | struct gl_program_cache *cache, |
| 170 | const void *key, GLuint keysize, |
| 171 | struct gl_program *program) |
| 172 | { |
| 173 | const GLuint hash = hash_key(key, keysize); |
| 174 | struct cache_item *c = CALLOC_STRUCT(cache_item); |
| 175 | |
| 176 | c->hash = hash; |
| 177 | |
| 178 | c->key = _mesa_malloc(keysize); |
| 179 | memcpy(c->key, key, keysize); |
| 180 | |
Brian Paul | a56a59c | 2008-05-07 08:55:33 -0600 | [diff] [blame^] | 181 | c->program = program; /* no refcount change */ |
Brian | 0ee15e0 | 2007-10-30 12:23:22 -0600 | [diff] [blame] | 182 | |
| 183 | if (cache->n_items > cache->size * 1.5) { |
| 184 | if (cache->size < 1000) |
| 185 | rehash(cache); |
| 186 | else |
| 187 | clear_cache(ctx, cache); |
| 188 | } |
| 189 | |
| 190 | cache->n_items++; |
| 191 | c->next = cache->items[hash % cache->size]; |
| 192 | cache->items[hash % cache->size] = c; |
| 193 | } |