Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SOLVED! - Differences in speed between DEBUG and RELEASE builds on Win32  (Read 6627 times)

0 Members and 1 Guest are viewing this topic.

joeduf

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hello all,

I'm relatively new to SFML. I absolutely love the library so far. I have a question that has me a bit puzzled at the moment. I believe it is related to the Graphics subsystem but I'm not 100% sure.

I have a small Tetris-clone that, for all time / animation related entities, uses a clock object, sf::Time, as the asSeconds() method to determine how "far" to correctly move / increment certain game pieces a la movement timers, starfield background, etc.

Since the time calculations are the same for DEBUG and RELEASE versions why does the DEBUG version run smoothly, without "hiccups" for lack of better description compare to the RELEASE version which you can clearly see starfield "stuttering" etc.

I thought the release version should run smoother / faster, yet it appears to be far worse.

I don't understand. Looking to deploy the release version to different machines but hesitant because of the performance glitches. Any ideas?

I'm not a fantastic programmer. Is there any way I can profile functions using Visual Studio 2017 Prof to "learn / discover" what's going on?!?

Thanks for any help you can provide.
« Last Edit: August 29, 2017, 09:01:54 pm by joeduf »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #1 on: August 25, 2017, 12:25:59 am »
The release mode is probably"too fast", thus you only get very small changes between frames, which leads to subpixel position, which again leads to OpenGL having to interpolate the actual position on a per pixel basis, which then leads to that the interpolation might end up rounding down for one frame and rounding up in the next frame.

What you want to do is still use a float for positioning, but before rendering round the position up or down to interger pixel positions.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HughPH

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #2 on: August 25, 2017, 12:30:20 am »
Wow, OK, so this could be a difficult one to untangle.

The first thing is that SFML is not building when you build your code. You're using a library that's already built - so SFML is *always* in Release mode.

That means it's probably your code. And it would DEFINITELY be a good idea to test it out on different machines in both Release and Debug modes.

If you haven't already, it's worth getting SFML to use VSYNC, or force it to 60FPS (or whatever FPS you will find acceptable.) Before you publish, you'll want to consider whether to support machines that are too slow to run at 60fps - which could give you major headaches if your game logic is dependent on specific numbers of frames passing. 60fps is roughly 1 frame per 16.6667ms (or 1/60 of a second) so you want to either do all your processing in 16ms or force your framerate down to the number of milliseconds you need to do your inter-frame processing.

I'm interested to know - if you don't consider yourself a *fantastic* programmer, why do you use C++? C# is far more forgiving. You don't need to worry about allocating memory, freeing it, and all that. I'm a bloody amazing coder (although a really bad mathematician), and I prefer C#. And yes, it is definitely fast enough.

Now, I don't know what your code looks like, but if you're creating a new Time object frequently, that might be causing you problems. For keeping track of time, you're better getting the start time of your application then using Modulo (% operator) to get the current value of a hypothetical looping timer. Let's say a is always increasing, a % 100 will always be between 0 and 99, and will "roll over" to 0 when it reaches 100.

For moving stuff around I also prefer to 'outsource' the responsibility to a physics engine. I'm using Box2D/LiquidFun. Main issue I have at the moment is that it's single-threaded, and making it multithreaded would not be a straightforward project. So I just call Box2D in a separate thread. I have a semaphore joined to my vsync, so Box2D runs in parallel to my game logic. I've got a C# wrapper around a C++/CLI wrapper around Box2D with more semaphores so that stepping the world and modifying the world do not overlap. That's not to say that this is necessarily a "normal" way of doing things - you don't have to follow my example. I am not a professional game developer (yet).

The other thing you might consider is offloading your starfield to GLSL. Using a Stopwatch (or just counting milliseconds since your exe started) you can pass the number of elapsed milliseconds to a Fragment Shader, and let that deal with drawing a starfield. If it's a horizontal startfield you could "take inspiration from" these:
http://glslsandbox.com/e#41903.0
http://glslsandbox.com/e#41888.3

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #3 on: August 25, 2017, 06:47:35 am »
The first thing is that SFML is not building when you build your code. You're using a library that's already built - so SFML is *always* in Release mode.
There are debug builds that have to be used in debug mode
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HughPH

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #4 on: August 26, 2017, 01:02:08 pm »
There are debug builds that have to be used in debug mode

