ihhub / fheroes2

fheroes2 is a recreation of Heroes of Might and Magic II game engine.
https://ihhub.github.io/fheroes2/
GNU General Public License v2.0
2.58k stars 359 forks source link

A button to switch between software and hardware cursor rendering #6386

Open Branikolog opened 1 year ago

Branikolog commented 1 year ago

Preliminary checks

Describe the problem requiring a solution

image

image

Currently, the only way to switch between these two modes is to change .cfg file, which is not intuitive.

Describe the possible solution

I think we need to allow players to change between these two modes somewhere inside game settings, since there could appear questions about how to run game in "fake fullscreen" mode.

Additional info

No response

oleg-derevenetz commented 1 year ago

First of all, in certain circumstances you just not able to have the hardware cursor: SDL1 builds do not support it, systems with emulated cursor (such as Android devices and any devices with touch screens) do not support it, etc, etc. The hardware cursor support may even be removed entirely in future once we better optimize the rendering performance in certain cases. This is not a kind of setting that user should normally interact with, this is more a some sort of "emergency setting".

Branikolog commented 1 year ago

Hello, @oleg-derevenetz !

this is more a some sort of "emergency setting".

I completely agree with you, but we have an issue now, and don't provide a clear solution for players. The same story was with v-sync. Some users complain about rendering issues. We implemented a button within game to enable v-sync to get rid of problems, so any player can select the proper mode he satisfied. Currently some players and even streamers experience issues with running game fullscreen in dual-monitor setup. Lack of cursor scaling for windowed mode. Correct me, if I'm wrong, I'm not sure we have a documented description somewhere, that issues I've mentioned could be fixed by enabling the software mode. I'm quite sure, more than a half of our players don't even know about .cfg file we have for such purposes. :)

And again, correct me if I'm wrong, software mode doesn't need v-sync option enabled, which could reduce performance. I'm not sure, this was mentioned anywhere. Could we even disable v-sync for software on default or even at all?

ihhub commented 1 year ago

Hi @Branikolog and @oleg-derevenetz , to solve the problem with multiple monitors for some users we can enable cursor software rendering by default like we do for devices with touch support. I believe that this would solve most of issues for streamers. What do you think?

oleg-derevenetz commented 1 year ago

to solve the problem with multiple monitors for some users we can enable cursor software rendering by default like we do for devices with touch support.

Yes, we can. There should be no problems with software cursor unless they turn on the v-sync... or will aggressively move the mouse during the hero movement :)

Branikolog commented 1 year ago

@ihhub

Hi @Branikolog and @oleg-derevenetz , to solve the problem with multiple monitors for some users we can enable cursor software rendering by default like we do for devices with touch support. I believe that this would solve most of issues for streamers. What do you think?

Unless they are agreed to have mess on their desktop, but to have properly rendered sprites in a true fullscreen mode. Anyway, I think we should show to the average player a possibility to select and use the mode he likes.

@oleg-derevenetz

There should be no problems with software cursor unless they turn on the v-sync... or will aggressively move the mouse during the hero movement :)

Does enabling v-sync makes sense for "software mode" at any circumstances? Can we disable it for this mode?

oleg-derevenetz commented 1 year ago

@Branikolog

Unless they are agreed to have mess on their desktop, but to have properly rendered sprites in a true fullscreen mode.

The "true fullscreen" mode is not used to get some "properly rendered sprites". The main problem with the hardware cursor is that it does not scale its image depending on the resolution of the game if the game is not running in that "true fullscreen" mode, so it may be too big in comparison with in-game objects - depending on the in-game resolution. With software cursor we don't need that "true fullscreen" at all, because software cursor always scales properly.

Does enabling v-sync makes sense for "software mode" at any circumstances? Can we disable it for this mode?

V-sync is always makes sense, because it prevents the well-known issue on some systems called "image tearing" when we in some moments have a part of the old frame together with a part of a new frame on the screen. Consider:

  1. We have an "old frame" on the screen;
  2. Game begins to render a "new frame" and already rendered it a halfway;
  3. Monitor updates its picture in a middle of the process;
  4. User for a brief moment sees both a half of an "old frame" and a half of a "new frame" on the screen;
  5. Game finishes the render of a "new frame";
  6. After a few milliseconds monitor updates its picture again and now user sees the "new frame" as a whole.

It may be especially noticeable when a big parts of a screen updated at once - for example when scrolling the adventure map on large resolutions. What it really does is synchronize the rendering with the monitor refresh rate, i.e. you cannot render your frames more often than the monitor's refresh rate (usually ~60 Hz). Hardware cursor is not affected by this, because it is drawn by the system, but the software cursor is drawn by the same machinery as all other game GUI, so it can't be drawn more often than 60 times per second (if monitor's refresh rate is 60 Hz), so you will notice that it moves more "jerkily", it may disappear in one place for a moment and then appear in another place instead of moving more smoothly, it becomes more difficult to follow the cursor with the eye. So with v-sync the hardware cursor is still preferred.

oleg-derevenetz commented 1 year ago

I just tested the latest snapshot on the Windows PC, it works just fine with software cursor both with v-sync and without v-sync in 1920x1080. I hope that on weaker machines this is also the case. Some rendering optimizations have been done recently.

zenseii commented 1 year ago

I tested the latest snapshot on a SDL2 release build using an 11 years old Windows 8 laptop with a Pentium 2,4 GHZ CPU and there were no performance issues with the software cursor on and V-Sync both on and off. Tested this in the Slugfest map and had the AI move at 9 speed while spinning the cursor around quickly. The map was completely discovered.

oleg-derevenetz commented 1 year ago

@zenseii what in-game resolution did you set?

zenseii commented 1 year ago

@zenseii what in-game resolution did you set?

The max for this laptop's screen which is 1366x768. I can connect it to a TV and see if it can handle 1080p later.

zenseii commented 1 year ago

@zenseii what in-game resolution did you set?

On 1080p I get 40 fps when I try my best to reduce it on the adventure map. It doesn't matter if V Sync is on.

Branikolog commented 1 year ago

Hi, @oleg-derevenetz

The "true fullscreen" mode is not used to get some "properly rendered sprites". The main problem with the hardware cursor is that it does not scale its image depending on the resolution of the game if the game is not running in that "true fullscreen" mode, so it may be too big in comparison with in-game objects - depending on the in-game resolution. With software cursor we don't need that "true fullscreen" at all, because software cursor always scales properly.

For me, hardware cursor option provides a much better image. If you check the image with software cursor rendering, it is blurry on all resolutions lower than native of your screen. That's the only issue I don't love in this mode.

V-sync is always makes sense, because it prevents the well-known issue on some systems called "image tearing" when we in some moments have a part of the old frame together with a part of a new frame on the screen.

Yes, I know the issue, since v-sync fixed the problem for me. The problem, which I was noticing for so long on all my devices. (I think, it was since the beginning of my participation in the project...) When software mode appeared, the problem never occured, no matter was enabled or not the v-sync option. That's why I actually asked a question. I simply thought, software mode make that visual flaws irrelevant.

Hi, @zenseii !

there were no performance issues with the software cursor on and V-Sync both on and off.

Curious thing... With v-sync turned off I experience noticeable slow down of hero movement, while cursor moving on my laptop. With v-sync enabled everything works well, as it can on my not so powerful laptop on fHD resolution.

Branikolog commented 1 year ago

Btw, @zenseii , @oleg-derevenetz While running with software mode on maximum resolution do you experience problems of screen moving with your cursor? While my cursor is touching the most edge of the screen, scrolling arrow disappears showing normal game cursor and scrolling stops.

Also, in software rendering mode cursor movement is a little bit... Soft... Or lagging a bit...? Hardware mode has a bit more precise and sharp cursor movement for me, comparing to software. Did you noticed any differences?

oleg-derevenetz commented 1 year ago

@Branikolog

While running with software mode on maximum resolution do you experience problems of screen moving with your cursor? While my cursor is touching the most edge of the screen, scrolling arrow disappears showing normal game cursor and scrolling stops.

No, I don't see this effect. I believe you are running the game in fullscreen (because the game doesn't fit on the screen in windowed maximum resolution mode due to the window title)?

Also, in software rendering mode cursor movement is a little bit... Soft... Or lagging a bit...? Hardware mode has a bit more precise and sharp cursor movement for me, comparing to software. Did you noticed any differences?

Of course there will be differences, especially on not-so-fast hardware. With hardware cursor, the system redraws this cursor independently of the game itself. With software cursor, the cursor will not be redrawn if the game is currently busy with something else - for example, when it redraws all animations on the big screen with fully revealed map. First, it will completely redraw, say, the next frame of animation of all map objects, and only then it will redraw the cursor that has moved. The game may not even redraw the cursor at all for a (relatively) long time - for example, during the complex calculations on the AI turn.

oleg-derevenetz commented 1 year ago

For me, hardware cursor option provides a much better image. If you check the image with software cursor rendering, it is blurry on all resolutions lower than native of your screen. That's the only issue I don't love in this mode.

We can also always use the "non-true fullscreen mode", even with hardware cursor. But in this case there will be an issue with cursor scaling - here is the video of game with 640x480 in-game resolution running in the "non-true fullscreen mode" on screen with native resolution 1920x1080 with HARDWARE cursor:

https://user-images.githubusercontent.com/32623900/208317974-e2da945b-61aa-41c0-963f-208219d284a7.mp4

and here is the game running in the same conditions but with SOFTWARE cursor:

https://user-images.githubusercontent.com/32623900/208317994-32b0f92e-49ba-4d20-a0f2-3f9cc869c2cf.mp4

Branikolog commented 1 year ago

@oleg-derevenetz

Of course there will be differences, especially on not-so-fast hardware.

Oh, I've thought, I have paranoia regarding cursor rendering. So, if slow downs are inevitable, then I won't complain anymore. :)

I believe you are running the game in fullscreen

Definitely, I ran it fullscreen. :) Strangely, I can't reproduce it now, in fullscreen and even windowed mode... Probably my OS is showing some tricks...

Branikolog commented 1 year ago

@oleg-derevenetz

We can also always use the "non-true fullscreen mode", even with hardware cursor. But in this case there will be an issue with cursor scaling - here is the video of game with 640x480 in-game resolution running in the "non-true fullscreen mode" on screen with native resolution 1920x1080 with HARDWARE cursor:

I know the problem with cursor scaling. But it's actual for windowed mode only. Both these modes show cursor well in fullscreen mode.

I was talking about image quality above. Here I've made a screenshots of fheroes2 running fullscreen in hardware mode first on minimum and maximum resolution, and then in software mode. You can zoom and compare the images. I'm not sure, they will be shown correctly here, so you can download them and open in your image viewer (I hope, it won't filter mages as well. :) I think, "Paint" will show everything correctly. ) Resolution screenshots.zip

Hardware 640x480: 640x480 hardware mini

Hardware 1600x900: 1600x900 hardware mini

Software 640x480: 640x480 software crop

Software 1600x900: 1600x900 software crop

In software fullscreen mode only maximum resolution shows image without scaling or filtering. All other resolutions become smooth and blurry. It's barely noticeable, while playing on small size devices, like phones. But on large monitors it's quite visible. That's why I still prefer using hardware mode.

oleg-derevenetz commented 1 year ago

@Branikolog

Both these modes show cursor well in fullscreen mode.

Nope. To show the hardware cursor well in the fullscreen mode, you will need to use the "true fullscreen" which may be incompatible with multiple monitors. What happens if you use a hardware cursor in the "non-true fullscreen" (which is compatible with multiple monitors), I showed in the first video above. There is no problem to use hardware cursor with the "non-true fullscreen", except the cursor scaling.

oleg-derevenetz commented 1 year ago

@Branikolog so basically the options are as follows:

  1. True fullscreen + hardware cursor (how does it work now if the software cursor setting is turned off). Everything is fine except the issues with multiple monitors;
  2. Non-true fullscreen + software cursor (how does it work now if the software cursor setting is turned on). Cursor and screen image are blurry (can be sort of regulated by the "screen scaling type" option in the fheroes2.cfg), there may be performance issues with software cursor, BUT there are no issues with multiple monitors;
  3. Non-true fullscreen + hardware cursor (as on my first video above). Screen image is still blurry on lower resolutions, cursor is too small on lower resolutions, but no performance issues with cursor and no issues with multiple monitors;
  4. True fullscreen + software cursor. Screen image is fine, but there are potential issues with cursor performance as well as issues with multiple monitors. The most useless option :)
Branikolog commented 1 year ago

@oleg-derevenetz

  1. True fullscreen + hardware cursor (how does it work now if the software cursor setting is turned off). Everything is fine except the issues with multiple monitors;
  2. Non-true fullscreen + software cursor (how does it work now if the software cursor setting is turned on). Cursor and screen image are blurry (can be sort of regulated by the "screen scaling type" option in the fheroes2.cfg), there may be performance issues with software cursor, BUT there are no issues with multiple monitors;
  3. Non-true fullscreen + hardware cursor (as on my first video above). Screen image is still blurry on lower resolutions, cursor is too small on lower resolutions, but no performance issues with cursor and no issues with multiple monitors;
  4. True fullscreen + software cursor. Screen image is fine, but there are potential issues with cursor performance as well as issues with multiple monitors. The most useless option :)

I absolutely agree, that we don't have a universal solution, which fixes all our issues. That's why I've created this thread, so players could easily switch between 1 and 2 (obviously the most relevant solutions), depending on their circumstances and the result they want to achieve, and also understand, consequences of their choice by checking the info. Currently, the majority of players didn't even open .cfg file and even so, I'm not sure they understand the meaning of those "software" options. Enabling software mode on default for dual monitors set up can confuse players with lower image quality without explanation of why it has happened.

oleg-derevenetz commented 1 year ago

