diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.h b/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.h index bb010902..e401e898 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.h @@ -166,15 +166,11 @@ protected: friend class MVKInlineUniformBlockDescriptor; void initMetalResourceIndexOffsets(const VkDescriptorSetLayoutBinding* pBinding, uint32_t stage); - void addMTLArgumentDescriptors(NSMutableArray* args, - mvk::SPIRVToMSLConversionConfiguration& shaderConfig, - uint32_t descSetIdx); + void addMTLArgumentDescriptors(NSMutableArray* args); void addMTLArgumentDescriptor(NSMutableArray* args, uint32_t argIndex, MTLDataType dataType, - MTLArgumentAccess access, - mvk::SPIRVToMSLConversionConfiguration& shaderConfig, - uint32_t descSetIdx); + MTLArgumentAccess access); bool isUsingMetalArgumentBuffer(); void populateShaderConverterContext(mvk::SPIRVToMSLConversionConfiguration& context, MVKShaderResourceBinding& dslMTLRezIdxOffsets, diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.mm index 29123a6d..a7f53a8c 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDescriptor.mm @@ -444,48 +444,46 @@ void MVKDescriptorSetLayoutBinding::push(MVKCommandEncoder* cmdEncoder, bool MVKDescriptorSetLayoutBinding::isUsingMetalArgumentBuffer() { return _layout->isUsingMetalArgumentBuffer(); }; // Adds MTLArgumentDescriptors to the array, and updates resource indexes consumed. -void MVKDescriptorSetLayoutBinding::addMTLArgumentDescriptors(NSMutableArray* args, - mvk::SPIRVToMSLConversionConfiguration& shaderConfig, - uint32_t descSetIdx) { +void MVKDescriptorSetLayoutBinding::addMTLArgumentDescriptors(NSMutableArray* args) { switch (getDescriptorType()) { case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadOnly, shaderConfig, descSetIdx); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadOnly); break; case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite, shaderConfig, descSetIdx); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite); break; case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadOnly, shaderConfig, descSetIdx); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadOnly); break; case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadWrite, shaderConfig, descSetIdx); - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite, shaderConfig, descSetIdx); // Needed for atomic operations + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadWrite); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite); // Needed for atomic operations break; case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadOnly, shaderConfig, descSetIdx); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadOnly); break; case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadWrite, shaderConfig, descSetIdx); - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite, shaderConfig, descSetIdx); // Needed for atomic operations + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadWrite); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().bufferIndex, MTLDataTypePointer, MTLArgumentAccessReadWrite); // Needed for atomic operations break; case VK_DESCRIPTOR_TYPE_SAMPLER: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().samplerIndex, MTLDataTypeSampler, MTLArgumentAccessReadOnly, shaderConfig, descSetIdx); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().samplerIndex, MTLDataTypeSampler, MTLArgumentAccessReadOnly); break; case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadOnly, shaderConfig, descSetIdx); - addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().samplerIndex, MTLDataTypeSampler, MTLArgumentAccessReadOnly, shaderConfig, descSetIdx); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().textureIndex, MTLDataTypeTexture, MTLArgumentAccessReadOnly); + addMTLArgumentDescriptor(args, getMetalResourceIndexOffsets().samplerIndex, MTLDataTypeSampler, MTLArgumentAccessReadOnly); break; default: @@ -493,13 +491,10 @@ void MVKDescriptorSetLayoutBinding::addMTLArgumentDescriptors(NSMutableArray* args, uint32_t argIndex, MTLDataType dataType, - MTLArgumentAccess access, - mvk::SPIRVToMSLConversionConfiguration& shaderConfig, - uint32_t descSetIdx) { + MTLArgumentAccess access) { uint32_t descCnt = getDescriptorCount(); if (descCnt == 0) { return; } @@ -508,7 +503,7 @@ void MVKDescriptorSetLayoutBinding::addMTLArgumentDescriptor(NSMutableArraygetDescriptorIndex(elementIndex); } MVKDescriptorSetLayoutBinding* getBinding(uint32_t binding) { return &_bindings[_bindingToIndex[binding]]; } const VkDescriptorBindingFlags* getBindingFlags(const VkDescriptorSetLayoutCreateInfo* pCreateInfo); - void initForMetalArgumentBufferUse(); - id newMTLArgumentEncoder(mvk::SPIRVToMSLConversionConfiguration& shaderConfig, uint32_t descSetIdx); + void initMTLArgumentEncoder(); MVKSmallVector _bindings; std::unordered_map _bindingToIndex; diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDescriptorSet.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDescriptorSet.mm index 9a9fb103..8afe563b 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDescriptorSet.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDescriptorSet.mm @@ -183,19 +183,6 @@ void MVKDescriptorSetLayout::populateShaderConverterContext(mvk::SPIRVToMSLConve } } -id MVKDescriptorSetLayout::newMTLArgumentEncoder(mvk::SPIRVToMSLConversionConfiguration& shaderConfig, - uint32_t descSetIdx) { - if ( !(isUsingMetalArgumentBuffer() && isUsingDescriptorSetMetalArgumentBuffers()) ) { return nil; } - - @autoreleasepool { - NSMutableArray* args = [NSMutableArray arrayWithCapacity: _bindings.size()]; - for (auto& dslBind : _bindings) { - dslBind.addMTLArgumentDescriptors(args, shaderConfig, descSetIdx); - } - return (args.count) ? [getMTLDevice() newArgumentEncoderWithArguments: args] : nil; - } -} - MVKDescriptorSetLayout::MVKDescriptorSetLayout(MVKDevice* device, const VkDescriptorSetLayoutCreateInfo* pCreateInfo) : MVKVulkanAPIDeviceObject(device) { @@ -229,7 +216,7 @@ MVKDescriptorSetLayout::MVKDescriptorSetLayout(MVKDevice* device, _descriptorCount += _bindings.back().getDescriptorCount(); } - initForMetalArgumentBufferUse(); + initMTLArgumentEncoder(); } // Find and return an array of binding flags from the pNext chain of pCreateInfo, @@ -248,9 +235,14 @@ const VkDescriptorBindingFlags* MVKDescriptorSetLayout::getBindingFlags(const Vk return nullptr; } -void MVKDescriptorSetLayout::initForMetalArgumentBufferUse() { - SPIRVToMSLConversionConfiguration shaderConfig; - _mtlArgumentEncoder.init(newMTLArgumentEncoder(shaderConfig, 0)); +void MVKDescriptorSetLayout::initMTLArgumentEncoder() { + if (isUsingDescriptorSetMetalArgumentBuffers() && isUsingMetalArgumentBuffer()) { + @autoreleasepool { + NSMutableArray* args = [NSMutableArray arrayWithCapacity: _bindings.size()]; + for (auto& dslBind : _bindings) { dslBind.addMTLArgumentDescriptors(args); } + _mtlArgumentEncoder.init(args.count ? [getMTLDevice() newArgumentEncoderWithArguments: args] : nil); + } + } } diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm index 075cfbcb..c6a2ab3c 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm @@ -2202,7 +2202,6 @@ namespace mvk { void serialize(Archive & archive, MSLResourceBinding& rb) { archive(rb.resourceBinding, rb.constExprSampler, - rb.outMTLTextureType, rb.requiresConstExprSampler, rb.outIsUsedByShader); } diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVReflection.h b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVReflection.h index 08365780..915ef522 100644 --- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVReflection.h +++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVReflection.h @@ -26,23 +26,6 @@ #include #include -#ifdef __OBJC__ -#import -#else -enum MTLTextureType { - MTLTextureType1D, - MTLTextureType1DArray, - MTLTextureType2D, - MTLTextureType2DArray, - MTLTextureType2DMultisample, - MTLTextureType2DMultisampleArray, - MTLTextureType3D, - MTLTextureTypeCube, - MTLTextureTypeCubeArray, - MTLTextureTypeTextureBuffer -}; -#endif - namespace mvk { @@ -297,51 +280,5 @@ namespace mvk { } } - /** Given the compiler, returns the MTLTextureType of the descriptor set binding. */ - template - MTLTextureType getMTLTextureType(C* compiler, uint32_t desc_set, uint32_t binding) { - for (auto varID : compiler->get_active_interface_variables()) { - if (compiler->has_decoration(varID, spv::DecorationDescriptorSet) && - compiler->get_decoration(varID, spv::DecorationDescriptorSet) == desc_set && - compiler->has_decoration(varID, spv::DecorationBinding) && - compiler->get_decoration(varID, spv::DecorationBinding) == binding) { - - auto& mslOpts = compiler->get_msl_options(); - auto& imgType = compiler->get_type_from_variable(varID).image; - bool isArray = imgType.arrayed; - switch (imgType.dim) { - case spv::DimBuffer: - return mslOpts.texture_buffer_native ? MTLTextureTypeTextureBuffer : MTLTextureType2D; - - case spv::Dim1D: - return (mslOpts.texture_1D_as_2D - ? (isArray ? MTLTextureType2DArray : MTLTextureType2D) - : (isArray ? MTLTextureType1DArray : MTLTextureType1D)); - - case spv::Dim2D: - case spv::DimSubpassData: -#if MVK_MACOS_OR_IOS - if (isArray && imgType.ms) { return MTLTextureType2DMultisampleArray; } -#endif - return (isArray - ? MTLTextureType2DArray - : (imgType.ms ? MTLTextureType2DMultisample : MTLTextureType2D)); - - case spv::Dim3D: - return MTLTextureType3D; - - case spv::DimCube: - return (mslOpts.emulate_cube_array - ? (isArray ? MTLTextureType2DArray : MTLTextureType2D) - : (isArray ? MTLTextureTypeCubeArray : MTLTextureTypeCube)); - - default: - return MTLTextureType2D; - } - } - } - return MTLTextureType2D; - } - } #endif diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h index 10792848..0def16d9 100644 --- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h +++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.h @@ -96,13 +96,10 @@ namespace mvk { * hardcoded into the MSL as a constexpr type, instead of passed in as a runtime-bound variable. * The content of that constexpr sampler is defined in the constExprSampler parameter. * - * The outIsUsedByShader and outMTLTextureType values are set by the shader converter - * based on the content of the SPIR-V (and resulting MSL), and provide feedback to the - * pipeline about shader content. The outIsUsedByShader value is set to true if the shader - * makes use of this resource binding. This allows a pipeline to be optimized, and for two - * shader conversion configurations to be compared only against the resource bindings that - * are actually used by the shader. The outMTLTextureType value provides feedback to the - * pipeline regarding the texture type expected by the shader. + * The outIsUsedByShader value is set by the shader converter based on the content of the SPIR-V + * (and resulting MSL), and is set to true if the shader makes use of this resource binding. + * This allows a pipeline to be optimized, and for two shader conversion configurations to + * be compared only against the resource bindings that are actually used by the shader. * * THIS STRUCT IS STREAMED OUT AS PART OF THE PIEPLINE CACHE. * CHANGES TO THIS STRUCT SHOULD BE CAPTURED IN THE STREAMING LOGIC OF THE PIPELINE CACHE. @@ -110,13 +107,12 @@ namespace mvk { typedef struct MSLResourceBinding { SPIRV_CROSS_NAMESPACE::MSLResourceBinding resourceBinding; SPIRV_CROSS_NAMESPACE::MSLConstexprSampler constExprSampler; - MTLTextureType outMTLTextureType = MTLTextureType2D; bool requiresConstExprSampler = false; bool outIsUsedByShader = false; /** * Returns whether the specified resource binding match this one. - * It does if all corresponding elements except outMTLTextureType and outIsUsedByShader are equal. + * It does if all corresponding elements except outIsUsedByShader are equal. */ bool matches(const MSLResourceBinding& other) const; @@ -164,9 +160,6 @@ namespace mvk { /** Returns whether the vertex buffer at the specified Vulkan binding is used by the shader. */ bool isVertexBufferUsed(uint32_t binding) const { return countShaderInputsAt(binding) > 0; } - /** Returns the MTLTextureType of the image resource at the descriptor set and binding. */ - MTLTextureType getMTLTextureType(uint32_t descSet, uint32_t binding) const; - /** Marks all input variables and resources as being used by the shader. */ void markAllInputsAndResourcesUsed(); diff --git a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm index 2b76630e..bfe00712 100644 --- a/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm +++ b/MoltenVKShaderConverter/MoltenVKShaderConverter/SPIRVToMSLConverter.mm @@ -181,16 +181,6 @@ MVK_PUBLIC_SYMBOL uint32_t SPIRVToMSLConversionConfiguration::countShaderInputsA return siCnt; } -MVK_PUBLIC_SYMBOL MTLTextureType SPIRVToMSLConversionConfiguration::getMTLTextureType(uint32_t descSet, uint32_t binding) const { - for (auto& rb : resourceBindings) { - auto& rbb = rb.resourceBinding; - if (rb.outIsUsedByShader && rbb.desc_set == descSet && rbb.binding == binding) { - return rb.outMTLTextureType; - } - } - return MTLTextureType2D; -} - MVK_PUBLIC_SYMBOL void SPIRVToMSLConversionConfiguration::markAllInputsAndResourcesUsed() { for (auto& si : shaderInputs) { si.outIsUsedByShader = true; } for (auto& rb : resourceBindings) { rb.outIsUsedByShader = true; } @@ -231,10 +221,8 @@ MVK_PUBLIC_SYMBOL void SPIRVToMSLConversionConfiguration::alignWith(const SPIRVT for (auto& rb : resourceBindings) { rb.outIsUsedByShader = false; - rb.outMTLTextureType = MTLTextureType2D; for (auto& srcRB : srcContext.resourceBindings) { if (rb.matches(srcRB)) { - rb.outMTLTextureType = srcRB.outMTLTextureType; rb.outIsUsedByShader = srcRB.outIsUsedByShader; } } @@ -376,9 +364,6 @@ MVK_PUBLIC_SYMBOL bool SPIRVToMSLConverter::convert(SPIRVToMSLConversionConfigur } for (auto& ctxRB : context.resourceBindings) { if (ctxRB.resourceBinding.stage == context.options.entryPointStage) { - ctxRB.outMTLTextureType = getMTLTextureType(pMSLCompiler, - ctxRB.resourceBinding.desc_set, - ctxRB.resourceBinding.binding); ctxRB.outIsUsedByShader = pMSLCompiler->is_msl_resource_binding_used(ctxRB.resourceBinding.stage, ctxRB.resourceBinding.desc_set, ctxRB.resourceBinding.binding);