rendering_device_binds.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, 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(1, ls.length() - 2).strip_edges();
  54. if (section == "versions") {
  55. if (stages_found) {
  56. base_error = "Invalid shader file, [version] 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 (reading_versions) {
  82. String l = line.strip_edges();
  83. if (l != "") {
  84. int eqpos = l.find("=");
  85. if (eqpos == -1) {
  86. base_error = "Version syntax is version=\"<defines with C escaping>\".";
  87. break;
  88. }
  89. String version = l.get_slice("=", 0).strip_edges();
  90. if (!version.is_valid_identifier()) {
  91. base_error = "Version names must be valid identifiers, found '" + version + "' instead.";
  92. break;
  93. }
  94. String define = l.get_slice("=", 1).strip_edges();
  95. if (!define.begins_with("\"") || !define.ends_with("\"")) {
  96. base_error = "Version text must be quoted using \"\", instead found '" + define + "'.";
  97. break;
  98. }
  99. define = "\n" + define.substr(1, define.length() - 2).c_unescape() + "\n"; //add newline before and after jsut in case
  100. version_texts[version] = define;
  101. }
  102. } else {
  103. if (stage == RD::SHADER_STAGE_MAX && line.strip_edges() != "") {
  104. base_error = "Text was found that does not belong to a valid section: " + line;
  105. break;
  106. }
  107. if (stage != RD::SHADER_STAGE_MAX) {
  108. if (line.strip_edges().begins_with("#include")) {
  109. if (p_include_func) {
  110. //process include
  111. String include = line.replace("#include", "").strip_edges();
  112. if (!include.begins_with("\"") || !include.ends_with("\"")) {
  113. base_error = "Malformed #include syntax, expected #include \"<path>\", found instad: " + include;
  114. break;
  115. }
  116. include = include.substr(1, include.length() - 2).strip_edges();
  117. String include_text = p_include_func(include, p_include_func_userdata);
  118. if (include_text != String()) {
  119. stage_code[stage] += "\n" + include_text + "\n";
  120. } else {
  121. base_error = "#include failed for file '" + include + "'";
  122. }
  123. } else {
  124. base_error = "#include used, but no include function provided.";
  125. }
  126. } else {
  127. stage_code[stage] += line + "\n";
  128. }
  129. }
  130. }
  131. }
  132. Ref<RDShaderFile> shader_file;
  133. shader_file.instance();
  134. if (base_error == "") {
  135. if (stage_found[RD::SHADER_STAGE_COMPUTE] && stages_found > 1) {
  136. ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "When writing compute shaders, [compute] mustbe the only stage present.");
  137. }
  138. if (version_texts.empty()) {
  139. version_texts[""] = ""; //make sure a default version exists
  140. }
  141. bool errors_found = false;
  142. /* STEP 2, Compile the versions, add to shader file */
  143. for (Map<StringName, String>::Element *E = version_texts.front(); E; E = E->next()) {
  144. Ref<RDShaderBytecode> bytecode;
  145. bytecode.instance();
  146. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  147. String code = stage_code[i];
  148. if (code == String()) {
  149. continue;
  150. }
  151. code = code.replace("VERSION_DEFINES", E->get());
  152. String error;
  153. Vector<uint8_t> spirv = RenderingDevice::get_singleton()->shader_compile_from_source(RD::ShaderStage(i), code, RD::SHADER_LANGUAGE_GLSL, &error, false);
  154. bytecode->set_stage_bytecode(RD::ShaderStage(i), spirv);
  155. if (error != "") {
  156. error += String() + "\n\nStage '" + stage_str[i] + "' source code: \n\n";
  157. Vector<String> sclines = code.split("\n");
  158. for (int j = 0; j < sclines.size(); j++) {
  159. error += itos(j + 1) + "\t\t" + sclines[j] + "\n";
  160. }
  161. errors_found = true;
  162. }
  163. bytecode->set_stage_compile_error(RD::ShaderStage(i), error);
  164. }
  165. set_bytecode(bytecode, E->key());
  166. }
  167. return errors_found ? ERR_PARSE_ERROR : OK;
  168. } else {
  169. return ERR_PARSE_ERROR;
  170. }
  171. }