Metal autorelease pools at API boundaries

This commit is contained in:
Jack Andersen 2017-10-27 00:09:22 -10:00
parent d1b980b529
commit 3c207386e7

View File

@ -50,7 +50,7 @@ class MetalDataFactoryImpl : public MetalDataFactory
void deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf); void deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf);
public: public:
MetalDataFactoryImpl(IGraphicsContext* parent, MetalContext* ctx, uint32_t sampleCount); MetalDataFactoryImpl(IGraphicsContext* parent, MetalContext* ctx, uint32_t sampleCount);
~MetalDataFactoryImpl() {} ~MetalDataFactoryImpl() = default;
Platform platform() const {return Platform::Metal;} Platform platform() const {return Platform::Metal;}
const char* platformName() const {return "Metal";} const char* platformName() const {return "Metal";}
@ -891,10 +891,13 @@ struct MetalCommandQueue : IGraphicsCommandQueue
MTLPrimitiveType m_currentPrimitive = MTLPrimitiveTypeTriangle; MTLPrimitiveType m_currentPrimitive = MTLPrimitiveTypeTriangle;
void setShaderDataBinding(IShaderDataBinding* binding) void setShaderDataBinding(IShaderDataBinding* binding)
{ {
MetalShaderDataBinding* cbind = static_cast<MetalShaderDataBinding*>(binding); @autoreleasepool
cbind->bind(m_enc, m_fillBuf); {
m_boundData = cbind; MetalShaderDataBinding* cbind = static_cast<MetalShaderDataBinding*>(binding);
m_currentPrimitive = cbind->m_pipeline->m_drawPrim; cbind->bind(m_enc, m_fillBuf);
m_boundData = cbind;
m_currentPrimitive = cbind->m_pipeline->m_drawPrim;
}
} }
MetalTextureR* m_boundTarget = nullptr; MetalTextureR* m_boundTarget = nullptr;
@ -1221,69 +1224,90 @@ MetalDataFactoryImpl::MetalDataFactoryImpl(IGraphicsContext* parent, MetalContex
IGraphicsBufferS* MetalDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count) IGraphicsBufferS* MetalDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); {
MetalGraphicsBufferS* retval = new MetalGraphicsBufferS(d, use, factory.m_ctx, data, stride, count); MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
d->m_SBufs.emplace_back(retval); MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return retval; MetalGraphicsBufferS* retval = new MetalGraphicsBufferS(d, use, factory.m_ctx, data, stride, count);
d->m_SBufs.emplace_back(retval);
return retval;
}
} }
IGraphicsBufferD* MetalDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count) IGraphicsBufferD* MetalDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); {
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue()); MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
MetalGraphicsBufferD* retval = new MetalGraphicsBufferD(d, q, use, factory.m_ctx, stride, count); MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
d->m_DBufs.emplace_back(retval); MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue());
return retval; MetalGraphicsBufferD* retval = new MetalGraphicsBufferD(d, q, use, factory.m_ctx, stride, count);
d->m_DBufs.emplace_back(retval);
return retval;
}
} }
ITextureS* MetalDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt, ITextureS* MetalDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
TextureClampMode clampMode, const void* data, size_t sz) TextureClampMode clampMode, const void* data, size_t sz)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); {
MetalTextureS* retval = new MetalTextureS(d, factory.m_ctx, width, height, mips, fmt, data, sz); MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
d->m_STexs.emplace_back(retval); MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return retval; MetalTextureS* retval = new MetalTextureS(d, factory.m_ctx, width, height, mips, fmt, data, sz);
d->m_STexs.emplace_back(retval);
return retval;
}
} }
ITextureSA* MetalDataFactory::Context::newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips, ITextureSA* MetalDataFactory::Context::newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
TextureFormat fmt, TextureClampMode clampMode, TextureFormat fmt, TextureClampMode clampMode,
const void* data, size_t sz) const void* data, size_t sz)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); {
MetalTextureSA* retval = new MetalTextureSA(d, factory.m_ctx, width, height, layers, mips, fmt, data, sz); MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
d->m_SATexs.emplace_back(retval); MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
return retval; MetalTextureSA* retval = new MetalTextureSA(d, factory.m_ctx, width, height, layers, mips, fmt, data, sz);
d->m_SATexs.emplace_back(retval);
return retval;
}
} }
ITextureD* MetalDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt, ITextureD* MetalDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt,
TextureClampMode clampMode) TextureClampMode clampMode)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); {
MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue()); MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
MetalTextureD* retval = new MetalTextureD(d, q, factory.m_ctx, width, height, fmt); MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
d->m_DTexs.emplace_back(retval); MetalCommandQueue* q = static_cast<MetalCommandQueue*>(factory.m_parent->getCommandQueue());
return retval; MetalTextureD* retval = new MetalTextureD(d, q, factory.m_ctx, width, height, fmt);
d->m_DTexs.emplace_back(retval);
return retval;
}
} }
ITextureR* MetalDataFactory::Context::newRenderTexture(size_t width, size_t height, TextureClampMode clampMode, ITextureR* MetalDataFactory::Context::newRenderTexture(size_t width, size_t height, TextureClampMode clampMode,
size_t colorBindCount, size_t depthBindCount) size_t colorBindCount, size_t depthBindCount)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); {
MetalTextureR* retval = new MetalTextureR(d, factory.m_ctx, width, height, factory.m_sampleCount, MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
colorBindCount, depthBindCount); MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
d->m_RTexs.emplace_back(retval); MetalTextureR* retval = new MetalTextureR(d, factory.m_ctx, width, height, factory.m_sampleCount,
return retval; colorBindCount, depthBindCount);
d->m_RTexs.emplace_back(retval);
return retval;
}
} }
IVertexFormat* MetalDataFactory::Context::newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements, IVertexFormat* MetalDataFactory::Context::newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements,
size_t baseVert, size_t baseInst) size_t baseVert, size_t baseInst)
{ {
MetalData* d = MetalDataFactoryImpl::m_deferredData.get(); @autoreleasepool
MetalVertexFormat* retval = new struct MetalVertexFormat(d, elementCount, elements); {
d->m_VFmts.emplace_back(retval); MetalData* d = MetalDataFactoryImpl::m_deferredData.get();
return retval; MetalVertexFormat* retval = new struct MetalVertexFormat(d, elementCount, elements);
d->m_VFmts.emplace_back(retval);
return retval;
}
} }
IShaderPipeline* MetalDataFactory::Context::newShaderPipeline(const char* vertSource, const char* fragSource, IShaderPipeline* MetalDataFactory::Context::newShaderPipeline(const char* vertSource, const char* fragSource,
@ -1375,15 +1399,18 @@ MetalDataFactory::Context::newShaderDataBinding(IShaderPipeline* pipeline,
const int* texBindIdxs, const bool* depthBind, const int* texBindIdxs, const bool* depthBind,
size_t baseVert, size_t baseInst) size_t baseVert, size_t baseInst)
{ {
MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent); @autoreleasepool
MetalShaderDataBinding* retval = {
new MetalShaderDataBinding(MetalDataFactoryImpl::m_deferredData.get(), MetalDataFactoryImpl& factory = static_cast<MetalDataFactoryImpl&>(m_parent);
factory.m_ctx, pipeline, vbuf, instVbo, ibuf, MetalShaderDataBinding* retval =
ubufCount, ubufs, ubufStages, ubufOffs, new MetalShaderDataBinding(MetalDataFactoryImpl::m_deferredData.get(),
ubufSizes, texCount, texs, texBindIdxs, factory.m_ctx, pipeline, vbuf, instVbo, ibuf,
depthBind, baseVert, baseInst); ubufCount, ubufs, ubufStages, ubufOffs,
MetalDataFactoryImpl::m_deferredData->m_SBinds.emplace_back(retval); ubufSizes, texCount, texs, texBindIdxs,
return retval; depthBind, baseVert, baseInst);
MetalDataFactoryImpl::m_deferredData->m_SBinds.emplace_back(retval);
return retval;
}
} }
GraphicsDataToken MetalDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans) GraphicsDataToken MetalDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans)