4

It is common knowledge that you should mask floating-point exceptions if you use OpenGL in your Win32 Delphi application.

In fact, the RTL more or less does this behind your back, because the OpenGL unit has the following initialization section:

SetExceptionMask([exInvalidOp..exPrecision]);

(That set is the universe.)

In addition, the 2022 documentation for this function states

You typically need to mask and unmask exceptions when you are interacting with external code such as TWebBrowser, OLEDB, .NET assembly, ActiveX controls, and OpenGL.

However, I strongly suspect this text has been essentially the same for 20 years or more. Apparently the RTL documentation on Set8087CW contained this already in early 1999:

For example, it is recommended that you disable all floating point exceptions when using OpenGL to render 3D graphics.

Today, this text has been rephrased slightly:

When using OpenGL to render 3D graphics, we recommend that you disable all floating-point exceptions for performance reasons.

Now, a lot has changed since the last millennium. When the 1999 text was written, people were using Windows 95, nVidia's GeForce had not been heard of yet, and everyone was using the fixed-function pipeline in OpenGL. Most programs probably sent a single vertex at a time to the driver! The fixed-function API was used for materials, lighting, fog, etc.

This is very far from modern OpenGL, where you write and compile your own GPU programs (shaders) and send large buffers to the video card's memory. The CPU isn't involved much at all.

So, my question is: Do you still need to mask floating-point exceptions in modern Win32 Delphi OpenGL applications?

Obviously, you may not want to change your application's floating-point handling just because you have added a few 3D features to it.

I have tried myself to re-enable floating point exceptions, and my modern Win32 Delphi OpenGL app seems to run just fine -- on my computer. But that doesn't prove that it is safe to do so. (Perhaps enabling these exceptions will make the app crash on 10% of all modern PCs?)

Unfortunately, I have not been able to find any authoritative and up-to-date information on this topic on the WWW. (It's almost like kids these days don't spend their free time writing OpenGL apps in Delphi.)

Related Q&As

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Kids these days almost don't even write classical Delphi apps at all! :-( – Delphi Coder Aug 23 '22 at 21:24
  • 1
    Frameworks. Everyone uses frameworks. Programs these days are just remixes of frameworks and understanding the actual processes vanishes more and more - that's also why masking/disabling exceptions is just done without actual explanation. – AmigoJack Aug 23 '22 at 22:43
  • 2
    "Do you still need to mask floating-point exceptions in modern Win32 Delphi OpenGL applications?" Yes. In fact this is true for pretty much all libraries that use floating point. Mask when you call, unmask when that call returns. – David Heffernan Aug 24 '22 at 07:55
  • @DavidHeffernan: Thank you. This comment of yours is likely the most clear and authoritative one on the subject since the turn of the millennium. It would be great if you could post that as an answer, so the WWW contains at least one clear A on this subject. – Andreas Rejbrand Aug 24 '22 at 08:26
  • Nothing has changed. And so long as the native toolsets (i.e. MSVC) continue to expect unmasked exceptions, this will never change. I don't think we need a new Q&A for this. – David Heffernan Aug 24 '22 at 09:56
  • 1
    @DavidHeffernan: Well, I'd agree if the documentation was a bit more clear. The current wording "When using OpenGL to render 3D graphics, we recommend that you disable all floating-point exceptions *for performance reasons*." almost makes the reader believe that it is safe to re-enable the exceptions if they see that the performance of their app is far from an issue. – Andreas Rejbrand Aug 24 '22 at 10:11

0 Answers0