Skip to content

Commit c172b19

Browse files
committed
use beta function in G2 correlated as well
1 parent 309e216 commit c172b19

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct SCookTorrance
175175
quant_type D = ndf.template D<sample_type, Interaction, MicrofacetCache>(qq, _sample, interaction, cache);
176176
scalar_type DG = D.projectedLightMeasure;
177177
if (D.microfacetMeasure < bit_cast<scalar_type>(numeric_limits<scalar_type>::infinity))
178-
DG *= ndf.template correlated<sample_type, Interaction>(gq, _sample, interaction);
178+
DG *= ndf.template correlated<sample_type, Interaction, MicrofacetCache>(gq, _sample, interaction, cache);
179179
else
180180
return hlsl::promote<spectral_type>(0.0);
181181

@@ -242,7 +242,7 @@ struct SCookTorrance
242242
template<typename C=bool_constant<IsBSDF> >
243243
enable_if_t<C::value && IsBSDF, sample_type> generate(NBL_CONST_REF_ARG(anisotropic_interaction_type) interaction, const vector3_type u, NBL_REF_ARG(anisocache_type) cache)
244244
{
245-
const vector3_type localV = interaction.getTangentSpaceV();
245+
const vector3_type localV = hlsl::normalize(interaction.getTangentSpaceV());
246246
const scalar_type NdotV = localV.z;
247247

248248
fresnel_type _f = impl::getOrientedFresnel<fresnel_type, IsBSDF>::__call(fresnel, NdotV);
@@ -327,9 +327,10 @@ struct SCookTorrance
327327
{
328328
bxdf::Reflect<scalar_type> r = bxdf::Reflect<scalar_type>::create(V.getDirection(), H);
329329
L = V.reflect(r);
330+
L.direction = hlsl::normalize(L.direction);
330331
}
331332

332-
vector3_type _N = interaction.getN();
333+
vector3_type _N = hlsl::normalize(interaction.getN());
333334
scalar_type NdotL = hlsl::dot(_N, L.getDirection());
334335
if (ComputeMicrofacetNormal<scalar_type>::isTransmissionPath(NdotV, NdotL) != transmitted)
335336
return sample_type::createInvalid(); // should check if sample direction is invalid
@@ -351,7 +352,7 @@ struct SCookTorrance
351352

352353
// const scalar_type _viewShortenFactor = hlsl::mix(scalar_type(1.0), rcpEta.value[0], transmitted);
353354
// const scalar_type _NdotL = localH.z * (VdotH * _viewShortenFactor + cache.iso_cache.LdotH) - NdotV * _viewShortenFactor;
354-
scalar_type _NdotL = hlsl::dot(interaction.getN(), L.getDirection());
355+
scalar_type _NdotL = hlsl::dot(_N, L.getDirection());
355356
assert(hlsl::abs(_NdotL - NdotL) < 1e-4);
356357

357358
const vector3_type T = interaction.getT();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ NBL_CONCEPT_END(
5151
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((T::GuaranteedVNDF), ::nbl::hlsl::is_same_v, bool))
5252
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((ndf.template D<dummy_impl::sample_t, dummy_impl::interaction_t, dummy_impl::cache_t>(quant_query, _sample, interaction, cache)), ::nbl::hlsl::is_same_v, typename T::quant_type))
5353
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((ndf.template DG1<dummy_impl::sample_t, dummy_impl::interaction_t>(dg1_query, quant_query, _sample, interaction)), ::nbl::hlsl::is_same_v, typename T::quant_type))
54-
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((ndf.template correlated<dummy_impl::sample_t, dummy_impl::interaction_t>(g2_query, _sample, interaction)), ::nbl::hlsl::is_same_v, typename T::scalar_type))
54+
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((ndf.template correlated<dummy_impl::sample_t, dummy_impl::interaction_t, dummy_impl::cache_t>(g2_query, _sample, interaction, cache)), ::nbl::hlsl::is_same_v, typename T::scalar_type))
5555
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((ndf.template G2_over_G1<dummy_impl::sample_t, dummy_impl::interaction_t, dummy_impl::cache_t>(g2_query, _sample, interaction, cache)), ::nbl::hlsl::is_same_v, typename T::scalar_type))
5656
);
5757
#undef g2_query

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,12 @@ struct Beckmann
327327
return createDualMeasureQuantity<T, reflect_refract, quant_query_type>(dg1, interaction.getNdotV(BxDFClampMode::BCM_ABS), _sample.getNdotL(BxDFClampMode::BCM_ABS), quant_query);
328328
}
329329

