1 | /**************************************************************************/ |
2 | /* uniform_set_cache_rd.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef UNIFORM_SET_CACHE_RD_H |
32 | #define UNIFORM_SET_CACHE_RD_H |
33 | |
34 | #include "core/templates/local_vector.h" |
35 | #include "core/templates/paged_allocator.h" |
36 | #include "servers/rendering/rendering_device.h" |
37 | |
38 | class UniformSetCacheRD : public Object { |
39 | GDCLASS(UniformSetCacheRD, Object) |
40 | |
41 | struct Cache { |
42 | Cache *prev = nullptr; |
43 | Cache *next = nullptr; |
44 | uint32_t hash = 0; |
45 | RID shader; |
46 | uint32_t set = 0; |
47 | RID cache; |
48 | LocalVector<RD::Uniform> uniforms; |
49 | }; |
50 | |
51 | PagedAllocator<Cache> cache_allocator; |
52 | |
53 | enum { |
54 | HASH_TABLE_SIZE = 16381 // Prime |
55 | }; |
56 | |
57 | Cache *hash_table[HASH_TABLE_SIZE] = {}; |
58 | |
59 | static _FORCE_INLINE_ uint32_t _hash_uniform(const RD::Uniform &u, uint32_t h) { |
60 | h = hash_murmur3_one_32(u.uniform_type, h); |
61 | h = hash_murmur3_one_32(u.binding, h); |
62 | uint32_t rsize = u.get_id_count(); |
63 | for (uint32_t j = 0; j < rsize; j++) { |
64 | h = hash_murmur3_one_64(u.get_id(j).get_id(), h); |
65 | } |
66 | return hash_fmix32(h); |
67 | } |
68 | |
69 | static _FORCE_INLINE_ bool _compare_uniform(const RD::Uniform &a, const RD::Uniform &b) { |
70 | if (a.binding != b.binding) { |
71 | return false; |
72 | } |
73 | if (a.uniform_type != b.uniform_type) { |
74 | return false; |
75 | } |
76 | uint32_t rsize = a.get_id_count(); |
77 | if (rsize != b.get_id_count()) { |
78 | return false; |
79 | } |
80 | for (uint32_t j = 0; j < rsize; j++) { |
81 | if (a.get_id(j) != b.get_id(j)) { |
82 | return false; |
83 | } |
84 | } |
85 | return true; |
86 | } |
87 | |
88 | _FORCE_INLINE_ uint32_t _hash_args(uint32_t h, const RD::Uniform &arg) { |
89 | return _hash_uniform(arg, h); |
90 | } |
91 | |
92 | template <typename... Args> |
93 | uint32_t _hash_args(uint32_t h, const RD::Uniform &arg, Args... args) { |
94 | h = _hash_uniform(arg, h); |
95 | return _hash_args(h, args...); |
96 | } |
97 | |
98 | _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RD::Uniform> &uniforms, const RD::Uniform &arg) { |
99 | return _compare_uniform(uniforms[idx], arg); |
100 | } |
101 | |
102 | template <typename... Args> |
103 | _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RD::Uniform> &uniforms, const RD::Uniform &arg, Args... args) { |
104 | if (!_compare_uniform(uniforms[idx], arg)) { |
105 | return false; |
106 | } |
107 | return _compare_args(idx + 1, uniforms, args...); |
108 | } |
109 | |
110 | _FORCE_INLINE_ void _create_args(Vector<RD::Uniform> &uniforms, const RD::Uniform &arg) { |
111 | uniforms.push_back(arg); |
112 | } |
113 | |
114 | template <typename... Args> |
115 | _FORCE_INLINE_ void _create_args(Vector<RD::Uniform> &uniforms, const RD::Uniform &arg, Args... args) { |
116 | uniforms.push_back(arg); |
117 | _create_args(uniforms, args...); |
118 | } |
119 | |
120 | static UniformSetCacheRD *singleton; |
121 | |
122 | uint32_t cache_instances_used = 0; |
123 | |
124 | void _invalidate(Cache *p_cache); |
125 | static void _uniform_set_invalidation_callback(void *p_userdata); |
126 | |
127 | RID _allocate_from_uniforms(RID p_shader, uint32_t p_set, uint32_t p_hash, uint32_t p_table_idx, const Vector<RD::Uniform> &p_uniforms) { |
128 | RID rid = RD::get_singleton()->uniform_set_create(p_uniforms, p_shader, p_set); |
129 | ERR_FAIL_COND_V(rid.is_null(), rid); |
130 | |
131 | Cache *c = cache_allocator.alloc(); |
132 | c->hash = p_hash; |
133 | c->set = p_set; |
134 | c->shader = p_shader; |
135 | c->cache = rid; |
136 | c->uniforms.resize(p_uniforms.size()); |
137 | for (uint32_t i = 0; i < c->uniforms.size(); i++) { |
138 | c->uniforms[i] = p_uniforms[i]; |
139 | } |
140 | c->prev = nullptr; |
141 | c->next = hash_table[p_table_idx]; |
142 | if (hash_table[p_table_idx]) { |
143 | hash_table[p_table_idx]->prev = c; |
144 | } |
145 | hash_table[p_table_idx] = c; |
146 | |
147 | RD::get_singleton()->uniform_set_set_invalidation_callback(rid, _uniform_set_invalidation_callback, c); |
148 | |
149 | cache_instances_used++; |
150 | |
151 | return rid; |
152 | } |
153 | |
154 | public: |
155 | template <typename... Args> |
156 | RID get_cache(RID p_shader, uint32_t p_set, Args... args) { |
157 | uint32_t h = hash_murmur3_one_64(p_shader.get_id()); |
158 | h = hash_murmur3_one_32(p_set, h); |
159 | h = _hash_args(h, args...); |
160 | |
161 | uint32_t table_idx = h % HASH_TABLE_SIZE; |
162 | { |
163 | const Cache *c = hash_table[table_idx]; |
164 | |
165 | while (c) { |
166 | if (c->hash == h && c->set == p_set && c->shader == p_shader && sizeof...(Args) == c->uniforms.size() && _compare_args(0, c->uniforms, args...)) { |
167 | return c->cache; |
168 | } |
169 | c = c->next; |
170 | } |
171 | } |
172 | |
173 | // Not in cache, create: |
174 | |
175 | Vector<RD::Uniform> uniforms; |
176 | _create_args(uniforms, args...); |
177 | |
178 | return _allocate_from_uniforms(p_shader, p_set, h, table_idx, uniforms); |
179 | } |
180 | |
181 | template <typename... Args> |
182 | RID get_cache_vec(RID p_shader, uint32_t p_set, const Vector<RD::Uniform> &p_uniforms) { |
183 | uint32_t h = hash_murmur3_one_64(p_shader.get_id()); |
184 | h = hash_murmur3_one_32(p_set, h); |
185 | for (int i = 0; i < p_uniforms.size(); i++) { |
186 | h = _hash_uniform(p_uniforms[i], h); |
187 | } |
188 | |
189 | h = hash_fmix32(h); |
190 | |
191 | uint32_t table_idx = h % HASH_TABLE_SIZE; |
192 | { |
193 | const Cache *c = hash_table[table_idx]; |
194 | |
195 | while (c) { |
196 | if (c->hash == h && c->set == p_set && c->shader == p_shader && (uint32_t)p_uniforms.size() == c->uniforms.size()) { |
197 | bool all_ok = true; |
198 | for (int i = 0; i < p_uniforms.size(); i++) { |
199 | if (!_compare_uniform(p_uniforms[i], c->uniforms[i])) { |
200 | all_ok = false; |
201 | break; |
202 | } |
203 | } |
204 | |
205 | if (all_ok) { |
206 | return c->cache; |
207 | } |
208 | } |
209 | c = c->next; |
210 | } |
211 | } |
212 | |
213 | // Not in cache, create: |
214 | return _allocate_from_uniforms(p_shader, p_set, h, table_idx, p_uniforms); |
215 | } |
216 | |
217 | static UniformSetCacheRD *get_singleton() { return singleton; } |
218 | |
219 | UniformSetCacheRD(); |
220 | ~UniformSetCacheRD(); |
221 | }; |
222 | |
223 | #endif // UNIFORM_SET_CACHE_RD_H |
224 | |