| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/local_vector.h" |
| 34 |
#include "core/templates/paged_allocator.h" |
| 35 |
#include "servers/rendering/rendering_device.h" |
| 36 |
#include "servers/rendering/rendering_device_binds.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 |
| 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 |
static UniformSetCacheRD *singleton; |
| 111 |
|
| 112 |
uint32_t cache_instances_used = 0; |
| 113 |
|
| 114 |
void _invalidate(Cache *p_cache); |
| 115 |
static void _uniform_set_invalidation_callback(void *p_userdata); |
| 116 |
|
| 117 |
template <typename Collection> |
| 118 |
RID _allocate_from_uniforms(RID p_shader, uint32_t p_set, uint32_t p_hash, uint32_t p_table_idx, const Collection &p_uniforms) { |
| 119 |
RID rid = RD::get_singleton()->uniform_set_create(p_uniforms, p_shader, p_set); |
| 120 |
ERR_FAIL_COND_V(rid.is_null(), rid); |
| 121 |
|
| 122 |
Cache *c = cache_allocator.alloc(); |
| 123 |
c->hash = p_hash; |
| 124 |
c->set = p_set; |
| 125 |
c->shader = p_shader; |
| 126 |
c->cache = rid; |
| 127 |
c->uniforms.resize(p_uniforms.size()); |
| 128 |
for (uint32_t i = 0; i < c->uniforms.size(); i++) { |
| 129 |
c->uniforms[i] = p_uniforms[i]; |
| 130 |
} |
| 131 |
c->prev = nullptr; |
| 132 |
c->next = hash_table[p_table_idx]; |
| 133 |
if (hash_table[p_table_idx]) { |
| 134 |
hash_table[p_table_idx]->prev = c; |
| 135 |
} |
| 136 |
hash_table[p_table_idx] = c; |
| 137 |
|
| 138 |
RD::get_singleton()->uniform_set_set_invalidation_callback(rid, _uniform_set_invalidation_callback, c); |
| 139 |
|
| 140 |
cache_instances_used++; |
| 141 |
|
| 142 |
return rid; |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
static void _bind_methods(); |
| 147 |
|
| 148 |
public: |
| 149 |
template <typename... Args> |
| 150 |
RID get_cache(RID p_shader, uint32_t p_set, Args... args) { |
| 151 |
uint32_t h = hash_murmur3_one_64(p_shader.get_id()); |
| 152 |
h = hash_murmur3_one_32(p_set, h); |
| 153 |
h = _hash_args(h, args...); |
| 154 |
|
| 155 |
uint32_t table_idx = h % HASH_TABLE_SIZE; |
| 156 |
{ |
| 157 |
const Cache *c = hash_table[table_idx]; |
| 158 |
|
| 159 |
while (c) { |
| 160 |
if (c->hash == h && c->set == p_set && c->shader == p_shader && sizeof...(Args) == c->uniforms.size() && _compare_args(0, c->uniforms, args...)) { |
| 161 |
return c->cache; |
| 162 |
} |
| 163 |
c = c->next; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
return _allocate_from_uniforms(p_shader, p_set, h, table_idx, Vector<RD::Uniform>{ args... }); |
| 170 |
} |
| 171 |
|
| 172 |
template <typename... Args> |
| 173 |
RID get_cache_vec(RID p_shader, uint32_t p_set, const LocalVector<RD::Uniform> &p_uniforms) { |
| 174 |
uint32_t h = hash_murmur3_one_64(p_shader.get_id()); |
| 175 |
h = hash_murmur3_one_32(p_set, h); |
| 176 |
for (uint32_t i = 0; i < p_uniforms.size(); i++) { |
| 177 |
h = _hash_uniform(p_uniforms[i], h); |
| 178 |
} |
| 179 |
|
| 180 |
h = hash_fmix32(h); |
| 181 |
|
| 182 |
uint32_t table_idx = h % HASH_TABLE_SIZE; |
| 183 |
{ |
| 184 |
const Cache *c = hash_table[table_idx]; |
| 185 |
|
| 186 |
while (c) { |
| 187 |
if (c->hash == h && c->set == p_set && c->shader == p_shader && (uint32_t)p_uniforms.size() == c->uniforms.size()) { |
| 188 |
bool all_ok = true; |
| 189 |
for (uint32_t i = 0; i < p_uniforms.size(); i++) { |
| 190 |
if (!_compare_uniform(p_uniforms[i], c->uniforms[i])) { |
| 191 |
all_ok = false; |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if (all_ok) { |
| 197 |
return c->cache; |
| 198 |
} |
| 199 |
} |
| 200 |
c = c->next; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
return _allocate_from_uniforms(p_shader, p_set, h, table_idx, p_uniforms); |
| 206 |
} |
| 207 |
|
| 208 |
static RID get_cache_array(RID p_shader, uint32_t p_set, const TypedArray<RDUniform> &p_uniforms); |
| 209 |
|
| 210 |
static UniformSetCacheRD *get_singleton() { return singleton; } |
| 211 |
|
| 212 |
UniformSetCacheRD(); |
| 213 |
~UniformSetCacheRD(); |
| 214 |
}; |