diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-10-10 11:53:15 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-10-11 10:59:05 +0200 |
commit | 8116df5331a8bc17b9173437150ace8d5defd2c0 (patch) | |
tree | 2815f862fece4b39e7a6d5551b68b73ff4abbd82 /src/gui/rhi | |
parent | 755f24f153087eeafa332b36b4233ee7515c2352 (diff) |
rhi: d3d: Save a few cycles in bindShaderResources
Change-Id: Ib11ea33eb695d4599f4f040415d497aaf19cb15b
Reviewed-by: Andy Nichols <andy.nichols@qt.io>
Diffstat (limited to 'src/gui/rhi')
-rw-r--r-- | src/gui/rhi/qrhi_p_p.h | 3 | ||||
-rw-r--r-- | src/gui/rhi/qrhid3d11.cpp | 254 | ||||
-rw-r--r-- | src/gui/rhi/qrhid3d11_p_p.h | 8 |
3 files changed, 144 insertions, 121 deletions
diff --git a/src/gui/rhi/qrhi_p_p.h b/src/gui/rhi/qrhi_p_p.h index 862d68f5a5..491f0c6f26 100644 --- a/src/gui/rhi/qrhi_p_p.h +++ b/src/gui/rhi/qrhi_p_p.h @@ -517,9 +517,10 @@ struct QRhiBatchedBindings curBinding = binding; } - void finish() { + bool finish() { if (!curBatch.resources.isEmpty()) batches.append(curBatch); + return !batches.isEmpty(); } void clear() { diff --git a/src/gui/rhi/qrhid3d11.cpp b/src/gui/rhi/qrhid3d11.cpp index 09a1c28bc9..9fccff7b5c 100644 --- a/src/gui/rhi/qrhid3d11.cpp +++ b/src/gui/rhi/qrhid3d11.cpp @@ -2106,7 +2106,7 @@ void QRhiD3D11::updateShaderResourceBindings(QD3D11ShaderResourceBindings *srbD, srbD->vsubufoffsets.feed(buf.breg, buf.offsetInConstants); srbD->vsubufsizes.feed(buf.breg, buf.sizeInConstants); } - srbD->vsubufs.finish(); + srbD->vsubufsPresent = srbD->vsubufs.finish(); srbD->vsubuforigbindings.finish(); srbD->vsubufoffsets.finish(); srbD->vsubufsizes.finish(); @@ -2117,7 +2117,7 @@ void QRhiD3D11::updateShaderResourceBindings(QD3D11ShaderResourceBindings *srbD, srbD->fsubufoffsets.feed(buf.breg, buf.offsetInConstants); srbD->fsubufsizes.feed(buf.breg, buf.sizeInConstants); } - srbD->fsubufs.finish(); + srbD->fsubufsPresent = srbD->fsubufs.finish(); srbD->fsubuforigbindings.finish(); srbD->fsubufoffsets.finish(); srbD->fsubufsizes.finish(); @@ -2128,7 +2128,7 @@ void QRhiD3D11::updateShaderResourceBindings(QD3D11ShaderResourceBindings *srbD, srbD->csubufoffsets.feed(buf.breg, buf.offsetInConstants); srbD->csubufsizes.feed(buf.breg, buf.sizeInConstants); } - srbD->csubufs.finish(); + srbD->csubufsPresent = srbD->csubufs.finish(); srbD->csubuforigbindings.finish(); srbD->csubufoffsets.finish(); srbD->csubufsizes.finish(); @@ -2137,26 +2137,26 @@ void QRhiD3D11::updateShaderResourceBindings(QD3D11ShaderResourceBindings *srbD, srbD->vsshaderresources.feed(t.treg, t.srv); for (const Stage::Sampler &s : qAsConst(res[RBM_VERTEX].samplers)) srbD->vssamplers.feed(s.sreg, s.sampler); - srbD->vssamplers.finish(); + srbD->vssamplersPresent = srbD->vssamplers.finish(); srbD->vsshaderresources.finish(); for (const Stage::Texture &t : qAsConst(res[RBM_FRAGMENT].textures)) srbD->fsshaderresources.feed(t.treg, t.srv); for (const Stage::Sampler &s : qAsConst(res[RBM_FRAGMENT].samplers)) srbD->fssamplers.feed(s.sreg, s.sampler); - srbD->fssamplers.finish(); + srbD->fssamplersPresent = srbD->fssamplers.finish(); srbD->fsshaderresources.finish(); for (const Stage::Texture &t : qAsConst(res[RBM_COMPUTE].textures)) srbD->csshaderresources.feed(t.treg, t.srv); for (const Stage::Sampler &s : qAsConst(res[RBM_COMPUTE].samplers)) srbD->cssamplers.feed(s.sreg, s.sampler); - srbD->cssamplers.finish(); + srbD->cssamplersPresent = srbD->cssamplers.finish(); srbD->csshaderresources.finish(); for (const Stage::Uav &u : qAsConst(res[RBM_COMPUTE].uavs)) srbD->csUAVs.feed(u.ureg, u.uav); - srbD->csUAVs.finish(); + srbD->csUAVsPresent = srbD->csUAVs.finish(); } void QRhiD3D11::executeBufferHostWrites(QD3D11Buffer *bufD) @@ -2214,143 +2214,157 @@ void QRhiD3D11::bindShaderResources(QD3D11ShaderResourceBindings *srbD, const uint *dynOfsPairs, int dynOfsPairCount, bool offsetOnlyChange) { - if (!offsetOnlyChange) { - for (const auto &batch : srbD->vssamplers.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, "VS sampler"); - if (count) - context->VSSetSamplers(batch.startBinding, count, batch.resources.constData()); - } + UINT offsets[QD3D11CommandBuffer::Command::MAX_DYNAMIC_OFFSET_COUNT]; - for (const auto &batch : srbD->vsshaderresources.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, "VS SRV"); + if (srbD->vsubufsPresent) { + for (int i = 0, ie = srbD->vsubufs.batches.count(); i != ie; ++i) { + const uint count = clampedResourceCount(srbD->vsubufs.batches[i].startBinding, + srbD->vsubufs.batches[i].resources.count(), + D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, + "VS cbuf"); if (count) { - context->VSSetShaderResources(batch.startBinding, count, batch.resources.constData()); - contextState.vsHighestActiveSrvBinding = qMax(contextState.vsHighestActiveSrvBinding, - int(batch.startBinding + count) - 1); + if (!dynOfsPairCount) { + context->VSSetConstantBuffers1(srbD->vsubufs.batches[i].startBinding, + count, + srbD->vsubufs.batches[i].resources.constData(), + srbD->vsubufoffsets.batches[i].resources.constData(), + srbD->vsubufsizes.batches[i].resources.constData()); + } else { + applyDynamicOffsets(offsets, i, &srbD->vsubuforigbindings, &srbD->vsubufoffsets, + dynOfsPairs, dynOfsPairCount); + context->VSSetConstantBuffers1(srbD->vsubufs.batches[i].startBinding, + count, + srbD->vsubufs.batches[i].resources.constData(), + offsets, + srbD->vsubufsizes.batches[i].resources.constData()); + } } } + } - for (const auto &batch : srbD->fssamplers.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, "PS sampler"); - if (count) - context->PSSetSamplers(batch.startBinding, count, batch.resources.constData()); - } - - for (const auto &batch : srbD->fsshaderresources.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, "PS SRV"); + if (srbD->fsubufsPresent) { + for (int i = 0, ie = srbD->fsubufs.batches.count(); i != ie; ++i) { + const uint count = clampedResourceCount(srbD->fsubufs.batches[i].startBinding, + srbD->fsubufs.batches[i].resources.count(), + D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, + "PS cbuf"); if (count) { - context->PSSetShaderResources(batch.startBinding, count, batch.resources.constData()); - contextState.fsHighestActiveSrvBinding = qMax(contextState.fsHighestActiveSrvBinding, - int(batch.startBinding + count) - 1); + if (!dynOfsPairCount) { + context->PSSetConstantBuffers1(srbD->fsubufs.batches[i].startBinding, + count, + srbD->fsubufs.batches[i].resources.constData(), + srbD->fsubufoffsets.batches[i].resources.constData(), + srbD->fsubufsizes.batches[i].resources.constData()); + } else { + applyDynamicOffsets(offsets, i, &srbD->fsubuforigbindings, &srbD->fsubufoffsets, + dynOfsPairs, dynOfsPairCount); + context->PSSetConstantBuffers1(srbD->fsubufs.batches[i].startBinding, + count, + srbD->fsubufs.batches[i].resources.constData(), + offsets, + srbD->fsubufsizes.batches[i].resources.constData()); + } } } + } - for (const auto &batch : srbD->cssamplers.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, "CS sampler"); - if (count) - context->CSSetSamplers(batch.startBinding, count, batch.resources.constData()); - } - - for (const auto &batch : srbD->csshaderresources.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, "CS SRV"); + if (srbD->csubufsPresent) { + for (int i = 0, ie = srbD->csubufs.batches.count(); i != ie; ++i) { + const uint count = clampedResourceCount(srbD->csubufs.batches[i].startBinding, + srbD->csubufs.batches[i].resources.count(), + D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, + "CS cbuf"); if (count) { - context->CSSetShaderResources(batch.startBinding, count, batch.resources.constData()); - contextState.csHighestActiveSrvBinding = qMax(contextState.csHighestActiveSrvBinding, - int(batch.startBinding + count) - 1); + if (!dynOfsPairCount) { + context->CSSetConstantBuffers1(srbD->csubufs.batches[i].startBinding, + count, + srbD->csubufs.batches[i].resources.constData(), + srbD->csubufoffsets.batches[i].resources.constData(), + srbD->csubufsizes.batches[i].resources.constData()); + } else { + applyDynamicOffsets(offsets, i, &srbD->csubuforigbindings, &srbD->csubufoffsets, + dynOfsPairs, dynOfsPairCount); + context->CSSetConstantBuffers1(srbD->csubufs.batches[i].startBinding, + count, + srbD->csubufs.batches[i].resources.constData(), + offsets, + srbD->csubufsizes.batches[i].resources.constData()); + } } } } - UINT offsets[QD3D11CommandBuffer::Command::MAX_DYNAMIC_OFFSET_COUNT]; + if (!offsetOnlyChange) { + if (srbD->vssamplersPresent) { + for (const auto &batch : srbD->vssamplers.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, "VS sampler"); + if (count) + context->VSSetSamplers(batch.startBinding, count, batch.resources.constData()); + } - for (int i = 0, ie = srbD->vsubufs.batches.count(); i != ie; ++i) { - const uint count = clampedResourceCount(srbD->vsubufs.batches[i].startBinding, - srbD->vsubufs.batches[i].resources.count(), - D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, - "VS cbuf"); - if (count) { - if (!dynOfsPairCount) { - context->VSSetConstantBuffers1(srbD->vsubufs.batches[i].startBinding, - count, - srbD->vsubufs.batches[i].resources.constData(), - srbD->vsubufoffsets.batches[i].resources.constData(), - srbD->vsubufsizes.batches[i].resources.constData()); - } else { - applyDynamicOffsets(offsets, i, &srbD->vsubuforigbindings, &srbD->vsubufoffsets, - dynOfsPairs, dynOfsPairCount); - context->VSSetConstantBuffers1(srbD->vsubufs.batches[i].startBinding, - count, - srbD->vsubufs.batches[i].resources.constData(), - offsets, - srbD->vsubufsizes.batches[i].resources.constData()); + for (const auto &batch : srbD->vsshaderresources.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, "VS SRV"); + if (count) { + context->VSSetShaderResources(batch.startBinding, count, batch.resources.constData()); + contextState.vsHighestActiveSrvBinding = qMax(contextState.vsHighestActiveSrvBinding, + int(batch.startBinding + count) - 1); + } } } - } - for (int i = 0, ie = srbD->fsubufs.batches.count(); i != ie; ++i) { - const uint count = clampedResourceCount(srbD->fsubufs.batches[i].startBinding, - srbD->fsubufs.batches[i].resources.count(), - D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, - "PS cbuf"); - if (count) { - if (!dynOfsPairCount) { - context->PSSetConstantBuffers1(srbD->fsubufs.batches[i].startBinding, - count, - srbD->fsubufs.batches[i].resources.constData(), - srbD->fsubufoffsets.batches[i].resources.constData(), - srbD->fsubufsizes.batches[i].resources.constData()); - } else { - applyDynamicOffsets(offsets, i, &srbD->fsubuforigbindings, &srbD->fsubufoffsets, - dynOfsPairs, dynOfsPairCount); - context->PSSetConstantBuffers1(srbD->fsubufs.batches[i].startBinding, - count, - srbD->fsubufs.batches[i].resources.constData(), - offsets, - srbD->fsubufsizes.batches[i].resources.constData()); + if (srbD->fssamplersPresent) { + for (const auto &batch : srbD->fssamplers.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, "PS sampler"); + if (count) + context->PSSetSamplers(batch.startBinding, count, batch.resources.constData()); + } + + for (const auto &batch : srbD->fsshaderresources.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, "PS SRV"); + if (count) { + context->PSSetShaderResources(batch.startBinding, count, batch.resources.constData()); + contextState.fsHighestActiveSrvBinding = qMax(contextState.fsHighestActiveSrvBinding, + int(batch.startBinding + count) - 1); + } } } - } - for (int i = 0, ie = srbD->csubufs.batches.count(); i != ie; ++i) { - const uint count = clampedResourceCount(srbD->csubufs.batches[i].startBinding, - srbD->csubufs.batches[i].resources.count(), - D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, - "CS cbuf"); - if (count) { - if (!dynOfsPairCount) { - context->CSSetConstantBuffers1(srbD->csubufs.batches[i].startBinding, - count, - srbD->csubufs.batches[i].resources.constData(), - srbD->csubufoffsets.batches[i].resources.constData(), - srbD->csubufsizes.batches[i].resources.constData()); - } else { - applyDynamicOffsets(offsets, i, &srbD->csubuforigbindings, &srbD->csubufoffsets, - dynOfsPairs, dynOfsPairCount); - context->CSSetConstantBuffers1(srbD->csubufs.batches[i].startBinding, - count, - srbD->csubufs.batches[i].resources.constData(), - offsets, - srbD->csubufsizes.batches[i].resources.constData()); + if (srbD->cssamplersPresent) { + for (const auto &batch : srbD->cssamplers.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, "CS sampler"); + if (count) + context->CSSetSamplers(batch.startBinding, count, batch.resources.constData()); + } + + for (const auto &batch : srbD->csshaderresources.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, "CS SRV"); + if (count) { + context->CSSetShaderResources(batch.startBinding, count, batch.resources.constData()); + contextState.csHighestActiveSrvBinding = qMax(contextState.csHighestActiveSrvBinding, + int(batch.startBinding + count) - 1); + } } } - } - for (const auto &batch : srbD->csUAVs.batches) { - const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), - D3D11_1_UAV_SLOT_COUNT, "CS UAV"); - if (count) { - context->CSSetUnorderedAccessViews(batch.startBinding, - count, - batch.resources.constData(), - nullptr); - contextState.csHighestActiveUavBinding = qMax(contextState.csHighestActiveUavBinding, - int(batch.startBinding + count) - 1); + if (srbD->csUAVsPresent) { + for (const auto &batch : srbD->csUAVs.batches) { + const uint count = clampedResourceCount(batch.startBinding, batch.resources.count(), + D3D11_1_UAV_SLOT_COUNT, "CS UAV"); + if (count) { + context->CSSetUnorderedAccessViews(batch.startBinding, + count, + batch.resources.constData(), + nullptr); + contextState.csHighestActiveUavBinding = qMax(contextState.csHighestActiveUavBinding, + int(batch.startBinding + count) - 1); + } + } } } } diff --git a/src/gui/rhi/qrhid3d11_p_p.h b/src/gui/rhi/qrhid3d11_p_p.h index 7f5ec28571..6de899310c 100644 --- a/src/gui/rhi/qrhid3d11_p_p.h +++ b/src/gui/rhi/qrhid3d11_p_p.h @@ -240,6 +240,14 @@ struct QD3D11ShaderResourceBindings : public QRhiShaderResourceBindings }; QVarLengthArray<BoundResourceData, 8> boundResourceData; + bool vsubufsPresent = false; + bool fsubufsPresent = false; + bool csubufsPresent = false; + bool vssamplersPresent = false; + bool fssamplersPresent = false; + bool cssamplersPresent = false; + bool csUAVsPresent = false; + QRhiBatchedBindings<ID3D11Buffer *> vsubufs; QRhiBatchedBindings<UINT> vsubuforigbindings; QRhiBatchedBindings<UINT> vsubufoffsets; |