internal/shader: ban the operator div on a matrix

The operator div on a matrix doesn't work on Metal.
This commit is contained in:
Hajime Hoshi 2022-01-21 03:37:31 +09:00
parent 2e5b4954f3
commit 84c680c6ed
3 changed files with 13 additions and 1 deletions

View File

@ -182,6 +182,10 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
}
t = lhst
case lhst.Equal(&rhst):
if op == shaderir.Div && (rhst.Main == shaderir.Mat2 || rhst.Main == shaderir.Mat3 || rhst.Main == shaderir.Mat4) {
cs.addError(e.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", e.Op, rhst.String()))
return nil, nil, nil, false
}
t = lhst
case lhst.Main == shaderir.Float:
switch rhst.Main {

View File

@ -94,7 +94,12 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
}
}
if lts[0].Main != rts[0].Main {
if lts[0].Main == rts[0].Main {
if op == shaderir.Div && (rts[0].Main == shaderir.Mat2 || rts[0].Main == shaderir.Mat3 || rts[0].Main == shaderir.Mat4) {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator / not defined on %s", rts[0].String()))
return nil, false
}
} else {
switch lts[0].Main {
case shaderir.Int:
if !cs.forceToInt(stmt, &rhs[0]) {

View File

@ -1594,6 +1594,7 @@ func TestShaderOperatorMultiply(t *testing.T) {
{stmt: "a := mat2(1) * vec3(2); _ = a", err: true},
{stmt: "a := mat2(1) * vec4(2); _ = a", err: true},
{stmt: "a := mat2(1) * mat2(2); _ = a", err: false},
{stmt: "a := mat2(1) / mat2(2); _ = a", err: true},
{stmt: "a := mat2(1) * mat3(2); _ = a", err: true},
{stmt: "a := mat2(1) * mat4(2); _ = a", err: true},
}
@ -1641,6 +1642,7 @@ func TestShaderOperatorMultiplyAssign(t *testing.T) {
{stmt: "a := vec2(1); a *= vec4(2)", err: true},
{stmt: "a := vec2(1); a *= mat2(2)", err: false},
{stmt: "a := vec2(1); a += mat2(2)", err: true},
{stmt: "a := vec2(1); a /= mat2(2)", err: true},
{stmt: "a := vec2(1); a *= mat3(2)", err: true},
{stmt: "a := vec2(1); a *= mat4(2)", err: true},
{stmt: "a := mat2(1); a *= 2", err: false},
@ -1656,6 +1658,7 @@ func TestShaderOperatorMultiplyAssign(t *testing.T) {
{stmt: "a := mat2(1); a *= vec4(2)", err: true},
{stmt: "a := mat2(1); a *= mat2(2)", err: false},
{stmt: "a := mat2(1); a += mat2(2)", err: false},
{stmt: "a := mat2(1); a /= mat2(2)", err: true},
{stmt: "a := mat2(1); a *= mat3(2)", err: true},
{stmt: "a := mat2(1); a *= mat4(2)", err: true},
}