patriciogonzalezvivo / thebookofshaders

Step-by-step guide through the abstract and complex universe of Fragment Shaders.
http://TheBookOfShaders.com
Other
5.97k stars 680 forks source link

Shaping functions: first example uses undefined behavior of smoothstep() #344

Open kevinms opened 3 years ago

kevinms commented 3 years ago

The first code example in the Shaping functions chapter has the following plot function:

float plot(vec2 st) {    
    return smoothstep(0.02, 0.0, abs(st.y - st.x));
}

Clicking on smoothstep() takes you to the glossary which states:

float smoothstep(float edge0, float edge1, float x) ... Results are undefined if edge0 ≥ edge1.

Note the first argument (0.02) is greater the second (0.0), which appears to be undefined. All the Khronos API references I could find say the same. However, also note the example shader does render properly on my machine.

timohausmann commented 3 years ago

I agree the "twisted" parameters should be explained in the book, the docs make it look like it's against the rules. However it's frequently used, seems like an inversion, similar to 1.0 - smoothstep(0.0, 0.02 ...). (Example)