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

Metal: implement more accurate buffer tracking #7839

Merged
merged 2 commits into from
May 10, 2024

Conversation

bejado
Copy link
Member

@bejado bejado commented May 9, 2024

This PR updates our Metal buffer tracking logic to use a NSHashTable to track alive buffers. NSHashTable is configured to hold weak references to the buffers we add to it. When a buffer is deallocated, it is automatically removed from the hash table. By counting the number of buffers in the hash table, we get an accurate count of the number of alive buffers.

The previous implementation had the limitation that once we pass a buffer off to Metal (when a command buffer becomes the sole owner of it, for example), we assume Metal will eventually release it and and decrement our count. In reality, that buffer could stay alive for a frame or two while the command buffer executes. Using the hash table should give us a more accurate tally of the alive buffer count.

@bejado bejado added the internal Issue/PR does not affect clients label May 9, 2024
filament/backend/src/metal/MetalBuffer.h Show resolved Hide resolved
filament/backend/src/metal/MetalBuffer.h Outdated Show resolved Hide resolved
NSHashTable* hashTable = aliveBuffers[toIndex(type)];
// Caution! We can't simply use hashTable.count here, which is inaccurate.
// See http://cocoamine.net/blog/2013/12/13/nsmaptable-and-zeroing-weak-references/
return hashTable.objectEnumerator.allObjects.count;
Copy link
Collaborator

Choose a reason for hiding this comment

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

everything hinges on this working, which looks like magic to me. do you have high confidence this is indeed working?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do. Objective-C guarantees that weak pointers are set to nil automatically when whatever they're referencing is deallocated. So essentially the hashTable simply needs to tally which pointers aren't nil.

@bejado bejado merged commit 54a800a into main May 10, 2024
11 checks passed
@bejado bejado deleted the bjd/metal-better-buffer-tracking branch May 10, 2024 18:14
bejado added a commit that referenced this pull request May 24, 2024
bejado added a commit that referenced this pull request May 24, 2024
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.

2 participants