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