mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
shaderir: Implement 'if'
This commit is contained in:
parent
71ee622997
commit
e6d78abd11
@ -119,8 +119,8 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
|
||||
var glslExpr func(e *Expr) string
|
||||
glslExpr = func(e *Expr) string {
|
||||
switch e.Type {
|
||||
case Literal:
|
||||
return e.Value
|
||||
case Numeric:
|
||||
return fmt.Sprintf("%.9e", e.Num)
|
||||
case VarName:
|
||||
switch e.Variable.Type {
|
||||
case Uniform:
|
||||
@ -135,7 +135,7 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
|
||||
return fmt.Sprintf("?(unexpected variable type: %d)", e.Variable.Type)
|
||||
}
|
||||
case Ident:
|
||||
return e.Value
|
||||
return e.Ident
|
||||
case Unary:
|
||||
return fmt.Sprintf("%s(%s)", e.Op, glslExpr(&e.Exprs[0]))
|
||||
case Binary:
|
||||
@ -163,7 +163,13 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
|
||||
case Assign:
|
||||
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, glslExpr(&s.Exprs[0]), glslExpr(&s.Exprs[1])))
|
||||
case If:
|
||||
panic("not implemented")
|
||||
lines = append(lines, fmt.Sprintf("%sif (%s) {", idt, glslExpr(&s.Exprs[0])))
|
||||
lines = append(lines, p.glslBlock(s.Block, f, level+1, localVarIndex)...)
|
||||
if s.ElseBlock != nil {
|
||||
lines = append(lines, fmt.Sprintf("%s} else {", idt))
|
||||
lines = append(lines, p.glslBlock(s.ElseBlock, f, level+1, localVarIndex)...)
|
||||
}
|
||||
lines = append(lines, fmt.Sprintf("%s}", idt))
|
||||
case For:
|
||||
panic("not implemented")
|
||||
case Continue:
|
||||
|
@ -27,6 +27,22 @@ func assignStmt(lhs Expr, rhs Expr) Stmt {
|
||||
}
|
||||
}
|
||||
|
||||
func ifStmt(cond Expr, block *Block, elseBlock *Block) Stmt {
|
||||
return Stmt{
|
||||
Type: If,
|
||||
Exprs: []Expr{cond},
|
||||
Block: block,
|
||||
ElseBlock: elseBlock,
|
||||
}
|
||||
}
|
||||
|
||||
func numericExpr(value float64) Expr {
|
||||
return Expr{
|
||||
Type: Numeric,
|
||||
Num: value,
|
||||
}
|
||||
}
|
||||
|
||||
func varNameExpr(vt VariableType, index int) Expr {
|
||||
return Expr{
|
||||
Type: VarName,
|
||||
@ -224,7 +240,8 @@ varying vec3 V0;`,
|
||||
Stmts: []Stmt{
|
||||
assignStmt(
|
||||
varNameExpr(Local, 2),
|
||||
binaryExpr(Add,
|
||||
binaryExpr(
|
||||
Add,
|
||||
varNameExpr(Local, 0),
|
||||
varNameExpr(Local, 1),
|
||||
),
|
||||
@ -236,6 +253,57 @@ varying vec3 V0;`,
|
||||
},
|
||||
Glsl: `void F0(in float l0, in float l1, out float l2) {
|
||||
l2 = (l0) + (l1);
|
||||
}`,
|
||||
},
|
||||
{
|
||||
Name: "FuncIf",
|
||||
Program: Program{
|
||||
Funcs: []Func{
|
||||
{
|
||||
Name: "F0",
|
||||
InParams: []Type{
|
||||
{Main: Float},
|
||||
{Main: Float},
|
||||
},
|
||||
OutParams: []Type{
|
||||
{Main: Float},
|
||||
},
|
||||
Block: Block{
|
||||
Stmts: []Stmt{
|
||||
ifStmt(
|
||||
binaryExpr(
|
||||
Eq,
|
||||
varNameExpr(Local, 0),
|
||||
numericExpr(0),
|
||||
),
|
||||
&Block{
|
||||
Stmts: []Stmt{
|
||||
assignStmt(
|
||||
varNameExpr(Local, 2),
|
||||
varNameExpr(Local, 0),
|
||||
),
|
||||
},
|
||||
},
|
||||
&Block{
|
||||
Stmts: []Stmt{
|
||||
assignStmt(
|
||||
varNameExpr(Local, 2),
|
||||
varNameExpr(Local, 1),
|
||||
),
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Glsl: `void F0(in float l0, in float l1, out float l2) {
|
||||
if ((l0) == (0.000000000e+00)) {
|
||||
l2 = l0;
|
||||
} else {
|
||||
l2 = l1;
|
||||
}
|
||||
}`,
|
||||
},
|
||||
}
|
||||
|
@ -42,11 +42,10 @@ type Block struct {
|
||||
type Stmt struct {
|
||||
Type StmtType
|
||||
Exprs []Expr
|
||||
Condition Expr
|
||||
ElseExprs []Expr
|
||||
Block *Block
|
||||
ElseBlock *Block
|
||||
ForInit Expr
|
||||
ForRest Expr
|
||||
Block *Block
|
||||
}
|
||||
|
||||
type StmtType int
|
||||
@ -67,14 +66,15 @@ type Expr struct {
|
||||
Type ExprType
|
||||
Exprs []Expr
|
||||
Variable Variable
|
||||
Value string
|
||||
Num float64
|
||||
Ident string
|
||||
Op Op
|
||||
}
|
||||
|
||||
type ExprType int
|
||||
|
||||
const (
|
||||
Literal ExprType = iota
|
||||
Numeric ExprType = iota
|
||||
VarName
|
||||
Ident
|
||||
Unary
|
||||
|
Loading…
Reference in New Issue
Block a user