internal/atlas: replace a global map with a member

This should be a pure performance improvement.

Updates #2601
This commit is contained in:
Hajime Hoshi 2023-08-19 04:57:30 +09:00
parent 32f1436576
commit 3c49f81b5c

View File

@ -99,6 +99,10 @@ type backend struct {
// If a non-source (destination) image is used as a source many times, // If a non-source (destination) image is used as a source many times,
// the image's backend might be turned into a source backend to optimize draw calls. // the image's backend might be turned into a source backend to optimize draw calls.
source bool source bool
// sourceInThisFrame reports whether this backend is used as a source in this frame.
// sourceInThisFrame is reset every frame.
sourceInThisFrame bool
} }
func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) { func (b *backend) tryAlloc(width, height int) (*packing.Node, bool) {
@ -122,10 +126,6 @@ var (
// theBackends is a set of atlases. // theBackends is a set of atlases.
theBackends []*backend theBackends []*backend
// theSourceBackendsForOneFrame is a temporary set of backends that are used as sources in one frame.
// theSourceBackendsForOneFrame is reset every frame.
theSourceBackendsForOneFrame = map[*backend]struct{}{}
imagesToPutOnSourceBackend = map[*Image]struct{}{} imagesToPutOnSourceBackend = map[*Image]struct{}{}
imagesUsedAsDestination = map[*Image]struct{}{} imagesUsedAsDestination = map[*Image]struct{}{}
@ -231,10 +231,12 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
imagesUsedAsDestination[i] = struct{}{} imagesUsedAsDestination[i] = struct{}{}
if i.backend == nil { if i.backend == nil {
// `theSourceBackendsForOneFrame` already includes `backends`. // `sourceInThisFrame` of `backends` should be true, so `backends` should be in `bs`.
bs := make([]*backend, 0, len(theSourceBackendsForOneFrame)) var bs []*backend
for b := range theSourceBackendsForOneFrame { for _, b := range theBackends {
bs = append(bs, b) if b.sourceInThisFrame {
bs = append(bs, b)
}
} }
i.allocate(bs, false) i.allocate(bs, false)
i.backendJustCreated = true i.backendJustCreated = true
@ -260,11 +262,12 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
newI := NewImage(i.width, i.height, i.imageType) newI := NewImage(i.width, i.height, i.imageType)
// Call allocate explicitly in order to have an isolated backend from the specified backends. // Call allocate explicitly in order to have an isolated backend from the specified backends.
// `theSourceBackendsForOneFrame` already includes `backends`. // `sourceInThisFrame` of `backends` should be true, so `backends` should be in `bs`.
bs := make([]*backend, 0, 1+len(theSourceBackendsForOneFrame)) bs := []*backend{i.backend}
bs = append(bs, i.backend) for _, b := range theBackends {
for b := range theSourceBackendsForOneFrame { if b.sourceInThisFrame {
bs = append(bs, b) bs = append(bs, b)
}
} }
newI.allocate(bs, false) newI.allocate(bs, false)
@ -371,7 +374,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
src.allocate(nil, true) src.allocate(nil, true)
} }
backends = append(backends, src.backend) backends = append(backends, src.backend)
theSourceBackendsForOneFrame[src.backend] = struct{}{} src.backend.sourceInThisFrame = true
} }
i.ensureIsolatedFromSource(backends) i.ensureIsolatedFromSource(backends)
@ -734,8 +737,8 @@ func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) e
return err return err
} }
for b := range theSourceBackendsForOneFrame { for _, b := range theBackends {
delete(theSourceBackendsForOneFrame, b) b.sourceInThisFrame = false
} }
return nil return nil