Skip to content

Commit

Permalink
Merge pull request #2243 from billhollings/fix-shdr-native-atomics
Browse files Browse the repository at this point in the history
Fix shader conversion failure when using native texture atomics.
  • Loading branch information
billhollings authored May 30, 2024
2 parents fc52da9 + f36e5a8 commit 4ef99e5
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
1 change: 1 addition & 0 deletions Docs/Whats_New.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ MoltenVK 1.2.10

Released TBD

- Fix shader conversion failure when using native texture atomics.
- MSL shader conversion, only pass resource bindings that apply to current shader stage.
- Update documentation for minimum runtime OS requirements to indicate _macOS 10.15_, _iOS 13_, or _tvOS 13_.
- Update to latest SPIRV-Cross:
Expand Down
2 changes: 1 addition & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
if ( mvkIsAnyFlagEnabled(_buffer->getUsage(), VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT) ) {
usage |= MTLTextureUsageShaderWrite;
#if MVK_XCODE_15
if (getPhysicalDevice()->useNativeTextureAtomics() && (_mtlPixelFormat == MTLPixelFormatR32Sint || _mtlPixelFormat == MTLPixelFormatR32Uint || _mtlPixelFormat == MTLPixelFormatRG32Uint))
if (getMetalFeatures().nativeTextureAtomics && (_mtlPixelFormat == MTLPixelFormatR32Sint || _mtlPixelFormat == MTLPixelFormatR32Uint || _mtlPixelFormat == MTLPixelFormatRG32Uint))
usage |= MTLTextureUsageShaderAtomic;
#endif
}
Expand Down
3 changes: 2 additions & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
uint32_t bindingIndex,
uint32_t count,
VkDescriptorType descType,
MVKSampler* immutableSampler);
MVKSampler* immutableSampler,
bool usingNativeTextureAtomics);


#pragma mark -
Expand Down
28 changes: 16 additions & 12 deletions MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
uint32_t bindingIndex,
uint32_t count,
VkDescriptorType descType,
MVKSampler* immutableSampler) {
MVKSampler* immutableSampler,
bool usingNativeTextureAtomics) {

#define addResourceBinding(spvRezType) \
do { \
Expand Down Expand Up @@ -167,7 +168,9 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
addResourceBinding(Image);
addResourceBinding(Void);
if ( !usingNativeTextureAtomics ) {
addResourceBinding(Void);
}
break;

case VK_DESCRIPTOR_TYPE_SAMPLER:
Expand Down Expand Up @@ -323,7 +326,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
if (_applyToStage[i]) {
tb.index = mtlIdxs.stages[i].textureIndex + rezIdx + planeIndex;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindTexture, pipelineBindPoint, i, tb);
if (_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE && !getPhysicalDevice()->useNativeTextureAtomics()) {
if (_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE && !getMetalFeatures().nativeTextureAtomics) {
bb.index = mtlIdxs.stages[i].bufferIndex + rezIdx;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindBuffer, pipelineBindPoint, i, bb);
}
Expand All @@ -348,7 +351,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
if (_applyToStage[i]) {
tb.index = mtlIdxs.stages[i].textureIndex + rezIdx;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindTexture, pipelineBindPoint, i, tb);
if (_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER && !getPhysicalDevice()->useNativeTextureAtomics()) {
if (_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER && !getMetalFeatures().nativeTextureAtomics) {
bb.index = mtlIdxs.stages[i].bufferIndex + rezIdx;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindBuffer, pipelineBindPoint, i, bb);
}
Expand Down Expand Up @@ -440,7 +443,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s

case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadWrite);
if (!getPhysicalDevice()->useNativeTextureAtomics()) { // Needed for emulated atomic operations
if (!getMetalFeatures().nativeTextureAtomics) { // Needed for emulated atomic operations
addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite);
}
break;
Expand All @@ -451,7 +454,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s

case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadWrite);
if (!getPhysicalDevice()->useNativeTextureAtomics()) { // Needed for emulated atomic operations
if (!getMetalFeatures().nativeTextureAtomics) { // Needed for emulated atomic operations
addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite);
}
break;
Expand Down Expand Up @@ -507,7 +510,8 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
_info.binding,
descCnt,
getDescriptorType(),
mvkSamp);
mvkSamp,
getMetalFeatures().nativeTextureAtomics);
}
}
}
Expand Down Expand Up @@ -669,7 +673,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
setResourceIndexOffset(textureIndex);
if (!getPhysicalDevice()->useNativeTextureAtomics()) setResourceIndexOffset(bufferIndex);
if (!getMetalFeatures().nativeTextureAtomics) setResourceIndexOffset(bufferIndex);

