material_storage.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. using namespace RendererDummy;
  32. MaterialStorage *MaterialStorage::singleton = nullptr;
  33. MaterialStorage::MaterialStorage() {
  34. singleton = this;
  35. ShaderCompiler::DefaultIdentifierActions actions;
  36. dummy_compiler.initialize(actions);
  37. }
  38. MaterialStorage::~MaterialStorage() {
  39. singleton = nullptr;
  40. }
  41. RID MaterialStorage::shader_allocate() {
  42. return shader_owner.allocate_rid();
  43. }
  44. void MaterialStorage::shader_initialize(RID p_rid) {
  45. shader_owner.initialize_rid(p_rid, DummyShader());
  46. }
  47. void MaterialStorage::shader_free(RID p_rid) {
  48. DummyShader *shader = shader_owner.get_or_null(p_rid);
  49. ERR_FAIL_NULL(shader);
  50. shader_owner.free(p_rid);
  51. }
  52. void MaterialStorage::shader_set_code(RID p_shader, const String &p_code) {
  53. DummyShader *shader = shader_owner.get_or_null(p_shader);
  54. ERR_FAIL_NULL(shader);
  55. if (p_code.is_empty()) {
  56. return;
  57. }
  58. String mode_string = ShaderLanguage::get_shader_type(p_code);
  59. RS::ShaderMode new_mode;
  60. if (mode_string == "canvas_item") {
  61. new_mode = RS::SHADER_CANVAS_ITEM;
  62. } else if (mode_string == "particles") {
  63. new_mode = RS::SHADER_PARTICLES;
  64. } else if (mode_string == "spatial") {
  65. new_mode = RS::SHADER_SPATIAL;
  66. } else if (mode_string == "sky") {
  67. new_mode = RS::SHADER_SKY;
  68. } else if (mode_string == "fog") {
  69. new_mode = RS::SHADER_FOG;
  70. } else {
  71. new_mode = RS::SHADER_MAX;
  72. ERR_FAIL_MSG("Shader type " + mode_string + " not supported in Dummy renderer.");
  73. }
  74. ShaderCompiler::IdentifierActions actions;
  75. actions.uniforms = &shader->uniforms;
  76. ShaderCompiler::GeneratedCode gen_code;
  77. Error err = MaterialStorage::get_singleton()->dummy_compiler.compile(new_mode, p_code, &actions, "", gen_code);
  78. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  79. }
  80. void MaterialStorage::get_shader_parameter_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  81. DummyShader *shader = shader_owner.get_or_null(p_shader);
  82. ERR_FAIL_NULL(shader);
  83. SortArray<Pair<StringName, int>, ShaderLanguage::UniformOrderComparator> sorter;
  84. LocalVector<Pair<StringName, int>> filtered_uniforms;
  85. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {
  86. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_LOCAL) {
  87. continue;
  88. }
  89. filtered_uniforms.push_back(Pair<StringName, int>(E.key, E.value.prop_order));
  90. }
  91. int uniform_count = filtered_uniforms.size();
  92. sorter.sort(filtered_uniforms.ptr(), uniform_count);
  93. String last_group;
  94. for (int i = 0; i < uniform_count; i++) {
  95. const StringName &uniform_name = filtered_uniforms[i].first;
  96. const ShaderLanguage::ShaderNode::Uniform &uniform = shader->uniforms[uniform_name];
  97. String group = uniform.group;
  98. if (!uniform.subgroup.is_empty()) {
  99. group += "::" + uniform.subgroup;
  100. }
  101. if (group != last_group) {
  102. PropertyInfo pi;
  103. pi.usage = PROPERTY_USAGE_GROUP;
  104. pi.name = group;
  105. p_param_list->push_back(pi);
  106. last_group = group;
  107. }
  108. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniform);
  109. pi.name = uniform_name;
  110. p_param_list->push_back(pi);
  111. }
  112. }