What do you mean by "have to be used"? How does it make a difference to the calling code? And how do you propose that this might work where people generally just download the libraries and call out to them? (CSMFL-2.4-windows-*.zip only contains one set of dll files)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #5 on: August 26, 2017, 01:17:31 pm »
The debug libraries link against the debug runtime libraries and the release libraries link against the release runtime libraries. They are not compatible with each other.

SFML provides debug and release versions, but that's mostly because we try to be very user-friendly. Most libraries you find out there don't provide any binaries at all and you're left with having to build from source.

I'm not sure how the runtime libraries work in C. CSFML's goal is to provide a C API to make lives easier when building bindings. If you need to debug code at the C level, you may have to rebuild CSFML yourself.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HughPH

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #6 on: August 28, 2017, 01:58:17 pm »
OK, but for your average Joe all of that is irrelevant.

[My Code] => [CSFML] => [Runtime Libraries]

My Code can be in Debug or Release, and still target the Release build of SFML (and by extension, the Other Libraries) - it does not require a Debug build of CSFML to run, and running in Debug won't break the integration with the Release build of CSFML.

And 99% (or more) of the time, that will be the use case.

I find not-providing-binaries to be typical of Linux-targeted libraries where the target machine could be running any version of Linux on any platform. Some of those also target Windows, but maintain the paradigm. Generally Windows-focused libraries come with or as binaries - Windows for RISC and Windows for PowerPC never really took off. (Largely because everything comes pre-compiled for x86!)

joeduf

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #7 on: August 29, 2017, 01:45:25 am »
Thanks for the reply! It's not sub-pixel positioning. Using floats and rounding already.  It's almost as if the double buffering is really "quad" (or maybe octo!?!) buffering and every 4th (or 8th or so) frame goes back to the "last" position / last render... a la smooth smooth smooth hiccup smooth smooth smooth etc

I'd built my own debug and release SFML libs with VStudio2017 so maybe the problem lies within that process itself or possibly within my release project settings.

I'll dig in more as soon as my day job allows! Love SFML - Fantastic work guys! Thanks again!

The release mode is probably"too fast", thus you only get very small changes between frames, which leads to subpixel position, which again leads to OpenGL having to interpolate the actual position on a per pixel basis, which then leads to that the interpolation might end up rounding down for one frame and rounding up in the next frame.

What you want to do is still use a float for positioning, but before rendering round the position up or down to interger pixel positions.

joeduf

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #8 on: August 29, 2017, 09:00:44 pm »
I found the simple solution. Visual Studio Compiler Optimizations getting in the way!

For my Release build I simply set the following options:

C/C++ -> Optimization -> Optimization = Disabled (/Od)

C/C++ -> Optimization -> Enable Intrinsic Functions = No

NOTE: These are the default values for a DEBUG build but not for a RELEASE build so I just changed the two settings and all my problems went away!

...Just in case this can help someone else!

The release mode is probably"too fast", thus you only get very small changes between frames, which leads to subpixel position, which again leads to OpenGL having to interpolate the actual position on a per pixel basis, which then leads to that the interpolation might end up rounding down for one frame and rounding up in the next frame.

What you want to do is still use a float for positioning, but before rendering round the position up or down to interger pixel positions.

jace10

  • Newbie
  • *
  • Posts: 11
    • View Profile
5 years later and this helped me, thanks for posting how you ended up fixing!

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: SOLVED! - Differences in speed between DEBUG and RELEASE builds on Win32
« Reply #10 on: September 01, 2023, 02:39:39 am »
My Code can be in Debug or Release, and still target the Release build of SFML (and by extension, the Other Libraries) - it does not require a Debug build of CSFML to run, and running in Debug won't break the integration with the Release build of CSFML.

The key thing here is CSFML, it's not SFML.
Usually using a release library with debug applications is ok (a lot of binary only libraries do this). The main cause of problems is having C++ features like STL in the interface. Debug and release versions of classes like std::string and std::vector are different sizes (debug adds in some extra safety stuff), meaning a debug app calling a release library with an std::string as a parameter will pass incorrect data.

For example, std::string on Visual Studio 2022 is 40 bytes in debug builds and 32 bytes in release. An iterator is 24 bytes in debug and 8 bytes in release.

CSFML uses a C interface for the libraries, so there's no STL as parameters/returns.