test_shader_lang.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*************************************************************************/
  2. /* test_shader_lang.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "test_shader_lang.h"
  31. #include "core/os/main_loop.h"
  32. #include "core/os/os.h"
  33. #include "servers/rendering/shader_language.h"
  34. typedef ShaderLanguage SL;
  35. namespace TestShaderLang {
  36. static String _mktab(int p_level) {
  37. String tb;
  38. for (int i = 0; i < p_level; i++) {
  39. tb += "\t";
  40. }
  41. return tb;
  42. }
  43. static String _typestr(SL::DataType p_type) {
  44. return ShaderLanguage::get_datatype_name(p_type);
  45. }
  46. static String _prestr(SL::DataPrecision p_pres) {
  47. switch (p_pres) {
  48. case SL::PRECISION_LOWP:
  49. return "lowp ";
  50. case SL::PRECISION_MEDIUMP:
  51. return "mediump ";
  52. case SL::PRECISION_HIGHP:
  53. return "highp ";
  54. case SL::PRECISION_DEFAULT:
  55. return "";
  56. }
  57. return "";
  58. }
  59. static String _opstr(SL::Operator p_op) {
  60. return ShaderLanguage::get_operator_text(p_op);
  61. }
  62. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) {
  63. switch (p_type) {
  64. case SL::TYPE_BOOL:
  65. return p_values[0].boolean ? "true" : "false";
  66. case SL::TYPE_BVEC2:
  67. return String() + "bvec2(" + (p_values[0].boolean ? "true" : "false") + (p_values[1].boolean ? "true" : "false") + ")";
  68. case SL::TYPE_BVEC3:
  69. return String() + "bvec3(" + (p_values[0].boolean ? "true" : "false") + "," + (p_values[1].boolean ? "true" : "false") + "," + (p_values[2].boolean ? "true" : "false") + ")";
  70. case SL::TYPE_BVEC4:
  71. return String() + "bvec4(" + (p_values[0].boolean ? "true" : "false") + "," + (p_values[1].boolean ? "true" : "false") + "," + (p_values[2].boolean ? "true" : "false") + "," + (p_values[3].boolean ? "true" : "false") + ")";
  72. case SL::TYPE_INT:
  73. return rtos(p_values[0].sint);
  74. case SL::TYPE_IVEC2:
  75. return String() + "ivec2(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + ")";
  76. case SL::TYPE_IVEC3:
  77. return String() + "ivec3(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + ")";
  78. case SL::TYPE_IVEC4:
  79. return String() + "ivec4(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + "," + rtos(p_values[3].sint) + ")";
  80. case SL::TYPE_UINT:
  81. return rtos(p_values[0].real);
  82. case SL::TYPE_UVEC2:
  83. return String() + "uvec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")";
  84. case SL::TYPE_UVEC3:
  85. return String() + "uvec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")";
  86. case SL::TYPE_UVEC4:
  87. return String() + "uvec4(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + "," + rtos(p_values[3].real) + ")";
  88. case SL::TYPE_FLOAT:
  89. return rtos(p_values[0].real);
  90. case SL::TYPE_VEC2:
  91. return String() + "vec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")";
  92. case SL::TYPE_VEC3:
  93. return String() + "vec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")";
  94. case SL::TYPE_VEC4:
  95. return String() + "vec4(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + "," + rtos(p_values[3].real) + ")";
  96. default:
  97. ERR_FAIL_V(String());
  98. }
  99. }
  100. static String dump_node_code(SL::Node *p_node, int p_level) {
  101. String code;
  102. switch (p_node->type) {
  103. case SL::Node::TYPE_SHADER: {
  104. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  105. for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  106. String ucode = "uniform ";
  107. ucode += _prestr(E.value.precision);
  108. ucode += _typestr(E.value.type);
  109. ucode += " " + String(E.key);
  110. if (E.value.array_size > 0) {
  111. ucode += "[";
  112. ucode += itos(E.value.array_size);
  113. ucode += "]";
  114. } else {
  115. if (E.value.default_value.size()) {
  116. ucode += " = " + get_constant_text(E.value.type, E.value.default_value);
  117. }
  118. static const char *hint_name[SL::ShaderNode::Uniform::HINT_MAX] = {
  119. "",
  120. "color",
  121. "range",
  122. "albedo",
  123. "normal",
  124. "black",
  125. "white"
  126. };
  127. if (E.value.hint) {
  128. ucode += " : " + String(hint_name[E.value.hint]);
  129. }
  130. }
  131. code += ucode + "\n";
  132. }
  133. for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
  134. String vcode = "varying ";
  135. vcode += _prestr(E.value.precision);
  136. vcode += _typestr(E.value.type);
  137. vcode += " " + String(E.key);
  138. code += vcode + "\n";
  139. }
  140. for (int i = 0; i < pnode->functions.size(); i++) {
  141. SL::FunctionNode *fnode = pnode->functions[i].function;
  142. String header;
  143. header = _typestr(fnode->return_type) + " " + fnode->name + "(";
  144. for (int j = 0; j < fnode->arguments.size(); j++) {
  145. if (j > 0) {
  146. header += ", ";
  147. }
  148. header += _prestr(fnode->arguments[j].precision) + _typestr(fnode->arguments[j].type) + " " + fnode->arguments[j].name;
  149. }
  150. header += ")\n";
  151. code += header;
  152. code += dump_node_code(fnode->body, p_level + 1);
  153. }
  154. //code+=dump_node_code(pnode->body,p_level);
  155. } break;
  156. case SL::Node::TYPE_STRUCT: {
  157. } break;
  158. case SL::Node::TYPE_FUNCTION: {
  159. } break;
  160. case SL::Node::TYPE_BLOCK: {
  161. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  162. //variables
  163. code += _mktab(p_level - 1) + "{\n";
  164. for (const KeyValue<StringName, SL::BlockNode::Variable> &E : bnode->variables) {
  165. code += _mktab(p_level) + _prestr(E.value.precision) + _typestr(E.value.type) + " " + E.key + ";\n";
  166. }
  167. for (int i = 0; i < bnode->statements.size(); i++) {
  168. String scode = dump_node_code(bnode->statements[i], p_level);
  169. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) {
  170. code += scode; //use directly
  171. } else {
  172. code += _mktab(p_level) + scode + ";\n";
  173. }
  174. }
  175. code += _mktab(p_level - 1) + "}\n";
  176. } break;
  177. case SL::Node::TYPE_VARIABLE: {
  178. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  179. code = vnode->name;
  180. } break;
  181. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  182. // FIXME: Implement
  183. } break;
  184. case SL::Node::TYPE_ARRAY: {
  185. SL::ArrayNode *vnode = (SL::ArrayNode *)p_node;
  186. code = vnode->name;
  187. } break;
  188. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  189. // FIXME: Implement
  190. } break;
  191. case SL::Node::TYPE_CONSTANT: {
  192. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  193. return get_constant_text(cnode->datatype, cnode->values);
  194. } break;
  195. case SL::Node::TYPE_OPERATOR: {
  196. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  197. switch (onode->op) {
  198. case SL::OP_ASSIGN:
  199. case SL::OP_ASSIGN_ADD:
  200. case SL::OP_ASSIGN_SUB:
  201. case SL::OP_ASSIGN_MUL:
  202. case SL::OP_ASSIGN_DIV:
  203. case SL::OP_ASSIGN_SHIFT_LEFT:
  204. case SL::OP_ASSIGN_SHIFT_RIGHT:
  205. case SL::OP_ASSIGN_MOD:
  206. case SL::OP_ASSIGN_BIT_AND:
  207. case SL::OP_ASSIGN_BIT_OR:
  208. case SL::OP_ASSIGN_BIT_XOR:
  209. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level);
  210. break;
  211. case SL::OP_BIT_INVERT:
  212. case SL::OP_NEGATE:
  213. case SL::OP_NOT:
  214. case SL::OP_DECREMENT:
  215. case SL::OP_INCREMENT:
  216. code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level);
  217. break;
  218. case SL::OP_POST_DECREMENT:
  219. case SL::OP_POST_INCREMENT:
  220. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op);
  221. break;
  222. case SL::OP_CALL:
  223. case SL::OP_CONSTRUCT:
  224. code = dump_node_code(onode->arguments[0], p_level) + "(";
  225. for (int i = 1; i < onode->arguments.size(); i++) {
  226. if (i > 1) {
  227. code += ", ";
  228. }
  229. code += dump_node_code(onode->arguments[i], p_level);
  230. }
  231. code += ")";
  232. break;
  233. case SL::OP_EMPTY:
  234. break;
  235. default: {
  236. code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")";
  237. break;
  238. }
  239. }
  240. } break;
  241. case SL::Node::TYPE_CONTROL_FLOW: {
  242. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  243. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  244. code += _mktab(p_level) + "if (" + dump_node_code(cfnode->expressions[0], p_level) + ")\n";
  245. code += dump_node_code(cfnode->blocks[0], p_level + 1);
  246. if (cfnode->blocks.size() == 2) {
  247. code += _mktab(p_level) + "else\n";
  248. code += dump_node_code(cfnode->blocks[1], p_level + 1);
  249. }
  250. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  251. if (cfnode->blocks.size()) {
  252. code = "return " + dump_node_code(cfnode->blocks[0], p_level);
  253. } else {
  254. code = "return";
  255. }
  256. }
  257. } break;
  258. case SL::Node::TYPE_MEMBER: {
  259. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  260. code = dump_node_code(mnode->owner, p_level) + "." + mnode->name;
  261. } break;
  262. }
  263. return code;
  264. }
  265. static Error recreate_code(void *p_str, SL::ShaderNode *p_program) {
  266. String *str = (String *)p_str;
  267. *str = dump_node_code(p_program, 0);
  268. return OK;
  269. }
  270. MainLoop *test() {
  271. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  272. if (cmdlargs.is_empty()) {
  273. //try editor!
  274. print_line("usage: godot -test shader_lang <shader>");
  275. return nullptr;
  276. }
  277. String test = cmdlargs.back()->get();
  278. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  279. if (!fa) {
  280. ERR_FAIL_V(nullptr);
  281. }
  282. String code;
  283. while (true) {
  284. char32_t c = fa->get_8();
  285. if (fa->eof_reached()) {
  286. break;
  287. }
  288. code += c;
  289. }
  290. SL sl;
  291. print_line("tokens:\n\n" + sl.token_debug(code));
  292. Map<StringName, SL::FunctionInfo> dt;
  293. dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3;
  294. dt["fragment"].can_discard = true;
  295. Vector<SL::ModeInfo> rm;
  296. rm.push_back({ "popo" });
  297. Set<String> types;
  298. types.insert("spatial");
  299. ShaderLanguage::ShaderCompileInfo info;
  300. info.functions = dt;
  301. info.render_modes = rm;
  302. info.shader_types = types;
  303. Error err = sl.compile(code, info);
  304. if (err) {
  305. print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text());
  306. return nullptr;
  307. } else {
  308. String code2;
  309. recreate_code(&code2, sl.get_shader());
  310. print_line("code:\n\n" + code2);
  311. }
  312. return nullptr;
  313. }
  314. } // namespace TestShaderLang