utilities.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* utilities.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "utilities.h"
  31. #include "../environment/fog.h"
  32. #include "../environment/gi.h"
  33. #include "light_storage.h"
  34. #include "mesh_storage.h"
  35. #include "particles_storage.h"
  36. #include "texture_storage.h"
  37. using namespace RendererRD;
  38. Utilities *Utilities::singleton = nullptr;
  39. Utilities::Utilities() {
  40. singleton = this;
  41. }
  42. Utilities::~Utilities() {
  43. singleton = nullptr;
  44. }
  45. /* INSTANCES */
  46. RS::InstanceType Utilities::get_base_type(RID p_rid) const {
  47. if (RendererRD::MeshStorage::get_singleton()->owns_mesh(p_rid)) {
  48. return RS::INSTANCE_MESH;
  49. }
  50. if (RendererRD::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {
  51. return RS::INSTANCE_MULTIMESH;
  52. }
  53. if (RendererRD::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {
  54. return RS::INSTANCE_REFLECTION_PROBE;
  55. }
  56. if (RendererRD::TextureStorage::get_singleton()->owns_decal(p_rid)) {
  57. return RS::INSTANCE_DECAL;
  58. }
  59. if (RendererRD::GI::get_singleton()->owns_voxel_gi(p_rid)) {
  60. return RS::INSTANCE_VOXEL_GI;
  61. }
  62. if (RendererRD::LightStorage::get_singleton()->owns_light(p_rid)) {
  63. return RS::INSTANCE_LIGHT;
  64. }
  65. if (RendererRD::LightStorage::get_singleton()->owns_lightmap(p_rid)) {
  66. return RS::INSTANCE_LIGHTMAP;
  67. }
  68. if (RendererRD::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {
  69. return RS::INSTANCE_PARTICLES;
  70. }
  71. if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {
  72. return RS::INSTANCE_PARTICLES_COLLISION;
  73. }
  74. if (RendererRD::Fog::get_singleton()->owns_fog_volume(p_rid)) {
  75. return RS::INSTANCE_FOG_VOLUME;
  76. }
  77. if (owns_visibility_notifier(p_rid)) {
  78. return RS::INSTANCE_VISIBLITY_NOTIFIER;
  79. }
  80. return RS::INSTANCE_NONE;
  81. }
  82. bool Utilities::free(RID p_rid) {
  83. if (RendererRD::LightStorage::get_singleton()->free(p_rid)) {
  84. return true;
  85. } else if (RendererRD::MaterialStorage::get_singleton()->free(p_rid)) {
  86. return true;
  87. } else if (RendererRD::MeshStorage::get_singleton()->free(p_rid)) {
  88. return true;
  89. } else if (RendererRD::ParticlesStorage::get_singleton()->free(p_rid)) {
  90. return true;
  91. } else if (RendererRD::TextureStorage::get_singleton()->free(p_rid)) {
  92. return true;
  93. } else if (RendererRD::GI::get_singleton()->owns_voxel_gi(p_rid)) {
  94. RendererRD::GI::get_singleton()->voxel_gi_free(p_rid);
  95. return true;
  96. } else if (RendererRD::Fog::get_singleton()->owns_fog_volume(p_rid)) {
  97. RendererRD::Fog::get_singleton()->fog_volume_free(p_rid);
  98. return true;
  99. } else if (owns_visibility_notifier(p_rid)) {
  100. visibility_notifier_free(p_rid);
  101. return true;
  102. }
  103. return false;
  104. }
  105. /* DEPENDENCIES */
  106. void Utilities::base_update_dependency(RID p_base, DependencyTracker *p_instance) {
  107. if (MeshStorage::get_singleton()->owns_mesh(p_base)) {
  108. Dependency *dependency = MeshStorage::get_singleton()->mesh_get_dependency(p_base);
  109. p_instance->update_dependency(dependency);
  110. } else if (MeshStorage::get_singleton()->owns_multimesh(p_base)) {
  111. Dependency *dependency = MeshStorage::get_singleton()->multimesh_get_dependency(p_base);
  112. p_instance->update_dependency(dependency);
  113. RID mesh = MeshStorage::get_singleton()->multimesh_get_mesh(p_base);
  114. if (mesh.is_valid()) {
  115. base_update_dependency(mesh, p_instance);
  116. }
  117. } else if (LightStorage::get_singleton()->owns_reflection_probe(p_base)) {
  118. Dependency *dependency = LightStorage::get_singleton()->reflection_probe_get_dependency(p_base);
  119. p_instance->update_dependency(dependency);
  120. } else if (TextureStorage::get_singleton()->owns_decal(p_base)) {
  121. Dependency *dependency = TextureStorage::get_singleton()->decal_get_dependency(p_base);
  122. p_instance->update_dependency(dependency);
  123. } else if (GI::get_singleton()->owns_voxel_gi(p_base)) {
  124. Dependency *dependency = GI::get_singleton()->voxel_gi_get_dependency(p_base);
  125. p_instance->update_dependency(dependency);
  126. } else if (LightStorage::get_singleton()->owns_lightmap(p_base)) {
  127. Dependency *dependency = LightStorage::get_singleton()->lightmap_get_dependency(p_base);
  128. p_instance->update_dependency(dependency);
  129. } else if (LightStorage::get_singleton()->owns_light(p_base)) {
  130. Dependency *dependency = LightStorage::get_singleton()->light_get_dependency(p_base);
  131. p_instance->update_dependency(dependency);
  132. } else if (ParticlesStorage::get_singleton()->owns_particles(p_base)) {
  133. Dependency *dependency = ParticlesStorage::get_singleton()->particles_get_dependency(p_base);
  134. p_instance->update_dependency(dependency);
  135. } else if (ParticlesStorage::get_singleton()->owns_particles_collision(p_base)) {
  136. Dependency *dependency = ParticlesStorage::get_singleton()->particles_collision_get_dependency(p_base);
  137. p_instance->update_dependency(dependency);
  138. } else if (Fog::get_singleton()->owns_fog_volume(p_base)) {
  139. Dependency *dependency = Fog::get_singleton()->fog_volume_get_dependency(p_base);
  140. p_instance->update_dependency(dependency);
  141. } else if (owns_visibility_notifier(p_base)) {
  142. VisibilityNotifier *vn = get_visibility_notifier(p_base);
  143. p_instance->update_dependency(&vn->dependency);
  144. }
  145. }
  146. /* VISIBILITY NOTIFIER */
  147. RID Utilities::visibility_notifier_allocate() {
  148. return visibility_notifier_owner.allocate_rid();
  149. }
  150. void Utilities::visibility_notifier_initialize(RID p_notifier) {
  151. visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier());
  152. }
  153. void Utilities::visibility_notifier_free(RID p_notifier) {
  154. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  155. vn->dependency.deleted_notify(p_notifier);
  156. visibility_notifier_owner.free(p_notifier);
  157. }
  158. void Utilities::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) {
  159. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  160. ERR_FAIL_COND(!vn);
  161. vn->aabb = p_aabb;
  162. vn->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  163. }
  164. void Utilities::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) {
  165. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  166. ERR_FAIL_COND(!vn);
  167. vn->enter_callback = p_enter_callbable;
  168. vn->exit_callback = p_exit_callable;
  169. }
  170. AABB Utilities::visibility_notifier_get_aabb(RID p_notifier) const {
  171. const VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  172. ERR_FAIL_COND_V(!vn, AABB());
  173. return vn->aabb;
  174. }
  175. void Utilities::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) {
  176. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  177. ERR_FAIL_COND(!vn);
  178. if (p_enter) {
  179. if (!vn->enter_callback.is_null()) {
  180. if (p_deferred) {
  181. vn->enter_callback.call_deferredp(nullptr, 0);
  182. } else {
  183. Variant r;
  184. Callable::CallError ce;
  185. vn->enter_callback.callp(nullptr, 0, r, ce);
  186. }
  187. }
  188. } else {
  189. if (!vn->exit_callback.is_null()) {
  190. if (p_deferred) {
  191. vn->exit_callback.call_deferredp(nullptr, 0);
  192. } else {
  193. Variant r;
  194. Callable::CallError ce;
  195. vn->exit_callback.callp(nullptr, 0, r, ce);
  196. }
  197. }
  198. }
  199. }
  200. /* TIMING */
  201. void Utilities::capture_timestamps_begin() {
  202. RD::get_singleton()->capture_timestamp("Frame Begin");
  203. }
  204. void Utilities::capture_timestamp(const String &p_name) {
  205. RD::get_singleton()->capture_timestamp(p_name);
  206. }
  207. uint32_t Utilities::get_captured_timestamps_count() const {
  208. return RD::get_singleton()->get_captured_timestamps_count();
  209. }
  210. uint64_t Utilities::get_captured_timestamps_frame() const {
  211. return RD::get_singleton()->get_captured_timestamps_frame();
  212. }
  213. uint64_t Utilities::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  214. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  215. }
  216. uint64_t Utilities::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  217. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  218. }
  219. String Utilities::get_captured_timestamp_name(uint32_t p_index) const {
  220. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  221. }
  222. /* MISC */
  223. void Utilities::update_dirty_resources() {
  224. MaterialStorage::get_singleton()->_update_global_shader_uniforms(); //must do before materials, so it can queue them for update
  225. MaterialStorage::get_singleton()->_update_queued_materials();
  226. MeshStorage::get_singleton()->_update_dirty_multimeshes();
  227. MeshStorage::get_singleton()->_update_dirty_skeletons();
  228. TextureStorage::get_singleton()->update_decal_atlas();
  229. }
  230. bool Utilities::has_os_feature(const String &p_feature) const {
  231. if (!RD::get_singleton()) {
  232. return false;
  233. }
  234. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  235. return true;
  236. }
  237. #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
  238. // Some Android devices report support for S3TC but we don't expect that and don't export the textures.
  239. // This could be fixed but so few devices support it that it doesn't seem useful (and makes bigger APKs).
  240. // For good measure we do the same hack for iOS, just in case.
  241. if (p_feature == "s3tc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  242. return true;
  243. }
  244. #endif
  245. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  246. return true;
  247. }
  248. if ((p_feature == "etc" || p_feature == "etc2") && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  249. return true;
  250. }
  251. return false;
  252. }
  253. void Utilities::update_memory_info() {
  254. texture_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TEXTURES);
  255. buffer_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_BUFFERS);
  256. total_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TOTAL);
  257. }
  258. uint64_t Utilities::get_rendering_info(RS::RenderingInfo p_info) {
  259. if (p_info == RS::RENDERING_INFO_TEXTURE_MEM_USED) {
  260. return texture_mem_cache;
  261. } else if (p_info == RS::RENDERING_INFO_BUFFER_MEM_USED) {
  262. return buffer_mem_cache;
  263. } else if (p_info == RS::RENDERING_INFO_VIDEO_MEM_USED) {
  264. return total_mem_cache;
  265. }
  266. return 0;
  267. }
  268. String Utilities::get_video_adapter_name() const {
  269. return RenderingDevice::get_singleton()->get_device_name();
  270. }
  271. String Utilities::get_video_adapter_vendor() const {
  272. return RenderingDevice::get_singleton()->get_device_vendor_name();
  273. }
  274. RenderingDevice::DeviceType Utilities::get_video_adapter_type() const {
  275. return RenderingDevice::get_singleton()->get_device_type();
  276. }
  277. String Utilities::get_video_adapter_api_version() const {
  278. return RenderingDevice::get_singleton()->get_device_api_version();
  279. }