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