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 p.structTypes = nil
var lines []string var lines []string
for _, u := range p.Uniforms { for i, t := range p.Uniforms {
lines = append(lines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&u.Type, u.Name))) lines = append(lines, fmt.Sprintf("uniform %s;", p.glslVarDecl(&t, fmt.Sprintf("U%d", i))))
} }
for _, a := range p.Attributes { for i, t := range p.Attributes {
lines = append(lines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&a.Type, a.Name))) lines = append(lines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&t, fmt.Sprintf("A%d", i))))
} }
for _, v := range p.Varyings { for i, t := range p.Varyings {
lines = append(lines, fmt.Sprintf("varying %s;", p.glslVarDecl(&v.Type, v.Name))) lines = append(lines, fmt.Sprintf("varying %s;", p.glslVarDecl(&t, fmt.Sprintf("V%d", i))))
} }
for _, f := range p.Funcs { for _, f := range p.Funcs {
lines = append(lines, p.glslFunc(&f)...) 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 { switch e.Type {
case Literal: case Literal:
return e.Value 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: case Ident:
return e.Value return e.Value
case Unary: case Unary:

View File

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

View File

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