Skip to content

Commit

Permalink
Update builtinShader to not use mutex when single thread build tag is…
Browse files Browse the repository at this point in the history
… set
  • Loading branch information
ernestrc committed Jun 24, 2024
1 parent b749976 commit 117957c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
9 changes: 2 additions & 7 deletions shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ebiten

import (
"fmt"
"sync"

"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
Expand Down Expand Up @@ -82,14 +81,10 @@ func (s *Shader) appendUniforms(dst []uint32, uniforms map[string]any) []uint32
}

var (
builtinShaders [builtinshader.FilterCount][builtinshader.AddressCount][2]*Shader
builtinShadersM sync.Mutex
builtinShaders [builtinshader.FilterCount][builtinshader.AddressCount][2]*Shader
)

func builtinShader(filter builtinshader.Filter, address builtinshader.Address, useColorM bool) *Shader {
builtinShadersM.Lock()
defer builtinShadersM.Unlock()

func getBuiltinShader(filter builtinshader.Filter, address builtinshader.Address, useColorM bool) *Shader {
var c int
if useColorM {
c = 1
Expand Down
32 changes: 32 additions & 0 deletions shader_multithread.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 The Ebiten 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.

//go:build !ebitenginesinglethread && !ebitensinglethread

package ebiten

import (
"sync"

"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
)

var shaderMu sync.Mutex

func builtinShader(filter builtinshader.Filter, address builtinshader.Address, useColorM bool) *Shader {
shaderMu.Lock()
defer shaderMu.Unlock()

return getBuiltinShader(filter, address, useColorM)
}
23 changes: 23 additions & 0 deletions shader_singlethread.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2020 The Ebiten 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.

//go:build ebitenginesinglethread || ebitensinglethread

package ebiten

import "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"

func builtinShader(filter builtinshader.Filter, address builtinshader.Address, useColorM bool) *Shader {
return getBuiltinShader(filter, address, useColorM)
}

0 comments on commit 117957c

Please sign in to comment.