utilities.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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::TextureStorage::get_singleton()->owns_texture(p_rid)) {
  84. RendererRD::TextureStorage::get_singleton()->texture_free(p_rid);
  85. } else if (RendererRD::TextureStorage::get_singleton()->owns_canvas_texture(p_rid)) {
  86. RendererRD::TextureStorage::get_singleton()->canvas_texture_free(p_rid);
  87. } else if (RendererRD::MaterialStorage::get_singleton()->owns_shader(p_rid)) {
  88. RendererRD::MaterialStorage::get_singleton()->shader_free(p_rid);
  89. } else if (RendererRD::MaterialStorage::get_singleton()->owns_material(p_rid)) {
  90. RendererRD::MaterialStorage::get_singleton()->material_free(p_rid);
  91. } else if (RendererRD::MeshStorage::get_singleton()->owns_mesh(p_rid)) {
  92. RendererRD::MeshStorage::get_singleton()->mesh_free(p_rid);
  93. } else if (RendererRD::MeshStorage::get_singleton()->owns_mesh_instance(p_rid)) {
  94. RendererRD::MeshStorage::get_singleton()->mesh_instance_free(p_rid);
  95. } else if (RendererRD::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {
  96. RendererRD::MeshStorage::get_singleton()->multimesh_free(p_rid);
  97. } else if (RendererRD::MeshStorage::get_singleton()->owns_skeleton(p_rid)) {
  98. RendererRD::MeshStorage::get_singleton()->skeleton_free(p_rid);
  99. } else if (RendererRD::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {
  100. RendererRD::LightStorage::get_singleton()->reflection_probe_free(p_rid);
  101. } else if (RendererRD::TextureStorage::get_singleton()->owns_decal(p_rid)) {
  102. RendererRD::TextureStorage::get_singleton()->decal_free(p_rid);
  103. } else if (RendererRD::GI::get_singleton()->owns_voxel_gi(p_rid)) {
  104. RendererRD::GI::get_singleton()->voxel_gi_free(p_rid);
  105. } else if (RendererRD::LightStorage::get_singleton()->owns_lightmap(p_rid)) {
  106. RendererRD::LightStorage::get_singleton()->lightmap_free(p_rid);
  107. } else if (RendererRD::LightStorage::get_singleton()->owns_light(p_rid)) {
  108. RendererRD::LightStorage::get_singleton()->light_free(p_rid);
  109. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {
  110. RendererRD::ParticlesStorage::get_singleton()->particles_free(p_rid);
  111. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {
  112. RendererRD::ParticlesStorage::get_singleton()->particles_collision_free(p_rid);
  113. } else if (owns_visibility_notifier(p_rid)) {
  114. visibility_notifier_free(p_rid);
  115. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision_instance(p_rid)) {
  116. RendererRD::ParticlesStorage::get_singleton()->particles_collision_instance_free(p_rid);
  117. } else if (RendererRD::Fog::get_singleton()->owns_fog_volume(p_rid)) {
  118. RendererRD::Fog::get_singleton()->fog_free(p_rid);
  119. } else if (RendererRD::TextureStorage::get_singleton()->owns_render_target(p_rid)) {
  120. RendererRD::TextureStorage::get_singleton()->render_target_free(p_rid);
  121. } else {
  122. return false;
  123. }
  124. return true;
  125. }
  126. /* DEPENDENCIES */
  127. void Utilities::base_update_dependency(RID p_base, DependencyTracker *p_instance) {
  128. if (MeshStorage::get_singleton()->owns_mesh(p_base)) {
  129. Mesh *mesh = MeshStorage::get_singleton()->get_mesh(p_base);
  130. p_instance->update_dependency(&mesh->dependency);
  131. } else if (MeshStorage::get_singleton()->owns_multimesh(p_base)) {
  132. MultiMesh *multimesh = MeshStorage::get_singleton()->get_multimesh(p_base);
  133. p_instance->update_dependency(&multimesh->dependency);
  134. if (multimesh->mesh.is_valid()) {
  135. base_update_dependency(multimesh->mesh, p_instance);
  136. }
  137. } else if (LightStorage::get_singleton()->owns_reflection_probe(p_base)) {
  138. ReflectionProbe *rp = LightStorage::get_singleton()->get_reflection_probe(p_base);
  139. p_instance->update_dependency(&rp->dependency);
  140. } else if (TextureStorage::get_singleton()->owns_decal(p_base)) {
  141. Decal *decal = TextureStorage::get_singleton()->get_decal(p_base);
  142. p_instance->update_dependency(&decal->dependency);
  143. } else if (GI::get_singleton()->owns_voxel_gi(p_base)) {
  144. GI::VoxelGI *gip = GI::get_singleton()->get_voxel_gi(p_base);
  145. p_instance->update_dependency(&gip->dependency);
  146. } else if (LightStorage::get_singleton()->owns_lightmap(p_base)) {
  147. Lightmap *lm = LightStorage::get_singleton()->get_lightmap(p_base);
  148. p_instance->update_dependency(&lm->dependency);
  149. } else if (LightStorage::get_singleton()->owns_light(p_base)) {
  150. Light *l = LightStorage::get_singleton()->get_light(p_base);
  151. p_instance->update_dependency(&l->dependency);
  152. } else if (ParticlesStorage::get_singleton()->owns_particles(p_base)) {
  153. Particles *p = ParticlesStorage::get_singleton()->get_particles(p_base);
  154. p_instance->update_dependency(&p->dependency);
  155. } else if (ParticlesStorage::get_singleton()->owns_particles_collision(p_base)) {
  156. ParticlesCollision *pc = ParticlesStorage::get_singleton()->get_particles_collision(p_base);
  157. p_instance->update_dependency(&pc->dependency);
  158. } else if (Fog::get_singleton()->owns_fog_volume(p_base)) {
  159. Fog::FogVolume *fv = Fog::get_singleton()->get_fog_volume(p_base);
  160. p_instance->update_dependency(&fv->dependency);
  161. } else if (owns_visibility_notifier(p_base)) {
  162. VisibilityNotifier *vn = get_visibility_notifier(p_base);
  163. p_instance->update_dependency(&vn->dependency);
  164. }
  165. }
  166. /* VISIBILITY NOTIFIER */
  167. RID Utilities::visibility_notifier_allocate() {
  168. return visibility_notifier_owner.allocate_rid();
  169. }
  170. void Utilities::visibility_notifier_initialize(RID p_notifier) {
  171. visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier());
  172. }
  173. void Utilities::visibility_notifier_free(RID p_notifier) {
  174. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  175. vn->dependency.deleted_notify(p_notifier);
  176. visibility_notifier_owner.free(p_notifier);
  177. }
  178. void Utilities::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) {
  179. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  180. ERR_FAIL_COND(!vn);
  181. vn->aabb = p_aabb;
  182. vn->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  183. }
  184. void Utilities::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) {
  185. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  186. ERR_FAIL_COND(!vn);
  187. vn->enter_callback = p_enter_callbable;
  188. vn->exit_callback = p_exit_callable;
  189. }
  190. AABB Utilities::visibility_notifier_get_aabb(RID p_notifier) const {
  191. const VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  192. ERR_FAIL_COND_V(!vn, AABB());
  193. return vn->aabb;
  194. }
  195. void Utilities::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) {
  196. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  197. ERR_FAIL_COND(!vn);
  198. if (p_enter) {
  199. if (!vn->enter_callback.is_null()) {
  200. if (p_deferred) {
  201. vn->enter_callback.call_deferred(nullptr, 0);
  202. } else {
  203. Variant r;
  204. Callable::CallError ce;
  205. vn->enter_callback.call(nullptr, 0, r, ce);
  206. }
  207. }
  208. } else {
  209. if (!vn->exit_callback.is_null()) {
  210. if (p_deferred) {
  211. vn->exit_callback.call_deferred(nullptr, 0);
  212. } else {
  213. Variant r;
  214. Callable::CallError ce;
  215. vn->exit_callback.call(nullptr, 0, r, ce);
  216. }
  217. }
  218. }
  219. }
  220. /* TIMING */
  221. void Utilities::capture_timestamps_begin() {
  222. RD::get_singleton()->capture_timestamp("Frame Begin");
  223. }
  224. void Utilities::capture_timestamp(const String &p_name) {
  225. RD::get_singleton()->capture_timestamp(p_name);
  226. }
  227. uint32_t Utilities::get_captured_timestamps_count() const {
  228. return RD::get_singleton()->get_captured_timestamps_count();
  229. }
  230. uint64_t Utilities::get_captured_timestamps_frame() const {
  231. return RD::get_singleton()->get_captured_timestamps_frame();
  232. }
  233. uint64_t Utilities::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  234. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  235. }
  236. uint64_t Utilities::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  237. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  238. }
  239. String Utilities::get_captured_timestamp_name(uint32_t p_index) const {
  240. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  241. }
  242. /* MISC */
  243. void Utilities::update_dirty_resources() {
  244. MaterialStorage::get_singleton()->_update_global_variables(); //must do before materials, so it can queue them for update
  245. MaterialStorage::get_singleton()->_update_queued_materials();
  246. MeshStorage::get_singleton()->_update_dirty_multimeshes();
  247. MeshStorage::get_singleton()->_update_dirty_skeletons();
  248. TextureStorage::get_singleton()->update_decal_atlas();
  249. }
  250. bool Utilities::has_os_feature(const String &p_feature) const {
  251. if (!RD::get_singleton()) {
  252. return false;
  253. }
  254. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  255. return true;
  256. }
  257. 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)) {
  258. return true;
  259. }
  260. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  261. return true;
  262. }
  263. 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)) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. void Utilities::update_memory_info() {
  269. texture_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TEXTURES);
  270. buffer_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_BUFFERS);
  271. total_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TOTAL);
  272. }
  273. uint64_t Utilities::get_rendering_info(RS::RenderingInfo p_info) {
  274. if (p_info == RS::RENDERING_INFO_TEXTURE_MEM_USED) {
  275. return texture_mem_cache;
  276. } else if (p_info == RS::RENDERING_INFO_BUFFER_MEM_USED) {
  277. return buffer_mem_cache;
  278. } else if (p_info == RS::RENDERING_INFO_VIDEO_MEM_USED) {
  279. return total_mem_cache;
  280. }
  281. return 0;
  282. }
  283. String Utilities::get_video_adapter_name() const {
  284. return RenderingDevice::get_singleton()->get_device_name();
  285. }
  286. String Utilities::get_video_adapter_vendor() const {
  287. return RenderingDevice::get_singleton()->get_device_vendor_name();
  288. }
  289. RenderingDevice::DeviceType Utilities::get_video_adapter_type() const {
  290. return RenderingDevice::get_singleton()->get_device_type();
  291. }
  292. String Utilities::get_video_adapter_api_version() const {
  293. return RenderingDevice::get_singleton()->get_device_api_version();
  294. }