shaderir: Implement return

This commit is contained in:
Hajime Hoshi 2020-05-16 20:16:04 +09:00
parent 57d80c185e
commit eb5a2efad8
3 changed files with 52 additions and 2 deletions

View File

@ -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:

View File

@ -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;
}`,
},
{

View File

@ -33,6 +33,7 @@ type Func struct {
InParams []Type
InOutParams []Type
OutParams []Type
Return Type
Block Block
}