mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
graphicsdriver/metal: Reuse MTLBuffer instead of re-creating (#762)
This commit is contained in:
parent
a8dcd5c628
commit
882d0c0bc8
104
internal/graphicsdriver/metal/buffer.go
Normal file
104
internal/graphicsdriver/metal/buffer.go
Normal file
@ -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 <CoreFoundation/CoreFoundation.h>
|
||||||
|
//
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
@ -227,8 +227,8 @@ type Driver struct {
|
|||||||
|
|
||||||
screenDrawable ca.MetalDrawable
|
screenDrawable ca.MetalDrawable
|
||||||
|
|
||||||
vb mtl.Buffer
|
vb *buffer
|
||||||
ib mtl.Buffer
|
ib *buffer
|
||||||
|
|
||||||
src *Image
|
src *Image
|
||||||
dst *Image
|
dst *Image
|
||||||
@ -253,14 +253,14 @@ func (d *Driver) SetWindow(window uintptr) {
|
|||||||
|
|
||||||
func (d *Driver) SetVertices(vertices []float32, indices []uint16) {
|
func (d *Driver) SetVertices(vertices []float32, indices []uint16) {
|
||||||
mainthread.Run(func() error {
|
mainthread.Run(func() error {
|
||||||
if d.vb != (mtl.Buffer{}) {
|
if d.vb != nil {
|
||||||
d.vb.Release()
|
putBuffer(d.vb)
|
||||||
}
|
}
|
||||||
if d.ib != (mtl.Buffer{}) {
|
if d.ib != nil {
|
||||||
d.ib.Release()
|
putBuffer(d.ib)
|
||||||
}
|
}
|
||||||
d.vb = d.device.MakeBuffer(unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)), mtl.ResourceStorageModeManaged)
|
d.vb = getBuffer(d.device, unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)))
|
||||||
d.ib = d.device.MakeBuffer(unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)), mtl.ResourceStorageModeManaged)
|
d.ib = getBuffer(d.device, unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -539,7 +539,7 @@ func (d *Driver) Draw(indexLen int, indexOffset int, mode graphics.CompositeMode
|
|||||||
rce.SetRenderPipelineState(d.rpss[mode])
|
rce.SetRenderPipelineState(d.rpss[mode])
|
||||||
}
|
}
|
||||||
rce.SetViewport(mtl.Viewport{0, 0, float64(w), float64(h), -1, 1})
|
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)}
|
viewportSize := [...]float32{float32(w), float32(h)}
|
||||||
rce.SetVertexBytes(unsafe.Pointer(&viewportSize[0]), unsafe.Sizeof(viewportSize), 1)
|
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 {
|
} else {
|
||||||
rce.SetFragmentTexture(mtl.Texture{}, 0)
|
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()
|
rce.EndEncoding()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -479,12 +479,19 @@ func (d Device) MakeRenderPipelineState(rpd RenderPipelineDescriptor) (RenderPip
|
|||||||
return RenderPipelineState{rps.RenderPipelineState}, nil
|
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.
|
// and initializes its contents by copying existing data into it.
|
||||||
//
|
//
|
||||||
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433429-makebuffer.
|
// Reference: https://developer.apple.com/documentation/metal/mtldevice/1433429-makebuffer.
|
||||||
func (d Device) MakeBuffer(bytes unsafe.Pointer, length uintptr, opt ResourceOptions) Buffer {
|
func (d Device) MakeBufferWithBytes(bytes unsafe.Pointer, length uintptr, opt ResourceOptions) Buffer {
|
||||||
return Buffer{C.Device_MakeBuffer(d.device, bytes, C.size_t(length), C.uint16_t(opt))}
|
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
|
// MakeTexture creates a texture object with privately owned storage
|
||||||
@ -772,10 +779,22 @@ type Buffer struct {
|
|||||||
buffer unsafe.Pointer
|
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() {
|
func (b Buffer) Release() {
|
||||||
C.Buffer_Release(b.buffer)
|
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.
|
// Function represents a programmable graphics or compute function executed by the GPU.
|
||||||
//
|
//
|
||||||
// Reference: https://developer.apple.com/documentation/metal/mtlfunction.
|
// Reference: https://developer.apple.com/documentation/metal/mtlfunction.
|
||||||
|
@ -115,7 +115,9 @@ struct Library Device_MakeLibrary(void *device, const char *source,
|
|||||||
struct RenderPipelineState
|
struct RenderPipelineState
|
||||||
Device_MakeRenderPipelineState(void *device,
|
Device_MakeRenderPipelineState(void *device,
|
||||||
struct RenderPipelineDescriptor descriptor);
|
struct RenderPipelineDescriptor descriptor);
|
||||||
void *Device_MakeBuffer(void *device, const void *bytes, size_t length,
|
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);
|
uint16_t options);
|
||||||
void *Device_MakeTexture(void *device, struct TextureDescriptor descriptor);
|
void *Device_MakeTexture(void *device, struct TextureDescriptor descriptor);
|
||||||
|
|
||||||
@ -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 Texture_ReplaceRegion(void *texture, struct Region region, uint_t level,
|
||||||
void *pixelBytes, uint_t bytesPerRow);
|
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);
|
void Buffer_Release(void *buffer);
|
||||||
|
@ -108,13 +108,20 @@ Device_MakeRenderPipelineState(void *device,
|
|||||||
return rps;
|
return rps;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *Device_MakeBuffer(void *device, const void *bytes, size_t length,
|
void *Device_MakeBufferWithBytes(void *device, const void *bytes, size_t length,
|
||||||
uint16_t options) {
|
uint16_t options) {
|
||||||
return [(id<MTLDevice>)device newBufferWithBytes:(const void *)bytes
|
return [(id<MTLDevice>)device newBufferWithBytes:(const void *)bytes
|
||||||
length:(NSUInteger)length
|
length:(NSUInteger)length
|
||||||
options:(MTLResourceOptions)options];
|
options:(MTLResourceOptions)options];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *Device_MakeBufferWithLength(void *device, size_t length,
|
||||||
|
uint16_t options) {
|
||||||
|
return
|
||||||
|
[(id<MTLDevice>)device newBufferWithLength:(NSUInteger)length
|
||||||
|
options:(MTLResourceOptions)options];
|
||||||
|
}
|
||||||
|
|
||||||
void *Device_MakeTexture(void *device, struct TextureDescriptor descriptor) {
|
void *Device_MakeTexture(void *device, struct TextureDescriptor descriptor) {
|
||||||
MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];
|
MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];
|
||||||
textureDescriptor.pixelFormat = descriptor.PixelFormat;
|
textureDescriptor.pixelFormat = descriptor.PixelFormat;
|
||||||
@ -311,4 +318,10 @@ void Texture_ReplaceRegion(void *texture, struct Region region, uint_t level,
|
|||||||
bytesPerRow:(NSUInteger)bytesPerRow];
|
bytesPerRow:(NSUInteger)bytesPerRow];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Buffer_CopyToContents(void *buffer, void *data, size_t lengthInBytes) {
|
||||||
|
memcpy(((id<MTLBuffer>)buffer).contents, data, lengthInBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Buffer_Retain(void *buffer) { [(id<MTLBuffer>)buffer retain]; }
|
||||||
|
|
||||||
void Buffer_Release(void *buffer) { [(id<MTLBuffer>)buffer release]; }
|
void Buffer_Release(void *buffer) { [(id<MTLBuffer>)buffer release]; }
|
||||||
|
Loading…
Reference in New Issue
Block a user