framebuffer_cache_rd.h 9.6 KB

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