shader: Bug fix: Wrong local variable names at for-loop

Fixes #1245
This commit is contained in:
Hajime Hoshi 2020-07-13 22:49:11 +09:00
parent 0e6f4fccc0
commit 385ff8efdf
4 changed files with 74 additions and 36 deletions

View File

@ -0,0 +1,8 @@
void main(void) {
vec4 l0 = vec4(0);
for (int l1 = 0; l1 < 4; l1++) {
(l0).x = ((l0).x) + ((l1) * (1.000000000e-02));
}
gl_FragColor = l0;
return;
}

View File

@ -0,0 +1,10 @@
attribute vec2 A0;
void main(void) {
vec4 l0 = vec4(0);
for (int l1 = 0; l1 < 4; l1++) {
(l0).x = ((l0).x) + ((l1) * (1.000000000e-02));
}
gl_Position = l0;
return;
}

17
internal/shader/testdata/issue1245.go vendored Normal file
View File

@ -0,0 +1,17 @@
package main
func Vertex(position vec2) vec4 {
var v vec4
for i := 0; i < 4.0; i++ {
v.x += i * 0.01
}
return v
}
func Fragment(position vec4) vec4 {
var v vec4
for i := 0; i < 4.0; i++ {
v.x += i * 0.01
}
return v
}

View File

@ -260,29 +260,7 @@ func constantToNumberLiteral(t ConstType, v constant.Value) string {
return fmt.Sprintf("?(unexpected literal: %s)", v) return fmt.Sprintf("?(unexpected literal: %s)", v)
} }
func (p *Program) glslBlock(topBlock, block *Block, level int, localVarIndex int) []string { func (p *Program) localVariableName(topBlock *Block, idx int) string {
idt := strings.Repeat("\t", level+1)
var lines []string
for _, t := range block.LocalVars {
// The type is None e.g., when the variable is a for-loop counter.
if t.Main != None {
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, p.glslVarDecl(&t, fmt.Sprintf("l%d", localVarIndex)), p.glslVarInit(&t)))
}
localVarIndex++
}
var glslExpr func(e *Expr) string
glslExpr = func(e *Expr) string {
switch e.Type {
case NumberExpr:
return constantToNumberLiteral(e.ConstType, e.Const)
case UniformVariable:
return fmt.Sprintf("U%d", e.Index)
case TextureVariable:
return fmt.Sprintf("T%d", e.Index)
case LocalVariable:
idx := e.Index
switch topBlock { switch topBlock {
case &p.VertexFunc.Block: case &p.VertexFunc.Block:
na := len(p.Attributes) na := len(p.Attributes)
@ -312,6 +290,31 @@ func (p *Program) glslBlock(topBlock, block *Block, level int, localVarIndex int
default: default:
return fmt.Sprintf("l%d", idx) return fmt.Sprintf("l%d", idx)
} }
}
func (p *Program) glslBlock(topBlock, block *Block, level int, localVarIndex int) []string {
idt := strings.Repeat("\t", level+1)
var lines []string
for _, t := range block.LocalVars {
// The type is None e.g., when the variable is a for-loop counter.
if t.Main != None {
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, p.glslVarDecl(&t, fmt.Sprintf("l%d", localVarIndex)), p.glslVarInit(&t)))
}
localVarIndex++
}
var glslExpr func(e *Expr) string
glslExpr = func(e *Expr) string {
switch e.Type {
case NumberExpr:
return constantToNumberLiteral(e.ConstType, e.Const)
case UniformVariable:
return fmt.Sprintf("U%d", e.Index)
case TextureVariable:
return fmt.Sprintf("T%d", e.Index)
case LocalVariable:
return p.localVariableName(topBlock, e.Index)
case StructMember: case StructMember:
return fmt.Sprintf("M%d", e.Index) return fmt.Sprintf("M%d", e.Index)
case BuiltinFuncExpr: case BuiltinFuncExpr:
@ -380,22 +383,22 @@ func (p *Program) glslBlock(topBlock, block *Block, level int, localVarIndex int
ct = ConstTypeFloat ct = ConstTypeFloat
} }
v := s.ForVarIndex v := p.localVariableName(topBlock, s.ForVarIndex)
var delta string var delta string
switch val, _ := constant.Float64Val(s.ForDelta); val { switch val, _ := constant.Float64Val(s.ForDelta); val {
case 0: case 0:
delta = fmt.Sprintf("?(unexpected delta: %v)", s.ForDelta) delta = fmt.Sprintf("?(unexpected delta: %v)", s.ForDelta)
case 1: case 1:
delta = fmt.Sprintf("l%d++", v) delta = fmt.Sprintf("%s++", v)
case -1: case -1:
delta = fmt.Sprintf("l%d--", v) delta = fmt.Sprintf("%s--", v)
default: default:
d := s.ForDelta d := s.ForDelta
if val > 0 { if val > 0 {
delta = fmt.Sprintf("l%d += %s", v, constantToNumberLiteral(ct, d)) delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d))
} else { } else {
d = constant.UnaryOp(token.SUB, d, 0) d = constant.UnaryOp(token.SUB, d, 0)
delta = fmt.Sprintf("l%d -= %s", v, constantToNumberLiteral(ct, d)) delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d))
} }
} }
var op string var op string
@ -409,7 +412,7 @@ func (p *Program) glslBlock(topBlock, block *Block, level int, localVarIndex int
t := s.ForVarType.Main t := s.ForVarType.Main
init := constantToNumberLiteral(ct, s.ForInit) init := constantToNumberLiteral(ct, s.ForInit)
end := constantToNumberLiteral(ct, s.ForEnd) end := constantToNumberLiteral(ct, s.ForEnd)
lines = append(lines, fmt.Sprintf("%sfor (%s l%d = %s; l%d %s %s; %s) {", idt, t.Glsl(), v, init, v, op, end, delta)) lines = append(lines, fmt.Sprintf("%sfor (%s %s = %s; %s %s %s; %s) {", idt, t.Glsl(), v, init, v, op, end, delta))
lines = append(lines, p.glslBlock(topBlock, &s.Blocks[0], level+1, localVarIndex)...) lines = append(lines, p.glslBlock(topBlock, &s.Blocks[0], level+1, localVarIndex)...)
lines = append(lines, fmt.Sprintf("%s}", idt)) lines = append(lines, fmt.Sprintf("%s}", idt))
case Continue: case Continue: