utilities.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #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_NULL(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_NULL(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_NULL_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_NULL(vn);
  179. if (p_enter) {
  180. if (!vn->enter_callback.is_null()) {
  181. if (p_deferred) {
  182. vn->enter_callback.call_deferred();
  183. } else {
  184. vn->enter_callback.call();
  185. }
  186. }
  187. } else {
  188. if (!vn->exit_callback.is_null()) {
  189. if (p_deferred) {
  190. vn->exit_callback.call_deferred();
  191. } else {
  192. vn->exit_callback.call();
  193. }
  194. }
  195. }
  196. }
  197. /* TIMING */
  198. void Utilities::capture_timestamps_begin() {
  199. RD::get_singleton()->capture_timestamp("Frame Begin");
  200. }
  201. void Utilities::capture_timestamp(const String &p_name) {
  202. RD::get_singleton()->capture_timestamp(p_name);
  203. }
  204. uint32_t Utilities::get_captured_timestamps_count() const {
  205. return RD::get_singleton()->get_captured_timestamps_count();
  206. }
  207. uint64_t Utilities::get_captured_timestamps_frame() const {
  208. return RD::get_singleton()->get_captured_timestamps_frame();
  209. }
  210. uint64_t Utilities::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  211. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  212. }
  213. uint64_t Utilities::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  214. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  215. }
  216. String Utilities::get_captured_timestamp_name(uint32_t p_index) const {
  217. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  218. }
  219. /* MISC */
  220. void Utilities::update_dirty_resources() {
  221. MaterialStorage::get_singleton()->_update_global_shader_uniforms(); //must do before materials, so it can queue them for update
  222. MaterialStorage::get_singleton()->_update_queued_materials();
  223. MeshStorage::get_singleton()->_update_dirty_multimeshes();
  224. MeshStorage::get_singleton()->_update_dirty_skeletons();
  225. TextureStorage::get_singleton()->update_decal_atlas();
  226. }
  227. bool Utilities::has_os_feature(const String &p_feature) const {
  228. if (!RD::get_singleton()) {
  229. return false;
  230. }
  231. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  232. return true;
  233. }
  234. #if !defined(ANDROID_ENABLED) && !defined(IOS_ENABLED)
  235. // Some Android devices report support for S3TC but we don't expect that and don't export the textures.
  236. // This could be fixed but so few devices support it that it doesn't seem useful (and makes bigger APKs).
  237. // For good measure we do the same hack for iOS, just in case.
  238. 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)) {
  239. return true;
  240. }
  241. #endif
  242. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  243. return true;
  244. }
  245. 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)) {
  246. return true;
  247. }
  248. if (p_feature == "astc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ASTC_4x4_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. }
  280. Size2i Utilities::get_maximum_viewport_size() const {
  281. RenderingDevice *device = RenderingDevice::get_singleton();
  282. int max_x = device->limit_get(RenderingDevice::LIMIT_MAX_VIEWPORT_DIMENSIONS_X);
  283. int max_y = device->limit_get(RenderingDevice::LIMIT_MAX_VIEWPORT_DIMENSIONS_Y);
  284. return Size2i(max_x, max_y);
  285. }