utilities.cpp 18 KB

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