Skip to content

Lua runs every vsync while the window has focus, even when idle #131

Backlog

Description

@zubSero

While PoB has focus, OnFrame runs every vsync even when nothing changes. The main loop only waits when minimized (

while (exitFlag == false) {
if (minimized) {
glfwWaitEventsTimeout(0.1);
}
else {
glfwPollEvents();
}
auto wnd = (GLFWwindow*)video->GetWindowHandle();
), and ui_main_c::Frame only sleeps when the window is unfocused (
else if (!sys->video->IsActive() && !sys->video->IsCursorOverWindow() && !hasActiveCoroutine && !hasSubscript) {
sys->Sleep(100);
return;
and
if ( !sys->video->IsActive() && !hasActiveCoroutine && !hasSubscript ) {
sys->Sleep(100);
}
). Frame elision skips drawing the layers (
uint64_t commandDigest = 0;
if (elideFrames) {
auto* frameHasher = frameHashState.get();
XXH3_64bits_reset(frameHasher);
for (auto lIdx = 0; lIdx < numLayer; ++lIdx) {
auto layer = layerSort[lIdx];
uint64_t subHash = XXH3_64bits(layer->cmdStorage.data(), (int)layer->cmdCursor);
XXH3_64bits_update(frameHasher, &subHash, sizeof(subHash));
}
commandDigest = XXH3_64bits_digest(frameHasher);
}
++totalFrames;
const bool elideDraw = lastFrameHash != 0 && lastFrameHash == commandDigest;
if (!elideDraw)
{
glBindFramebuffer(GL_FRAMEBUFFER, GetDrawRenderTarget().framebuffer);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
for (int l = 0; l < numLayer; l++) {
auto& layer = layerSort[l];
if (layerBreak && layerBreak->first == layer->layer && layerBreak->second == layer->subLayer) {
#ifdef _WIN32
DebugBreak();
#endif
}
inhibitElision = layer->Render() || inhibitElision;
}
presentRtt = 1 - presentRtt;
++drawnFrames;
}
), but Lua, the blit and the swap still run every frame. On a 144 Hz screen that's 144 Lua frames a second for an idle window.

I know running Lua only when something happens is on your list. These are the places I think it would touch:

  • The elision hash could double as an idle detector. After a few identical frames with no input, no subscript, no running coroutine and no pending texture, the loop could switch from glfwPollEvents to glfwWaitEventsTimeout.
  • The GLFW callbacks would set a dirty flag. The cursor callback already stores the position (
    glfwSetCursorPosCallback(wnd, [](GLFWwindow* wnd, double x, double y) {
    auto sys = (sys_main_c*)glfwGetWindowUserPointer(wnd);
    if (ImGui::GetIO().WantCaptureMouse) {
    return;
    }
    auto video = (sys_video_c*)sys->video;
    video->lastCursorPos = CursorPos{ (int)x, (int)y };
    });
    ).
  • Workers would call glfwPostEmptyEvent() when they hand something to the main thread: subscript messages, and texture uploads (
    void t_manager_c::EnqueueTextureUpload(r_tex_c* tex)
    {
    std::scoped_lock lk(uploadMutex);
    uploadQueue.push_back(tex);
    }
    ).
  • The texture workers spin on Sleep(1) while idle ( ). The condvar change from fix: accurate frame elision and draw performance #110 (6a74097) already fixes that.

On the PoB side the caret blink, toasts and the rotating jewel rings need regular frames, so a timeout of a few hundred ms, or a way for Lua to ask for the next frame, would keep those working.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions