From 2f40eb039cba30ed47801d1d58ce9dd096efa74f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 5 Mar 2025 22:56:58 +0900 Subject: [PATCH] ebiten: add DebugInfo.TotalGPUImageMemoryUsageInBytes Updates #3205 --- examples/windowsize/main.go | 6 +++++- graphics.go | 8 ++++++++ internal/atlas/image.go | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index 6e7d852e5..b83287dcc 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -353,6 +353,9 @@ func (g *game) Draw(screen *ebiten.Image) { fg = "No" } + var debug ebiten.DebugInfo + ebiten.ReadDebugInfo(&debug) + msg := fmt.Sprintf(`[Arrow keys] Move the window [Shift + Arrow keys] Change the window size %s @@ -375,7 +378,8 @@ Window size limitation: (%d, %d) - (%d, %d) Cursor: (%d, %d) TPS: Current: %0.2f / Max: %s FPS: %0.2f -Device Scale Factor: %0.2f`, msgM, msgR, fg, wx, wy, ww, wh, minw, minh, maxw, maxh, cx, cy, ebiten.ActualTPS(), tpsStr, ebiten.ActualFPS(), ebiten.Monitor().DeviceScaleFactor()) +Device Scale Factor: %0.2f +GPU Memory Usage: %d`, msgM, msgR, fg, wx, wy, ww, wh, minw, minh, maxw, maxh, cx, cy, ebiten.ActualTPS(), tpsStr, ebiten.ActualFPS(), ebiten.Monitor().DeviceScaleFactor(), debug.TotalGPUImageMemoryUsageInBytes) ebitenutil.DebugPrint(screen, msg) } diff --git a/graphics.go b/graphics.go index 35f596ef5..d1850e31a 100644 --- a/graphics.go +++ b/graphics.go @@ -15,6 +15,7 @@ package ebiten import ( + "github.com/hajimehoshi/ebiten/v2/internal/atlas" "github.com/hajimehoshi/ebiten/v2/internal/builtinshader" "github.com/hajimehoshi/ebiten/v2/internal/ui" ) @@ -70,11 +71,18 @@ var _ [GraphicsLibraryAuto]int = [0]int{} type DebugInfo struct { // GraphicsLibrary represents the graphics library currently in use. GraphicsLibrary GraphicsLibrary + + // TotalGPUImageMemoryUsageInBytes is the total image memory usage for GPU in bytes. + // TotalGPUImageMemoryUsageInBytes is approximately the total memory usage for GPU. + TotalGPUImageMemoryUsageInBytes int64 } // ReadDebugInfo writes debug info (e.g. current graphics library) into a provided struct. +// +// ReadDebugInfo is concurrent-safe. func ReadDebugInfo(d *DebugInfo) { d.GraphicsLibrary = GraphicsLibrary(ui.Get().GraphicsLibrary()) + d.TotalGPUImageMemoryUsageInBytes = atlas.TotalGPUImageMemoryUsageInBytes() } // ColorSpace represents the color space of the screen. diff --git a/internal/atlas/image.go b/internal/atlas/image.go index aabd4d252..915cbe2f8 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -838,3 +838,18 @@ func DumpImages(graphicsDriver graphicsdriver.Graphics, dir string) (string, err return restorable.DumpImages(graphicsDriver, dir) } + +func TotalGPUImageMemoryUsageInBytes() int64 { + backendsM.Lock() + defer backendsM.Unlock() + + var sum int64 + for _, b := range theBackends { + if b.restorable == nil { + continue + } + w, h := b.restorable.InternalSize() + sum += 4 * int64(w) * int64(h) + } + return sum +}