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 {
if t.MainType != Struct {
if t.Main != Struct {
panic("shaderir: the given type at structName must be a struct")
}
s := t.serialize()
@ -41,11 +41,20 @@ func (p *Program) Glsl() string {
for _, u := range p.Uniforms {
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
for i, t := range p.structTypes {
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, "};")
@ -56,16 +65,23 @@ func (p *Program) Glsl() string {
}
func (p *Program) glslVarDecl(t *Type, varname string) string {
switch t.MainType {
switch t.Main {
case None:
return "?(none)"
case Image2D:
panic("not implemented")
case Array:
return fmt.Sprintf("")
panic("not implemented")
case Struct:
return fmt.Sprintf("%s %s", p.structName(t), varname)
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{
Uniforms: []Variable{
{
Name: "U1",
Type: Type{MainType: Float},
Name: "U0",
Type: Type{Main: Float},
},
},
},
Glsl: `uniform float U1;`,
Glsl: `uniform float U0;`,
},
{
Name: "UniformStruct",
Program: Program{
Uniforms: []Variable{
{
Name: "U1",
Name: "U0",
Type: Type{
MainType: Struct,
SubTypes: []Type{
{MainType: Float},
Main: Struct,
Sub: []Type{
{Main: Float},
},
},
},
@ -61,7 +61,45 @@ func TestOutput(t *testing.T) {
Glsl: `struct S0 {
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 {

View File

@ -20,13 +20,13 @@ import (
)
type Type struct {
MainType BasicType
SubTypes []Type
Main BasicType
Sub []Type
Length int
}
func (t *Type) serialize() string {
switch t.MainType {
switch t.Main {
case None:
return "none"
case Bool:
@ -48,11 +48,11 @@ func (t *Type) serialize() string {
case Image2D:
return "image2d"
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:
str := "struct{"
sub := make([]string, 0, len(t.SubTypes))
for _, st := range t.SubTypes {
sub := make([]string, 0, len(t.Sub))
for _, st := range t.Sub {
sub = append(sub, st.serialize())
}
str += strings.Join(sub, ",")