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. } else {
  103. return false;
  104. }
  105. }
  106. /* DEPENDENCIES */
  107. void Utilities::base_update_dependency(RID p_base, DependencyTracker *p_instance) {
  108. if (MeshStorage::get_singleton()->owns_mesh(p_base)) {
  109. Dependency *dependency = MeshStorage::get_singleton()->mesh_get_dependency(p_base);
  110. p_instance->update_dependency(dependency);
  111. } else if (MeshStorage::get_singleton()->owns_multimesh(p_base)) {
  112. Dependency *dependency = MeshStorage::get_singleton()->multimesh_get_dependency(p_base);
  113. p_instance->update_dependency(dependency);
  114. RID mesh = MeshStorage::get_singleton()->multimesh_get_mesh(p_base);
  115. if (mesh.is_valid()) {
  116. base_update_dependency(mesh, p_instance);
  117. }
  118. } else if (LightStorage::get_singleton()->owns_reflection_probe(p_base)) {
  119. Dependency *dependency = LightStorage::get_singleton()->reflection_probe_get_dependency(p_base);
  120. p_instance->update_dependency(dependency);
  121. } else if (TextureStorage::get_singleton()->owns_decal(p_base)) {
  122. Dependency *dependency = TextureStorage::get_singleton()->decal_get_dependency(p_base);
  123. p_instance->update_dependency(dependency);
  124. } else if (GI::get_singleton()->owns_voxel_gi(p_base)) {
  125. Dependency *dependency = GI::get_singleton()->voxel_gi_get_dependency(p_base);
  126. p_instance->update_dependency(dependency);
  127. } else if (LightStorage::get_singleton()->owns_lightmap(p_base)) {
  128. Dependency *dependency = LightStorage::get_singleton()->lightmap_get_dependency(p_base);
  129. p_instance->update_dependency(dependency);
  130. } else if (LightStorage::get_singleton()->owns_light(p_base)) {
  131. Dependency *dependency = LightStorage::get_singleton()->light_get_dependency(p_base);
  132. p_instance->update_dependency(dependency);
  133. } else if (ParticlesStorage::get_singleton()->owns_particles(p_base)) {
  134. Dependency *dependency = ParticlesStorage::get_singleton()->particles_get_dependency(p_base);
  135. p_instance->update_dependency(dependency);
  136. } else if (ParticlesStorage::get_singleton()->owns_particles_collision(p_base)) {
  137. Dependency *dependency = ParticlesStorage::get_singleton()->particles_collision_get_dependency(p_base);
  138. p_instance->update_dependency(dependency);
  139. } else if (Fog::get_singleton()->owns_fog_volume(p_base)) {
  140. Dependency *dependency = Fog::get_singleton()->fog_volume_get_dependency(p_base);
  141. p_instance->update_dependency(dependency);
  142. } else if (owns_visibility_notifier(p_base)) {
  143. VisibilityNotifier *vn = get_visibility_notifier(p_base);
  144. p_instance->update_dependency(&vn->dependency);
  145. }
  146. }
  147. /* VISIBILITY NOTIFIER */
  148. RID Utilities::visibility_notifier_allocate() {
  149. return visibility_notifier_owner.allocate_rid();
  150. }
  151. void Utilities::visibility_notifier_initialize(RID p_notifier) {
  152. visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier());
  153. }
  154. void Utilities::visibility_notifier_free(RID p_notifier) {
  155. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  156. vn->dependency.deleted_notify(p_notifier);
  157. visibility_notifier_owner.free(p_notifier);
  158. }
  159. void Utilities::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) {
  160. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  161. ERR_FAIL_COND(!vn);
  162. vn->aabb = p_aabb;
  163. vn->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  164. }
  165. void Utilities::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) {
  166. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  167. ERR_FAIL_COND(!vn);
  168. vn->enter_callback = p_enter_callbable;
  169. vn->exit_callback = p_exit_callable;
  170. }
  171. AABB Utilities::visibility_notifier_get_aabb(RID p_notifier) const {
  172. const VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  173. ERR_FAIL_COND_V(!vn, AABB());
  174. return vn->aabb;
  175. }
  176. void Utilities::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) {
  177. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  178. ERR_FAIL_COND(!vn);
  179. if (p_enter) {
  180. if (!vn->enter_callback.is_null()) {
  181. if (p_deferred) {
  182. vn->enter_callback.call_deferredp(nullptr, 0);
  183. } else {
  184. Variant r;
  185. Callable::CallError ce;
  186. vn->enter_callback.callp(nullptr, 0, r, ce);
  187. }
  188. }
  189. } else {
  190. if (!vn->exit_callback.is_null()) {
  191. if (p_deferred) {
  192. vn->exit_callback.call_deferredp(nullptr, 0);
  193. } else {
  194. Variant r;
  195. Callable::CallError ce;
  196. vn->exit_callback.callp(nullptr, 0, r, ce);
  197. }
  198. }
  199. }
  200. }
  201. /* TIMING */
  202. void Utilities::capture_timestamps_begin() {
  203. RD::get_singleton()->capture_timestamp("Frame Begin");
  204. }
  205. void Utilities::capture_timestamp(const String &p_name) {
  206. RD::get_singleton()->capture_timestamp(p_name);
  207. }
  208. uint32_t Utilities::get_captured_timestamps_count() const {
  209. return RD::get_singleton()->get_captured_timestamps_count();
  210. }
  211. uint64_t Utilities::get_captured_timestamps_frame() const {
  212. return RD::get_singleton()->get_captured_timestamps_frame();
  213. }
  214. uint64_t Utilities::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  215. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  216. }
  217. uint64_t Utilities::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  218. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  219. }
  220. String Utilities::get_captured_timestamp_name(uint32_t p_index) const {
  221. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  222. }
  223. /* MISC */
  224. void Utilities::update_dirty_resources() {
  225. MaterialStorage::get_singleton()->_update_global_shader_uniforms(); //must do before materials, so it can queue them for update
  226. MaterialStorage::get_singleton()->_update_queued_materials();
  227. MeshStorage::get_singleton()->_update_dirty_multimeshes();
  228. MeshStorage::get_singleton()->_update_dirty_skeletons();
  229. TextureStorage::get_singleton()->update_decal_atlas();
  230. }
  231. bool Utilities::has_os_feature(const String &p_feature) const {
  232. if (!RD::get_singleton()) {
  233. return false;
  234. }
  235. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  236. return true;
  237. }
  238. #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
  239. // Some Android devices report support for S3TC but we don't expect that and don't export the textures.
  240. // This could be fixed but so few devices support it that it doesn't seem useful (and makes bigger APKs).
  241. // For good measure we do the same hack for iOS, just in case.
  242. 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)) {
  243. return true;
  244. }
  245. #endif
  246. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  247. return true;
  248. }
  249. 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)) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. void Utilities::update_memory_info() {
  255. texture_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TEXTURES);
  256. buffer_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_BUFFERS);
  257. total_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TOTAL);
  258. }
  259. uint64_t Utilities::get_rendering_info(RS::RenderingInfo p_info) {
  260. if (p_info == RS::RENDERING_INFO_TEXTURE_MEM_USED) {
  261. return texture_mem_cache;
  262. } else if (p_info == RS::RENDERING_INFO_BUFFER_MEM_USED) {
  263. return buffer_mem_cache;
  264. } else if (p_info == RS::RENDERING_INFO_VIDEO_MEM_USED) {
  265. return total_mem_cache;
  266. }
  267. return 0;
  268. }
  269. String Utilities::get_video_adapter_name() const {
  270. return RenderingDevice::get_singleton()->get_device_name();
  271. }
  272. String Utilities::get_video_adapter_vendor() const {
  273. return RenderingDevice::get_singleton()->get_device_vendor_name();
  274. }
  275. RenderingDevice::DeviceType Utilities::get_video_adapter_type() const {
  276. return RenderingDevice::get_singleton()->get_device_type();
  277. }
  278. String Utilities::get_video_adapter_api_version() const {
  279. return RenderingDevice::get_singleton()->get_device_api_version();
  280. }