mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 10:48:53 +01:00
graphicsdriver/metal: Give up reusing buffers
It was inevitable to rely on reference counts, which was too hacky. Besides, reusing buffers doesn't improve performance.
This commit is contained in:
parent
19079ae7a2
commit
207ae08c9d
@ -1,108 +0,0 @@
|
||||
// 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) {
|
||||
// // TODO: Don't rely on the number of ref counts. CFGetRetainCount should be used only for debugging.
|
||||
// // Note that checking whether MTLCommandBuffer's status is completed or not does not work, because the
|
||||
// // CommandBuffer might still be used even in such situation.
|
||||
// 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()
|
||||
// The buffer will be actually released after all the current command buffers are finished.
|
||||
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
|
||||
|
||||
vb *buffer
|
||||
ib *buffer
|
||||
vb mtl.Buffer
|
||||
ib mtl.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 != nil {
|
||||
putBuffer(d.vb)
|
||||
if d.vb != (mtl.Buffer{}) {
|
||||
d.vb.Release()
|
||||
}
|
||||
if d.ib != nil {
|
||||
putBuffer(d.ib)
|
||||
if d.ib != (mtl.Buffer{}) {
|
||||
d.ib.Release()
|
||||
}
|
||||
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)))
|
||||
d.vb = d.device.MakeBufferWithBytes(unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)), mtl.ResourceStorageModeManaged)
|
||||
d.ib = d.device.MakeBufferWithBytes(unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)), mtl.ResourceStorageModeManaged)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@ -538,7 +538,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.b, 0, 0)
|
||||
rce.SetVertexBuffer(d.vb, 0, 0)
|
||||
|
||||
viewportSize := [...]float32{float32(w), float32(h)}
|
||||
rce.SetVertexBytes(unsafe.Pointer(&viewportSize[0]), unsafe.Sizeof(viewportSize), 1)
|
||||
@ -561,7 +561,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.b, indexOffset*2)
|
||||
rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, indexLen, mtl.IndexTypeUInt16, d.ib, indexOffset*2)
|
||||
rce.EndEncoding()
|
||||
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user