internal/shader: refactoring

This commit is contained in:
Hajime Hoshi 2022-08-19 15:24:00 +09:00
parent a4b9d3c241
commit f63b87f675
2 changed files with 17 additions and 17 deletions

View File

@ -115,7 +115,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
stmts = append(stmts, ss...) stmts = append(stmts, ss...)
rhst := ts[0] rhst := ts[0]
if lhs[0].Type == shaderir.NumberExpr && rhs[0].Type == shaderir.NumberExpr { if lhs[0].Const != nil && rhs[0].Const != nil {
op := e.Op op := e.Op
// https://golang.org/pkg/go/constant/#BinaryOp // https://golang.org/pkg/go/constant/#BinaryOp
// "To force integer division of Int operands, use op == token.QUO_ASSIGN instead of // "To force integer division of Int operands, use op == token.QUO_ASSIGN instead of
@ -175,7 +175,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
case op == shaderir.LessThanOp || op == shaderir.LessThanEqualOp || op == shaderir.GreaterThanOp || op == shaderir.GreaterThanEqualOp || op == shaderir.EqualOp || op == shaderir.NotEqualOp || op == shaderir.VectorEqualOp || op == shaderir.VectorNotEqualOp || op == shaderir.AndAnd || op == shaderir.OrOr: case op == shaderir.LessThanOp || op == shaderir.LessThanEqualOp || op == shaderir.GreaterThanOp || op == shaderir.GreaterThanEqualOp || op == shaderir.EqualOp || op == shaderir.NotEqualOp || op == shaderir.VectorEqualOp || op == shaderir.VectorNotEqualOp || op == shaderir.AndAnd || op == shaderir.OrOr:
// TODO: Check types of the operands. // TODO: Check types of the operands.
t = shaderir.Type{Main: shaderir.Bool} t = shaderir.Type{Main: shaderir.Bool}
case lhs[0].Type == shaderir.NumberExpr && rhs[0].Type != shaderir.NumberExpr: case lhs[0].Const != nil && rhs[0].Const == nil:
switch rhst.Main { switch rhst.Main {
case shaderir.Mat2, shaderir.Mat3, shaderir.Mat4: case shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
if op != shaderir.MatrixMul { if op != shaderir.MatrixMul {
@ -196,7 +196,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
lhs[0].ConstType = shaderir.ConstTypeInt lhs[0].ConstType = shaderir.ConstTypeInt
} }
t = rhst t = rhst
case lhs[0].Type != shaderir.NumberExpr && rhs[0].Type == shaderir.NumberExpr: case lhs[0].Const == nil && rhs[0].Const != nil:
switch lhst.Main { switch lhst.Main {
case shaderir.Mat2, shaderir.Mat3, shaderir.Mat4: case shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
if op != shaderir.MatrixMul && op != shaderir.Div { if op != shaderir.MatrixMul && op != shaderir.Div {
@ -346,7 +346,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
}, },
}, []shaderir.Type{{Main: shaderir.Int}}, stmts, true }, []shaderir.Type{{Main: shaderir.Int}}, stmts, true
case shaderir.IntF: case shaderir.IntF:
if len(args) == 1 && args[0].Type == shaderir.NumberExpr { if len(args) == 1 && args[0].Const != nil {
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
@ -360,7 +360,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
}, []shaderir.Type{{Main: shaderir.Int}}, stmts, true }, []shaderir.Type{{Main: shaderir.Int}}, stmts, true
} }
case shaderir.FloatF: case shaderir.FloatF:
if len(args) == 1 && args[0].Type == shaderir.NumberExpr { if len(args) == 1 && args[0].Const != nil {
if gconstant.ToFloat(args[0].Const).Kind() != gconstant.Unknown { if gconstant.ToFloat(args[0].Const).Kind() != gconstant.Unknown {
return []shaderir.Expr{ return []shaderir.Expr{
{ {
@ -466,7 +466,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
} }
for i := range args { for i := range args {
// If the argument is a non-typed constant value, treat this as a float value (#1874). // If the argument is a non-typed constant value, treat this as a float value (#1874).
if args[i].Type == shaderir.NumberExpr && args[i].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown { if args[i].Const != nil && args[i].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown {
args[i].Const = gconstant.ToFloat(args[i].Const) args[i].Const = gconstant.ToFloat(args[i].Const)
args[i].ConstType = shaderir.ConstTypeFloat args[i].ConstType = shaderir.ConstTypeFloat
argts[i] = shaderir.Type{Main: shaderir.Float} argts[i] = shaderir.Type{Main: shaderir.Float}
@ -527,7 +527,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
} }
for i := range args { for i := range args {
// If the argument is a non-typed constant value, treat this as a float value (#1874). // If the argument is a non-typed constant value, treat this as a float value (#1874).
if args[i].Type == shaderir.NumberExpr && args[i].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown { if args[i].Const != nil && args[i].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown {
args[i].Const = gconstant.ToFloat(args[i].Const) args[i].Const = gconstant.ToFloat(args[i].Const)
args[i].ConstType = shaderir.ConstTypeFloat args[i].ConstType = shaderir.ConstTypeFloat
argts[i] = shaderir.Type{Main: shaderir.Float} argts[i] = shaderir.Type{Main: shaderir.Float}
@ -578,7 +578,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
return nil, nil, nil, false return nil, nil, nil, false
} }
// If the argument is a non-typed constant value, treat this as a float value (#1874). // If the argument is a non-typed constant value, treat this as a float value (#1874).
if args[0].Type == shaderir.NumberExpr && args[0].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[0].Const).Kind() != gconstant.Unknown { if args[0].Const != nil && args[0].ConstType == shaderir.ConstTypeNone && gconstant.ToFloat(args[0].Const).Kind() != gconstant.Unknown {
args[0].Const = gconstant.ToFloat(args[0].Const) args[0].Const = gconstant.ToFloat(args[0].Const)
args[0].ConstType = shaderir.ConstTypeFloat args[0].ConstType = shaderir.ConstTypeFloat
argts[0] = shaderir.Type{Main: shaderir.Float} argts[0] = shaderir.Type{Main: shaderir.Float}
@ -626,7 +626,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
} }
for i, p := range f.ir.InParams { for i, p := range f.ir.InParams {
if args[i].Type == shaderir.NumberExpr && p.Main == shaderir.Int { if args[i].Const != nil && p.Main == shaderir.Int {
if !cs.forceToInt(e, &args[i]) { if !cs.forceToInt(e, &args[i]) {
return nil, nil, nil, false return nil, nil, nil, false
} }
@ -816,7 +816,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
return nil, nil, nil, false return nil, nil, nil, false
} }
if exprs[0].Type == shaderir.NumberExpr { if exprs[0].Const != nil {
v := gconstant.UnaryOp(e.Op, exprs[0].Const, 0) v := gconstant.UnaryOp(e.Op, exprs[0].Const, 0)
t := shaderir.Type{Main: shaderir.Int} t := shaderir.Type{Main: shaderir.Int}
if v.Kind() == gconstant.Float { if v.Kind() == gconstant.Float {
@ -919,7 +919,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
return nil, nil, nil, false return nil, nil, nil, false
} }
idx := exprs[0] idx := exprs[0]
if idx.Type == shaderir.NumberExpr { if idx.Const != nil {
if !canTruncateToInteger(idx.Const) { if !canTruncateToInteger(idx.Const) {
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", idx.Const.String())) cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", idx.Const.String()))
return nil, nil, nil, false return nil, nil, nil, false

View File

@ -220,7 +220,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
return nil, false return nil, false
} }
varidx := ss[0].Exprs[0].Index varidx := ss[0].Exprs[0].Index
if ss[0].Exprs[1].Type != shaderir.NumberExpr { if ss[0].Exprs[1].Const == nil {
cs.addError(stmt.Pos(), msg) cs.addError(stmt.Pos(), msg)
return nil, false return nil, false
} }
@ -261,7 +261,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
cs.addError(stmt.Pos(), msg) cs.addError(stmt.Pos(), msg)
return nil, false return nil, false
} }
if exprs[0].Exprs[1].Type != shaderir.NumberExpr { if exprs[0].Exprs[1].Const == nil {
cs.addError(stmt.Pos(), msg) cs.addError(stmt.Pos(), msg)
return nil, false return nil, false
} }
@ -299,7 +299,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
cs.addError(stmt.Pos(), msg) cs.addError(stmt.Pos(), msg)
return nil, false return nil, false
} }
if postSs[0].Exprs[1].Exprs[1].Type != shaderir.NumberExpr { if postSs[0].Exprs[1].Exprs[1].Const == nil {
cs.addError(stmt.Pos(), msg) cs.addError(stmt.Pos(), msg)
return nil, false return nil, false
} }
@ -479,7 +479,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
} else { } else {
outT = outParams[i].typ outT = outParams[i].typ
} }
if expr.Type == shaderir.NumberExpr { if expr.Const != nil {
switch outT.Main { switch outT.Main {
case shaderir.Int: case shaderir.Int:
if !cs.forceToInt(stmt, &expr) { if !cs.forceToInt(stmt, &expr) {
@ -652,7 +652,7 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r
} }
allblank = false allblank = false
if r[0].Type == shaderir.NumberExpr { if r[0].Const != nil {
t, ok := block.findLocalVariableByIndex(l[0].Index) t, ok := block.findLocalVariableByIndex(l[0].Index)
if !ok { if !ok {
cs.addError(pos, fmt.Sprintf("unexpected local variable index: %d", l[0].Index)) cs.addError(pos, fmt.Sprintf("unexpected local variable index: %d", l[0].Index))
@ -793,7 +793,7 @@ func canAssign(re *shaderir.Expr, lt *shaderir.Type, rt *shaderir.Type) bool {
if lt.Equal(rt) { if lt.Equal(rt) {
return true return true
} }
if re.Type == shaderir.NumberExpr { if re.Const != nil {
switch lt.Main { switch lt.Main {
case shaderir.Bool: case shaderir.Bool:
return re.Const.Kind() == gconstant.Bool return re.Const.Kind() == gconstant.Bool