shaderir: Add more tests

This commit is contained in:
Hajime Hoshi 2020-05-12 23:32:32 +09:00
parent dc3ed76e3e
commit a3105d6fca
3 changed files with 74 additions and 20 deletions

View File

@ -20,7 +20,7 @@ import (
) )
func (p *Program) structName(t *Type) string { func (p *Program) structName(t *Type) string {
if t.MainType != Struct { if t.Main != Struct {
panic("shaderir: the given type at structName must be a struct") panic("shaderir: the given type at structName must be a struct")
} }
s := t.serialize() s := t.serialize()
@ -41,11 +41,20 @@ func (p *Program) Glsl() string {
for _, u := range p.Uniforms { for _, u := 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(&u.Type, u.Name)))
} }
for _, a := range p.Attributes {
lines = append(lines, fmt.Sprintf("attribute %s;", p.glslVarDecl(&a.Type, a.Name)))
}
for _, v := range p.Varyings {
lines = append(lines, fmt.Sprintf("varying %s;", p.glslVarDecl(&v.Type, v.Name)))
}
for _, f := range p.Funcs {
lines = append(lines, p.glslFunc(&f)...)
}
var stLines []string var stLines []string
for i, t := range p.structTypes { for i, t := range p.structTypes {
stLines = append(stLines, fmt.Sprintf("struct S%d {", i)) stLines = append(stLines, fmt.Sprintf("struct S%d {", i))
for j, st := range t.SubTypes { for j, st := range t.Sub {
stLines = append(stLines, fmt.Sprintf("\t%s;", p.glslVarDecl(&st, fmt.Sprintf("M%d", j)))) stLines = append(stLines, fmt.Sprintf("\t%s;", p.glslVarDecl(&st, fmt.Sprintf("M%d", j))))
} }
stLines = append(stLines, "};") stLines = append(stLines, "};")
@ -56,16 +65,23 @@ func (p *Program) Glsl() string {
} }
func (p *Program) glslVarDecl(t *Type, varname string) string { func (p *Program) glslVarDecl(t *Type, varname string) string {
switch t.MainType { switch t.Main {
case None: case None:
return "?(none)" return "?(none)"
case Image2D: case Image2D:
panic("not implemented") panic("not implemented")
case Array: case Array:
return fmt.Sprintf("") panic("not implemented")
case Struct: case Struct:
return fmt.Sprintf("%s %s", p.structName(t), varname) return fmt.Sprintf("%s %s", p.structName(t), varname)
default: default:
return fmt.Sprintf("%s %s", t.MainType.Glsl(), varname) return fmt.Sprintf("%s %s", t.Main.Glsl(), varname)
}
}
func (p *Program) glslFunc(f *Func) []string {
return []string{
fmt.Sprintf(`void %s(void) {`, f.Name),
`}`,
} }
} }

View File

@ -36,23 +36,23 @@ func TestOutput(t *testing.T) {
Program: Program{ Program: Program{
Uniforms: []Variable{ Uniforms: []Variable{
{ {
Name: "U1", Name: "U0",
Type: Type{MainType: Float}, Type: Type{Main: Float},
}, },
}, },
}, },
Glsl: `uniform float U1;`, Glsl: `uniform float U0;`,
}, },
{ {
Name: "UniformStruct", Name: "UniformStruct",
Program: Program{ Program: Program{
Uniforms: []Variable{ Uniforms: []Variable{
{ {
Name: "U1", Name: "U0",
Type: Type{ Type: Type{
MainType: Struct, Main: Struct,
SubTypes: []Type{ Sub: []Type{
{MainType: Float}, {Main: Float},
}, },
}, },
}, },
@ -61,7 +61,45 @@ func TestOutput(t *testing.T) {
Glsl: `struct S0 { Glsl: `struct S0 {
float M0; float M0;
}; };
uniform S0 U1;`, uniform S0 U0;`,
},
{
Name: "Vars",
Program: Program{
Uniforms: []Variable{
{
Name: "U0",
Type: Type{Main: Float},
},
},
Attributes: []Variable{
{
Name: "A0",
Type: Type{Main: Vec2},
},
},
Varyings: []Variable{
{
Name: "V0",
Type: Type{Main: Vec3},
},
},
},
Glsl: `uniform float U0;
attribute vec2 A0;
varying vec3 V0;`,
},
{
Name: "Function",
Program: Program{
Funcs: []Func{
{
Name: "F0",
},
},
},
Glsl: `void F0(void) {
}`,
}, },
} }
for _, tc := range tests { for _, tc := range tests {

View File

@ -20,13 +20,13 @@ import (
) )
type Type struct { type Type struct {
MainType BasicType Main BasicType
SubTypes []Type Sub []Type
Length int Length int
} }
func (t *Type) serialize() string { func (t *Type) serialize() string {
switch t.MainType { switch t.Main {
case None: case None:
return "none" return "none"
case Bool: case Bool:
@ -48,11 +48,11 @@ func (t *Type) serialize() string {
case Image2D: case Image2D:
return "image2d" return "image2d"
case Array: case Array:
return fmt.Sprintf("%s[%d]", t.SubTypes[0].serialize(), t.Length) return fmt.Sprintf("%s[%d]", t.Sub[0].serialize(), t.Length)
case Struct: case Struct:
str := "struct{" str := "struct{"
sub := make([]string, 0, len(t.SubTypes)) sub := make([]string, 0, len(t.Sub))
for _, st := range t.SubTypes { for _, st := range t.Sub {
sub = append(sub, st.serialize()) sub = append(sub, st.serialize())
} }
str += strings.Join(sub, ",") str += strings.Join(sub, ",")