framebuffer_cache_rd.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**************************************************************************/
  2. /* framebuffer_cache_rd.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 FRAMEBUFFER_CACHE_RD_H
  31. #define FRAMEBUFFER_CACHE_RD_H
  32. #include "core/templates/local_vector.h"
  33. #include "core/templates/paged_allocator.h"
  34. #include "servers/rendering/rendering_device.h"
  35. #include "servers/rendering/rendering_device_binds.h"
  36. class FramebufferCacheRD : public Object {
  37. GDCLASS(FramebufferCacheRD, Object)
  38. struct Cache {
  39. Cache *prev = nullptr;
  40. Cache *next = nullptr;
  41. uint32_t hash = 0;
  42. RID cache;
  43. LocalVector<RID> textures;
  44. LocalVector<RD::FramebufferPass> passes;
  45. uint32_t views = 0;
  46. };
  47. PagedAllocator<Cache> cache_allocator;
  48. enum {
  49. HASH_TABLE_SIZE = 16381 // Prime
  50. };
  51. Cache *hash_table[HASH_TABLE_SIZE] = {};
  52. static _FORCE_INLINE_ uint32_t _hash_pass(const RD::FramebufferPass &p, uint32_t h) {
  53. h = hash_murmur3_one_32(p.depth_attachment, h);
  54. h = hash_murmur3_one_32(p.vrs_attachment, h);
  55. h = hash_murmur3_one_32(p.color_attachments.size(), h);
  56. for (int i = 0; i < p.color_attachments.size(); i++) {
  57. h = hash_murmur3_one_32(p.color_attachments[i], h);
  58. }
  59. h = hash_murmur3_one_32(p.resolve_attachments.size(), h);
  60. for (int i = 0; i < p.resolve_attachments.size(); i++) {
  61. h = hash_murmur3_one_32(p.resolve_attachments[i], h);
  62. }
  63. h = hash_murmur3_one_32(p.preserve_attachments.size(), h);
  64. for (int i = 0; i < p.preserve_attachments.size(); i++) {
  65. h = hash_murmur3_one_32(p.preserve_attachments[i], h);
  66. }
  67. return h;
  68. }
  69. static _FORCE_INLINE_ bool _compare_pass(const RD::FramebufferPass &a, const RD::FramebufferPass &b) {
  70. if (a.depth_attachment != b.depth_attachment) {
  71. return false;
  72. }
  73. if (a.vrs_attachment != b.vrs_attachment) {
  74. return false;
  75. }
  76. if (a.color_attachments.size() != b.color_attachments.size()) {
  77. return false;
  78. }
  79. for (int i = 0; i < a.color_attachments.size(); i++) {
  80. if (a.color_attachments[i] != b.color_attachments[i]) {
  81. return false;
  82. }
  83. }
  84. if (a.resolve_attachments.size() != b.resolve_attachments.size()) {
  85. return false;
  86. }
  87. for (int i = 0; i < a.resolve_attachments.size(); i++) {
  88. if (a.resolve_attachments[i] != b.resolve_attachments[i]) {
  89. return false;
  90. }
  91. }
  92. if (a.preserve_attachments.size() != b.preserve_attachments.size()) {
  93. return false;
  94. }
  95. for (int i = 0; i < a.preserve_attachments.size(); i++) {
  96. if (a.preserve_attachments[i] != b.preserve_attachments[i]) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. _FORCE_INLINE_ uint32_t _hash_rids(uint32_t h, const RID &arg) {
  103. return hash_murmur3_one_64(arg.get_id(), h);
  104. }
  105. template <typename... Args>
  106. uint32_t _hash_rids(uint32_t h, const RID &arg, Args... args) {
  107. h = hash_murmur3_one_64(arg.get_id(), h);
  108. return _hash_rids(h, args...);
  109. }
  110. _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg) {
  111. return textures[idx] == arg;
  112. }
  113. template <typename... Args>
  114. _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RID> &textures, const RID &arg, Args... args) {
  115. if (textures[idx] != arg) {
  116. return false;
  117. }
  118. return _compare_args(idx + 1, textures, args...);
  119. }
  120. _FORCE_INLINE_ void _create_args(Vector<RID> &textures, const RID &arg) {
  121. textures.push_back(arg);
  122. }
  123. template <typename... Args>
  124. _FORCE_INLINE_ void _create_args(Vector<RID> &textures, const RID &arg, Args... args) {
  125. textures.push_back(arg);
  126. _create_args(textures, args...);
  127. }
  128. static FramebufferCacheRD *singleton;
  129. uint32_t cache_instances_used = 0;
  130. void _invalidate(Cache *p_cache);
  131. static void _framebuffer_invalidation_callback(void *p_userdata);
  132. RID _allocate_from_data(uint32_t p_views, uint32_t p_hash, uint32_t p_table_idx, const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes) {
  133. RID rid;
  134. if (p_passes.size()) {
  135. rid = RD::get_singleton()->framebuffer_create_multipass(p_textures, p_passes, RD::INVALID_ID, p_views);
  136. } else {
  137. rid = RD::get_singleton()->framebuffer_create(p_textures, RD::INVALID_ID, p_views);
  138. }
  139. ERR_FAIL_COND_V(rid.is_null(), rid);
  140. Cache *c = cache_allocator.alloc();
  141. c->views = p_views;
  142. c->cache = rid;
  143. c->hash = p_hash;
  144. c->textures.resize(p_textures.size());
  145. for (uint32_t i = 0; i < c->textures.size(); i++) {
  146. c->textures[i] = p_textures[i];
  147. }
  148. c->passes.resize(p_passes.size());
  149. for (uint32_t i = 0; i < c->passes.size(); i++) {
  150. c->passes[i] = p_passes[i];
  151. }
  152. c->prev = nullptr;
  153. c->next = hash_table[p_table_idx];
  154. if (hash_table[p_table_idx]) {
  155. hash_table[p_table_idx]->prev = c;
  156. }
  157. hash_table[p_table_idx] = c;
  158. RD::get_singleton()->framebuffer_set_invalidation_callback(rid, _framebuffer_invalidation_callback, c);
  159. cache_instances_used++;
  160. return rid;
  161. }
  162. private:
  163. static void _bind_methods();
  164. public:
  165. template <typename... Args>
  166. RID get_cache(Args... args) {
  167. uint32_t h = hash_murmur3_one_32(1); //1 view
  168. h = hash_murmur3_one_32(sizeof...(Args), h);
  169. h = _hash_rids(h, args...);
  170. h = hash_murmur3_one_32(0, h); // 0 passes
  171. h = hash_fmix32(h);
  172. uint32_t table_idx = h % HASH_TABLE_SIZE;
  173. {
  174. const Cache *c = hash_table[table_idx];
  175. while (c) {
  176. if (c->hash == h && c->passes.size() == 0 && c->textures.size() == sizeof...(Args) && c->views == 1 && _compare_args(0, c->textures, args...)) {
  177. return c->cache;
  178. }
  179. c = c->next;
  180. }
  181. }
  182. // Not in cache, create:
  183. Vector<RID> textures;
  184. _create_args(textures, args...);
  185. return _allocate_from_data(1, h, table_idx, textures, Vector<RD::FramebufferPass>());
  186. }
  187. template <typename... Args>
  188. RID get_cache_multiview(uint32_t p_views, Args... args) {
  189. uint32_t h = hash_murmur3_one_32(p_views);
  190. h = hash_murmur3_one_32(sizeof...(Args), h);
  191. h = _hash_rids(h, args...);
  192. h = hash_murmur3_one_32(0, h); // 0 passes
  193. h = hash_fmix32(h);
  194. uint32_t table_idx = h % HASH_TABLE_SIZE;
  195. {
  196. const Cache *c = hash_table[table_idx];
  197. while (c) {
  198. if (c->hash == h && c->passes.size() == 0 && c->textures.size() == sizeof...(Args) && c->views == p_views && _compare_args(0, c->textures, args...)) {
  199. return c->cache;
  200. }
  201. c = c->next;
  202. }
  203. }
  204. // Not in cache, create:
  205. Vector<RID> textures;
  206. _create_args(textures, args...);
  207. return _allocate_from_data(p_views, h, table_idx, textures, Vector<RD::FramebufferPass>());
  208. }
  209. RID get_cache_multipass(const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes, uint32_t p_views = 1) {
  210. uint32_t h = hash_murmur3_one_32(p_views);
  211. h = hash_murmur3_one_32(p_textures.size(), h);
  212. for (int i = 0; i < p_textures.size(); i++) {
  213. h = hash_murmur3_one_64(p_textures[i].get_id(), h);
  214. }
  215. h = hash_murmur3_one_32(p_passes.size(), h);
  216. for (int i = 0; i < p_passes.size(); i++) {
  217. h = _hash_pass(p_passes[i], h);
  218. }
  219. h = hash_fmix32(h);
  220. uint32_t table_idx = h % HASH_TABLE_SIZE;
  221. {
  222. const Cache *c = hash_table[table_idx];
  223. while (c) {
  224. if (c->hash == h && c->views == p_views && c->textures.size() == (uint32_t)p_textures.size() && c->passes.size() == (uint32_t)p_passes.size()) {
  225. bool all_ok = true;
  226. for (int i = 0; i < p_textures.size(); i++) {
  227. if (p_textures[i] != c->textures[i]) {
  228. all_ok = false;
  229. break;
  230. }
  231. }
  232. if (all_ok) {
  233. for (int i = 0; i < p_passes.size(); i++) {
  234. if (!_compare_pass(p_passes[i], c->passes[i])) {
  235. all_ok = false;
  236. break;
  237. }
  238. }
  239. }
  240. if (all_ok) {
  241. return c->cache;
  242. }
  243. }
  244. c = c->next;
  245. }
  246. }
  247. // Not in cache, create:
  248. return _allocate_from_data(p_views, h, table_idx, p_textures, p_passes);
  249. }
  250. static RID get_cache_multipass_array(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_views = 1);
  251. static FramebufferCacheRD *get_singleton() { return singleton; }
  252. FramebufferCacheRD();
  253. ~FramebufferCacheRD();
  254. };
  255. #endif // FRAMEBUFFER_CACHE_RD_H