division by zero @runtime does reshade handle it?

More
10 months 3 weeks ago - 10 months 3 weeks ago #1 by thrive4
First off the best of wishes for 2020!

I was wondering does reshade handle division by zero errors at runtime?
During compilation reshade has quite a number of ways to detect a division by zero
and when detected neatly suspends compilation and an error message with line number
is written to the log file.

However there are bound to be cases where it slips through detection during compilation
and I am curios how, or for that matter if, reshade handles those cases.

And just to up the ante *if* these occurrences are left to the relevant api (dx9, dx11, opengl, etc)
does it then effect performance gpu wise?
Last edit: 10 months 3 weeks ago by thrive4. Reason: grammar

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago - 10 months 3 weeks ago #2 by crosire
Floating point divison by zero has a defined value, which is infinity. So if you have a division by zero at runtime, the result is the value infinity (encoded as a special floating point value). Any further arithmetic on that value then usually results in either infinity or NaN (not a number). There are the "isnan" and "isfinite" intrinsics to check if a value is NaN or not infinity at runtime. None of this affects performance, since it is a normal operation with a defined result.
Last edit: 10 months 3 weeks ago by crosire.

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago - 10 months 3 weeks ago #3 by thrive4
So, is that handled by reshade internally or are you referring to the floating point IEEE 754 rules in general?
example d3d10:
docs.microsoft.com/en-us/windows/win32/d...esources-float-rules

Just to be clear does reshade write the occurrences to the log?

> None of this affects performance, since it is a normal operation with a defined result.
Both the occurance of a division by zero as the handeling of it by "isnan" and "isfinite"?
Last edit: 10 months 3 weeks ago by thrive4. Reason: quote

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago #4 by crosire
I'm refering to IEEE-754.
ReShade cannot write occurences to the log, since shaders run on the GPU + a division by zero is nothing special to the GPU, it's an arithmetic operation like any other. Because of that, both don't have a performance penalty.

Please Log in or Create an account to join the conversation.

More
10 months 3 weeks ago - 10 months 3 weeks ago #5 by thrive4
Great, thank you for clarifying.

Just to round things of I tried the suggested isnan and isfinite on a number
of supported api's (dx9, dx11 and opengl) with this simple shader:
float3 checkdivz(float3 colorinput)
{
    float testcolor = colorinput.x / colorinput.y;
    if (isnan(testcolor.x)){ colorinput = float3(1.0f, 0.0f, 0.0f); }
    if (isinf(testcolor.x)){ colorinput = float3(0.0f, 1.0f, 0.0f); }
    return colorinput;
}

At first I was a bit surprised to see alternating red or green pixels
showing up but after carefully reading the floating point rules (32bit)
'Divide by 0 produces +/- INF, except 0/0 which results in NaN'
it makes sense 0/0 results in a nan any other number divided by zero
results in an inf this occurred in all the tested api's.

Some thing else also became quite clear, that execution of the shader
is not suspended. This does mean that if 'testcolor' would be used further
in the code, with out trapping it, this could lead to an unpredictable result.

So the moral of the story is if one divides it is prudent to test otherwise
you might end up conquering less then expected. ;)

For reference:
docs.microsoft.com/en-us/windows/win32/d...esources-float-rules
docs.microsoft.com/en-us/windows/win32/d...floating-point-rules

Bye the bye isnan and isfinite seem to be implemented in later versions of reshade:
github.com/crosire/reshade/pull/32/commi...1b03d949173dd1d02a95

Plus a nice blog posting that goes a little more in depth:
sakibsaikia.github.io/graphics/2017/01/1...nting-Down-NaNs.html
Last edit: 10 months 3 weeks ago by thrive4. Reason: grammar

Please Log in or Create an account to join the conversation.