diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2017-03-05 16:44:50 +0100 |
commit | 5dbf1809c6e3e905b94b8764e99491e608122261 (patch) | |
tree | 5e5a5360db15d86d59ec8c6e4f7eb511388c5a9a /main/tests/test_shader_lang.cpp | |
parent | 45438e9918d421b244bfd7776a30e67dc7f2d3e3 (diff) |
A Whole New World (clang-format edition)
I can show you the code
Pretty, with proper whitespace
Tell me, coder, now when did
You last write readable code?
I can open your eyes
Make you see your bad indent
Force you to respect the style
The core devs agreed upon
A whole new world
A new fantastic code format
A de facto standard
With some sugar
Enforced with clang-format
A whole new world
A dazzling style we all dreamed of
And when we read it through
It's crystal clear
That now we're in a whole new world of code
Diffstat (limited to 'main/tests/test_shader_lang.cpp')
-rw-r--r-- | main/tests/test_shader_lang.cpp | 228 |
1 files changed, 104 insertions, 124 deletions
diff --git a/main/tests/test_shader_lang.cpp b/main/tests/test_shader_lang.cpp index 64c3853065..4ca09fe656 100644 --- a/main/tests/test_shader_lang.cpp +++ b/main/tests/test_shader_lang.cpp @@ -28,28 +28,25 @@ /*************************************************************************/ #include "test_shader_lang.h" - +#include "os/file_access.h" #include "os/main_loop.h" #include "os/os.h" -#include "os/file_access.h" +#include "print_string.h" #include "scene/gui/control.h" #include "scene/gui/text_edit.h" -#include "print_string.h" #include "servers/visual/shader_language.h" //#include "drivers/gles2/shader_compiler_gles2.h" - typedef ShaderLanguage SL; namespace TestShaderLang { - static String _mktab(int p_level) { String tb; - for(int i=0;i<p_level;i++) { - tb+="\t"; + for (int i = 0; i < p_level; i++) { + tb += "\t"; } return tb; @@ -62,11 +59,9 @@ static String _typestr(SL::DataType p_type) { return ""; } - static String _prestr(SL::DataPrecision p_pres) { - - switch(p_pres) { + switch (p_pres) { case SL::PRECISION_LOWP: return "lowp "; case SL::PRECISION_MEDIUMP: return "mediump "; case SL::PRECISION_HIGHP: return "highp "; @@ -75,60 +70,56 @@ static String _prestr(SL::DataPrecision p_pres) { return ""; } - static String _opstr(SL::Operator p_op) { return ShaderLanguage::get_operator_text(p_op); - - } - -static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value>& p_values) { - - switch(p_type) { - case SL::TYPE_BOOL: return p_values[0].boolean?"true":"false"; - case SL::TYPE_BVEC2: return String()+"bvec2("+(p_values[0].boolean?"true":"false")+(p_values[1].boolean?"true":"false")+")"; - 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")+")"; - 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")+")"; - case SL::TYPE_INT: return rtos(p_values[0].sint); - case SL::TYPE_IVEC2: return String()+"ivec2("+rtos(p_values[0].sint)+","+rtos(p_values[1].sint)+")"; - case SL::TYPE_IVEC3: return String()+"ivec3("+rtos(p_values[0].sint)+","+rtos(p_values[1].sint)+","+rtos(p_values[2].sint)+")"; - 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)+")"; - case SL::TYPE_UINT: return rtos(p_values[0].real); - case SL::TYPE_UVEC2: return String()+"uvec2("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+")"; - case SL::TYPE_UVEC3: return String()+"uvec3("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+","+rtos(p_values[2].real)+")"; - 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)+")"; - case SL::TYPE_FLOAT: return rtos(p_values[0].real); - case SL::TYPE_VEC2: return String()+"vec2("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+")"; - case SL::TYPE_VEC3: return String()+"vec3("+rtos(p_values[0].real)+","+rtos(p_values[1].real)+","+rtos(p_values[2].real)+")"; - 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)+")"; +static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) { + + switch (p_type) { + case SL::TYPE_BOOL: return p_values[0].boolean ? "true" : "false"; + case SL::TYPE_BVEC2: return String() + "bvec2(" + (p_values[0].boolean ? "true" : "false") + (p_values[1].boolean ? "true" : "false") + ")"; + 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") + ")"; + 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") + ")"; + case SL::TYPE_INT: return rtos(p_values[0].sint); + case SL::TYPE_IVEC2: return String() + "ivec2(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + ")"; + case SL::TYPE_IVEC3: return String() + "ivec3(" + rtos(p_values[0].sint) + "," + rtos(p_values[1].sint) + "," + rtos(p_values[2].sint) + ")"; + 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) + ")"; + case SL::TYPE_UINT: return rtos(p_values[0].real); + case SL::TYPE_UVEC2: return String() + "uvec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")"; + case SL::TYPE_UVEC3: return String() + "uvec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")"; + 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) + ")"; + case SL::TYPE_FLOAT: return rtos(p_values[0].real); + case SL::TYPE_VEC2: return String() + "vec2(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + ")"; + case SL::TYPE_VEC3: return String() + "vec3(" + rtos(p_values[0].real) + "," + rtos(p_values[1].real) + "," + rtos(p_values[2].real) + ")"; + 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) + ")"; default: ERR_FAIL_V(String()); } } -static String dump_node_code(SL::Node *p_node,int p_level) { +static String dump_node_code(SL::Node *p_node, int p_level) { String code; - switch(p_node->type) { + switch (p_node->type) { case SL::Node::TYPE_SHADER: { - SL::ShaderNode *pnode=(SL::ShaderNode*)p_node; + SL::ShaderNode *pnode = (SL::ShaderNode *)p_node; - for(Map<StringName,SL::ShaderNode::Uniform>::Element *E=pnode->uniforms.front();E;E=E->next()) { + 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()); + 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); + ucode += " = " + get_constant_text(E->get().type, E->get().default_value); } - static const char*hint_name[SL::ShaderNode::Uniform::HINT_MAX]={ + static const char *hint_name[SL::ShaderNode::Uniform::HINT_MAX] = { "", "color", "range", @@ -139,38 +130,36 @@ static String dump_node_code(SL::Node *p_node,int p_level) { }; if (E->get().hint) - ucode+=" : "+String(hint_name[E->get().hint]); - - code+=ucode+"\n"; + 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()); + for (Map<StringName, SL::ShaderNode::Varying>::Element *E = pnode->varyings.front(); E; E = E->next()) { - code+=vcode+"\n"; + 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++) { + for (int i = 0; i < pnode->functions.size(); i++) { - SL::FunctionNode *fnode=pnode->functions[i].function; + 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++) { + 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; + 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); + header += ")\n"; + code += header; + code += dump_node_code(fnode->body, p_level + 1); } //code+=dump_node_code(pnode->body,p_level); @@ -179,44 +168,42 @@ static String dump_node_code(SL::Node *p_node,int p_level) { } break; case SL::Node::TYPE_BLOCK: { - SL::BlockNode *bnode=(SL::BlockNode*)p_node; + 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 - 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"; + code += _mktab(p_level) + _prestr(E->get().precision) + _typestr(E->get().type) + " " + E->key() + ";\n"; } - for(int i=0;i<bnode->statements.size();i++) { + for (int i = 0; i < bnode->statements.size(); i++) { - String scode = dump_node_code(bnode->statements[i],p_level); + 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 + 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) + scode + ";\n"; } } - code+=_mktab(p_level-1)+"}\n"; - + code += _mktab(p_level - 1) + "}\n"; } break; case SL::Node::TYPE_VARIABLE: { - SL::VariableNode *vnode=(SL::VariableNode*)p_node; - code=vnode->name; + 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); + 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; - + SL::OperatorNode *onode = (SL::OperatorNode *)p_node; - switch(onode->op) { + switch (onode->op) { case SL::OP_ASSIGN: case SL::OP_ASSIGN_ADD: @@ -229,84 +216,79 @@ static String dump_node_code(SL::Node *p_node,int p_level) { 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); + 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); + 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); + 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 = 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+=")"; + code += ")"; break; default: { - code="("+dump_node_code(onode->arguments[0],p_level)+_opstr(onode->op)+dump_node_code(onode->arguments[1],p_level)+")"; + 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) { + 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) + "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); + 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) { + } else if (cfnode->flow_op == SL::FLOW_OP_RETURN) { if (cfnode->blocks.size()) { - code="return "+dump_node_code(cfnode->blocks[0],p_level); + code = "return " + dump_node_code(cfnode->blocks[0], p_level); } else { - code="return"; + 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; + SL::MemberNode *mnode = (SL::MemberNode *)p_node; + code = dump_node_code(mnode->owner, p_level) + "." + mnode->name; } break; } return code; - } -static Error recreate_code(void *p_str,SL::ShaderNode *p_program) { - +static Error recreate_code(void *p_str, SL::ShaderNode *p_program) { - String *str=(String*)p_str; + String *str = (String *)p_str; - *str=dump_node_code(p_program,0); + *str = dump_node_code(p_program, 0); return OK; } - -MainLoop* test() { +MainLoop *test() { List<String> cmdlargs = OS::get_singleton()->get_cmdline_args(); @@ -318,7 +300,7 @@ MainLoop* test() { String test = cmdlargs.back()->get(); - FileAccess *fa = FileAccess::open(test,FileAccess::READ); + FileAccess *fa = FileAccess::open(test, FileAccess::READ); if (!fa) { ERR_FAIL_V(NULL); @@ -326,36 +308,34 @@ MainLoop* test() { String code; - while(true) { + while (true) { CharType c = fa->get_8(); if (fa->eof_reached()) break; - code+=c; + code += c; } SL sl; - print_line("tokens:\n\n"+sl.token_debug(code)); + print_line("tokens:\n\n" + sl.token_debug(code)); - Map<StringName,Map<StringName,SL::DataType> > dt; - dt["fragment"]["ALBEDO"]=SL::TYPE_VEC3; + Map<StringName, Map<StringName, SL::DataType> > dt; + dt["fragment"]["ALBEDO"] = SL::TYPE_VEC3; Set<String> rm; rm.insert("popo"); - Error err = sl.compile(code,dt,rm); + Error err = sl.compile(code, dt, rm); if (err) { - print_line("Error at line: "+rtos(sl.get_error_line())+": "+sl.get_error_text()); + print_line("Error at line: " + rtos(sl.get_error_line()) + ": " + sl.get_error_text()); return NULL; } else { String code; - recreate_code(&code,sl.get_shader()); - print_line("code:\n\n"+code); + recreate_code(&code, sl.get_shader()); + print_line("code:\n\n" + code); } return NULL; } - - } |