Skip to content

Commit

Permalink
internal/shaderir: remove ConstType
Browse files Browse the repository at this point in the history
Closes #2550
  • Loading branch information
hajimehoshi committed Jul 29, 2023
1 parent 7549f4f commit 5ddf1df
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 147 deletions.
41 changes: 12 additions & 29 deletions internal/shader/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)),
},
},
},
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions internal/shader/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type variable struct {
type constant struct {
name string
typ shaderir.Type
ctyp shaderir.ConstType
value gconstant.Value
}

Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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,
})
}
Expand Down
23 changes: 2 additions & 21 deletions internal/shader/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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) ||
Expand All @@ -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()))
Expand Down Expand Up @@ -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),
},
},
},
Expand Down Expand Up @@ -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(&lts[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()))
Expand Down
29 changes: 6 additions & 23 deletions internal/shaderir/glsl/glsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)...)
Expand Down
29 changes: 6 additions & 23 deletions internal/shaderir/hlsl/hlsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)...)
Expand Down
Loading

0 comments on commit 5ddf1df

Please sign in to comment.