godot/main/tests/test_shader_lang.cpp

343 lines
10 KiB
C++
Raw Normal View History

2014-02-10 01:10:30 +00:00
/*************************************************************************/
/* test_shader_lang.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
2014-02-10 01:10:30 +00:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "test_shader_lang.h"
#include "os/file_access.h"
2014-02-10 01:10:30 +00:00
#include "os/main_loop.h"
#include "os/os.h"
#include "drivers/gles2/shader_compiler_gles2.h"
#include "print_string.h"
2014-02-10 01:10:30 +00:00
#include "scene/gui/control.h"
#include "scene/gui/text_edit.h"
#include "servers/visual/shader_language.h"
typedef ShaderLanguage SL;
namespace TestShaderLang {
static String _mktab(int p_level) {
String tb;
for (int i = 0; i < p_level; i++) {
tb += "\t";
2014-02-10 01:10:30 +00:00
}
return tb;
}
static String _typestr(SL::DataType p_type) {
switch (p_type) {
2014-02-10 01:10:30 +00:00
case SL::TYPE_VOID: return "void";
case SL::TYPE_BOOL: return "bool";
case SL::TYPE_FLOAT: return "float";
case SL::TYPE_VEC2: return "vec2";
case SL::TYPE_VEC3: return "vec3";
case SL::TYPE_VEC4: return "vec4";
case SL::TYPE_MAT3: return "mat3";
case SL::TYPE_MAT4: return "mat4";
case SL::TYPE_TEXTURE: return "texture";
case SL::TYPE_CUBEMAP: return "cubemap";
default: {}
}
return "";
}
static String _opstr(SL::Operator p_op) {
switch (p_op) {
2014-02-10 01:10:30 +00:00
case SL::OP_ASSIGN: return "=";
case SL::OP_ADD: return "+";
case SL::OP_SUB: return "-";
case SL::OP_MUL: return "*";
case SL::OP_DIV: return "/";
case SL::OP_ASSIGN_ADD: return "+=";
case SL::OP_ASSIGN_SUB: return "-=";
case SL::OP_ASSIGN_MUL: return "*=";
case SL::OP_ASSIGN_DIV: return "/=";
case SL::OP_NEG: return "-";
case SL::OP_NOT: return "!";
case SL::OP_CMP_EQ: return "==";
case SL::OP_CMP_NEQ: return "!=";
case SL::OP_CMP_LEQ: return "<=";
case SL::OP_CMP_GEQ: return ">=";
case SL::OP_CMP_LESS: return "<";
case SL::OP_CMP_GREATER: return ">";
case SL::OP_CMP_OR: return "||";
case SL::OP_CMP_AND: return "&&";
default: return "";
}
return "";
}
static String dump_node_code(SL::Node *p_node, int p_level) {
2014-02-10 01:10:30 +00:00
String code;
switch (p_node->type) {
2014-02-10 01:10:30 +00:00
case SL::Node::TYPE_PROGRAM: {
SL::ProgramNode *pnode = (SL::ProgramNode *)p_node;
2014-02-10 01:10:30 +00:00
for (Map<StringName, SL::Uniform>::Element *E = pnode->uniforms.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
String ucode = "uniform ";
ucode += _typestr(E->get().type) + "=" + String(E->get().default_value) + "\n";
code += ucode;
2014-02-10 01:10:30 +00:00
}
for (int i = 0; i < pnode->functions.size(); i++) {
2014-02-10 01:10:30 +00:00
SL::FunctionNode *fnode = pnode->functions[i].function;
2014-02-10 01:10:30 +00:00
String header;
header = _typestr(fnode->return_type) + " " + fnode->name + "(";
for (int i = 0; i < fnode->arguments.size(); i++) {
2014-02-10 01:10:30 +00:00
if (i > 0)
header += ", ";
header += _typestr(fnode->arguments[i].type) + " " + fnode->arguments[i].name;
2014-02-10 01:10:30 +00:00
}
header += ") {\n";
code += header;
code += dump_node_code(fnode->body, p_level + 1);
code += "}\n";
2014-02-10 01:10:30 +00:00
}
code += dump_node_code(pnode->body, p_level);
2014-02-10 01:10:30 +00:00
} break;
case SL::Node::TYPE_FUNCTION: {
} break;
case SL::Node::TYPE_BLOCK: {
SL::BlockNode *bnode = (SL::BlockNode *)p_node;
2014-02-10 01:10:30 +00:00
//variables
for (Map<StringName, SL::DataType>::Element *E = bnode->variables.front(); E; E = E->next()) {
2014-02-10 01:10:30 +00:00
code += _mktab(p_level) + _typestr(E->value()) + " " + E->key() + ";\n";
2014-02-10 01:10:30 +00:00
}
for (int i = 0; i < bnode->statements.size(); i++) {
2014-02-10 01:10:30 +00:00
code += _mktab(p_level) + dump_node_code(bnode->statements[i], p_level) + ";\n";
2014-02-10 01:10:30 +00:00
}
} break;
case SL::Node::TYPE_VARIABLE: {
SL::VariableNode *vnode = (SL::VariableNode *)p_node;
code = vnode->name;
2014-02-10 01:10:30 +00:00
} 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 = cnode->value; break;
case SL::TYPE_VEC2: {
Vector2 v = cnode->value;
code = "vec2(" + rtos(v.x) + ", " + rtos(v.y) + ")";
} break;
case SL::TYPE_VEC3: {
Vector3 v = cnode->value;
code = "vec3(" + rtos(v.x) + ", " + rtos(v.y) + ", " + rtos(v.z) + ")";
} break;
case SL::TYPE_VEC4: {
Plane v = cnode->value;
code = "vec4(" + rtos(v.normal.x) + ", " + rtos(v.normal.y) + ", " + rtos(v.normal.z) + ", " + rtos(v.d) + ")";
} break;
case SL::TYPE_MAT3: {
Matrix3 x = cnode->value;
code = "mat3( vec3(" + rtos(x.get_axis(0).x) + ", " + rtos(x.get_axis(0).y) + ", " + rtos(x.get_axis(0).z) + "), vec3(" + rtos(x.get_axis(1).x) + ", " + rtos(x.get_axis(1).y) + ", " + rtos(x.get_axis(1).z) + "), vec3(" + rtos(x.get_axis(2).x) + ", " + rtos(x.get_axis(2).y) + ", " + rtos(x.get_axis(2).z) + "))";
} break;
case SL::TYPE_MAT4: {
Transform x = cnode->value;
code = "mat4( vec3(" + rtos(x.basis.get_axis(0).x) + ", " + rtos(x.basis.get_axis(0).y) + ", " + rtos(x.basis.get_axis(0).z) + "), vec3(" + rtos(x.basis.get_axis(1).x) + ", " + rtos(x.basis.get_axis(1).y) + ", " + rtos(x.basis.get_axis(1).z) + "), vec3(" + rtos(x.basis.get_axis(2).x) + ", " + rtos(x.basis.get_axis(2).y) + ", " + rtos(x.basis.get_axis(2).z) + "), vec3(" + rtos(x.origin.x) + ", " + rtos(x.origin.y) + ", " + rtos(x.origin.z) + "))";
} break;
default: code = "<error: " + Variant::get_type_name(cnode->value.get_type()) + " (" + itos(cnode->datatype) + ">";
2014-02-10 01:10:30 +00:00
}
} break;
case SL::Node::TYPE_OPERATOR: {
SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
2014-02-10 01:10:30 +00:00
switch (onode->op) {
2014-02-10 01:10:30 +00:00
case SL::OP_ASSIGN:
case SL::OP_ASSIGN_ADD:
case SL::OP_ASSIGN_SUB:
case SL::OP_ASSIGN_MUL:
case SL::OP_ASSIGN_DIV:
code = dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level);
2014-02-10 01:10:30 +00:00
break;
case SL::OP_ADD:
case SL::OP_SUB:
case SL::OP_MUL:
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:
code = "(" + dump_node_code(onode->arguments[0], p_level) + _opstr(onode->op) + dump_node_code(onode->arguments[1], p_level) + ")";
2014-02-10 01:10:30 +00:00
break;
case SL::OP_NEG:
case SL::OP_NOT:
code = _opstr(onode->op) + dump_node_code(onode->arguments[0], p_level);
2014-02-10 01:10:30 +00:00
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);
2014-02-10 01:10:30 +00:00
}
code += ")";
2014-02-10 01:10:30 +00:00
break;
default: {}
}
} break;
case SL::Node::TYPE_CONTROL_FLOW: {
SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
if (cfnode->flow_op == SL::FLOW_OP_IF) {
2014-02-10 01:10:30 +00:00
code += "if (" + dump_node_code(cfnode->statements[0], p_level) + ") {\n";
code += dump_node_code(cfnode->statements[1], p_level + 1);
if (cfnode->statements.size() == 3) {
2014-02-10 01:10:30 +00:00
code += "} else {\n";
code += dump_node_code(cfnode->statements[2], p_level + 1);
2014-02-10 01:10:30 +00:00
}
code += "}\n";
2014-02-10 01:10:30 +00:00
} else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
2014-02-10 01:10:30 +00:00
if (cfnode->statements.size()) {
code = "return " + dump_node_code(cfnode->statements[0], p_level);
2014-02-10 01:10:30 +00:00
} else {
code = "return";
2014-02-10 01:10:30 +00:00
}
}
} break;
case SL::Node::TYPE_MEMBER: {
SL::MemberNode *mnode = (SL::MemberNode *)p_node;
code = dump_node_code(mnode->owner, p_level) + "." + mnode->name;
2014-02-10 01:10:30 +00:00
} break;
}
return code;
}
static Error recreate_code(void *p_str, SL::ProgramNode *p_program) {
2014-02-10 01:10:30 +00:00
print_line("recr");
String *str = (String *)p_str;
2014-02-10 01:10:30 +00:00
*str = dump_node_code(p_program, 0);
2014-02-10 01:10:30 +00:00
return OK;
2014-02-10 01:10:30 +00:00
}
MainLoop *test() {
2014-02-10 01:10:30 +00:00
List<String> cmdlargs = OS::get_singleton()->get_cmdline_args();
if (cmdlargs.empty()) {
//try editor!
return NULL;
}
String test = cmdlargs.back()->get();
FileAccess *fa = FileAccess::open(test, FileAccess::READ);
2014-02-10 01:10:30 +00:00
2016-03-08 23:00:52 +00:00
if (!fa) {
2014-02-10 01:10:30 +00:00
ERR_FAIL_V(NULL);
}
String code;
while (true) {
2014-02-10 01:10:30 +00:00
CharType c = fa->get_8();
if (fa->eof_reached())
break;
code += c;
2014-02-10 01:10:30 +00:00
}
int errline;
int errcol;
String error;
print_line(SL::lex_debug(code));
Error err = SL::compile(code, ShaderLanguage::SHADER_MATERIAL_FRAGMENT, NULL, NULL, &error, &errline, &errcol);
2014-02-10 01:10:30 +00:00
if (err) {
print_line("Error: " + itos(errline) + ":" + itos(errcol) + " " + error);
2014-02-10 01:10:30 +00:00
return NULL;
}
print_line("Compile OK! - pretty printing");
String rcode;
err = SL::compile(code, ShaderLanguage::SHADER_MATERIAL_FRAGMENT, recreate_code, &rcode, &error, &errline, &errcol);
2014-02-10 01:10:30 +00:00
if (!err) {
print_line(rcode);
}
ShaderCompilerGLES2 comp;
String codeline, globalsline;
2014-02-10 01:10:30 +00:00
SL::VarInfo vi;
vi.name = "mongs";
vi.type = SL::TYPE_VEC3;
2014-02-10 01:10:30 +00:00
ShaderCompilerGLES2::Flags fl;
comp.compile(code, ShaderLanguage::SHADER_MATERIAL_FRAGMENT, codeline, globalsline, fl);
2014-02-10 01:10:30 +00:00
return NULL;
}
}