rendering_device_binds.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "rendering_device_binds.h"
  2. Error RDShaderFile::parse_versions_from_text(const String &p_text, OpenIncludeFunction p_include_func, void *p_include_func_userdata) {
  3. Vector<String> lines = p_text.split("\n");
  4. bool reading_versions = false;
  5. bool stage_found[RD::SHADER_STAGE_MAX] = { false, false, false, false, false };
  6. RD::ShaderStage stage = RD::SHADER_STAGE_MAX;
  7. static const char *stage_str[RD::SHADER_STAGE_MAX] = {
  8. "vertex",
  9. "fragment",
  10. "tesselation_control",
  11. "tesselation_evaluation",
  12. "compute"
  13. };
  14. String stage_code[RD::SHADER_STAGE_MAX];
  15. int stages_found = 0;
  16. Map<StringName, String> version_texts;
  17. versions.clear();
  18. base_error = "";
  19. for (int lidx = 0; lidx < lines.size(); lidx++) {
  20. String line = lines[lidx];
  21. {
  22. String ls = line.strip_edges();
  23. if (ls.begins_with("[") && ls.ends_with("]")) {
  24. String section = ls.substr(1, ls.length() - 2).strip_edges();
  25. if (section == "versions") {
  26. if (stages_found) {
  27. base_error = "Invalid shader file, [version] must be the first section found.";
  28. break;
  29. }
  30. reading_versions = true;
  31. } else {
  32. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  33. if (section == stage_str[i]) {
  34. if (stage_found[i]) {
  35. base_error = "Invalid shader file, stage appears twice: " + section;
  36. break;
  37. }
  38. stage_found[i] = true;
  39. stages_found++;
  40. stage = RD::ShaderStage(i);
  41. reading_versions = false;
  42. break;
  43. }
  44. }
  45. if (base_error != String()) {
  46. break;
  47. }
  48. }
  49. continue;
  50. }
  51. }
  52. if (reading_versions) {
  53. String l = line.strip_edges();
  54. if (l != "") {
  55. int eqpos = l.find("=");
  56. if (eqpos == -1) {
  57. base_error = "Version syntax is version=\"<defines with C escaping>\".";
  58. break;
  59. }
  60. String version = l.get_slice("=", 0).strip_edges();
  61. if (!version.is_valid_identifier()) {
  62. base_error = "Version names must be valid identifiers, found '" + version + "' instead.";
  63. break;
  64. }
  65. String define = l.get_slice("=", 1).strip_edges();
  66. if (!define.begins_with("\"") || !define.ends_with("\"")) {
  67. base_error = "Version text must be quoted using \"\", instead found '" + define + "'.";
  68. break;
  69. }
  70. define = "\n" + define.substr(1, define.length() - 2).c_unescape() + "\n"; //add newline before and after jsut in case
  71. version_texts[version] = define;
  72. }
  73. } else {
  74. if (stage == RD::SHADER_STAGE_MAX && line.strip_edges() != "") {
  75. base_error = "Text was found that does not belong to a valid section: " + line;
  76. break;
  77. }
  78. if (stage != RD::SHADER_STAGE_MAX) {
  79. if (line.strip_edges().begins_with("#include")) {
  80. if (p_include_func) {
  81. //process include
  82. String include = line.replace("#include", "").strip_edges();
  83. if (!include.begins_with("\"") || !include.ends_with("\"")) {
  84. base_error = "Malformed #include syntax, expected #include \"<path>\", found instad: " + include;
  85. break;
  86. }
  87. include = include.substr(1, include.length() - 2).strip_edges();
  88. String include_text = p_include_func(include, p_include_func_userdata);
  89. if (include_text != String()) {
  90. stage_code[stage] += "\n" + include_text + "\n";
  91. } else {
  92. base_error = "#include failed for file '" + include + "'";
  93. }
  94. } else {
  95. base_error = "#include used, but no include function provided.";
  96. }
  97. } else {
  98. stage_code[stage] += line + "\n";
  99. }
  100. }
  101. }
  102. }
  103. Ref<RDShaderFile> shader_file;
  104. shader_file.instance();
  105. if (base_error == "") {
  106. if (stage_found[RD::SHADER_STAGE_COMPUTE] && stages_found > 1) {
  107. ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "When writing compute shaders, [compute] mustbe the only stage present.");
  108. }
  109. if (version_texts.empty()) {
  110. version_texts[""] = ""; //make sure a default version exists
  111. }
  112. bool errors_found = false;
  113. /* STEP 2, Compile the versions, add to shader file */
  114. for (Map<StringName, String>::Element *E = version_texts.front(); E; E = E->next()) {
  115. Ref<RDShaderBytecode> bytecode;
  116. bytecode.instance();
  117. for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
  118. String code = stage_code[i];
  119. if (code == String()) {
  120. continue;
  121. }
  122. code = code.replace("VERSION_DEFINES", E->get());
  123. String error;
  124. Vector<uint8_t> spirv = RenderingDevice::get_singleton()->shader_compile_from_source(RD::ShaderStage(i), code, RD::SHADER_LANGUAGE_GLSL, &error, false);
  125. bytecode->set_stage_bytecode(RD::ShaderStage(i), spirv);
  126. if (error != "") {
  127. error += String() + "\n\nStage '" + stage_str[i] + "' source code: \n\n";
  128. Vector<String> sclines = code.split("\n");
  129. for (int j = 0; j < sclines.size(); j++) {
  130. error += itos(j + 1) + "\t\t" + sclines[j] + "\n";
  131. }
  132. errors_found = true;
  133. }
  134. bytecode->set_stage_compile_error(RD::ShaderStage(i), error);
  135. }
  136. set_bytecode(bytecode, E->key());
  137. }
  138. return errors_found ? ERR_PARSE_ERROR : OK;
  139. } else {
  140. return ERR_PARSE_ERROR;
  141. }
  142. }