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

Add a global id to QRhiResource

...which comes handy to avoid problems of alloc, destroy, alloc
-> same address as before.
parent ad38f57f
......@@ -1570,9 +1570,10 @@ QRhiReadbackDescription::QRhiReadbackDescription(QRhiTexture *texture)
/*!
\internal
*/
QRhiResource::QRhiResource(QRhiImplementation *rhi_)
: rhi(rhi_)
QRhiResource::QRhiResource(QRhiImplementation *rhi)
: m_rhi(rhi)
{
m_id = QRhiGlobalObjectIdGenerator::newId();
}
/*!
......@@ -1607,7 +1608,7 @@ void QRhiResource::releaseAndDestroy()
*/
QByteArray QRhiResource::name() const
{
return objectName;
return m_objectName;
}
/*!
......@@ -1633,8 +1634,8 @@ QByteArray QRhiResource::name() const
*/
void QRhiResource::setName(const QByteArray &name)
{
objectName = name;
objectName.replace(',', '_'); // cannot contain comma for QRhiProfiler
m_objectName = name;
m_objectName.replace(',', '_'); // cannot contain comma for QRhiProfiler
}
/*!
......@@ -1661,6 +1662,17 @@ bool QRhiResource::isShareable() const
return false;
}
/*!
\return the global, unique identifier of this QRhiResource.
User code rarely needs to deal with the value directly. It is used
internally for tracking and bookkeeping purposes.
*/
quint64 QRhiResource::globalResourceId() const
{
return m_id;
}
/*!
\class QRhiBuffer
\inmodule QtRhi
......@@ -3036,12 +3048,12 @@ QRhiImplementation::~QRhiImplementation()
if (rsh) {
for (QRhiResource *res : qAsConst(resources)) {
if (res->isShareable()) {
res->orphanedWithRsh = rsh;
res->m_orphanedWithRsh = rsh;
} else {
qWarning("QRhi %p going down orphaning an unreleased, non-shareable resource %p (%s). This is bad.",
q, res, res->objectName.constData());
q, res, res->m_objectName.constData());
}
res->rhi = nullptr;
res->m_rhi = nullptr;
}
} else {
// Be nice and show something about leaked stuff. Though we may not get
......@@ -3052,8 +3064,8 @@ QRhiImplementation::~QRhiImplementation()
qWarning("QRhi %p going down with %d unreleased resources and no QRhiResourceSharingHost. This is bad.",
q, resources.count());
for (QRhiResource *res : qAsConst(resources)) {
qWarning(" Resource %p (%s)", res, res->objectName.constData());
res->rhi = nullptr;
qWarning(" Resource %p (%s)", res, res->m_objectName.constData());
res->m_rhi = nullptr;
}
}
#endif
......@@ -3800,7 +3812,7 @@ void QRhiResourceUpdateBatchPrivate::merge(QRhiResourceUpdateBatchPrivate *other
*/
void QRhiCommandBuffer::resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates)
{
rhi->resourceUpdate(this, resourceUpdates);
m_rhi->resourceUpdate(this, resourceUpdates);
}
/*!
......@@ -3838,7 +3850,7 @@ void QRhiCommandBuffer::beginPass(QRhiRenderTarget *rt,
const QRhiDepthStencilClearValue &depthStencilClearValue,
QRhiResourceUpdateBatch *resourceUpdates)
{
rhi->beginPass(this, rt, colorClearValue, depthStencilClearValue, resourceUpdates);
m_rhi->beginPass(this, rt, colorClearValue, depthStencilClearValue, resourceUpdates);
}
/*!
......@@ -3849,7 +3861,7 @@ void QRhiCommandBuffer::beginPass(QRhiRenderTarget *rt,
*/
void QRhiCommandBuffer::endPass(QRhiResourceUpdateBatch *resourceUpdates)
{
rhi->endPass(this, resourceUpdates);
m_rhi->endPass(this, resourceUpdates);
}
/*!
......@@ -3867,7 +3879,7 @@ void QRhiCommandBuffer::endPass(QRhiResourceUpdateBatch *resourceUpdates)
*/
void QRhiCommandBuffer::setGraphicsPipeline(QRhiGraphicsPipeline *ps)
{
rhi->setGraphicsPipeline(this, ps);
m_rhi->setGraphicsPipeline(this, ps);
}
/*!
......@@ -3914,7 +3926,7 @@ void QRhiCommandBuffer::setGraphicsPipeline(QRhiGraphicsPipeline *ps)
void QRhiCommandBuffer::setShaderResources(QRhiShaderResourceBindings *srb,
const QVector<DynamicOffset> &dynamicOffsets)
{
rhi->setShaderResources(this, srb, dynamicOffsets);
m_rhi->setShaderResources(this, srb, dynamicOffsets);
}
/*!
......@@ -3973,7 +3985,7 @@ void QRhiCommandBuffer::setVertexInput(int startBinding, const QVector<VertexInp
QRhiBuffer *indexBuf, quint32 indexOffset,
IndexFormat indexFormat)
{
rhi->setVertexInput(this, startBinding, bindings, indexBuf, indexOffset, indexFormat);
m_rhi->setVertexInput(this, startBinding, bindings, indexBuf, indexOffset, indexFormat);
}
/*!
......@@ -3992,7 +4004,7 @@ void QRhiCommandBuffer::setVertexInput(int startBinding, const QVector<VertexInp
*/
void QRhiCommandBuffer::setViewport(const QRhiViewport &viewport)
{
rhi->setViewport(this, viewport);
m_rhi->setViewport(this, viewport);
}
/*!
......@@ -4011,7 +4023,7 @@ void QRhiCommandBuffer::setViewport(const QRhiViewport &viewport)
*/
void QRhiCommandBuffer::setScissor(const QRhiScissor &scissor)
{
rhi->setScissor(this, scissor);
m_rhi->setScissor(this, scissor);
}
/*!
......@@ -4025,7 +4037,7 @@ void QRhiCommandBuffer::setScissor(const QRhiScissor &scissor)
*/
void QRhiCommandBuffer::setBlendConstants(const QVector4D &c)
{
rhi->setBlendConstants(this, c);
m_rhi->setBlendConstants(this, c);
}
/*!
......@@ -4039,7 +4051,7 @@ void QRhiCommandBuffer::setBlendConstants(const QVector4D &c)
*/
void QRhiCommandBuffer::setStencilRef(quint32 refValue)
{
rhi->setStencilRef(this, refValue);
m_rhi->setStencilRef(this, refValue);
}
/*!
......@@ -4056,7 +4068,7 @@ void QRhiCommandBuffer::setStencilRef(quint32 refValue)
void QRhiCommandBuffer::draw(quint32 vertexCount,
quint32 instanceCount, quint32 firstVertex, quint32 firstInstance)
{
rhi->draw(this, vertexCount, instanceCount, firstVertex, firstInstance);
m_rhi->draw(this, vertexCount, instanceCount, firstVertex, firstInstance);
}
/*!
......@@ -4084,7 +4096,7 @@ void QRhiCommandBuffer::drawIndexed(quint32 indexCount,
quint32 instanceCount, quint32 firstIndex,
qint32 vertexOffset, quint32 firstInstance)
{
rhi->drawIndexed(this, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
m_rhi->drawIndexed(this, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
}
/*!
......@@ -4100,7 +4112,7 @@ void QRhiCommandBuffer::drawIndexed(quint32 indexCount,
*/
void QRhiCommandBuffer::debugMarkBegin(const QByteArray &name)
{
rhi->debugMarkBegin(this, name);
m_rhi->debugMarkBegin(this, name);
}
/*!
......@@ -4113,7 +4125,7 @@ void QRhiCommandBuffer::debugMarkBegin(const QByteArray &name)
*/
void QRhiCommandBuffer::debugMarkEnd()
{
rhi->debugMarkEnd(this);
m_rhi->debugMarkEnd(this);
}
/*!
......@@ -4128,7 +4140,7 @@ void QRhiCommandBuffer::debugMarkEnd()
*/
void QRhiCommandBuffer::debugMarkMsg(const QByteArray &msg)
{
rhi->debugMarkMsg(this, msg);
m_rhi->debugMarkMsg(this, msg);
}
/*!
......@@ -4515,4 +4527,10 @@ int QRhi::ubufAlignment() const
return d->ubufAlignment();
}
QRhiGlobalObjectIdGenerator::Type QRhiGlobalObjectIdGenerator::newId()
{
static QRhiGlobalObjectIdGenerator inst;
return ++inst.counter;
}
QT_END_NAMESPACE
......@@ -572,13 +572,16 @@ public:
virtual bool isShareable() const;
quint64 globalResourceId() const;
protected:
QRhiResource(QRhiImplementation *rhi_);
QRhiResource(QRhiImplementation *rhi);
Q_DISABLE_COPY(QRhiResource)
friend class QRhiImplementation;
QRhiImplementation *rhi = nullptr;
QByteArray objectName;
QRhiResourceSharingHostPrivate *orphanedWithRsh = nullptr;
QRhiImplementation *m_rhi = nullptr;
quint64 m_id;
QByteArray m_objectName;
QRhiResourceSharingHostPrivate *m_orphanedWithRsh = nullptr;
Q_DECL_UNUSED_MEMBER quint64 m_reserved;
};
......
......@@ -46,12 +46,13 @@
#include "qrhiprofiler_p.h"
#include <QBitArray>
#include <QAtomicInt>
#include <QAtomicInteger>
QT_BEGIN_NAMESPACE
#define QRHI_RES(t, x) static_cast<t *>(x)
#define QRHI_RES_RHI(t) t *rhiD = static_cast<t *>(rhi)
#define QRHI_PROF QRhiProfilerPrivate *rhiP = rhi->profilerPrivateOrNull()
#define QRHI_RES_RHI(t) t *rhiD = static_cast<t *>(m_rhi)
#define QRHI_PROF QRhiProfilerPrivate *rhiP = m_rhi->profilerPrivateOrNull()
#define QRHI_PROF_F(f) for (bool qrhip_enabled = rhiP != nullptr; qrhip_enabled; qrhip_enabled = false) rhiP->f
class QRhiReferenceRenderTarget : public QRhiRenderTarget
......@@ -162,7 +163,7 @@ public:
// only really care about resources that own native graphics resources underneath
void registerResource(QRhiResource *res)
{
res->orphanedWithRsh = nullptr;
res->m_orphanedWithRsh = nullptr;
resources.insert(res);
}
......@@ -178,9 +179,9 @@ public:
static bool orphanCheck(QRhiResource *res)
{
if (res->orphanedWithRsh) {
if (res->m_orphanedWithRsh) {
qWarning("Attempted to perform something on an orphaned QRhiResource %p (%s). This is invalid.",
res, res->objectName.constData());
res, res->m_objectName.constData());
return false;
}
return true;
......@@ -387,6 +388,20 @@ private:
int curBinding = -1;
};
class QRhiGlobalObjectIdGenerator
{
public:
#ifdef Q_ATOMIC_INT64_IS_SUPPORTED
using Type = quint64;
#else
using Type = quint32;
#endif
static Type newId();
private:
QAtomicInteger<Type> counter;
};
QT_END_NAMESPACE
#endif
......@@ -504,8 +504,9 @@ void QRhiD3D11::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBind
if (bufD->m_type == QRhiBuffer::Dynamic)
executeBufferHostWritesForCurrentFrame(bufD);
if (bufD->generation != bd.ubuf.generation) {
if (bufD->generation != bd.ubuf.generation || bufD->id != bd.ubuf.id) {
srbUpdate = true;
bd.ubuf.id = bufD->id;
bd.ubuf.generation = bufD->generation;
}
......@@ -518,10 +519,14 @@ void QRhiD3D11::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBind
QD3D11Texture *texD = QRHI_RES(QD3D11Texture, b->u.stex.tex);
QD3D11Sampler *samplerD = QRHI_RES(QD3D11Sampler, b->u.stex.sampler);
if (texD->generation != bd.stex.texGeneration
|| samplerD->generation != bd.stex.samplerGeneration)
|| texD->id != bd.stex.texId
|| samplerD->generation != bd.stex.samplerGeneration
|| samplerD->id != bd.stex.samplerId)
{
srbUpdate = true;
bd.stex.texId = texD->id;
bd.stex.texGeneration = texD->generation;
bd.stex.samplerId = samplerD->id;
bd.stex.samplerGeneration = samplerD->generation;
}
}
......@@ -1456,6 +1461,7 @@ void QRhiD3D11::updateShaderResourceBindings(QD3D11ShaderResourceBindings *srbD)
{
QD3D11Buffer *bufD = QRHI_RES(QD3D11Buffer, b->u.ubuf.buf);
Q_ASSERT(aligned(b->u.ubuf.offset, 256) == b->u.ubuf.offset);
bd.ubuf.id = bufD->id;
bd.ubuf.generation = bufD->generation;
// dynamic ubuf offsets are not considered here, those are baked in
// at a later stage, which is good as vsubufoffsets and friends are
......@@ -1483,7 +1489,9 @@ void QRhiD3D11::updateShaderResourceBindings(QD3D11ShaderResourceBindings *srbD)
// with registers sN and tN by SPIRV-Cross.
QD3D11Texture *texD = QRHI_RES(QD3D11Texture, b->u.stex.tex);
QD3D11Sampler *samplerD = QRHI_RES(QD3D11Sampler, b->u.stex.sampler);
bd.stex.texId = texD->id;
bd.stex.texGeneration = texD->generation;
bd.stex.samplerId = samplerD->id;
bd.stex.samplerGeneration = samplerD->generation;
if (b->stage.testFlag(QRhiShaderResourceBinding::VertexStage)) {
srbD->vssamplers.feed(b->binding, samplerD->samplerState);
......@@ -1810,7 +1818,7 @@ void QD3D11Buffer::release()
buffer->Release();
buffer = nullptr;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiD3D11);
QRHI_PROF;
QRHI_PROF_F(releaseBuffer(this));
......@@ -1860,8 +1868,8 @@ bool QD3D11Buffer::build()
hasPendingDynamicUpdates = false;
}
if (!objectName.isEmpty())
buffer->SetPrivateData(WKPDID_D3DDebugObjectName, objectName.size(), objectName.constData());
if (!m_objectName.isEmpty())
buffer->SetPrivateData(WKPDID_D3DDebugObjectName, m_objectName.size(), m_objectName.constData());
QRHI_PROF;
QRHI_PROF_F(newBuffer(this, roundedSize, 1, m_type == Dynamic ? 1 : 0));
......@@ -1900,7 +1908,7 @@ void QD3D11RenderBuffer::release()
tex->Release();
tex = nullptr;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiD3D11);
QRHI_PROF;
QRHI_PROF_F(releaseRenderBuffer(this));
......@@ -1972,8 +1980,8 @@ bool QD3D11RenderBuffer::build()
return false;
}
if (!objectName.isEmpty())
tex->SetPrivateData(WKPDID_D3DDebugObjectName, objectName.size(), objectName.constData());
if (!m_objectName.isEmpty())
tex->SetPrivateData(WKPDID_D3DDebugObjectName, m_objectName.size(), m_objectName.constData());
QRHI_PROF;
QRHI_PROF_F(newRenderBuffer(this, false, false, sampleDesc.Count));
......@@ -2013,7 +2021,7 @@ void QD3D11Texture::release()
tex = nullptr;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiD3D11);
QRHI_PROF;
QRHI_PROF_F(releaseTexture(this));
......@@ -2166,8 +2174,8 @@ bool QD3D11Texture::build()
if (!finishBuild())
return false;
if (!objectName.isEmpty())
tex->SetPrivateData(WKPDID_D3DDebugObjectName, objectName.size(), objectName.constData());
if (!m_objectName.isEmpty())
tex->SetPrivateData(WKPDID_D3DDebugObjectName, m_objectName.size(), m_objectName.constData());
QRHI_PROF;
QRHI_PROF_F(newTexture(this, true, mipLevelCount, isCube ? 6 : 1, sampleDesc.Count));
......@@ -2224,7 +2232,7 @@ void QD3D11Sampler::release()
samplerState->Release();
samplerState = nullptr;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiD3D11);
rhiD->unregisterResource(this);
}
......
......@@ -189,10 +189,13 @@ struct QD3D11ShaderResourceBindings : public QRhiShaderResourceBindings
// Keep track of the generation number of each referenced QRhi* to be able
// to detect that the batched bindings are out of date.
struct BoundUniformBufferData {
quint64 id;
uint generation;
};
struct BoundSampledTextureData {
quint64 texId;
uint texGeneration;
quint64 samplerId;
uint samplerGeneration;
};
struct BoundResourceData {
......
......@@ -940,11 +940,11 @@ void QRhiGles2::enqueueResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdate
Q_ASSERT(bufD->m_type == QRhiBuffer::Dynamic);
if (bufD->m_usage.testFlag(QRhiBuffer::UniformBuffer)) {
memcpy(bufD->ubuf.data() + u.offset, u.data.constData(), u.data.size());
QGles2Buffer::ChangeRange &r(bufD->ubufChangeRange);
if (r.changeBegin == -1 || u.offset < r.changeBegin)
r.changeBegin = u.offset;
if (r.changeEnd == -1 || u.offset + u.data.size() > r.changeEnd)
r.changeEnd = u.offset + u.data.size();
// QGles2Buffer::ChangeRange &r(bufD->ubufChangeRange);
// if (r.changeBegin == -1 || u.offset < r.changeBegin)
// r.changeBegin = u.offset;
// if (r.changeEnd == -1 || u.offset + u.data.size() > r.changeEnd)
// r.changeEnd = u.offset + u.data.size();
} else {
QGles2CommandBuffer::Command cmd;
cmd.cmd = QGles2CommandBuffer::Command::BufferSubData;
......@@ -963,7 +963,7 @@ void QRhiGles2::enqueueResourceUpdates(QRhiCommandBuffer *cb, QRhiResourceUpdate
Q_ASSERT(u.offset + u.data.size() <= bufD->m_size);
if (bufD->m_usage.testFlag(QRhiBuffer::UniformBuffer)) {
memcpy(bufD->ubuf.data() + u.offset, u.data.constData(), u.data.size());
bufD->ubufChangeRange = { 0, u.data.size() };
// bufD->ubufChangeRange = { 0, u.data.size() };
} else {
QGles2CommandBuffer::Command cmd;
cmd.cmd = QGles2CommandBuffer::Command::BufferSubData;
......@@ -1459,8 +1459,7 @@ void QRhiGles2::executeCommandBuffer(QRhiCommandBuffer *cb)
setChangedUniforms(cmd.args.bindShaderResources.ps,
cmd.args.bindShaderResources.srb,
cmd.args.bindShaderResources.dynamicOffsetPairs,
cmd.args.bindShaderResources.dynamicOffsetCount,
false);
cmd.args.bindShaderResources.dynamicOffsetCount);
break;
case QGles2CommandBuffer::Command::BindFramebuffer:
if (cmd.args.bindFramebuffer.fbo)
......@@ -1650,12 +1649,8 @@ void QRhiGles2::executeBindGraphicsPipeline(QRhiGraphicsPipeline *ps)
}
void QRhiGles2::setChangedUniforms(QRhiGraphicsPipeline *ps, QRhiShaderResourceBindings *srb,
const uint *dynOfsPairs, int dynOfsCount,
bool changedOnly)
const uint *dynOfsPairs, int dynOfsCount)
{
if (dynOfsCount)
changedOnly = false;
QGles2GraphicsPipeline *psD = QRHI_RES(QGles2GraphicsPipeline, ps);
QGles2ShaderResourceBindings *srbD = QRHI_RES(QGles2ShaderResourceBindings, srb);
......@@ -1666,10 +1661,6 @@ void QRhiGles2::setChangedUniforms(QRhiGraphicsPipeline *ps, QRhiShaderResourceB
switch (b->type) {
case QRhiShaderResourceBinding::UniformBuffer:
{
QGles2Buffer *bufD = QRHI_RES(QGles2Buffer, b->u.ubuf.buf);
if (changedOnly && bufD->ubufChangeRange.isNull()) // do not set again when nothing changed
break;
int viewOffset = b->u.ubuf.offset;
if (dynOfsCount) {
for (int j = 0; j < dynOfsCount; ++j) {
......@@ -1679,14 +1670,11 @@ void QRhiGles2::setChangedUniforms(QRhiGraphicsPipeline *ps, QRhiShaderResourceB
}
}
}
QGles2Buffer *bufD = QRHI_RES(QGles2Buffer, b->u.ubuf.buf);
const QByteArray bufView = QByteArray::fromRawData(bufD->ubuf.constData() + viewOffset,
b->u.ubuf.maybeSize ? b->u.ubuf.maybeSize : bufD->m_size);
for (QGles2GraphicsPipeline::Uniform &uniform : psD->uniforms) {
if (uniform.binding == b->binding
&& (!changedOnly ||
(uniform.offset >= uint(bufD->ubufChangeRange.changeBegin)
&& uniform.offset < uint(bufD->ubufChangeRange.changeEnd))))
{
if (uniform.binding == b->binding) {
memcpy(uniform.data.data(), bufView.constData() + uniform.offset, uniform.data.size());
switch (uniform.type) {
......@@ -1730,20 +1718,23 @@ void QRhiGles2::setChangedUniforms(QRhiGraphicsPipeline *ps, QRhiShaderResourceB
}
}
bufD->ubufChangeRange = QGles2Buffer::ChangeRange();
// bufD->ubufChangeRange = QGles2Buffer::ChangeRange();
}
break;
case QRhiShaderResourceBinding::SampledTexture:
{
QGles2Texture *texD = QRHI_RES(QGles2Texture, b->u.stex.tex);
QGles2Sampler *samplerD = QRHI_RES(QGles2Sampler, b->u.stex.sampler);
const bool textureChanged = QRHI_RES(QGles2Texture, b->u.stex.tex)->generation != bd.stex.texGeneration;
if (textureChanged)
bd.stex.texGeneration = QRHI_RES(QGles2Texture, b->u.stex.tex)->generation;
const bool samplerChanged = QRHI_RES(QGles2Sampler, b->u.stex.sampler)->generation != bd.stex.samplerGeneration;
if (samplerChanged)
bd.stex.samplerGeneration = QRHI_RES(QGles2Sampler, b->u.stex.sampler)->generation;
const bool textureChanged = texD->generation != bd.stex.texGeneration || texD->m_id != bd.stex.texId;
const bool samplerChanged = samplerD->generation != bd.stex.samplerGeneration || samplerD->m_id != bd.stex.samplerId;
if (textureChanged) {
bd.stex.texId = texD->m_id;
bd.stex.texGeneration = texD->generation;
}
if (samplerChanged) {
bd.stex.samplerId = samplerD->m_id;
bd.stex.samplerGeneration = samplerD->generation;
}
int texUnit = 0;
for (QGles2GraphicsPipeline::Sampler &sampler : psD->samplers) {
......@@ -1911,7 +1902,7 @@ void QGles2Buffer::release()
buffer = 0;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiGles2);
rhiD->releaseQueue.append(e);
QRHI_PROF;
......@@ -1919,7 +1910,7 @@ void QGles2Buffer::release()
rhiD->unregisterResource(this);
} else {
// associated rhi is already gone, queue the deferred release to the rsh instead
addToRshReleaseQueue(orphanedWithRsh, e);
addToRshReleaseQueue(m_orphanedWithRsh, e);
}
}
......@@ -1981,7 +1972,7 @@ void QGles2RenderBuffer::release()
renderbuffer = 0;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiGles2);
rhiD->releaseQueue.append(e);
QRHI_PROF;
......@@ -1989,7 +1980,7 @@ void QGles2RenderBuffer::release()
rhiD->unregisterResource(this);
} else {
// associated rhi is already gone, queue the deferred release to the rsh instead
addToRshReleaseQueue(orphanedWithRsh, e);
addToRshReleaseQueue(m_orphanedWithRsh, e);
}
}
......@@ -2081,7 +2072,7 @@ void QGles2Texture::release()
specified = false;
nativeHandlesStruct.texture = 0;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiGles2);
if (owns)
rhiD->releaseQueue.append(e);
......@@ -2090,7 +2081,7 @@ void QGles2Texture::release()
rhiD->unregisterResource(this);
} else {
// associated rhi is already gone, queue the deferred release to the rsh instead
addToRshReleaseQueue(orphanedWithRsh, e);
addToRshReleaseQueue(m_orphanedWithRsh, e);
}
}
......@@ -2313,7 +2304,7 @@ void QGles2TextureRenderTarget::release()
QRhiRenderPassDescriptor *QGles2TextureRenderTarget::newCompatibleRenderPassDescriptor()
{
return new QGles2RenderPassDescriptor(rhi);
return new QGles2RenderPassDescriptor(m_rhi);
}
bool QGles2TextureRenderTarget::build()
......@@ -2414,8 +2405,10 @@ bool QGles2ShaderResourceBindings::build()
break;
case QRhiShaderResourceBinding::SampledTexture:
// Start with values that will fail the first comparison for sure.
bd.stex.texGeneration = UINT_MAX; // QRHI_RES(QGles2Texture, b.stex.tex)->generation;
bd.stex.samplerGeneration = UINT_MAX; // QRHI_RES(QGles2Sampler, b.stex.sampler)->generation;
bd.stex.texId = UINT_MAX;
bd.stex.texGeneration = UINT_MAX;
bd.stex.samplerId = UINT_MAX;
bd.stex.samplerGeneration = UINT_MAX;
break;
default:
Q_UNREACHABLE();
......@@ -2619,7 +2612,7 @@ QSize QGles2SwapChain::surfacePixelSize()
QRhiRenderPassDescriptor *QGles2SwapChain::newCompatibleRenderPassDescriptor()
{
return new QGles2RenderPassDescriptor(rhi);
return new QGles2RenderPassDescriptor(m_rhi);
}
bool QGles2SwapChain::buildOrResize()
......
......@@ -62,15 +62,15 @@ struct QGles2Buffer : public QRhiBuffer
GLuint buffer = 0;
GLenum target;
QByteArray ubuf;
struct ChangeRange {
ChangeRange(int b = -1, int e = -1)
: changeBegin(b), changeEnd(e)
{ }
bool isNull() const { return changeBegin == -1 && changeEnd == -1; }
int changeBegin;
int changeEnd;
};
ChangeRange ubufChangeRange;
// struct ChangeRange {
// ChangeRange(int b = -1, int e = -1)
// : changeBegin(b), changeEnd(e)
// { }
// bool isNull() const { return changeBegin == -1 && changeEnd == -1; }
// int changeBegin;
// int changeEnd;
// };
// ChangeRange ubufChangeRange;
friend class QRhiGles2;
};
......@@ -183,7 +183,9 @@ struct QGles2ShaderResourceBindings : public QRhiShaderResourceBindings
bool build() override;
struct BoundSampledTextureData {
quint64 texId;
uint texGeneration;
quint64 samplerId;
uint samplerGeneration;
};
struct BoundResourceData {
......@@ -548,8 +550,7 @@ public:
void executeCommandBuffer(QRhiCommandBuffer *cb);
void executeBindGraphicsPipeline(QRhiGraphicsPipeline *ps);
void setChangedUniforms(QRhiGraphicsPipeline *ps, QRhiShaderResourceBindings *srb,
const uint *dynOfsPairs, int dynOfsCount,
bool changedOnly);
const uint *dynOfsPairs, int dynOfsCount);
QOpenGLContext *ctx = nullptr;
bool importedContext = false;
......
......@@ -748,8 +748,9 @@ void QRhiMetal::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBind
hasSlottedResourceInSrb = true;
if (b->u.ubuf.hasDynamicOffset)
hasDynamicOffsetInSrb = true;
if (bufD->generation != bd.ubuf.generation) {
if (bufD->generation != bd.ubuf.generation || bufD->m_id != bd.ubuf.id) {
resNeedsRebind = true;
bd.ubuf.id = bufD->m_id;
bd.ubuf.generation = bufD->generation;
}
bufD->lastActiveFrameSlot = currentFrameSlot;
......@@ -760,10 +761,14 @@ void QRhiMetal::setShaderResources(QRhiCommandBuffer *cb, QRhiShaderResourceBind
QMetalTexture *texD = QRHI_RES(QMetalTexture, b->u.stex.tex);
QMetalSampler *samplerD = QRHI_RES(QMetalSampler, b->u.stex.sampler);
if (texD->generation != bd.stex.texGeneration
|| samplerD->generation != bd.stex.samplerGeneration)
|| texD->m_id != bd.stex.texId
|| samplerD->generation != bd.stex.samplerGeneration
|| samplerD->m_id != bd.stex.samplerId)
{
resNeedsRebind = true;
bd.stex.texId = texD->m_id;
bd.stex.texGeneration = texD->generation;
bd.stex.samplerId = samplerD->m_id;
bd.stex.samplerGeneration = samplerD->generation;
}
texD->lastActiveFrameSlot = currentFrameSlot;
......@@ -1718,7 +1723,7 @@ void QMetalBuffer::release()
d->pendingUpdates[i].clear();
}
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiMetal);
rhiD->d->releaseQueue.append(e);
QRHI_PROF;
......@@ -1726,7 +1731,7 @@ void QMetalBuffer::release()
rhiD->unregisterResource(this);
} else {
// associated rhi is already gone, queue the deferred release to the rsh instead
addToRshReleaseQueue(orphanedWithRsh, e);
addToRshReleaseQueue(m_orphanedWithRsh, e);
}
}
......@@ -1757,11 +1762,11 @@ bool QMetalBuffer::build()
if (i == 0 || m_type != Immutable) {
d->buf[i] = [rhiD->d->dev newBufferWithLength: roundedSize options: opts];
d->pendingUpdates[i].reserve(16);
if (!objectName.isEmpty()) {
if (!m_objectName.isEmpty()) {
if (m_type == Immutable) {
d->buf[i].label = [NSString stringWithUTF8String: objectName.constData()];
d->buf[i].label = [NSString stringWithUTF8String: m_objectName.constData()];
} else {
const QByteArray name = objectName + '/' + QByteArray::number(i);
const QByteArray name = m_objectName + '/' + QByteArray::number(i);
d->buf[i].label = [NSString stringWithUTF8String: name.constData()];
}
}
......@@ -1806,14 +1811,14 @@ void QMetalRenderBuffer::release()
e.renderbuffer.texture = d->tex;
d->tex = nil;
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiMetal);
rhiD->d->releaseQueue.append(e);
QRHI_PROF;
QRHI_PROF_F(releaseRenderBuffer(this));
rhiD->unregisterResource(this);
} else {
addToRshReleaseQueue(orphanedWithRsh, e);
addToRshReleaseQueue(m_orphanedWithRsh, e);
}
}
......@@ -1866,8 +1871,8 @@ bool QMetalRenderBuffer::build()
d->tex = [rhiD->d->dev newTextureWithDescriptor: desc];
[desc release];
if (!objectName.isEmpty())
d->tex.label = [NSString stringWithUTF8String: objectName.constData()];
if (!m_objectName.isEmpty())
d->tex.label = [NSString stringWithUTF8String: m_objectName.constData()];
QRHI_PROF;
QRHI_PROF_F(newRenderBuffer(this, transientBacking, false, samples));
......@@ -1920,14 +1925,14 @@ void QMetalTexture::release()
d->stagingBuf[i] = nil;
}
if (!orphanedWithRsh) {
if (!m_orphanedWithRsh) {
QRHI_RES_RHI(QRhiMetal);
rhiD->d->releaseQueue.append(e);
QRHI_PROF;
QRHI_PROF_F(releaseTexture(this));
rhiD->unregisterResource(this);
} else {
addToRshReleaseQueue(orphanedWithRsh, e);
addToRshReleaseQueue(m_orphanedWithRsh, e);
}