Skip to content
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

Don't render when not necessary #189

Closed
Flamaros opened this issue Mar 1, 2021 · 7 comments
Closed

Don't render when not necessary #189

Flamaros opened this issue Mar 1, 2021 · 7 comments

Comments

@Flamaros
Copy link

Flamaros commented Mar 1, 2021

The application should avoid to draw when not needed to reduce power consumption.

I think that it is critical to be able to work correctly on a laptop.

PS: I reported some other issues, it's because I find ImHex pretty cool and useful. Thank you.

@TrueBers
Copy link

TrueBers commented Mar 6, 2021

Or should at least have an optional FPS limiter, like 30 or 15.
60 FPS for a hex editor is pretty much overkill.

imhex

15% of i7-7700HQ CPU and 28-30% of GTX1050 GPU load is enormous for an idling editor. My laptop is gonna hate me soon.
The editor is really nice, but we definitely have to do something with that global warming load percentage. It's unusable on a laptop without connected power supply.

WerWolv added a commit that referenced this issue Mar 6, 2021
@WerWolv
Copy link
Owner

WerWolv commented Mar 6, 2021

Added a few fixes for this issue with the latest commit.
First of all, ImHex will no longer render at all when the window is not visible or if it's been minimized. Second, I added a setting now where you can choose an FPS limit between 15FPS and 60FPS. Everything below 30 does become kinda laggy but I figured some people might still want it.
If there's anything else that should be done, please let me know.

@Flamaros
Copy link
Author

Flamaros commented Mar 20, 2021

Sorry for late answer.
I think that it is already a really good improvement, but for really saving the battery I think that the application shouldn't render if there is not change at all, even when having the focus.

When developing mobile applications the gain was really huge between slowing down the refresh rate and only rendering when the graphical scene changed. Generally there is only a flag to put at true when an processed input trigger something that require a redraw like a button hover event,...
Things become more difficult when the application can display animations because in this case when animations ends, the rendering mode have to come back the on-demand one.

I don't know if Dear ImGUI provide a flag to notify if the scene changed (Qt-QML do this with his scenegraph).

In all cases, I'll take a try with the next release. I really appreciate the latest improvements. Thx

EDIT: On my laptop I have 2 GPUs (with Nvidia Optimus), so if you use OpenGL it is by default on the low energy one (CPU integrated), but I know that with Vulkan you have to pick one manually In which case looking for the integrated one is certainly the best choice for ImHex.

@mirh
Copy link

mirh commented Jun 2, 2021

Indeed, it seems completely stupid for an UI to render on the same principles of a 3D game.
ocornut/imgui#2268

@WerWolv
Copy link
Owner

WerWolv commented Jun 2, 2021

This has been fixed a while ago though

@mirh
Copy link

mirh commented Jun 2, 2021

Not really? Both my cpu and gpu have a quite significant load when the program is open.

@Tey
Copy link

Tey commented Jul 25, 2021

@WerWolv The way you use ImGui::IsWindowFocused() to detect if the application has the focus does not work (at least on Windows). From my understanding of ImGui code, the ImGuiFocusedFlags_AnyWindow flag will only tell you which of the GUI or the underlying game has the focus, it does not tell you if the program has the focus. Thus, since there is no underlying "game" for ImHex, ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow) always returns true on Windows, even when another application has the focus.
You should probably use the focus callbacks from glfw to detect that instead, but I haven't tested.

IMHO, ImHex still uses too much CPU than it should for rendering. To reduce that even more, you could prevent rendering a new frame in the lack of input events, as you do when the app is minimized. The only drawback is that it also prevents event-less animations to play. One middle ground is to use a small FPS in the absence of events, and keep using a high FPS for events so that the app is still responsive. Something like that:

diff --git a/source/window.cpp b/source/window.cpp
index ef41139..c839abc 100644
--- a/source/window.cpp
+++ b/source/window.cpp
@@ -186,9 +186,11 @@ namespace hex {
         this->m_lastFrameTime = glfwGetTime();
         while (!glfwWindowShouldClose(this->m_window)) {
             if (!glfwGetWindowAttrib(this->m_window, GLFW_VISIBLE) || glfwGetWindowAttrib(this->m_window, GLFW_ICONIFIED))
+                // Only render when new events are received
                 glfwWaitEvents();
-
-            glfwPollEvents();
+            else
+                // Render at 5 FPS when no new events are received
+                glfwWaitEventsTimeout(this->m_lastFrameTime - glfwGetTime() + 1 / 5.0);
 
             this->frameBegin();
             this->frame();

I haven't seen any animations in ImHex that requires more than 1 FPS to run, so maybe this should be the minimum FPS goal instead of 5. Or even better, let the users select the value they want through a parameter (making it possible to be less than 1 FPS maybe).

With latest source code from the master branch, ImHex uses around 6% of my CPU when idle. When using a minimum of 5 FPS, it uses around 1% of CPU, and this goes down to 0.1% with 1 FPS, so that might be significant for battery powered devices.

On a side note, glfwWaitEvents() and glfwWaitEventsTimeout() do call glfwPollEvents() when an event is available.

WerWolv added a commit that referenced this issue Jul 27, 2021
Now frames are rendered at only 5 FPS if no input events are being processed. Thanks a lot to @Tey. Properly fixes #189 now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants