internal/shader: bug fix: fix some failing tests

This commit is contained in:
Hajime Hoshi 2022-03-10 01:54:11 +09:00
parent d3b72d4ef9
commit 880dd0682e
2 changed files with 45 additions and 7 deletions

View File

@ -29,6 +29,26 @@ func canTruncateToInteger(v gconstant.Value) bool {
return gconstant.ToInt(v).Kind() != gconstant.Unknown
}
func isUntypedInteger(expr *shaderir.Expr) bool {
return expr.Const.Kind() == gconstant.Int && expr.ConstType == shaderir.ConstTypeNone
}
func isModAvailable(lhs, rhs *shaderir.Expr) bool {
// % is available only when
// 1) both are an untyped integer
// 2) either is an typed integer and the other is truncatable to an integer
if isUntypedInteger(lhs) && isUntypedInteger(rhs) {
return true
}
if lhs.ConstType == shaderir.ConstTypeInt && canTruncateToInteger(rhs.Const) {
return true
}
if rhs.ConstType == shaderir.ConstTypeInt && canTruncateToInteger(lhs.Const) {
return true
}
return false
}
func goConstantKindString(k gconstant.Kind) string {
switch k {
case gconstant.Bool:
@ -111,7 +131,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
t = shaderir.Type{Main: shaderir.Bool}
default:
if op == token.REM {
if lhs[0].Const.Kind() != gconstant.Int || rhs[0].Const.Kind() != gconstant.Int {
if !isModAvailable(&lhs[0], &rhs[0]) {
var wrongTypeName string
if lhs[0].Const.Kind() != gconstant.Int {
wrongTypeName = goConstantKindString(lhs[0].Const.Kind())
@ -121,6 +141,12 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
cs.addError(e.Pos(), fmt.Sprintf("invalid operation: operator %% not defined on untyped %s", wrongTypeName))
return nil, nil, nil, false
}
if !cs.forceToInt(e, &lhs[0]) {
return nil, nil, nil, false
}
if !cs.forceToInt(e, &rhs[0]) {
return nil, nil, nil, false
}
}
v = gconstant.BinaryOp(lhs[0].Const, op, rhs[0].Const)
if v.Kind() == gconstant.Float {
@ -156,8 +182,13 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
}
if rhst.Main == shaderir.Int {
fallthrough
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4:
if lhs[0].ConstType == shaderir.ConstTypeInt {
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
case shaderir.Int:
if !canTruncateToInteger(lhs[0].Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", lhs[0].Const.String()))
return nil, nil, nil, false
@ -172,8 +203,13 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
}
if lhst.Main == shaderir.Int {
fallthrough
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4:
if rhs[0].ConstType == shaderir.ConstTypeInt {
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), e.Op, rhst.String()))
return nil, nil, nil, false
}
case shaderir.Int:
if !canTruncateToInteger(rhs[0].Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", rhs[0].Const.String()))
return nil, nil, nil, false

View File

@ -29,6 +29,7 @@ func (cs *compileState) forceToInt(node ast.Node, expr *shaderir.Expr) bool {
cs.addError(node.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String()))
return false
}
expr.Const = gconstant.ToInt(expr.Const)
expr.ConstType = shaderir.ConstTypeInt
return true
}
@ -88,6 +89,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
stmts = append(stmts, ss...)
// Treat an integer literal as an integer constant value.
wasTypedConstInt := rhs[0].ConstType == shaderir.ConstTypeInt
if rhs[0].Type == shaderir.NumberExpr && rts[0].Main == shaderir.Int {
if !cs.forceToInt(stmt, &rhs[0]) {
return nil, false
@ -106,7 +108,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
return nil, false
}
case shaderir.Float:
if rhs[0].Const != nil && rhs[0].Const.Kind() == gconstant.Int {
if rhs[0].Const != nil && rhs[0].Const.Kind() == gconstant.Int && !wasTypedConstInt {
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
rhs[0].ConstType = shaderir.ConstTypeFloat
} else {
@ -125,7 +127,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
(lts[0].Main == shaderir.Vec3 && rts[0].Main == shaderir.Mat3) ||
(lts[0].Main == shaderir.Vec4 && rts[0].Main == shaderir.Mat4)) {
// OK
} else if rhs[0].Const != nil && rhs[0].Const.Kind() == gconstant.Int {
} else if rhs[0].Const != nil && rhs[0].Const.Kind() == gconstant.Int && !wasTypedConstInt {
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
rhs[0].ConstType = shaderir.ConstTypeFloat
} else {