Debian Codegraph @

drivers/gles3/storage/utilities.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* utilities.cpp */
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 #ifdef GLES3_ENABLED
32
33 #include "utilities.h"
34
35 #include "../rasterizer_gles3.h"
36 #include "config.h"
37 #include "light_storage.h"
38 #include "material_storage.h"
39 #include "mesh_storage.h"
40 #include "particles_storage.h"
41 #include "texture_storage.h"
42
43 using namespace GLES3;
44
45 Utilities *Utilities::singleton = nullptr;
46
47 Utilities::Utilities() {
48 singleton = this;
49 frame = 0;
50 for (int i = 0; i < FRAME_COUNT; i++) {
51 frames[i].index = 0;
52 glGenQueries(max_timestamp_query_elements, frames[i].queries);
53
54 frames[i].timestamp_names.resize(max_timestamp_query_elements);
55 frames[i].timestamp_cpu_values.resize(max_timestamp_query_elements);
56 frames[i].timestamp_count = 0;
57
58 frames[i].timestamp_result_names.resize(max_timestamp_query_elements);
59 frames[i].timestamp_cpu_result_values.resize(max_timestamp_query_elements);
60 frames[i].timestamp_result_values.resize(max_timestamp_query_elements);
61 frames[i].timestamp_result_count = 0;
62 }
63 }
64
65 Utilities::~Utilities() {
66 singleton = nullptr;
67 for (int i = 0; i < FRAME_COUNT; i++) {
68 glDeleteQueries(max_timestamp_query_elements, frames[i].queries);
69 }
70
71 if (texture_mem_cache) {
72 uint32_t leaked_data_size = 0;
73 for (const KeyValue<GLuint, ResourceAllocation> &E : texture_allocs_cache) {
74 #ifdef DEV_ENABLED
75 ERR_PRINT(E.value.name + ": leaked " + itos(E.value.size) + " bytes.");
76 #else
77 ERR_PRINT("Texture with GL ID of " + itos(E.key) + ": leaked " + itos(E.value.size) + " bytes.");
78 #endif
79 leaked_data_size += E.value.size;
80 }
81 if (leaked_data_size < texture_mem_cache) {
82 ERR_PRINT("Texture cache is not empty. There may be an additional texture leak of " + itos(texture_mem_cache - leaked_data_size) + " bytes.");
83 }
84 }
85
86 if (render_buffer_mem_cache) {
87 uint32_t leaked_data_size = 0;
88 for (const KeyValue<GLuint, ResourceAllocation> &E : render_buffer_allocs_cache) {
89 #ifdef DEV_ENABLED
90 ERR_PRINT(E.value.name + ": leaked " + itos(E.value.size) + " bytes.");
91 #else
92 ERR_PRINT("Render buffer with GL ID of " + itos(E.key) + ": leaked " + itos(E.value.size) + " bytes.");
93 #endif
94 leaked_data_size += E.value.size;
95 }
96 if (leaked_data_size < render_buffer_mem_cache) {
97 ERR_PRINT("Render buffer cache is not empty. There may be an additional render buffer leak of " + itos(render_buffer_mem_cache - leaked_data_size) + " bytes.");
98 }
99 }
100
101 if (buffer_mem_cache) {
102 uint32_t leaked_data_size = 0;
103
104 for (const KeyValue<GLuint, ResourceAllocation> &E : buffer_allocs_cache) {
105 #ifdef DEV_ENABLED
106 ERR_PRINT(E.value.name + ": leaked " + itos(E.value.size) + " bytes.");
107 #else
108 ERR_PRINT("Buffer with GL ID of " + itos(E.key) + ": leaked " + itos(E.value.size) + " bytes.");
109 #endif
110 leaked_data_size += E.value.size;
111 }
112 if (leaked_data_size < buffer_mem_cache) {
113 ERR_PRINT("Buffer cache is not empty. There may be an additional buffer leak of " + itos(buffer_mem_cache - leaked_data_size) + " bytes.");
114 }
115 }
116 }
117
118 Vector<uint8_t> Utilities::buffer_get_data(GLenum p_target, GLuint p_buffer, uint32_t p_buffer_size) {
119 Vector<uint8_t> ret;
120
121 if (p_buffer_size == 0) {
122 return ret;
123 }
124
125 ret.resize(p_buffer_size);
126 glBindBuffer(p_target, p_buffer);
127
128 #if defined(__EMSCRIPTEN__)
129 {
130 uint8_t *w = ret.ptrw();
131 godot_webgl2_glGetBufferSubData(p_target, 0, p_buffer_size, w);
132 }
133 #else
134 void *data = glMapBufferRange(p_target, 0, p_buffer_size, GL_MAP_READ_BIT);
135 ERR_FAIL_NULL_V(data, Vector<uint8_t>());
136 {
137 uint8_t *w = ret.ptrw();
138 memcpy(w, data, p_buffer_size);
139 }
140 glUnmapBuffer(p_target);
141 #endif
142 glBindBuffer(p_target, 0);
143 return ret;
144 }
145
146 /* INSTANCES */
147
148 RS::InstanceType Utilities::get_base_type(RID p_rid) const {
149 if (GLES3::MeshStorage::get_singleton()->owns_mesh(p_rid)) {
150 return RS::INSTANCE_MESH;
151 } else if (GLES3::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {
152 return RS::INSTANCE_MULTIMESH;
153 } else if (GLES3::LightStorage::get_singleton()->owns_light(p_rid)) {
154 return RS::INSTANCE_LIGHT;
155 } else if (GLES3::LightStorage::get_singleton()->owns_lightmap(p_rid)) {
156 return RS::INSTANCE_LIGHTMAP;
157 } else if (GLES3::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {
158 return RS::INSTANCE_PARTICLES;
159 } else if (GLES3::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {
160 return RS::INSTANCE_REFLECTION_PROBE;
161 } else if (GLES3::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {
162 return RS::INSTANCE_PARTICLES_COLLISION;
163 } else if (owns_visibility_notifier(p_rid)) {
164 return RS::INSTANCE_VISIBLITY_NOTIFIER;
165 }
166 return RS::INSTANCE_NONE;
167 }
168
169 bool Utilities::free(RID p_rid) {
170 if (GLES3::TextureStorage::get_singleton()->owns_render_target(p_rid)) {
171 GLES3::TextureStorage::get_singleton()->render_target_free(p_rid);
172 return true;
173 } else if (GLES3::TextureStorage::get_singleton()->owns_texture(p_rid)) {
174 GLES3::TextureStorage::get_singleton()->texture_free(p_rid);
175 return true;
176 } else if (GLES3::TextureStorage::get_singleton()->owns_canvas_texture(p_rid)) {
177 GLES3::TextureStorage::get_singleton()->canvas_texture_free(p_rid);
178 return true;
179 } else if (GLES3::MaterialStorage::get_singleton()->owns_shader(p_rid)) {
180 GLES3::MaterialStorage::get_singleton()->shader_free(p_rid);
181 return true;
182 } else if (GLES3::MaterialStorage::get_singleton()->owns_material(p_rid)) {
183 GLES3::MaterialStorage::get_singleton()->material_free(p_rid);
184 return true;
185 } else if (GLES3::MeshStorage::get_singleton()->owns_mesh(p_rid)) {
186 GLES3::MeshStorage::get_singleton()->mesh_free(p_rid);
187 return true;
188 } else if (GLES3::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {
189 GLES3::MeshStorage::get_singleton()->multimesh_free(p_rid);
190 return true;
191 } else if (GLES3::MeshStorage::get_singleton()->owns_mesh_instance(p_rid)) {
192 GLES3::MeshStorage::get_singleton()->mesh_instance_free(p_rid);
193 return true;
194 } else if (GLES3::LightStorage::get_singleton()->owns_light(p_rid)) {
195 GLES3::LightStorage::get_singleton()->light_free(p_rid);
196 return true;
197 } else if (GLES3::LightStorage::get_singleton()->owns_lightmap(p_rid)) {
198 GLES3::LightStorage::get_singleton()->lightmap_free(p_rid);
199 return true;
200 } else if (GLES3::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {
201 GLES3::LightStorage::get_singleton()->reflection_probe_free(p_rid);
202 return true;
203 } else if (GLES3::LightStorage::get_singleton()->owns_reflection_atlas(p_rid)) {
204 GLES3::LightStorage::get_singleton()->reflection_atlas_free(p_rid);
205 return true;
206 } else if (GLES3::LightStorage::get_singleton()->owns_reflection_probe_instance(p_rid)) {
207 GLES3::LightStorage::get_singleton()->reflection_probe_instance_free(p_rid);
208 return true;
209 } else if (GLES3::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {
210 GLES3::ParticlesStorage::get_singleton()->particles_free(p_rid);
211 return true;
212 } else if (GLES3::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {
213 GLES3::ParticlesStorage::get_singleton()->particles_collision_free(p_rid);
214 return true;
215 } else if (GLES3::ParticlesStorage::get_singleton()->owns_particles_collision_instance(p_rid)) {
216 GLES3::ParticlesStorage::get_singleton()->particles_collision_instance_free(p_rid);
217 return true;
218 } else if (GLES3::MeshStorage::get_singleton()->owns_skeleton(p_rid)) {
219 GLES3::MeshStorage::get_singleton()->skeleton_free(p_rid);
220 return true;
221 } else if (owns_visibility_notifier(p_rid)) {
222 visibility_notifier_free(p_rid);
223 return true;
224 } else {
225 return false;
226 }
227 }
228
229 /* DEPENDENCIES */
230
231 void Utilities::base_update_dependency(RID p_base, DependencyTracker *p_instance) {
232 if (MeshStorage::get_singleton()->owns_mesh(p_base)) {
233 Mesh *mesh = MeshStorage::get_singleton()->get_mesh(p_base);
234 p_instance->update_dependency(&mesh->dependency);
235 } else if (MeshStorage::get_singleton()->owns_multimesh(p_base)) {
236 MultiMesh *multimesh = MeshStorage::get_singleton()->get_multimesh(p_base);
237 p_instance->update_dependency(&multimesh->dependency);
238 if (multimesh->mesh.is_valid()) {
239 base_update_dependency(multimesh->mesh, p_instance);
240 }
241 } else if (LightStorage::get_singleton()->owns_reflection_probe(p_base)) {
242 Dependency *dependency = LightStorage::get_singleton()->reflection_probe_get_dependency(p_base);
243 p_instance->update_dependency(dependency);
244 } else if (LightStorage::get_singleton()->owns_light(p_base)) {
245 Light *l = LightStorage::get_singleton()->get_light(p_base);
246 p_instance->update_dependency(&l->dependency);
247 } else if (ParticlesStorage::get_singleton()->owns_particles(p_base)) {
248 Dependency *dependency = ParticlesStorage::get_singleton()->particles_get_dependency(p_base);
249 p_instance->update_dependency(dependency);
250 } else if (ParticlesStorage::get_singleton()->owns_particles_collision(p_base)) {
251 Dependency *dependency = ParticlesStorage::get_singleton()->particles_collision_get_dependency(p_base);
252 p_instance->update_dependency(dependency);
253 } else if (owns_visibility_notifier(p_base)) {
254 VisibilityNotifier *vn = get_visibility_notifier(p_base);
255 p_instance->update_dependency(&vn->dependency);
256 }
257 }
258
259 /* VISIBILITY NOTIFIER */
260
261 RID Utilities::visibility_notifier_allocate() {
262 return visibility_notifier_owner.allocate_rid();
263 }
264
265 void Utilities::visibility_notifier_initialize(RID p_notifier) {
266 visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier());
267 }
268
269 void Utilities::visibility_notifier_free(RID p_notifier) {
270 VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
271 vn->dependency.deleted_notify(p_notifier);
272 visibility_notifier_owner.free(p_notifier);
273 }
274
275 void Utilities::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) {
276 VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
277 ERR_FAIL_NULL(vn);
278 vn->aabb = p_aabb;
279 vn->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
280 }
281
282 void Utilities::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) {
283 VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
284 ERR_FAIL_NULL(vn);
285 vn->enter_callback = p_enter_callbable;
286 vn->exit_callback = p_exit_callable;
287 }
288
289 AABB Utilities::visibility_notifier_get_aabb(RID p_notifier) const {
290 const VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
291 ERR_FAIL_NULL_V(vn, AABB());
292 return vn->aabb;
293 }
294
295 void Utilities::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) {
296 VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
297 ERR_FAIL_NULL(vn);
298
299 if (p_enter) {
300 if (vn->enter_callback.is_valid()) {
301 if (p_deferred) {
302 vn->enter_callback.call_deferred();
303 } else {
304 vn->enter_callback.call();
305 }
306 }
307 } else {
308 if (vn->exit_callback.is_valid()) {
309 if (p_deferred) {
310 vn->exit_callback.call_deferred();
311 } else {
312 vn->exit_callback.call();
313 }
314 }
315 }
316 }
317
318 /* TIMING */
319
320 void Utilities::capture_timestamps_begin() {
321 capture_timestamp("Frame Begin");
322 }
323
324 void Utilities::capture_timestamp(const String &p_name) {
325 ERR_FAIL_COND(frames[frame].timestamp_count >= max_timestamp_query_elements);
326
327 #ifdef GL_API_ENABLED
328 if (RasterizerGLES3::is_gles_over_gl()) {
329 glQueryCounter(frames[frame].queries[frames[frame].timestamp_count], GL_TIMESTAMP);
330 }
331 #endif // GL_API_ENABLED
332
333 frames[frame].timestamp_names[frames[frame].timestamp_count] = p_name;
334 frames[frame].timestamp_cpu_values[frames[frame].timestamp_count] = OS::get_singleton()->get_ticks_usec();
335 frames[frame].timestamp_count++;
336 }
337
338 void Utilities::_capture_timestamps_begin() {
339 // frame is incremented at the end of the frame so this gives us the queries for frame - 2. By then they should be ready.
340 if (frames[frame].timestamp_count) {
341 #ifdef GL_API_ENABLED
342 if (RasterizerGLES3::is_gles_over_gl()) {
343 for (uint32_t i = 0; i < frames[frame].timestamp_count; i++) {
344 uint64_t temp = 0;
345 glGetQueryObjectui64v(frames[frame].queries[i], GL_QUERY_RESULT, &temp);
346 frames[frame].timestamp_result_values[i] = temp;
347 }
348 }
349 #endif // GL_API_ENABLED
350 SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names);
351 SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values);
352 }
353
354 frames[frame].timestamp_result_count = frames[frame].timestamp_count;
355 frames[frame].timestamp_count = 0;
356 frames[frame].index = Engine::get_singleton()->get_frames_drawn();
357 capture_timestamp("Internal Begin");
358 }
359
360 void Utilities::capture_timestamps_end() {
361 capture_timestamp("Internal End");
362 frame = (frame + 1) % FRAME_COUNT;
363 }
364
365 uint32_t Utilities::get_captured_timestamps_count() const {
366 return frames[frame].timestamp_result_count;
367 }
368
369 uint64_t Utilities::get_captured_timestamps_frame() const {
370 return frames[frame].index;
371 }
372
373 uint64_t Utilities::get_captured_timestamp_gpu_time(uint32_t p_index) const {
374 ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);
375 return frames[frame].timestamp_result_values[p_index];
376 }
377
378 uint64_t Utilities::get_captured_timestamp_cpu_time(uint32_t p_index) const {
379 ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0);
380 return frames[frame].timestamp_cpu_result_values[p_index];
381 }
382
383 String Utilities::get_captured_timestamp_name(uint32_t p_index) const {
384 ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, String());
385 return frames[frame].timestamp_result_names[p_index];
386 }
387
388 /* MISC */
389
390 void Utilities::update_dirty_resources() {
391 MaterialStorage::get_singleton()->_update_global_shader_uniforms();
392 MaterialStorage::get_singleton()->_update_queued_materials();
393 MeshStorage::get_singleton()->_update_dirty_skeletons();
394 MeshStorage::get_singleton()->_update_dirty_multimeshes();
395 TextureStorage::get_singleton()->update_texture_atlas();
396 }
397
398 void Utilities::set_debug_generate_wireframes(bool p_generate) {
399 Config *config = Config::get_singleton();
400 config->generate_wireframes = p_generate;
401 }
402
403 bool Utilities::has_os_feature(const String &p_feature) const {
404 Config *config = Config::get_singleton();
405 if (!config) {
406 return false;
407 }
408
409 if (p_feature == "rgtc") {
410 return config->rgtc_supported;
411 }
412 if (p_feature == "s3tc") {
413 return config->s3tc_supported;
414 }
415 if (p_feature == "bptc") {
416 return config->bptc_supported;
417 }
418 if (p_feature == "astc") {
419 return config->astc_supported;
420 }
421 if (p_feature == "etc2") {
422 return config->etc2_supported;
423 }
424 if (p_feature == "astc_hdr") {
425 return config->astc_hdr_supported;
426 }
427
428 return false;
429 }
430
431 void Utilities::update_memory_info() {
432 }
433
434 uint64_t Utilities::get_rendering_info(RS::RenderingInfo p_info) {
435 if (p_info == RS::RENDERING_INFO_TEXTURE_MEM_USED) {
436 return texture_mem_cache + render_buffer_mem_cache; // Add render buffer memory to our texture mem.
437 } else if (p_info == RS::RENDERING_INFO_BUFFER_MEM_USED) {
438 return buffer_mem_cache;
439 } else if (p_info == RS::RENDERING_INFO_VIDEO_MEM_USED) {
440 return texture_mem_cache + buffer_mem_cache + render_buffer_mem_cache;
441 }
442 return 0;
443 }
444
445 String Utilities::get_video_adapter_name() const {
446 const String rendering_device_name = String::utf8((const char *)glGetString(GL_RENDERER));
447 // NVIDIA suffixes all GPU model names with "/PCIe/SSE2" in OpenGL (but not Vulkan). This isn't necessary to display nowadays, so it can be trimmed.
448 return rendering_device_name.trim_suffix("/PCIe/SSE2");
449 }
450
451 String Utilities::get_video_adapter_vendor() const {
452 const String rendering_device_vendor = String::utf8((const char *)glGetString(GL_VENDOR));
453 // NVIDIA suffixes its vendor name with " Corporation". This is neither necessary to process nor display.
454 return rendering_device_vendor.trim_suffix(" Corporation");
455 }
456
457 RenderingDevice::DeviceType Utilities::get_video_adapter_type() const {
458 return RenderingDevice::DeviceType::DEVICE_TYPE_OTHER;
459 }
460
461 String Utilities::get_video_adapter_api_version() const {
462 return String::utf8((const char *)glGetString(GL_VERSION));
463 }
464
465 Size2i Utilities::get_maximum_viewport_size() const {
466 Config *config = Config::get_singleton();
467 ERR_FAIL_NULL_V(config, Size2i());
468 return Size2i(config->max_viewport_size[0], config->max_viewport_size[1]);
469 }
470
471 uint32_t Utilities::get_maximum_shader_varyings() const {
472 Config *config = Config::get_singleton();
473 ERR_FAIL_NULL_V(config, 31);
474 return config->max_shader_varyings;
475 }
476
477 uint64_t Utilities::get_maximum_uniform_buffer_size() const {
478 Config *config = Config::get_singleton();
479 ERR_FAIL_NULL_V(config, 65536);
480 return uint64_t(config->max_uniform_buffer_size);
481 }
482
483 #endif // GLES3_ENABLED