shader: Bug fix: Wrong local variable index in a block

This commit is contained in:
Hajime Hoshi 2020-08-09 23:08:24 +09:00
parent eacc9fac6a
commit a0494210c3
4 changed files with 46 additions and 3 deletions

View File

@ -240,7 +240,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
var outParams []int
for _, p := range f.ir.OutParams {
idx := len(block.vars)
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: p,
})
@ -257,7 +257,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
return nil, nil, nil, false
}
idx := len(block.vars)
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: t,
})
@ -453,7 +453,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
return nil, nil, nil, false
}
idx := len(block.vars)
idx := block.totalLocalVariableNum()
block.vars = append(block.vars, variable{
typ: t,
})

View File

@ -95,6 +95,14 @@ type block struct {
ir *shaderir.Block
}
func (b *block) totalLocalVariableNum() int {
c := len(b.vars)
if b.outer != nil {
c += b.outer.totalLocalVariableNum()
}
return c
}
func (b *block) findLocalVariable(name string) (int, shaderir.Type, bool) {
idx := 0
for outer := b.outer; outer != nil; outer = outer.outer {

View File

@ -0,0 +1,21 @@
void F0(in int l0, out int l1);
void F1(out int l0);
void F0(in int l0, out int l1) {
l1 = l0;
return;
}
void F1(out int l0) {
int l1 = 0;
l1 = 0;
for (int l2 = 0; l2 < 10; l2++) {
int l3 = 0;
int l4 = 0;
F0(l2, l3);
l4 = l3;
l1 = (l1) + (l4);
}
l0 = l1;
return;
}

14
internal/shader/testdata/for4.go vendored Normal file
View File

@ -0,0 +1,14 @@
package main
func Ident(x int) int {
return x
}
func Foo() int {
sum := 0
for i := 0; i < 10; i++ {
x := Ident(i)
sum += x
}
return sum
}