shaderir: Avoid duplications of local variable names

This commit is contained in:
Hajime Hoshi 2020-05-14 03:52:36 +09:00
parent 32a88b3b03
commit 1ed004ae05
2 changed files with 49 additions and 9 deletions

View File

@ -101,23 +101,19 @@ func (p *Program) glslFunc(f *Func) []string {
var lines []string var lines []string
lines = append(lines, fmt.Sprintf("void %s(%s) {", f.Name, argsstr)) lines = append(lines, fmt.Sprintf("void %s(%s) {", f.Name, argsstr))
lines = append(lines, p.glslBlock(&f.Block, f, 0)...) lines = append(lines, p.glslBlock(&f.Block, f, 0, idx)...)
lines = append(lines, "}") lines = append(lines, "}")
return lines return lines
} }
func (p *Program) glslBlock(b *Block, f *Func, level int) []string { func (p *Program) glslBlock(b *Block, f *Func, level int, localVarIndex int) []string {
idt := strings.Repeat("\t", level+1) idt := strings.Repeat("\t", level+1)
var lines []string var lines []string
var idx int
if level == 0 {
idx = len(f.InParams) + len(f.InOutParams) + len(f.OutParams)
}
for _, t := range b.LocalVars { for _, t := range b.LocalVars {
lines = append(lines, fmt.Sprintf("%s%s;", idt, p.glslVarDecl(&t, fmt.Sprintf("l%d", idx)))) lines = append(lines, fmt.Sprintf("%s%s;", idt, p.glslVarDecl(&t, fmt.Sprintf("l%d", localVarIndex))))
idx++ localVarIndex++
} }
var glslExpr func(e *Expr) string var glslExpr func(e *Expr) string
@ -149,7 +145,7 @@ func (p *Program) glslBlock(b *Block, f *Func, level int) []string {
lines = append(lines, fmt.Sprintf("%s%s;", idt, glslExpr(&s.Exprs[0]))) lines = append(lines, fmt.Sprintf("%s%s;", idt, glslExpr(&s.Exprs[0])))
case BlockStmt: case BlockStmt:
lines = append(lines, idt+"{") lines = append(lines, idt+"{")
lines = append(lines, p.glslBlock(s.Block, f, level+1)...) lines = append(lines, p.glslBlock(s.Block, f, level+1, localVarIndex)...)
lines = append(lines, idt+"}") lines = append(lines, idt+"}")
case Assign: case Assign:
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, glslExpr(&s.Exprs[0]), glslExpr(&s.Exprs[1]))) lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, glslExpr(&s.Exprs[0]), glslExpr(&s.Exprs[1])))

View File

@ -151,6 +151,50 @@ varying vec3 V0;`,
Glsl: `void F0(in float l0, inout float l1, out float l2) { Glsl: `void F0(in float l0, inout float l1, out float l2) {
mat4 l3; mat4 l3;
mat4 l4; mat4 l4;
}`,
},
{
Name: "FuncBlocks",
Program: Program{
Funcs: []Func{
{
Name: "F0",
InParams: []Type{
{Main: Float},
},
InOutParams: []Type{
{Main: Float},
},
OutParams: []Type{
{Main: Float},
},
Block: Block{
LocalVars: []Type{
{Main: Mat4},
{Main: Mat4},
},
Stmts: []Stmt{
{
Type: BlockStmt,
Block: &Block{
LocalVars: []Type{
{Main: Mat4},
{Main: Mat4},
},
},
},
},
},
},
},
},
Glsl: `void F0(in float l0, inout float l1, out float l2) {
mat4 l3;
mat4 l4;
{
mat4 l5;
mat4 l6;
}
}`, }`,
}, },
{ {