AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
24.99k stars 2.17k forks source link

High GPU usage on 4K #6298

Open trympet opened 3 years ago

trympet commented 3 years ago

I have been dealing with low FPS inside scroll viewers for some time now. No amount of Xaml optimization seems to do the trick.

The Avalonia Controls Gallery exhibits the exact same behavior. I have reproduced the issue on several machines as well.

It seems the FPS scales linearly with the raw size of the window. I am only able to reproduce the issue on 4k screens, however, since DPI scaling is common place, it affects all high resolution devices. This serves as an indicator that the rendering stack struggles with large work areas.

The issue is most noticeable on touch devices or devices equipped with a precision trackpad, i.e., all mobile devices.

dotTrace shows that the scene is updated on every scroll tick. Why? How about adding an API for freezing the content or deferring updates when scrolling? Seems like a cheap way to save on cycles.

Google Chrome has no issues with scrolling on the same devices, so Skia should be up for the task.

Here is what the flame graph looks like when scrolling in dotTrace. image

Task manager when scrolling: image

My apologies for a vague bug report; I am not sure where to start in order to debug the issue, however, I'd be happy to assist in resolving it.

To reproduce: Obtain a device running Windows 10 with

Note: I have reproduced this on a regular desktop as well, however, rigs with dedicated GPUs will likely not exhibit the same behavior.

Env: Windows 19043 Deferred rendering, WinUI compositor, EGL Avalonia 0.10.6, 0.10.3 Intel i7-10750H; 10th gen UHD iGPU

grokys commented 3 years ago

We're aware that our current renderer isn't the best, and we're working on a new renderer, but in the meantime I took a (very) quick look at your repro with the Avalonia Controls Gallery on my 4k screen and I'm not seeing any lag and GPU usage is barely registering.

The main difference here is that I don't have a touch screen or precision touch pad (it's a desktop) but I'm not sure why that would make a difference. Are you able to repro by dragging the scrollbar instead of using touch?

One more thing: which page of the Avalonia Control Gallery are you seeing this on?

trympet commented 3 years ago

@grokys, Thanks for the response. I am very happy with Avalonia and the API design. This is my only pet peeve :)

Yes, I the same behavior when manipulating the scrollbar.

It is noticeable on all pages, but most apparent on the layout page, since it's the tallest.

Attempts to reproduce the issue on macOS (2017 i5 MacBook, 2020 M1 Mac Mini) have failed, so the issue is limited to Windows. The only performance hiccup I noticed on macOS was when resizing the window, though I think that is due to acrylic blur.

The reason I mentioned precision trackpads in particular is that they send WM_MOUSEWHEEL with deltas >= 1, where regular trackpads or scroll wheels send deltas of 120, thus more redraws. The deferred renderer is basically saturated from my observations, which might explain why the immediate renderer performs so much better.

This does, however, not explain the difference in performance performance between macOS. Is scrolling handled differently by Avalonia.Native?

Intel System Analyzers shows the same GPU usage for EGL and D2D.

Please let me know if you require any diagnostic data.

trympet commented 3 years ago

Here is a dxdiag: https://pastebin.com/jpcSnHJc

trympet commented 3 years ago

I was able to replicate the exact same issue in Plex for Windows. It uses Qt and libGLES. Maybe it is the GL interface which is the culprit?

grokys commented 3 years ago

Yes, I the same behavior when manipulating the scrollbar.

Ok, so the issue doesn't seem to be that the touch device is producing too many scroll events as I suspected...

I'm not sure where to start diagnosing this as I can't reproduce on either of my machines. Do you by any chance have another machine besides these Windows and OSX machines to test on?

For reference this is what I see on my machine (and the GPU usage appears to be mostly due to the screen recording software - when I'm not recording the GPU usage is almost flat):

https://user-images.githubusercontent.com/1775141/128307614-0aa53dec-3f15-4bea-ade7-5cefe445be94.mp4

I was able to replicate the exact same issue in Plex for Windows. It uses Qt and libGLES. Maybe it is the GL interface which is the culprit?

Perhaps; this might give us a clue. I know you've probably already done this, but are your drivers up-to-date?

In addition, @kekekeks might have some thoughts.

kekekeks commented 3 years ago

said libGLES is most likely ANGLE

trympet commented 3 years ago

Interesting.. I will definitely attempt to reproduce on more systems. My 2015 MBP has yet to run the gauntlet. It has a 2880x1800 screen and bootcamp, which should be sufficient to determine whether macos behaves differently than Windows -- if at all.

Perhaps; this might give us a clue. I know you've probably already done this, but are your drivers up-to-date?

You are right! I was not running the latest driver, however, updating (to 9684) made no difference. Also, the issue has persisted across reinstalls of Windows.

I ran the rendering demo built from master (81e49c329e5b8461a780c41e7be525aaae0b570b) on my XPS. Curiously, the reported FPS is equal to half the refresh rate.

Dedicated GPU at 4K 48hz: rendering-demo-dedicated-gpu@4k-48hz

Integrated GPU at 4K 48hz: rendering-demo-igpu@4k-48hz

Integrated GPU at 4K 60hz: rendering-demo-igpu@4k-60hz

Integrated GPU with smaller bounds show a stable 60 fps: image

SysInternals process explorer threads page for renderdemo.exe: image

trympet commented 2 years ago

@grokys, do you know of any way to benchmark the 2d rendering performance on these systems?

On my desktop (GTX 1080), I see nothing in Task manager, but without empirical data, it's hard to determine whether Avalonia rendering performance scales linearly with other 2d benchmarks.

ltetak commented 2 years ago

I can confirm this problem. To test this I opened Avalonia RenderDemo sample app or ControlCatalog sample app. With either one, I can see that the GPU usage (in Task Manager) is increasing linearly with the window size (with DPI scaling this is counted in raw pixels). After some threshold, FPS starts dropping from 60 to 30 and lower in my case.

The ultimate test is to update just one label 60 times per second (put some buttons and labels on the window but update only the label). This simple job uses 50% of my GPU (according to Task Manager) when the window is fullscreen on a 4K monitor. my CPU: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz

trympet commented 2 years ago

@ltetak Thanks for confirming! Maybe it could be a video memory issue? It could be thrashing the video memory of the igpu.

It could also be why hardware rendering provides no tangible benefit over software rendering (from my experience). The immediate renderer could be seeing improved performance, since it is not using layered rendering, so half (?) the vmem?

I have not been able to reproduce with any other applications aside from Avalonia. Sadly, I have not the slightest clue about how to trace this or do any further investigation to confirm or deny :/