Skip to content

Commit

Permalink
Add latency to surface updates (#3056)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-cojocaru authored Dec 3, 2024
1 parent 5a7e740 commit c548212
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
3 changes: 2 additions & 1 deletion include/mbgl/vulkan/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Context final : public gfx::Context {
void enqueueDeletion(std::function<void(Context&)>&& function);
void submitOneTimeCommand(const std::function<void(const vk::UniqueCommandBuffer&)>& function) const;

void requestSurfaceUpdate() { surfaceUpdateRequested = true; }
void requestSurfaceUpdate(bool useDelay = true);

private:
struct FrameResources {
Expand Down Expand Up @@ -197,6 +197,7 @@ class Context final : public gfx::Context {
uint8_t frameResourceIndex = 0;
std::vector<FrameResources> frameResources;
bool surfaceUpdateRequested{false};
int32_t surfaceUpdateLatency{0};
int32_t currentFrameCount{0};

struct {
Expand Down
22 changes: 17 additions & 5 deletions src/mbgl/vulkan/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ void Context::submitOneTimeCommand(const std::function<void(const vk::UniqueComm
}
}

void Context::requestSurfaceUpdate(bool useDelay) {
if (surfaceUpdateRequested) {
if (!useDelay) {
surfaceUpdateLatency = 0;
}

return;
}

surfaceUpdateRequested = true;
surfaceUpdateLatency = useDelay ? backend.getMaxFrames() * 3 : 0;
}

void Context::waitFrame() const {
MLN_TRACE_FUNC();
const auto& device = backend.getDevice();
Expand Down Expand Up @@ -203,7 +216,7 @@ void Context::beginFrame() {
}
}

if (platformSurface && surfaceUpdateRequested) {
if (platformSurface && surfaceUpdateRequested && --surfaceUpdateLatency <= 0) {
renderableResource.recreateSwapchain();

// we wait for an idle device to recreate the swapchain
Expand Down Expand Up @@ -243,14 +256,13 @@ void Context::beginFrame() {
if (acquireImageResult.result == vk::Result::eSuccess) {
renderableResource.setAcquiredImageIndex(acquireImageResult.value);
} else if (acquireImageResult.result == vk::Result::eSuboptimalKHR) {
renderableResource.setAcquiredImageIndex(acquireImageResult.value);
requestSurfaceUpdate();
beginFrame();
return;
}

} catch (const vk::OutOfDateKHRError& e) {
// request an update and restart frame
requestSurfaceUpdate();
requestSurfaceUpdate(false);
beginFrame();
return;
}
Expand Down Expand Up @@ -310,7 +322,7 @@ void Context::submitFrame() {
requestSurfaceUpdate();
}
} catch (const vk::OutOfDateKHRError& e) {
requestSurfaceUpdate();
requestSurfaceUpdate(false);
}
}

Expand Down

0 comments on commit c548212

Please sign in to comment.