utilities.cpp 15 KB

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