@Branikolog the problem with this setting is that it is not universal. It will not work on Android. It will not work on Windows devices with touch screen. It will not work on game consoles. People will either complain that this setting does not work, or, if we disable it in certain configurations, people will complain "why it is disabled, I want my hardware cursor".

Branikolog commented 1 year ago

Hi, @oleg-derevenetz I think, a user could easily accept, that some options are unavailable on his device. If it's possible, we can even not show the option for such versions, so user would never know it existed and play the game, as it runs (the only way it can run on a particular device, as you've mentioned) without bothering. On the other hand, if the potential choice is available for a user, it would be nice to tell him: "You can use this mode and get xxx, or you can use that mode and get yyy." Okay, if it's not possible, to distinguish the device and OS specifications to make a visual representation of the available choice in game, can we at least mention somewhere, that user has a certain choice on certain circumstances. Inside readme file or smth like that?. So, at least 10% of users, who read "readme" would know, that they could switch back to hardware mode with their two monitor setup, and experience some issues with the second monitor, but enjoy the best quality picture, they can get. ;)

lexa2 commented 1 year ago

Hi dear all, sorry for jumping into the topic but I believe I can provide one more argument into making hw/sw cursor choice an option in the game GUI. With recent release of 1.0 and ongoing holidays I decided to jump onto the nostalgia train and play some HoMM2/fheroes2. I run Windows 10 x64 with dual-monitor setup and one monitor being a 144Hz G-Sync monitor. Things worked more or less great but I was really disappointed by the fact that mouse cursor movements were jerky updating at a rate of like 20-30 FPS at best. No matter the display resolution I choose, no matter if playing fullscreen or in windowed mode - as soon as fheroes2 was the foreground process mouse cursor movements were jumping from silk-smooth in other programs to a jerky show in fheroes2. At the same time I noticed that this jerkiness was disappearing during screen scrolling.

Having no reason to believe that it's "only me" and devs and other players are OK with having suck a jerky mouse cursor movements I came here to Issues page and quick search brought me to this very issue. Thanks to the topic starter I became aware that there's an option to select between hw and sw cursor rendering in fheroes2.cfg. I had tried it and found out that enabling sw cursor totally fixes the jerky mouse cursor movement problem for me. Enabling FPS counter revealed that moving cursor in sw mode resulted in FPS jumping to the ~144 which is to be expected having 144Hz monitor and v-sync enabled. Contrary in hw cursor mode FPS was staying at around 20 no matter the cursor movements with exception of screen scrolling where it was jumping to around 60-70.

This behavior led me to believe that G-Sync might be a main suspect for my case - and it was. As soon as I turned off G-SYNC there was no jerky hw cursor movement anymore. Turning G-SYNC back on brought the problem back.

TL/DR here is that unless there is a way to somehow enforce G-SYNC screen updates triggered by hw cursor motion the only options to have a smoothly moving cursor for G-SYNC monitor owners are either to disable G-SYNC each time they want to play fheroes2 or to switch into using sw cursor. It might be better to have sw cursor option exposed in GUI as it is somewhat inconvenient to trigger display drivers system-wide setting back and forth just to play a single game.

oleg-derevenetz commented 1 year ago

@lexa2

Enabling FPS counter revealed that moving cursor in sw mode resulted in FPS jumping to the ~144 which is to be expected having 144Hz monitor and v-sync enabled. Contrary in hw cursor mode FPS was staying at around 20 no matter the cursor movements with exception of screen scrolling where it was jumping to around 60-70.

Please note that in-game FPS counter is not able to display the frequency of "hardware" cursor updates because this cursor is redrawn by the system and not by the game itself (unlike the software cursor, which is redrawn by the game engine). These 20 FPS are caused by various animations on the adventure map, and not by the "hardware" cursor.

lexa2 commented 1 year ago

These 20 FPS are caused by various animations on the adventure map, and not by the "hardware" cursor.

Yeah, that's was exactly my point. HW cursor updates does not trigger hardware G-SYNC frame redraw signal to the monitor it seems. For my monitor minimal G-SYNC refresh rate is 15 which means that having game animations updated at ~20FPS results in real life monitor update rate of 20Hz. It results in perceived jerky mouse cursor movement causing a lot of discomfort.

Switching to SW cursor is a workaround for this case. SW cursor being a part of the game-generated framebuffer (SDL surface/bitmap/whatever) results in G-SYNC updates triggered each time game updates the cursor position, redraws and performs a front/back buffer flip. With my mouse having an update rate of 300 and monitor running at 144Hz it results in ~144FPS during mouse cursor movement translating into "silky-smooth" end-user experience. Obvious downside is the excessive system resources load spent on producing these 144 FPS just to reflect mouse cursor movements. It is not that bad for my case as my rig is pretty fast but might be a problem for someone with low end spec PC.