ebiten, internal/builtinshader: use an array instead of a map

runtime.mapaccess2 is one of heavy function calls.

Updates #2601
This commit is contained in:
Hajime Hoshi 2023-08-20 04:58:36 +09:00
parent 10c9f489ce
commit d9797423e5
2 changed files with 16 additions and 26 deletions

View File

@ -28,6 +28,8 @@ const (
FilterLinear
)
const FilterCount = 2
type Address int
const (
@ -36,19 +38,15 @@ const (
AddressRepeat
)
const AddressCount = 3
const (
UniformColorMBody = "ColorMBody"
UniformColorMTranslation = "ColorMTranslation"
)
type key struct {
Filter Filter
Address Address
UseColorM bool
}
var (
shaders = map[key][]byte{}
shaders [FilterCount][AddressCount][2][]byte
shadersM sync.Mutex
)
@ -132,12 +130,11 @@ func Shader(filter Filter, address Address, useColorM bool) []byte {
shadersM.Lock()
defer shadersM.Unlock()
k := key{
Filter: filter,
Address: address,
UseColorM: useColorM,
var c int
if useColorM {
c = 1
}
if s, ok := shaders[k]; ok {
if s := shaders[filter][address][c]; s != nil {
return s
}
@ -165,6 +162,6 @@ func Shader(filter Filter, address Address, useColorM bool) []byte {
}
b := buf.Bytes()
shaders[k] = b
shaders[filter][address][c] = b
return b
}

View File

@ -59,14 +59,8 @@ func (s *Shader) appendUniforms(dst []uint32, uniforms map[string]any) []uint32
return s.shader.AppendUniforms(dst, uniforms)
}
type builtinShaderKey struct {
filter builtinshader.Filter
address builtinshader.Address
useColorM bool
}
var (
builtinShaders = map[builtinShaderKey]*Shader{}
builtinShaders [builtinshader.FilterCount][builtinshader.AddressCount][2]*Shader
builtinShadersM sync.Mutex
)
@ -74,12 +68,11 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u
builtinShadersM.Lock()
defer builtinShadersM.Unlock()
key := builtinShaderKey{
filter: filter,
address: address,
useColorM: useColorM,
var c int
if useColorM {
c = 1
}
if s, ok := builtinShaders[key]; ok {
if s := builtinShaders[filter][address][c]; s != nil {
return s
}
@ -100,6 +93,6 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u
shader = s
}
builtinShaders[key] = shader
builtinShaders[filter][address][c] = shader
return shader
}