internal/shader: bug fix: ++/-- statements didn't work for vec2 on browsers

Closes #2933
This commit is contained in:
Hajime Hoshi 2024-03-22 22:58:20 +09:00
parent 1586c6764a
commit e7bb66bb2f
2 changed files with 84 additions and 2 deletions

View File

@ -292,7 +292,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
})
case *ast.IncDecStmt:
exprs, _, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
exprs, ts, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
if !ok {
return nil, false
}
@ -304,6 +304,16 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
case token.DEC:
op = shaderir.Sub
}
var c gconstant.Value
switch {
case ts[0].Main == shaderir.Int, ts[0].IsIntVector():
c = gconstant.MakeInt64(1)
case ts[0].Main == shaderir.Float, ts[0].IsFloatVector():
c = gconstant.MakeFloat64(1)
default:
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation %s (non-numeric type %s)", stmt.Tok.String(), ts[0].String()))
return nil, false
}
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
@ -315,7 +325,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
exprs[0],
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeInt64(1),
Const: c,
},
},
},

View File

@ -2487,3 +2487,75 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
}
}
}
// Issue #2933
func TestShaderIncDecStmt(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
a++
b := -0.5
b++
c := ivec2(0)
c++
d := vec2(-0.25)
d++
return vec4(float(a), b, float(c.x), d.y)
}
`))
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: 0x80, B: 0xff, A: 0xc0}
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
dst.Clear()
s, err = ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
a := 1
a--
b := 1.5
b--
c := ivec2(1)
c--
d := vec2(1.25)
d--
return vec4(float(a), b, float(c.x), d.y)
}
`))
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: 0x00, G: 0x80, B: 0x00, A: 0x40}
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}