test_shader_preprocessor.h 11 KB

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