shaderir: Implement call

This commit is contained in:
Hajime Hoshi 2020-05-16 19:10:12 +09:00
parent 5b69e81bd7
commit 57d80c185e
3 changed files with 59 additions and 2 deletions

View File

@ -152,8 +152,11 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
case Selection:
return fmt.Sprintf("(%s) ? (%s) : (%s)", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]), glslExpr(&e.Exprs[2]))
case Call:
// TODO: Take multiple args
return fmt.Sprintf("(%s).(%s)", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
var args []string
for _, exp := range e.Exprs {
args = append(args, glslExpr(&exp))
}
return fmt.Sprintf("%s(%s)", e.Ident, strings.Join(args, ", "))
case FieldSelector:
return fmt.Sprintf("(%s).%s", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
case Index:

View File

@ -27,6 +27,13 @@ func block(localVars []Type, stmts ...Stmt) Block {
}
}
func exprStmt(expr Expr) Stmt {
return Stmt{
Type: ExprStmt,
Exprs: []Expr{expr},
}
}
func blockStmt(block Block) Stmt {
return Stmt{
Type: BlockStmt,
@ -99,6 +106,14 @@ func selectionExpr(cond, a, b Expr) Expr {
}
}
func callExpr(name string, args ...Expr) Expr {
return Expr{
Type: Call,
Ident: name,
Exprs: args,
}
}
func fieldSelectorExpr(a, b Expr) Expr {
return Expr{
Type: FieldSelector,
@ -324,6 +339,43 @@ varying vec3 V0;`,
},
Glsl: `void F0(in bool l0, in float l1, in float l2, out float l3) {
l3 = (l0) ? (l1) : (l2);
}`,
},
{
Name: "Call",
Program: Program{
Funcs: []Func{
{
Name: "F0",
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Vec2},
},
Block: block(
nil,
exprStmt(
callExpr(
"F1",
),
),
assignStmt(
varNameExpr(Local, 2),
callExpr(
"F2",
varNameExpr(Local, 0),
varNameExpr(Local, 1),
),
),
),
},
},
},
Glsl: `void F0(in float l0, in float l1, out vec2 l2) {
F1();
l2 = F2(l0, l1);
}`,
},
{

View File

@ -26,6 +26,8 @@ type Program struct {
structTypes []Type
}
// TODO: How to avoid the name with existing functions?
type Func struct {
Name string
InParams []Type