1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
28 #include "util/u_memory.h"
29 #include "pipe/p_state.h"
30 #include "translate.h"
31 #include "translate_cache.h"
33 #include "cso_cache/cso_cache.h"
34 #include "cso_cache/cso_hash.h"
36 struct translate_cache
{
37 struct cso_hash
*hash
;
40 struct translate_cache
* translate_cache_create( void )
42 struct translate_cache
*cache
= MALLOC_STRUCT(translate_cache
);
47 cache
->hash
= cso_hash_create();
52 static INLINE
void delete_translates(struct translate_cache
*cache
)
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
);
60 state
->release(state
);
65 void translate_cache_destroy(struct translate_cache
*cache
)
67 delete_translates(cache
);
68 cso_hash_delete(cache
->hash
);
73 static INLINE
unsigned translate_hash_key_size(struct translate_key
*key
)
75 unsigned size
= sizeof(struct translate_key
) -
76 sizeof(struct translate_element
) * (PIPE_MAX_ATTRIBS
- key
->nr_elements
);
80 static INLINE
unsigned create_key(struct translate_key
*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
);
90 struct translate
* translate_cache_find(struct translate_cache
*cache
,
91 struct translate_key
*key
)
93 unsigned hash_key
= create_key(key
);
94 struct translate
*translate
= (struct translate
*)
95 cso_hash_find_data_from_template(cache
->hash
,
101 translate
= translate_create(key
);
102 cso_hash_insert(cache
->hash
, hash_key
, translate
);