diff --git a/internal/shaderir/glsl.go b/internal/shaderir/glsl.go index a426f99e7..07b630453 100644 --- a/internal/shaderir/glsl.go +++ b/internal/shaderir/glsl.go @@ -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: diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index efb586682..1861e3a99 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -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, diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index 688b01947..df88d285e 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -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 = "?:" )