Skip to content

Commit

Permalink
internal/shaderir: improve FilterUniformVariables
Browse files Browse the repository at this point in the history
```
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
  • Loading branch information
hajimehoshi committed Aug 19, 2023
1 parent 3b67b91 commit 4f23275
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
34 changes: 34 additions & 0 deletions internal/shaderir/bench_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 6 additions & 6 deletions internal/shaderir/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Program struct {
FragmentFunc FragmentFunc
Unit Unit

uniformMask []uint32
uniformFactors []uint32
}

type Func struct {
Expand Down Expand Up @@ -464,7 +464,7 @@ func (p *Program) appendReachableUniformVariablesFromBlock(indices []int, block
// FilterUniformVariables replaces uniform variables with nil when they are not used.
// By minimizing uniform variables, more commands can be merged in the graphicscommand package.
func (p *Program) FilterUniformVariables(uniforms []uint32) {
if p.uniformMask == nil {
if p.uniformFactors == nil {
indices := p.appendReachableUniformVariablesFromBlock(nil, p.VertexFunc.Block)
indices = p.appendReachableUniformVariablesFromBlock(indices, p.FragmentFunc.Block)
reachableUniforms := make([]bool, len(p.Uniforms))
Expand All @@ -475,14 +475,14 @@ func (p *Program) FilterUniformVariables(uniforms []uint32) {
fs := make([]uint32, typ.Uint32Count())
if reachableUniforms[i] {
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 {
uniforms[i] &= factor
for i, factor := range p.uniformFactors {
uniforms[i] *= factor
}
}

0 comments on commit 4f23275

Please sign in to comment.