void ShaderCompilerGLES2::_dump_function_deps(SL::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, StringBuilder &r_to_add, Set<StringName> &r_added) { int fidx = -1; for (int i = 0; i < p_node->functions.size(); i++) { if (p_node->functions[i].name == p_for_func) { fidx = i; break; } } ERR_FAIL_COND(fidx == -1); for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) { if (r_added.has(E->get())) { continue; } _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, r_added); SL::FunctionNode *fnode = NULL; for (int i = 0; i < p_node->functions.size(); i++) { if (p_node->functions[i].name == E->get()) { fnode = p_node->functions[i].function; break; } } ERR_FAIL_COND(!fnode); r_to_add += "\n"; StringBuffer<128> header; header += _typestr(fnode->return_type); header += " "; header += _mkid(fnode->name); header += "("; for (int i = 0; i < fnode->arguments.size(); i++) { if (i > 0) header += ", "; header += _qualstr(fnode->arguments[i].qualifier); header += _prestr(fnode->arguments[i].precision); header += _typestr(fnode->arguments[i].type); header += " "; header += _mkid(fnode->arguments[i].name); } header += ")\n"; r_to_add += header.as_string(); r_to_add += p_func_code[E->get()]; r_added.insert(E->get()); } }
void ShaderCompilerGLES3::_dump_function_deps(SL::ShaderNode *p_node, const StringName &p_for_func, const Map<StringName, String> &p_func_code, String &r_to_add, Set<StringName> &added) { int fidx = -1; for (int i = 0; i < p_node->functions.size(); i++) { if (p_node->functions[i].name == p_for_func) { fidx = i; break; } } ERR_FAIL_COND(fidx == -1); for (Set<StringName>::Element *E = p_node->functions[fidx].uses_function.front(); E; E = E->next()) { print_line(String(p_node->functions[fidx].name) + " uses function: " + String(E->get())); if (added.has(E->get())) { continue; //was added already } _dump_function_deps(p_node, E->get(), p_func_code, r_to_add, added); SL::FunctionNode *fnode = NULL; for (int i = 0; i < p_node->functions.size(); i++) { if (p_node->functions[i].name == E->get()) { fnode = p_node->functions[i].function; break; } } ERR_FAIL_COND(!fnode); r_to_add += "\n"; String header; header = _typestr(fnode->return_type) + " " + _mkid(fnode->name) + "("; for (int i = 0; i < fnode->arguments.size(); i++) { if (i > 0) header += ", "; header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name); } header += ")\n"; r_to_add += header; r_to_add += p_func_code[E->get()]; added.insert(E->get()); } }
String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions) { String code; switch (p_node->type) { case SL::Node::TYPE_SHADER: { SL::ShaderNode *pnode = (SL::ShaderNode *)p_node; for (int i = 0; i < pnode->render_modes.size(); i++) { if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) { r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]].utf8()); used_rmode_defines.insert(pnode->render_modes[i]); } if (p_actions.render_mode_flags.has(pnode->render_modes[i])) { *p_actions.render_mode_flags[pnode->render_modes[i]] = true; } if (p_actions.render_mode_values.has(pnode->render_modes[i])) { Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]]; *p.first = p.second; } } int max_texture_uniforms = 0; int max_uniforms = 0; for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) { if (SL::is_sampler_type(E->get().type)) max_texture_uniforms++; else max_uniforms++; } r_gen_code.texture_uniforms.resize(max_texture_uniforms); r_gen_code.texture_hints.resize(max_texture_uniforms); Vector<int> uniform_sizes; Vector<int> uniform_alignments; Vector<StringName> uniform_defines; uniform_sizes.resize(max_uniforms); uniform_alignments.resize(max_uniforms); uniform_defines.resize(max_uniforms); bool uses_uniforms = false; for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) { String ucode; if (SL::is_sampler_type(E->get().type)) { ucode = "uniform "; } ucode += _prestr(E->get().precission); ucode += _typestr(E->get().type); ucode += " " + _mkid(E->key()); ucode += ";\n"; if (SL::is_sampler_type(E->get().type)) { r_gen_code.vertex_global += ucode; r_gen_code.fragment_global += ucode; r_gen_code.texture_uniforms[E->get().texture_order] = _mkid(E->key()); r_gen_code.texture_hints[E->get().texture_order] = E->get().hint; } else { if (!uses_uniforms) { r_gen_code.defines.push_back(String("#define USE_MATERIAL\n").ascii()); uses_uniforms = true; } uniform_defines[E->get().order] = ucode; uniform_sizes[E->get().order] = _get_datatype_size(E->get().type); uniform_alignments[E->get().order] = MIN(16, _get_datatype_size(E->get().type)); } p_actions.uniforms->insert(E->key(), E->get()); } for (int i = 0; i < max_uniforms; i++) { r_gen_code.uniforms += uniform_defines[i]; } // add up for (int i = 0; i < uniform_sizes.size(); i++) { if (i > 0) { int align = uniform_sizes[i - 1] % uniform_alignments[i]; if (align != 0) { uniform_sizes[i - 1] += uniform_alignments[i] - align; } uniform_sizes[i] = uniform_sizes[i] + uniform_sizes[i - 1]; } } //offset r_gen_code.uniform_offsets.resize(uniform_sizes.size()); for (int i = 0; i < uniform_sizes.size(); i++) { if (i > 0) r_gen_code.uniform_offsets[i] = uniform_sizes[i - 1]; else r_gen_code.uniform_offsets[i] = 0; } /* for(Map<StringName,SL::ShaderNode::Uniform>::Element *E=pnode->uniforms.front();E;E=E->next()) { if (SL::is_sampler_type(E->get().type)) { continue; } print_line("u - "+String(E->key())+" offset: "+itos(r_gen_code.uniform_offsets[E->get().order])); } */ if (uniform_sizes.size()) { r_gen_code.uniform_total_size = uniform_sizes[uniform_sizes.size() - 1]; } else { r_gen_code.uniform_total_size = 0; } for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) { String vcode; vcode += _prestr(E->get().precission); vcode += _typestr(E->get().type); vcode += " " + _mkid(E->key()); vcode += ";\n"; r_gen_code.vertex_global += "out " + vcode; r_gen_code.fragment_global += "in " + vcode; } Map<StringName, String> function_code; //code for functions for (int i = 0; i < pnode->functions.size(); i++) { SL::FunctionNode *fnode = pnode->functions[i].function; function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions); } //place functions in actual code Set<StringName> added_vtx; Set<StringName> added_fragment; //share for light for (int i = 0; i < pnode->functions.size(); i++) { SL::FunctionNode *fnode = pnode->functions[i].function; current_func_name = fnode->name; if (fnode->name == "vertex") { _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.vertex_global, added_vtx); r_gen_code.vertex = function_code["vertex"]; } if (fnode->name == "fragment") { _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment); r_gen_code.fragment = function_code["fragment"]; } if (fnode->name == "light") { _dump_function_deps(pnode, fnode->name, function_code, r_gen_code.fragment_global, added_fragment); r_gen_code.light = function_code["light"]; } } //code+=dump_node_code(pnode->body,p_level); } break; case SL::Node::TYPE_FUNCTION: { } break; case SL::Node::TYPE_BLOCK: { SL::BlockNode *bnode = (SL::BlockNode *)p_node; //variables code += _mktab(p_level - 1) + "{\n"; for (Map<StringName, SL::BlockNode::Variable>::Element *E = bnode->variables.front(); E; E = E->next()) { code += _mktab(p_level) + _prestr(E->get().precision) + _typestr(E->get().type) + " " + _mkid(E->key()) + ";\n"; } for (int i = 0; i < bnode->statements.size(); i++) { String scode = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions); if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) { // FIXME: if (A || A) ? I am hesitant to delete one of them, could be copy-paste error. code += scode; //use directly } else { code += _mktab(p_level) + scode + ";\n"; } } code += _mktab(p_level - 1) + "}\n"; } break; case SL::Node::TYPE_VARIABLE: { SL::VariableNode *vnode = (SL::VariableNode *)p_node; if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) { String define = p_default_actions.usage_defines[vnode->name]; if (define.begins_with("@")) { define = p_default_actions.usage_defines[define.substr(1, define.length())]; } r_gen_code.defines.push_back(define.utf8()); used_name_defines.insert(vnode->name); } if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) { *p_actions.usage_flag_pointers[vnode->name] = true; used_flag_pointers.insert(vnode->name); } if (p_default_actions.renames.has(vnode->name)) code = p_default_actions.renames[vnode->name]; else code = _mkid(vnode->name); if (vnode->name == time_name) { if (current_func_name == vertex_name) { r_gen_code.uses_vertex_time = true; } if (current_func_name == fragment_name) { r_gen_code.uses_fragment_time = true; } } } break; case SL::Node::TYPE_CONSTANT: { SL::ConstantNode *cnode = (SL::ConstantNode *)p_node; return get_constant_text(cnode->datatype, cnode->values); } break; case SL::Node::TYPE_OPERATOR: { SL::OperatorNode *onode = (SL::OperatorNode *)p_node; switch (onode->op) { case SL::OP_ASSIGN: case SL::OP_ASSIGN_ADD: case SL::OP_ASSIGN_SUB: case SL::OP_ASSIGN_MUL: case SL::OP_ASSIGN_DIV: case SL::OP_ASSIGN_SHIFT_LEFT: case SL::OP_ASSIGN_SHIFT_RIGHT: case SL::OP_ASSIGN_MOD: case SL::OP_ASSIGN_BIT_AND: case SL::OP_ASSIGN_BIT_OR: case SL::OP_ASSIGN_BIT_XOR: if (onode->arguments[0]->type == SL::Node::TYPE_VARIABLE) { SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0]; if (p_actions.write_flag_pointers.has(vnode->name)) { *p_actions.write_flag_pointers[vnode->name] = true; } } code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions); break; case SL::OP_BIT_INVERT: case SL::OP_NEGATE: case SL::OP_NOT: case SL::OP_DECREMENT: case SL::OP_INCREMENT: code = _opstr(onode->op) + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions); break; case SL::OP_POST_DECREMENT: case SL::OP_POST_INCREMENT: code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions) + _opstr(onode->op); break; case SL::OP_CALL: case SL::OP_CONSTRUCT: { ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::TYPE_VARIABLE, String()); SL::VariableNode *vnode = (SL::VariableNode *)onode->arguments[0]; if (onode->op == SL::OP_CONSTRUCT) { code += String(vnode->name); } else { if (internal_functions.has(vnode->name)) { code += vnode->name; } else if (p_default_actions.renames.has(vnode->name)) { code += p_default_actions.renames[vnode->name]; } else { code += _mkid(vnode->name); } } code += "("; for (int i = 1; i < onode->arguments.size(); i++) { if (i > 1) code += ", "; code += _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions); } code += ")"; } break; case SL::OP_INDEX: { code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions); code += "["; code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions); code += "]"; } break; case SL::OP_SELECT_IF: { code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions); code += "?"; code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions); code += ":"; code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions); } break; default: { code = "(" + _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions) + ")"; break; } } } break; case SL::Node::TYPE_CONTROL_FLOW: { SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node; if (cfnode->flow_op == SL::FLOW_OP_IF) { code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions) + ")\n"; code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions); if (cfnode->blocks.size() == 2) { code += _mktab(p_level) + "else\n"; code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions); } } else if (cfnode->flow_op == SL::FLOW_OP_WHILE) { code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions) + ")\n"; code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions); } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) { if (cfnode->expressions.size()) { code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions) + ";"; } else { code = "return;"; } } } break; case SL::Node::TYPE_MEMBER: { SL::MemberNode *mnode = (SL::MemberNode *)p_node; code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions) + "." + mnode->name; } break; } return code; }
String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning) { StringBuilder code; switch (p_node->type) { case SL::Node::TYPE_SHADER: { SL::ShaderNode *snode = (SL::ShaderNode *)p_node; for (int i = 0; i < snode->render_modes.size(); i++) { if (p_default_actions.render_mode_defines.has(snode->render_modes[i]) && !used_rmode_defines.has(snode->render_modes[i])) { r_gen_code.custom_defines.push_back(p_default_actions.render_mode_defines[snode->render_modes[i]].utf8()); used_rmode_defines.insert(snode->render_modes[i]); } if (p_actions.render_mode_flags.has(snode->render_modes[i])) { *p_actions.render_mode_flags[snode->render_modes[i]] = true; } if (p_actions.render_mode_values.has(snode->render_modes[i])) { Pair<int *, int> &p = p_actions.render_mode_values[snode->render_modes[i]]; *p.first = p.second; } } int max_texture_uniforms = 0; int max_uniforms = 0; for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = snode->uniforms.front(); E; E = E->next()) { if (SL::is_sampler_type(E->get().type)) max_texture_uniforms++; else max_uniforms++; } r_gen_code.texture_uniforms.resize(max_texture_uniforms); r_gen_code.texture_hints.resize(max_texture_uniforms); r_gen_code.uniforms.resize(max_uniforms + max_texture_uniforms); StringBuilder vertex_global; StringBuilder fragment_global; // uniforms for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = snode->uniforms.front(); E; E = E->next()) { StringBuffer<> uniform_code; // use highp if no precision is specified to prevent different default values in fragment and vertex shader SL::DataPrecision precision = E->get().precision; if (precision == SL::PRECISION_DEFAULT) { precision = SL::PRECISION_HIGHP; } uniform_code += "uniform "; uniform_code += _prestr(precision); uniform_code += _typestr(E->get().type); uniform_code += " "; uniform_code += _mkid(E->key()); uniform_code += ";\n"; if (SL::is_sampler_type(E->get().type)) { r_gen_code.texture_uniforms.write[E->get().texture_order] = E->key(); r_gen_code.texture_hints.write[E->get().texture_order] = E->get().hint; } else { r_gen_code.uniforms.write[E->get().order] = E->key(); } vertex_global += uniform_code.as_string(); fragment_global += uniform_code.as_string(); p_actions.uniforms->insert(E->key(), E->get()); } // varyings for (Map<StringName, SL::ShaderNode::Varying>::Element *E = snode->varyings.front(); E; E = E->next()) { StringBuffer<> varying_code; varying_code += "varying "; varying_code += _prestr(E->get().precision); varying_code += _typestr(E->get().type); varying_code += " "; varying_code += _mkid(E->key()); varying_code += ";\n"; String final_code = varying_code.as_string(); vertex_global += final_code; fragment_global += final_code; } // functions Map<StringName, String> function_code; for (int i = 0; i < snode->functions.size(); i++) { SL::FunctionNode *fnode = snode->functions[i].function; current_func_name = fnode->name; function_code[fnode->name] = _dump_node_code(fnode->body, 1, r_gen_code, p_actions, p_default_actions, p_assigning); } Set<StringName> added_vertex; Set<StringName> added_fragment; for (int i = 0; i < snode->functions.size(); i++) { SL::FunctionNode *fnode = snode->functions[i].function; current_func_name = fnode->name; if (fnode->name == vertex_name) { _dump_function_deps(snode, fnode->name, function_code, vertex_global, added_vertex); r_gen_code.vertex = function_code[vertex_name]; } else if (fnode->name == fragment_name) { _dump_function_deps(snode, fnode->name, function_code, fragment_global, added_fragment); r_gen_code.fragment = function_code[fragment_name]; } else if (fnode->name == light_name) { _dump_function_deps(snode, fnode->name, function_code, fragment_global, added_fragment); r_gen_code.light = function_code[light_name]; } } r_gen_code.vertex_global = vertex_global.as_string(); r_gen_code.fragment_global = fragment_global.as_string(); } break; case SL::Node::TYPE_FUNCTION: { } break; case SL::Node::TYPE_BLOCK: { SL::BlockNode *bnode = (SL::BlockNode *)p_node; if (!bnode->single_statement) { code += _mktab(p_level - 1); code += "{\n"; } for (int i = 0; i < bnode->statements.size(); i++) { String statement_code = _dump_node_code(bnode->statements[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->single_statement) { code += statement_code; } else { code += _mktab(p_level); code += statement_code; code += ";\n"; } } if (!bnode->single_statement) { code += _mktab(p_level - 1); code += "}\n"; } } break; case SL::Node::TYPE_VARIABLE_DECLARATION: { SL::VariableDeclarationNode *var_dec_node = (SL::VariableDeclarationNode *)p_node; StringBuffer<> declaration; declaration += _prestr(var_dec_node->precision); declaration += _typestr(var_dec_node->datatype); for (int i = 0; i < var_dec_node->declarations.size(); i++) { if (i > 0) { declaration += ","; } declaration += " "; declaration += _mkid(var_dec_node->declarations[i].name); if (var_dec_node->declarations[i].initializer) { declaration += " = "; declaration += _dump_node_code(var_dec_node->declarations[i].initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } } code += declaration.as_string(); } break; case SL::Node::TYPE_VARIABLE: { SL::VariableNode *var_node = (SL::VariableNode *)p_node; if (p_assigning && p_actions.write_flag_pointers.has(var_node->name)) { *p_actions.write_flag_pointers[var_node->name] = true; } if (p_default_actions.usage_defines.has(var_node->name) && !used_name_defines.has(var_node->name)) { String define = p_default_actions.usage_defines[var_node->name]; if (define.begins_with("@")) { define = p_default_actions.usage_defines[define.substr(1, define.length())]; } r_gen_code.custom_defines.push_back(define.utf8()); used_name_defines.insert(var_node->name); } if (p_actions.usage_flag_pointers.has(var_node->name) && !used_flag_pointers.has(var_node->name)) { *p_actions.usage_flag_pointers[var_node->name] = true; used_flag_pointers.insert(var_node->name); } if (p_default_actions.renames.has(var_node->name)) { code += p_default_actions.renames[var_node->name]; } else { code += _mkid(var_node->name); } if (var_node->name == time_name) { if (current_func_name == vertex_name) { r_gen_code.uses_vertex_time = true; } if (current_func_name == fragment_name || current_func_name == light_name) { r_gen_code.uses_fragment_time = true; } } } break; case SL::Node::TYPE_CONSTANT: { SL::ConstantNode *const_node = (SL::ConstantNode *)p_node; return get_constant_text(const_node->datatype, const_node->values); } break; case SL::Node::TYPE_OPERATOR: { SL::OperatorNode *op_node = (SL::OperatorNode *)p_node; switch (op_node->op) { case SL::OP_ASSIGN: case SL::OP_ASSIGN_ADD: case SL::OP_ASSIGN_SUB: case SL::OP_ASSIGN_MUL: case SL::OP_ASSIGN_DIV: case SL::OP_ASSIGN_SHIFT_LEFT: case SL::OP_ASSIGN_SHIFT_RIGHT: case SL::OP_ASSIGN_BIT_AND: case SL::OP_ASSIGN_BIT_OR: case SL::OP_ASSIGN_BIT_XOR: { code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, true); code += " "; code += _opstr(op_node->op); code += " "; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } break; case SL::OP_ASSIGN_MOD: { code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, true); code += " = "; code += "mod("; code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, true); code += ", "; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += ")"; } break; case SL::OP_BIT_INVERT: case SL::OP_NEGATE: case SL::OP_NOT: case SL::OP_DECREMENT: case SL::OP_INCREMENT: { code += _opstr(op_node->op); code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } break; case SL::OP_POST_DECREMENT: case SL::OP_POST_INCREMENT: { code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += _opstr(op_node->op); } break; case SL::OP_CALL: case SL::OP_CONSTRUCT: { ERR_FAIL_COND_V(op_node->arguments[0]->type != SL::Node::TYPE_VARIABLE, String()); SL::VariableNode *var_node = (SL::VariableNode *)op_node->arguments[0]; if (op_node->op == SL::OP_CONSTRUCT) { code += var_node->name; } else { if (var_node->name == "texture") { // emit texture call if (op_node->arguments[1]->get_datatype() == SL::TYPE_SAMPLER2D) { code += "texture2D"; } else if (op_node->arguments[1]->get_datatype() == SL::TYPE_SAMPLERCUBE) { code += "textureCube"; } } else if (var_node->name == "textureLod") { // emit texture call if (op_node->arguments[1]->get_datatype() == SL::TYPE_SAMPLER2D) { code += "texture2DLod"; } else if (op_node->arguments[1]->get_datatype() == SL::TYPE_SAMPLERCUBE) { code += "textureCubeLod"; } } else if (var_node->name == "mix") { switch (op_node->arguments[3]->get_datatype()) { case SL::TYPE_BVEC2: { code += "select2"; } break; case SL::TYPE_BVEC3: { code += "select3"; } break; case SL::TYPE_BVEC4: { code += "select4"; } break; case SL::TYPE_VEC2: case SL::TYPE_VEC3: case SL::TYPE_VEC4: case SL::TYPE_FLOAT: { code += "mix"; } break; default: { SL::DataType type = op_node->arguments[3]->get_datatype(); // FIXME: Proper error print or graceful handling print_line(String("uhhhh invalid mix with type: ") + itos(type)); } break; } } else if (p_default_actions.renames.has(var_node->name)) { code += p_default_actions.renames[var_node->name]; } else if (internal_functions.has(var_node->name)) { code += var_node->name; } else { code += _mkid(var_node->name); } } code += "("; for (int i = 1; i < op_node->arguments.size(); i++) { if (i > 1) { code += ", "; } code += _dump_node_code(op_node->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } code += ")"; } break; case SL::OP_INDEX: { code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "["; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "]"; } break; case SL::OP_SELECT_IF: { code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += " ? "; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += " : "; code += _dump_node_code(op_node->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } break; case SL::OP_MOD: { code += "mod(float("; code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "), float("; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "))"; } break; default: { code += "("; code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += " "; code += _opstr(op_node->op); code += " "; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += ")"; } break; } } break; case SL::Node::TYPE_CONTROL_FLOW: { SL::ControlFlowNode *cf_node = (SL::ControlFlowNode *)p_node; if (cf_node->flow_op == SL::FLOW_OP_IF) { code += _mktab(p_level); code += "if ("; code += _dump_node_code(cf_node->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += ")\n"; code += _dump_node_code(cf_node->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); if (cf_node->blocks.size() == 2) { code += _mktab(p_level); code += "else\n"; code += _dump_node_code(cf_node->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); } } else if (cf_node->flow_op == SL::FLOW_OP_WHILE) { code += _mktab(p_level); code += "while ("; code += _dump_node_code(cf_node->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += ")\n"; code += _dump_node_code(cf_node->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); } else if (cf_node->flow_op == SL::FLOW_OP_FOR) { code += _mktab(p_level); code += "for ("; code += _dump_node_code(cf_node->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "; "; code += _dump_node_code(cf_node->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "; "; code += _dump_node_code(cf_node->expressions[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += ")\n"; code += _dump_node_code(cf_node->blocks[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } else if (cf_node->flow_op == SL::FLOW_OP_RETURN) { code += _mktab(p_level); code += "return"; if (cf_node->expressions.size()) { code += " "; code += _dump_node_code(cf_node->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); } code += ";\n"; } else if (cf_node->flow_op == SL::FLOW_OP_DISCARD) { if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) { *p_actions.usage_flag_pointers["DISCARD"] = true; used_flag_pointers.insert("DISCARD"); } code += "discard;"; } else if (cf_node->flow_op == SL::FLOW_OP_CONTINUE) { code += "continue;"; } else if (cf_node->flow_op == SL::FLOW_OP_BREAK) { code += "break;"; } } break; case SL::Node::TYPE_MEMBER: { SL::MemberNode *member_node = (SL::MemberNode *)p_node; code += _dump_node_code(member_node->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning); code += "."; code += member_node->name; } break; } return code.as_string(); }
static String dump_node_code(SL::Node *p_node, int p_level) { String code; switch (p_node->type) { case SL::Node::TYPE_SHADER: { SL::ShaderNode *pnode = (SL::ShaderNode *)p_node; for (Map<StringName, SL::ShaderNode::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) { String ucode = "uniform "; ucode += _prestr(E->get().precission); ucode += _typestr(E->get().type); ucode += " " + String(E->key()); if (E->get().default_value.size()) { ucode += " = " + get_constant_text(E->get().type, E->get().default_value); } static const char *hint_name[SL::ShaderNode::Uniform::HINT_MAX] = { "", "color", "range", "albedo", "normal", "black", "white" }; if (E->get().hint) ucode += " : " + String(hint_name[E->get().hint]); code += ucode + "\n"; } for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) { String vcode = "varying "; vcode += _prestr(E->get().precission); vcode += _typestr(E->get().type); vcode += " " + String(E->key()); code += vcode + "\n"; } for (int i = 0; i < pnode->functions.size(); i++) { SL::FunctionNode *fnode = pnode->functions[i].function; String header; header = _typestr(fnode->return_type) + " " + fnode->name + "("; for (int i = 0; i < fnode->arguments.size(); i++) { if (i > 0) header += ", "; header += _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + fnode->arguments[i].name; } header += ")\n"; code += header; code += dump_node_code(fnode->body, p_level + 1); } //code+=dump_node_code(pnode->body,p_level); } break; case SL::Node::TYPE_FUNCTION: { } break; case SL::Node::TYPE_BLOCK: { SL::BlockNode *bnode = (SL::BlockNode *)p_node; //variables code += _mktab(p_level - 1) + "{\n"; for (Map<StringName, SL::BlockNode::Variable>::Element *E = bnode->variables.front(); E; E = E->next()) { code += _mktab(p_level) + _prestr(E->get().precision) + _typestr(E->get().type) + " " + E->key() + ";\n"; } for (int i = 0; i < bnode->statements.size(); i++) { String scode = dump_node_code(bnode->statements[i], p_level); if (bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW || bnode->statements[i]->type == SL::Node::TYPE_CONTROL_FLOW) { code += scode; //use directly } else { code += _mktab(p_level) + scode + ";\n"; } } code += _mktab(p_level - 1) + "}\n"; } break; case SL::Node::TYPE_VARIABLE: { SL::VariableNode *vnode = (SL::VariableNode *)p_node; code = vnode->name; } break; case SL::Node::TYPE_CONSTANT: { SL::ConstantNode *cnode = (SL::ConstantNode *)p_node; return get_constant_text(cnode->datatype, cnode->values); } break; case SL::Node::TYPE_OPERATOR: { SL::OperatorNode *onode = (SL::OperatorNode *)p_node; switch (onode->op) { case SL::OP_ASSIGN: case SL::OP_ASSIGN_ADD: case SL::OP_ASSIGN_SUB: case SL::OP_ASSIGN_MUL: case SL::OP_ASSIGN_DIV: case SL::OP_ASSIGN_SHIFT_LEFT: case SL::OP_ASSIGN_SHIFT_RIGHT: case SL::OP_ASSIGN_MOD: case SL::OP_ASSIGN_BIT_AND: case SL::OP_ASSIGN_BIT_OR: case SL::OP_ASSIGN_BIT_XOR: code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level); break; case SL::OP_BIT_INVERT: case SL::OP_NEGATE: case SL::OP_NOT: case SL::OP_DECREMENT: case SL::OP_INCREMENT: code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level); break; case SL::OP_POST_DECREMENT: case SL::OP_POST_INCREMENT: code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op); break; case SL::OP_CALL: case SL::OP_CONSTRUCT: code = dump_node_code(onode->arguments[0], p_level) + "("; for (int i = 1; i < onode->arguments.size(); i++) { if (i > 1) code += ", "; code += dump_node_code(onode->arguments[i], p_level); } code += ")"; break; default: { code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")"; break; } } } break; case SL::Node::TYPE_CONTROL_FLOW: { SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node; if (cfnode->flow_op == SL::FLOW_OP_IF) { code += _mktab(p_level) + "if (" + dump_node_code(cfnode->expressions[0], p_level) + ")\n"; code += dump_node_code(cfnode->blocks[0], p_level + 1); if (cfnode->blocks.size() == 2) { code += _mktab(p_level) + "else\n"; code += dump_node_code(cfnode->blocks[1], p_level + 1); } } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) { if (cfnode->blocks.size()) { code = "return " + dump_node_code(cfnode->blocks[0], p_level); } else { code = "return"; } } } break; case SL::Node::TYPE_MEMBER: { SL::MemberNode *mnode = (SL::MemberNode *)p_node; code = dump_node_code(mnode->owner, p_level) + "." + mnode->name; } break; } return code; }
Error ShaderCompilerGLES2::compile_node(SL::ProgramNode *p_program) { // feed the local replace table and global code global_code=""; // uniforms first! int ubase=0; if (uniforms) ubase=uniforms->size(); for(Map<StringName,SL::Uniform>::Element *E=p_program->uniforms.front();E;E=E->next()) { String uline="uniform "+_typestr(E->get().type)+" _"+E->key().operator String()+";"ENDL; global_code+=uline; if (uniforms) { //if (uniforms->has(E->key())) { // //repeated uniform, error // ERR_EXPLAIN("Uniform already exists from other shader: "+String(E->key())); // ERR_FAIL_COND_V(uniforms->has(E->key()),ERR_ALREADY_EXISTS); // // } SL::Uniform u = E->get(); u.order+=ubase; uniforms->insert(E->key(),u); } } for(int i=0;i<p_program->functions.size();i++) { SL::FunctionNode *fnode=p_program->functions[i].function; StringName funcname=fnode->name; String newfuncname=replace_string(funcname); String header; header=_typestr(fnode->return_type)+" "+newfuncname+"("; for(int i=0;i<fnode->arguments.size();i++) { if (i>0) header+=", "; header+=_typestr(fnode->arguments[i].type)+" "+replace_string(fnode->arguments[i].name); } header+=") {"ENDL; String fcode=header; fcode+=dump_node_code(fnode->body,1); fcode+="}"ENDL; global_code+=fcode; } /* for(Map<StringName,SL::DataType>::Element *E=p_program->preexisting_variables.front();E;E=E->next()) { StringName varname=E->key(); String newvarname=replace_string(varname); global_code+="uniform "+_typestr(E->get())+" "+newvarname+";"ENDL; }*/ code=dump_node_code(p_program,0); #ifdef DEBUG_SHADER_ENABLED print_line("GLOBAL CODE:\n\n"); print_line(global_code); global_code=global_code.replace("\n",""); print_line("CODE:\n\n"); print_line(code); code=code.replace("\n",""); #endif return OK; }
String ShaderCompilerGLES2::dump_node_code(SL::Node *p_node,int p_level,bool p_assign_left) { String code; switch(p_node->type) { case SL::Node::TYPE_PROGRAM: { SL::ProgramNode *pnode=(SL::ProgramNode*)p_node; code+=dump_node_code(pnode->body,p_level); } break; case SL::Node::TYPE_FUNCTION: { } break; case SL::Node::TYPE_BLOCK: { SL::BlockNode *bnode=(SL::BlockNode*)p_node; //variables for(Map<StringName,SL::DataType>::Element *E=bnode->variables.front();E;E=E->next()) { code+=_mktab(p_level)+_typestr(E->value())+" "+replace_string(E->key())+";"ENDL; } for(int i=0;i<bnode->statements.size();i++) { code+=_mktab(p_level)+dump_node_code(bnode->statements[i],p_level)+";"ENDL; } } break; case SL::Node::TYPE_VARIABLE: { SL::VariableNode *vnode=(SL::VariableNode*)p_node; if (type==ShaderLanguage::SHADER_MATERIAL_VERTEX) { if (vnode->name==vname_vertex && p_assign_left) { vertex_code_writes_vertex=true; } } if (type==ShaderLanguage::SHADER_MATERIAL_FRAGMENT) { if (vnode->name==vname_discard) { uses_discard=true; } if (vnode->name==vname_screen_uv) { uses_screen_uv=true; } if (vnode->name==vname_diffuse_alpha && p_assign_left) { uses_alpha=true; } if (vnode->name==vname_color_interp) { flags->use_color_interp=true; } if (vnode->name==vname_uv_interp) { flags->use_uv_interp=true; } if (vnode->name==vname_uv2_interp) { flags->use_uv2_interp=true; } if (vnode->name==vname_var1_interp) { flags->use_var1_interp=true; } if (vnode->name==vname_var2_interp) { flags->use_var2_interp=true; } if (vnode->name==vname_tangent_interp || vnode->name==vname_binormal_interp) { flags->use_tangent_interp=true; } } if (type==ShaderLanguage::SHADER_MATERIAL_LIGHT) { if (vnode->name==vname_light) { uses_light=true; } } if (vnode->name==vname_time) { uses_time=true; } code=replace_string(vnode->name); } break; case SL::Node::TYPE_CONSTANT: { SL::ConstantNode *cnode=(SL::ConstantNode*)p_node; switch(cnode->datatype) { case SL::TYPE_BOOL: code=cnode->value.operator bool()?"true":"false"; break; case SL::TYPE_FLOAT: code=_mknum(cnode->value); break; //force zeros, so GLSL doesn't confuse with integer. case SL::TYPE_VEC2: { Vector2 v = cnode->value; code="vec2("+_mknum(v.x)+", "+_mknum(v.y)+")"; } break; case SL::TYPE_VEC3: { Vector3 v = cnode->value; code="vec3("+_mknum(v.x)+", "+_mknum(v.y)+", "+_mknum(v.z)+")"; } break; case SL::TYPE_VEC4: { Plane v = cnode->value; code="vec4("+_mknum(v.normal.x)+", "+_mknum(v.normal.y)+", "+_mknum(v.normal.z)+", "+_mknum(v.d)+")"; } break; case SL::TYPE_MAT3: { Matrix3 x = cnode->value; code="mat3( vec3("+_mknum(x.get_axis(0).x)+", "+_mknum(x.get_axis(0).y)+", "+_mknum(x.get_axis(0).z)+"), vec3("+_mknum(x.get_axis(1).x)+", "+_mknum(x.get_axis(1).y)+", "+_mknum(x.get_axis(1).z)+"), vec3("+_mknum(x.get_axis(2).x)+", "+_mknum(x.get_axis(2).y)+", "+_mknum(x.get_axis(2).z)+"))"; } break; case SL::TYPE_MAT4: { Transform x = cnode->value; code="mat4( vec4("+_mknum(x.basis.get_axis(0).x)+", "+_mknum(x.basis.get_axis(0).y)+", "+_mknum(x.basis.get_axis(0).z)+",0.0), vec4("+_mknum(x.basis.get_axis(1).x)+", "+_mknum(x.basis.get_axis(1).y)+", "+_mknum(x.basis.get_axis(1).z)+",0.0), vec4("+_mknum(x.basis.get_axis(2).x)+", "+_mknum(x.basis.get_axis(2).y)+", "+_mknum(x.basis.get_axis(2).z)+",0.0), vec4("+_mknum(x.origin.x)+", "+_mknum(x.origin.y)+", "+_mknum(x.origin.z)+",1.0))"; } break; default: code="<error: "+Variant::get_type_name(cnode->value.get_type())+" ("+itos(cnode->datatype)+">"; } } break; case SL::Node::TYPE_OPERATOR: { SL::OperatorNode *onode=(SL::OperatorNode*)p_node; switch(onode->op) { case SL::OP_ASSIGN_MUL: { if (onode->arguments[0]->get_datatype()==SL::TYPE_VEC3 && onode->arguments[1]->get_datatype()==SL::TYPE_MAT4) { String mul_l=dump_node_code(onode->arguments[0],p_level,true); String mul_r=dump_node_code(onode->arguments[1],p_level); code=mul_l+"=(vec4("+mul_l+",1.0)*("+mul_r+")).xyz"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_MAT4 && onode->arguments[1]->get_datatype()==SL::TYPE_VEC3) { String mul_l=dump_node_code(onode->arguments[0],p_level,true); String mul_r=dump_node_code(onode->arguments[1],p_level); code=mul_l+"=(("+mul_l+")*vec4("+mul_r+",1.0)).xyz"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_VEC2 && onode->arguments[1]->get_datatype()==SL::TYPE_MAT4) { String mul_l=dump_node_code(onode->arguments[0],p_level,true); String mul_r=dump_node_code(onode->arguments[1],p_level); code=mul_l+"=(vec4("+mul_l+",1.0,1.0)*("+mul_r+")).xy"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_MAT4 && onode->arguments[1]->get_datatype()==SL::TYPE_VEC2) { String mul_l=dump_node_code(onode->arguments[0],p_level,true); String mul_r=dump_node_code(onode->arguments[1],p_level); code=mul_l+"=(("+mul_l+")*vec4("+mul_r+",1.0,1.0)).xy"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_VEC2 && onode->arguments[1]->get_datatype()==SL::TYPE_MAT3) { String mul_l=dump_node_code(onode->arguments[0],p_level,true); String mul_r=dump_node_code(onode->arguments[1],p_level); code=mul_l+"=(("+mul_l+")*vec3("+mul_r+",1.0)).xy"; break; } }; case SL::OP_ASSIGN: case SL::OP_ASSIGN_ADD: case SL::OP_ASSIGN_SUB: case SL::OP_ASSIGN_DIV: code="("+dump_node_code(onode->arguments[0],p_level,true)+_opstr(onode->op)+dump_node_code(onode->arguments[1],p_level)+")"; break; case SL::OP_MUL: if (onode->arguments[0]->get_datatype()==SL::TYPE_MAT4 && onode->arguments[1]->get_datatype()==SL::TYPE_VEC3) { code="("+dump_node_code(onode->arguments[0],p_level)+"*vec4("+dump_node_code(onode->arguments[1],p_level)+",1.0)).xyz"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_VEC3 && onode->arguments[1]->get_datatype()==SL::TYPE_MAT4) { code="(vec4("+dump_node_code(onode->arguments[0],p_level)+",1.0)*"+dump_node_code(onode->arguments[1],p_level)+").xyz"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_MAT4 && onode->arguments[1]->get_datatype()==SL::TYPE_VEC2) { code="("+dump_node_code(onode->arguments[0],p_level)+"*vec4("+dump_node_code(onode->arguments[1],p_level)+",1.0,1.0)).xyz"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_VEC2 && onode->arguments[1]->get_datatype()==SL::TYPE_MAT4) { code="(vec4("+dump_node_code(onode->arguments[0],p_level)+",1.0,1.0)*"+dump_node_code(onode->arguments[1],p_level)+").xyz"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_MAT3 && onode->arguments[1]->get_datatype()==SL::TYPE_VEC2) { code="("+dump_node_code(onode->arguments[0],p_level)+"*vec3("+dump_node_code(onode->arguments[1],p_level)+",1.0)).xy"; break; } else if (onode->arguments[0]->get_datatype()==SL::TYPE_VEC2 && onode->arguments[1]->get_datatype()==SL::TYPE_MAT3) { code="(vec3("+dump_node_code(onode->arguments[0],p_level)+",1.0)*"+dump_node_code(onode->arguments[1],p_level)+").xy"; break; } case SL::OP_ADD: case SL::OP_SUB: case SL::OP_DIV: case SL::OP_CMP_EQ: case SL::OP_CMP_NEQ: case SL::OP_CMP_LEQ: case SL::OP_CMP_GEQ: case SL::OP_CMP_LESS: case SL::OP_CMP_GREATER: case SL::OP_CMP_OR: case SL::OP_CMP_AND: //handle binary code="("+dump_node_code(onode->arguments[0],p_level)+_opstr(onode->op)+dump_node_code(onode->arguments[1],p_level)+")"; break; case SL::OP_NEG: case SL::OP_NOT: //handle unary code=_opstr(onode->op)+dump_node_code(onode->arguments[0],p_level); break; case SL::OP_CONSTRUCT: case SL::OP_CALL: { String callfunc=dump_node_code(onode->arguments[0],p_level); code=callfunc+"("; /*if (callfunc=="mat4") { //fix constructor for mat4 for(int i=1;i<onode->arguments.size();i++) { if (i>1) code+=", "; //transform code+="vec4( "+dump_node_code(onode->arguments[i],p_level)+(i==4?",1.0)":",0.0)"); } } else*/ if (callfunc=="tex") { code="texture2D( "+dump_node_code(onode->arguments[1],p_level)+","+dump_node_code(onode->arguments[2],p_level)+")"; break; } else if (callfunc=="texcube") { code="(textureCube( "+dump_node_code(onode->arguments[1],p_level)+",("+dump_node_code(onode->arguments[2],p_level)+")).xyz"; break; } else if (callfunc=="texscreen") { //create the call to sample the screen, and clamp it uses_texscreen=true; code="(texture2D( texscreen_tex, min(("+dump_node_code(onode->arguments[1],p_level)+").xy*texscreen_screen_mult,texscreen_screen_mult))).rgb"; //code="(texture2D( screen_texture, ("+dump_node_code(onode->arguments[1],p_level)+").xy).rgb"; break; } else if (callfunc=="texpos") { //create the call to sample the screen, and clamp it uses_texpos=true; code="get_texpos("+dump_node_code(onode->arguments[1],p_level)+""; // code="get_texpos(gl_ProjectionMatrixInverse * texture2D( depth_texture, clamp(("+dump_node_code(onode->arguments[1],p_level)+").xy,vec2(0.0),vec2(1.0))*gl_LightSource[5].specular.zw+gl_LightSource[5].specular.xy)"; //code="(texture2D( screen_texture, ("+dump_node_code(onode->arguments[1],p_level)+").xy).rgb"; break; } else { for(int i=1;i<onode->arguments.size();i++) { if (i>1) code+=", "; //transform code+=dump_node_code(onode->arguments[i],p_level); } } code+=")"; break; } break; default: {} } } break; case SL::Node::TYPE_CONTROL_FLOW: { SL::ControlFlowNode *cfnode=(SL::ControlFlowNode*)p_node; if (cfnode->flow_op==SL::FLOW_OP_IF) { code+="if ("+dump_node_code(cfnode->statements[0],p_level)+") {"ENDL; code+=dump_node_code(cfnode->statements[1],p_level+1); if (cfnode->statements.size()==3) { code+="} else {"ENDL; code+=dump_node_code(cfnode->statements[2],p_level+1); } code+="}"ENDL; } else if (cfnode->flow_op==SL::FLOW_OP_RETURN) { if (cfnode->statements.size()) { code="return "+dump_node_code(cfnode->statements[0],p_level); } else { code="return"; } } } break; case SL::Node::TYPE_MEMBER: { SL::MemberNode *mnode=(SL::MemberNode*)p_node; String m; if (mnode->basetype==SL::TYPE_MAT4) { if (mnode->name=="x") m="[0]"; else if (mnode->name=="y") m="[1]"; else if (mnode->name=="z") m="[2]"; else if (mnode->name=="w") m="[3]"; } else if (mnode->basetype==SL::TYPE_MAT3) { if (mnode->name=="x") m="[0]"; else if (mnode->name=="y") m="[1]"; else if (mnode->name=="z") m="[2]"; } else { m="."+mnode->name; } code=dump_node_code(mnode->owner,p_level)+m; } break; } return code; }