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

GPU: Make SDL_SetGPUBufferName and SDL_SetGPUTextureName thread safe #11929

Closed

Conversation

thatcosmonaut
Copy link
Collaborator

Resolves #11927

@thatcosmonaut thatcosmonaut requested a review from slouken January 12, 2025 22:52
@kg
Copy link
Contributor

kg commented Jan 12, 2025

For consistency with how these naming APIs work in some other places you probably want to strdup the name, since it could be a temporary stack buffer being passed in. Or document that it needs to stay alive indefinitely, but that seems worse.

You should be able to free the name once you actually pass it through to vulkan though.

@thatcosmonaut
Copy link
Collaborator Author

Good call.

@slouken
Copy link
Collaborator

slouken commented Jan 12, 2025

This code looks fine, but are we guaranteed that the main thread is a safe thread to call this on? Is there a possibility of interacting with other graphics calls being made off the main thread?

@thatcosmonaut
Copy link
Collaborator Author

Skimmed the Vulkan spec again and saw:

Host access to pNameInfo->objectHandle must be externally synchronized

This might be more problematic than I thought. I'm not sure that we have any way to guarantee this for the client.

@slouken
Copy link
Collaborator

slouken commented Jan 12, 2025

Skimmed the Vulkan spec again and saw:

Host access to pNameInfo->objectHandle must be externally synchronized

This might be more problematic than I thought. I'm not sure that we have any way to guarantee this for the client.

What does externally synchronized mean? Just that it can only be done by one thread at a time?

@slouken
Copy link
Collaborator

slouken commented Jan 12, 2025

@danginsburg, do you have any insight here?

@slime73
Copy link
Contributor

slime73 commented Jan 12, 2025

Is the thread safety of these mainly hard to reason about because they can be called at any point after creating the resource? Would making resource names be part of buffer and texture creation SDL_Properties instead solve that? Although I suppose it might be too late to do that now..

@thatcosmonaut
Copy link
Collaborator Author

What does externally synchronized mean? Just that it can only be done by one thread at a time?

It means the handle should only be accessed by one thread at a time.

@thatcosmonaut
Copy link
Collaborator Author

Is the thread safety of these mainly hard to reason about because they can be called at any point after creating the resource? Would making resource names be part of buffer and texture creation SDL_Properties instead solve that? Although I suppose it might be too late to do that now..

Yeah I think that would be a good solution. As long as the thread safety issue on the existing functions is clearly documented (which it currently is) we could just add this as a creation property and sidestep the safety issues that way.

@slouken
Copy link
Collaborator

slouken commented Jan 13, 2025

Is the thread safety of these mainly hard to reason about because they can be called at any point after creating the resource? Would making resource names be part of buffer and texture creation SDL_Properties instead solve that? Although I suppose it might be too late to do that now..

Yeah I think that would be a good solution. As long as the thread safety issue on the existing functions is clearly documented (which it currently is) we could just add this as a creation property and sidestep the safety issues that way.

Agreed. Having the name as a creation property makes a lot of sense.

@kg
Copy link
Contributor

kg commented Jan 13, 2025

Does passing the name at creation time actually fix this? That's what I was doing when I encountered this bug, because I wasn't guarding the name setting operation behind a lock.

It's safe to create textures from any thread (based on my reading of the vulkan and d3d12 docs) but in vulkan it's not safe to assign a name to the texture even if it's done immediately after from the same thread. It seems like there's some internal command buffer these naming operations go into and you have to externally synchronize around it.

EDIT: That is, the code looked like this:
image
And guarding it with the same lock I guard rendering operations with made the validator warnings go away, so it seems to dislike names being set while the device is otherwise in use.

@thatcosmonaut
Copy link
Collaborator Author

Does passing the name at creation time actually fix this? That's what I was doing when I encountered this bug, because I wasn't guarding the name setting operation behind a lock.

It should, because if we call the Vulkan debug name function before returning from the creation function there's no way for the client to even have a handle that they would need to synchronize.

@kg
Copy link
Contributor

kg commented Jan 13, 2025

In my repro case the texture wasn't externally visible yet or being used. The synchronization hazard is on the device I think.

@thatcosmonaut
Copy link
Collaborator Author

Closing this in favor of #11946

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GPU: Make resource naming calls thread safe
4 participants