Skip to content

Make VulkanProgram a ThreadSafeResource#9788

Open
anishmgoyal wants to merge 2 commits intogoogle:mainfrom
anishmgoyal:makeProgramsThreadSafe
Open

Make VulkanProgram a ThreadSafeResource#9788
anishmgoyal wants to merge 2 commits intogoogle:mainfrom
anishmgoyal:makeProgramsThreadSafe

Conversation

@anishmgoyal
Copy link
Contributor

With the introduction of cache prewarming, handles for vulkan programs will be managed on a separate thread in the backend along with the main backend thread. This requires an atomic refcount to ensure accuracy and prevent race conditions.

@anishmgoyal anishmgoyal force-pushed the makeProgramsThreadSafe branch from f47200e to 71924d1 Compare March 10, 2026 15:49
@anishmgoyal
Copy link
Contributor Author

@poweifeng @phoenixxxx @pixelflinger @rafadevai fyi

note - the VulkanProgram was moved from VulkanHandles to VulkanAsyncHandles to follow existing patterns. the only code change in the program class itself is the shift from using the base class Resource to using the base class ThreadSafeResource.

@phoenixxxx phoenixxxx added the internal Issue/PR does not affect clients label Mar 10, 2026
@anishmgoyal anishmgoyal changed the title Make VulkanProgram handles thread safe Make VulkanProgram handles a ThreadSafeResource Mar 10, 2026
@anishmgoyal anishmgoyal changed the title Make VulkanProgram handles a ThreadSafeResource Make VulkanProgram a ThreadSafeResource Mar 10, 2026
mResources.push_back(resource);
}

inline void acquire(fvkmemory::resource_ptr<fvkmemory::ThreadSafeResource> resource) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why this is needed? is it for the VulkanProgram?

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. really, we just need the vector to hold resources, be they threadsafe or not. it doesn't interact with them in any other way other than to hold and later free.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe use template as a shorthand.

@rafadevai
Copy link
Contributor

Have we considered any solution that doesnt require the change to threadsaferesource?

To me performance is always a big consideration, most use cases right now dont support parallel compilation, so they are going to pay a penalty here.

@anishmgoyal
Copy link
Contributor Author

anishmgoyal commented Mar 11, 2026

Have we considered any solution that doesnt require the change to threadsaferesource?

To me performance is always a big consideration, most use cases right now dont support parallel compilation, so they are going to pay a penalty here.

this is a very fair point. i don't see a situation where we avoid locking though - the very nature of parallel compilation essentially requires that we're going to be operating on multiple threads when interacting with programs.

even if we were to inc() / dec() on the main thread, we'd have to add a lock to every frame to check if there are pending tasks. there's no way to synchronize multiple threads without a lock or atomic of some sort. to your point, we are adding some atomic ref counts to every frame by doing this, which is a cost because that atomic will not make it into the cache.

that said, we could prevent performance hits in synchronous compilation cases by having completely separate codepaths. one option is to have two separate program classes, one for parallel compilation and one for synchronous.

uint32_t mRangeCount;
};

struct VulkanProgram : public HwProgram, fvkmemory::ThreadSafeResource {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment as to why this needs to be a ThreadSafeResource?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe also mention that all the methods still need to be called on the backend thread. "ThreadSafety" only pertains to the reference counter.

With the introduction of cache prewarming, handles for vulkan programs
will be managed on a separate thread in the backend along with the main
backend thread. This requires an atomic refcount to ensure accuracy and
prevent race conditions.
@anishmgoyal anishmgoyal force-pushed the makeProgramsThreadSafe branch from 71924d1 to 0a8abd6 Compare March 12, 2026 12:36
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.

4 participants