framebuffer_cache_rd.h 9.6 KB

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