instance_uniforms.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**************************************************************************/
  2. /* instance_uniforms.cpp */
  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. #include "instance_uniforms.h"
  31. #include "rendering_server_globals.h"
  32. void InstanceUniforms::free(RID p_self) {
  33. ERR_FAIL_COND(p_self.is_null());
  34. if (is_allocated()) {
  35. RSG::material_storage->global_shader_parameters_instance_free(p_self);
  36. _location = -1;
  37. }
  38. _invalidate_items();
  39. }
  40. void InstanceUniforms::materials_start() {
  41. _invalidate_items();
  42. }
  43. void InstanceUniforms::materials_append(RID p_material) {
  44. ERR_FAIL_COND(p_material.is_null());
  45. List<RendererMaterialStorage::InstanceShaderParam> params;
  46. RSG::material_storage->material_get_instance_shader_parameters(p_material, &params);
  47. for (const RendererMaterialStorage::InstanceShaderParam &srcp : params) {
  48. StringName name = srcp.info.name;
  49. if (Item *ptr = _parameters.getptr(name); ptr) {
  50. if (!ptr->is_valid()) {
  51. _init_param(*ptr, srcp);
  52. } else if (ptr->index != srcp.index) {
  53. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +
  54. "', but they do it with different indices. Only the first one (in order) will display correctly.");
  55. } else if (ptr->info.type != srcp.info.type) {
  56. WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +
  57. "', but they do it with different data types. Only the first one (in order) will display correctly.");
  58. }
  59. } else {
  60. Item i;
  61. _init_param(i, srcp);
  62. _parameters[name] = i;
  63. }
  64. }
  65. }
  66. bool InstanceUniforms::materials_finish(RID p_self) {
  67. ERR_FAIL_COND_V(p_self.is_null(), false);
  68. if (_parameters.is_empty()) {
  69. if (is_allocated()) {
  70. free(p_self);
  71. return true;
  72. }
  73. return false;
  74. }
  75. const bool should_alloc = !is_allocated();
  76. if (should_alloc) {
  77. _location = RSG::material_storage->global_shader_parameters_instance_allocate(p_self);
  78. }
  79. for (KeyValue<StringName, Item> &kv : _parameters) {
  80. Item &i = kv.value;
  81. if (i.is_valid()) {
  82. RSG::material_storage->global_shader_parameters_instance_update(p_self, i.index, i.value, i.flags);
  83. }
  84. }
  85. return should_alloc;
  86. }
  87. Variant InstanceUniforms::get(const StringName &p_name) const {
  88. if (const Item *ptr = _parameters.getptr(p_name); ptr) {
  89. return ptr->value;
  90. }
  91. return Variant();
  92. }
  93. void InstanceUniforms::set(RID p_self, const StringName &p_name, const Variant &p_value) {
  94. ERR_FAIL_COND(p_self.is_null());
  95. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  96. if (Item *ptr = _parameters.getptr(p_name); ptr) {
  97. ptr->value = p_value;
  98. if (ptr->is_valid()) {
  99. RSG::material_storage->global_shader_parameters_instance_update(p_self, ptr->index, ptr->value, ptr->flags);
  100. }
  101. } else {
  102. Item i; // Initialize in materials_finish.
  103. i.value = p_value;
  104. _parameters[p_name] = i;
  105. }
  106. }
  107. Variant InstanceUniforms::get_default(const StringName &p_name) const {
  108. if (const Item *ptr = _parameters.getptr(p_name); ptr) {
  109. return ptr->default_value;
  110. }
  111. return Variant();
  112. }
  113. void InstanceUniforms::get_property_list(List<PropertyInfo> &r_parameters) const {
  114. Vector<StringName> names;
  115. // Invalid items won't be saved, but will remain in memory in case of shader compilation failure.
  116. for (const KeyValue<StringName, Item> &kv : _parameters) {
  117. if (kv.value.is_valid()) {
  118. names.push_back(kv.key);
  119. }
  120. }
  121. names.sort_custom<StringName::AlphCompare>();
  122. for (const StringName &n : names) {
  123. PropertyInfo pinfo = _parameters[n].info;
  124. r_parameters.push_back(pinfo);
  125. }
  126. }
  127. void InstanceUniforms::_init_param(Item &r_item, const RendererMaterialStorage::InstanceShaderParam &p_param) const {
  128. r_item.index = p_param.index;
  129. r_item.flags = 0;
  130. r_item.info = p_param.info;
  131. r_item.default_value = p_param.default_value;
  132. if (r_item.default_value.get_type() == Variant::NIL) {
  133. Callable::CallError cerr;
  134. Variant::construct(r_item.info.type, r_item.default_value, nullptr, 0, cerr);
  135. }
  136. if (r_item.value.get_type() == Variant::NIL) {
  137. r_item.value = r_item.default_value;
  138. }
  139. if (r_item.info.hint == PROPERTY_HINT_FLAGS) {
  140. // HACK: Detect boolean flags count and prevent overhead.
  141. switch (r_item.info.hint_string.length()) {
  142. case 3: // "x,y"
  143. r_item.flags = 1;
  144. break;
  145. case 5: // "x,y,z"
  146. r_item.flags = 2;
  147. break;
  148. case 7: // "x,y,z,w"
  149. r_item.flags = 3;
  150. break;
  151. }
  152. }
  153. }
  154. void InstanceUniforms::_invalidate_items() {
  155. for (KeyValue<StringName, Item> &kv : _parameters) {
  156. kv.value.index = -1;
  157. }
  158. }