Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/renderer_rd/uniform_set_cache_rd.h
10278 views
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
#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 // 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
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
// Not in cache, create:
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
// Not in cache, create:
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
};
215
216