shaderir: Remove Variable and VariableType

This commit is contained in:
Hajime Hoshi 2020-05-17 04:28:03 +09:00
parent 094d845edd
commit 156ed320cc
3 changed files with 83 additions and 96 deletions

View File

@ -189,44 +189,39 @@ func (p *Program) glslBlock(b *Block, level int, localVarIndex int) []string {
return fmt.Sprintf("%d", e.Int)
case FloatExpr:
return fmt.Sprintf("%.9e", e.Float)
case VarName:
switch e.Variable.Type {
case Uniform:
return fmt.Sprintf("U%d", e.Variable.Index)
case StructMember:
return fmt.Sprintf("M%d", e.Variable.Index)
case Local:
idx := e.Variable.Index
switch b {
case &p.VertexFunc.Block:
na := len(p.Attributes)
nv := len(p.Varyings)
switch {
case idx < na:
return fmt.Sprintf("A%d", idx)
case idx < na+nv:
return fmt.Sprintf("V%d", idx-na)
case idx == na+nv:
return "gl_Position"
default:
return fmt.Sprintf("l%d", idx-(na+nv+1))
}
case &p.FragmentFunc.Block:
nv := len(p.Varyings)
switch {
case idx < nv:
return fmt.Sprintf("V%d", idx)
case idx == nv:
return "gl_FragCoord"
default:
return fmt.Sprintf("l%d", idx-(nv+1))
}
case UniformVariable:
return fmt.Sprintf("U%d", e.Index)
case LocalVariable:
idx := e.Index
switch b {
case &p.VertexFunc.Block:
na := len(p.Attributes)
nv := len(p.Varyings)
switch {
case idx < na:
return fmt.Sprintf("A%d", idx)
case idx < na+nv:
return fmt.Sprintf("V%d", idx-na)
case idx == na+nv:
return "gl_Position"
default:
return fmt.Sprintf("l%d", idx)
return fmt.Sprintf("l%d", idx-(na+nv+1))
}
case &p.FragmentFunc.Block:
nv := len(p.Varyings)
switch {
case idx < nv:
return fmt.Sprintf("V%d", idx)
case idx == nv:
return "gl_FragCoord"
default:
return fmt.Sprintf("l%d", idx-(nv+1))
}
default:
return fmt.Sprintf("?(unexpected variable type: %d)", e.Variable.Type)
return fmt.Sprintf("l%d", idx)
}
case StructMember:
return fmt.Sprintf("M%d", e.Index)
case BuiltinFuncExpr:
return string(e.BuiltinFunc)
case SwizzlingExpr:

View File

@ -81,13 +81,17 @@ func floatExpr(value float32) Expr {
}
}
func varNameExpr(vt VariableType, index int) Expr {
func uniformVariableExpr(index int) Expr {
return Expr{
Type: VarName,
Variable: Variable{
Type: vt,
Index: index,
},
Type: UniformVariable,
Index: index,
}
}
func localVariableExpr(index int) Expr {
return Expr{
Type: LocalVariable,
Index: index,
}
}
@ -243,7 +247,7 @@ varying vec3 V0;`,
Block: block(
nil,
returnStmt(
varNameExpr(Local, 0),
localVariableExpr(0),
),
),
},
@ -337,11 +341,11 @@ varying vec3 V0;`,
Block: block(
nil,
assignStmt(
varNameExpr(Local, 2),
localVariableExpr(2),
binaryExpr(
Add,
varNameExpr(Local, 0),
varNameExpr(Local, 1),
localVariableExpr(0),
localVariableExpr(1),
),
),
),
@ -369,11 +373,11 @@ varying vec3 V0;`,
Block: block(
nil,
assignStmt(
varNameExpr(Local, 3),
localVariableExpr(3),
selectionExpr(
varNameExpr(Local, 0),
varNameExpr(Local, 1),
varNameExpr(Local, 2),
localVariableExpr(0),
localVariableExpr(1),
localVariableExpr(2),
),
),
),
@ -405,11 +409,11 @@ varying vec3 V0;`,
),
),
assignStmt(
varNameExpr(Local, 2),
localVariableExpr(2),
callExpr(
functionExpr(2),
varNameExpr(Local, 0),
varNameExpr(Local, 1),
localVariableExpr(0),
localVariableExpr(1),
),
),
),
@ -437,11 +441,11 @@ varying vec3 V0;`,
Block: block(
nil,
assignStmt(
varNameExpr(Local, 2),
localVariableExpr(2),
callExpr(
builtinFuncExpr(Min),
varNameExpr(Local, 0),
varNameExpr(Local, 1),
localVariableExpr(0),
localVariableExpr(1),
),
),
),
@ -467,9 +471,9 @@ varying vec3 V0;`,
Block: block(
nil,
assignStmt(
varNameExpr(Local, 1),
localVariableExpr(1),
fieldSelectorExpr(
varNameExpr(Local, 0),
localVariableExpr(0),
swizzlingExpr("xz"),
),
),
@ -499,21 +503,21 @@ varying vec3 V0;`,
ifStmt(
binaryExpr(
EqualOp,
varNameExpr(Local, 0),
localVariableExpr(0),
floatExpr(0),
),
block(
nil,
assignStmt(
varNameExpr(Local, 2),
varNameExpr(Local, 0),
localVariableExpr(2),
localVariableExpr(0),
),
),
block(
nil,
assignStmt(
varNameExpr(Local, 2),
varNameExpr(Local, 1),
localVariableExpr(2),
localVariableExpr(1),
),
),
),
@ -552,8 +556,8 @@ varying vec3 V0;`,
block(
nil,
assignStmt(
varNameExpr(Local, 2),
varNameExpr(Local, 0),
localVariableExpr(2),
localVariableExpr(0),
),
),
),
@ -586,16 +590,16 @@ varying vec3 V0;`,
Block: block(
nil,
assignStmt(
varNameExpr(Local, 5),
varNameExpr(Local, 0),
localVariableExpr(5),
localVariableExpr(0),
),
assignStmt(
varNameExpr(Local, 3),
varNameExpr(Local, 1),
localVariableExpr(3),
localVariableExpr(1),
),
assignStmt(
varNameExpr(Local, 4),
varNameExpr(Local, 2),
localVariableExpr(4),
localVariableExpr(2),
),
),
},
@ -633,16 +637,16 @@ void main(void) {
Block: block(
nil,
assignStmt(
varNameExpr(Local, 5),
varNameExpr(Local, 0),
localVariableExpr(5),
localVariableExpr(0),
),
assignStmt(
varNameExpr(Local, 3),
varNameExpr(Local, 1),
localVariableExpr(3),
localVariableExpr(1),
),
assignStmt(
varNameExpr(Local, 4),
varNameExpr(Local, 2),
localVariableExpr(4),
localVariableExpr(2),
),
),
},
@ -654,16 +658,16 @@ void main(void) {
{Main: Float},
},
assignStmt(
varNameExpr(Local, 5),
varNameExpr(Local, 0),
localVariableExpr(5),
localVariableExpr(0),
),
assignStmt(
varNameExpr(Local, 3),
varNameExpr(Local, 1),
localVariableExpr(3),
localVariableExpr(1),
),
assignStmt(
varNameExpr(Local, 4),
varNameExpr(Local, 2),
localVariableExpr(4),
localVariableExpr(2),
),
),
},

View File

@ -83,7 +83,6 @@ const (
type Expr struct {
Type ExprType
Exprs []Expr
Variable Variable
Int int32
Float float32
BuiltinFunc BuiltinFunc
@ -97,7 +96,9 @@ type ExprType int
const (
IntExpr ExprType = iota
FloatExpr
VarName
UniformVariable
LocalVariable
StructMember
BuiltinFuncExpr
SwizzlingExpr
FunctionExpr
@ -109,19 +110,6 @@ const (
Index
)
type Variable struct {
Type VariableType
Index int
}
type VariableType int
const (
Uniform VariableType = iota
StructMember
Local
)
type Op string
const (