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++ localVarIndex++
var delta string var delta string
switch s.ForDelta { switch s.ForDelta {
case 0:
delta = fmt.Sprintf("?(unexpected delta: %d)", s.ForDelta)
case 1: case 1:
delta = fmt.Sprintf("l%d++", v) delta = fmt.Sprintf("l%d++", v)
case -1: case -1:
delta = fmt.Sprintf("l%d--", v) delta = fmt.Sprintf("l%d--", v)
default: 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, p.glslBlock(&s.Blocks[0], f, level+1, localVarIndex)...)
lines = append(lines, fmt.Sprintf("%s}", idt)) lines = append(lines, fmt.Sprintf("%s}", idt))
case Continue: 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{ return Stmt{
Type: For, Type: For,
Blocks: []Block{block}, Blocks: []Block{block},
ForInit: init, ForInit: init,
ForEnd: end, ForEnd: end,
ForOp: op,
ForDelta: delta, ForDelta: delta,
} }
} }
@ -289,7 +290,7 @@ varying vec3 V0;`,
nil, nil,
ifStmt( ifStmt(
binaryExpr( binaryExpr(
Eq, Equal,
varNameExpr(Local, 0), varNameExpr(Local, 0),
floatExpr(0), floatExpr(0),
), ),
@ -338,6 +339,7 @@ varying vec3 V0;`,
forStmt( forStmt(
0, 0,
100, 100,
LessThan,
1, 1,
block( block(
nil, nil,

View File

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