mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
shader: Bug fix: index out of range at defining a var
This commit is contained in:
parent
dcb693460e
commit
4a8bd688a9
@ -110,6 +110,21 @@ func (b *block) findLocalVariable(name string) (int, shaderir.Type, bool) {
|
||||
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 {
|
||||
errs []string
|
||||
}
|
||||
|
@ -268,7 +268,12 @@ func (cs *compileState) assign(block *block, pos token.Pos, lhs, rhs []ast.Expr,
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
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:
|
||||
r[0].ConstType = shaderir.ConstTypeInt
|
||||
case shaderir.Float:
|
||||
|
12
internal/shader/testdata/blocks2.expected.vs
vendored
Normal file
12
internal/shader/testdata/blocks2.expected.vs
vendored
Normal 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
10
internal/shader/testdata/blocks2.go
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
func Foo() vec2 {
|
||||
x := true
|
||||
{
|
||||
x := 0
|
||||
return vec2(x)
|
||||
}
|
||||
return vec2(1)
|
||||
}
|
Loading…
Reference in New Issue
Block a user