Skip to content

Commit

Permalink
renderer: fix screen tearing issues
Browse files Browse the repository at this point in the history
Fixes #2569
  • Loading branch information
osy committed Jul 10, 2021
1 parent b186c14 commit 9977a79
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Renderer/UTMRenderer.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ - (void)drawInMTKView:(nonnull MTKView *)view
[commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];
renderEncoder.label = @"MyRenderEncoder";

// Lock screen updates
dispatch_semaphore_t drawLock = source.drawLock;
dispatch_semaphore_wait(drawLock, DISPATCH_TIME_FOREVER);

// Render the screen first

bool hasAlpha = NO;
Expand Down Expand Up @@ -245,11 +241,25 @@ - (void)drawInMTKView:(nonnull MTKView *)view
// Schedule a present once the framebuffer is complete using the current drawable
[commandBuffer presentDrawable:view.currentDrawable];

// Lock screen updates
__weak dispatch_semaphore_t drawLock = source.drawLock;

// Acquire lock before GPU schedules rendering
[commandBuffer addScheduledHandler:^(id<MTLCommandBuffer> commandBuffer) {
dispatch_semaphore_t _drawLock = drawLock;
if (_drawLock) {
dispatch_semaphore_wait(drawLock, DISPATCH_TIME_FOREVER);
}
}];

// Release lock after GPU is done
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> commandBuffer) {
// GPU work is complete
// Signal the semaphore to start the CPU work
dispatch_semaphore_signal(drawLock);
dispatch_semaphore_t _drawLock = drawLock;
if (_drawLock) {
dispatch_semaphore_signal(_drawLock);
}
}];
}

Expand Down

0 comments on commit 9977a79

Please sign in to comment.