internal/shaderir: refactoring

This commit is contained in:
Hajime Hoshi 2022-11-03 17:44:11 +09:00
parent e2c25dbc6d
commit 7dbb078a9e
2 changed files with 15 additions and 25 deletions

View File

@ -19,7 +19,6 @@ import (
"go/constant"
"go/token"
"regexp"
"sort"
"strings"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
@ -99,12 +98,6 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh
structNames: map[string]string{},
}
indexToFunc := map[int]*shaderir.Func{}
for _, f := range p.Funcs {
f := f
indexToFunc[f.Index] = &f
}
// Vertex func
var vslines []string
{
@ -136,12 +129,7 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh
var funcs []*shaderir.Func
if p.VertexFunc.Block != nil {
indices := p.ReachableFuncIndicesFromVertexShader()
sort.Ints(indices)
funcs = make([]*shaderir.Func, 0, len(indices))
for _, idx := range indices {
funcs = append(funcs, indexToFunc[idx])
}
funcs = p.ReachableFuncsFromVertexShader()
} else {
// When a vertex entry point is not defined, allow to put all the functions. This is useful for testing.
funcs = make([]*shaderir.Func, 0, len(p.Funcs))
@ -234,12 +222,7 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh
var funcs []*shaderir.Func
if p.VertexFunc.Block != nil {
indices := p.ReachableFuncIndicesFromFragmentShader()
sort.Ints(indices)
funcs = make([]*shaderir.Func, 0, len(indices))
for _, idx := range indices {
funcs = append(funcs, indexToFunc[idx])
}
funcs = p.ReachableFuncsFromFragmentShader()
} else {
// When a fragment entry point is not defined, allow to put all the functions. This is useful for testing.
funcs = make([]*shaderir.Func, 0, len(p.Funcs))

View File

@ -18,6 +18,7 @@ package shaderir
import (
"go/constant"
"go/token"
"sort"
"strings"
)
@ -363,22 +364,28 @@ func IsValidSwizzling(s string) bool {
return false
}
func (p *Program) ReachableFuncIndicesFromVertexShader() []int {
return p.reachableFuncIndicesFromBlockEntryPoint(p.VertexFunc.Block)
func (p *Program) ReachableFuncsFromVertexShader() []*Func {
return p.reachableFuncsFromBlockEntryPoint(p.VertexFunc.Block)
}
func (p *Program) ReachableFuncIndicesFromFragmentShader() []int {
return p.reachableFuncIndicesFromBlockEntryPoint(p.FragmentFunc.Block)
func (p *Program) ReachableFuncsFromFragmentShader() []*Func {
return p.reachableFuncsFromBlockEntryPoint(p.FragmentFunc.Block)
}
func (p *Program) reachableFuncIndicesFromBlockEntryPoint(b *Block) []int {
func (p *Program) reachableFuncsFromBlockEntryPoint(b *Block) []*Func {
indexToFunc := map[int]*Func{}
for _, f := range p.Funcs {
f := f
indexToFunc[f.Index] = &f
}
visited := map[int]struct{}{}
return reachableFuncIndicesFromBlock(b, indexToFunc, visited)
indices := reachableFuncIndicesFromBlock(b, indexToFunc, visited)
sort.Ints(indices)
funcs := make([]*Func, 0, len(indices))
for _, i := range indices {
funcs = append(funcs, indexToFunc[i])
}
return funcs
}
func reachableFuncIndicesFromBlock(b *Block, indexToFunc map[int]*Func, visited map[int]struct{}) []int {