shaderir: Add for-loop

This commit is contained in:
Hajime Hoshi 2020-05-16 03:10:03 +09:00
parent b519ad2853
commit 3cffe88334
4 changed files with 72 additions and 6 deletions

View File

@ -171,7 +171,20 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
}
lines = append(lines, fmt.Sprintf("%s}", idt))
case For:
panic("not implemented")
v := localVarIndex
localVarIndex++
var delta string
switch s.ForDelta {
case 1:
delta = fmt.Sprintf("l%d++", v)
case -1:
delta = fmt.Sprintf("l%d--", v)
default:
delta = fmt.Sprintf("l%d += %d", v, s.ForDelta)
}
lines = append(lines, fmt.Sprintf("%sfor (int l%d = %d; l%d < %d; %s) {", idt, v, s.ForInit, v, s.ForEnd, delta))
lines = append(lines, p.glslBlock(&s.Blocks[0], f, level+1, localVarIndex)...)
lines = append(lines, fmt.Sprintf("%s}", idt))
case Continue:
lines = append(lines, idt+"continue;")
case Break:

View File

@ -49,6 +49,16 @@ func ifStmt(cond Expr, block Block, elseBlock Block) Stmt {
}
}
func forStmt(init, end, delta int, block Block) Stmt {
return Stmt{
Type: For,
Blocks: []Block{block},
ForInit: init,
ForEnd: end,
ForDelta: delta,
}
}
func numericExpr(value float64) Expr {
return Expr{
Type: Numeric,
@ -308,6 +318,43 @@ varying vec3 V0;`,
} else {
l2 = l1;
}
}`,
},
{
Name: "FuncFor",
Program: Program{
Funcs: []Func{
{
Name: "F0",
InParams: []Type{
{Main: Float},
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
Block: block(
nil,
forStmt(
0,
100,
1,
block(
nil,
assignStmt(
varNameExpr(Local, 2),
varNameExpr(Local, 0),
),
),
),
),
},
},
},
Glsl: `void F0(in float l0, in float l1, out float l2) {
for (int l3 = 0; l3 < 100; l3++) {
l2 = l0;
}
}`,
},
}

View File

@ -40,11 +40,12 @@ type Block struct {
}
type Stmt struct {
Type StmtType
Exprs []Expr
Blocks []Block
ForInit Expr
ForRest Expr
Type StmtType
Exprs []Expr
Blocks []Block
ForInit int
ForEnd int
ForDelta int
}
type StmtType int

View File

@ -31,6 +31,8 @@ func (t *Type) serialize() string {
return "none"
case Bool:
return "bool"
case Int:
return "int"
case Float:
return "float"
case Vec2:
@ -68,6 +70,7 @@ type BasicType int
const (
None BasicType = iota
Bool
Int
Float
Vec2
Vec3
@ -86,6 +89,8 @@ func (t BasicType) Glsl() string {
return "?(none)"
case Bool:
return "bool"
case Int:
return "Int"
case Float:
return "float"
case Vec2: