Glxgears stutters when started with prime-run and when I output to only the external monitor

Hi, I have a Lenovo Legion Y540-15IRH 81SX 81SX0008MX laptop which I have installed Linux Arch on. The laptop uses the Optimus technology and has a NVIDIA GeForce GTX 1660 Ti Mobile discrete GPU as well as an Intel CoffeeLake-H GT2 [UHD Graphics 630] integrated GPU.

I have an issue where if I run glxgears with prime-run using the command prime-run glxgears and if I output to my external monitor only, then glxgears will stutter. If I output to only my laptop monitor or to both monitors, then there is no stutter when using prime-run.

I have disabled Vertical Synchronization (VSYNC) for my Intel graphics as described here. Without doing this I experience stutter when running glxgears even without using prime-run, whenever I output to my external monitor only. Without disabling VSYNC I also experience lag when starting Google Chrome and outputting to my external monitor only. Google Chrome does however not have an issue when VSYNC is disabled and starting it with prime-run like glxgears has.

The problem with stuttering only happens when I have set my laptop to use switchable graphics in the UEFI. If I set it to discrete graphics (using the NVIDIA GPU only) then there is no stutter. Likewise I do not get any stutter if I use switchable graphics but configure the proprietary NVIDIA driver to be the primary rendering provider as described here.

I am experiencing the same issues with stuttering with the program projectM.

Here is a table with the tests I have done:

Graphics Configure NVIDIA driver as primary? Monitor Disable VSYNC? Start with prime-run? glxgears stutters?
switchable no external yes no no
switchable no external yes yes yes
switchable no external no no yes
switchable no external no yes yes
switchable no both yes no no
switchable no both yes yes no
switchable no both no no no
switchable no both no yes no
switchable no laptop yes no no
switchable no laptop yes yes no
switchable no laptop no no no
switchable no laptop no yes no
discrete no external yes no no
discrete no external yes yes no
discrete no external no no no
discrete no external no yes no
discrete no both yes no no
discrete no both yes yes no
discrete no both no no no
discrete no both no yes no
discrete no laptop yes no no
discrete no laptop yes yes no
discrete no laptop no no no
discrete no laptop no yes no
switchable yes external yes no no
switchable yes external yes yes no
switchable yes external no no no
switchable yes external no yes no
switchable yes both yes no no
switchable yes both yes yes no
switchable yes both no no no
switchable yes both no yes no
switchable yes laptop yes no no
switchable yes laptop yes yes no
switchable yes laptop no no no
switchable yes laptop no yes no

This rather sounds like a compositor issue if you even have to disable vsync on plain intel.
Which DE are you using? Do you have prime sync enabled? Please run nvidia-bug-report.sh as root and attach the resulting nvidia-bug-report.log.gz file to your post.

Forgot: glxgears fullscreen or windowed?

I am just using the window manager wmderland and the compositor picom. I am experiencing the issue even if I start an X session with only xterm (that is without the window manager and the compositor) and running glxgears from xterm. My tests were done with glxgears windowed, but running it in fullscreen does not seem to make a difference. Prime sync seems to be enabled. It reads PRIME Synchronization: 1 on all external outputs when running the command xrandr --prop.

Here are the logs:

Both logs were generated from a X session where my window manager and compositor were running. Glxgears was started in windowed mode.

In this log glxgears was started using prime-run and VSYNC was disabled:
nvidia-bug-report_prime_run_yes_vsync_no.log.gz (261.9 KB)

In this log glxgears was started without prime-run and VSYNC was enabled:
nvidia-bug-report_prime-run_no_vsync_yes.log.gz (259.0 KB)

In both cases glxgears stutters.

Can somebody please help with this?