Skip to content

Improve resource allocation and binding.#83

Merged
crud89 merged 10 commits intomainfrom
buffer-alloc-helpers
Mar 22, 2023
Merged

Improve resource allocation and binding.#83
crud89 merged 10 commits intomainfrom
buffer-alloc-helpers

Conversation

@crud89
Copy link
Copy Markdown
Owner

@crud89 crud89 commented Mar 22, 2023

Describe the pull request

This PR improves allocation and binding of resources. When creating buffers, it is no longer required to query the descriptor layout in advance. For example, the following versions are now all equal:

// Current way of allocating buffer for descriptor:
auto& pipeline= m_device->state().pipeline("Pipeline");
auto& descriptorSetLayout = pipeline.layout()->descriptorSet(space);
auto& descriptorLayout = descriptorSetLayout.descriptor(descriptor);
auto buffer = m_device->factory().createBuffer("Buffer", descriptorLayout.type(), BufferUsage::Resource, descriptorLayout.elementSize());

// New way using descriptor set layout:
auto& pipeline= m_device->state().pipeline("Pipeline");
auto& descriptorSetLayout = pipeline.layout()->descriptorSet(space);
auto buffer = m_device->factory().createBuffer("Buffer", descriptorSetLayout, descriptor, BufferUsage::Resource);

// New way using only pipeline:
auto& pipeline= m_device->state().pipeline("Pipeline");
auto buffer = m_device->factory().createBuffer("Buffer", pipeline, space, descriptor, BufferUsage::Resource);

Additionally, when allocating descriptor sets, it is now possible to provide default bindings, removing the need to call update for each resource to bind:

// Current style:
auto& pipeline= m_device->state().pipeline("Pipeline");
auto& descriptorSetLayout = pipeline.layout()->descriptorSet(space);
auto descriptorSet = descriptorSetLayout.allocate();
descriptorSet->update(descriptorA, *bufferA, 0, 1);
descriptorSet->update(descriptorB, *bufferB, 0, 1);

// New style using default binding: 
auto& pipeline= m_device->state().pipeline("Pipeline");
auto& descriptorSetLayout = pipeline.layout()->descriptorSet(space);
auto descriptorSet = descriptorSetLayout.allocate({ { descriptorA, *bufferA, 0, 1 }, { descriptorB, *bufferB, 0, 1 } });

When allocating multiple descriptor sets, this can get hard to read and repetitive. Thus there is another new overload, accepting a factory method for default descriptor bindings:

auto& pipeline= m_device->state().pipeline("Pipeline");
auto& descriptorSetLayout = pipeline.layout()->descriptorSet(space);
auto descriptorSets = descriptorSetLayout.allocateMultiple(3, [&](const UInt32& setId) -> Array<DescriptorBinding> {
    return { { descriptorA, *buffers[setId], 0, 1 }, { descriptorB, *textures[setId], 0, 1 } };
});

This also allows to mass-generate bindings efficiently using ranges:

auto descriptorSets = descriptorSetLayout.allocateMultiple(3, [&](const UInt32& setId) { 
    // Generate bindings for binding points 0 - 7.
    return std::views::iota(0, 7) | 
        std::views::transform([&](const auto& binding) { return DescriptorBinding { binding, *buffers[binding], 0, 1 }; }) |
        ranges::to<Array<DescriptorBinding>>();
});

All samples have been updated to use some form of the new interface.

@crud89 crud89 added Priority: Medium A issue with normal priority. Type: Requirement Vulkan 🌋 The issue involves the Vulkan backend. DX12 ❎ The issue involves the DX12 backend. labels Mar 22, 2023
@crud89 crud89 added this to the Alpha #04 milestone Mar 22, 2023
@crud89 crud89 self-assigned this Mar 22, 2023
@crud89 crud89 marked this pull request as ready for review March 22, 2023 17:03
@crud89 crud89 merged commit 425e8b3 into main Mar 22, 2023
@crud89 crud89 deleted the buffer-alloc-helpers branch March 22, 2023 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DX12 ❎ The issue involves the DX12 backend. Priority: Medium A issue with normal priority. Vulkan 🌋 The issue involves the Vulkan backend.

Projects

Status: v0.4.1

Development

Successfully merging this pull request may close these issues.

1 participant