| 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 FramebufferCacheRD : public Object { |
| 39 |
GDCLASS(FramebufferCacheRD, Object) |
| 40 |
|
| 41 |
struct Cache { |
| 42 |
Cache *prev = nullptr; |
| 43 |
Cache *next = nullptr; |
| 44 |
uint32_t hash = 0; |
| 45 |
RID cache; |
| 46 |
LocalVector<RID> textures; |
| 47 |
LocalVector<RD::FramebufferPass> passes; |
| 48 |
uint32_t views = 0; |
| 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_pass(const RD::FramebufferPass &p, uint32_t h) { |
| 60 |
h = hash_murmur3_one_32(p.depth_attachment, h); |
| 61 |
h = hash_murmur3_one_32(p.depth_resolve_attachment, h); |
| 62 |
|
| 63 |
h = hash_murmur3_one_32(p.color_attachments.size(), h); |
| 64 |
for (int i = 0; i < p.color_attachments.size(); i++) { |
| 65 |
h = hash_murmur3_one_32(p.color_attachments[i], h); |
| 66 |
} |
| 67 |
|
| 68 |
h = hash_murmur3_one_32(p.resolve_attachments.size(), h); |
| 69 |
for (int i = 0; i < p.resolve_attachments.size(); i++) { |
| 70 |
h = hash_murmur3_one_32(p.resolve_attachments[i], h); |
| 71 |
} |
| 72 |
|
| 73 |
h = hash_murmur3_one_32(p.preserve_attachments.size(), h); |
| 74 |
for (int i = 0; i < p.preserve_attachments.size(); i++) { |
| 75 |
h = hash_murmur3_one_32(p.preserve_attachments[i], h); |
| 76 |
} |
| 77 |
|
| 78 |
return h; |
| 79 |
} |
| 80 |
|
| 81 |
static _FORCE_INLINE_ bool _compare_pass(const RD::FramebufferPass &a, const RD::FramebufferPass &b) { |
| 82 |
if (a.depth_attachment != b.depth_attachment) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
if (a.depth_resolve_attachment != b.depth_resolve_attachment) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
if (a.color_attachments.size() != b.color_attachments.size()) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
for (int i = 0; i < a.color_attachments.size(); i++) { |
| 95 |
if (a.color_attachments[i] != b.color_attachments[i]) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (a.resolve_attachments.size() != b.resolve_attachments.size()) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
for (int i = 0; i < a.resolve_attachments.size(); i++) { |
| 105 |
if (a.resolve_attachments[i] != b.resolve_attachments[i]) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (a.preserve_attachments.size() != b.preserve_attachments.size()) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
for (int i = 0; i < a.preserve_attachments.size(); i++) { |
| 115 |
if (a.preserve_attachments[i] != b.preserve_attachments[i]) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
_FORCE_INLINE_ uint32_t _hash_rids(uint32_t h, const RID &arg) { |
| 124 |
return hash_murmur3_one_64(arg.get_id(), h); |
| 125 |
} |
| 126 |
|
| 127 |
template <typename... Args> |
| 128 |
uint32_t _hash_rids(uint32_t h, const RID &arg, Args... args) { |
| 129 |
h = hash_murmur3_one_64(arg.get_id(), h); |
| 130 |
return _hash_rids(h, args...); |
| 131 |
} |
| 132 |
|
| 133 |
_FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg) { |
| 134 |
return textures[idx] == arg; |
| 135 |
} |
| 136 |
|
| 137 |
template <typename... Args> |
| 138 |
_FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg, Args... args) { |
| 139 |
if (textures[idx] != arg) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
return _compare_args(idx + 1, textures, args...); |
| 143 |
} |
| 144 |
|
| 145 |
static FramebufferCacheRD *singleton; |
| 146 |
|
| 147 |
uint32_t cache_instances_used = 0; |
| 148 |
|
| 149 |
void _invalidate(Cache *p_cache); |
| 150 |
static void _framebuffer_invalidation_callback(void *p_userdata); |
| 151 |
|
| 152 |
RID _allocate_from_data(uint32_t p_views, uint32_t p_hash, uint32_t p_table_idx, const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes) { |
| 153 |
RID rid; |
| 154 |
if (p_passes.size()) { |
| 155 |
rid = RD::get_singleton()->framebuffer_create_multipass(p_textures, p_passes, RD::INVALID_ID, p_views); |
| 156 |
} else { |
| 157 |
rid = RD::get_singleton()->framebuffer_create(p_textures, RD::INVALID_ID, p_views); |
| 158 |
} |
| 159 |
|
| 160 |
ERR_FAIL_COND_V(rid.is_null(), rid); |
| 161 |
|
| 162 |
Cache *c = cache_allocator.alloc(); |
| 163 |
c->views = p_views; |
| 164 |
c->cache = rid; |
| 165 |
c->hash = p_hash; |
| 166 |
c->textures.resize(p_textures.size()); |
| 167 |
for (uint32_t i = 0; i < c->textures.size(); i++) { |
| 168 |
c->textures[i] = p_textures[i]; |
| 169 |
} |
| 170 |
c->passes.resize(p_passes.size()); |
| 171 |
for (uint32_t i = 0; i < c->passes.size(); i++) { |
| 172 |
c->passes[i] = p_passes[i]; |
| 173 |
} |
| 174 |
c->prev = nullptr; |
| 175 |
c->next = hash_table[p_table_idx]; |
| 176 |
if (hash_table[p_table_idx]) { |
| 177 |
hash_table[p_table_idx]->prev = c; |
| 178 |
} |
| 179 |
hash_table[p_table_idx] = c; |
| 180 |
|
| 181 |
RD::get_singleton()->framebuffer_set_invalidation_callback(rid, _framebuffer_invalidation_callback, c); |
| 182 |
|
| 183 |
cache_instances_used++; |
| 184 |
|
| 185 |
return rid; |
| 186 |
} |
| 187 |
|
| 188 |
private: |
| 189 |
static void _bind_methods(); |
| 190 |
|
| 191 |
public: |
| 192 |
template <typename... Args> |
| 193 |
RID get_cache(Args... args) { |
| 194 |
uint32_t h = hash_murmur3_one_32(1); |
| 195 |
h = hash_murmur3_one_32(sizeof...(Args), h); |
| 196 |
h = _hash_rids(h, args...); |
| 197 |
h = hash_murmur3_one_32(0, h); |
| 198 |
h = hash_fmix32(h); |
| 199 |
|
| 200 |
uint32_t table_idx = h % HASH_TABLE_SIZE; |
| 201 |
{ |
| 202 |
const Cache *c = hash_table[table_idx]; |
| 203 |
|
| 204 |
while (c) { |
| 205 |
if (c->hash == h && c->passes.is_empty() && c->textures.size() == sizeof...(Args) && c->views == 1 && _compare_args(0, c->textures, args...)) { |
| 206 |
return c->cache; |
| 207 |
} |
| 208 |
c = c->next; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
return _allocate_from_data(1, h, table_idx, Vector<RID>{ args... }, Vector<RD::FramebufferPass>()); |
| 215 |
} |
| 216 |
|
| 217 |
template <typename... Args> |
| 218 |
RID get_cache_multiview(uint32_t p_views, Args... args) { |
| 219 |
uint32_t h = hash_murmur3_one_32(p_views); |
| 220 |
h = hash_murmur3_one_32(sizeof...(Args), h); |
| 221 |
h = _hash_rids(h, args...); |
| 222 |
h = hash_murmur3_one_32(0, h); |
| 223 |
h = hash_fmix32(h); |
| 224 |
|
| 225 |
uint32_t table_idx = h % HASH_TABLE_SIZE; |
| 226 |
{ |
| 227 |
const Cache *c = hash_table[table_idx]; |
| 228 |
|
| 229 |
while (c) { |
| 230 |
if (c->hash == h && c->passes.is_empty() && c->textures.size() == sizeof...(Args) && c->views == p_views && _compare_args(0, c->textures, args...)) { |
| 231 |
return c->cache; |
| 232 |
} |
| 233 |
c = c->next; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
return _allocate_from_data(p_views, h, table_idx, Vector<RID>{ args... }, Vector<RD::FramebufferPass>()); |
| 240 |
} |
| 241 |
|
| 242 |
RID get_cache_multipass(const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes, uint32_t p_views = 1) { |
| 243 |
uint32_t h = hash_murmur3_one_32(p_views); |
| 244 |
h = hash_murmur3_one_32(p_textures.size(), h); |
| 245 |
for (int i = 0; i < p_textures.size(); i++) { |
| 246 |
h = hash_murmur3_one_64(p_textures[i].get_id(), h); |
| 247 |
} |
| 248 |
h = hash_murmur3_one_32(p_passes.size(), h); |
| 249 |
for (int i = 0; i < p_passes.size(); i++) { |
| 250 |
h = _hash_pass(p_passes[i], h); |
| 251 |
} |
| 252 |
|
| 253 |
h = hash_fmix32(h); |
| 254 |
|
| 255 |
uint32_t table_idx = h % HASH_TABLE_SIZE; |
| 256 |
{ |
| 257 |
const Cache *c = hash_table[table_idx]; |
| 258 |
|
| 259 |
while (c) { |
| 260 |
if (c->hash == h && c->views == p_views && c->textures.size() == (uint32_t)p_textures.size() && c->passes.size() == (uint32_t)p_passes.size()) { |
| 261 |
bool all_ok = true; |
| 262 |
|
| 263 |
for (int i = 0; i < p_textures.size(); i++) { |
| 264 |
if (p_textures[i] != c->textures[i]) { |
| 265 |
all_ok = false; |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if (all_ok) { |
| 271 |
for (int i = 0; i < p_passes.size(); i++) { |
| 272 |
if (!_compare_pass(p_passes[i], c->passes[i])) { |
| 273 |
all_ok = false; |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if (all_ok) { |
| 280 |
return c->cache; |
| 281 |
} |
| 282 |
} |
| 283 |
c = c->next; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
return _allocate_from_data(p_views, h, table_idx, p_textures, p_passes); |
| 289 |
} |
| 290 |
|
| 291 |
static RID get_cache_multipass_array(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_views = 1); |
| 292 |
|
| 293 |
static FramebufferCacheRD *get_singleton() { return singleton; } |
| 294 |
|
| 295 |
FramebufferCacheRD(); |
| 296 |
~FramebufferCacheRD(); |
| 297 |
}; |