diff --git a/internal/shaderir/glsl.go b/internal/shaderir/glsl.go index 6f976bb66..7316bba60 100644 --- a/internal/shaderir/glsl.go +++ b/internal/shaderir/glsl.go @@ -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), + `}`, } } diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index beac69b4e..567a62246 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -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 { diff --git a/internal/shaderir/type.go b/internal/shaderir/type.go index 5faaf623e..db2207d6a 100644 --- a/internal/shaderir/type.go +++ b/internal/shaderir/type.go @@ -20,13 +20,13 @@ import ( ) type Type struct { - MainType BasicType - SubTypes []Type - Length int + 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, ",")