uniform_set_cache_rd.h 7.5 KB

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