internal/shaderir: improve FilterUniformVariables

```
name      old time/op  new time/op  delta
Filter-8  31.7ns ± 1%  29.9ns ± 1%  -5.60%  (p=0.000 n=9+10)
```

Updates #2601
This commit is contained in:
Hajime Hoshi 2023-08-20 03:08:35 +09:00
parent 3b67b91bb2
commit 4f2327536c
2 changed files with 40 additions and 6 deletions

View File

@ -0,0 +1,34 @@
// Copyright 2023 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shaderir_test
import (
"testing"
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
)
func BenchmarkFilter(b *testing.B) {
src := builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)
s, err := graphics.CompileShader(src)
if err != nil {
b.Fatal(err)
}
uniforms := make([]uint32, graphics.PreservedUniformUint32Count)
for i := 0; i < b.N; i++ {
s.FilterUniformVariables(uniforms)
}
}

View File

@ -40,7 +40,7 @@ type Program struct {
FragmentFunc FragmentFunc FragmentFunc FragmentFunc
Unit Unit Unit Unit
uniformMask []uint32 uniformFactors []uint32
} }
type Func struct { type Func struct {
@ -464,7 +464,7 @@ func (p *Program) appendReachableUniformVariablesFromBlock(indices []int, block
// 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.uniformFactors == nil {
indices := p.appendReachableUniformVariablesFromBlock(nil, p.VertexFunc.Block) indices := p.appendReachableUniformVariablesFromBlock(nil, p.VertexFunc.Block)
indices = p.appendReachableUniformVariablesFromBlock(indices, p.FragmentFunc.Block) indices = p.appendReachableUniformVariablesFromBlock(indices, p.FragmentFunc.Block)
reachableUniforms := make([]bool, len(p.Uniforms)) reachableUniforms := make([]bool, len(p.Uniforms))
@ -475,14 +475,14 @@ func (p *Program) FilterUniformVariables(uniforms []uint32) {
fs := make([]uint32, typ.Uint32Count()) fs := make([]uint32, typ.Uint32Count())
if reachableUniforms[i] { if reachableUniforms[i] {
for j := range fs { for j := range fs {
fs[j] = (1 << 32) - 1 fs[j] = 1
} }
} }
p.uniformMask = append(p.uniformMask, fs...) p.uniformFactors = append(p.uniformFactors, fs...)
} }
} }
for i, factor := range p.uniformMask { for i, factor := range p.uniformFactors {
uniforms[i] &= factor uniforms[i] *= factor
} }
} }