internal/shaderir: change Op from string to int

This is a preparation to distinguish Hadamard product and matrix
product for HLSL.

Updates #1007
This commit is contained in:
Hajime Hoshi 2022-03-13 16:20:01 +09:00
parent 7d0f95e9be
commit 044d41dd2d
5 changed files with 119 additions and 31 deletions

View File

@ -504,9 +504,9 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
var op string
switch e.Op {
case shaderir.Add, shaderir.Sub, shaderir.NotOp:
op = string(e.Op)
op = opString(e.Op)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(e.Op))
op = fmt.Sprintf("?(unexpected op: %d)", e.Op)
}
return fmt.Sprintf("%s(%s)", op, expr(&e.Exprs[0]))
case shaderir.Binary:
@ -514,7 +514,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
// '%' is not defined.
return fmt.Sprintf("modInt((%s), (%s))", expr(&e.Exprs[0]), expr(&e.Exprs[1]))
}
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), e.Op, expr(&e.Exprs[1]))
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), opString(e.Op), expr(&e.Exprs[1]))
case shaderir.Selection:
return fmt.Sprintf("(%s) ? (%s) : (%s)", expr(&e.Exprs[0]), expr(&e.Exprs[1]), expr(&e.Exprs[2]))
case shaderir.Call:
@ -594,9 +594,9 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
var op string
switch s.ForOp {
case shaderir.LessThanOp, shaderir.LessThanEqualOp, shaderir.GreaterThanOp, shaderir.GreaterThanEqualOp, shaderir.EqualOp, shaderir.NotEqualOp:
op = string(s.ForOp)
op = opString(s.ForOp)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(s.ForOp))
op = fmt.Sprintf("?(unexpected op: %d)", s.ForOp)
}
t := s.ForVarType

View File

@ -20,6 +20,50 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
func opString(op shaderir.Op) string {
switch op {
case shaderir.Add:
return "+"
case shaderir.Sub:
return "-"
case shaderir.NotOp:
return "!"
case shaderir.Mul:
return "*"
case shaderir.Div:
return "/"
case shaderir.ModOp:
return "%"
case shaderir.LeftShift:
return "<<"
case shaderir.RightShift:
return ">>"
case shaderir.LessThanOp:
return "<"
case shaderir.LessThanEqualOp:
return "<="
case shaderir.GreaterThanOp:
return ">"
case shaderir.GreaterThanEqualOp:
return ">="
case shaderir.EqualOp:
return "=="
case shaderir.NotEqualOp:
return "!="
case shaderir.And:
return "&"
case shaderir.Xor:
return "^"
case shaderir.Or:
return "|"
case shaderir.AndAnd:
return "&&"
case shaderir.OrOr:
return "||"
}
return fmt.Sprintf("!(unexpected operator: %d)", op)
}
func typeString(t *shaderir.Type) (string, string) {
switch t.Main {
case shaderir.Array:

View File

@ -371,13 +371,13 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
var op string
switch e.Op {
case shaderir.Add, shaderir.Sub, shaderir.NotOp:
op = string(e.Op)
op = opString(e.Op)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(e.Op))
op = fmt.Sprintf("?(unexpected op: %d)", e.Op)
}
return fmt.Sprintf("%s(%s)", op, expr(&e.Exprs[0]))
case shaderir.Binary:
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), e.Op, expr(&e.Exprs[1]))
return fmt.Sprintf("(%s) %s (%s)", expr(&e.Exprs[0]), opString(e.Op), expr(&e.Exprs[1]))
case shaderir.Selection:
return fmt.Sprintf("(%s) ? (%s) : (%s)", expr(&e.Exprs[0]), expr(&e.Exprs[1]), expr(&e.Exprs[2]))
case shaderir.Call:
@ -469,9 +469,9 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl
var op string
switch s.ForOp {
case shaderir.LessThanOp, shaderir.LessThanEqualOp, shaderir.GreaterThanOp, shaderir.GreaterThanEqualOp, shaderir.EqualOp, shaderir.NotEqualOp:
op = string(s.ForOp)
op = opString(s.ForOp)
default:
op = fmt.Sprintf("?(unexpected op: %s)", string(s.ForOp))
op = fmt.Sprintf("?(unexpected op: %d)", s.ForOp)
}
t := s.ForVarType

View File

@ -20,6 +20,50 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
)
func opString(op shaderir.Op) string {
switch op {
case shaderir.Add:
return "+"
case shaderir.Sub:
return "-"
case shaderir.NotOp:
return "!"
case shaderir.Mul:
return "*"
case shaderir.Div:
return "/"
case shaderir.ModOp:
return "%"
case shaderir.LeftShift:
return "<<"
case shaderir.RightShift:
return ">>"
case shaderir.LessThanOp:
return "<"
case shaderir.LessThanEqualOp:
return "<="
case shaderir.GreaterThanOp:
return ">"
case shaderir.GreaterThanEqualOp:
return ">="
case shaderir.EqualOp:
return "=="
case shaderir.NotEqualOp:
return "!="
case shaderir.And:
return "&"
case shaderir.Xor:
return "^"
case shaderir.Or:
return "|"
case shaderir.AndAnd:
return "&&"
case shaderir.OrOr:
return "||"
}
return fmt.Sprintf("!(unexpected operator: %d)", op)
}
func typeString(t *shaderir.Type, packed bool, ref bool) string {
switch t.Main {
case shaderir.Array:

View File

@ -131,28 +131,28 @@ const (
Index
)
type Op string
type Op int
const (
Add Op = "+"
Sub Op = "-"
NotOp Op = "!"
Mul Op = "*"
Div Op = "/"
ModOp Op = "%"
LeftShift Op = "<<"
RightShift Op = ">>"
LessThanOp Op = "<"
LessThanEqualOp Op = "<="
GreaterThanOp Op = ">"
GreaterThanEqualOp Op = ">="
EqualOp Op = "=="
NotEqualOp Op = "!="
And Op = "&"
Xor Op = "^"
Or Op = "|"
AndAnd Op = "&&"
OrOr Op = "||"
Add Op = iota
Sub
NotOp
Mul // TODO: Separate Hadamard-product and Matrix-product
Div
ModOp
LeftShift
RightShift
LessThanOp
LessThanEqualOp
GreaterThanOp
GreaterThanEqualOp
EqualOp
NotEqualOp
And
Xor
Or
AndAnd
OrOr
)
func OpFromToken(t token.Token) (Op, bool) {
@ -196,7 +196,7 @@ func OpFromToken(t token.Token) (Op, bool) {
case token.LOR:
return OrOr, true
}
return "", false
return 0, false
}
type BuiltinFunc string