material_storage.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**************************************************************************/
  2. /* material_storage.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 "material_storage.h"
  31. #include "core/config/project_settings.h"
  32. using namespace RendererDummy;
  33. MaterialStorage *MaterialStorage::singleton = nullptr;
  34. MaterialStorage::MaterialStorage() {
  35. singleton = this;
  36. ShaderCompiler::DefaultIdentifierActions actions;
  37. dummy_compiler.initialize(actions);
  38. }
  39. MaterialStorage::~MaterialStorage() {
  40. singleton = nullptr;
  41. global_shader_variables.clear();
  42. }
  43. void MaterialStorage::global_shader_parameter_add(const StringName &p_name, RS::GlobalShaderParameterType p_type, const Variant &p_value) {
  44. ERR_FAIL_COND(global_shader_variables.has(p_name));
  45. global_shader_variables[p_name] = p_type;
  46. }
  47. void MaterialStorage::global_shader_parameter_remove(const StringName &p_name) {
  48. if (!global_shader_variables.has(p_name)) {
  49. return;
  50. }
  51. global_shader_variables.erase(p_name);
  52. }
  53. Vector<StringName> MaterialStorage::global_shader_parameter_get_list() const {
  54. Vector<StringName> names;
  55. for (const KeyValue<StringName, RS::GlobalShaderParameterType> &E : global_shader_variables) {
  56. names.push_back(E.key);
  57. }
  58. names.sort_custom<StringName::AlphCompare>();
  59. return names;
  60. }
  61. RS::GlobalShaderParameterType MaterialStorage::global_shader_parameter_get_type(const StringName &p_name) const {
  62. if (!global_shader_variables.has(p_name)) {
  63. print_line("don't have name, sorry");
  64. return RS::GLOBAL_VAR_TYPE_MAX;
  65. }
  66. return global_shader_variables[p_name];
  67. }
  68. void MaterialStorage::global_shader_parameters_load_settings(bool p_load_textures) {
  69. List<PropertyInfo> settings;
  70. ProjectSettings::get_singleton()->get_property_list(&settings);
  71. for (const PropertyInfo &E : settings) {
  72. if (E.name.begins_with("shader_globals/")) {
  73. StringName name = E.name.get_slicec('/', 1);
  74. Dictionary d = GLOBAL_GET(E.name);
  75. ERR_CONTINUE(!d.has("type"));
  76. ERR_CONTINUE(!d.has("value"));
  77. String type = d["type"];
  78. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  79. "bool",
  80. "bvec2",
  81. "bvec3",
  82. "bvec4",
  83. "int",
  84. "ivec2",
  85. "ivec3",
  86. "ivec4",
  87. "rect2i",
  88. "uint",
  89. "uvec2",
  90. "uvec3",
  91. "uvec4",
  92. "float",
  93. "vec2",
  94. "vec3",
  95. "vec4",
  96. "color",
  97. "rect2",
  98. "mat2",
  99. "mat3",
  100. "mat4",
  101. "transform_2d",
  102. "transform",
  103. "sampler2D",
  104. "sampler2DArray",
  105. "sampler3D",
  106. "samplerCube",
  107. "samplerExternalOES"
  108. };
  109. RS::GlobalShaderParameterType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  110. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  111. if (global_var_type_names[i] == type) {
  112. gvtype = RS::GlobalShaderParameterType(i);
  113. break;
  114. }
  115. }
  116. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  117. if (!global_shader_variables.has(name)) {
  118. global_shader_parameter_add(name, gvtype, Variant());
  119. }
  120. }
  121. }
  122. }
  123. RID MaterialStorage::shader_allocate() {
  124. return shader_owner.allocate_rid();
  125. }
  126. void MaterialStorage::shader_initialize(RID p_rid) {
  127. shader_owner.initialize_rid(p_rid, DummyShader());
  128. }
  129. void MaterialStorage::shader_free(RID p_rid) {
  130. DummyShader *shader = shader_owner.get_or_null(p_rid);
  131. ERR_FAIL_NULL(shader);
  132. shader_owner.free(p_rid);
  133. }
  134. void MaterialStorage::shader_set_code(RID p_shader, const String &p_code) {
  135. DummyShader *shader = shader_owner.get_or_null(p_shader);
  136. ERR_FAIL_NULL(shader);
  137. if (p_code.is_empty()) {
  138. return;
  139. }
  140. String mode_string = ShaderLanguage::get_shader_type(p_code);
  141. RS::ShaderMode new_mode;
  142. if (mode_string == "canvas_item") {
  143. new_mode = RS::SHADER_CANVAS_ITEM;
  144. } else if (mode_string == "particles") {
  145. new_mode = RS::SHADER_PARTICLES;
  146. } else if (mode_string == "spatial") {
  147. new_mode = RS::SHADER_SPATIAL;
  148. } else if (mode_string == "sky") {
  149. new_mode = RS::SHADER_SKY;
  150. } else if (mode_string == "fog") {
  151. new_mode = RS::SHADER_FOG;
  152. } else {
  153. new_mode = RS::SHADER_MAX;
  154. ERR_FAIL_MSG("Shader type " + mode_string + " not supported in Dummy renderer.");
  155. }
  156. ShaderCompiler::IdentifierActions actions;
  157. actions.uniforms = &shader->uniforms;
  158. ShaderCompiler::GeneratedCode gen_code;
  159. Error err = MaterialStorage::get_singleton()->dummy_compiler.compile(new_mode, p_code, &actions, "", gen_code);
  160. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  161. }
  162. void MaterialStorage::get_shader_parameter_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  163. DummyShader *shader = shader_owner.get_or_null(p_shader);
  164. ERR_FAIL_NULL(shader);
  165. SortArray<Pair<StringName, int>, ShaderLanguage::UniformOrderComparator> sorter;
  166. LocalVector<Pair<StringName, int>> filtered_uniforms;
  167. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {
  168. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_LOCAL) {
  169. continue;
  170. }
  171. filtered_uniforms.push_back(Pair<StringName, int>(E.key, E.value.prop_order));
  172. }
  173. int uniform_count = filtered_uniforms.size();
  174. sorter.sort(filtered_uniforms.ptr(), uniform_count);
  175. String last_group;
  176. for (int i = 0; i < uniform_count; i++) {
  177. const StringName &uniform_name = filtered_uniforms[i].first;
  178. const ShaderLanguage::ShaderNode::Uniform &uniform = shader->uniforms[uniform_name];
  179. String group = uniform.group;
  180. if (!uniform.subgroup.is_empty()) {
  181. group += "::" + uniform.subgroup;
  182. }
  183. if (group != last_group) {
  184. PropertyInfo pi;
  185. pi.usage = PROPERTY_USAGE_GROUP;
  186. pi.name = group;
  187. p_param_list->push_back(pi);
  188. last_group = group;
  189. }
  190. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniform);
  191. pi.name = uniform_name;
  192. p_param_list->push_back(pi);
  193. }
  194. }
  195. RID MaterialStorage::material_allocate() {
  196. return material_owner.allocate_rid();
  197. }
  198. void MaterialStorage::material_initialize(RID p_rid) {
  199. material_owner.initialize_rid(p_rid, DummyMaterial());
  200. }
  201. void MaterialStorage::material_free(RID p_rid) {
  202. DummyMaterial *material = material_owner.get_or_null(p_rid);
  203. ERR_FAIL_NULL(material);
  204. material_owner.free(p_rid);
  205. }
  206. void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {
  207. DummyMaterial *material = material_owner.get_or_null(p_material);
  208. ERR_FAIL_NULL(material);
  209. material->shader = p_shader;
  210. }
  211. void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {
  212. DummyMaterial *material = material_owner.get_or_null(p_material);
  213. ERR_FAIL_NULL(material);
  214. material->next_pass = p_next_material;
  215. }
  216. void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  217. DummyMaterial *material = material_owner.get_or_null(p_material);
  218. ERR_FAIL_NULL(material);
  219. DummyShader *shader = shader_owner.get_or_null(material->shader);
  220. if (shader) {
  221. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {
  222. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  223. continue;
  224. }
  225. RendererMaterialStorage::InstanceShaderParam p;
  226. p.info = ShaderLanguage::uniform_to_property_info(E.value);
  227. p.info.name = E.key; //supply name
  228. p.index = E.value.instance_index;
  229. p.default_value = ShaderLanguage::constant_value_to_variant(E.value.default_value, E.value.type, E.value.array_size, E.value.hint);
  230. r_parameters->push_back(p);
  231. }
  232. }
  233. if (material->next_pass.is_valid()) {
  234. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  235. }
  236. }