a multi-threading problem of SDL3 GPU

Hi.
My game program has main thread and render thread. The main thread is used to create a window, handle window events, and run some scripts on every tick. The render thread is used to render game scene. I record render command to a byte buffer in the main thread, and in the render thread I call sdl gpu function to render.
So I need to call SDL_AcquireGPUCommandBuffer in the rendering thread to create a command buffer, and then call SDL_AcquireGPUSwapchainTexture(commandBuffer, sdlWindow, &colorTexture, &swapchainTextureWidth, &swapchainTextureHeight) to acquire swapchain texture for rendering. I found that the SDL_AcquireGPUSwapchainTexture function annotation says “This function should only be called from the thread that created the window”. But my window is created in the main thread. Can I only call SDL_AcquireGPUSwapchainTexture in the main thread?
What should I do if I want to call the rendering command in the rendering thread.

1 Like

FWIW, I’m headed toward a similar architecture and have the same question.

I use multiple threads but I’m not using the GPU api and I’m not writing a game.

I have the heavy lifting happen in a background thread that sends messages to the main thread (in a similar way sdl event does, a large union with a struct for every event type). My main thread is where I render and I do rendering logic + merges in the events which is pretty simple.

If you want work to be done on multiple threads one option is to build the geometry (which is just data) on 1 or more thread then send it to the thread that executes the GPU function or SDL_RenderGeometry. You don’t need to send all of the geometry at once.

Now that I look at SDL_UploadToGPUBuffer (i’m not sure if that’ll help) more carefully it doesn’t seem to say anything about threads. I haven’t used the gpu api so I have no idea what problems you may run into

1 Like

Maybe you can render to a texture that is not the swapchain texture and draw it to the swapchain texture in the main thread after you’ve finished your draw commands in the render thread.

1 Like