diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 6f6a28460..b9c672c50 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -440,9 +440,7 @@ func (c *compileContext) glslBlock(p *shaderir.Program, topBlock, block *shaderi end := constantToNumberLiteral(ct, s.ForEnd) t0, t1 := typeString(&t) lines = append(lines, fmt.Sprintf("%sfor (%s %s%s = %s; %s %s %s; %s) {", idt, t0, v, t1, init, v, op, end, delta)) - // For's variable is special and defiend in the for statement. - // Add 1 to the number of the local variables. - lines = append(lines, c.glslBlock(p, topBlock, s.Blocks[0], level+1, localVarIndex+1)...) + lines = append(lines, c.glslBlock(p, topBlock, s.Blocks[0], level+1, localVarIndex)...) lines = append(lines, fmt.Sprintf("%s}", idt)) case shaderir.Continue: lines = append(lines, idt+"continue;") diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index 28cd5ca5e..29211d900 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -657,7 +657,9 @@ void F0(in float l0, in float l1, out float l2) { {Main: Float}, }, Block: block( - nil, + []Type{ + {}, + }, forStmt( Type{Main: Int}, 3, @@ -707,7 +709,9 @@ void F0(in float l0, in float l1, out float l2) { {Main: Float}, }, Block: block( - nil, + []Type{ + {}, + }, forStmt( Type{Main: Int}, 3, @@ -775,7 +779,10 @@ void F0(float l0, float l1, thread float& l2) { {Main: Float}, }, Block: block( - nil, + []Type{ + {}, + {}, + }, forStmt( Type{Main: Int}, 3, @@ -789,13 +796,13 @@ void F0(float l0, float l1, thread float& l2) { }, assignStmt( localVariableExpr(2), - localVariableExpr(4), + localVariableExpr(5), ), ), ), forStmt( Type{Main: Float}, - 3, + 4, 0, 100, LessThanOp, @@ -806,7 +813,7 @@ void F0(float l0, float l1, thread float& l2) { }, assignStmt( localVariableExpr(2), - localVariableExpr(4), + localVariableExpr(5), ), ), ), @@ -818,12 +825,12 @@ void F0(float l0, float l1, thread float& l2) { void F0(in float l0, in float l1, out float l2) { for (int l3 = 0; l3 < 100; l3++) { - int l4 = 0; - l2 = l4; + int l5 = 0; + l2 = l5; } - for (float l3 = 0.0; l3 < 100.0; l3++) { - int l4 = 0; - l2 = l4; + for (float l4 = 0.0; l4 < 100.0; l4++) { + int l5 = 0; + l2 = l5; } }`, GlslFS: glslPrelude + ` @@ -831,12 +838,12 @@ void F0(in float l0, in float l1, out float l2); void F0(in float l0, in float l1, out float l2) { for (int l3 = 0; l3 < 100; l3++) { - int l4 = 0; - l2 = l4; + int l5 = 0; + l2 = l5; } - for (float l3 = 0.0; l3 < 100.0; l3++) { - int l4 = 0; - l2 = l4; + for (float l4 = 0.0; l4 < 100.0; l4++) { + int l5 = 0; + l2 = l5; } }`, Metal: `#include @@ -849,12 +856,12 @@ void F0(float l0, float l1, thread float& l2); void F0(float l0, float l1, thread float& l2) { for (int l3 = 0; l3 < 100; l3++) { - int l4 = 0; - l2 = l4; + int l5 = 0; + l2 = l5; } - for (float l3 = 0.0; l3 < 100.0; l3++) { - int l4 = 0; - l2 = l4; + for (float l4 = 0.0; l4 < 100.0; l4++) { + int l5 = 0; + l2 = l5; } }`, }, diff --git a/internal/shaderir/metal/metal.go b/internal/shaderir/metal/metal.go index f9712dcd8..58677787d 100644 --- a/internal/shaderir/metal/metal.go +++ b/internal/shaderir/metal/metal.go @@ -448,9 +448,7 @@ func (c *compileContext) metalBlock(p *shaderir.Program, topBlock, block *shader end := constantToNumberLiteral(ct, s.ForEnd) ts := typeString(&t, false, false) lines = append(lines, fmt.Sprintf("%sfor (%s %s = %s; %s %s %s; %s) {", idt, ts, v, init, v, op, end, delta)) - // For's variable is special and defiend in the for statement. - // Add 1 to the number of the local variables. - lines = append(lines, c.metalBlock(p, topBlock, s.Blocks[0], level+1, localVarIndex+1)...) + lines = append(lines, c.metalBlock(p, topBlock, s.Blocks[0], level+1, localVarIndex)...) lines = append(lines, fmt.Sprintf("%s}", idt)) case shaderir.Continue: lines = append(lines, idt+"continue;")