if (pBinding->descriptorCount > 1 && !mtlFeats.arrayOfTextures) {
_layout->setConfigurationResult(reportError(VK_ERROR_FEATURE_NOT_PRESENT, "Device %s does not support arrays of textures.", _device->getName()));
Expand Down Expand Up @@ -932,7 +936,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
if (stages[i]) {
tb.index = mtlIndexes.stages[i].textureIndex + elementIndex + planeIndex;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindTexture, pipelineBindPoint, i, tb);
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE && !cmdEncoder->getPhysicalDevice()->useNativeTextureAtomics()) {
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE && !cmdEncoder->getMetalFeatures().nativeTextureAtomics) {
bb.index = mtlIndexes.stages[i].bufferIndex + elementIndex + planeIndex;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindBuffer, pipelineBindPoint, i, bb);
}
Expand Down Expand Up @@ -963,7 +967,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
if (encodeUsage) {
rezEncState->encodeResourceUsage(stage, mtlTexture, getMTLResourceUsage(), mvkDSLBind->getMTLRenderStages());
}
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE && !mvkDSLBind->getPhysicalDevice()->useNativeTextureAtomics()) {
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE && !mvkDSLBind->getMetalFeatures().nativeTextureAtomics) {
id<MTLTexture> mtlTex = mtlTexture.parentTexture ? mtlTexture.parentTexture : mtlTexture;
id<MTLBuffer> mtlBuff = mtlTex.buffer;
if (mtlBuff) {
Expand Down Expand Up @@ -1231,7 +1235,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
if (stages[i]) {
tb.index = mtlIndexes.stages[i].textureIndex + elementIndex;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindTexture, pipelineBindPoint, i, tb);
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER && !cmdEncoder->getPhysicalDevice()->useNativeTextureAtomics()) {
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER && !cmdEncoder->getMetalFeatures().nativeTextureAtomics) {
bb.index = mtlIndexes.stages[i].bufferIndex + elementIndex;
BIND_GRAPHICS_OR_COMPUTE(cmdEncoder, bindBuffer, pipelineBindPoint, i, bb);
}
Expand All @@ -1256,7 +1260,7 @@ void mvkPopulateShaderConversionConfig(mvk::SPIRVToMSLConversionConfiguration& s
rezEncState->encodeResourceUsage(stage, mtlTexture, getMTLResourceUsage(), mvkDSLBind->getMTLRenderStages());
}

if (descType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER && !mvkDSLBind->getPhysicalDevice()->useNativeTextureAtomics()) {
if (descType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER && !mvkDSLBind->getMetalFeatures().nativeTextureAtomics) {
id<MTLBuffer> mtlBuff = mtlTexture.buffer;
if (mtlBuff) {
if (encodeToArgBuffer) {
Expand Down
2 changes: 1 addition & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKDescriptorSet.mm
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
mtlTexCnt += poolSize.descriptorCount;
if (!getPhysicalDevice()->useNativeTextureAtomics())
if (!getMetalFeatures().nativeTextureAtomics)
mtlBuffCnt += poolSize.descriptorCount;
break;

Expand Down
3 changes: 0 additions & 3 deletions MoltenVK/MoltenVK/GPUObjects/MVKDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,6 @@ class MVKPhysicalDevice : public MVKDispatchableVulkanAPIObject {
return _metalFeatures.argumentBuffers && getMVKConfig().useMetalArgumentBuffers != MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS_NEVER;
};

/** Returns whether native texture atomics are supported and should be used. */
bool useNativeTextureAtomics() { return _metalFeatures.nativeTextureAtomics; }

/** Returns the MTLStorageMode that matches the Vulkan memory property flags. */
MTLStorageMode getMTLStorageModeFromVkMemoryPropertyFlags(VkMemoryPropertyFlags vkFlags);

Expand Down
2 changes: 1 addition & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3764,7 +3764,7 @@ static uint32_t mvkGetEntryProperty(io_registry_entry_t entry, CFStringRef prope
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
mtlTexCnt += pBind->descriptorCount;
if (_physicalDevice->useNativeTextureAtomics()) {
if (_physicalDevice->_metalFeatures.nativeTextureAtomics) {
mtlBuffCnt += pBind->descriptorCount;
}
maxVarDescCount = min(mtlFeats.maxPerStageTextureCount - mtlTexCnt,
Expand Down
4 changes: 2 additions & 2 deletions MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ static MTLRegion getMTLRegion(const ImgRgn& imgRgn) {

MTLTextureUsage mtlUsage = pixFmts->getMTLTextureUsage(getCombinedUsage(), mtlPixFmt, _samples,
_isLinear || _isLinearForAtomics, needsReinterpretation, _hasExtendedUsage,
_shouldSupportAtomics && getPhysicalDevice()->useNativeTextureAtomics());
_shouldSupportAtomics && getMetalFeatures().nativeTextureAtomics);

// Metal before 3.0 doesn't support 3D compressed textures, so we'll
// decompress the texture ourselves, and we need to be able to write to it.
Expand Down Expand Up @@ -1162,7 +1162,7 @@ static MTLRegion getMTLRegion(const ImgRgn& imgRgn) {
((_vkFormat == VK_FORMAT_R32_UINT || _vkFormat == VK_FORMAT_R32_SINT) ||
(_hasMutableFormat && pixFmts->getViewClass(_vkFormat) == MVKMTLViewClass::Color32 && (getIsValidViewFormat(VK_FORMAT_R32_UINT) || getIsValidViewFormat(VK_FORMAT_R32_SINT))));

_isLinearForAtomics = _shouldSupportAtomics && !getPhysicalDevice()->useNativeTextureAtomics() && _arrayLayers == 1 && getImageType() == VK_IMAGE_TYPE_2D;
_isLinearForAtomics = _shouldSupportAtomics && !getMetalFeatures().nativeTextureAtomics && _arrayLayers == 1 && getImageType() == VK_IMAGE_TYPE_2D;

_is3DCompressed = (getImageType() == VK_IMAGE_TYPE_3D) && (pixFmts->getFormatType(pCreateInfo->format) == kMVKFormatCompressed) && !mtlFeats.native3DCompressedTextures;
_isDepthStencilAttachment = (mvkAreAllFlagsEnabled(pCreateInfo->usage, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ||
Expand Down
3 changes: 2 additions & 1 deletion MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
kPushConstBinding,
1,
VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT,
nullptr);
nullptr,
getMetalFeatures().nativeTextureAtomics);
}
}

Expand Down

0 comments on commit 4ef99e5

Please sign in to comment.