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,
// the image's backend might be turned into a source backend to optimize draw calls.
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) {
@ -122,10 +126,6 @@ var (
// theBackends is a set of atlases.
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{}{}
imagesUsedAsDestination = map[*Image]struct{}{}
@ -231,11 +231,13 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
imagesUsedAsDestination[i] = struct{}{}
if i.backend == nil {
// `theSourceBackendsForOneFrame` already includes `backends`.
bs := make([]*backend, 0, len(theSourceBackendsForOneFrame))
for b := range theSourceBackendsForOneFrame {
// `sourceInThisFrame` of `backends` should be true, so `backends` should be in `bs`.
var bs []*backend
for _, b := range theBackends {
if b.sourceInThisFrame {
bs = append(bs, b)
}
}
i.allocate(bs, false)
i.backendJustCreated = true
return
@ -260,12 +262,13 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
newI := NewImage(i.width, i.height, i.imageType)
// Call allocate explicitly in order to have an isolated backend from the specified backends.
// `theSourceBackendsForOneFrame` already includes `backends`.
bs := make([]*backend, 0, 1+len(theSourceBackendsForOneFrame))
bs = append(bs, i.backend)
for b := range theSourceBackendsForOneFrame {
// `sourceInThisFrame` of `backends` should be true, so `backends` should be in `bs`.
bs := []*backend{i.backend}
for _, b := range theBackends {
if b.sourceInThisFrame {
bs = append(bs, b)
}
}
newI.allocate(bs, false)
w, h := float32(i.width), float32(i.height)
@ -371,7 +374,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
src.allocate(nil, true)
}
backends = append(backends, src.backend)
theSourceBackendsForOneFrame[src.backend] = struct{}{}
src.backend.sourceInThisFrame = true
}
i.ensureIsolatedFromSource(backends)
@ -734,8 +737,8 @@ func EndFrame(graphicsDriver graphicsdriver.Graphics, swapBuffersForGL func()) e
return err
}
for b := range theSourceBackendsForOneFrame {
delete(theSourceBackendsForOneFrame, b)
for _, b := range theBackends {
b.sourceInThisFrame = false
}
return nil