uniform_set_cache_rd.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**************************************************************************/
  2. /* uniform_set_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 UNIFORM_SET_CACHE_RD_H
  31. #define UNIFORM_SET_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 UniformSetCacheRD : public Object {
  36. GDCLASS(UniformSetCacheRD, Object)
  37. struct Cache {
  38. Cache *prev = nullptr;
  39. Cache *next = nullptr;
  40. uint32_t hash = 0;
  41. RID shader;
  42. uint32_t set = 0;
  43. RID cache;
  44. LocalVector<RD::Uniform> uniforms;
  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_uniform(const RD::Uniform &u, uint32_t h) {
  52. h = hash_murmur3_one_32(u.uniform_type, h);
  53. h = hash_murmur3_one_32(u.binding, h);
  54. uint32_t rsize = u.get_id_count();
  55. for (uint32_t j = 0; j < rsize; j++) {
  56. h = hash_murmur3_one_64(u.get_id(j).get_id(), h);
  57. }
  58. return hash_fmix32(h);
  59. }
  60. static _FORCE_INLINE_ bool _compare_uniform(const RD::Uniform &a, const RD::Uniform &b) {
  61. if (a.binding != b.binding) {
  62. return false;
  63. }
  64. if (a.uniform_type != b.uniform_type) {
  65. return false;
  66. }
  67. uint32_t rsize = a.get_id_count();
  68. if (rsize != b.get_id_count()) {
  69. return false;
  70. }
  71. for (uint32_t j = 0; j < rsize; j++) {
  72. if (a.get_id(j) != b.get_id(j)) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. _FORCE_INLINE_ uint32_t _hash_args(uint32_t h, const RD::Uniform &arg) {
  79. return _hash_uniform(arg, h);
  80. }
  81. template <typename... Args>
  82. uint32_t _hash_args(uint32_t h, const RD::Uniform &arg, Args... args) {
  83. h = _hash_uniform(arg, h);
  84. return _hash_args(h, args...);
  85. }
  86. _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RD::Uniform> &uniforms, const RD::Uniform &arg) {
  87. return _compare_uniform(uniforms[idx], arg);
  88. }
  89. template <typename... Args>
  90. _FORCE_INLINE_ bool _compare_args(uint32_t idx, const LocalVector<RD::Uniform> &uniforms, const RD::Uniform &arg, Args... args) {
  91. if (!_compare_uniform(uniforms[idx], arg)) {
  92. return false;
  93. }
  94. return _compare_args(idx + 1, uniforms, args...);
  95. }
  96. _FORCE_INLINE_ void _create_args(Vector<RD::Uniform> &uniforms, const RD::Uniform &arg) {
  97. uniforms.push_back(arg);
  98. }
  99. template <typename... Args>
  100. _FORCE_INLINE_ void _create_args(Vector<RD::Uniform> &uniforms, const RD::Uniform &arg, Args... args) {
  101. uniforms.push_back(arg);
  102. _create_args(uniforms, args...);
  103. }
  104. static UniformSetCacheRD *singleton;
  105. uint32_t cache_instances_used = 0;
  106. void _invalidate(Cache *p_cache);
  107. static void _uniform_set_invalidation_callback(void *p_userdata);
  108. RID _allocate_from_uniforms(RID p_shader, uint32_t p_set, uint32_t p_hash, uint32_t p_table_idx, const Vector<RD::Uniform> &p_uniforms) {
  109. RID rid = RD::get_singleton()->uniform_set_create(p_uniforms, p_shader, p_set);
  110. ERR_FAIL_COND_V(rid.is_null(), rid);
  111. Cache *c = cache_allocator.alloc();
  112. c->hash = p_hash;
  113. c->set = p_set;
  114. c->shader = p_shader;
  115. c->cache = rid;
  116. c->uniforms.resize(p_uniforms.size());
  117. for (uint32_t i = 0; i < c->uniforms.size(); i++) {
  118. c->uniforms[i] = p_uniforms[i];
  119. }
  120. c->prev = nullptr;
  121. c->next = hash_table[p_table_idx];
  122. if (hash_table[p_table_idx]) {
  123. hash_table[p_table_idx]->prev = c;
  124. }
  125. hash_table[p_table_idx] = c;
  126. RD::get_singleton()->uniform_set_set_invalidation_callback(rid, _uniform_set_invalidation_callback, c);
  127. cache_instances_used++;
  128. return rid;
  129. }
  130. public:
  131. template <typename... Args>
  132. RID get_cache(RID p_shader, uint32_t p_set, Args... args) {
  133. uint32_t h = hash_murmur3_one_64(p_shader.get_id());
  134. h = hash_murmur3_one_32(p_set, h);
  135. h = _hash_args(h, args...);
  136. uint32_t table_idx = h % HASH_TABLE_SIZE;
  137. {
  138. const Cache *c = hash_table[table_idx];
  139. while (c) {
  140. if (c->hash == h && c->set == p_set && c->shader == p_shader && sizeof...(Args) == c->uniforms.size() && _compare_args(0, c->uniforms, args...)) {
  141. return c->cache;
  142. }
  143. c = c->next;
  144. }
  145. }
  146. // Not in cache, create:
  147. Vector<RD::Uniform> uniforms;
  148. _create_args(uniforms, args...);
  149. return _allocate_from_uniforms(p_shader, p_set, h, table_idx, uniforms);
  150. }
  151. template <typename... Args>
  152. RID get_cache_vec(RID p_shader, uint32_t p_set, const Vector<RD::Uniform> &p_uniforms) {
  153. uint32_t h = hash_murmur3_one_64(p_shader.get_id());
  154. h = hash_murmur3_one_32(p_set, h);
  155. for (int i = 0; i < p_uniforms.size(); i++) {
  156. h = _hash_uniform(p_uniforms[i], h);
  157. }
  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->set == p_set && c->shader == p_shader && (uint32_t)p_uniforms.size() == c->uniforms.size()) {
  164. bool all_ok = true;
  165. for (int i = 0; i < p_uniforms.size(); i++) {
  166. if (!_compare_uniform(p_uniforms[i], c->uniforms[i])) {
  167. all_ok = false;
  168. break;
  169. }
  170. }
  171. if (all_ok) {
  172. return c->cache;
  173. }
  174. }
  175. c = c->next;
  176. }
  177. }
  178. // Not in cache, create:
  179. return _allocate_from_uniforms(p_shader, p_set, h, table_idx, p_uniforms);
  180. }
  181. static UniformSetCacheRD *get_singleton() { return singleton; }
  182. UniformSetCacheRD();
  183. ~UniformSetCacheRD();
  184. };
  185. #endif // UNIFORM_SET_CACHE_RD_H