#include "shader_compiler_gles3.h" #include "os/os.h" #define SL ShaderLanguage static String _mktab(int p_level) { String tb; for(int i=0;i& p_values) { switch(p_type) { case SL::TYPE_BOOL: return p_values[0].boolean?"true":"false"; case SL::TYPE_BVEC2: case SL::TYPE_BVEC3: case SL::TYPE_BVEC4: { String text="bvec"+itos(p_type-SL::TYPE_BOOL+1)+"("; for(int i=0;i0) text+=","; text+=p_values[i].boolean?"true":"false"; } text+=")"; return text; } case SL::TYPE_INT: return itos(p_values[0].sint); case SL::TYPE_IVEC2: case SL::TYPE_IVEC3: case SL::TYPE_IVEC4: { String text="ivec"+itos(p_type-SL::TYPE_INT+1)+"("; for(int i=0;i0) text+=","; text+=itos(p_values[i].sint); } text+=")"; return text; } break; case SL::TYPE_UINT: return itos(p_values[0].uint)+"u"; case SL::TYPE_UVEC2: case SL::TYPE_UVEC3: case SL::TYPE_UVEC4: { String text="uvec"+itos(p_type-SL::TYPE_UINT+1)+"("; for(int i=0;i0) text+=","; text+=itos(p_values[i].uint)+"u"; } text+=")"; return text; } break; case SL::TYPE_FLOAT: return f2sp0(p_values[0].real)+"f"; case SL::TYPE_VEC2: case SL::TYPE_VEC3: case SL::TYPE_VEC4: { String text="vec"+itos(p_type-SL::TYPE_FLOAT+1)+"("; for(int i=0;i0) text+=","; text+=f2sp0(p_values[i].real); } text+=")"; return text; } break; default: ERR_FAIL_V(String()); } } void ShaderCompilerGLES3::_dump_function_deps(SL::ShaderNode* p_node, const StringName& p_for_func, const Map& p_func_code, String& r_to_add, Set &added) { int fidx=-1; for(int i=0;ifunctions.size();i++) { if (p_node->functions[i].name==p_for_func) { fidx=i; break; } } ERR_FAIL_COND(fidx==-1); for (Set::Element *E=p_node->functions[fidx].uses_function.front();E;E=E->next()) { 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;ifunctions.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;iarguments.size();i++) { if (i>0) header+=", "; header+=_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;irender_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 &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::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 uniform_sizes; Vector uniform_alignments; Vector uniform_defines; uniform_sizes.resize(max_uniforms); uniform_alignments.resize(max_uniforms); uniform_defines.resize(max_uniforms); for(Map::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 (r_gen_code.uniforms.empty()) { r_gen_code.defines.push_back(String("#define USE_MATERIAL\n").ascii()); } 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;i0) { 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;i0) r_gen_code.uniform_offsets[i]=uniform_sizes[i-1]; else r_gen_code.uniform_offsets[i]=0; } /* for(Map::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::Element *E=pnode->varyings.front();E;E=E->next()) { String vcode; vcode+=_prestr(E->get().precission); vcode+=_typestr(E->get().type); vcode+=" "+String(E->key()); vcode+=";\n"; r_gen_code.vertex_global+="out "+vcode; r_gen_code.fragment_global+="in "+vcode; } Map function_code; //code for functions for(int i=0;ifunctions.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 added_vtx; Set added_fragment; //share for light for(int i=0;ifunctions.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::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;istatements.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) { 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_name_defines.has(vnode->name)) { *p_actions.usage_flag_pointers[vnode->name]=true; used_name_defines.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: 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;iarguments.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; 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_RETURN) { if (cfnode->blocks.size()) { code="return "+_dump_node_code(cfnode->blocks[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; } Error ShaderCompilerGLES3::compile(VS::ShaderMode p_mode, const String& p_code, IdentifierActions* p_actions, const String &p_path,GeneratedCode& r_gen_code) { Error err = parser.compile(p_code,ShaderTypes::get_singleton()->get_functions(p_mode),ShaderTypes::get_singleton()->get_modes(p_mode)); if (err!=OK) { #if 1 Vector shader = p_code.split("\n"); for(int i=0;i func_list; ShaderLanguage::get_builtin_funcs(&func_list); for (List::Element *E=func_list.front();E;E=E->next()) { internal_functions.insert(E->get()); } }