test_shader_lang.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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: return "lowp ";
  53. case SL::PRECISION_MEDIUMP: return "mediump ";
  54. case SL::PRECISION_HIGHP: return "highp ";
  55. case SL::PRECISION_DEFAULT: 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: return p_values[0].boolean ? "true" : "false";
  65. case SL::TYPE_BVEC2: return String() + "bvec2(" + (p_values[0].boolean ? "true" : "false") + (p_values[1].boolean ? "true" : "false") + ")";
  66. case SL::TYPE_BVEC3: return String() + "bvec3(" + (p_values[0].boolean ? "true" : "false") + "," + (p_values[1].boolean ? "true" : "false") + "," + (p_values[2].boolean ? "true" : "false") + ")";
  67. case SL::TYPE_BVEC4: 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") + ")";
  68. case SL::TYPE_INT: return rtos(p_values[0].sint);
  69. case SL::TYPE_IVEC2: return String() + "ivec2(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + ")";
  70. case SL::TYPE_IVEC3: return String() + "ivec3(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + ")";
  71. case SL::TYPE_IVEC4: return String() + "ivec4(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + "," + rtos(p_values[3].sint) + ")";
  72. case SL::TYPE_UINT: return rtos(p_values[0].real);
  73. case SL::TYPE_UVEC2: return String() + "uvec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")";
  74. case SL::TYPE_UVEC3: return String() + "uvec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")";
  75. case SL::TYPE_UVEC4: return String() + "uvec4(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + "," + rtos(p_values[3].real) + ")";
  76. case SL::TYPE_FLOAT: return rtos(p_values[0].real);
  77. case SL::TYPE_VEC2: return String() + "vec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")";
  78. case SL::TYPE_VEC3: return String() + "vec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")";
  79. case SL::TYPE_VEC4: return String() + "vec4(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + "," + rtos(p_values[3].real) + ")";
  80. default: ERR_FAIL_V(String());
  81. }
  82. }
  83. static String dump_node_code(SL::Node *p_node, int p_level) {
  84. String code;
  85. switch (p_node->type) {
  86. case SL::Node::TYPE_SHADER: {
  87. SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
  88. for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
  89. String ucode = "uniform ";
  90. ucode += _prestr(E->get().precission);
  91. ucode += _typestr(E->get().type);
  92. ucode += " " + String(E->key());
  93. if (E->get().default_value.size()) {
  94. ucode += " = " + get_constant_text(E->get().type, E->get().default_value);
  95. }
  96. static const char *hint_name[SL::ShaderNode::Uniform::HINT_MAX] = {
  97. "",
  98. "color",
  99. "range",
  100. "albedo",
  101. "normal",
  102. "black",
  103. "white"
  104. };
  105. if (E->get().hint)
  106. ucode += " : " + String(hint_name[E->get().hint]);
  107. code += ucode + "\n";
  108. }
  109. for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) {
  110. String vcode = "varying ";
  111. vcode += _prestr(E->get().precission);
  112. vcode += _typestr(E->get().type);
  113. vcode += " " + String(E->key());
  114. code += vcode + "\n";
  115. }
  116. for (int i = 0; i < pnode->functions.size(); i++) {
  117. SL::FunctionNode *fnode = pnode->functions[i].function;
  118. String header;
  119. header = _typestr(fnode->return_type) + " " + fnode->name + "(";
  120. for (int i = 0; i < fnode->arguments.size(); i++) {
  121. if (i > 0)
  122. header += ", ";
  123. header += _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + fnode->arguments[i].name;
  124. }
  125. header += ")\n";
  126. code += header;
  127. code += dump_node_code(fnode->body, p_level + 1);
  128. }
  129. //code+=dump_node_code(pnode->body,p_level);
  130. } break;
  131. case SL::Node::TYPE_FUNCTION: {
  132. } break;
  133. case SL::Node::TYPE_BLOCK: {
  134. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  135. //variables
  136. code += _mktab(p_level - 1) + "{\n";
  137. for (Map<StringName, SL::BlockNode::Variable>::Element *E = bnode->variables.front(); E; E = E->next()) {
  138. code += _mktab(p_level) + _prestr(E->get().precision) + _typestr(E->get().type) + " " + E->key() + ";\n";
  139. }
  140. for (int i = 0; i < bnode->statements.size(); i++) {
  141. String scode = dump_node_code(bnode->statements[i], p_level);
  142. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) {
  143. code += scode; //use directly
  144. } else {
  145. code += _mktab(p_level) + scode + ";\n";
  146. }
  147. }
  148. code += _mktab(p_level - 1) + "}\n";
  149. } break;
  150. case SL::Node::TYPE_VARIABLE: {
  151. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  152. code = vnode->name;
  153. } break;
  154. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  155. // FIXME: Implement
  156. } break;
  157. case SL::Node::TYPE_CONSTANT: {
  158. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  159. return get_constant_text(cnode->datatype, cnode->values);
  160. } break;
  161. case SL::Node::TYPE_OPERATOR: {
  162. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  163. switch (onode->op) {
  164. case SL::OP_ASSIGN:
  165. case SL::OP_ASSIGN_ADD:
  166. case SL::OP_ASSIGN_SUB:
  167. case SL::OP_ASSIGN_MUL:
  168. case SL::OP_ASSIGN_DIV:
  169. case SL::OP_ASSIGN_SHIFT_LEFT:
  170. case SL::OP_ASSIGN_SHIFT_RIGHT:
  171. case SL::OP_ASSIGN_MOD:
  172. case SL::OP_ASSIGN_BIT_AND:
  173. case SL::OP_ASSIGN_BIT_OR:
  174. case SL::OP_ASSIGN_BIT_XOR:
  175. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level);
  176. break;
  177. case SL::OP_BIT_INVERT:
  178. case SL::OP_NEGATE:
  179. case SL::OP_NOT:
  180. case SL::OP_DECREMENT:
  181. case SL::OP_INCREMENT:
  182. code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level);
  183. break;
  184. case SL::OP_POST_DECREMENT:
  185. case SL::OP_POST_INCREMENT:
  186. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op);
  187. break;
  188. case SL::OP_CALL:
  189. case SL::OP_CONSTRUCT:
  190. code = dump_node_code(onode->arguments[0], p_level) + "(";
  191. for (int i = 1; i < onode->arguments.size(); i++) {
  192. if (i > 1)
  193. code += ", ";
  194. code += dump_node_code(onode->arguments[i], p_level);
  195. }
  196. code += ")";
  197. break;
  198. default: {
  199. code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")";
  200. break;
  201. }
  202. }
  203. } break;
  204. case SL::Node::TYPE_CONTROL_FLOW: {
  205. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  206. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  207. code += _mktab(p_level) + "if (" + dump_node_code(cfnode->expressions[0], p_level) + ")\n";
  208. code += dump_node_code(cfnode->blocks[0], p_level + 1);
  209. if (cfnode->blocks.size() == 2) {
  210. code += _mktab(p_level) + "else\n";
  211. code += dump_node_code(cfnode->blocks[1], p_level + 1);
  212. }
  213. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  214. if (cfnode->blocks.size()) {
  215. code = "return " + dump_node_code(cfnode->blocks[0], p_level);
  216. } else {
  217. code = "return";
  218. }
  219. }
  220. } break;
  221. case SL::Node::TYPE_MEMBER: {
  222. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  223. code = dump_node_code(mnode->owner, p_level) + "." + mnode->name;
  224. } break;
  225. }
  226. return code;
  227. }
  228. static Error recreate_code(void *p_str, SL::ShaderNode *p_program) {
  229. String *str = (String *)p_str;
  230. *str = dump_node_code(p_program, 0);
  231. return OK;
  232. }
  233. MainLoop *test() {
  234. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  235. if (cmdlargs.empty()) {
  236. //try editor!
  237. print_line("usage: godot -test shader_lang <shader>");
  238. return NULL;
  239. }
  240. String test = cmdlargs.back()->get();
  241. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  242. if (!fa) {
  243. ERR_FAIL_V(NULL);
  244. }
  245. String code;
  246. while (true) {
  247. CharType c = fa->get_8();
  248. if (fa->eof_reached())
  249. break;
  250. code += c;
  251. }
  252. SL sl;
  253. print_line("tokens:\n\n" + sl.token_debug(code));
  254. Map<StringName, SL::FunctionInfo> dt;
  255. dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3;
  256. dt["fragment"].can_discard = true;
  257. Vector<StringName> rm;
  258. rm.push_back("popo");
  259. Set<String> types;
  260. types.insert("spatial");
  261. Error err = sl.compile(code, dt, rm, types);
  262. if (err) {
  263. print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text());
  264. return NULL;
  265. } else {
  266. String code;
  267. recreate_code(&code, sl.get_shader());
  268. print_line("code:\n\n" + code);
  269. }
  270. return NULL;
  271. }
  272. } // namespace TestShaderLang