From e7bb66bb2f13ae302e8fd14409faa0b0c749e367 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 22 Mar 2024 22:58:20 +0900 Subject: [PATCH] internal/shader: bug fix: ++/-- statements didn't work for vec2 on browsers Closes #2933 --- internal/shader/stmt.go | 14 ++++++-- shader_test.go | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 6c07057da..902cc7b41 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -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, }, }, }, diff --git a/shader_test.go b/shader_test.go index d2ca7aafd..df826ab02 100644 --- a/shader_test.go +++ b/shader_test.go @@ -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) + } + } + } +}