diff --git a/internal/shaderir/glsl.go b/internal/shaderir/glsl.go index f1ca54355..b4a3dc682 100644 --- a/internal/shaderir/glsl.go +++ b/internal/shaderir/glsl.go @@ -64,6 +64,21 @@ func (p *Program) Glsl() string { return strings.Join(lines, "\n") + "\n" } +func (p *Program) glslType(t *Type) string { + switch t.Main { + case None: + return "void" + case Image2D: + panic("not implemented") + case Array: + panic("not implemented") + case Struct: + return p.structName(t) + default: + return t.Main.Glsl() + } +} + func (p *Program) glslVarDecl(t *Type, varname string) string { switch t.Main { case None: @@ -100,7 +115,7 @@ func (p *Program) glslFunc(f *Func) []string { } var lines []string - lines = append(lines, fmt.Sprintf("void %s(%s) {", f.Name, argsstr)) + lines = append(lines, fmt.Sprintf("%s %s(%s) {", p.glslType(&f.Return), f.Name, argsstr)) lines = append(lines, p.glslBlock(&f.Block, f, 0, idx)...) lines = append(lines, "}") @@ -217,7 +232,11 @@ func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []s case Break: lines = append(lines, idt+"break;") case Return: - lines = append(lines, idt+"return;") + if len(s.Exprs) == 0 { + lines = append(lines, idt+"return;") + } else { + lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, glslExpr(&s.Exprs[0]))) + } case Discard: lines = append(lines, idt+"discard;") default: diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index cc29e7950..f8f4a7a28 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -41,6 +41,13 @@ func blockStmt(block Block) Stmt { } } +func returnStmt(expr Expr) Stmt { + return Stmt{ + Type: Return, + Exprs: []Expr{expr}, + } +} + func assignStmt(lhs Expr, rhs Expr) Stmt { return Stmt{ Type: Assign, @@ -208,6 +215,29 @@ varying vec3 V0;`, }, }, Glsl: `void F0(in float l0, in vec2 l1, in vec4 l2, inout mat2 l3, out mat4 l4) { +}`, + }, + { + Name: "FuncReturn", + Program: Program{ + Funcs: []Func{ + { + Name: "F0", + InParams: []Type{ + {Main: Float}, + }, + Return: Type{Main: Float}, + Block: block( + nil, + returnStmt( + varNameExpr(Local, 0), + ), + ), + }, + }, + }, + Glsl: `float F0(in float l0) { + return l0; }`, }, { diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index 4cd3b02d0..f6a8d6b14 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -33,6 +33,7 @@ type Func struct { InParams []Type InOutParams []Type OutParams []Type + Return Type Block Block }