rendering_device_binds.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************************************/
  2. /* rendering_device_binds.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 "rendering_device_binds.h"
  31. Error RDShaderFile::parse_versions_from_text(const String &p_text, const String p_defines, OpenIncludeFunction p_include_func, void *p_include_func_userdata) {
  32. ERR_FAIL_NULL_V_MSG(
  33. RenderingDevice::get_singleton(),
  34. ERR_UNAVAILABLE,
  35. "Cannot import custom .glsl shaders when running without a RenderingDevice. This can happen if you are using the headless more or the Compatibility backend.");
  36. Vector<String> lines = p_text.split("\n");
  37. bool reading_versions = false;
  38. bool stage_found[RD::SHADER_STAGE_MAX] = { false, false, false, false, false };
  39. RD::ShaderStage stage = RD::SHADER_STAGE_MAX;
  40. static const char *stage_str[RD::SHADER_STAGE_MAX] = {
  41. "vertex",
  42. "fragment",
  43. "tesselation_control",
  44. "tesselation_evaluation",
  45. "compute",
  46. };
  47. String stage_code[RD::SHADER_STAGE_MAX];
  48. int stages_found = 0;
  49. HashMap<StringName, String> version_texts;
  50. versions.clear();
  51. base_error = "";
  52. for (int lidx = 0; lidx < lines.size(); lidx++) {
  53. String line = lines[lidx];
  54. {
  55. String ls = line.strip_edges();
  56. if (ls.begins_with("#[") && ls.ends_with("]")) {
  57. String section = ls.substr(2, ls.length() - 3).strip_edges();
  58. if (section == "versions") {
  59. if (stages_found) {
  60. base_error = "Invalid shader file, #[versions] must be the first section found.";
  61. break;
  62. }
  63. reading_versions = true;
  64. } else {
  65. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  66. if (section == stage_str[i]) {
  67. if (stage_found[i]) {
  68. base_error = "Invalid shader file, stage appears twice: " + section;
  69. break;
  70. }
  71. stage_found[i] = true;
  72. stages_found++;
  73. stage = RD::ShaderStage(i);
  74. reading_versions = false;
  75. break;
  76. }
  77. }
  78. if (!base_error.is_empty()) {
  79. break;
  80. }
  81. }
  82. continue;
  83. }
  84. }
  85. if (stage == RD::SHADER_STAGE_MAX && !line.strip_edges().is_empty()) {
  86. line = line.strip_edges();
  87. if (line.begins_with("//") || line.begins_with("/*")) {
  88. continue; //assuming comment (single line)
  89. }
  90. }
  91. if (reading_versions) {
  92. String l = line.strip_edges();
  93. if (!l.is_empty()) {
  94. if (!l.contains("=")) {
  95. base_error = "Missing `=` in '" + l + "'. Version syntax is `version = \"<defines with C escaping>\";`.";
  96. break;
  97. }
  98. if (!l.contains(";")) {
  99. // We don't require a semicolon per se, but it's needed for clang-format to handle things properly.
  100. base_error = "Missing `;` in '" + l + "'. Version syntax is `version = \"<defines with C escaping>\";`.";
  101. break;
  102. }
  103. Vector<String> slices = l.get_slice(";", 0).split("=");
  104. String version = slices[0].strip_edges();
  105. if (!version.is_valid_ascii_identifier()) {
  106. base_error = "Version names must be valid identifiers, found '" + version + "' instead.";
  107. break;
  108. }
  109. String define = slices[1].strip_edges();
  110. if (!define.begins_with("\"") || !define.ends_with("\"")) {
  111. base_error = "Version text must be quoted using \"\", instead found '" + define + "'.";
  112. break;
  113. }
  114. define = "\n" + define.substr(1, define.length() - 2).c_unescape() + "\n"; // Add newline before and after just in case.
  115. version_texts[version] = define + "\n" + p_defines;
  116. }
  117. } else {
  118. if (stage == RD::SHADER_STAGE_MAX && !line.strip_edges().is_empty()) {
  119. base_error = "Text was found that does not belong to a valid section: " + line;
  120. break;
  121. }
  122. if (stage != RD::SHADER_STAGE_MAX) {
  123. if (line.strip_edges().begins_with("#include")) {
  124. if (p_include_func) {
  125. //process include
  126. String include = line.replace("#include", "").strip_edges();
  127. if (!include.begins_with("\"") || !include.ends_with("\"")) {
  128. base_error = "Malformed #include syntax, expected #include \"<path>\", found instead: " + include;
  129. break;
  130. }
  131. include = include.substr(1, include.length() - 2).strip_edges();
  132. String include_text = p_include_func(include, p_include_func_userdata);
  133. if (!include_text.is_empty()) {
  134. stage_code[stage] += "\n" + include_text + "\n";
  135. } else {
  136. base_error = "#include failed for file '" + include + "'";
  137. }
  138. } else {
  139. base_error = "#include used, but no include function provided.";
  140. }
  141. } else {
  142. stage_code[stage] += line + "\n";
  143. }
  144. }
  145. }
  146. }
  147. if (base_error.is_empty()) {
  148. if (stage_found[RD::SHADER_STAGE_COMPUTE] && stages_found > 1) {
  149. ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "When writing compute shaders, [compute] mustbe the only stage present.");
  150. }
  151. if (version_texts.is_empty()) {
  152. version_texts[""] = ""; //make sure a default version exists
  153. }
  154. bool errors_found = false;
  155. /* STEP 2, Compile the versions, add to shader file */
  156. for (const KeyValue<StringName, String> &E : version_texts) {
  157. Ref<RDShaderSPIRV> bytecode;
  158. bytecode.instantiate();
  159. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  160. String code = stage_code[i];
  161. if (code.is_empty()) {
  162. continue;
  163. }
  164. code = code.replace("VERSION_DEFINES", E.value);
  165. String error;
  166. Vector<uint8_t> spirv = RenderingDevice::get_singleton()->shader_compile_spirv_from_source(RD::ShaderStage(i), code, RD::SHADER_LANGUAGE_GLSL, &error, false);
  167. bytecode->set_stage_bytecode(RD::ShaderStage(i), spirv);
  168. if (!error.is_empty()) {
  169. error += String() + "\n\nStage '" + stage_str[i] + "' source code: \n\n";
  170. Vector<String> sclines = code.split("\n");
  171. for (int j = 0; j < sclines.size(); j++) {
  172. error += itos(j + 1) + "\t\t" + sclines[j] + "\n";
  173. }
  174. errors_found = true;
  175. }
  176. bytecode->set_stage_compile_error(RD::ShaderStage(i), error);
  177. }
  178. set_bytecode(bytecode, E.key);
  179. }
  180. return errors_found ? ERR_PARSE_ERROR : OK;
  181. } else {
  182. return ERR_PARSE_ERROR;
  183. }
  184. }