Skip to content

Commit

Permalink
Merge pull request #2417 from squidbus/arg-buf-binding
Browse files Browse the repository at this point in the history
Reduce number of unused pipeline bindings reserved for argument buffers.
  • Loading branch information
billhollings authored Jan 23, 2025
2 parents 3803cc9 + 29def23 commit 2473ce6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
4 changes: 4 additions & 0 deletions MoltenVK/MoltenVK/GPUObjects/MVKPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -98,6 +101,7 @@ class MVKPipelineLayout : public MVKVulkanAPIDeviceObject {
MVKSmallVector<VkPushConstantRange> _pushConstants;
MVKShaderResourceBinding _mtlResourceCounts;
MVKShaderResourceBinding _pushConstantsMTLResourceIndexes;
bool _canUseMetalArgumentBuffers;
};


Expand Down
30 changes: 20 additions & 10 deletions MoltenVK/MoltenVK/GPUObjects/MVKPipeline.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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).
// - An argument buffer for each descriptor set (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
Expand All @@ -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()) {
Expand Down

0 comments on commit 2473ce6

Please sign in to comment.