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

High CPU usage on FreeBSD #6043

Closed
mekanix opened this issue Jan 4, 2023 · 10 comments
Closed

High CPU usage on FreeBSD #6043

mekanix opened this issue Jan 4, 2023 · 10 comments

Comments

@mekanix
Copy link

mekanix commented Jan 4, 2023

Version/Branch of Dear ImGui:

Version: 1.89.2
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Compiler: Clang 13.0
Operating System: FreeBSD

My Issue/Question:

FPS is not the one of the display. Depending on the situation, it gets as high as 500 FPS and it's killing the CPU. If you could help me narrow it down, I would be grateful. Just for reference xrandr | grep '*+' output is 1366x768 60.00*+

Screenshots/Video

Standalone, minimal, complete and verifiable example: (see #2261)

@nicolasnoble
Copy link
Contributor

If you're getting 500FPS it means you're uncapped. You haven't shared code however, but if you're using glfw and opengl3, you want to cap glfw's swap interval.

See https://www.glfw.org/docs/3.3/group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed

You'll want to call this function with a value of 1.

@ocornut
Copy link
Owner

ocornut commented Jan 4, 2023

Either rely on vsync to tie your framerate to monitor refresh rate, or add a timing/waiting mechanism to throttle your framerate to an arbitrary refresh rate.

Please note that Dear ImGui and those examples are coming from a background of game programming, where people tend to refresh the entire display many times a seconds (typically 60 FPS).

If you don't have an underlying game engine running in the background, you may be interested in rendering at slower and variable framerate to tend toward 0 cost on idle. It is possible to load lazy rendering (some references #2749 #4076 #5599) not currently out of the box but possible to setup yourself if you understand the underlying system. Future version should make it easier and better supported out of the box.

@mekanix
Copy link
Author

mekanix commented Jan 4, 2023

The code I'm talking about is in the examples and I see it is enabling vsync. Sorry for not being more clear in the first place. Is there anything I'm missing? Thank you!

@ocornut
Copy link
Owner

ocornut commented Jan 4, 2023

Thanks for confirming.
Your drivers and/or X-Window settings are probably overriding this.

@mekanix
Copy link
Author

mekanix commented Jan 4, 2023

OK, but can I do something to figure out what's wrong on my system? Or is there an easy way to limit ImGui renders to like 60 FPS or something? I'm just not good enough GUI developer.

@emoon
Copy link
Contributor

emoon commented Jan 4, 2023

If you can't get vsync working the more imprecise way of doing this is to sleep manually instead. Something like this

void update_loop() {
  float time = read_time();
  // update imgui, etc here
  float delta_time = read_time() - time;
  float time_to_sleep_sec = (1.0f / 60.0f) - delta_time;
  sleep(time_to_sleep_sec);
}

I haven't included any error checking, but the idea here is that you calculate how much time all your update code took including dear imgui and then you sleep your CPU to the delta up to 16.666 ms (60 fps)

That being said getting vsync working is a better approach and the above should be seen as a workround

@nicolasnoble
Copy link
Contributor

If glfwSwapInterval isn't working properly on FreeBSD, this is probably a bug that should be reported to the glfw project. I doubt FreeBSD is a very well supported platform however.

@mekanix
Copy link
Author

mekanix commented Jan 4, 2023

Same problem when I use Wayland and/or if I use sdl+opengl. I guess the problem is deeper in the OpenGL on FreeBSD. If I find the solution, I'll post it here.

@ocornut
Copy link
Owner

ocornut commented Jan 5, 2023

You may want to also try Vulkan.

I'll close this issue as non being Dear ImGui specific, but happy to hear if you find something.

@ocornut ocornut closed this as completed Jan 5, 2023
@mekanix
Copy link
Author

mekanix commented Jan 8, 2023

Finally found some free time to fiddle with this and using vulkan gives expected results. For some reason I believed vulkan is only for AMD. Thank you for the tip and I will probably switch to vulkan as a more recent standard.

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

No branches or pull requests

4 participants