Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/freedreno/ir3/ir3_cache.c
4574 views
1
/*
2
* Copyright (C) 2015 Rob Clark <[email protected]>
3
*
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:
10
*
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
13
* Software.
14
*
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
21
* SOFTWARE.
22
*
23
* Authors:
24
* Rob Clark <[email protected]>
25
*/
26
27
#include "util/hash_table.h"
28
#include "util/ralloc.h"
29
#define XXH_INLINE_ALL
30
#include "util/xxhash.h"
31
32
#include "ir3_cache.h"
33
#include "ir3_gallium.h"
34
35
static uint32_t
36
key_hash(const void *_key)
37
{
38
const struct ir3_cache_key *key = _key;
39
return XXH32(key, sizeof(*key), 0);
40
}
41
42
static bool
43
key_equals(const void *_a, const void *_b)
44
{
45
const struct ir3_cache_key *a = _a;
46
const struct ir3_cache_key *b = _b;
47
// TODO we could optimize the key shader-variant key comparison by not
48
// ignoring has_per_samp.. not really sure if that helps..
49
return memcmp(a, b, sizeof(struct ir3_cache_key)) == 0;
50
}
51
52
struct ir3_cache {
53
/* cache mapping gallium/etc shader state-objs + shader-key to backend
54
* specific state-object
55
*/
56
struct hash_table *ht;
57
58
const struct ir3_cache_funcs *funcs;
59
void *data;
60
};
61
62
struct ir3_cache *
63
ir3_cache_create(const struct ir3_cache_funcs *funcs, void *data)
64
{
65
struct ir3_cache *cache = rzalloc(NULL, struct ir3_cache);
66
67
cache->ht = _mesa_hash_table_create(cache, key_hash, key_equals);
68
cache->funcs = funcs;
69
cache->data = data;
70
71
return cache;
72
}
73
74
void
75
ir3_cache_destroy(struct ir3_cache *cache)
76
{
77
if (!cache)
78
return;
79
80
/* _mesa_hash_table_destroy is so *almost* useful.. */
81
hash_table_foreach (cache->ht, entry) {
82
cache->funcs->destroy_state(cache->data, entry->data);
83
}
84
85
ralloc_free(cache);
86
}
87
88
struct ir3_program_state *
89
ir3_cache_lookup(struct ir3_cache *cache, const struct ir3_cache_key *key,
90
struct pipe_debug_callback *debug)
91
{
92
uint32_t hash = key_hash(key);
93
struct hash_entry *entry =
94
_mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
95
96
if (entry) {
97
return entry->data;
98
}
99
100
if (key->hs)
101
debug_assert(key->ds);
102
103
struct ir3_shader *shaders[MESA_SHADER_STAGES] = {
104
[MESA_SHADER_VERTEX] = ir3_get_shader(key->vs),
105
[MESA_SHADER_TESS_CTRL] = ir3_get_shader(key->hs),
106
[MESA_SHADER_TESS_EVAL] = ir3_get_shader(key->ds),
107
[MESA_SHADER_GEOMETRY] = ir3_get_shader(key->gs),
108
[MESA_SHADER_FRAGMENT] = ir3_get_shader(key->fs),
109
};
110
111
struct ir3_shader_variant *variants[MESA_SHADER_STAGES];
112
struct ir3_shader_key shader_key = key->key;
113
114
for (gl_shader_stage stage = MESA_SHADER_VERTEX; stage < MESA_SHADER_STAGES;
115
stage++) {
116
if (shaders[stage]) {
117
variants[stage] =
118
ir3_shader_variant(shaders[stage], shader_key, false, debug);
119
if (!variants[stage])
120
return NULL;
121
} else {
122
variants[stage] = NULL;
123
}
124
}
125
126
struct ir3_compiler *compiler = shaders[MESA_SHADER_VERTEX]->compiler;
127
uint32_t safe_constlens = ir3_trim_constlen(variants, compiler);
128
shader_key.safe_constlen = true;
129
130
for (gl_shader_stage stage = MESA_SHADER_VERTEX; stage < MESA_SHADER_STAGES;
131
stage++) {
132
if (safe_constlens & (1 << stage)) {
133
variants[stage] =
134
ir3_shader_variant(shaders[stage], shader_key, false, debug);
135
if (!variants[stage])
136
return NULL;
137
}
138
}
139
140
struct ir3_shader_variant *bs;
141
142
if (ir3_has_binning_vs(&key->key)) {
143
shader_key.safe_constlen = !!(safe_constlens & (1 << MESA_SHADER_VERTEX));
144
bs =
145
ir3_shader_variant(shaders[MESA_SHADER_VERTEX], key->key, true, debug);
146
if (!bs)
147
return NULL;
148
} else {
149
bs = variants[MESA_SHADER_VERTEX];
150
}
151
152
struct ir3_program_state *state = cache->funcs->create_state(
153
cache->data, bs, variants[MESA_SHADER_VERTEX],
154
variants[MESA_SHADER_TESS_CTRL], variants[MESA_SHADER_TESS_EVAL],
155
variants[MESA_SHADER_GEOMETRY], variants[MESA_SHADER_FRAGMENT],
156
&key->key);
157
state->key = *key;
158
159
/* NOTE: uses copy of key in state obj, because pointer passed by caller
160
* is probably on the stack
161
*/
162
_mesa_hash_table_insert_pre_hashed(cache->ht, hash, &state->key, state);
163
164
return state;
165
}
166
167
/* call when an API level state object is destroyed, to invalidate
168
* cache entries which reference that state object.
169
*/
170
void
171
ir3_cache_invalidate(struct ir3_cache *cache, void *stobj)
172
{
173
if (!cache)
174
return;
175
176
hash_table_foreach (cache->ht, entry) {
177
const struct ir3_cache_key *key = entry->key;
178
if ((key->fs == stobj) || (key->vs == stobj) || (key->ds == stobj) ||
179
(key->hs == stobj) || (key->gs == stobj)) {
180
cache->funcs->destroy_state(cache->data, entry->data);
181
_mesa_hash_table_remove(cache->ht, entry);
182
return;
183
}
184
}
185
}
186
187