test_shader_preprocessor.h 11 KB

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