Skip to content

Commit

Permalink
replace window data mutex with atomic int
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcosmonaut committed Sep 29, 2024
1 parent d028fdb commit 7da11b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
19 changes: 5 additions & 14 deletions src/gpu/d3d11/SDL_gpu_d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,7 @@ typedef struct D3D11WindowData
DXGI_COLOR_SPACE_TYPE swapchainColorSpace;
SDL_GPUFence *inFlightFences[MAX_FRAMES_IN_FLIGHT];
Uint32 frameCounter;
bool needsSwapchainRecreate;
SDL_Mutex *lock;
SDL_AtomicInt needsSwapchainRecreate;
} D3D11WindowData;

typedef struct D3D11Shader
Expand Down Expand Up @@ -5029,9 +5028,7 @@ static bool D3D11_INTERNAL_OnWindowResize(void *userdata, SDL_Event *e)
D3D11WindowData *data;
if (e->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
data = D3D11_INTERNAL_FetchWindowData(w);
SDL_LockMutex(data->lock);
data->needsSwapchainRecreate = true;
SDL_UnlockMutex(data->lock);
SDL_SetAtomicInt(&data->needsSwapchainRecreate, true);
}

return true;
Expand Down Expand Up @@ -5291,7 +5288,7 @@ static bool D3D11_INTERNAL_ResizeSwapchain(

windowData->textureContainer.header.info.width = w;
windowData->textureContainer.header.info.height = h;
windowData->needsSwapchainRecreate = !result;
SDL_SetAtomicInt(&windowData->needsSwapchainRecreate, !result);
return result;
}

Expand Down Expand Up @@ -5379,7 +5376,6 @@ static bool D3D11_ClaimWindow(
if (windowData == NULL) {
windowData = (D3D11WindowData *)SDL_calloc(1, sizeof(D3D11WindowData));
windowData->window = window;
windowData->lock = SDL_CreateMutex();

if (D3D11_INTERNAL_CreateSwapchain(renderer, windowData, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC)) {
SDL_SetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, windowData);
Expand Down Expand Up @@ -5464,7 +5460,6 @@ static void D3D11_ReleaseWindow(
}
SDL_UnlockMutex(renderer->windowLock);

SDL_DestroyMutex(windowData->lock);
SDL_free(windowData);

SDL_ClearProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA);
Expand All @@ -5488,12 +5483,8 @@ static bool D3D11_AcquireSwapchainTexture(
SET_STRING_ERROR_AND_RETURN("Cannot acquire a swapchain texture from an unclaimed window!", false)
}

if (windowData->needsSwapchainRecreate) {
SDL_LockMutex(windowData->lock);
bool resizeSuccess = D3D11_INTERNAL_ResizeSwapchain(renderer, windowData);
SDL_UnlockMutex(windowData->lock);

if (!resizeSuccess) {
if (SDL_GetAtomicInt(&windowData->needsSwapchainRecreate)) {
if (!D3D11_INTERNAL_ResizeSwapchain(renderer, windowData)) {
return false;
}
}
Expand Down
19 changes: 5 additions & 14 deletions src/gpu/d3d12/SDL_gpu_d3d12.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,7 @@ typedef struct D3D12WindowData

D3D12TextureContainer textureContainers[MAX_FRAMES_IN_FLIGHT];
SDL_GPUFence *inFlightFences[MAX_FRAMES_IN_FLIGHT];
bool needsSwapchainRecreate;
SDL_Mutex *lock;
SDL_AtomicInt needsSwapchainRecreate;
} D3D12WindowData;

typedef struct D3D12PresentData
Expand Down Expand Up @@ -5980,9 +5979,7 @@ static bool D3D12_INTERNAL_OnWindowResize(void *userdata, SDL_Event *e)
D3D12WindowData *data;
if (e->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
data = D3D12_INTERNAL_FetchWindowData(w);
SDL_LockMutex(data->lock);
data->needsSwapchainRecreate = true;
SDL_UnlockMutex(data->lock);
SDL_SetAtomicInt(&data->needsSwapchainRecreate, true);
}

return true;
Expand Down Expand Up @@ -6334,7 +6331,7 @@ static bool D3D12_INTERNAL_ResizeSwapchain(
}
}

windowData->needsSwapchainRecreate = false;
SDL_SetAtomicInt(&windowData->needsSwapchainRecreate, false);
return true;
}

