Skip to content

Commit

Permalink
MVKCmdDraw: Fix indirect index for triangle fan topology
Browse files Browse the repository at this point in the history
When we call vkCmdDraw with vertexCount=N and firstVertex=X, we only
need an array with N element, X, X+1, ..., X+N-1, to generate the
indirect indices. However, the current logic generates all the way
from 0 to X+N-1 (which is X+N elements) while only reserving N spaces.
This causes issue when the unreserved part is overwritten, for example,
when issuing consecutive draw calls with triangle fan primitives.
  • Loading branch information
dboyan committed Jan 13, 2025
1 parent 9f0b616 commit f5b8df3
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions MoltenVK/MoltenVK/Commands/MVKCmdDraw.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,20 @@
auto* indirectIdxBuff = cmdEncoder->getTempMTLBuffer(indirectIdxBuffStride);
auto* pIndArg = (MTLDrawIndexedPrimitivesIndirectArguments*)indirectIdxBuff->getContents();
pIndArg->indexCount = _vertexCount;
pIndArg->indexStart = _firstVertex;
// let the indirect index point to the beginning of vertex index buffer below
pIndArg->indexStart = 0;
pIndArg->baseVertex = 0;
pIndArg->instanceCount = _instanceCount;
pIndArg->baseInstance = _firstInstance;

// Create an index buffer populated with synthetic indexes.
// Start populating indexes below _firstVertex so that indexes align with their corresponding vertexes
// Start populating indexes directly from the beginning and align with corresponding vertexes by adding _firstVertex
MTLIndexType mtlIdxType = MTLIndexTypeUInt32;
auto* vtxIdxBuff = cmdEncoder->getTempMTLBuffer(mvkMTLIndexTypeSizeInBytes(mtlIdxType) * _vertexCount);
auto* pIdxBuff = (uint32_t*)vtxIdxBuff->getContents();
uint32_t idxCnt = _firstVertex + _vertexCount;
for (uint32_t idx = 0; idx < idxCnt; idx++) {
pIdxBuff[idx] = idx;

for (uint32_t idx = 0; idx < _vertexCount; idx++) {
pIdxBuff[idx] = _firstVertex + idx;
}

MVKIndexMTLBufferBinding ibb;
Expand Down

0 comments on commit f5b8df3

Please sign in to comment.