shaderir: Allow specifying op for 'for' loop

This commit is contained in:
Hajime Hoshi 2020-05-16 15:51:54 +09:00
parent 9f4985943c
commit d4d43ce193
3 changed files with 40 additions and 24 deletions

View File

@ -177,14 +177,27 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
localVarIndex++
var delta string
switch s.ForDelta {
case 0:
delta = fmt.Sprintf("?(unexpected delta: %d)", 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)
if s.ForDelta > 0 {
delta = fmt.Sprintf("l%d += %d", v, s.ForDelta)
} else {
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))
var op string
switch s.ForOp {
case LessThan, LessEqual, GreaterThan, GreaterEqual, Equal, NotEqual:
op = string(s.ForOp)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(s.ForOp))
}
lines = append(lines, fmt.Sprintf("%sfor (int l%d = %d; l%d %s %d; %s) {", idt, v, s.ForInit, v, op, s.ForEnd, delta))
lines = append(lines, p.glslBlock(&s.Blocks[0], f, level+1, localVarIndex)...)
lines = append(lines, fmt.Sprintf("%s}", idt))
case Continue:

View File

@ -49,12 +49,13 @@ func ifStmt(cond Expr, block Block, elseBlock Block) Stmt {
}
}
func forStmt(init, end, delta int, block Block) Stmt {
func forStmt(init, end int, op Op, delta int, block Block) Stmt {
return Stmt{
Type: For,
Blocks: []Block{block},
ForInit: init,
ForEnd: end,
ForOp: op,
ForDelta: delta,
}
}
@ -289,7 +290,7 @@ varying vec3 V0;`,
nil,
ifStmt(
binaryExpr(
Eq,
Equal,
varNameExpr(Local, 0),
floatExpr(0),
),
@ -338,6 +339,7 @@ varying vec3 V0;`,
forStmt(
0,
100,
LessThan,
1,
block(
nil,

View File

@ -45,6 +45,7 @@ type Stmt struct {
Blocks []Block
ForInit int
ForEnd int
ForOp Op
ForDelta int
}
@ -103,24 +104,24 @@ const (
type Op string
const (
Add Op = "+"
Sub Op = "-"
Neg Op = "!"
Mul Op = "*"
Div Op = "/"
Mod Op = "%"
LShift Op = "<<"
RShift Op = ">>"
LT Op = "<"
LE Op = "<="
GT Op = ">"
GE Op = ">="
Eq Op = "=="
NE Op = "!="
And Op = "&"
Xor Op = "^"
Or Op = "|"
AndAnd Op = "&&"
OrOr Op = "||"
Cond Op = "?:"
Add Op = "+"
Sub Op = "-"
Neg Op = "!"
Mul Op = "*"
Div Op = "/"
Mod Op = "%"
LeftShift Op = "<<"
RightShift Op = ">>"
LessThan Op = "<"
LessEqual Op = "<="
GreaterThan Op = ">"
GreaterEqual Op = ">="
Equal Op = "=="
NotEqual Op = "!="
And Op = "&"
Xor Op = "^"
Or Op = "|"
AndAnd Op = "&&"
OrOr Op = "||"
Condition Op = "?:"
)