Skip to content

Commit a08fdfc

Browse files
committed
fixes to generateH, VdotL calculation
1 parent 5229d25 commit a08fdfc

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

include/nbl/builtin/hlsl/bxdf/base/cook_torrance_base.hlsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ struct SCookTorrance
260260
bool transmitted = math::partitionRandVariable(reflectance, z, rcpChoiceProb);
261261

262262
ray_dir_info_type V = interaction.getV();
263-
const vector3_type _N = interaction.getN();
264263
const vector3_type H = hlsl::mul(interaction.getFromTangentSpace(), localH);
265264
Refract<scalar_type> r = Refract<scalar_type>::create(V.getDirection(), H);
266265
const scalar_type LdotH = hlsl::mix(VdotH, r.getNdotT(rcpEta.value2[0]), transmitted);
@@ -274,7 +273,7 @@ struct SCookTorrance
274273
return sample_type::createInvalid(); // should check if sample direction is invalid
275274

276275
cache = anisocache_type::createPartial(VdotH, LdotH, localH.z, transmitted, rcpEta);
277-
// assert(cache.isValid(fresnel.getRefractionOrientedEta()));
276+
assert(cache.isValid(_f.getRefractionOrientedEta()));
278277

279278
struct reflect_refract_wrapper // so we don't recalculate LdotH
280279
{

include/nbl/builtin/hlsl/bxdf/common.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ struct SAnisotropicMicrofacetCache
828828
this_t retval;
829829
retval.iso_cache.VdotH = VdotH;
830830
retval.iso_cache.LdotH = LdotH;
831-
retval.iso_cache.VdotL = hlsl::mix(scalar_type(2.0) * VdotH * VdotH - scalar_type(1.0),
832-
VdotH * (LdotH - rcpOrientedEta.value[0] + VdotH * rcpOrientedEta.value[0]), transmitted);
831+
const scalar_type viewShortenFactor = hlsl::mix(scalar_type(1.0), rcpOrientedEta.value[0], transmitted);
832+
retval.iso_cache.VdotL = VdotH * (VdotH * viewShortenFactor + LdotH) - viewShortenFactor;
833833
assert(NdotH > scalar_type(0.0));
834834
retval.iso_cache.absNdotH = hlsl::abs(NdotH);
835835
retval.iso_cache.NdotH2 = NdotH * NdotH;

include/nbl/builtin/hlsl/bxdf/ndf/beckmann.hlsl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

include/nbl/builtin/hlsl/bxdf/ndf/ggx.hlsl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ struct GGXGenerateH
149149

150150
scalar_type lensq = V.x*V.x + V.y*V.y;
151151
vector3_type T1 = lensq > 0.0 ? vector3_type(-V.y, V.x, 0.0) * hlsl::rsqrt(lensq) : vector3_type(1.0,0.0,0.0);
152-
vector3_type T2 = cross<scalar_type>(V,T1);
153-
154-
scalar_type r = sqrt<scalar_type>(u.x);
155-
scalar_type phi = 2.0 * numbers::pi<scalar_type> * u.y;
156-
scalar_type t1 = r * cos<scalar_type>(phi);
157-
scalar_type t2 = r * sin<scalar_type>(phi);
158-
scalar_type s = 0.5 * (1.0 + V.z);
159-
t2 = (1.0 - s)*sqrt<scalar_type>(1.0 - t1*t1) + s*t2;
152+
vector3_type T2 = hlsl::cross(V,T1);
153+
154+
scalar_type r = hlsl::sqrt(u.x);
155+
scalar_type phi = scalar_type(2.0) * numbers::pi<scalar_type> * u.y;
156+
scalar_type t1 = r * hlsl::cos(phi);
157+
scalar_type t2 = r * hlsl::sin(phi);
158+
scalar_type s = scalar_type(0.5) * (scalar_type(1.0) + V.z);
159+
t2 = hlsl::mix(hlsl::sqrt(scalar_type(1.0) - t1*t1), t2, s);
160160

161161
//reprojection onto hemisphere
162162
//found cases where t1*t1+t2*t2>1.0 due to fp32 precision issues, hence the max

0 commit comments

Comments
 (0)