opentk / GLControl

WinForms control for OpenTK 4.x.
https://opentk.net
Other
51 stars 24 forks source link

Application Slows when GLControl Out of View #28

Open sdreb3421 opened 1 year ago

sdreb3421 commented 1 year ago

Setup: Windows 10 Pro 21H2. OpenGL 3.3 core running on a NVIDIA GeForce GTX 1050. Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz CPU WinForms NET 6 OpenTK 4.7.4 OpenTK.WinForms 4.0.0-pre-6

Issue: When the parent form of the GLControl is minimized, or the GLControl is hidden from view on an unseen tab page of a tab control, the refresh rate of OpenGL drops from 2ms to ~33ms. Additionally, the entire application UI runs about 20x slower than normal. Completely unrelated child forms that normally open in ~250ms to open take ~5 seconds to open.

Notes: This issue did not occur when using NET 4.8 with the original GLControl. The only obvious difference with the NET 4.8 GLControl version is that VSync was set to False (VSync property does not exist with new version).

seanofw commented 1 year ago

This seems odd, but I haven't done a lot with the GLControl and background threads. Your notes seem to suggest that maybe there's runaway window messaging going on. Does the CPU usage spike as well when the refresh rate or performance drops?

Also, can you describe a little about what hardware you're running on? Which version of Windows (presumably this is Windows?), CPU, GPU, etc?

sdreb3421 commented 1 year ago

I only notice an increase in CPU when opening up a child form while the GLControl is out of view. Normally it would take 1 core an additional 10% for a very short period, but when the issue is happening the 1 core goes up 10% for 5 seconds.

sdreb3421 commented 1 year ago

I could probably make a demo project. Would that be helpful in this scenario?

sdreb3421 commented 1 year ago

I found that the slowdown is associated with the NVIDIA GPU. When the application is set to use the INTEL GPU the issue goes away. Additionally, it happens equally when using the UI thread or a background thread for OpenTK drawing.

Check out the demo project and video below. The video shows how the frame rate changes when switching between tabs (hiding the GLControl), first illustrating INTEL and second illustrating NVIDIA.

I've tried changing between the NVIDIA Game Ready and Studio drivers (516.94) with the same result. I've also tried messing with other NVIDIA control panel settings (PhysX, etc) with the same result.

If you have a NVIDIA GPU I'd be curios if you could reproduce this using the demo below.

OpenTkDemo.zip

https://user-images.githubusercontent.com/1911567/188698667-723e7439-ecb2-4502-955b-5104e8e4e0c1.mp4

sdreb3421 commented 1 year ago

UPDATE: Tested on another computer with a NVIDIA RTX 2070 and the issue is not present.

sdreb3421 commented 1 year ago

All of the delay happens in the swap buffers line. If I downloaded the OpenTK source is there anything that might be debuggable inside of the swap buffers method, or is this an unsolvable driver issue of some sort?

seanofw commented 1 year ago

Ouch. Sadly, no, SwapBuffers() forwards pretty directly through OpenTK and GLFW, and goes directly into the video driver. If you're experiencing slowdowns there, it's a driver issue and there's not much that can be done about it other than detecting when the panel is in a hidden tab and then not rendering.

NogginBops commented 1 year ago

Definitely a weird issue. My recommendation/workaround is just to avoid submitting drawcalls for stuff that isn't going to appear on the screen. You can just avoid calling SwapBuffers entirely when the control isn't shown.

sdreb3421 commented 1 year ago

That's doable. The only other difference in API from the original is the ability to set Vsync. Probably not related, but is there a way to set this in OpenTK 4?

NogginBops commented 1 year ago

OpenTK 4 NativeWindow has a setting for this, but I don't know if this would work for this control. I would have to delegate that question to @seanofw for now.

NogginBops commented 9 months ago

I'll look into enabling VSync in GLControl for 4.0 release. Opening a separate issue for that in #36