rendering_device_binds.cpp 8.1 KB

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