Commit b5b55be8 authored by Laszlo Agocs's avatar Laszlo Agocs
Browse files

vk: minimize vertex input changes

parent 97eb0bc5
......@@ -368,7 +368,7 @@ struct QD3D11CommandBuffer : public QRhiCommandBuffer
quint32 currentIndexOffset;
DXGI_FORMAT currentIndexFormat;
ID3D11Buffer *currentVertexBuffers[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT];
UINT currentVertexOffsets[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT];
quint32 currentVertexOffsets[D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT];
QVector<QByteArray> dataRetainPool;
QVector<QImage> imageRetainPool;
......
......@@ -2938,24 +2938,37 @@ void QRhiVulkan::setVertexInput(QRhiCommandBuffer *cb, int startBinding, const Q
QRhiBuffer *indexBuf, quint32 indexOffset, QRhiCommandBuffer::IndexFormat indexFormat)
{
Q_ASSERT(inPass);
QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb);
QVarLengthArray<VkBuffer, 4> bufs;
QVarLengthArray<VkDeviceSize, 4> ofs;
bool needsBindVBuf = false;
for (int i = 0, ie = bindings.count(); i != ie; ++i) {
QRhiBuffer *buf = bindings[i].first;
QVkBuffer *bufD = QRHI_RES(QVkBuffer, buf);
const int inputSlot = startBinding + i;
QVkBuffer *bufD = QRHI_RES(QVkBuffer, bindings[i].first);
Q_ASSERT(bufD->m_usage.testFlag(QRhiBuffer::VertexBuffer));
bufD->lastActiveFrameSlot = currentFrameSlot;
if (bufD->m_type == QRhiBuffer::Dynamic)
executeBufferHostWritesForCurrentFrame(bufD);
const int idx = bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0;
bufs.append(bufD->buffers[idx]);
ofs.append(bindings[i].second);
const VkBuffer vkvertexbuf = bufD->buffers[bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0];
if (cbD->currentVertexBuffers[inputSlot] != vkvertexbuf
|| cbD->currentVertexOffsets[inputSlot] != bindings[i].second)
{
needsBindVBuf = true;
cbD->currentVertexBuffers[inputSlot] = vkvertexbuf;
cbD->currentVertexOffsets[inputSlot] = bindings[i].second;
}
}
QVkCommandBuffer *cbD = QRHI_RES(QVkCommandBuffer, cb);
if (!bufs.isEmpty())
if (needsBindVBuf) {
QVarLengthArray<VkBuffer, 4> bufs;
QVarLengthArray<VkDeviceSize, 4> ofs;
for (int i = 0, ie = bindings.count(); i != ie; ++i) {
QVkBuffer *bufD = QRHI_RES(QVkBuffer, bindings[i].first);
bufs.append(bufD->buffers[bufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0]);
ofs.append(bindings[i].second);
}
df->vkCmdBindVertexBuffers(cbD->cb, startBinding, bufs.count(), bufs.constData(), ofs.constData());
}
if (indexBuf) {
QVkBuffer *ibufD = QRHI_RES(QVkBuffer, indexBuf);
......@@ -2964,10 +2977,20 @@ void QRhiVulkan::setVertexInput(QRhiCommandBuffer *cb, int startBinding, const Q
if (ibufD->m_type == QRhiBuffer::Dynamic)
executeBufferHostWritesForCurrentFrame(ibufD);
const int idx = ibufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0;
const VkBuffer vkindexbuf = ibufD->buffers[ibufD->m_type == QRhiBuffer::Dynamic ? currentFrameSlot : 0];
const VkIndexType type = indexFormat == QRhiCommandBuffer::IndexUInt16 ? VK_INDEX_TYPE_UINT16
: VK_INDEX_TYPE_UINT32;
df->vkCmdBindIndexBuffer(cbD->cb, ibufD->buffers[idx], indexOffset, type);
if (cbD->currentIndexBuffer != vkindexbuf
|| cbD->currentIndexOffset != indexOffset
|| cbD->currentIndexFormat != type)
{
cbD->currentIndexBuffer = vkindexbuf;
cbD->currentIndexOffset = indexOffset;
cbD->currentIndexFormat = type;
df->vkCmdBindIndexBuffer(cbD->cb, vkindexbuf, indexOffset, type);
}
}
}
......
......@@ -254,13 +254,25 @@ struct QVkCommandBuffer : public QRhiCommandBuffer
currentSrb = nullptr;
currentSrbGeneration = 0;
currentDescSetSlot = -1;
currentIndexBuffer = VK_NULL_HANDLE;
currentIndexOffset = 0;
currentIndexFormat = VK_INDEX_TYPE_UINT16;
memset(currentVertexBuffers, 0, sizeof(currentVertexBuffers));
memset(currentVertexOffsets, 0, sizeof(currentVertexOffsets));
}
QRhiRenderTarget *currentTarget;
QRhiGraphicsPipeline *currentPipeline;
uint currentPipelineGeneration;
QRhiShaderResourceBindings *currentSrb;
uint currentSrbGeneration;
int currentDescSetSlot;
VkBuffer currentIndexBuffer;
quint32 currentIndexOffset;
VkIndexType currentIndexFormat;
static const int VERTEX_INPUT_RESOURCE_SLOT_COUNT = 32;
VkBuffer currentVertexBuffers[VERTEX_INPUT_RESOURCE_SLOT_COUNT];
quint32 currentVertexOffsets[VERTEX_INPUT_RESOURCE_SLOT_COUNT];
friend class QRhiVulkan;
};
......
......@@ -45,7 +45,7 @@ dxc for d3d as an alternative to fxc?
hlsl -> dxc -> spirv -> spirv-cross hmmm...
+++ done
d3d, mtl: minimize vertex input changes
vk, d3d, mtl: minimize vertex input changes
mtl: cbuffers, textures, samplers set should be batched too
mtl: reduce set*
mtl: report readback temp buf
......
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