Debian Codegraph @

servers/rendering/rendering_device_binds.cpp

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