internal/buffered: refactoring

This commit is contained in:
Hajime Hoshi 2022-06-25 01:42:40 +09:00
parent bf27f25e26
commit 0eb2f76422
2 changed files with 17 additions and 26 deletions

View File

@ -23,24 +23,20 @@ var (
// delayedCommands represents a queue for image operations that are ordered before the game starts
// (BeginFrame). Before the game starts, the package shareable doesn't determine the minimum/maximum texture
// sizes (#879).
delayedCommands = []func() error{}
delayedCommands = []func(){}
delayedCommandsM sync.Mutex
delayedCommandsFlushed uint32
)
func flushDelayedCommands() error {
func flushDelayedCommands() {
fs := getDelayedFuncsAndClear()
for _, f := range fs {
if err := f(); err != nil {
return err
f()
}
}
return nil
}
func getDelayedFuncsAndClear() []func() error {
func getDelayedFuncsAndClear() []func() {
if atomic.LoadUint32(&delayedCommandsFlushed) == 0 {
// Outline the slow-path to expect the fast-path is inlined.
return getDelayedFuncsAndClearSlow()
@ -48,14 +44,14 @@ func getDelayedFuncsAndClear() []func() error {
return nil
}
func getDelayedFuncsAndClearSlow() []func() error {
func getDelayedFuncsAndClearSlow() []func() {
delayedCommandsM.Lock()
defer delayedCommandsM.Unlock()
if delayedCommandsFlushed == 0 {
defer atomic.StoreUint32(&delayedCommandsFlushed, 1)
fs := make([]func() error, len(delayedCommands))
fs := make([]func(), len(delayedCommands))
copy(fs, delayedCommands)
delayedCommands = nil
return fs
@ -71,13 +67,13 @@ func maybeCanAddDelayedCommand() bool {
return atomic.LoadUint32(&delayedCommandsFlushed) == 0
}
func tryAddDelayedCommand(f func() error) bool {
func tryAddDelayedCommand(f func()) bool {
delayedCommandsM.Lock()
defer delayedCommandsM.Unlock()
if delayedCommandsFlushed == 0 {
delayedCommands = append(delayedCommands, func() error {
return f()
delayedCommands = append(delayedCommands, func() {
f()
})
return true
}

View File

@ -38,7 +38,8 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error {
if err := atlas.BeginFrame(graphicsDriver); err != nil {
return err
}
return flushDelayedCommands()
flushDelayedCommands()
return nil
}
func EndFrame(graphicsDriver graphicsdriver.Graphics) error {
@ -56,9 +57,8 @@ func NewImage(width, height int, imageType atlas.ImageType) *Image {
func (i *Image) initialize(imageType atlas.ImageType) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
if tryAddDelayedCommand(func() {
i.initialize(imageType)
return nil
}) {
return
}
@ -87,9 +87,8 @@ func (i *Image) resolvePendingPixels(keepPendingPixels bool) {
func (i *Image) MarkDisposed() {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
if tryAddDelayedCommand(func() {
i.MarkDisposed()
return nil
}) {
return
}
@ -137,9 +136,8 @@ func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) {
if maybeCanAddDelayedCommand() {
copied := make([]byte, len(pix))
copy(copied, pix)
if tryAddDelayedCommand(func() error {
if tryAddDelayedCommand(func() {
i.ReplacePixels(copied, x, y, width, height)
return nil
}) {
return
}
@ -192,10 +190,9 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f
}
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
if tryAddDelayedCommand(func() {
// Arguments are not copied. Copying is the caller's responsibility.
i.DrawTriangles(srcs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd)
return nil
}) {
return
}
@ -236,9 +233,8 @@ func NewShader(ir *shaderir.Program) *Shader {
func (s *Shader) initialize(ir *shaderir.Program) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
if tryAddDelayedCommand(func() {
s.initialize(ir)
return nil
}) {
return
}
@ -248,9 +244,8 @@ func (s *Shader) initialize(ir *shaderir.Program) {
func (s *Shader) MarkDisposed() {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() error {
if tryAddDelayedCommand(func() {
s.MarkDisposed()
return nil
}) {
return
}