utilities.cpp 13 KB

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