From 4940159e5b750fe87b154f39fb40f87b068762f5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 17 Jan 2022 20:52:41 +0900 Subject: [PATCH] internal/shader: bug fix: operators between vec/mat and float should work Closes #1963 --- internal/shader/stmt.go | 7 +++++++ shader_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 3a3f6aeb1..cf44e1ab1 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -108,6 +108,13 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String())) return nil, false } + case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4: + if rts[0].Main == shaderir.Float || rhs[0].Const != nil { + // ok + } else { + cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String())) + return nil, false + } default: cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String())) return nil, false diff --git a/shader_test.go b/shader_test.go index 296ee8227..648b4e213 100644 --- a/shader_test.go +++ b/shader_test.go @@ -1459,3 +1459,46 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { t.Errorf("error must be non-nil but was nil") } } + +// Issue #1963 +func TestShaderOperatorVecAndNumber(t *testing.T) { + if _, err := ebiten.NewShader([]byte(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + a := vec2(1) + a += 2 + return a.xxyy +}`)); err != nil { + t.Error(err) + } + + if _, err := ebiten.NewShader([]byte(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + a := vec2(1) + a += 2.1 + return a.xxyy +}`)); err != nil { + t.Error(err) + } + + if _, err := ebiten.NewShader([]byte(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + a := vec2(1) + a %= 2 + return a.xxyy +}`)); err == nil { + t.Errorf("error must be non-nil but was nil") + } + + if _, err := ebiten.NewShader([]byte(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + a := vec2(1) + a %= 2.1 + return a.xxyy +}`)); err == nil { + t.Errorf("error must be non-nil but was nil") + } +}