utilities.h 7.0 KB

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