internal/shader: bug fix: operators between vec/mat and float should work

Closes #1963
This commit is contained in:
Hajime Hoshi 2022-01-17 20:52:41 +09:00
parent f5edc189e6
commit 4940159e5b
2 changed files with 50 additions and 0 deletions

View File

@ -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())) cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
return nil, false 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: default:
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String())) cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
return nil, false return nil, false

View File

@ -1459,3 +1459,46 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Errorf("error must be non-nil but was nil") 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")
}
}