shader: Refactoring: Simplify calculation of LocalVarIndexOffset

This commit is contained in:
Hajime Hoshi 2020-09-08 02:42:31 +09:00
parent 41d4fc288b
commit 0a0401e217
3 changed files with 9 additions and 24 deletions

View File

@ -695,27 +695,11 @@ func (cs *compileState) parseBlock(outer *block, fname string, stmts []ast.Stmt,
} }
var offset int var offset int
switch { for b := outer; b != nil; b = b.outer {
case outer.outer == nil && fname == cs.vertexEntry: offset += len(b.vars)
offset = 0 }
case outer.outer == nil && fname == cs.fragmentEntry: if outer == &cs.global {
offset = 0 offset += len(inParams) + len(outParams)
case outer.outer == nil:
offset = len(inParams) + len(outParams)
default:
for b := outer; b != nil; b = b.outer {
offset += len(b.vars)
}
if fname == cs.vertexEntry {
offset -= len(cs.ir.Attributes)
offset-- // position
offset -= len(cs.ir.Varyings)
}
if fname == cs.fragmentEntry {
offset-- // position
offset -= len(cs.ir.Varyings)
offset-- // color
}
} }
block := &block{ block := &block{

View File

@ -346,8 +346,8 @@ func (c *compileContext) glslBlock(p *shaderir.Program, topBlock, block *shaderi
idt := strings.Repeat("\t", level+1) idt := strings.Repeat("\t", level+1)
var lines []string var lines []string
for i, t := range block.LocalVars { for i := range block.LocalVars {
name := fmt.Sprintf("l%d", block.LocalVarIndexOffset+i) name, t := localVariable(p, topBlock, block, block.LocalVarIndexOffset+i)
switch t.Main { switch t.Main {
case shaderir.Array: case shaderir.Array:
lines = append(lines, fmt.Sprintf("%s%s;", idt, c.glslVarDecl(p, &t, name))) lines = append(lines, fmt.Sprintf("%s%s;", idt, c.glslVarDecl(p, &t, name)))

View File

@ -321,8 +321,9 @@ func (c *compileContext) metalBlock(p *shaderir.Program, topBlock, block *shader
var lines []string var lines []string
for i, t := range block.LocalVars { for i, t := range block.LocalVars {
// The type is None e.g., when the variable is a for-loop counter. // The type is None e.g., when the variable is a for-loop counter.
name := localVariableName(p, topBlock, block.LocalVarIndexOffset+i)
if t.Main != shaderir.None { if t.Main != shaderir.None {
lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.metalVarDecl(p, &t, fmt.Sprintf("l%d", block.LocalVarIndexOffset+i), false, false), c.metalVarInit(p, &t))) lines = append(lines, fmt.Sprintf("%s%s = %s;", idt, c.metalVarDecl(p, &t, name, false, false), c.metalVarInit(p, &t)))
} }
} }