330-
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
331-
scalar_type correlated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
330+
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)
331+
scalar_type correlated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
332332
{
333-
return scalar_type(1.0) / (scalar_type(1.0) + query.getLambdaV() + query.getLambdaL());
333+
scalar_type onePlusLambda_V = scalar_type(1.0) + query.getLambdaV();
334+
scalar_type lambda_L = query.getLambdaL();
335+
return hlsl::mix(scalar_type(1.0)/(onePlusLambda_V + lambda_L), bxdf::beta<scalar_type>(onePlusLambda_V, scalar_type(1.0) + lambda_L), cache.isTransmission());
334336
}
335337

336338
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,27 +284,42 @@ struct GGX
284284
return dmq;
285285
}
286286

287-
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
288-
scalar_type correlated_wo_numerator(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
287+
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)
288+
scalar_type correlated_wo_numerator(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
289289
{
290+
scalar_type NdotV = interaction.getNdotV(_clamp);
291+
scalar_type NdotL = _sample.getNdotL(_clamp);
292+
scalar_type devsh_v = query.getDevshV();
293+
scalar_type devsh_l = query.getDevshL();
290294
// without numerator, numerator is 2 * NdotV * NdotL, we factor out 4 * NdotV * NdotL, hence 0.5
291-
scalar_type Vterm = _sample.getNdotL(_clamp) * query.getDevshV();
292-
scalar_type Lterm = interaction.getNdotV(_clamp) * query.getDevshL();
293-
return scalar_type(0.5) / (Vterm + Lterm);
295+
if (cache.isTransmission())
296+
{
297+
if (NdotV < 1e-7 || NdotL < 1e-7)
298+
return 0.0;
299+
scalar_type onePlusLambda_V = scalar_type(0.5) * (devsh_v / NdotV + scalar_type(1.0));
300+
scalar_type onePlusLambda_L = scalar_type(0.5) * (devsh_l / NdotL + scalar_type(1.0));
301+
return bxdf::beta_wo_check<scalar_type>(onePlusLambda_L, onePlusLambda_V);
302+
}
303+
else
304+
{
305+
scalar_type Vterm = NdotL * devsh_v;
306+
scalar_type Lterm = NdotV * devsh_l;
307+
return scalar_type(0.5) / (Vterm + Lterm);
308+
}
294309
}
295310

296-
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
297-
scalar_type correlated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
311+
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)
312+
scalar_type correlated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
298313
{
299-
return scalar_type(4.0) * interaction.getNdotV(_clamp) * _sample.getNdotL(_clamp) * correlated_wo_numerator<LS, Interaction>(query, _sample, interaction);
314+
return scalar_type(4.0) * interaction.getNdotV(_clamp) * _sample.getNdotL(_clamp) * correlated_wo_numerator<LS, Interaction, MicrofacetCache>(query, _sample, interaction, cache);
300315
}
301316

302317
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)
303318
quant_type Dcorrelated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(quant_query_type) quant_query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
304319
{
305320
scalar_type dg = __ndf_base.template D<MicrofacetCache>(cache);
306321
if (dg < bit_cast<scalar_type>(numeric_limits<scalar_type>::infinity))
307-
dg *= correlated_wo_numerator<LS, Interaction>(query, _sample, interaction);
322+
dg *= correlated_wo_numerator<LS, Interaction, MicrofacetCache>(query, _sample, interaction, cache);
308323
else
309324
dg = scalar_type(0.0);
310325

0 commit comments

Comments
 (0)