2 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/ralloc.h"
28 #include "util/hash_table.h"
29 #define XXH_INLINE_ALL
30 #include "util/xxhash.h"
32 #include "ir3_cache.h"
33 #include "ir3_gallium.h"
37 key_hash(const void *_key
)
39 const struct ir3_cache_key
*key
= _key
;
40 return XXH32(key
, sizeof(*key
), 0);
44 key_equals(const void *_a
, const void *_b
)
46 const struct ir3_cache_key
*a
= _a
;
47 const struct ir3_cache_key
*b
= _b
;
48 // TODO we could optimize the key shader-variant key comparison by not
49 // ignoring has_per_samp.. not really sure if that helps..
50 return memcmp(a
, b
, sizeof(struct ir3_cache_key
)) == 0;
54 /* cache mapping gallium/etc shader state-objs + shader-key to backend
55 * specific state-object
57 struct hash_table
*ht
;
59 const struct ir3_cache_funcs
*funcs
;
63 struct ir3_cache
* ir3_cache_create(const struct ir3_cache_funcs
*funcs
, void *data
)
65 struct ir3_cache
*cache
= rzalloc(NULL
, struct ir3_cache
);
67 cache
->ht
= _mesa_hash_table_create(cache
, key_hash
, key_equals
);
74 void ir3_cache_destroy(struct ir3_cache
*cache
)
76 /* _mesa_hash_table_destroy is so *almost* useful.. */
77 hash_table_foreach(cache
->ht
, entry
) {
78 cache
->funcs
->destroy_state(cache
->data
, entry
->data
);
84 struct ir3_program_state
*
85 ir3_cache_lookup(struct ir3_cache
*cache
, const struct ir3_cache_key
*key
,
86 struct pipe_debug_callback
*debug
)
88 uint32_t hash
= key_hash(key
);
89 struct hash_entry
*entry
=
90 _mesa_hash_table_search_pre_hashed(cache
->ht
, hash
, key
);
96 struct ir3_shader_variant
*vs
= ir3_shader_variant(key
->vs
, key
->key
, false, debug
);
100 struct ir3_shader_variant
*hs
= NULL
, *ds
= NULL
;
102 debug_assert(key
->ds
);
103 hs
= ir3_shader_variant(key
->hs
, key
->key
, false, debug
);
104 ds
= ir3_shader_variant(key
->ds
, key
->key
, false, debug
);
109 /* For tessellation, the binning shader is derived from the DS. */
110 struct ir3_shader_variant
*bs
;
112 bs
= ir3_shader_variant(key
->ds
, key
->key
, true, debug
);
114 bs
= ir3_shader_variant(key
->vs
, key
->key
, true, debug
);
118 struct ir3_shader_variant
*gs
= NULL
;
120 gs
= ir3_shader_variant(key
->gs
, key
->key
, false, debug
);
125 struct ir3_shader_variant
*fs
= ir3_shader_variant(key
->fs
, key
->key
, false, debug
);
129 struct ir3_program_state
*state
=
130 cache
->funcs
->create_state(cache
->data
, bs
, vs
, hs
, ds
, gs
, fs
, &key
->key
);
133 /* NOTE: uses copy of key in state obj, because pointer passed by caller
134 * is probably on the stack
136 _mesa_hash_table_insert_pre_hashed(cache
->ht
, hash
, &state
->key
, state
);
141 /* call when an API level state object is destroyed, to invalidate
142 * cache entries which reference that state object.
144 void ir3_cache_invalidate(struct ir3_cache
*cache
, void *stobj
)
146 hash_table_foreach(cache
->ht
, entry
) {
147 const struct ir3_cache_key
*key
= entry
->key
;
148 if ((key
->fs
== stobj
) || (key
->vs
== stobj
)) {
149 cache
->funcs
->destroy_state(cache
->data
, entry
->data
);
150 _mesa_hash_table_remove(cache
->ht
, entry
);