framebuffer_cache_rd.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. static FramebufferCacheRD *singleton;
  116. uint32_t cache_instances_used = 0;
  117. void _invalidate(Cache *p_cache);
  118. static void _framebuffer_invalidation_callback(void *p_userdata);
  119. 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) {
  120. RID rid;
  121. if (p_passes.size()) {
  122. rid = RD::get_singleton()->framebuffer_create_multipass(p_textures, p_passes, RD::INVALID_ID, p_views);
  123. } else {
  124. rid = RD::get_singleton()->framebuffer_create(p_textures, RD::INVALID_ID, p_views);
  125. }
  126. ERR_FAIL_COND_V(rid.is_null(), rid);
  127. Cache *c = cache_allocator.alloc();
  128. c->views = p_views;
  129. c->cache = rid;
  130. c->hash = p_hash;
  131. c->textures.resize(p_textures.size());
  132. for (uint32_t i = 0; i < c->textures.size(); i++) {
  133. c->textures[i] = p_textures[i];
  134. }
  135. c->passes.resize(p_passes.size());
  136. for (uint32_t i = 0; i < c->passes.size(); i++) {
  137. c->passes[i] = p_passes[i];
  138. }
  139. c->prev = nullptr;
  140. c->next = hash_table[p_table_idx];
  141. if (hash_table[p_table_idx]) {
  142. hash_table[p_table_idx]->prev = c;
  143. }
  144. hash_table[p_table_idx] = c;
  145. RD::get_singleton()->framebuffer_set_invalidation_callback(rid, _framebuffer_invalidation_callback, c);
  146. cache_instances_used++;
  147. return rid;
  148. }
  149. private:
  150. static void _bind_methods();
  151. public:
  152. template <typename... Args>
  153. RID get_cache(Args... args) {
  154. uint32_t h = hash_murmur3_one_32(1); //1 view
  155. h = hash_murmur3_one_32(sizeof...(Args), h);
  156. h = _hash_rids(h, args...);
  157. h = hash_murmur3_one_32(0, h); // 0 passes
  158. h = hash_fmix32(h);
  159. uint32_t table_idx = h % HASH_TABLE_SIZE;
  160. {
  161. const Cache *c = hash_table[table_idx];
  162. while (c) {
  163. if (c->hash == h && c->passes.is_empty() && c->textures.size() == sizeof...(Args) && c->views == 1 && _compare_args(0, c->textures, args...)) {
  164. return c->cache;
  165. }
  166. c = c->next;
  167. }
  168. }
  169. // Not in cache, create:
  170. return _allocate_from_data(1, h, table_idx, Vector<RID>{ args... }, Vector<RD::FramebufferPass>());
  171. }
  172. template <typename... Args>
  173. RID get_cache_multiview(uint32_t p_views, Args... args) {
  174. uint32_t h = hash_murmur3_one_32(p_views);
  175. h = hash_murmur3_one_32(sizeof...(Args), h);
  176. h = _hash_rids(h, args...);
  177. h = hash_murmur3_one_32(0, h); // 0 passes
  178. h = hash_fmix32(h);
  179. uint32_t table_idx = h % HASH_TABLE_SIZE;
  180. {
  181. const Cache *c = hash_table[table_idx];
  182. while (c) {
  183. if (c->hash == h && c->passes.is_empty() && c->textures.size() == sizeof...(Args) && c->views == p_views && _compare_args(0, c->textures, args...)) {
  184. return c->cache;
  185. }
  186. c = c->next;
  187. }
  188. }
  189. // Not in cache, create:
  190. return _allocate_from_data(p_views, h, table_idx, Vector<RID>{ args... }, Vector<RD::FramebufferPass>());
  191. }
  192. RID get_cache_multipass(const Vector<RID> &p_textures, const Vector<RD::FramebufferPass> &p_passes, uint32_t p_views = 1) {
  193. uint32_t h = hash_murmur3_one_32(p_views);
  194. h = hash_murmur3_one_32(p_textures.size(), h);
  195. for (int i = 0; i < p_textures.size(); i++) {
  196. h = hash_murmur3_one_64(p_textures[i].get_id(), h);
  197. }
  198. h = hash_murmur3_one_32(p_passes.size(), h);
  199. for (int i = 0; i < p_passes.size(); i++) {
  200. h = _hash_pass(p_passes[i], h);
  201. }
  202. h = hash_fmix32(h);
  203. uint32_t table_idx = h % HASH_TABLE_SIZE;
  204. {
  205. const Cache *c = hash_table[table_idx];
  206. while (c) {
  207. if (c->hash == h && c->views == p_views && c->textures.size() == (uint32_t)p_textures.size() && c->passes.size() == (uint32_t)p_passes.size()) {
  208. bool all_ok = true;
  209. for (int i = 0; i < p_textures.size(); i++) {
  210. if (p_textures[i] != c->textures[i]) {
  211. all_ok = false;
  212. break;
  213. }
  214. }
  215. if (all_ok) {
  216. for (int i = 0; i < p_passes.size(); i++) {
  217. if (!_compare_pass(p_passes[i], c->passes[i])) {
  218. all_ok = false;
  219. break;
  220. }
  221. }
  222. }
  223. if (all_ok) {
  224. return c->cache;
  225. }
  226. }
  227. c = c->next;
  228. }
  229. }
  230. // Not in cache, create:
  231. return _allocate_from_data(p_views, h, table_idx, p_textures, p_passes);
  232. }
  233. static RID get_cache_multipass_array(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_views = 1);
  234. static FramebufferCacheRD *get_singleton() { return singleton; }
  235. FramebufferCacheRD();
  236. ~FramebufferCacheRD();
  237. };