From 98b59bca3e4baf9ae1abddb1e8422ae2cf360e4f Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Mon, 6 Jan 2025 01:22:12 -0800 Subject: [PATCH 1/2] Reduce number of unused pipeline bindings reserved for argument buffers. --- MoltenVK/MoltenVK/GPUObjects/MVKPipeline.h | 4 +++ MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm | 28 ++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.h b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.h index 4f72ba2a9..4869d4977 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.h @@ -82,6 +82,9 @@ class MVKPipelineLayout : public MVKVulkanAPIDeviceObject { /** Returns a text description of this layout. */ std::string getLogDescription(std::string indent = ""); + /** Overridden because pipeline descriptor sets may be marked as discrete and not use an argument buffer. */ + bool isUsingMetalArgumentBuffers() override; + /** Constructs an instance for the specified device. */ MVKPipelineLayout(MVKDevice* device, const VkPipelineLayoutCreateInfo* pCreateInfo); @@ -98,6 +101,7 @@ class MVKPipelineLayout : public MVKVulkanAPIDeviceObject { MVKSmallVector _pushConstants; MVKShaderResourceBinding _mtlResourceCounts; MVKShaderResourceBinding _pushConstantsMTLResourceIndexes; + bool _canUseMetalArgumentBuffers; }; diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm index 7a0942930..80f4ad3b9 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm @@ -141,19 +141,34 @@ return descStr.str(); } +bool MVKPipelineLayout::isUsingMetalArgumentBuffers() { + return MVKDeviceTrackingMixin::isUsingMetalArgumentBuffers() && _canUseMetalArgumentBuffers; +} + MVKPipelineLayout::MVKPipelineLayout(MVKDevice* device, const VkPipelineLayoutCreateInfo* pCreateInfo) : MVKVulkanAPIDeviceObject(device) { + _canUseMetalArgumentBuffers = false; + uint32_t dslCnt = pCreateInfo->setLayoutCount; + _descriptorSetLayouts.reserve(dslCnt); + for (uint32_t i = 0; i < dslCnt; i++) { + MVKDescriptorSetLayout* pDescSetLayout = (MVKDescriptorSetLayout*)pCreateInfo->pSetLayouts[i]; + pDescSetLayout->retain(); + _descriptorSetLayouts.push_back(pDescSetLayout); + _canUseMetalArgumentBuffers = _canUseMetalArgumentBuffers || pDescSetLayout->isUsingMetalArgumentBuffers(); + } + // For pipeline layout compatibility (“compatible for set N”), // consume the Metal resource indexes in this order: // - Fixed count of argument buffers for descriptor sets (if using Metal argument buffers). // - Push constants // - Descriptor set content - // If we are using Metal argument buffers, consume a fixed number - // of buffer indexes for the Metal argument buffers themselves. + // If we are using Metal argument buffers, consume a number of + // buffer indexes covering all descriptor sets for the Metal + // argument buffers themselves. if (isUsingMetalArgumentBuffers()) { - _mtlResourceCounts.addArgumentBuffers(kMVKMaxDescriptorSetCount); + _mtlResourceCounts.addArgumentBuffers(dslCnt); } // Add push constants from config @@ -172,13 +187,8 @@ // Add descriptor set layouts, accumulating the resource index offsets used by the corresponding DSL, // and associating the current accumulated resource index offsets with each DSL as it is added. - uint32_t dslCnt = pCreateInfo->setLayoutCount; - _descriptorSetLayouts.reserve(dslCnt); for (uint32_t i = 0; i < dslCnt; i++) { - MVKDescriptorSetLayout* pDescSetLayout = (MVKDescriptorSetLayout*)pCreateInfo->pSetLayouts[i]; - pDescSetLayout->retain(); - _descriptorSetLayouts.push_back(pDescSetLayout); - + MVKDescriptorSetLayout* pDescSetLayout = _descriptorSetLayouts[i]; MVKShaderResourceBinding adjstdDSLRezOfsts = _mtlResourceCounts; MVKShaderResourceBinding adjstdDSLRezCnts = pDescSetLayout->_mtlResourceCounts; if (pDescSetLayout->isUsingMetalArgumentBuffers()) { From 29def23f57e48b42e26a88f11e82c3b7a22cabbe Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Tue, 21 Jan 2025 20:07:47 -0800 Subject: [PATCH 2/2] Update descriptor set argument buffers comment. --- MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm index 80f4ad3b9..1b2c7afbc 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm @@ -160,7 +160,7 @@ // For pipeline layout compatibility (“compatible for set N”), // consume the Metal resource indexes in this order: - // - Fixed count of argument buffers for descriptor sets (if using Metal argument buffers). + // - An argument buffer for each descriptor set (if using Metal argument buffers). // - Push constants // - Descriptor set content