Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vk: add resource ref-counting alternative #8220

Merged
merged 5 commits into from
Nov 1, 2024

Conversation

poweifeng
Copy link
Contributor

We add a set of classes to the vk backend that will enable cleaner ref-counting.

In particular, all allocation from the HandleAllocator will be wrapped within a resource_ptr<> smart pointer. This struct will maintain a count that will increment/decrement with respect to references (similar to std::shared_ptr).

This commit only introduces the new classes/structs and does not actually make any functional difference. A follow-up commit will make the switch to use the smart pointer.

We also put VulkanFence and VulkanTimerQuery in a different header because we will need to depend on them separately in the follow-up (reason: they are accessed on the backend thread but allocated on "sync"/filament thread).

@poweifeng poweifeng added the internal Issue/PR does not affect clients label Oct 22, 2024

void destroy(ResourceType type, HandleId id);

HandleId id; // 8
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleId is a uint32_t

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I wonder why the object needs its own handle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleId is a uint32_t

corrected.

Also I wonder why the object needs its own handle?

Because by the time the count reaches 0, I no longer have access to the handle, and I need it in order to destroy. Would it preferable to keep it in resource_ptr ? I can pass it in as dec(HandleId id). That would increase the size overall (multiple references to one handle object) but reduce the increase to the Handle objects.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, it's probably okay to keep it where you have it, because due to padding, you'll always have space for it I think.

filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
}

template<bool THREAD_SAFE = false>
struct ResourceImpl {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it all backend handles that now will inherit from ResourceImpl or only those that need reference counting? (Asking because the size overhead is not trivial).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, for simplicity all backend handles will inherit from ResourceImpl. With the previous change where I moved bits from VulkanRenderTarget (the largest handle) to the heap, we might be ok with this increase.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that means that all handles will increase by at least 16 bytes. I think 8 bytes is the minimum (the cound + some meta data). What is really sad is to have to store a pointer. Would it be too painful to pass it around everywhere it's needed? Or maybe we can think of doing that in another change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think for the pointer to the manager, we can definitely revisit.

filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/ResourcePointer.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
filament/backend/src/vulkan/memory/Resource.h Outdated Show resolved Hide resolved
We add a set of classes to the vk backend that will enable
cleaner ref-counting.

In particular, all allocation from the HandleAllocator will be
wrapped within a resource_ptr<> smart pointer. This struct will
maintain a count that will increment/decrement with respect to
references (similar to std::shared_ptr).

This commit only introduces the new classes/structs and does not
actually make any functional difference. A follow-up commit will
make the switch to use the smart pointer.

We also put VulkanFence and VulkanTimerQuery in a different header
because we will need to depend on them separately in the follow-up
(reason: they are accessed on the backend thread but allocated on
"sync"/filament thread).
@poweifeng poweifeng enabled auto-merge (squash) November 1, 2024 21:24
@poweifeng poweifeng merged commit 3304a0c into main Nov 1, 2024
12 checks passed
@poweifeng poweifeng deleted the pf/vk-add-ref-count-resource branch November 1, 2024 21:36
poweifeng added a commit that referenced this pull request Nov 6, 2024
Continuing from PR #8220.

Here we change all of the references from the old ref-counting
ways to the new ref-counting structs and mechanisms. There should
be no functional change.
poweifeng added a commit that referenced this pull request Nov 6, 2024
Continuing from PR #8220.

Here we change all of the references from the old ref-counting
ways to the new ref-counting structs and mechanisms. There should
be no functional change.
poweifeng added a commit that referenced this pull request Nov 8, 2024
Continuing from PR #8220.

Here we change all of the references from the old ref-counting
ways to the new ref-counting structs and mechanisms. There should
be no functional change.
poweifeng pushed a commit that referenced this pull request Nov 12, 2024
Continuing from PR #8220.

Here we change all of the references from the old ref-counting ways
to the new ref-counting structs and mechanisms. There should be no
functional change.
poweifeng pushed a commit that referenced this pull request Nov 12, 2024
Continuing from PR #8220.

Here we change all of the references from the old ref-counting ways
to the new ref-counting structs and mechanisms. There should be no
functional change.
poweifeng added a commit that referenced this pull request Nov 12, 2024
Continuing from PR #8220.

Here we change all of the references from the old ref-counting ways
to the new ref-counting structs and mechanisms. There should be no
functional change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal Issue/PR does not affect clients
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants