From e6bd367192108cb5f94770a59b572224b63f383d Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Wed, 25 Jun 2025 17:12:27 +0300 Subject: [PATCH] d3d11: use sync.Pool for Buffers to avoid garbage Signed-off-by: 5684185+vsariola@users.noreply.github.com <5684185+vsariola@users.noreply.github.com> --- gpu/internal/d3d11/d3d11_windows.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/gpu/internal/d3d11/d3d11_windows.go b/gpu/internal/d3d11/d3d11_windows.go index f27eda75..d3a43c12 100644 --- a/gpu/internal/d3d11/d3d11_windows.go +++ b/gpu/internal/d3d11/d3d11_windows.go @@ -8,6 +8,7 @@ import ( "image" "math" "math/bits" + "sync" "unsafe" "golang.org/x/sys/windows" @@ -89,6 +90,8 @@ type Buffer struct { immutable bool } +var bufferPool sync.Pool = sync.Pool{New: func() any { return &Buffer{} }} + func init() { driver.NewDirect3D11Device = newDirect3D11Device } @@ -472,7 +475,9 @@ func (b *Backend) newBuffer(typ driver.BufferBinding, size int, data []byte, imm return nil, err } } - return &Buffer{backend: b, buf: buf, bind: bind, size: size, resView: resView, uaView: uaView, immutable: immutable}, nil + buffer := bufferPool.Get().(*Buffer) + *buffer = Buffer{backend: b, buf: buf, bind: bind, size: size, resView: resView, uaView: uaView, immutable: immutable} + return buffer, nil } func (b *Backend) NewComputeProgram(shader shader.Sources) (driver.Program, error) { @@ -750,6 +755,7 @@ func (b *Buffer) Release() { } d3d11.IUnknownRelease(unsafe.Pointer(b.buf), b.buf.Vtbl.Release) *b = Buffer{} + bufferPool.Put(b) // Return to pool for reuse. Note: this means that you should not use the buffer after calling Release. } func (t *Texture) ReadPixels(src image.Rectangle, pixels []byte, stride int) error {