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