shaderir: Use auto names for variables

This commit is contained in:
Hajime Hoshi 2020-05-14 23:47:15 +09:00
parent ea0f2c9085
commit 71ee622997
3 changed files with 86 additions and 68 deletions

View File

@ -38,14 +38,14 @@ func (p *Program) Glsl() string {
p.structTypes = nil
var lines []string
for _, u := range p.Uniforms {
lines = append(lines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&u.Type, u.Name)))
for i, t := range p.Uniforms {
lines = append(lines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
}
for _, a := range p.Attributes {
lines = append(lines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&a.Type, a.Name)))
for i, t := range p.Attributes {
lines = append(lines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&t, fmt.Sprintf("A%d", i))))
}
for _, v := range p.Varyings {
lines = append(lines, fmt.Sprintf("varying %s;", p.glslVarDecl(&v.Type, v.Name)))
for i, t := range p.Varyings {
lines = append(lines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
}
for _, f := range p.Funcs {
lines = append(lines, p.glslFunc(&f)...)
@ -121,6 +121,19 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s
switch e.Type {
case Literal:
return e.Value
case VarName:
switch e.Variable.Type {
case Uniform:
return fmt.Sprintf("U%d", e.Variable.Index)
case Attribute:
return fmt.Sprintf("A%d", e.Variable.Index)
case Varying:
return fmt.Sprintf("V%d", e.Variable.Index)
case Local:
return fmt.Sprintf("l%d", e.Variable.Index)
default:
return fmt.Sprintf("?(unexpected variable type: %d)", e.Variable.Type)
}
case Ident:
return e.Value
case Unary:

View File

@ -20,6 +20,31 @@ import (
. "github.com/hajimehoshi/ebiten/internal/shaderir"
)
func assignStmt(lhs Expr, rhs Expr) Stmt {
return Stmt{
Type: Assign,
Exprs: []Expr{lhs, rhs},
}
}
func varNameExpr(vt VariableType, index int) Expr {
return Expr{
Type: VarName,
Variable: Variable{
Type: vt,
Index: index,
},
}
}
func binaryExpr(op Op, exprs ...Expr) Expr {
return Expr{
Type: Binary,
Op: op,
Exprs: exprs,
}
}
func TestOutput(t *testing.T) {
tests := []struct {
Name string
@ -34,11 +59,8 @@ func TestOutput(t *testing.T) {
{
Name: "Uniform",
Program: Program{
Uniforms: []Variable{
{
Name: "U0",
Type: Type{Main: Float},
},
Uniforms: []Type{
{Main: Float},
},
},
Glsl: `uniform float U0;`,
@ -46,14 +68,11 @@ func TestOutput(t *testing.T) {
{
Name: "UniformStruct",
Program: Program{
Uniforms: []Variable{
Uniforms: []Type{
{
Name: "U0",
Type: Type{
Main: Struct,
Sub: []Type{
{Main: Float},
},
Main: Struct,
Sub: []Type{
{Main: Float},
},
},
},
@ -66,23 +85,14 @@ uniform S0 U0;`,
{
Name: "Vars",
Program: Program{
Uniforms: []Variable{
{
Name: "U0",
Type: Type{Main: Float},
},
Uniforms: []Type{
{Main: Float},
},
Attributes: []Variable{
{
Name: "A0",
Type: Type{Main: Vec2},
},
Attributes: []Type{
{Main: Vec2},
},
Varyings: []Variable{
{
Name: "V0",
Type: Type{Main: Vec3},
},
Varyings: []Type{
{Main: Vec3},
},
},
Glsl: `uniform float U0;
@ -212,29 +222,13 @@ varying vec3 V0;`,
},
Block: Block{
Stmts: []Stmt{
{
Type: Assign,
Exprs: []Expr{
{
Type: Ident,
Value: "l2",
},
{
Type: Binary,
Op: Add,
Exprs: []Expr{
{
Type: Ident,
Value: "l0",
},
{
Type: Ident,
Value: "l1",
},
},
},
},
},
assignStmt(
varNameExpr(Local, 2),
binaryExpr(Add,
varNameExpr(Local, 0),
varNameExpr(Local, 1),
),
),
},
},
},

View File

@ -15,9 +15,9 @@
package shaderir
type Program struct {
Uniforms []Variable
Attributes []Variable
Varyings []Variable
Uniforms []Type
Attributes []Type
Varyings []Type
Funcs []Func
VertexEntry string
FragmentEntry string
@ -26,11 +26,6 @@ type Program struct {
structTypes []Type
}
type Variable struct {
Name string
Type Type
}
type Func struct {
Name string
InParams []Type
@ -69,16 +64,18 @@ const (
)
type Expr struct {
Type ExprType
Exprs []Expr
Value string
Op Op
Type ExprType
Exprs []Expr
Variable Variable
Value string
Op Op
}
type ExprType int
const (
Literal ExprType = iota
VarName
Ident
Unary
Binary
@ -87,6 +84,20 @@ const (
Index
)
type Variable struct {
Type VariableType
Index int
}
type VariableType int
const (
Uniform VariableType = iota
Attribute
Varying
Local
)
type Op string
const (