Expand Down Expand Up @@ -6530,7 +6527,6 @@ static bool D3D12_ClaimWindow(
return false;
}
windowData->window = window;
windowData->lock = SDL_CreateMutex();

if (D3D12_INTERNAL_CreateSwapchain(renderer, windowData, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC)) {
SDL_SetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, windowData);
Expand Down Expand Up @@ -6594,7 +6590,6 @@ static void D3D12_ReleaseWindow(
}
SDL_UnlockMutex(renderer->windowLock);

SDL_DestroyMutex(windowData->lock);
SDL_free(windowData);
SDL_ClearProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA);
SDL_RemoveEventWatch(D3D12_INTERNAL_OnWindowResize, window);
Expand Down Expand Up @@ -6927,12 +6922,8 @@ static bool D3D12_AcquireSwapchainTexture(
SET_STRING_ERROR_AND_RETURN("Cannot acquire swapchain texture from an unclaimed window!", false)
}

if (windowData->needsSwapchainRecreate) {
SDL_LockMutex(windowData->lock);
bool resizeSuccess = D3D12_INTERNAL_ResizeSwapchain(renderer, windowData);
SDL_UnlockMutex(windowData->lock);

if (!resizeSuccess) {
if (SDL_GetAtomicInt(&windowData->needsSwapchainRecreate)) {
if (!D3D12_INTERNAL_ResizeSwapchain(renderer, windowData)) {
return false;
}
}
Expand Down
19 changes: 5 additions & 14 deletions src/gpu/vulkan/SDL_gpu_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,7 @@ typedef struct WindowData
SDL_GPUSwapchainComposition swapchainComposition;
SDL_GPUPresentMode presentMode;
VulkanSwapchainData *swapchainData;
bool needsSwapchainRecreate;
SDL_Mutex *lock;
SDL_AtomicInt needsSwapchainRecreate;
} WindowData;

typedef struct SwapchainSupportDetails
Expand Down Expand Up @@ -4672,7 +4671,7 @@ static bool VULKAN_INTERNAL_CreateSwapchain(
}

windowData->swapchainData = swapchainData;
windowData->needsSwapchainRecreate = false;
SDL_SetAtomicInt(&windowData->needsSwapchainRecreate, false);

return true;
}
Expand Down Expand Up @@ -9332,9 +9331,7 @@ static bool VULKAN_INTERNAL_OnWindowResize(void *userdata, SDL_Event *e)
WindowData *data;
if (e->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
data = VULKAN_INTERNAL_FetchWindowData(w);
SDL_LockMutex(data->lock);
data->needsSwapchainRecreate = true;
SDL_UnlockMutex(data->lock);
SDL_SetAtomicInt(&data->needsSwapchainRecreate, true);
}

return true;
Expand Down Expand Up @@ -9432,7 +9429,6 @@ static bool VULKAN_ClaimWindow(
windowData->window = window;
windowData->presentMode = SDL_GPU_PRESENTMODE_VSYNC;
windowData->swapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR;
windowData->lock = SDL_CreateMutex();

if (VULKAN_INTERNAL_CreateSwapchain(renderer, windowData)) {
SDL_SetPointerProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA, windowData);
Expand Down Expand Up @@ -9500,7 +9496,6 @@ static void VULKAN_ReleaseWindow(
}
SDL_UnlockMutex(renderer->windowLock);

SDL_DestroyMutex(windowData->lock);
SDL_free(windowData);

SDL_ClearProperty(SDL_GetWindowProperties(window), WINDOW_PROPERTY_DATA);
Expand Down Expand Up @@ -9600,12 +9595,8 @@ static bool VULKAN_AcquireSwapchainTexture(
}

// If window data marked as needing swapchain recreate, try to recreate
if (windowData->needsSwapchainRecreate) {
SDL_LockMutex(windowData->lock);
bool recreateSuccess = VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
SDL_UnlockMutex(windowData->lock);

if (!recreateSuccess) {
if (SDL_GetAtomicInt(&windowData->needsSwapchainRecreate)) {
if (!VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData)) {
return false;
}

Expand Down

0 comments on commit 7da11b6

Please sign in to comment.