test_shader_lang.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*************************************************************************/
  2. /* test_shader_lang.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "test_shader_lang.h"
  30. #include "os/main_loop.h"
  31. #include "os/os.h"
  32. #include "os/file_access.h"
  33. #include "scene/gui/control.h"
  34. #include "scene/gui/text_edit.h"
  35. #include "print_string.h"
  36. #include "servers/visual/shader_language.h"
  37. //#include "drivers/gles2/shader_compiler_gles2.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. return "";
  50. }
  51. static String _prestr(SL::DataPrecision p_pres) {
  52. switch(p_pres) {
  53. case SL::PRECISION_LOWP: return "lowp ";
  54. case SL::PRECISION_MEDIUMP: return "mediump ";
  55. case SL::PRECISION_HIGHP: return "highp ";
  56. case SL::PRECISION_DEFAULT: return "";
  57. }
  58. return "";
  59. }
  60. static String _opstr(SL::Operator p_op) {
  61. return ShaderLanguage::get_operator_text(p_op);
  62. }
  63. static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value>& p_values) {
  64. switch(p_type) {
  65. case SL::TYPE_BOOL: return p_values[0].boolean?"true":"false";
  66. case SL::TYPE_BVEC2: return String()+"bvec2("+(p_values[0].boolean?"true":"false")+(p_values[1].boolean?"true":"false")+")";
  67. 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")+")";
  68. 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")+")";
  69. case SL::TYPE_INT: return rtos(p_values[0].sint);
  70. case SL::TYPE_IVEC2: return String()+"ivec2("+rtos(p_values[0].sint)+","+rtos(p_values[1].sint)+")";
  71. case SL::TYPE_IVEC3: return String()+"ivec3("+rtos(p_values[0].sint)+","+rtos(p_values[1].sint)+","+rtos(p_values[2].sint)+")";
  72. 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)+")";
  73. case SL::TYPE_UINT: return rtos(p_values[0].real);
  74. case SL::TYPE_UVEC2: return String()+"uvec2("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+")";
  75. case SL::TYPE_UVEC3: return String()+"uvec3("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+","+rtos(p_values[2].real)+")";
  76. 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)+")";
  77. case SL::TYPE_FLOAT: return rtos(p_values[0].real);
  78. case SL::TYPE_VEC2: return String()+"vec2("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+")";
  79. case SL::TYPE_VEC3: return String()+"vec3("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+","+rtos(p_values[2].real)+")";
  80. 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)+")";
  81. default: ERR_FAIL_V(String());
  82. }
  83. }
  84. static String dump_node_code(SL::Node *p_node,int p_level) {
  85. String code;
  86. switch(p_node->type) {
  87. case SL::Node::TYPE_SHADER: {
  88. SL::ShaderNode *pnode=(SL::ShaderNode*)p_node;
  89. for(Map<StringName,SL::ShaderNode::Uniform>::Element *E=pnode->uniforms.front();E;E=E->next()) {
  90. String ucode="uniform ";
  91. ucode+=_prestr(E->get().precission);
  92. ucode+=_typestr(E->get().type);
  93. ucode+=" "+String(E->key());
  94. if (E->get().default_value.size()) {
  95. ucode+=" = "+get_constant_text(E->get().type,E->get().default_value);
  96. }
  97. static const char*hint_name[SL::ShaderNode::Uniform::HINT_MAX]={
  98. "",
  99. "color",
  100. "range",
  101. "albedo",
  102. "normal",
  103. "black",
  104. "white"
  105. };
  106. if (E->get().hint)
  107. ucode+=" : "+String(hint_name[E->get().hint]);
  108. code+=ucode+"\n";
  109. }
  110. for(Map<StringName,SL::ShaderNode::Varying>::Element *E=pnode->varyings.front();E;E=E->next()) {
  111. String vcode="varying ";
  112. vcode+=_prestr(E->get().precission);
  113. vcode+=_typestr(E->get().type);
  114. vcode+=" "+String(E->key());
  115. code+=vcode+"\n";
  116. }
  117. for(int i=0;i<pnode->functions.size();i++) {
  118. SL::FunctionNode *fnode=pnode->functions[i].function;
  119. String header;
  120. header=_typestr(fnode->return_type)+" "+fnode->name+"(";
  121. for(int i=0;i<fnode->arguments.size();i++) {
  122. if (i>0)
  123. header+=", ";
  124. header+=_prestr(fnode->arguments[i].precision)+_typestr(fnode->arguments[i].type)+" "+fnode->arguments[i].name;
  125. }
  126. header+=")\n";
  127. code+=header;
  128. code+=dump_node_code(fnode->body,p_level+1);
  129. }
  130. //code+=dump_node_code(pnode->body,p_level);
  131. } break;
  132. case SL::Node::TYPE_FUNCTION: {
  133. } break;
  134. case SL::Node::TYPE_BLOCK: {
  135. SL::BlockNode *bnode=(SL::BlockNode*)p_node;
  136. //variables
  137. code+=_mktab(p_level-1)+"{\n";
  138. for(Map<StringName,SL::BlockNode::Variable>::Element *E=bnode->variables.front();E;E=E->next()) {
  139. code+=_mktab(p_level)+_prestr(E->get().precision)+_typestr(E->get().type)+" "+E->key()+";\n";
  140. }
  141. for(int i=0;i<bnode->statements.size();i++) {
  142. String scode = dump_node_code(bnode->statements[i],p_level);
  143. if (bnode->statements[i]->type==SL::Node::TYPE_CONTROL_FLOW || bnode->statements[i]->type==SL::Node::TYPE_CONTROL_FLOW) {
  144. code+=scode; //use directly
  145. } else {
  146. code+=_mktab(p_level)+scode+";\n";
  147. }
  148. }
  149. code+=_mktab(p_level-1)+"}\n";
  150. } break;
  151. case SL::Node::TYPE_VARIABLE: {
  152. SL::VariableNode *vnode=(SL::VariableNode*)p_node;
  153. code=vnode->name;
  154. } break;
  155. case SL::Node::TYPE_CONSTANT: {
  156. SL::ConstantNode *cnode=(SL::ConstantNode*)p_node;
  157. return get_constant_text(cnode->datatype,cnode->values);
  158. } break;
  159. case SL::Node::TYPE_OPERATOR: {
  160. SL::OperatorNode *onode=(SL::OperatorNode*)p_node;
  161. switch(onode->op) {
  162. case SL::OP_ASSIGN:
  163. case SL::OP_ASSIGN_ADD:
  164. case SL::OP_ASSIGN_SUB:
  165. case SL::OP_ASSIGN_MUL:
  166. case SL::OP_ASSIGN_DIV:
  167. case SL::OP_ASSIGN_SHIFT_LEFT:
  168. case SL::OP_ASSIGN_SHIFT_RIGHT:
  169. case SL::OP_ASSIGN_MOD:
  170. case SL::OP_ASSIGN_BIT_AND:
  171. case SL::OP_ASSIGN_BIT_OR:
  172. case SL::OP_ASSIGN_BIT_XOR:
  173. code=dump_node_code(onode->arguments[0],p_level)+_opstr(onode->op)+dump_node_code(onode->arguments[1],p_level);
  174. break;
  175. case SL::OP_BIT_INVERT:
  176. case SL::OP_NEGATE:
  177. case SL::OP_NOT:
  178. case SL::OP_DECREMENT:
  179. case SL::OP_INCREMENT:
  180. code=_opstr(onode->op)+dump_node_code(onode->arguments[0],p_level);
  181. break;
  182. case SL::OP_POST_DECREMENT:
  183. case SL::OP_POST_INCREMENT:
  184. code=dump_node_code(onode->arguments[0],p_level)+_opstr(onode->op);
  185. break;
  186. case SL::OP_CALL:
  187. case SL::OP_CONSTRUCT:
  188. code=dump_node_code(onode->arguments[0],p_level)+"(";
  189. for(int i=1;i<onode->arguments.size();i++) {
  190. if (i>1)
  191. code+=", ";
  192. code+=dump_node_code(onode->arguments[i],p_level);
  193. }
  194. code+=")";
  195. break;
  196. default: {
  197. code="("+dump_node_code(onode->arguments[0],p_level)+_opstr(onode->op)+dump_node_code(onode->arguments[1],p_level)+")";
  198. break;
  199. }
  200. }
  201. } break;
  202. case SL::Node::TYPE_CONTROL_FLOW: {
  203. SL::ControlFlowNode *cfnode=(SL::ControlFlowNode*)p_node;
  204. if (cfnode->flow_op==SL::FLOW_OP_IF) {
  205. code+=_mktab(p_level)+"if ("+dump_node_code(cfnode->expressions[0],p_level)+")\n";
  206. code+=dump_node_code(cfnode->blocks[0],p_level+1);
  207. if (cfnode->blocks.size()==2) {
  208. code+=_mktab(p_level)+"else\n";
  209. code+=dump_node_code(cfnode->blocks[1],p_level+1);
  210. }
  211. } else if (cfnode->flow_op==SL::FLOW_OP_RETURN) {
  212. if (cfnode->blocks.size()) {
  213. code="return "+dump_node_code(cfnode->blocks[0],p_level);
  214. } else {
  215. code="return";
  216. }
  217. }
  218. } break;
  219. case SL::Node::TYPE_MEMBER: {
  220. SL::MemberNode *mnode=(SL::MemberNode*)p_node;
  221. code=dump_node_code(mnode->owner,p_level)+"."+mnode->name;
  222. } break;
  223. }
  224. return code;
  225. }
  226. static Error recreate_code(void *p_str,SL::ShaderNode *p_program) {
  227. String *str=(String*)p_str;
  228. *str=dump_node_code(p_program,0);
  229. return OK;
  230. }
  231. MainLoop* test() {
  232. List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
  233. if (cmdlargs.empty()) {
  234. //try editor!
  235. print_line("usage: godot -test shader_lang <shader>");
  236. return NULL;
  237. }
  238. String test = cmdlargs.back()->get();
  239. FileAccess *fa = FileAccess::open(test,FileAccess::READ);
  240. if (!fa) {
  241. ERR_FAIL_V(NULL);
  242. }
  243. String code;
  244. while(true) {
  245. CharType c = fa->get_8();
  246. if (fa->eof_reached())
  247. break;
  248. code+=c;
  249. }
  250. SL sl;
  251. print_line("tokens:\n\n"+sl.token_debug(code));
  252. Map<StringName,Map<StringName,SL::DataType> > dt;
  253. dt["fragment"]["ALBEDO"]=SL::TYPE_VEC3;
  254. Set<String> rm;
  255. rm.insert("popo");
  256. Error err = sl.compile(code,dt,rm);
  257. if (err) {
  258. print_line("Error at line: "+rtos(sl.get_error_line())+": "+sl.get_error_text());
  259. return NULL;
  260. } else {
  261. String code;
  262. recreate_code(&code,sl.get_shader());
  263. print_line("code:\n\n"+code);
  264. }
  265. return NULL;
  266. }
  267. }