| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  utilities.h                                                           */ | 
|---|
| 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 | #ifndef RENDERER_UTILITIES_H | 
|---|
| 32 | #define RENDERER_UTILITIES_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "servers/rendering_server.h" | 
|---|
| 35 |  | 
|---|
| 36 | class DependencyTracker; | 
|---|
| 37 |  | 
|---|
| 38 | class Dependency { | 
|---|
| 39 | public: | 
|---|
| 40 | enum DependencyChangedNotification { | 
|---|
| 41 | DEPENDENCY_CHANGED_AABB, | 
|---|
| 42 | DEPENDENCY_CHANGED_MATERIAL, | 
|---|
| 43 | DEPENDENCY_CHANGED_MESH, | 
|---|
| 44 | DEPENDENCY_CHANGED_MULTIMESH, | 
|---|
| 45 | DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES, | 
|---|
| 46 | DEPENDENCY_CHANGED_PARTICLES, | 
|---|
| 47 | DEPENDENCY_CHANGED_PARTICLES_INSTANCES, | 
|---|
| 48 | DEPENDENCY_CHANGED_DECAL, | 
|---|
| 49 | DEPENDENCY_CHANGED_SKELETON_DATA, | 
|---|
| 50 | DEPENDENCY_CHANGED_SKELETON_BONES, | 
|---|
| 51 | DEPENDENCY_CHANGED_LIGHT, | 
|---|
| 52 | DEPENDENCY_CHANGED_LIGHT_SOFT_SHADOW_AND_PROJECTOR, | 
|---|
| 53 | DEPENDENCY_CHANGED_REFLECTION_PROBE, | 
|---|
| 54 | }; | 
|---|
| 55 |  | 
|---|
| 56 | void changed_notify(DependencyChangedNotification p_notification); | 
|---|
| 57 | void deleted_notify(const RID &p_rid); | 
|---|
| 58 |  | 
|---|
| 59 | ~Dependency(); | 
|---|
| 60 |  | 
|---|
| 61 | private: | 
|---|
| 62 | friend class DependencyTracker; | 
|---|
| 63 | HashMap<DependencyTracker *, uint32_t> instances; | 
|---|
| 64 | }; | 
|---|
| 65 |  | 
|---|
| 66 | class DependencyTracker { | 
|---|
| 67 | public: | 
|---|
| 68 | void *userdata = nullptr; | 
|---|
| 69 | typedef void (*ChangedCallback)(Dependency::DependencyChangedNotification, DependencyTracker *); | 
|---|
| 70 | typedef void (*DeletedCallback)(const RID &, DependencyTracker *); | 
|---|
| 71 |  | 
|---|
| 72 | ChangedCallback changed_callback = nullptr; | 
|---|
| 73 | DeletedCallback deleted_callback = nullptr; | 
|---|
| 74 |  | 
|---|
| 75 | void update_begin() { // call before updating dependencies | 
|---|
| 76 | instance_version++; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void update_dependency(Dependency *p_dependency) { //called internally, can't be used directly, use update functions in Storage | 
|---|
| 80 | dependencies.insert(p_dependency); | 
|---|
| 81 | p_dependency->instances[this] = instance_version; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | void update_end() { //call after updating dependencies | 
|---|
| 85 | List<Pair<Dependency *, DependencyTracker *>> to_clean_up; | 
|---|
| 86 |  | 
|---|
| 87 | for (Dependency *E : dependencies) { | 
|---|
| 88 | Dependency *dep = E; | 
|---|
| 89 | HashMap<DependencyTracker *, uint32_t>::Iterator F = dep->instances.find(this); | 
|---|
| 90 | ERR_CONTINUE(!F); | 
|---|
| 91 | if (F->value != instance_version) { | 
|---|
| 92 | Pair<Dependency *, DependencyTracker *> p; | 
|---|
| 93 | p.first = dep; | 
|---|
| 94 | p.second = F->key; | 
|---|
| 95 | to_clean_up.push_back(p); | 
|---|
| 96 | } | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | while (to_clean_up.size()) { | 
|---|
| 100 | to_clean_up.front()->get().first->instances.erase(to_clean_up.front()->get().second); | 
|---|
| 101 | dependencies.erase(to_clean_up.front()->get().first); | 
|---|
| 102 | to_clean_up.pop_front(); | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void clear() { // clear all dependencies | 
|---|
| 107 | for (Dependency *E : dependencies) { | 
|---|
| 108 | Dependency *dep = E; | 
|---|
| 109 | dep->instances.erase(this); | 
|---|
| 110 | } | 
|---|
| 111 | dependencies.clear(); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | ~DependencyTracker() { clear(); } | 
|---|
| 115 |  | 
|---|
| 116 | private: | 
|---|
| 117 | friend class Dependency; | 
|---|
| 118 | uint32_t instance_version = 0; | 
|---|
| 119 | HashSet<Dependency *> dependencies; | 
|---|
| 120 | }; | 
|---|
| 121 |  | 
|---|
| 122 | class RendererUtilities { | 
|---|
| 123 | public: | 
|---|
| 124 | virtual ~RendererUtilities() {} | 
|---|
| 125 |  | 
|---|
| 126 | /* INSTANCES */ | 
|---|
| 127 |  | 
|---|
| 128 | virtual RS::InstanceType get_base_type(RID p_rid) const = 0; | 
|---|
| 129 | virtual bool free(RID p_rid) = 0; | 
|---|
| 130 |  | 
|---|
| 131 | /* DEPENDENCIES */ | 
|---|
| 132 |  | 
|---|
| 133 | virtual void base_update_dependency(RID p_base, DependencyTracker *p_instance) = 0; | 
|---|
| 134 |  | 
|---|
| 135 | /* VISIBILITY NOTIFIER */ | 
|---|
| 136 |  | 
|---|
| 137 | virtual RID visibility_notifier_allocate() = 0; | 
|---|
| 138 | virtual void visibility_notifier_initialize(RID p_notifier) = 0; | 
|---|
| 139 | virtual void visibility_notifier_free(RID p_notifier) = 0; | 
|---|
| 140 |  | 
|---|
| 141 | virtual void visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) = 0; | 
|---|
| 142 | virtual void visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) = 0; | 
|---|
| 143 |  | 
|---|
| 144 | virtual AABB visibility_notifier_get_aabb(RID p_notifier) const = 0; | 
|---|
| 145 | virtual void visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) = 0; | 
|---|
| 146 |  | 
|---|
| 147 | /* TIMING */ | 
|---|
| 148 |  | 
|---|
| 149 | bool capturing_timestamps = false; | 
|---|
| 150 |  | 
|---|
| 151 | #define TIMESTAMP_BEGIN()                               \ | 
|---|
| 152 | {                                                   \ | 
|---|
| 153 | if (RSG::utilities->capturing_timestamps)       \ | 
|---|
| 154 | RSG::utilities->capture_timestamps_begin(); \ | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | #define RENDER_TIMESTAMP(m_text)                       \ | 
|---|
| 158 | {                                                  \ | 
|---|
| 159 | if (RSG::utilities->capturing_timestamps)      \ | 
|---|
| 160 | RSG::utilities->capture_timestamp(m_text); \ | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | virtual void capture_timestamps_begin() = 0; | 
|---|
| 164 | virtual void capture_timestamp(const String &p_name) = 0; | 
|---|
| 165 | virtual uint32_t get_captured_timestamps_count() const = 0; | 
|---|
| 166 | virtual uint64_t get_captured_timestamps_frame() const = 0; | 
|---|
| 167 | virtual uint64_t get_captured_timestamp_gpu_time(uint32_t p_index) const = 0; | 
|---|
| 168 | virtual uint64_t get_captured_timestamp_cpu_time(uint32_t p_index) const = 0; | 
|---|
| 169 | virtual String get_captured_timestamp_name(uint32_t p_index) const = 0; | 
|---|
| 170 |  | 
|---|
| 171 | /* MISC */ | 
|---|
| 172 |  | 
|---|
| 173 | virtual void update_dirty_resources() = 0; | 
|---|
| 174 | virtual void set_debug_generate_wireframes(bool p_generate) = 0; | 
|---|
| 175 |  | 
|---|
| 176 | virtual bool has_os_feature(const String &p_feature) const = 0; | 
|---|
| 177 |  | 
|---|
| 178 | virtual void update_memory_info() = 0; | 
|---|
| 179 |  | 
|---|
| 180 | virtual uint64_t get_rendering_info(RS::RenderingInfo p_info) = 0; | 
|---|
| 181 | virtual String get_video_adapter_name() const = 0; | 
|---|
| 182 | virtual String get_video_adapter_vendor() const = 0; | 
|---|
| 183 | virtual RenderingDevice::DeviceType get_video_adapter_type() const = 0; | 
|---|
| 184 | virtual String get_video_adapter_api_version() const = 0; | 
|---|
| 185 |  | 
|---|
| 186 | virtual Size2i get_maximum_viewport_size() const = 0; | 
|---|
| 187 | }; | 
|---|
| 188 |  | 
|---|
| 189 | #endif // RENDERER_UTILITIES_H | 
|---|
| 190 |  | 
|---|