summaryrefslogtreecommitdiff
path: root/servers/rendering/rendering_device_binds.cpp
blob: 111755eba3646326ba4e0f5c758c834387bd5b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "rendering_device_binds.h"

Error RDShaderFile::parse_versions_from_text(const String &p_text, OpenIncludeFunction p_include_func, void *p_include_func_userdata) {

	Vector<String> lines = p_text.split("\n");

	bool reading_versions = false;
	bool stage_found[RD::SHADER_STAGE_MAX] = { false, false, false, false, false };
	RD::ShaderStage stage = RD::SHADER_STAGE_MAX;
	static const char *stage_str[RD::SHADER_STAGE_MAX] = {
		"vertex",
		"fragment",
		"tesselation_control",
		"tesselation_evaluation",
		"compute"
	};
	String stage_code[RD::SHADER_STAGE_MAX];
	int stages_found = 0;
	Map<StringName, String> version_texts;

	versions.clear();
	base_error = "";

	for (int lidx = 0; lidx < lines.size(); lidx++) {
		String line = lines[lidx];

		{
			String ls = line.strip_edges();
			if (ls.begins_with("[") && ls.ends_with("]")) {
				String section = ls.substr(1, ls.length() - 2).strip_edges();
				if (section == "versions") {
					if (stages_found) {
						base_error = "Invalid shader file, [version] must be the first section found.";
						break;
					}
					reading_versions = true;
				} else {
					for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
						if (section == stage_str[i]) {
							if (stage_found[i]) {
								base_error = "Invalid shader file, stage appears twice: " + section;
								break;
							}

							stage_found[i] = true;
							stages_found++;

							stage = RD::ShaderStage(i);
							reading_versions = false;
							break;
						}
					}

					if (base_error != String()) {
						break;
					}
				}

				continue;
			}
		}

		if (reading_versions) {
			String l = line.strip_edges();
			if (l != "") {
				int eqpos = l.find("=");
				if (eqpos == -1) {
					base_error = "Version syntax is version=\"<defines with C escaping>\".";
					break;
				}
				String version = l.get_slice("=", 0).strip_edges();
				if (!version.is_valid_identifier()) {
					base_error = "Version names must be valid identifiers, found '" + version + "' instead.";
					break;
				}
				String define = l.get_slice("=", 1).strip_edges();
				if (!define.begins_with("\"") || !define.ends_with("\"")) {
					base_error = "Version text must be quoted using \"\", instead found '" + define + "'.";
					break;
				}
				define = "\n" + define.substr(1, define.length() - 2).c_unescape() + "\n"; //add newline before and after jsut in case

				version_texts[version] = define;
			}
		} else {
			if (stage == RD::SHADER_STAGE_MAX && line.strip_edges() != "") {
				base_error = "Text was found that does not belong to a valid section: " + line;
				break;
			}

			if (stage != RD::SHADER_STAGE_MAX) {
				if (line.strip_edges().begins_with("#include")) {
					if (p_include_func) {
						//process include
						String include = line.replace("#include", "").strip_edges();
						if (!include.begins_with("\"") || !include.ends_with("\"")) {
							base_error = "Malformed #include syntax, expected #include \"<path>\", found instad: " + include;
							break;
						}
						include = include.substr(1, include.length() - 2).strip_edges();
						String include_text = p_include_func(include, p_include_func_userdata);
						if (include_text != String()) {
							stage_code[stage] += "\n" + include_text + "\n";
						} else {
							base_error = "#include failed for file '" + include + "'";
						}
					} else {
						base_error = "#include used, but no include function provided.";
					}
				} else {

					stage_code[stage] += line + "\n";
				}
			}
		}
	}

	Ref<RDShaderFile> shader_file;
	shader_file.instance();

	if (base_error == "") {

		if (stage_found[RD::SHADER_STAGE_COMPUTE] && stages_found > 1) {
			ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "When writing compute shaders, [compute] mustbe the only stage present.");
		}

		if (version_texts.empty()) {
			version_texts[""] = ""; //make sure a default version exists
		}

		bool errors_found = false;

		/* STEP 2, Compile the versions, add to shader file */

		for (Map<StringName, String>::Element *E = version_texts.front(); E; E = E->next()) {

			Ref<RDShaderBytecode> bytecode;
			bytecode.instance();

			for (int i = 0; i < RD::SHADER_STAGE_MAX; i++) {
				String code = stage_code[i];
				if (code == String()) {
					continue;
				}
				code = code.replace("VERSION_DEFINES", E->get());
				String error;
				Vector<uint8_t> spirv = RenderingDevice::get_singleton()->shader_compile_from_source(RD::ShaderStage(i), code, RD::SHADER_LANGUAGE_GLSL, &error, false);
				bytecode->set_stage_bytecode(RD::ShaderStage(i), spirv);
				if (error != "") {
					error += String() + "\n\nStage '" + stage_str[i] + "' source code: \n\n";
					Vector<String> sclines = code.split("\n");
					for (int j = 0; j < sclines.size(); j++) {
						error += itos(j + 1) + "\t\t" + sclines[j] + "\n";
					}
					errors_found = true;
				}
				bytecode->set_stage_compile_error(RD::ShaderStage(i), error);
			}

			set_bytecode(bytecode, E->key());
		}

		return errors_found ? ERR_PARSE_ERROR : OK;
	} else {
		return ERR_PARSE_ERROR;
	}
}