2
0

test_shader_lang.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/io/file_access.h"
  32. #include "core/os/main_loop.h"
  33. #include "core/os/os.h"
  34. #include "core/string/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:
  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 (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
  110. String ucode = "uniform ";
  111. ucode += _prestr(E.value.precision);
  112. ucode += _typestr(E.value.type);
  113. ucode += " " + String(E.key);
  114. if (E.value.array_size > 0) {
  115. ucode += "[";
  116. ucode += itos(E.value.array_size);
  117. ucode += "]";
  118. } else {
  119. if (E.value.default_value.size()) {
  120. ucode += " = " + get_constant_text(E.value.type, E.value.default_value);
  121. }
  122. static const char *hint_name[SL::ShaderNode::Uniform::HINT_MAX] = {
  123. "",
  124. "color",
  125. "range",
  126. "albedo",
  127. "normal",
  128. "black",
  129. "white"
  130. };
  131. if (E.value.hint) {
  132. ucode += " : " + String(hint_name[E.value.hint]);
  133. }
  134. }
  135. code += ucode + "\n";
  136. }
  137. for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
  138. String vcode = "varying ";
  139. vcode += _prestr(E.value.precision);
  140. vcode += _typestr(E.value.type);
  141. vcode += " " + String(E.key);
  142. code += vcode + "\n";
  143. }
  144. for (int i = 0; i < pnode->functions.size(); i++) {
  145. SL::FunctionNode *fnode = pnode->functions[i].function;
  146. String header;
  147. header = _typestr(fnode->return_type) + " " + fnode->name + "(";
  148. for (int j = 0; j < fnode->arguments.size(); j++) {
  149. if (j > 0) {
  150. header += ", ";
  151. }
  152. header += _prestr(fnode->arguments[j].precision) + _typestr(fnode->arguments[j].type) + " " + fnode->arguments[j].name;
  153. }
  154. header += ")\n";
  155. code += header;
  156. code += dump_node_code(fnode->body, p_level + 1);
  157. }
  158. //code+=dump_node_code(pnode->body,p_level);
  159. } break;
  160. case SL::Node::TYPE_STRUCT: {
  161. } break;
  162. case SL::Node::TYPE_FUNCTION: {
  163. } break;
  164. case SL::Node::TYPE_BLOCK: {
  165. SL::BlockNode *bnode = (SL::BlockNode *)p_node;
  166. //variables
  167. code += _mktab(p_level - 1) + "{\n";
  168. for (const KeyValue<StringName, SL::BlockNode::Variable> &E : bnode->variables) {
  169. code += _mktab(p_level) + _prestr(E.value.precision) + _typestr(E.value.type) + " " + E.key + ";\n";
  170. }
  171. for (int i = 0; i < bnode->statements.size(); i++) {
  172. String scode = dump_node_code(bnode->statements[i], p_level);
  173. if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) {
  174. code += scode; //use directly
  175. } else {
  176. code += _mktab(p_level) + scode + ";\n";
  177. }
  178. }
  179. code += _mktab(p_level - 1) + "}\n";
  180. } break;
  181. case SL::Node::TYPE_VARIABLE: {
  182. SL::VariableNode *vnode = (SL::VariableNode *)p_node;
  183. code = vnode->name;
  184. } break;
  185. case SL::Node::TYPE_VARIABLE_DECLARATION: {
  186. // FIXME: Implement
  187. } break;
  188. case SL::Node::TYPE_ARRAY: {
  189. SL::ArrayNode *vnode = (SL::ArrayNode *)p_node;
  190. code = vnode->name;
  191. } break;
  192. case SL::Node::TYPE_ARRAY_DECLARATION: {
  193. // FIXME: Implement
  194. } break;
  195. case SL::Node::TYPE_ARRAY_CONSTRUCT: {
  196. // FIXME: Implement
  197. } break;
  198. case SL::Node::TYPE_CONSTANT: {
  199. SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
  200. return get_constant_text(cnode->datatype, cnode->values);
  201. } break;
  202. case SL::Node::TYPE_OPERATOR: {
  203. SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
  204. switch (onode->op) {
  205. case SL::OP_ASSIGN:
  206. case SL::OP_ASSIGN_ADD:
  207. case SL::OP_ASSIGN_SUB:
  208. case SL::OP_ASSIGN_MUL:
  209. case SL::OP_ASSIGN_DIV:
  210. case SL::OP_ASSIGN_SHIFT_LEFT:
  211. case SL::OP_ASSIGN_SHIFT_RIGHT:
  212. case SL::OP_ASSIGN_MOD:
  213. case SL::OP_ASSIGN_BIT_AND:
  214. case SL::OP_ASSIGN_BIT_OR:
  215. case SL::OP_ASSIGN_BIT_XOR:
  216. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level);
  217. break;
  218. case SL::OP_BIT_INVERT:
  219. case SL::OP_NEGATE:
  220. case SL::OP_NOT:
  221. case SL::OP_DECREMENT:
  222. case SL::OP_INCREMENT:
  223. code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level);
  224. break;
  225. case SL::OP_POST_DECREMENT:
  226. case SL::OP_POST_INCREMENT:
  227. code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op);
  228. break;
  229. case SL::OP_CALL:
  230. case SL::OP_CONSTRUCT:
  231. code = dump_node_code(onode->arguments[0], p_level) + "(";
  232. for (int i = 1; i < onode->arguments.size(); i++) {
  233. if (i > 1) {
  234. code += ", ";
  235. }
  236. code += dump_node_code(onode->arguments[i], p_level);
  237. }
  238. code += ")";
  239. break;
  240. default: {
  241. code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")";
  242. break;
  243. }
  244. }
  245. } break;
  246. case SL::Node::TYPE_CONTROL_FLOW: {
  247. SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
  248. if (cfnode->flow_op == SL::FLOW_OP_IF) {
  249. code += _mktab(p_level) + "if (" + dump_node_code(cfnode->expressions[0], p_level) + ")\n";
  250. code += dump_node_code(cfnode->blocks[0], p_level + 1);
  251. if (cfnode->blocks.size() == 2) {
  252. code += _mktab(p_level) + "else\n";
  253. code += dump_node_code(cfnode->blocks[1], p_level + 1);
  254. }
  255. } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
  256. if (cfnode->blocks.size()) {
  257. code = "return " + dump_node_code(cfnode->blocks[0], p_level);
  258. } else {
  259. code = "return";
  260. }
  261. }
  262. } break;
  263. case SL::Node::TYPE_MEMBER: {
  264. SL::MemberNode *mnode = (SL::MemberNode *)p_node;
  265. code = dump_node_code(mnode->owner, p_level) + "." + mnode->name;
  266. } break;
  267. }
  268. return code;
  269. }
  270. static Error recreate_code(void *p_str, SL::ShaderNode *p_program) {
  271. String *str = (String *)p_str;
  272. *str = dump_node_code(p_program, 0);
  273. return OK;
  274. }
  275. MainLoop *test() {
  276. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  277. if (cmdlargs.is_empty()) {
  278. //try editor!
  279. print_line("usage: godot -test shader_lang <shader>");
  280. return nullptr;
  281. }
  282. String test = cmdlargs.back()->get();
  283. FileAccess *fa = FileAccess::open(test, FileAccess::READ);
  284. if (!fa) {
  285. ERR_FAIL_V(nullptr);
  286. }
  287. String code;
  288. while (true) {
  289. char32_t c = fa->get_8();
  290. if (fa->eof_reached()) {
  291. break;
  292. }
  293. code += c;
  294. }
  295. SL sl;
  296. print_line("tokens:\n\n" + sl.token_debug(code));
  297. Map<StringName, SL::FunctionInfo> dt;
  298. dt["fragment"].built_ins["ALBEDO"] = SL::TYPE_VEC3;
  299. dt["fragment"].can_discard = true;
  300. Vector<StringName> rm;
  301. rm.push_back("popo");
  302. Set<String> types;
  303. types.insert("spatial");
  304. Error err = sl.compile(code, dt, rm, ShaderLanguage::VaryingFunctionNames(), types, nullptr);
  305. if (err) {
  306. print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text());
  307. return nullptr;
  308. } else {
  309. String code2;
  310. recreate_code(&code2, sl.get_shader());
  311. print_line("code:\n\n" + code2);
  312. }
  313. return nullptr;
  314. }
  315. } // namespace TestShaderLang