From 156ed320cce6726529b8fd6828a0f588eaae124b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 17 May 2020 04:28:03 +0900 Subject: [PATCH] shaderir: Remove Variable and VariableType --- internal/shaderir/glsl.go | 63 +++++++++++------------ internal/shaderir/ir_test.go | 98 +++++++++++++++++++----------------- internal/shaderir/program.go | 18 ++----- 3 files changed, 83 insertions(+), 96 deletions(-) diff --git a/internal/shaderir/glsl.go b/internal/shaderir/glsl.go index b94912705..2ded64789 100644 --- a/internal/shaderir/glsl.go +++ b/internal/shaderir/glsl.go @@ -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: diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index 1b41a1171..7a747ac59 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -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), ), ), }, diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index fa2d8a3c0..6175e2045 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -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 (