-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Remove busy loops in SDL_GPUFence #14547
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
Open
alexgu754
wants to merge
7
commits into
libsdl-org:main
Choose a base branch
from
alexgu754:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+36
−14
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
33bf26c
Remove busy loops in SDL_GPUFence
alexgu754 e12dab7
Update SDL_gpu_metal.m
alexgu754 0f86335
Apply suggestions from code review
alexgu754 02b175f
Update SDL_gpu_metal.m
alexgu754 9bb8cb2
Update SDL_gpu_metal.m
alexgu754 8cb5e25
Update SDL_gpu_metal.m
alexgu754 db099f5
Update SDL_gpu_metal.m
alexgu754 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,7 +445,9 @@ static MTLDepthClipMode SDLToMetal_DepthClipMode( | |
|
|
||
| typedef struct MetalFence | ||
| { | ||
| SDL_AtomicInt complete; | ||
| bool complete; | ||
| SDL_Mutex *mutex; | ||
| SDL_Condition *condition; | ||
| SDL_AtomicInt referenceCount; | ||
| } MetalFence; | ||
|
|
||
|
|
@@ -739,7 +741,10 @@ static void METAL_DestroyDevice(SDL_GPUDevice *device) | |
|
|
||
| // Release fence infrastructure | ||
| for (Uint32 i = 0; i < renderer->availableFenceCount; i += 1) { | ||
| SDL_free(renderer->availableFences[i]); | ||
| MetalFence *fence = (MetalFence *)renderer->availableFences[i]; | ||
| SDL_DestroyMutex(fence->mutex); | ||
| SDL_DestroyCondition(fence->condition); | ||
| SDL_free(fence); | ||
| } | ||
| SDL_free(renderer->availableFences); | ||
|
|
||
|
|
@@ -2085,7 +2090,9 @@ static Uint8 METAL_INTERNAL_CreateFence( | |
| MetalFence *fence; | ||
|
|
||
| fence = SDL_calloc(1, sizeof(MetalFence)); | ||
| SDL_SetAtomicInt(&fence->complete, 0); | ||
| fence->condition = SDL_CreateCondition(); | ||
| fence->mutex = SDL_CreateMutex(); | ||
| fence->complete = false; | ||
| SDL_SetAtomicInt(&fence->referenceCount, 0); | ||
|
|
||
| // Add it to the available pool | ||
|
|
@@ -2128,7 +2135,7 @@ static bool METAL_INTERNAL_AcquireFence( | |
|
|
||
| // Associate the fence with the command buffer | ||
| commandBuffer->fence = fence; | ||
| SDL_SetAtomicInt(&fence->complete, 0); // FIXME: Is this right? | ||
| fence->complete = false; | ||
| (void)SDL_AtomicIncRef(&commandBuffer->fence->referenceCount); | ||
|
|
||
| return true; | ||
|
|
@@ -3563,6 +3570,14 @@ static void METAL_INTERNAL_PerformPendingDestroys( | |
| } | ||
|
|
||
| // Fences | ||
| static bool METAL_INTERNAL_FenceComplete(MetalFence *fence) | ||
| { | ||
| bool complete; | ||
| SDL_LockMutex(fence->mutex); | ||
| complete = fence->complete; | ||
| SDL_UnlockMutex(fence->mutex); | ||
| return complete; | ||
| } | ||
|
|
||
| static bool METAL_WaitForFences( | ||
| SDL_GPURenderer *driverData, | ||
|
|
@@ -3576,19 +3591,24 @@ static bool METAL_WaitForFences( | |
|
|
||
| if (waitAll) { | ||
| for (Uint32 i = 0; i < numFences; i += 1) { | ||
| while (!SDL_GetAtomicInt(&((MetalFence *)fences[i])->complete)) { | ||
| // Spin! | ||
| MetalFence *fence = (MetalFence *)fences[i]; | ||
| SDL_LockMutex(fence->mutex); | ||
| while(!fence->complete) { | ||
| SDL_WaitCondition(fence->condition, fence->mutex); | ||
| } | ||
| SDL_UnlockMutex(fence->mutex); | ||
| } | ||
| } else { | ||
| waiting = 1; | ||
| while (waiting) { | ||
| for (Uint32 i = 0; i < numFences; i += 1) { | ||
| if (SDL_GetAtomicInt(&((MetalFence *)fences[i])->complete) > 0) { | ||
| MetalFence *fence = (MetalFence *)fences[i]; | ||
| if (METAL_INTERNAL_FenceComplete(fence)) { | ||
| waiting = 0; | ||
| break; | ||
| } | ||
| } | ||
| SDL_DelayNS(1000); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -3603,7 +3623,7 @@ static bool METAL_QueryFence( | |
| SDL_GPUFence *fence) | ||
| { | ||
| MetalFence *metalFence = (MetalFence *)fence; | ||
| return SDL_GetAtomicInt(&metalFence->complete) == 1; | ||
| return METAL_INTERNAL_FenceComplete(metalFence); | ||
| } | ||
|
|
||
| // Window and Swapchain Management | ||
|
|
@@ -3815,7 +3835,7 @@ static bool METAL_WaitForSwapchain( | |
|
|
||
| if (windowData->inFlightFences[windowData->frameCounter] != NULL) { | ||
| if (!METAL_WaitForFences( | ||
| driverData, | ||
| (SDL_GPURenderer *)driverData, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this cast necessary? |
||
| true, | ||
| &windowData->inFlightFences[windowData->frameCounter], | ||
| 1)) { | ||
|
|
@@ -4055,7 +4075,11 @@ static bool METAL_Submit( | |
|
|
||
| // Notify the fence when the command buffer has completed | ||
| [metalCommandBuffer->handle addCompletedHandler:^(id<MTLCommandBuffer> buffer) { | ||
| SDL_AtomicIncRef(&metalCommandBuffer->fence->complete); | ||
| MetalFence *metalFence = (MetalFence *)metalCommandBuffer->fence; | ||
| SDL_LockMutex(metalFence->mutex); | ||
| metalFence->complete = true; | ||
| SDL_SignalCondition(metalFence->condition); | ||
| SDL_UnlockMutex(metalFence->mutex); | ||
| }]; | ||
|
|
||
| // Submit the command buffer | ||
|
|
@@ -4075,7 +4099,7 @@ static bool METAL_Submit( | |
|
|
||
| // Check if we can perform any cleanups | ||
| for (Sint32 i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) { | ||
| if (SDL_GetAtomicInt(&renderer->submittedCommandBuffers[i]->fence->complete)) { | ||
| if (METAL_INTERNAL_FenceComplete(renderer->submittedCommandBuffers[i]->fence)) { | ||
| METAL_INTERNAL_CleanCommandBuffer( | ||
| renderer, | ||
| renderer->submittedCommandBuffers[i], | ||
|
|
@@ -4128,9 +4152,7 @@ static bool METAL_Wait( | |
| * Sort of equivalent to vkDeviceWaitIdle. | ||
| */ | ||
| for (Uint32 i = 0; i < renderer->submittedCommandBufferCount; i += 1) { | ||
| while (!SDL_GetAtomicInt(&renderer->submittedCommandBuffers[i]->fence->complete)) { | ||
| // Spin! | ||
| } | ||
| METAL_WaitForFences((SDL_GPURenderer *)renderer, true, (SDL_GPUFence **)&renderer->submittedCommandBuffers[i]->fence, 1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these casts necessary? You can just pass driverData for the first one. |
||
| } | ||
|
|
||
| SDL_LockMutex(renderer->submitLock); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.