doodle/pkg/uix/canvas_memory.go
Noah Petherbridge c5353df211 LoadUnloadChunk for Memory Optimization
Instead of the loadscreen eager-loading ALL level chunks to Go Images, only
load the chunks within the "LoadingViewport" - which is the on-screen
Viewport plus a margin of chunks off the screen edges.

During gameplay, every few ticks, reevaluate which chunks are inside or
outside the LoadingViewport; for chunks outside, free their SDL2 textures
and free their cached bitmaps to keep overall memory usage down. The
AzulianTag-Forest level now stays under 200 Textures at any given time
and the loadscreen goes faster as it doesn't have to load every chunk's
images up front.

The LoadUnloadChunk feature can be turned on/off with feature flags. If
disabled the old behavior is restored: loadscreen loads all images and
the LoadUnloadChunks function is not run.

Other changes:

* loadscreen: do not free textures in the Hide() function as this runs on
  a different goroutine and may break. The 4 wallpaper textures are OK
  to keep in memory anyway, the loadscreen is reused often!
* Free more leaked textures: on the Inventory frame and when an actor
  calls Self.Destroy()
* Stop leaking goroutines in the PubSub feature of the doodad script
  engine; scripting.Supervisor.Teardown() sends a stop signal to all
  scripts to clean up neatly. Canvas.Destroy() tears down its scripting
  supervisor automatically.
2022-04-10 12:40:25 -07:00

92 lines
2.3 KiB
Go

package uix
import (
"runtime"
"sync"
"git.kirsle.net/apps/doodle/pkg/balance"
"git.kirsle.net/apps/doodle/pkg/level"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/apps/doodle/pkg/shmem"
"git.kirsle.net/go/render"
)
// Memory optimization features of the Canvas.
/*
LoadUnloadChunks optimizes memory for (level) canvases by warming up chunk images
that fall within the LoadingViewport and freeing chunks that are outside of it.
*/
func (w *Canvas) LoadUnloadChunks() {
if w.level == nil || shmem.Tick%balance.CanvasLoadUnloadModuloTicks != 0 || !balance.Feature.LoadUnloadChunk {
return
}
var (
vp = w.LoadingViewport()
chunks = make(chan render.Point)
chunksInside = map[render.Point]interface{}{}
chunksTeardown = []*level.Chunk{}
cores = runtime.NumCPU()
wg sync.WaitGroup
// Collect metrics for the debug overlay.
resultInside int
resultOutside int
)
// Collect the chunks that are inside the viewport so we know which ones are not.
for chunk := range w.level.Chunker.IterViewportChunks(vp) {
chunksInside[chunk] = nil
}
// Spawn background goroutines to process the chunks quickly.
for i := 0; i < cores; i++ {
wg.Add(1)
go func(i int) {
for coord := range chunks {
if chunk, ok := w.level.Chunker.GetChunk(coord); ok {
chunk := chunk
if _, ok := chunksInside[coord]; ok {
// Preload its bitmap image.
_ = chunk.CachedBitmap(render.Invisible)
resultInside++
} else {
// Unload its bitmap and texture.
chunksTeardown = append(chunksTeardown, chunk)
resultOutside++
}
}
}
wg.Done()
}(i)
}
for chunk := range w.level.Chunker.IterChunks() {
chunks <- chunk
}
close(chunks)
wg.Wait()
// Tear down the SDL2 textures of chunks to free.
for i, chunk := range chunksTeardown {
if chunk == nil {
log.Error("LoadUnloadChunks: chunksTeardown#%d was nil??", i)
continue
}
chunk.Teardown()
}
// Export the metrics for the debug overlay.
w.loadUnloadInside = resultInside
w.loadUnloadOutside = resultOutside
}
// LoadUnloadMetrics returns the canvas's stored metrics from the LoadUnloadChunks
// function, for the debug overlay.
func (w *Canvas) LoadUnloadMetrics() (inside, outside int) {
return w.loadUnloadInside, w.loadUnloadOutside
}