Skip to content

Commit 4ea0b45

Browse files
committed
Fix dynamic vertex stride with tessellation.
1 parent 2473ce6 commit 4ea0b45

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

MoltenVK/MoltenVK/Commands/MVKCommandBuffer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,14 @@ class MVKCommandEncoder : public MVKBaseDeviceObject {
393393
void setComputeBytes(id<MTLComputeCommandEncoder> mtlEncoder, const void* bytes,
394394
NSUInteger length, uint32_t mtlBuffIndex, bool descOverride = false);
395395

396+
/**
397+
* Copy bytes into the Metal encoder at a Metal compute buffer index with dynamic stride,
398+
* and optionally indicate that this binding might override a desriptor binding. If so,
399+
* the descriptor binding will be marked dirty so that it will rebind before the next usage.
400+
*/
401+
void setComputeBytesWithStride(id<MTLComputeCommandEncoder> mtlEncoder, const void* bytes,
402+
NSUInteger length, uint32_t mtlBuffIndex, uint32_t stride, bool descOverride = false);
403+
396404
/** Get a temporary MTLBuffer that will be returned to a pool after the command buffer is finished. */
397405
const MVKMTLBufferAllocation* getTempMTLBuffer(NSUInteger length, bool isPrivate = false, bool isDedicated = false);
398406

MoltenVK/MoltenVK/Commands/MVKCommandBuffer.mm

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,25 @@
975975
}
976976
}
977977

978+
void MVKCommandEncoder::setComputeBytesWithStride(id<MTLComputeCommandEncoder> mtlEncoder,
979+
const void* bytes,
980+
NSUInteger length,
981+
uint32_t mtlBuffIndex,
982+
uint32_t stride,
983+
bool descOverride) {
984+
auto& mtlFeats = getMetalFeatures();
985+
if (mtlFeats.dynamicMTLBufferSize && length <= mtlFeats.dynamicMTLBufferSize) {
986+
[mtlEncoder setBytes: bytes length: length attributeStride: stride atIndex: mtlBuffIndex];
987+
} else {
988+
const MVKMTLBufferAllocation* mtlBuffAlloc = copyToTempMTLBufferAllocation(bytes, length);
989+
[mtlEncoder setBuffer: mtlBuffAlloc->_mtlBuffer offset: mtlBuffAlloc->_offset attributeStride: stride atIndex: mtlBuffIndex];
990+
}
991+
992+
if (descOverride) {
993+
_computeResourcesState.markBufferIndexOverridden(mtlBuffIndex);
994+
}
995+
}
996+
978997
// Return the MTLBuffer allocation to the pool once the command buffer is done with it
979998
const MVKMTLBufferAllocation* MVKCommandEncoder::getTempMTLBuffer(NSUInteger length, bool isPrivate, bool isDedicated) {
980999
MVKMTLBufferAllocation* mtlBuffAlloc = getCommandEncodingPool()->acquireMTLBufferAllocation(length, isPrivate, isDedicated);

MoltenVK/MoltenVK/Commands/MVKCommandEncoderState.mm

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -907,24 +907,47 @@ - (void)setDepthBoundsTestAMD:(BOOL)enable minDepth:(float)minDepth maxDepth:(fl
907907
auto* pipeline = _cmdEncoder->getGraphicsPipeline();
908908
bool fullImageViewSwizzle = pipeline->fullImageViewSwizzle() || _cmdEncoder->getMetalFeatures().nativeTextureSwizzle;
909909
bool forTessellation = pipeline->isTessellationPipeline();
910-
bool isDynamicVertexStride = pipeline->isDynamicState(VertexStride);
910+
bool isDynamicVertexStride = pipeline->isDynamicState(VertexStride) && _cmdEncoder->getMetalFeatures().dynamicVertexStride;
911911

912912
if (stage == kMVKGraphicsStageVertex) {
913913
encodeBindings(kMVKShaderStageVertex, "vertex", fullImageViewSwizzle,
914-
[](MVKCommandEncoder* cmdEncoder, MVKMTLBufferBinding& b)->void {
915-
if (b.isInline)
916-
cmdEncoder->setComputeBytes(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
917-
b.mtlBytes,
918-
b.size,
919-
b.index);
920-
else if (b.justOffset)
921-
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
922-
setBufferOffset: b.offset
923-
atIndex: b.index];
924-
else
925-
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl) setBuffer: b.mtlBuffer
926-
offset: b.offset
927-
atIndex: b.index];
914+
[isDynamicVertexStride](MVKCommandEncoder* cmdEncoder, MVKMTLBufferBinding& b)->void {
915+
if (isDynamicVertexStride) {
916+
#if MVK_XCODE_15
917+
if (b.isInline)
918+
cmdEncoder->setComputeBytesWithStride(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
919+
b.mtlBytes,
920+
b.size,
921+
b.index,
922+
b.stride);
923+
else if (b.justOffset)
924+
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
925+
setBufferOffset: b.offset
926+
attributeStride: b.stride
927+
atIndex: b.index];
928+
else
929+
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
930+
setBuffer: b.mtlBuffer
931+
offset: b.offset
932+
attributeStride: b.stride
933+
atIndex: b.index];
934+
#endif
935+
} else {
936+
if (b.isInline)
937+
cmdEncoder->setComputeBytes(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
938+
b.mtlBytes,
939+
b.size,
940+
b.index);
941+
else if (b.justOffset)
942+
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
943+
setBufferOffset: b.offset
944+
atIndex: b.index];
945+
else
946+
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
947+
setBuffer: b.mtlBuffer
948+
offset: b.offset
949+
atIndex: b.index];
950+
}
928951
},
929952
[](MVKCommandEncoder* cmdEncoder, MVKMTLBufferBinding& b, MVKArrayRef<const uint32_t> s)->void {
930953
cmdEncoder->setComputeBytes(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),

0 commit comments

Comments
 (0)