diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 18b148615..d1c4deb59 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -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)) diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index b0745a2f2..7ca989f13 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -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 {