internal/shader: bug fix: need to covert constant type correctly for assignments

Closes #2934
This commit is contained in:
Hajime Hoshi 2024-03-22 23:52:13 +09:00
parent e7bb66bb2f
commit 7842942b24
2 changed files with 42 additions and 0 deletions

View File

@ -582,6 +582,12 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rts[i].String(), lts[i].String())) cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rts[i].String(), lts[i].String()))
return nil, false return nil, false
} }
switch lts[0].Main {
case shaderir.Int:
r[i].Const = gconstant.ToInt(r[i].Const)
case shaderir.Float:
r[i].Const = gconstant.ToFloat(r[i].Const)
}
} }
if len(lhs) == 1 { if len(lhs) == 1 {

View File

@ -2559,3 +2559,39 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
} }
} }
} }
// Issue #2934
func TestShaderAssignConst(t *testing.T) {
const w, h = 16, 16
dst := ebiten.NewImage(w, h)
s, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
a := 0.0
a = 1
b, c := 0.0, 0.0
b, c = 1, 1
d := 0.0
d += 1
return vec4(a, b, c, d)
}
`))
if err != nil {
t.Fatal(err)
}
dst.DrawRectShader(w, h, s, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}