rendering_device_binds.cpp 7.6 KB

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