Commit 2bd91927 authored by Laszlo Agocs's avatar Laszlo Agocs

mtl: Use per frame staging buffers

parent 86dffa9d
......@@ -312,6 +312,8 @@ public:
UsageFlags usage() const { return m_usage; }
void setUsage(UsageFlags u) { m_usage = u; }
// no restrictions here, up to the backend to round up if needed (that
// won't be visible in the user-provided size reported here)
int size() const { return m_size; }
void setSize(int sz) { m_size = sz; }
......@@ -321,7 +323,7 @@ protected:
QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, int size_);
Type m_type;
UsageFlags m_usage;
int m_size; // no restrictions here, up to the backend to round up if needed
int m_size;
void *m_reserved;
};
......@@ -964,6 +966,7 @@ public:
void draw(QRhiCommandBuffer *cb, quint32 vertexCount,
quint32 instanceCount = 1, quint32 firstVertex = 0, quint32 firstInstance = 0);
// final offset (indexOffset + firstIndex * n) must be 4 byte aligned with some backends
void drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount,
quint32 instanceCount = 1, quint32 firstIndex = 0,
qint32 vertexOffset = 0, quint32 firstInstance = 0);
......
......@@ -88,7 +88,7 @@ struct QRhiMetalData
} renderbuffer;
struct {
id<MTLTexture> texture;
id<MTLBuffer> stagingBuffer;
id<MTLBuffer> stagingBuffers[QMTL_FRAMES_IN_FLIGHT];
} texture;
struct {
id<MTLSamplerState> samplerState;
......@@ -113,7 +113,7 @@ struct QMetalRenderBufferData
struct QMetalTextureData
{
id<MTLTexture> tex = nil;
id<MTLBuffer> stagingBuf = nil;
id<MTLBuffer> stagingBuf[QMTL_FRAMES_IN_FLIGHT];
};
struct QMetalSamplerData
......@@ -587,10 +587,10 @@ void QRhiMetal::commitResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdateB
}
}
if (!utexD->d->stagingBuf)
utexD->d->stagingBuf = [d->dev newBufferWithLength: stagingSize options: MTLResourceStorageModeShared];
if (!utexD->d->stagingBuf[currentFrameSlot])
utexD->d->stagingBuf[currentFrameSlot] = [d->dev newBufferWithLength: stagingSize options: MTLResourceStorageModeShared];
void *mp = [utexD->d->stagingBuf contents];
void *mp = [utexD->d->stagingBuf[currentFrameSlot] contents];
qsizetype curOfs = 0;
for (int layer = 0, layerCount = u.desc.layers.count(); layer != layerCount; ++layer) {
......@@ -600,7 +600,7 @@ void QRhiMetal::commitResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdateB
const qsizetype imageSizeBytes = mipDesc.image.sizeInBytes();
if (imageSizeBytes > 0) {
memcpy(reinterpret_cast<char *>(mp) + curOfs, mipDesc.image.constBits(), imageSizeBytes);
[blitEnc copyFromBuffer: utexD->d->stagingBuf
[blitEnc copyFromBuffer: utexD->d->stagingBuf[currentFrameSlot]
sourceOffset: curOfs
sourceBytesPerRow: mipDesc.image.bytesPerLine()
sourceBytesPerImage: 0
......@@ -728,7 +728,8 @@ void QRhiMetal::executeDeferredReleases(bool forced)
break;
case QRhiMetalData::DeferredReleaseEntry::Texture:
[e.texture.texture release];
[e.texture.stagingBuffer release];
for (int i = 0; i < QMTL_FRAMES_IN_FLIGHT; ++i)
[e.texture.stagingBuffers[i] release];
break;
case QRhiMetalData::DeferredReleaseEntry::Sampler:
[e.sampler.samplerState release];
......@@ -860,6 +861,8 @@ QMetalTexture::QMetalTexture(QRhiImplementation *rhi, Format format, const QSize
: QRhiTexture(rhi, format, pixelSize, flags),
d(new QMetalTextureData)
{
for (int i = 0; i < QMTL_FRAMES_IN_FLIGHT; ++i)
d->stagingBuf[i] = nil;
}
QMetalTexture::~QMetalTexture()
......@@ -877,10 +880,12 @@ void QMetalTexture::release()
e.lastActiveFrameSlot = lastActiveFrameSlot;
e.texture.texture = d->tex;
e.texture.stagingBuffer = d->stagingBuf;
d->tex = nil;
d->stagingBuf = nil;
for (int i = 0; i < QMTL_FRAMES_IN_FLIGHT; ++i) {
e.texture.stagingBuffers[i] = d->stagingBuf[i];
d->stagingBuf[i] = nil;
}
QRHI_RES_RHI(QRhiMetal);
rhiD->d->releaseQueue.append(e);
......
mtl: should currentPassEncoder & co. be in the cb data instead?
mtl: stagingBuffer should be per frame to be safe
mtl: drop texture staging buf when !ChangesFrequently
mtl: cubemaps
mtl: depth tex
......@@ -11,6 +10,7 @@ mtl: msaa (onscreen)
test cubemap
test cubemap face as target
cbuffer alignment rule chaos - figure out what to recommend to users
resource import/export, what's the co-op story?
compressed textures
copyimage (color and ds, no resolve or transforms here)
msaa offscreen (msaa texture? renderbuffer?)
......@@ -22,7 +22,6 @@ d3d device loss
more what-if-resource-rebuilt cases
tex/QImage formats
anisotropic
adopt texture, buffer, ...
d3d: support DxcCompiler (in addition to d3dcompiler?) when runtime compiling hlsl?
test FrameOpDeviceLost somehow
gl: more ubuf types
......@@ -45,6 +44,8 @@ bytecode (fxc/dxc) for d3d?
bytecode for metal?
+++ done
mtl: stagingBuffer should be per frame to be safe
vk: per frame staging buffers
mtl: mipmap upload
mtl: indexed draw
mtl: basic texture
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment