@@ -159,41 +159,41 @@ struct BeckmannGenerateH
159159 vector3_type V = nbl::hlsl::normalize<vector3_type>(vector3_type (ax * localV.x, ay * localV.y, localV.z));
160160
161161 vector2_type slope;
162- if (V.z > 0.9999 )// V.z=NdotV=cosTheta in tangent space
162+ if (V.z > 0.9999 ) // V.z=NdotV=cosTheta in tangent space
163163 {
164- scalar_type r = sqrt<scalar_type>(- log<scalar_type> (1.0 - u.x));
165- scalar_type sinPhi = sin<scalar_type> (2.0 * numbers::pi<scalar_type> * u.y);
166- scalar_type cosPhi = cos<scalar_type> (2.0 * numbers::pi<scalar_type> * u.y);
167- slope = ( vector2_type)r * vector2_type (cosPhi,sinPhi);
164+ scalar_type r = hlsl:: sqrt (-hlsl:: log (1.0 - u.x));
165+ scalar_type sinPhi = hlsl:: sin (2.0 * numbers::pi<scalar_type> * u.y);
166+ scalar_type cosPhi = hlsl:: cos (2.0 * numbers::pi<scalar_type> * u.y);
167+ slope = hlsl::promote< vector2_type>(r) * vector2_type (cosPhi,sinPhi);
168168 }
169169 else
170170 {
171171 scalar_type cosTheta = V.z;
172- scalar_type sinTheta = sqrt<scalar_type> (1.0 - cosTheta * cosTheta);
172+ scalar_type sinTheta = hlsl:: sqrt (1.0 - cosTheta * cosTheta);
173173 scalar_type tanTheta = sinTheta / cosTheta;
174174 scalar_type cotTheta = 1.0 / tanTheta;
175175
176176 scalar_type a = -1.0 ;
177- scalar_type c = erf<scalar_type>(cosTheta );
178- scalar_type sample_x = max <scalar_type> (u.x, 1. 0e- 6 );
179- scalar_type theta = acos<scalar_type> (cosTheta);
177+ scalar_type c = hlsl:: erf (cotTheta );
178+ scalar_type sample_x = hlsl:: max (u.x, scalar_type (1e- 6 ) );
179+ scalar_type theta = hlsl:: acos (cosTheta);
180180 scalar_type fit = 1.0 + theta * (-0.876 + theta * (0.4265 - 0.0594 *theta));
181- scalar_type b = c - (1.0 + c) * pow<scalar_type> (1.0 -sample_x, fit);
181+ scalar_type b = c - (1.0 + c) * hlsl:: pow (1.0 -sample_x, fit);
182182
183- scalar_type normalization = 1.0 / (1.0 + c + numbers::inv_sqrtpi<scalar_type> * tanTheta * exp<scalar_type>(-cosTheta*cosTheta ));
183+ scalar_type normalization = 1.0 / (1.0 + c + numbers::inv_sqrtpi<scalar_type> * tanTheta * hlsl:: exp (-cotTheta*cotTheta ));
184184
185- const int ITER_THRESHOLD = 10 ;
186- const float MAX_ACCEPTABLE_ERR = 1. 0e -5 ;
187- int it = 0 ;
188- float value= 1000.0 ;
189- while (++it < ITER_THRESHOLD && nbl:: hlsl::abs<scalar_type> (value) > MAX_ACCEPTABLE_ERR)
185+ const uint32_t ITER_THRESHOLD = 10 ;
186+ const scalar_type MAX_ACCEPTABLE_ERR = 1e -5 ;
187+ uint32_t it = 0 ;
188+ scalar_type value = 1000.0 ;
189+ while (++it < ITER_THRESHOLD && hlsl::abs (value) > MAX_ACCEPTABLE_ERR)
190190 {
191191 if (!(b >= a && b <= c))
192192 b = 0.5 * (a + c);
193193
194- float invErf = erfInv<scalar_type> (b);
195- value = normalization * (1.0 + b + numbers::inv_sqrtpi<scalar_type> * tanTheta * exp<scalar_type> (-invErf * invErf)) - sample_x;
196- float derivative = normalization * (1.0 - invErf * cosTheta);
194+ scalar_type invErf = hlsl:: erfInv (b);
195+ value = normalization * (1.0 + b + numbers::inv_sqrtpi<scalar_type> * tanTheta * hlsl:: exp (-invErf * invErf)) - sample_x;
196+ scalar_type derivative = normalization * (1.0 - invErf * cosTheta);
197197
198198 if (value > 0.0 )
199199 c = b;
@@ -203,13 +203,13 @@ struct BeckmannGenerateH
203203 b -= value/derivative;
204204 }
205205 // TODO: investigate if we can replace these two erf^-1 calls with a box muller transform
206- slope.x = erfInv<scalar_type> (b);
207- slope.y = erfInv<scalar_type> (2.0 * max <scalar_type> (u.y, 1. 0e- 6 ) - 1.0 );
206+ slope.x = hlsl:: erfInv (b);
207+ slope.y = hlsl:: erfInv (2.0 * hlsl:: max (u.y, scalar_type (1e- 6 ) ) - 1.0 );
208208 }
209209
210- scalar_type sinTheta = sqrt<scalar_type> (1.0 - V.z*V.z);
211- scalar_type cosPhi = sinTheta==0.0 ? 1.0 : clamp <scalar_type> (V.x/sinTheta, -1.0 , 1.0 );
212- scalar_type sinPhi = sinTheta==0.0 ? 0.0 : clamp <scalar_type> (V.y/sinTheta, -1.0 , 1.0 );
210+ scalar_type sinTheta = hlsl:: sqrt (1.0 - V.z*V.z);
211+ scalar_type cosPhi = sinTheta==0.0 ? 1.0 : hlsl:: clamp (V.x/sinTheta, -1.0 , 1.0 );
212+ scalar_type sinPhi = sinTheta==0.0 ? 0.0 : hlsl:: clamp (V.y/sinTheta, -1.0 , 1.0 );
213213 //rotate
214214 scalar_type tmp = cosPhi*slope.x - sinPhi*slope.y;
215215 slope.y = sinPhi*slope.x + cosPhi*slope.y;
0 commit comments