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
);
43 cache
->hash
= cso_hash_create();
48 static INLINE
void delete_translates(struct translate_cache
*cache
)
50 struct cso_hash
*hash
= cache
->hash
;
51 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
52 while (!cso_hash_iter_is_null(iter
)) {
53 struct translate
*state
= (struct translate
*)cso_hash_iter_data(iter
);
54 iter
= cso_hash_iter_next(iter
);
56 state
->release(state
);
61 void translate_cache_destroy(struct translate_cache
*cache
)
63 delete_translates(cache
);
64 cso_hash_delete(cache
->hash
);
69 static INLINE
unsigned translate_hash_key_size(struct translate_key
*key
)
71 unsigned size
= sizeof(struct translate_key
) -
72 sizeof(struct translate_element
) * (PIPE_MAX_ATTRIBS
- key
->nr_elements
);
76 static INLINE
unsigned create_key(struct translate_key
*key
)
79 unsigned size
= translate_hash_key_size(key
);
80 /*debug_printf("key size = %d, (els = %d)\n",
81 size, key->nr_elements);*/
82 hash_key
= cso_construct_key(key
, size
);
86 struct translate
* translate_cache_find(struct translate_cache
*cache
,
87 struct translate_key
*key
)
89 unsigned hash_key
= create_key(key
);
90 struct translate
*translate
= (struct translate
*)
91 cso_hash_find_data_from_template(cache
->hash
,
97 translate
= translate_create(key
);
98 cso_hash_insert(cache
->hash
, hash_key
, translate
);