test_shader_lang.cpp 12 KB

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