diff --git a/internal/shaderir/glsl.go b/internal/shaderir/glsl.go index 07b630453..438ae5262 100644 --- a/internal/shaderir/glsl.go +++ b/internal/shaderir/glsl.go @@ -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])) diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index 1861e3a99..c502acfae 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -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{ { diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index df88d285e..466eeb580 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -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 = "?:" )