From 882d0c0bc8c36229cd30de3664fbdcd45106f3f5 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 2 Feb 2019 04:01:53 +0900 Subject: [PATCH] graphicsdriver/metal: Reuse MTLBuffer instead of re-creating (#762) --- internal/graphicsdriver/metal/buffer.go | 104 +++++++++++++++++++++++ internal/graphicsdriver/metal/driver.go | 20 ++--- internal/graphicsdriver/metal/mtl/mtl.go | 25 +++++- internal/graphicsdriver/metal/mtl/mtl.h | 8 +- internal/graphicsdriver/metal/mtl/mtl.m | 17 +++- 5 files changed, 157 insertions(+), 17 deletions(-) create mode 100644 internal/graphicsdriver/metal/buffer.go diff --git a/internal/graphicsdriver/metal/buffer.go b/internal/graphicsdriver/metal/buffer.go new file mode 100644 index 000000000..afea35096 --- /dev/null +++ b/internal/graphicsdriver/metal/buffer.go @@ -0,0 +1,104 @@ +// Copyright 2019 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build darwin + +package metal + +import ( + "sort" + "unsafe" + + "github.com/hajimehoshi/ebiten/internal/graphicsdriver/metal/mtl" +) + +// #cgo LDFLAGS: -framework CoreFoundation +// +// #import +// +// static int count(void* obj) { +// return CFGetRetainCount(obj); +// } +import "C" + +type buffer struct { + b mtl.Buffer + len uintptr +} + +func (b *buffer) used() bool { + // If the count is 2 or more, the buffer is actually retained outside. + // If the count is 1, the buffer is retained only by the buffer pool. + // The count cannot be 0 since the object is already freed in this case. + return C.count(b.b.Native()) > 1 +} + +var bufferPool = map[*buffer]struct{}{} + +func getBuffer(device mtl.Device, data unsafe.Pointer, lengthInBytes uintptr) *buffer { + for buf := range bufferPool { + if buf.used() { + continue + } + if buf.len < lengthInBytes { + continue + } + buf.b.CopyToContents(data, lengthInBytes) + buf.b.Retain() + return buf + } + + gcBufferPool() + + buf := &buffer{ + b: device.MakeBufferWithBytes(data, lengthInBytes, mtl.ResourceStorageModeManaged), + len: lengthInBytes, + } + buf.b.Retain() + bufferPool[buf] = struct{}{} + return buf +} + +func putBuffer(buf *buffer) { + buf.b.Release() + gcBufferPool() +} + +func gcBufferPool() { + const threshold = 16 + + if len(bufferPool) < threshold { + return + } + + toRemove := []*buffer{} + for buf := range bufferPool { + if buf.used() { + continue + } + toRemove = append(toRemove, buf) + } + sort.Slice(toRemove, func(a, b int) bool { + return toRemove[a].len < toRemove[b].len + }) + + l := len(toRemove) + if l > len(bufferPool)-threshold { + l = len(bufferPool) - threshold + } + for _, buf := range toRemove[:l] { + buf.b.Release() + delete(bufferPool, buf) + } +} diff --git a/internal/graphicsdriver/metal/driver.go b/internal/graphicsdriver/metal/driver.go index 689c64fce..87bc14d7e 100644 --- a/internal/graphicsdriver/metal/driver.go +++ b/internal/graphicsdriver/metal/driver.go @@ -227,8 +227,8 @@ type Driver struct { screenDrawable ca.MetalDrawable - vb mtl.Buffer - ib mtl.Buffer + vb *buffer + ib *buffer src *Image dst *Image @@ -253,14 +253,14 @@ func (d *Driver) SetWindow(window uintptr) { func (d *Driver) SetVertices(vertices []float32, indices []uint16) { mainthread.Run(func() error { - if d.vb != (mtl.Buffer{}) { - d.vb.Release() + if d.vb != nil { + putBuffer(d.vb) } - if d.ib != (mtl.Buffer{}) { - d.ib.Release() + if d.ib != nil { + putBuffer(d.ib) } - d.vb = d.device.MakeBuffer(unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)), mtl.ResourceStorageModeManaged) - d.ib = d.device.MakeBuffer(unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)), mtl.ResourceStorageModeManaged) + d.vb = getBuffer(d.device, unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices))) + d.ib = getBuffer(d.device, unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices))) return nil }) } @@ -539,7 +539,7 @@ func (d *Driver) Draw(indexLen int, indexOffset int, mode graphics.CompositeMode rce.SetRenderPipelineState(d.rpss[mode]) } rce.SetViewport(mtl.Viewport{0, 0, float64(w), float64(h), -1, 1}) - rce.SetVertexBuffer(d.vb, 0, 0) + rce.SetVertexBuffer(d.vb.b, 0, 0) viewportSize := [...]float32{float32(w), float32(h)} rce.SetVertexBytes(unsafe.Pointer(&viewportSize[0]), unsafe.Sizeof(viewportSize), 1) @@ -562,7 +562,7 @@ func (d *Driver) Draw(indexLen int, indexOffset int, mode graphics.CompositeMode } else { rce.SetFragmentTexture(mtl.Texture{}, 0) } - rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, indexLen, mtl.IndexTypeUInt16, d.ib, indexOffset*2) + rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, indexLen, mtl.IndexTypeUInt16, d.ib.b, indexOffset*2) rce.EndEncoding() return nil diff --git a/internal/graphicsdriver/metal/mtl/mtl.go b/internal/graphicsdriver/metal/mtl/mtl.go index 2236e715d..8139ff855 100644 --- a/internal/graphicsdriver/metal/mtl/mtl.go +++ b/internal/graphicsdriver/metal/mtl/mtl.go @@ -479,12 +479,19 @@ func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPip return RenderPipelineState{rps.RenderPipelineState}, nil } -// MakeBuffer allocates a new buffer of a given length +// MakeBufferWithBytes allocates a new buffer of a given length // and initializes its contents by copying existing data into it. // // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433429-makebuffer. -func (d Device) MakeBuffer(bytes unsafe.Pointer, length uintptr, opt ResourceOptions) Buffer { - return Buffer{C.Device_MakeBuffer(d.device, bytes, C.size_t(length), C.uint16_t(opt))} +func (d Device) MakeBufferWithBytes(bytes unsafe.Pointer, length uintptr, opt ResourceOptions) Buffer { + return Buffer{C.Device_MakeBufferWithBytes(d.device, bytes, C.size_t(length), C.uint16_t(opt))} +} + +// MakeBufferWithLength allocates a new zero-filled buffer of a given length. +// +// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433375-newbufferwithlength +func (d Device) MakeBufferWithLength(length uintptr, opt ResourceOptions) Buffer { + return Buffer{C.Device_MakeBufferWithLength(d.device, C.size_t(length), C.uint16_t(opt))} } // MakeTexture creates a texture object with privately owned storage @@ -772,10 +779,22 @@ type Buffer struct { buffer unsafe.Pointer } +func (b Buffer) CopyToContents(data unsafe.Pointer, lengthInBytes uintptr) { + C.Buffer_CopyToContents(b.buffer, data, C.size_t(lengthInBytes)) +} + +func (b Buffer) Retain() { + C.Buffer_Retain(b.buffer) +} + func (b Buffer) Release() { C.Buffer_Release(b.buffer) } +func (b Buffer) Native() unsafe.Pointer { + return b.buffer +} + // Function represents a programmable graphics or compute function executed by the GPU. // // Reference: https://developer.apple.com/documentation/metal/mtlfunction. diff --git a/internal/graphicsdriver/metal/mtl/mtl.h b/internal/graphicsdriver/metal/mtl/mtl.h index d93b26687..eb7fccc12 100644 --- a/internal/graphicsdriver/metal/mtl/mtl.h +++ b/internal/graphicsdriver/metal/mtl/mtl.h @@ -115,8 +115,10 @@ struct Library Device_MakeLibrary(void *device, const char *source, struct RenderPipelineState Device_MakeRenderPipelineState(void *device, struct RenderPipelineDescriptor descriptor); -void *Device_MakeBuffer(void *device, const void *bytes, size_t length, - uint16_t options); +void *Device_MakeBufferWithBytes(void *device, const void *bytes, size_t length, + uint16_t options); +void *Device_MakeBufferWithLength(void *device, size_t length, + uint16_t options); void *Device_MakeTexture(void *device, struct TextureDescriptor descriptor); void CommandQueue_Release(void *commandQueue); @@ -172,4 +174,6 @@ void Texture_GetBytes(void *texture, void *pixelBytes, size_t bytesPerRow, void Texture_ReplaceRegion(void *texture, struct Region region, uint_t level, void *pixelBytes, uint_t bytesPerRow); +void Buffer_CopyToContents(void *buffer, void *data, size_t lengthInBytes); +void Buffer_Retain(void *buffer); void Buffer_Release(void *buffer); diff --git a/internal/graphicsdriver/metal/mtl/mtl.m b/internal/graphicsdriver/metal/mtl/mtl.m index f8e2ea5a6..a5c72d1cd 100644 --- a/internal/graphicsdriver/metal/mtl/mtl.m +++ b/internal/graphicsdriver/metal/mtl/mtl.m @@ -108,13 +108,20 @@ Device_MakeRenderPipelineState(void *device, return rps; } -void *Device_MakeBuffer(void *device, const void *bytes, size_t length, - uint16_t options) { +void *Device_MakeBufferWithBytes(void *device, const void *bytes, size_t length, + uint16_t options) { return [(id)device newBufferWithBytes:(const void *)bytes length:(NSUInteger)length options:(MTLResourceOptions)options]; } +void *Device_MakeBufferWithLength(void *device, size_t length, + uint16_t options) { + return + [(id)device newBufferWithLength:(NSUInteger)length + options:(MTLResourceOptions)options]; +} + void *Device_MakeTexture(void *device, struct TextureDescriptor descriptor) { MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init]; textureDescriptor.pixelFormat = descriptor.PixelFormat; @@ -311,4 +318,10 @@ void Texture_ReplaceRegion(void *texture, struct Region region, uint_t level, bytesPerRow:(NSUInteger)bytesPerRow]; } +void Buffer_CopyToContents(void *buffer, void *data, size_t lengthInBytes) { + memcpy(((id)buffer).contents, data, lengthInBytes); +} + +void Buffer_Retain(void *buffer) { [(id)buffer retain]; } + void Buffer_Release(void *buffer) { [(id)buffer release]; }