From 5ddf1df42399669526a0369d26da4728d351764a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 29 Jul 2023 14:10:31 +0900 Subject: [PATCH] internal/shaderir: remove ConstType Closes #2550 --- internal/shader/expr.go | 41 ++++++++++------------------------ internal/shader/shader.go | 15 ------------- internal/shader/stmt.go | 23 ++----------------- internal/shaderir/glsl/glsl.go | 29 +++++------------------- internal/shaderir/hlsl/hlsl.go | 29 +++++------------------- internal/shaderir/msl/msl.go | 29 +++++------------------- internal/shaderir/program.go | 13 ----------- 7 files changed, 32 insertions(+), 147 deletions(-) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index 2be899997ce0..9018873755b4 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -112,31 +112,24 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar // If either is typed, resolve the other type. // If both are untyped, keep them untyped. if lhst.Main != shaderir.None || rhst.Main != shaderir.None { - // TODO: Remove ConstType (#2550) if lhs[0].Const != nil { switch lhs[0].Const.Kind() { case gconstant.Float: lhst = shaderir.Type{Main: shaderir.Float} - lhs[0].ConstType = shaderir.ConstTypeFloat case gconstant.Int: lhst = shaderir.Type{Main: shaderir.Int} - lhs[0].ConstType = shaderir.ConstTypeInt case gconstant.Bool: lhst = shaderir.Type{Main: shaderir.Bool} - lhs[0].ConstType = shaderir.ConstTypeBool } } if rhs[0].Const != nil { switch rhs[0].Const.Kind() { case gconstant.Float: rhst = shaderir.Type{Main: shaderir.Float} - rhs[0].ConstType = shaderir.ConstTypeFloat case gconstant.Int: rhst = shaderir.Type{Main: shaderir.Int} - rhs[0].ConstType = shaderir.ConstTypeInt case gconstant.Bool: rhst = shaderir.Type{Main: shaderir.Bool} - rhs[0].ConstType = shaderir.ConstTypeBool } } } @@ -274,9 +267,8 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar } return []shaderir.Expr{ { - Type: shaderir.NumberExpr, - Const: gconstant.MakeInt64(int64(argts[0].Length)), - ConstType: shaderir.ConstTypeInt, + Type: shaderir.NumberExpr, + Const: gconstant.MakeInt64(int64(argts[0].Length)), }, }, []shaderir.Type{{Main: shaderir.Int}}, stmts, true case shaderir.BoolF: @@ -287,9 +279,8 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar } return []shaderir.Expr{ { - Type: shaderir.NumberExpr, - Const: args[0].Const, - ConstType: shaderir.ConstTypeBool, + Type: shaderir.NumberExpr, + Const: args[0].Const, }, }, []shaderir.Type{{Main: shaderir.Bool}}, stmts, true } @@ -304,9 +295,8 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar } return []shaderir.Expr{ { - Type: shaderir.NumberExpr, - Const: v, - ConstType: shaderir.ConstTypeInt, + Type: shaderir.NumberExpr, + Const: v, }, }, []shaderir.Type{{Main: shaderir.Int}}, stmts, true } @@ -319,9 +309,8 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar } return []shaderir.Expr{ { - Type: shaderir.NumberExpr, - Const: v, - ConstType: shaderir.ConstTypeFloat, + Type: shaderir.NumberExpr, + Const: v, }, }, []shaderir.Type{{Main: shaderir.Float}}, stmts, true } @@ -482,7 +471,6 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar // If the argument is a non-typed constant value, treat this as a float value (#1874). if args[i].Const != nil && argts[i].Main == shaderir.None && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown { args[i].Const = gconstant.ToFloat(args[i].Const) - args[i].ConstType = shaderir.ConstTypeFloat argts[i] = shaderir.Type{Main: shaderir.Float} } if argts[i].Main != shaderir.Float && argts[i].Main != shaderir.Vec2 && argts[i].Main != shaderir.Vec3 && argts[i].Main != shaderir.Vec4 { @@ -543,7 +531,6 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar // If the argument is a non-typed constant value, treat this as a float value (#1874). if args[i].Const != nil && argts[i].Main == shaderir.None && gconstant.ToFloat(args[i].Const).Kind() != gconstant.Unknown { args[i].Const = gconstant.ToFloat(args[i].Const) - args[i].ConstType = shaderir.ConstTypeFloat argts[i] = shaderir.Type{Main: shaderir.Float} } if argts[i].Main != shaderir.Float && argts[i].Main != shaderir.Vec2 && argts[i].Main != shaderir.Vec3 && argts[i].Main != shaderir.Vec4 { @@ -594,7 +581,6 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar // If the argument is a non-typed constant value, treat this as a float value (#1874). if args[0].Const != nil && argts[0].Main == shaderir.None && gconstant.ToFloat(args[0].Const).Kind() != gconstant.Unknown { args[0].Const = gconstant.ToFloat(args[0].Const) - args[0].ConstType = shaderir.ConstTypeFloat argts[0] = shaderir.Type{Main: shaderir.Float} } switch callee.BuiltinFunc { @@ -737,9 +723,8 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar if c, ok := block.findConstant(e.Name); ok { return []shaderir.Expr{ { - Type: shaderir.NumberExpr, - Const: c.value, - ConstType: c.ctyp, + Type: shaderir.NumberExpr, + Const: c.value, }, }, []shaderir.Type{c.typ}, nil, true } @@ -928,9 +913,8 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar Index: idx, }, { - Type: shaderir.NumberExpr, - Const: gconstant.MakeInt64(int64(i)), - ConstType: shaderir.ConstTypeInt, + Type: shaderir.NumberExpr, + Const: gconstant.MakeInt64(int64(i)), }, }, }, @@ -966,7 +950,6 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", idx.Const.String())) return nil, nil, nil, false } - idx.ConstType = shaderir.ConstTypeInt } exprs, ts, ss, ok := cs.parseExpr(block, fname, e.X, markLocalVariableUsed) diff --git a/internal/shader/shader.go b/internal/shader/shader.go index df280a056c7a..1ad5fa093ba2 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -37,7 +37,6 @@ type variable struct { type constant struct { name string typ shaderir.Type - ctyp shaderir.ConstType value gconstant.Value } @@ -550,15 +549,6 @@ func (s *compileState) parseVariable(block *block, fname string, vs *ast.ValueSp } } - if es[0].Type == shaderir.NumberExpr { - switch t.Main { - case shaderir.Int: - es[0].ConstType = shaderir.ConstTypeInt - case shaderir.Float: - es[0].ConstType = shaderir.ConstTypeFloat - } - } - for i, rt := range rts { if !canAssign(&t, &rt, es[i].Const) { s.addError(vs.Pos(), fmt.Sprintf("cannot use type %s as type %s in variable declaration", rt.String(), t.String())) @@ -682,22 +672,17 @@ func (s *compileState) parseConstant(block *block, fname string, vs *ast.ValueSp } c := es[0].Const - constType := es[0].ConstType switch t.Main { case shaderir.Bool: - constType = shaderir.ConstTypeBool case shaderir.Int: - constType = shaderir.ConstTypeInt c = gconstant.ToInt(c) case shaderir.Float: - constType = shaderir.ConstTypeFloat c = gconstant.ToFloat(c) } cs = append(cs, constant{ name: name, typ: t, - ctyp: constType, value: c, }) } diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 450adf596202..8c16f4cce549 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -30,7 +30,6 @@ func (cs *compileState) forceToInt(node ast.Node, expr *shaderir.Expr) bool { return false } expr.Const = gconstant.ToInt(expr.Const) - expr.ConstType = shaderir.ConstTypeInt return true } @@ -124,7 +123,6 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP (rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) && gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown { rhs[0].Const = gconstant.ToFloat(rhs[0].Const) - rhs[0].ConstType = shaderir.ConstTypeFloat } else { cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String())) return nil, false @@ -137,7 +135,6 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) { if rhs[0].Const != nil { rhs[0].Const = gconstant.ToFloat(rhs[0].Const) - rhs[0].ConstType = shaderir.ConstTypeFloat } } else if op == shaderir.MatrixMul && ((lts[0].Main == shaderir.Vec2 && rts[0].Main == shaderir.Mat2) || (lts[0].Main == shaderir.Vec3 && rts[0].Main == shaderir.Mat3) || @@ -150,7 +147,6 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) { if rhs[0].Const != nil { rhs[0].Const = gconstant.ToFloat(rhs[0].Const) - rhs[0].ConstType = shaderir.ConstTypeFloat } } else { cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String())) @@ -442,9 +438,8 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP Exprs: []shaderir.Expr{ exprs[0], { - Type: shaderir.NumberExpr, - Const: gconstant.MakeInt64(1), - ConstType: shaderir.ConstTypeInt, + Type: shaderir.NumberExpr, + Const: gconstant.MakeInt64(1), }, }, }, @@ -692,20 +687,6 @@ func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, r } allblank = false - if r[0].Const != nil { - t, ok := block.findLocalVariableByIndex(l[0].Index) - if !ok { - cs.addError(pos, fmt.Sprintf("unexpected local variable index: %d", l[0].Index)) - return nil, false - } - switch t.Main { - case shaderir.Int: - r[0].ConstType = shaderir.ConstTypeInt - case shaderir.Float: - r[0].ConstType = shaderir.ConstTypeFloat - } - } - for i := range lts { if !canAssign(<s[i], &rts[i], r[i].Const) { cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rts[i].String(), lts[i].String())) diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 0690e83f8691..e947e0b5cd10 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -377,26 +377,17 @@ func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototy return lines } -func constantToNumberLiteral(t shaderir.ConstType, v constant.Value) string { +func constantToNumberLiteral(v constant.Value) string { switch v.Kind() { case constant.Bool: - if t != shaderir.ConstTypeBool && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for bool: %d)", t) - } if constant.BoolVal(v) { return "true" } return "false" case constant.Int: - if t != shaderir.ConstTypeInt && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for int: %d)", t) - } x, _ := constant.Int64Val(v) return fmt.Sprintf("%d", x) case constant.Float: - if t != shaderir.ConstTypeFloat && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for float: %d)", t) - } x, _ := constant.Float64Val(v) if i := math.Floor(x); i == x { return fmt.Sprintf("%d.0", int64(i)) @@ -477,7 +468,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl expr = func(e *shaderir.Expr) string { switch e.Type { case shaderir.NumberExpr: - return constantToNumberLiteral(e.ConstType, e.Const) + return constantToNumberLiteral(e.Const) case shaderir.UniformVariable: return fmt.Sprintf("U%d", e.Index) case shaderir.TextureVariable: @@ -564,14 +555,6 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } lines = append(lines, fmt.Sprintf("%s}", idt)) case shaderir.For: - var ct shaderir.ConstType - switch s.ForVarType.Main { - case shaderir.Int: - ct = shaderir.ConstTypeInt - case shaderir.Float: - ct = shaderir.ConstTypeFloat - } - v := c.localVariableName(p, topBlock, s.ForVarIndex) var delta string switch val, _ := constant.Float64Val(s.ForDelta); val { @@ -584,10 +567,10 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl default: d := s.ForDelta if val > 0 { - delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d)) + delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(d)) } else { d = constant.UnaryOp(token.SUB, d, 0) - delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d)) + delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(d)) } } var op string @@ -599,8 +582,8 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } t := s.ForVarType - init := constantToNumberLiteral(ct, s.ForInit) - end := constantToNumberLiteral(ct, s.ForEnd) + init := constantToNumberLiteral(s.ForInit) + end := constantToNumberLiteral(s.ForEnd) t0, t1 := typeString(&t) lines = append(lines, fmt.Sprintf("%sfor (%s %s%s = %s; %s %s %s; %s) {", idt, t0, v, t1, init, v, op, end, delta)) lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...) diff --git a/internal/shaderir/hlsl/hlsl.go b/internal/shaderir/hlsl/hlsl.go index 134eccf0c6f1..374accac5276 100644 --- a/internal/shaderir/hlsl/hlsl.go +++ b/internal/shaderir/hlsl/hlsl.go @@ -310,26 +310,17 @@ func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototy return lines } -func constantToNumberLiteral(t shaderir.ConstType, v constant.Value) string { +func constantToNumberLiteral(v constant.Value) string { switch v.Kind() { case constant.Bool: - if t != shaderir.ConstTypeBool && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for bool: %d)", t) - } if constant.BoolVal(v) { return "true" } return "false" case constant.Int: - if t != shaderir.ConstTypeInt && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for int: %d)", t) - } x, _ := constant.Int64Val(v) return fmt.Sprintf("%d", x) case constant.Float: - if t != shaderir.ConstTypeFloat && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for float: %d)", t) - } x, _ := constant.Float64Val(v) if i := math.Floor(x); i == x { return fmt.Sprintf("%d.0", int64(i)) @@ -410,7 +401,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl expr = func(e *shaderir.Expr) string { switch e.Type { case shaderir.NumberExpr: - return constantToNumberLiteral(e.ConstType, e.Const) + return constantToNumberLiteral(e.Const) case shaderir.UniformVariable: return fmt.Sprintf("U%d", e.Index) case shaderir.TextureVariable: @@ -531,14 +522,6 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } lines = append(lines, fmt.Sprintf("%s}", idt)) case shaderir.For: - var ct shaderir.ConstType - switch s.ForVarType.Main { - case shaderir.Int: - ct = shaderir.ConstTypeInt - case shaderir.Float: - ct = shaderir.ConstTypeFloat - } - v := c.localVariableName(p, topBlock, s.ForVarIndex) var delta string switch val, _ := constant.Float64Val(s.ForDelta); val { @@ -551,10 +534,10 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl default: d := s.ForDelta if val > 0 { - delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d)) + delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(d)) } else { d = constant.UnaryOp(token.SUB, d, 0) - delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d)) + delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(d)) } } var op string @@ -566,8 +549,8 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } t := s.ForVarType - init := constantToNumberLiteral(ct, s.ForInit) - end := constantToNumberLiteral(ct, s.ForEnd) + init := constantToNumberLiteral(s.ForInit) + end := constantToNumberLiteral(s.ForEnd) t0, t1 := typeString(&t) lines = append(lines, fmt.Sprintf("%sfor (%s %s%s = %s; %s %s %s; %s) {", idt, t0, v, t1, init, v, op, end, delta)) lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...) diff --git a/internal/shaderir/msl/msl.go b/internal/shaderir/msl/msl.go index 1be11c1ecc5a..32bf766e0762 100644 --- a/internal/shaderir/msl/msl.go +++ b/internal/shaderir/msl/msl.go @@ -260,26 +260,17 @@ func (c *compileContext) function(p *shaderir.Program, f *shaderir.Func, prototy return lines } -func constantToNumberLiteral(t shaderir.ConstType, v constant.Value) string { +func constantToNumberLiteral(v constant.Value) string { switch v.Kind() { case constant.Bool: - if t != shaderir.ConstTypeBool && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for bool: %d)", t) - } if constant.BoolVal(v) { return "true" } return "false" case constant.Int: - if t != shaderir.ConstTypeInt && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for int: %d)", t) - } x, _ := constant.Int64Val(v) return fmt.Sprintf("%d", x) case constant.Float: - if t != shaderir.ConstTypeFloat && t != shaderir.ConstTypeNone { - return fmt.Sprintf("!(unexpected const-type for float: %d)", t) - } x, _ := constant.Float64Val(v) if i := math.Floor(x); i == x { return fmt.Sprintf("%d.0", int64(i)) @@ -352,7 +343,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl expr = func(e *shaderir.Expr) string { switch e.Type { case shaderir.NumberExpr: - return constantToNumberLiteral(e.ConstType, e.Const) + return constantToNumberLiteral(e.Const) case shaderir.UniformVariable: return fmt.Sprintf("U%d", e.Index) case shaderir.TextureVariable: @@ -456,14 +447,6 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } lines = append(lines, fmt.Sprintf("%s}", idt)) case shaderir.For: - var ct shaderir.ConstType - switch s.ForVarType.Main { - case shaderir.Int: - ct = shaderir.ConstTypeInt - case shaderir.Float: - ct = shaderir.ConstTypeFloat - } - v := localVariableName(p, topBlock, s.ForVarIndex) var delta string switch val, _ := constant.Float64Val(s.ForDelta); val { @@ -476,10 +459,10 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl default: d := s.ForDelta if val > 0 { - delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(ct, d)) + delta = fmt.Sprintf("%s += %s", v, constantToNumberLiteral(d)) } else { d = constant.UnaryOp(token.SUB, d, 0) - delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(ct, d)) + delta = fmt.Sprintf("%s -= %s", v, constantToNumberLiteral(d)) } } var op string @@ -491,8 +474,8 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } t := s.ForVarType - init := constantToNumberLiteral(ct, s.ForInit) - end := constantToNumberLiteral(ct, s.ForEnd) + init := constantToNumberLiteral(s.ForInit) + end := constantToNumberLiteral(s.ForEnd) ts := typeString(&t, false) lines = append(lines, fmt.Sprintf("%sfor (%s %s = %s; %s %s %s; %s) {", idt, ts, v, init, v, op, end, delta)) lines = append(lines, c.block(p, topBlock, s.Blocks[0], level+1)...) diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index 363bd884d488..3c73f912fb72 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -101,16 +101,6 @@ const ( Discard ) -// TODO: Remove ConstType (#2550) -type ConstType int - -const ( - ConstTypeNone ConstType = iota - ConstTypeBool - ConstTypeInt - ConstTypeFloat -) - type Expr struct { Type ExprType Exprs []Expr @@ -119,9 +109,6 @@ type Expr struct { Swizzling string Index int Op Op - - // TODO: Remove ConstType (#2550) - ConstType ConstType } type ExprType int