internal/shader: refactoring

This commit is contained in:
Hajime Hoshi 2022-07-08 01:09:42 +09:00
parent a3590cb258
commit 87be157b23

View File

@ -346,11 +346,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
}, },
}, []shaderir.Type{{Main: shaderir.Int}}, stmts, true }, []shaderir.Type{{Main: shaderir.Int}}, stmts, true
case shaderir.IntF: case shaderir.IntF:
if len(args) != 1 { if len(args) == 1 && args[0].Type == shaderir.NumberExpr {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 1 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
if args[0].Type == shaderir.NumberExpr {
if !canTruncateToInteger(args[0].Const) { if !canTruncateToInteger(args[0].Const) {
cs.addError(e.Pos(), fmt.Sprintf("cannot convert %s to type int", args[0].Const.String())) cs.addError(e.Pos(), fmt.Sprintf("cannot convert %s to type int", args[0].Const.String()))
return nil, nil, nil, false return nil, nil, nil, false
@ -364,11 +360,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
}, []shaderir.Type{{Main: shaderir.Int}}, stmts, true }, []shaderir.Type{{Main: shaderir.Int}}, stmts, true
} }
case shaderir.FloatF: case shaderir.FloatF:
if len(args) != 1 { if len(args) == 1 && args[0].Type == shaderir.NumberExpr {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 1 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
if args[0].Type == shaderir.NumberExpr {
if args[0].Const.Kind() == gconstant.Int || args[0].Const.Kind() == gconstant.Float { if args[0].Const.Kind() == gconstant.Int || args[0].Const.Kind() == gconstant.Float {
return []shaderir.Expr{ return []shaderir.Expr{
{ {
@ -379,17 +371,9 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
}, []shaderir.Type{{Main: shaderir.Float}}, stmts, true }, []shaderir.Type{{Main: shaderir.Float}}, stmts, true
} }
} }
case shaderir.Atan:
if len(args) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 1 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
case shaderir.Atan2:
if len(args) != 2 {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 2 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
} }
// Process the expression as a regular function call.
var t shaderir.Type var t shaderir.Type
switch callee.BuiltinFunc { switch callee.BuiltinFunc {
case shaderir.BoolF: case shaderir.BoolF:
@ -446,6 +430,30 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
return nil, nil, nil, false return nil, nil, nil, false
} }
t = shaderir.Type{Main: shaderir.Mat4} t = shaderir.Type{Main: shaderir.Mat4}
case shaderir.Atan:
if len(args) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 1 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
// TODO: Check arg types.
// If the argument is a non-typed constant value, treat is as a float value (#1874).
if args[0].Type == shaderir.NumberExpr && args[0].ConstType == shaderir.ConstTypeNone {
args[0].ConstType = shaderir.ConstTypeFloat
argts[0] = shaderir.Type{Main: shaderir.Float}
}
t = argts[0]
case shaderir.Atan2:
if len(args) != 2 {
cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 2 but %d", callee.BuiltinFunc, len(args)))
return nil, nil, nil, false
}
// TODO: Check arg types.
// If the argument is a non-typed constant value, treat is as a float value (#1874).
if args[0].Type == shaderir.NumberExpr && args[0].ConstType == shaderir.ConstTypeNone {
args[0].ConstType = shaderir.ConstTypeFloat
argts[0] = shaderir.Type{Main: shaderir.Float}
}
t = argts[0]
case shaderir.Step: case shaderir.Step:
// TODO: Check arg types. // TODO: Check arg types.
t = argts[1] t = argts[1]
@ -462,7 +470,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr, markLocalVariable
// TODO: Check arg types. // TODO: Check arg types.
t = shaderir.Type{Main: shaderir.Vec4} t = shaderir.Type{Main: shaderir.Vec4}
default: default:
// TODO: Check arg types? // TODO: Check arg types.
// If the argument is a non-typed constant value, treat is as a float value (#1874). // If the argument is a non-typed constant value, treat is as a float value (#1874).
if args[0].Type == shaderir.NumberExpr && args[0].ConstType == shaderir.ConstTypeNone { if args[0].Type == shaderir.NumberExpr && args[0].ConstType == shaderir.ConstTypeNone {
args[0].ConstType = shaderir.ConstTypeFloat args[0].ConstType = shaderir.ConstTypeFloat