3f1ecb630f2ab5c989f17b6c3cb6129e86997ea7
[mesa.git] / src / gallium / auxiliary / translate / translate_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2008 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 #include "util/u_memory.h"
29 #include "pipe/p_state.h"
30 #include "translate.h"
31 #include "translate_cache.h"
32
33 #include "cso_cache/cso_cache.h"
34 #include "cso_cache/cso_hash.h"
35
36 struct translate_cache {
37 struct cso_hash *hash;
38 };
39
40 struct translate_cache * translate_cache_create( void )
41 {
42 struct translate_cache *cache = MALLOC_STRUCT(translate_cache);
43 if (cache == NULL) {
44 return NULL;
45 }
46
47 cache->hash = cso_hash_create();
48 return cache;
49 }
50
51
52 static INLINE void delete_translates(struct translate_cache *cache)
53 {
54 struct cso_hash *hash = cache->hash;
55 struct cso_hash_iter iter = cso_hash_first_node(hash);
56 while (!cso_hash_iter_is_null(iter)) {
57 struct translate *state = (struct translate*)cso_hash_iter_data(iter);
58 iter = cso_hash_iter_next(iter);
59 if (state) {
60 state->release(state);
61 }
62 }
63 }
64
65 void translate_cache_destroy(struct translate_cache *cache)
66 {
67 delete_translates(cache);
68 cso_hash_delete(cache->hash);
69 FREE(cache);
70 }
71
72
73 static INLINE unsigned translate_hash_key_size(struct translate_key *key)
74 {
75 unsigned size = sizeof(struct translate_key) -
76 sizeof(struct translate_element) * (PIPE_MAX_ATTRIBS - key->nr_elements);
77 return size;
78 }
79
80 static INLINE unsigned create_key(struct translate_key *key)
81 {
82 unsigned hash_key;
83 unsigned size = translate_hash_key_size(key);
84 /*debug_printf("key size = %d, (els = %d)\n",
85 size, key->nr_elements);*/
86 hash_key = cso_construct_key(key, size);
87 return hash_key;
88 }
89
90 struct translate * translate_cache_find(struct translate_cache *cache,
91 struct translate_key *key)
92 {
93 unsigned hash_key = create_key(key);
94 struct translate *translate = (struct translate*)
95 cso_hash_find_data_from_template(cache->hash,
96 hash_key,
97 key, sizeof(*key));
98
99 if (!translate) {
100 /* create/insert */
101 translate = translate_create(key);
102 cso_hash_insert(cache->hash, hash_key, translate);
103 }
104
105 return translate;
106 }