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