Debian Codegraph @

tests/servers/rendering/test_shader_preprocessor.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_shader_preprocessor.h */
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 #pragma once
32
33 #include "servers/rendering/shader_preprocessor.h"
34
35 #include "tests/test_macros.h"
36
37 #include <cctype>
38
39 namespace TestShaderPreprocessor {
40
41 void erase_all_empty(Vector<String> &p_vec) {
42 int idx = p_vec.find(" ");
43 while (idx >= 0) {
44 p_vec.remove_at(idx);
45 idx = p_vec.find(" ");
46 }
47 }
48
49 bool is_variable_char(unsigned char c) {
50 return std::isalnum(c) || c == '_';
51 }
52
53 bool is_operator_char(unsigned char c) {
54 return (c == '*') || (c == '+') || (c == '-') || (c == '/') || ((c >= '<') && (c <= '>'));
55 }
56
57 // Remove unnecessary spaces from a line.
58 String remove_spaces(String &p_str) {
59 String res;
60 // Result is guaranteed to not be longer than the input.
61 res.resize_uninitialized(p_str.size());
62 int wp = 0;
63 char32_t last = 0;
64 bool has_removed = false;
65
66 for (int n = 0; n < p_str.size(); n++) {
67 // These test cases only use ASCII.
68 unsigned char c = static_cast<unsigned char>(p_str[n]);
69 if (std::isblank(c)) {
70 has_removed = true;
71 } else {
72 if (has_removed) {
73 // Insert a space to avoid joining things that could potentially form a new token.
74 // E.g. "float x" or "- -".
75 if ((is_variable_char(c) && is_variable_char(last)) ||
76 (is_operator_char(c) && is_operator_char(last))) {
77 res[wp++] = ' ';
78 }
79 has_removed = false;
80 }
81 res[wp++] = c;
82 last = c;
83 }
84 }
85 res.resize_uninitialized(wp);
86 return res;
87 }
88
89 // The pre-processor changes indentation and inserts spaces when inserting macros.
90 // Re-format the code, without changing its meaning, to make it easier to compare.
91 String compact_spaces(String &p_str) {
92 Vector<String> lines = p_str.split("\n", false);
93 erase_all_empty(lines);
94 for (String &line : lines) {
95 line = remove_spaces(line);
96 }
97 return String("\n").join(lines);
98 }
99
100 #define CHECK_SHADER_EQ(a, b) CHECK_EQ(compact_spaces(a), compact_spaces(b))
101 #define CHECK_SHADER_NE(a, b) CHECK_NE(compact_spaces(a), compact_spaces(b))
102
103 TEST_CASE("[ShaderPreprocessor] Simple defines") {
104 String code(
105 "#define X 1.0 // comment\n"
106 "#define Y mix\n"
107 "#define Z X\n"
108 "\n"
109 "#define func0 \\\n"
110 " vec3 my_fun(vec3 arg) {\\\n"
111 " return pow(arg, 2.2);\\\n"
112 " }\n"
113 "\n"
114 "func0\n"
115 "\n"
116 "fragment() {\n"
117 " ALBEDO = vec3(X);\n"
118 " float x = Y(0., Z, X);\n"
119 " #undef X\n"
120 " float X = x;\n"
121 " x = -Z;\n"
122 "}\n");
123 String expected(
124 "vec3 my_fun(vec3 arg) { return pow(arg, 2.2); }\n"
125 "\n"
126 "fragment() {\n"
127 " ALBEDO = vec3( 1.0 );\n"
128 " float x = mix(0., 1.0 , 1.0 );\n"
129 " float X = x;\n"
130 " x = -X;\n"
131 "}\n");
132 String result;
133
134 ShaderPreprocessor preprocessor;
135 CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
136
137 CHECK_SHADER_EQ(result, expected);
138 }
139
140 TEST_CASE("[ShaderPreprocessor] Avoid merging adjacent tokens") {
141 String code(
142 "#define X -10\n"
143 "#define Y(s) s\n"
144 "\n"
145 "fragment() {\n"
146 " float v = 1.0-X-Y(-2);\n"
147 "}\n");
148 String expected(
149 "fragment() {\n"
150 " float v = 1.0 - -10 - -2;\n"
151 "}\n");
152 String result;
153
154 ShaderPreprocessor preprocessor;
155 CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
156
157 CHECK_SHADER_EQ(result, expected);
158 }
159
160 TEST_CASE("[ShaderPreprocessor] Complex defines") {
161 String code(
162 "const float X = 2.0;\n"
163 "#define A(X) X*2.\n"
164 "#define X 1.0\n"
165 "#define Y Z(X, W)\n"
166 "#define Z max\n"
167 "#define C(X, Y) Z(A(Y), B(X))\n"
168 "#define W -X\n"
169 "#define B(X) X*3.\n"
170 "\n"
171 "fragment() {\n"
172 " float x = Y;\n"
173 " float y = C(5., 7.0);\n"
174 "}\n");
175 String expected(
176 "const float X = 2.0;\n"
177 "fragment() {\n"
178 " float x = max(1.0, - 1.0);\n"
179 " float y = max(7.0*2. , 5.*3.);\n"
180 "}\n");
181 String result;
182
183 ShaderPreprocessor preprocessor;
184 CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
185
186 CHECK_SHADER_EQ(result, expected);
187 }
188
189 TEST_CASE("[ShaderPreprocessor] Concatenation") {
190 String code(
191 "fragment() {\n"
192 " #define X 1 // this is fine ##\n"
193 " #define y 2\n"
194 " #define z 3##.## 1## 4 ## 59\n"
195 " #define Z(y) X ## y\n"
196 " #define Z2(y) y##X\n"
197 " #define W(y) X, y\n"
198 " #define A(x) fl## oat a = 1##x ##.3 ## x\n"
199 " #define C(x, y) x##.##y\n"
200 " #define J(x) x##=\n"
201 " float Z(y) = 1.2;\n"
202 " float Z(z) = 2.3;\n"
203 " float Z2(y) = z;\n"
204 " float Z2(z) = 2.3;\n"
205 " int b = max(W(3));\n"
206 " Xy J(+) b J(=) 3 ? 0.1 : 0.2;\n"
207 " A(9);\n"
208 " Xy = C(X, y);\n"
209 "}\n");
210 String expected(
211 "fragment() {\n"
212 " float Xy = 1.2;\n"
213 " float Xz = 2.3;\n"
214 " float yX = 3.1459;\n"
215 " float zX = 2.3;\n"
216 " int b = max(1, 3);\n"
217 " Xy += b == 3 ? 0.1 : 0.2;\n"
218 " float a = 19.39;\n"
219 " Xy = 1.2;\n"
220 "}\n");
221 String result;
222
223 ShaderPreprocessor preprocessor;
224 CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
225
226 CHECK_SHADER_EQ(result, expected);
227 }
228
229 TEST_CASE("[ShaderPreprocessor] Nested concatenation") {
230 // Concatenation ## should not expand adjacent tokens if they are macros,
231 // but this is currently not implemented in Godot's shader preprocessor.
232 // To force expanding, an extra macro should be required (B in this case).
233
234 String code(
235 "fragment() {\n"
236 " vec2 X = vec2(0);\n"
237 " #define X 1\n"
238 " #define y 2\n"
239 " #define B(x, y) C(x, y)\n"
240 " #define C(x, y) x##.##y\n"
241 " C(X, y) = B(X, y);\n"
242 "}\n");
243 String expected(
244 "fragment() {\n"
245 " vec2 X = vec2(0);\n"
246 " X.y = 1.2;\n"
247 "}\n");
248 String result;
249
250 ShaderPreprocessor preprocessor;
251 CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
252
253 // TODO: Reverse the check when/if this is changed.
254 CHECK_SHADER_NE(result, expected);
255 }
256
257 TEST_CASE("[ShaderPreprocessor] Concatenation sorting network") {
258 String code(
259 "fragment() {\n"
260 " #define ARR(X) test##X\n"
261 " #define ACMP(a, b) ARR(a) > ARR(b)\n"
262 " #define ASWAP(a, b) tmp = ARR(b); ARR(b) = ARR(a); ARR(a) = tmp;\n"
263 " #define ACSWAP(a, b) if(ACMP(a, b)) { ASWAP(a, b) }\n"
264 " float test0 = 1.2;\n"
265 " float test1 = 0.34;\n"
266 " float test3 = 0.8;\n"
267 " float test4 = 2.9;\n"
268 " float tmp;\n"
269 " ACSWAP(0,2)\n"
270 " ACSWAP(1,3)\n"
271 " ACSWAP(0,1)\n"
272 " ACSWAP(2,3)\n"
273 " ACSWAP(1,2)\n"
274 "}\n");
275 String expected(
276 "fragment() {\n"
277 " float test0 = 1.2;\n"
278 " float test1 = 0.34;\n"
279 " float test3 = 0.8;\n"
280 " float test4 = 2.9;\n"
281 " float tmp;\n"
282 " if(test0 > test2) { tmp = test2; test2 = test0; test0 = tmp; }\n"
283 " if(test1 > test3) { tmp = test3; test3 = test1; test1 = tmp; }\n"
284 " if(test0 > test1) { tmp = test1; test1 = test0; test0 = tmp; }\n"
285 " if(test2 > test3) { tmp = test3; test3 = test2; test2 = tmp; }\n"
286 " if(test1 > test2) { tmp = test2; test2 = test1; test1 = tmp; }\n"
287 "}\n");
288 String result;
289
290 ShaderPreprocessor preprocessor;
291 CHECK_EQ(preprocessor.preprocess(code, String("file.gdshader"), result), Error::OK);
292
293 CHECK_SHADER_EQ(result, expected);
294 }
295
296 TEST_CASE("[ShaderPreprocessor] Undefined behavior") {
297 // None of these are valid concatenation, nor valid shader code.
298 // Don't care about results, just make sure there's no crash.
299 const String filename("somefile.gdshader");
300 String result;
301 ShaderPreprocessor preprocessor;
302
303 preprocessor.preprocess("#define X ###\nX\n", filename, result);
304 preprocessor.preprocess("#define X ####\nX\n", filename, result);
305 preprocessor.preprocess("#define X #####\nX\n", filename, result);
306 preprocessor.preprocess("#define X 1 ### 2\nX\n", filename, result);
307 preprocessor.preprocess("#define X 1 #### 2\nX\n", filename, result);
308 preprocessor.preprocess("#define X 1 ##### 2\nX\n", filename, result);
309 preprocessor.preprocess("#define X ### 2\nX\n", filename, result);
310 preprocessor.preprocess("#define X #### 2\nX\n", filename, result);
311 preprocessor.preprocess("#define X ##### 2\nX\n", filename, result);
312 preprocessor.preprocess("#define X 1 ###\nX\n", filename, result);
313 preprocessor.preprocess("#define X 1 ####\nX\n", filename, result);
314 preprocessor.preprocess("#define X 1 #####\nX\n", filename, result);
315 }
316
317 TEST_CASE("[ShaderPreprocessor] Invalid concatenations") {
318 const String filename("somefile.gdshader");
319 String result;
320 ShaderPreprocessor preprocessor;
321
322 CHECK_NE(preprocessor.preprocess("#define X ##", filename, result), Error::OK);
323 CHECK_NE(preprocessor.preprocess("#define X 1 ##", filename, result), Error::OK);
324 CHECK_NE(preprocessor.preprocess("#define X ## 1", filename, result), Error::OK);
325 CHECK_NE(preprocessor.preprocess("#define X(y) ## ", filename, result), Error::OK);
326 CHECK_NE(preprocessor.preprocess("#define X(y) y ## ", filename, result), Error::OK);
327 CHECK_NE(preprocessor.preprocess("#define X(y) ## y", filename, result), Error::OK);
328 }
329
330 } // namespace TestShaderPreprocessor