Skip to content

Commit

Permalink
Merge pull request #2429 from squidbus/vtx-stride-tess
Browse files Browse the repository at this point in the history
Fix dynamic vertex stride with tessellation.
  • Loading branch information
cdavis5e authored Jan 27, 2025
2 parents 2473ce6 + 4ea0b45 commit 6fa077f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
8 changes: 8 additions & 0 deletions MoltenVK/MoltenVK/Commands/MVKCommandBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ class MVKCommandEncoder : public MVKBaseDeviceObject {
void setComputeBytes(id<MTLComputeCommandEncoder> mtlEncoder, const void* bytes,
NSUInteger length, uint32_t mtlBuffIndex, bool descOverride = false);

/**
* Copy bytes into the Metal encoder at a Metal compute buffer index with dynamic stride,
* and optionally indicate that this binding might override a desriptor binding. If so,
* the descriptor binding will be marked dirty so that it will rebind before the next usage.
*/
void setComputeBytesWithStride(id<MTLComputeCommandEncoder> mtlEncoder, const void* bytes,
NSUInteger length, uint32_t mtlBuffIndex, uint32_t stride, bool descOverride = false);

/** Get a temporary MTLBuffer that will be returned to a pool after the command buffer is finished. */
const MVKMTLBufferAllocation* getTempMTLBuffer(NSUInteger length, bool isPrivate = false, bool isDedicated = false);

Expand Down
19 changes: 19 additions & 0 deletions MoltenVK/MoltenVK/Commands/MVKCommandBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,25 @@
}
}

void MVKCommandEncoder::setComputeBytesWithStride(id<MTLComputeCommandEncoder> mtlEncoder,
const void* bytes,
NSUInteger length,
uint32_t mtlBuffIndex,
uint32_t stride,
bool descOverride) {
auto& mtlFeats = getMetalFeatures();
if (mtlFeats.dynamicMTLBufferSize && length <= mtlFeats.dynamicMTLBufferSize) {
[mtlEncoder setBytes: bytes length: length attributeStride: stride atIndex: mtlBuffIndex];
} else {
const MVKMTLBufferAllocation* mtlBuffAlloc = copyToTempMTLBufferAllocation(bytes, length);
[mtlEncoder setBuffer: mtlBuffAlloc->_mtlBuffer offset: mtlBuffAlloc->_offset attributeStride: stride atIndex: mtlBuffIndex];
}

if (descOverride) {
_computeResourcesState.markBufferIndexOverridden(mtlBuffIndex);
}
}

// Return the MTLBuffer allocation to the pool once the command buffer is done with it
const MVKMTLBufferAllocation* MVKCommandEncoder::getTempMTLBuffer(NSUInteger length, bool isPrivate, bool isDedicated) {
MVKMTLBufferAllocation* mtlBuffAlloc = getCommandEncodingPool()->acquireMTLBufferAllocation(length, isPrivate, isDedicated);
Expand Down
53 changes: 38 additions & 15 deletions MoltenVK/MoltenVK/Commands/MVKCommandEncoderState.mm
Original file line number Diff line number Diff line change
Expand Up @@ -907,24 +907,47 @@ - (void)setDepthBoundsTestAMD:(BOOL)enable minDepth:(float)minDepth maxDepth:(fl
auto* pipeline = _cmdEncoder->getGraphicsPipeline();
bool fullImageViewSwizzle = pipeline->fullImageViewSwizzle() || _cmdEncoder->getMetalFeatures().nativeTextureSwizzle;
bool forTessellation = pipeline->isTessellationPipeline();
bool isDynamicVertexStride = pipeline->isDynamicState(VertexStride);
bool isDynamicVertexStride = pipeline->isDynamicState(VertexStride) && _cmdEncoder->getMetalFeatures().dynamicVertexStride;

if (stage == kMVKGraphicsStageVertex) {
encodeBindings(kMVKShaderStageVertex, "vertex", fullImageViewSwizzle,
[](MVKCommandEncoder* cmdEncoder, MVKMTLBufferBinding& b)->void {
if (b.isInline)
cmdEncoder->setComputeBytes(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
b.mtlBytes,
b.size,
b.index);
else if (b.justOffset)
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
setBufferOffset: b.offset
atIndex: b.index];
else
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl) setBuffer: b.mtlBuffer
offset: b.offset
atIndex: b.index];
[isDynamicVertexStride](MVKCommandEncoder* cmdEncoder, MVKMTLBufferBinding& b)->void {
if (isDynamicVertexStride) {
#if MVK_XCODE_15
if (b.isInline)
cmdEncoder->setComputeBytesWithStride(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
b.mtlBytes,
b.size,
b.index,
b.stride);
else if (b.justOffset)
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
setBufferOffset: b.offset
attributeStride: b.stride
atIndex: b.index];
else
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
setBuffer: b.mtlBuffer
offset: b.offset
attributeStride: b.stride
atIndex: b.index];
#endif
} else {
if (b.isInline)
cmdEncoder->setComputeBytes(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
b.mtlBytes,
b.size,
b.index);
else if (b.justOffset)
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
setBufferOffset: b.offset
atIndex: b.index];
else
[cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl)
setBuffer: b.mtlBuffer
offset: b.offset
atIndex: b.index];
}
},
[](MVKCommandEncoder* cmdEncoder, MVKMTLBufferBinding& b, MVKArrayRef<const uint32_t> s)->void {
cmdEncoder->setComputeBytes(cmdEncoder->getMTLComputeEncoder(kMVKCommandUseTessellationVertexTessCtl),
Expand Down

0 comments on commit 6fa077f

Please sign in to comment.