Skip to content

Mysteriously poor performance on macOS #7644

Closed

Description

@nielsmh

Version of OpenTTD

1.9.0 and later, at least

Expected result

All operating systems have comparable performance when running on comparable hardware.

Actual result

Several people are reporting that the macOS version of OpenTTD has unreasonably poor performance, and the framerate window does not reveal any obvious reason.

This bug covers the situation when all of the following occur at the same time:

  • Game is running on macOS
  • Frame rate on a new game (256x256) with no mods is below 30 fps (typically even below 10 fps)
  • Sum of "Game loop", "Drawing" and "Video output" times in the Frame rate window is below 10 ms
  • Toggling "Full animation" does not have a significant impact on performance

Reported on TT-Forums and this comment on bug #7247.

Note that this is not related to #7247, as this bug manifests even when there are zero vehicles in game. The poor performance is also caused by something not being measured, since the sum of the game loop, drawing, and video output times do not add up to the time it should to cause the total frame rate.

Steps to reproduce

Run official OpenTTD builds on a macOS 10.14 system.

Activity

  1. added
    bugSomething isn't working
    OS: MacOSThis issue is related to a Mac OS problem
    on Jul 8, 2019
  2. andythenorth commented on Jul 8, 2019

    @andythenorth
    Contributor

    This manifests for me on Mojave (10.14) and did not occur on High Sierra (10.13).

    Notably:

    • FFWD was insanely fast on 10.13 following OpenTTD improvements in 2018-2019
    • FFWD barely works on 10.14
    • on a small game (2 AIs, 1500 vehicles total, 512x256 map), performance is dropping to 16 FPS, and one CPU core is pegged at 100% by OpenTTD
  3. nielsmh commented on Jul 8, 2019

    @nielsmh
    ContributorAuthor

    Some additional notes:

    • The total frame time budget in OpenTTD is 30 ms. The target frame rate is 33.33 fps, which is reached by having on average 30 ms between each frame.
    • This means that as long as the total of game loop (time taken simulating the game world), drawing (time producing the graphics for display on screen), and video output (time taken copying the produced graphics to the OS display driver) is less than 30 ms, the frame rate should be roughly 33.33 fps, when not in fast-forward mode.
    • Converting frame rate to frame time: ft = 1000 / fps
    • Converting frame time to frame rate: fps = 1000 / ft
    • When not in fast-forward mode, the OpenTTD "video driver" component inserts delays between frames to reach an average of 30 ms frame time.
    • When fast-forward mode is enabled, these delays are not inserted and instead the game is simulated and drawn as fast as the computer can handle.

    Example, taken from times measured on my Windows system running 1.9.2 64 bit:

    • Frame rate on the title screen is about 32.96 fps, so the frame time is about 30.33 ms
    • The game loop, drawing, and video output times are 0.50 ms, 2.62 ms, 2.50 ms, the sum is 5.62 ms, so if I could enable fast-forward on the title screen, the expected rate would be at least 177.94 fps.
    • The actual rate in fast-forward mode can be higher, since a recent change allows multiple game ticks to be simulated between each draw, so the total game loop rate might end up closer to 2000 fps.

    The frame rate window (and fps console command) also display a line for Sound mixing, this is generally not relevant, since sound mixing is run in a separate thread. It should never affect total frame rate. (It could get relevant if someone reported crackling/popping sound.)

  4. wousser commented on Jul 9, 2019

    @wousser

    As requested:

    ProductName:	Mac OS X
    ProductVersion:	10.14.5
    BuildVersion:	18F132
    

    1.8.0
    No issues
    Both with Full Animation Enabled or Disabled the game runs smoothly. (No fps command in 1.8.0)

    1.9.0
    Full animation Enabled:

    Game loop rate: 14.94fps (expected 33.33fps)
    Drawing rate: 14.96fps (expected 33.33fps)
    Video output rate: 35.99fps (expected 60.00fps)
    

    Full animation Disabled:

    Game loop rate: 29.18fps (expected 33.33fps)
    Drawing rate: 29.28fps (expected 33.33fps)
    Video output rate: 155.36fps (expected 60.00fps)
    

    1.9.2
    Full animation Enabled:

    Game loop rate: 14.70fps (expected 33.33fps)
    Drawing rate: 14.66fps (expected 33.33fps)
    Video output rate: 54.38fps (expected 60.00fps)
    

    Full animation Disabled:

    Game loop rate: 30.87fps (expected 33.33fps)
    Drawing rate: 30.88fps (expected 33.33fps)
    Video output rate: 195.84fps (expected 60.00fps)
    

    I'd like to add that this only occurs when playing full-screen or full window. If I reduce the window size to 1/4 of the screen, the FPS increases to ~30.

  5. drebroff commented on Jul 12, 2019

    @drebroff

    Confirm
    ProductName: Mac OS X
    ProductVersion: 10.14.5

    MacBook Pro 15 2017

  6. AbeWJS commented on Aug 16, 2019

    @AbeWJS

    OpenTTD is causing color space / pixel format conversion in the background. Likely due to mismatch of backing surface format chosen by OpenTTD and native surface format of the display/OS.

    The slowdown scales with OpenTTD resolution and Mac machine's high resolution exacerbates the problem. This format/color space conversion happens on OpenTTDs main thread after a call to CGContextDrawImageWithOptions.

    I don't have time to look at this in the source, but that is where the problem comes from. Perhaps these pointers are enough information?

  7. andythenorth commented on Aug 16, 2019

    @andythenorth
    Contributor

    I don't have any skills in Mac debugging, but I used Apple instructions to set up an Instruments run, long paste of results here: https://paste.openttdcoop.org/pef0g4kgn/mnd7ty/raw

    TL;DR

    • 98.5% of time is spent in OTTD_QuartzView drawRect
    • digging down, 29% of the time is spent in RGBAf16_sample_RGBAf_inner

    A cursory google search turns up a few other people with similar issue, nothing conclusive

    Apple developer guide is here: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaViewsGuide/Optimizing/Optimizing.html#//apple_ref/doc/uid/TP40002978-CH11-SW4

    My results are for macOS 10.14, on a 2.7Ghz i7

  8. nielsmh commented on Aug 16, 2019

    @nielsmh
    ContributorAuthor

    So if the rendering was changed to use explicit GPU acceleration instead of blitting 2D surfaces, it might resolve the issue? Since the GPU could receive texture data in our native pixel format, and a shader could convert it as necessary.

  9. SoothedTau commented on Aug 16, 2019

    @SoothedTau

    @nielsmh

    You could indeed switch to a MTKView (or other Metal view) and use Metal to do the transfer while having a shader convert the image to the final format. This would work just fine.

    Though I don't think you have to do it via Metal as it should be possible to set things up correctly without requiring Metal. This new behaviour likely has to do with changes to Layer Backed Views introduced in 10.14 SDK, I'll see if I can find out what exactly introduces the observed behavior.

    In the end it's just a choice but I could imagine not wanting to introduce a new dependency if it's not necessary.

  10. nielsmh commented on Aug 16, 2019

    @nielsmh
    ContributorAuthor

    I think we definitely want to support macOS versions as far back as reasonably possible, at least not having hard dependencies on newer features.

  11. faeton commented on Aug 21, 2019

    @faeton

    Just wanted to confirm here that the issue is important and in place. Tested on 10.14 and 10.13 both showing bad results, low fps, mouse pointer lags.

  12. SoothedTau commented on Aug 22, 2019

    @SoothedTau

    Trying to track this down but having issues getting a build working. Built master repo without any compression libs, build succeeded, but when executing I immediately encounter an error in libdyld: stack_not_16_byte_aligned.

    Probably missing some steps getting the build running. Can dig further if someone can help me out here :)

  13. DIYglenn commented on Aug 23, 2019

    @DIYglenn

    Anything I can test?
    If anything I can at least confirm the issue on a 5K iMac 2019 - 1.9.2 is unplayable, 1.8.0 seems to work as normal (but slower with 1440p resolution, which might actually be twice the resolution due to Retina)

  14. 101 remaining items

  15. msikma commented on Mar 31, 2020

    @msikma

    I see this issue just got closed, but I don't understand why. I tested this fix and it didn't work for me. The last commit that got added is identical to the one I tested. Has anyone else tested it?

  16. andythenorth commented on Mar 31, 2020

    @andythenorth
    Contributor

    I commented on the pull request here #8023 (comment)

    TL;DR. For me:

    • frame rate is now higher
    • graphics look worse
  17. added a commit that references this issue on Mar 31, 2020
    a4d73d7
  18. msikma commented on Mar 31, 2020

    @msikma

    That's weird, because the patch definitely did not bring back 1.8.0 performance for me, to the point where I could tell with the naked eye easily. Did anybody else test it? And did they get the same performance as 1.8.0 or simply "better" performance without being too specific? I'm sorry if I sound a bit disappointed. I can log the difference in FPS to get hard numbers for this.

    The difference in colors in the screenshot is an example of colors being interpreted as the wrong profile. The screenshot has "Display P3" as the embedded profile which is the user's system profile. Here's what happens when I take the left half of the image into Photoshop, convert it to sRGB, and then assign it the Display P3 profile without converting the color values:

    display-p3-2

    They look identical again (minus some loss from the conversion roundtrip). The color differences could be greater or smaller than that of the user who posted the original image, depending on what color profile they use.

  19. spauka commented on Apr 2, 2020

    @spauka
    Contributor

    FWIW I think it might be worth reopening this issue, or opening a new issue regarding the colorspace issue and continuing performance problems on OSX.

    The patch above makes the game playable but it does seem to fix all the issues that have been raised...

  20. nielsmh commented on Apr 3, 2020

    @nielsmh
    ContributorAuthor

    I think it would be a better use of time to work on an OpenGL based renderer which would most likely avoid all these issues.

  21. andythenorth commented on Apr 3, 2020

    @andythenorth
    Contributor

    I am happy in principle to have this or another issue open.

    It would be helpful though to have quantified the issues. It's unclear whether there is one mac performance issue or several.

    Apple is pretty much the only vendor where we can rely on there being 2 OS versions and only a handful of hardware versions :)

    It should be possible to categorise and reproduce the issues more reliably. :) Currently we lack consistency, e.g. a common set of FPS numbers on a common set of savegames (or something like that).

  22. nielsmh commented on Apr 3, 2020

    @nielsmh
    ContributorAuthor

    Ideas for what might be a useful test:

    • It needs to be repeatable.
    • It needs to generate clear diagnostics and results.
    • Testers need to accurately report hardware version, software versions, and display configuration the game is running on.
    • Need to use a known config file for the game so settings like resolution, blitter, news messages, and full animation don't affect different testers' results differently.
    • Probably involves running the game with the dummy video driver on a known savegame, for some number of ticks, to get the hardware's simulation rate capability when no graphics is involved. Then compare loading the same savegame in graphical mode and look at performance numbers.
  23. SoothedTau commented on Apr 3, 2020

    @SoothedTau

    I think it would be a better use of time to work on an OpenGL based renderer which would most likely avoid all these issues.

    OpenGL is deprecated on macOS. I wouldn't recommend developing new features with it.

  24. msikma commented on Apr 6, 2020

    @msikma

    It would be helpful though to have quantified the issues. It's unclear whether there is one mac performance issue or several.

    You're absolutely right.

    I'm sorry to say I tried to get some numbers on this but I haven't quite gotten them yet, because I tried to compile 1.8.0 to have a baseline and somehow that one doesn't run well on my system either (the official release does). It ran basically like the 1.9.0 release. So I'm not sure what's going on. I need to investigate this further.

    I'd love to help out in some way so I'll be looking into this when I've got some more time. 🙂

  25. michicc commented on Apr 6, 2020

    @michicc
    Member

    Well, the official 1.8.0 release was build using a very old cross-compiler using and targeting the 10.6 (or maybe even 10.5) SDK. The current builds are done with a real native compiler on Azure infrastructure, using a SDK 10.10+ (not sure which exact versions Azure provides).

    It it entirely possible that macOS exhibits different app compatibility changes depending on the OS version the app was compiled against.

  26. msikma commented on Apr 8, 2020

    @msikma

    Yeah, that's what I was thinking too. Could it be that this bug is really a result of SDK changes?

    I'll look into compiling with an older SDK to see what happens.

  27. added 3 commits that reference this issue on Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS: MacOSThis issue is related to a Mac OS problembugSomething isn't workingregressionIt used to work, and now it's broken.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions