shader: Bug fix: index out of range at defining a var

This commit is contained in:
Hajime Hoshi 2020-07-12 18:40:01 +09:00
parent dcb693460e
commit 4a8bd688a9
4 changed files with 43 additions and 1 deletions

View File

@ -110,6 +110,21 @@ func (b *block) findLocalVariable(name string) (int, shaderir.Type, bool) {
return 0, shaderir.Type{}, false return 0, shaderir.Type{}, false
} }
func (b *block) findLocalVariableByIndex(idx int) (shaderir.Type, bool) {
bs := []*block{b}
for outer := b.outer; outer != nil; outer = outer.outer {
bs = append(bs, outer)
}
for i := len(bs) - 1; i >= 0; i-- {
if len(bs[i].vars) <= idx {
idx -= len(bs[i].vars)
continue
}
return bs[i].vars[idx].typ, true
}
return shaderir.Type{}, false
}
type ParseError struct { type ParseError struct {
errs []string errs []string
} }

View File

@ -268,7 +268,12 @@ func (cs *compileState) assign(block *block, pos token.Pos, lhs, rhs []ast.Expr,
stmts = append(stmts, ss...) stmts = append(stmts, ss...)
if r[0].Type == shaderir.NumberExpr { if r[0].Type == shaderir.NumberExpr {
switch block.vars[l[0].Index].typ.Main { t, ok := block.findLocalVariableByIndex(l[0].Index)
if !ok {
cs.addError(pos, fmt.Sprintf("unexpected local variable index: %d", l[0].Index))
return nil, false
}
switch t.Main {
case shaderir.Int: case shaderir.Int:
r[0].ConstType = shaderir.ConstTypeInt r[0].ConstType = shaderir.ConstTypeInt
case shaderir.Float: case shaderir.Float:

View File

@ -0,0 +1,12 @@
void F0(out vec2 l0) {
bool l1 = false;
l1 = true;
{
int l2 = 0;
l2 = 0;
l0 = vec2(l2);
return;
}
l0 = vec2(1.0);
return;
}

10
internal/shader/testdata/blocks2.go vendored Normal file
View File

@ -0,0 +1,10 @@
package main
func Foo() vec2 {
x := true
{
x := 0
return vec2(x)
}
return vec2(1)
}