shaderir: Implement selector operator

This commit is contained in:
Hajime Hoshi 2020-05-16 17:22:17 +09:00
parent d4d43ce193
commit af5249de4d
3 changed files with 55 additions and 7 deletions

View File

@ -139,13 +139,22 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
case Ident:
return e.Ident
case Unary:
return fmt.Sprintf("%s(%s)", e.Op, glslExpr(&e.Exprs[0]))
var op string
switch e.Op {
case Add, Sub, Neg:
op = string(e.Op)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(e.Op))
}
return fmt.Sprintf("%s(%s)", op, glslExpr(&e.Exprs[0]))
case Binary:
return fmt.Sprintf("(%s) %s (%s)", glslExpr(&e.Exprs[0]), e.Op, glslExpr(&e.Exprs[1]))
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]))
case Selector:
case FieldSelector:
return fmt.Sprintf("(%s).%s", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
case Index:
return fmt.Sprintf("(%s)[%s]", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))

View File

@ -85,6 +85,13 @@ func binaryExpr(op Op, exprs ...Expr) Expr {
}
}
func selectionExpr(cond, a, b Expr) Expr {
return Expr{
Type: Selection,
Exprs: []Expr{cond, a, b},
}
}
func TestOutput(t *testing.T) {
tests := []struct {
Name string
@ -243,7 +250,7 @@ varying vec3 V0;`,
}`,
},
{
Name: "FuncAdd",
Name: "Add",
Program: Program{
Funcs: []Func{
{
@ -274,7 +281,39 @@ varying vec3 V0;`,
}`,
},
{
Name: "FuncIf",
Name: "Selection",
Program: Program{
Funcs: []Func{
{
Name: "F0",
InParams: []Type{
{Main: Bool},
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
Block: block(
nil,
assignStmt(
varNameExpr(Local, 3),
selectionExpr(
varNameExpr(Local, 0),
varNameExpr(Local, 1),
varNameExpr(Local, 2),
),
),
),
},
},
},
Glsl: `void F0(in bool l0, in float l1, in float l2, out float l3) {
l3 = (l0) ? (l1) : (l2);
}`,
},
{
Name: "If",
Program: Program{
Funcs: []Func{
{
@ -322,7 +361,7 @@ varying vec3 V0;`,
}`,
},
{
Name: "FuncFor",
Name: "For",
Program: Program{
Funcs: []Func{
{

View File

@ -82,8 +82,9 @@ const (
Ident
Unary
Binary
Selection
Call
Selector
FieldSelector
Index
)
@ -123,5 +124,4 @@ const (
Or Op = "|"
AndAnd Op = "&&"
OrOr Op = "||"
Condition Op = "?:"
)