Question

I am writing a glsl fragment shader in which I use shadow mapping. Following this tutorial http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/ , I wrote this line to evaluate the shaodw bias to avoid the shadow acne

float bias = 0.005 * tan( acos ( N_L_dot ) );

But I know from math that

tan ( acos ( x ) = sqrt ( 1 - x^2 ) / x

Would it be faster to use such identity instead of tan and acos? In practice, to use this line of code

float bias = 0.005 * sqrt ( 1.f - N_L_dot * N_L_dot   ) / N_L_dot ;

I think my question is something like "Is the gpu faster at doing sqrt and divisions or tan and acos?" ...or am I missing something?

Was it helpful?

Solution

Using AMD GPU Shader Analyzer it showed that float bias = 0.005 * sqrt ( 1.f - N_L_dot * N_L_dot ) / N_L_dot ; Will generate fewer clock cycle instructions in the shader assembly ( 4 instructions estimating 4 clock cycles).

Where the float bias = 0.005 * tan( acos ( N_L_dot ) ); generated 15 instructions estimating 8 clock cycles to complete.

I ran the two different methods against the Radeon HD 6450 Assembly code. But the results seemed to track well for the different Radeon HD cards.

Looks like the sqrt method will generally perform better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top