internal/shaderir: refactoring: use the append pattern

This commit is contained in:
Hajime Hoshi 2023-05-04 14:34:09 +09:00
parent e4c9d6705c
commit ceb2bfc89c
3 changed files with 15 additions and 16 deletions

View File

@ -14,6 +14,6 @@
package shaderir package shaderir
func (p *Program) ReachableUniformVariablesFromBlock(block *Block) []int { func (p *Program) AppendReachableUniformVariablesFromBlock(indices []int, block *Block) []int {
return p.reachableUniformVariablesFromBlock(block) return p.appendReachableUniformVariablesFromBlock(indices, block)
} }

View File

@ -436,7 +436,7 @@ func walkExprsInExpr(f func(expr *Expr), expr *Expr) {
} }
} }
func (p *Program) reachableUniformVariablesFromBlock(block *Block) []int { func (p *Program) appendReachableUniformVariablesFromBlock(indices []int, block *Block) []int {
indexToFunc := map[int]*Func{} indexToFunc := map[int]*Func{}
for _, f := range p.Funcs { for _, f := range p.Funcs {
f := f f := f
@ -444,12 +444,12 @@ func (p *Program) reachableUniformVariablesFromBlock(block *Block) []int {
} }
visitedFuncs := map[int]struct{}{} visitedFuncs := map[int]struct{}{}
indices := map[int]struct{}{} indicesSet := map[int]struct{}{}
var f func(expr *Expr) var f func(expr *Expr)
f = func(expr *Expr) { f = func(expr *Expr) {
switch expr.Type { switch expr.Type {
case UniformVariable: case UniformVariable:
indices[expr.Index] = struct{}{} indicesSet[expr.Index] = struct{}{}
case FunctionExpr: case FunctionExpr:
if _, ok := visitedFuncs[expr.Index]; ok { if _, ok := visitedFuncs[expr.Index]; ok {
return return
@ -460,24 +460,21 @@ func (p *Program) reachableUniformVariablesFromBlock(block *Block) []int {
} }
walkExprs(f, block) walkExprs(f, block)
is := make([]int, 0, len(indices)) for i := range indicesSet {
for i := range indices { indices = append(indices, i)
is = append(is, i)
} }
sort.Ints(is) return indices
return is
} }
// FilterUniformVariables replaces uniform variables with nil when they are not used. // FilterUniformVariables replaces uniform variables with nil when they are not used.
// By minimizing uniform variables, more commands can be merged in the graphicscommand package. // By minimizing uniform variables, more commands can be merged in the graphicscommand package.
func (p *Program) FilterUniformVariables(uniforms []uint32) { func (p *Program) FilterUniformVariables(uniforms []uint32) {
if p.uniformMask == nil { if p.uniformMask == nil {
indices := p.appendReachableUniformVariablesFromBlock(nil, p.VertexFunc.Block)
indices = p.appendReachableUniformVariablesFromBlock(indices, p.FragmentFunc.Block)
reachableUniforms := make([]bool, len(p.Uniforms)) reachableUniforms := make([]bool, len(p.Uniforms))
for _, i := range p.reachableUniformVariablesFromBlock(p.VertexFunc.Block) { for _, idx := range indices {
reachableUniforms[i] = true reachableUniforms[idx] = true
}
for _, i := range p.reachableUniformVariablesFromBlock(p.FragmentFunc.Block) {
reachableUniforms[i] = true
} }
for i, typ := range p.Uniforms { for i, typ := range p.Uniforms {
fs := make([]uint32, typ.Uint32Count()) fs := make([]uint32, typ.Uint32Count())

View File

@ -15,6 +15,7 @@
package shaderir_test package shaderir_test
import ( import (
"sort"
"testing" "testing"
"github.com/hajimehoshi/ebiten/v2/internal/shader" "github.com/hajimehoshi/ebiten/v2/internal/shader"
@ -105,7 +106,8 @@ func neverCalled() float {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
got := ir.ReachableUniformVariablesFromBlock(ir.Funcs[c.index].Block) got := ir.AppendReachableUniformVariablesFromBlock(nil, ir.Funcs[c.index].Block)
sort.Ints(got)
want := c.expected want := c.expected
if !areIntSlicesEqual(got, want) { if !areIntSlicesEqual(got, want) {
t.Errorf("test: %v, got: %v, want: %v", c, got, want) t.Errorf("test: %v, got: %v, want: %v", c, got, want)