framebuffer_cache_rd.h 9.4 KB

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