utilities.h 7.0 KB

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