Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/drivers/gles3/storage/utilities.h
10278 views
1
/**************************************************************************/
2
/* utilities.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
#ifdef GLES3_ENABLED
34
35
#include "servers/rendering/storage/utilities.h"
36
37
#include "platform_gl.h"
38
39
namespace GLES3 {
40
41
/* VISIBILITY NOTIFIER */
42
43
struct VisibilityNotifier {
44
AABB aabb;
45
Callable enter_callback;
46
Callable exit_callback;
47
Dependency dependency;
48
};
49
50
class Utilities : public RendererUtilities {
51
private:
52
static Utilities *singleton;
53
54
/* VISIBILITY NOTIFIER */
55
56
mutable RID_Owner<VisibilityNotifier> visibility_notifier_owner;
57
58
/* MISC */
59
60
struct ResourceAllocation {
61
#ifdef DEV_ENABLED
62
String name;
63
#endif
64
uint32_t size = 0;
65
};
66
HashMap<GLuint, ResourceAllocation> buffer_allocs_cache;
67
HashMap<GLuint, ResourceAllocation> render_buffer_allocs_cache;
68
HashMap<GLuint, ResourceAllocation> texture_allocs_cache;
69
70
uint64_t buffer_mem_cache = 0;
71
uint64_t render_buffer_mem_cache = 0;
72
uint64_t texture_mem_cache = 0;
73
74
public:
75
static Utilities *get_singleton() { return singleton; }
76
77
Utilities();
78
~Utilities();
79
80
// Buffer size is specified in bytes
81
static Vector<uint8_t> buffer_get_data(GLenum p_target, GLuint p_buffer, uint32_t p_buffer_size);
82
83
// Allocate memory with glBufferData. Does not handle resizing.
84
_FORCE_INLINE_ void buffer_allocate_data(GLenum p_target, GLuint p_id, uint32_t p_size, const void *p_data, GLenum p_usage, String p_name = "") {
85
glBufferData(p_target, p_size, p_data, p_usage);
86
buffer_mem_cache += p_size;
87
88
#ifdef DEV_ENABLED
89
ERR_FAIL_COND_MSG(buffer_allocs_cache.has(p_id), "trying to allocate buffer with name " + p_name + " but ID already used by " + buffer_allocs_cache[p_id].name);
90
#endif
91
92
ResourceAllocation resource_allocation;
93
resource_allocation.size = p_size;
94
#ifdef DEV_ENABLED
95
resource_allocation.name = p_name + ": " + itos((uint64_t)p_id);
96
#endif
97
buffer_allocs_cache[p_id] = resource_allocation;
98
}
99
100
_FORCE_INLINE_ void buffer_free_data(GLuint p_id) {
101
ERR_FAIL_COND(!buffer_allocs_cache.has(p_id));
102
glDeleteBuffers(1, &p_id);
103
buffer_mem_cache -= buffer_allocs_cache[p_id].size;
104
buffer_allocs_cache.erase(p_id);
105
}
106
107
_FORCE_INLINE_ void render_buffer_allocated_data(GLuint p_id, uint32_t p_size, String p_name = "") {
108
render_buffer_mem_cache += p_size;
109
#ifdef DEV_ENABLED
110
ERR_FAIL_COND_MSG(render_buffer_allocs_cache.has(p_id), "trying to allocate render buffer with name " + p_name + " but ID already used by " + render_buffer_allocs_cache[p_id].name);
111
#endif
112
ResourceAllocation resource_allocation;
113
resource_allocation.size = p_size;
114
#ifdef DEV_ENABLED
115
resource_allocation.name = p_name + ": " + itos((uint64_t)p_id);
116
#endif
117
render_buffer_allocs_cache[p_id] = resource_allocation;
118
}
119
120
_FORCE_INLINE_ void render_buffer_free_data(GLuint p_id) {
121
ERR_FAIL_COND(!render_buffer_allocs_cache.has(p_id));
122
glDeleteRenderbuffers(1, &p_id);
123
render_buffer_mem_cache -= render_buffer_allocs_cache[p_id].size;
124
render_buffer_allocs_cache.erase(p_id);
125
}
126
127
// Records that data was allocated for state tracking purposes.
128
// Size is measured in bytes.
129
_FORCE_INLINE_ void texture_allocated_data(GLuint p_id, uint32_t p_size, String p_name = "") {
130
texture_mem_cache += p_size;
131
#ifdef DEV_ENABLED
132
ERR_FAIL_COND_MSG(texture_allocs_cache.has(p_id), "trying to allocate texture with name " + p_name + " but ID already used by " + texture_allocs_cache[p_id].name);
133
#endif
134
ResourceAllocation resource_allocation;
135
resource_allocation.size = p_size;
136
#ifdef DEV_ENABLED
137
resource_allocation.name = p_name + ": " + itos((uint64_t)p_id);
138
#endif
139
texture_allocs_cache[p_id] = resource_allocation;
140
}
141
142
_FORCE_INLINE_ void texture_free_data(GLuint p_id) {
143
ERR_FAIL_COND(!texture_allocs_cache.has(p_id));
144
glDeleteTextures(1, &p_id);
145
texture_mem_cache -= texture_allocs_cache[p_id].size;
146
texture_allocs_cache.erase(p_id);
147
}
148
149
_FORCE_INLINE_ void texture_resize_data(GLuint p_id, uint32_t p_size) {
150
ERR_FAIL_COND(!texture_allocs_cache.has(p_id));
151
texture_mem_cache -= texture_allocs_cache[p_id].size;
152
texture_mem_cache += p_size;
153
texture_allocs_cache[p_id].size = p_size;
154
}
155
156
/* INSTANCES */
157
158
virtual RS::InstanceType get_base_type(RID p_rid) const override;
159
virtual bool free(RID p_rid) override;
160
161
/* DEPENDENCIES */
162
163
virtual void base_update_dependency(RID p_base, DependencyTracker *p_instance) override;
164
165
/* VISIBILITY NOTIFIER */
166
167
VisibilityNotifier *get_visibility_notifier(RID p_rid) { return visibility_notifier_owner.get_or_null(p_rid); }
168
bool owns_visibility_notifier(RID p_rid) const { return visibility_notifier_owner.owns(p_rid); }
169
170
virtual RID visibility_notifier_allocate() override;
171
virtual void visibility_notifier_initialize(RID p_notifier) override;
172
virtual void visibility_notifier_free(RID p_notifier) override;
173
174
virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) override;
175
virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) override;
176
177
virtual AABB visibility_notifier_get_aabb(RID p_notifier) const override;
178
virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) override;
179
180
/* TIMING */
181
182
#define MAX_QUERIES 256
183
#define FRAME_COUNT 3
184
185
struct Frame {
186
GLuint queries[MAX_QUERIES];
187
TightLocalVector<String> timestamp_names;
188
TightLocalVector<uint64_t> timestamp_cpu_values;
189
uint32_t timestamp_count = 0;
190
TightLocalVector<String> timestamp_result_names;
191
TightLocalVector<uint64_t> timestamp_cpu_result_values;
192
TightLocalVector<uint64_t> timestamp_result_values;
193
uint32_t timestamp_result_count = 0;
194
uint64_t index = 0;
195
};
196
197
const uint32_t max_timestamp_query_elements = MAX_QUERIES;
198
199
Frame frames[FRAME_COUNT]; // Frames for capturing timestamps. We use 3 so we don't need to wait for commands to complete
200
uint32_t frame = 0;
201
202
virtual void capture_timestamps_begin() override;
203
virtual void capture_timestamp(const String &p_name) override;
204
virtual uint32_t get_captured_timestamps_count() const override;
205
virtual uint64_t get_captured_timestamps_frame() const override;
206
virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const override;
207
virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const override;
208
virtual String get_captured_timestamp_name(uint32_t p_index) const override;
209
void _capture_timestamps_begin();
210
void capture_timestamps_end();
211
212
/* MISC */
213
214
virtual void update_dirty_resources() override;
215
virtual void set_debug_generate_wireframes(bool p_generate) override;
216
217
virtual bool has_os_feature(const String &p_feature) const override;
218
219
virtual void update_memory_info() override;
220
221
virtual uint64_t get_rendering_info(RS::RenderingInfo p_info) override;
222
virtual String get_video_adapter_name() const override;
223
virtual String get_video_adapter_vendor() const override;
224
virtual RenderingDevice::DeviceType get_video_adapter_type() const override;
225
virtual String get_video_adapter_api_version() const override;
226
227
virtual Size2i get_maximum_viewport_size() const override;
228
virtual uint32_t get_maximum_shader_varyings() const override;
229
virtual uint64_t get_maximum_uniform_buffer_size() const override;
230
};
231
232
} // namespace GLES3
233
234
#endif // GLES3_ENABLED
235
236