test_shader_lang.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/rendering/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().precision);
  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().precision);
  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 j = 0; j < fnode->arguments.size(); j++) {
  121. if (j > 0)
  122. header += ", ";
  123. header += _prestr(fnode->arguments[j].precision) + _typestr(fnode->arguments[j].type) + " " + fnode->arguments[j].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_STRUCT: {
  132. } break;
  133. case SL::Node::TYPE_FUNCTION: {
  134. } break;
  135. case SL::Node::TYPE_BLOCK: {
  136. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  137. //variables
  138. code += _mktab(p_level - 1) + "{\n";
  139. for (Map<StringName, SL::BlockNode::Variable>::Element *E = bnode->variables.front(); E; E = E->next()) {
  140. code += _mktab(p_level) + _prestr(E->get().precision) + _typestr(E->get().type) + " " + E->key() + ";\n";
  141. }
  142. for (int i = 0; i < bnode->statements.size(); i++) {
  143. String scode = dump_node_code(bnode->statements[i], p_level);
  144. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) {
  145. code += scode; //use directly
  146. } else {
  147. code += _mktab(p_level) + scode + ";\n";
  148. }
  149. }
  150. code += _mktab(p_level - 1) + "}\n";
  151. } break;
  152. case SL::Node::TYPE_VARIABLE: {
  153. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  154. code = vnode->name;
  155. } break;
  156. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  157. // FIXME: Implement
  158. } break;
  159. case SL::Node::TYPE_ARRAY: {
  160. SL::ArrayNode *vnode = (SL::ArrayNode *)p_node;
  161. code = vnode->name;
  162. } break;
  163. case SL::Node::TYPE_ARRAY_DECLARATION: {
  164. // FIXME: Implement
  165. } break;
  166. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  167. // FIXME: Implement
  168. } break;
  169. case SL::Node::TYPE_CONSTANT: {
  170. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  171. return get_constant_text(cnode->datatype, cnode->values);
  172. } break;
  173. case SL::Node::TYPE_OPERATOR: {
  174. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  175. switch (onode->op) {
  176. case SL::OP_ASSIGN:
  177. case SL::OP_ASSIGN_ADD:
  178. case SL::OP_ASSIGN_SUB:
  179. case SL::OP_ASSIGN_MUL:
  180. case SL::OP_ASSIGN_DIV:
  181. case SL::OP_ASSIGN_SHIFT_LEFT:
  182. case SL::OP_ASSIGN_SHIFT_RIGHT:
  183. case SL::OP_ASSIGN_MOD:
  184. case SL::OP_ASSIGN_BIT_AND:
  185. case SL::OP_ASSIGN_BIT_OR:
  186. case SL::OP_ASSIGN_BIT_XOR:
  187. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level);
  188. break;
  189. case SL::OP_BIT_INVERT:
  190. case SL::OP_NEGATE:
  191. case SL::OP_NOT:
  192. case SL::OP_DECREMENT:
  193. case SL::OP_INCREMENT:
  194. code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level);
  195. break;
  196. case SL::OP_POST_DECREMENT:
  197. case SL::OP_POST_INCREMENT:
  198. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op);
  199. break;
  200. case SL::OP_CALL:
  201. case SL::OP_CONSTRUCT:
  202. code = dump_node_code(onode->arguments[0], p_level) + "(";
  203. for (int i = 1; i < onode->arguments.size(); i++) {
  204. if (i > 1)
  205. code += ", ";
  206. code += dump_node_code(onode->arguments[i], p_level);
  207. }
  208. code += ")";
  209. break;
  210. default: {
  211. code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")";
  212. break;
  213. }
  214. }
  215. } break;
  216. case SL::Node::TYPE_CONTROL_FLOW: {
  217. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  218. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  219. code += _mktab(p_level) + "if (" + dump_node_code(cfnode->expressions[0], p_level) + ")\n";
  220. code += dump_node_code(cfnode->blocks[0], p_level + 1);
  221. if (cfnode->blocks.size() == 2) {
  222. code += _mktab(p_level) + "else\n";
  223. code += dump_node_code(cfnode->blocks[1], p_level + 1);
  224. }
  225. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  226. if (cfnode->blocks.size()) {
  227. code = "return " + dump_node_code(cfnode->blocks[0], p_level);
  228. } else {
  229. code = "return";
  230. }
  231. }
  232. } break;
  233. case SL::Node::TYPE_MEMBER: {
  234. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  235. code = dump_node_code(mnode->owner, p_level) + "." + mnode->name;
  236. } break;
  237. }
  238. return code;
  239. }
  240. static Error recreate_code(void *p_str, SL::ShaderNode *p_program) {
  241. String *str = (String *)p_str;
  242. *str = dump_node_code(p_program, 0);
  243. return OK;
  244. }
  245. MainLoop *test() {
  246. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  247. if (cmdlargs.empty()) {
  248. //try editor!
  249. print_line("usage: godot -test shader_lang <shader>");
  250. return nullptr;
  251. }
  252. String test = cmdlargs.back()->get();
  253. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  254. if (!fa) {
  255. ERR_FAIL_V(nullptr);
  256. }
  257. String code;
  258. while (true) {
  259. CharType c = fa->get_8();
  260. if (fa->eof_reached())
  261. break;
  262. code += c;
  263. }
  264. SL sl;
  265. print_line("tokens:\n\n" + sl.token_debug(code));
  266. Map<StringName, SL::FunctionInfo> dt;
  267. dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3;
  268. dt["fragment"].can_discard = true;
  269. Vector<StringName> rm;
  270. rm.push_back("popo");
  271. Set<String> types;
  272. types.insert("spatial");
  273. Error err = sl.compile(code, dt, rm, types, NULL);
  274. if (err) {
  275. print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text());
  276. return nullptr;
  277. } else {
  278. String code2;
  279. recreate_code(&code2, sl.get_shader());
  280. print_line("code:\n\n" + code2);
  281. }
  282. return nullptr;
  283. }
  284. } // namespace TestShaderLang