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 FilterLinear
) )
const FilterCount = 2
type Address int type Address int
const ( const (
@ -36,19 +38,15 @@ const (
AddressRepeat AddressRepeat
) )
const AddressCount = 3
const ( const (
UniformColorMBody = "ColorMBody" UniformColorMBody = "ColorMBody"
UniformColorMTranslation = "ColorMTranslation" UniformColorMTranslation = "ColorMTranslation"
) )
type key struct {
Filter Filter
Address Address
UseColorM bool
}
var ( var (
shaders = map[key][]byte{} shaders [FilterCount][AddressCount][2][]byte
shadersM sync.Mutex shadersM sync.Mutex
) )
@ -132,12 +130,11 @@ func Shader(filter Filter, address Address, useColorM bool) []byte {
shadersM.Lock() shadersM.Lock()
defer shadersM.Unlock() defer shadersM.Unlock()
k := key{ var c int
Filter: filter, if useColorM {
Address: address, c = 1
UseColorM: useColorM,
} }
if s, ok := shaders[k]; ok { if s := shaders[filter][address][c]; s != nil {
return s return s
} }
@ -165,6 +162,6 @@ func Shader(filter Filter, address Address, useColorM bool) []byte {
} }
b := buf.Bytes() b := buf.Bytes()
shaders[k] = b shaders[filter][address][c] = b
return 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) return s.shader.AppendUniforms(dst, uniforms)
} }
type builtinShaderKey struct {
filter builtinshader.Filter
address builtinshader.Address
useColorM bool
}
var ( var (
builtinShaders = map[builtinShaderKey]*Shader{} builtinShaders [builtinshader.FilterCount][builtinshader.AddressCount][2]*Shader
builtinShadersM sync.Mutex builtinShadersM sync.Mutex
) )
@ -74,12 +68,11 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u
builtinShadersM.Lock() builtinShadersM.Lock()
defer builtinShadersM.Unlock() defer builtinShadersM.Unlock()
key := builtinShaderKey{ var c int
filter: filter, if useColorM {
address: address, c = 1
useColorM: useColorM,
} }
if s, ok := builtinShaders[key]; ok { if s := builtinShaders[filter][address][c]; s != nil {
return s return s
} }
@ -100,6 +93,6 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u
shader = s shader = s
} }
builtinShaders[key] = shader builtinShaders[filter][address][c] = shader
return shader return shader
} }