Skip to content

Commit 7389c9a

Browse files
committed
added checking for TIR in bsdfs
1 parent f3cb6ff commit 7389c9a

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ struct quant_query_helper<N, F, false>
6868
}
6969
};
7070

71+
template<class F, bool IsBSDF>
72+
struct check_TIR_helper;
73+
74+
template<class F>
75+
struct check_TIR_helper<F, false>
76+
{
77+
template<class MicrofacetCache>
78+
static bool __call(NBL_CONST_REF_ARG(F) fresnel, NBL_CONST_REF_ARG(MicrofacetCache) cache)
79+
{
80+
return true;
81+
}
82+
};
83+
84+
template<class F>
85+
struct check_TIR_helper<F, true>
86+
{
87+
template<class MicrofacetCache>
88+
static bool __call(NBL_CONST_REF_ARG(F) fresnel, NBL_CONST_REF_ARG(MicrofacetCache) cache)
89+
{
90+
return ComputeMicrofacetNormal<typename F::scalar_type>::isValidMicrofacet(cache.isTransmission(), cache.getVdotL(), cache.getAbsNdotH(), fresnel.orientedEta);
91+
}
92+
};
93+
7194
template<class F, typename Spectral, bool IsAnisotropic>
7295
struct CookTorranceParams;
7396

@@ -129,7 +152,8 @@ struct SCookTorrance
129152
template<class Interaction, class MicrofacetCache>
130153
spectral_type __eval(NBL_CONST_REF_ARG(sample_type) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
131154
{
132-
if (IsBSDF || (_sample.getNdotL() > numeric_limits<scalar_type>::min && interaction.getNdotV() > numeric_limits<scalar_type>::min))
155+
const bool notTIR = impl::check_TIR_helper<fresnel_type, IsBSDF>::template __call<MicrofacetCache>(fresnel, cache);
156+
if ((IsBSDF && notTIR) || (!IsBSDF && _sample.getNdotL() > numeric_limits<scalar_type>::min && interaction.getNdotV() > numeric_limits<scalar_type>::min))
133157
{
134158
using quant_query_type = typename ndf_type::quant_query_type;
135159
using g2g1_query_type = typename ndf_type::g2g1_query_type;
@@ -283,14 +307,17 @@ struct SCookTorrance
283307
scalar_type _pdf = __pdf<Interaction, MicrofacetCache>(_sample, interaction, cache);
284308

285309
spectral_type quo = hlsl::promote<spectral_type>(0.0);
286-
287-
using g2g1_query_type = typename N::g2g1_query_type;
288-
g2g1_query_type gq = ndf.template createG2G1Query<sample_type, Interaction>(_sample, interaction);
289-
scalar_type G2_over_G1 = ndf.template G2_over_G1<sample_type, Interaction, MicrofacetCache>(gq, _sample, interaction, cache);
290-
NBL_IF_CONSTEXPR(IsBSDF)
291-
quo = hlsl::promote<spectral_type>(G2_over_G1);
292-
else
293-
quo = fresnel(cache.getVdotH()) * G2_over_G1;
310+
const bool notTIR = impl::check_TIR_helper<fresnel_type, IsBSDF>::template __call<MicrofacetCache>(fresnel, cache);
311+
if (notTIR)
312+
{
313+
using g2g1_query_type = typename N::g2g1_query_type;
314+
g2g1_query_type gq = ndf.template createG2G1Query<sample_type, Interaction>(_sample, interaction);
315+
scalar_type G2_over_G1 = ndf.template G2_over_G1<sample_type, Interaction, MicrofacetCache>(gq, _sample, interaction, cache);
316+
NBL_IF_CONSTEXPR(IsBSDF)
317+
quo = hlsl::promote<spectral_type>(G2_over_G1);
318+
else
319+
quo = fresnel(cache.getVdotH()) * G2_over_G1;
320+
}
294321

295322
// set pdf=0 when quo=0 because we don't want to give high weight to sampling strategy that yields 0 contribution
296323
_pdf = hlsl::mix(_pdf, scalar_type(0.0), hlsl::all(quo < hlsl::promote<spectral_type>(numeric_limits<scalar_type>::min)));

include/nbl/builtin/hlsl/bxdf/fresnel.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ struct ComputeMicrofacetNormal
161161

162162
static bool isValidMicrofacet(const bool transmitted, const scalar_type VdotL, const scalar_type NdotH, NBL_CONST_REF_ARG(fresnel::OrientedEtas<vector<scalar_type,1> >) orientedEta)
163163
{
164-
return !transmitted || (VdotL <= -hlsl::min(orientedEta.value, orientedEta.rcp) && NdotH >= nbl::hlsl::numeric_limits<scalar_type>::min);
164+
return !transmitted || (VdotL <= -hlsl::min(orientedEta.value[0], orientedEta.rcp[0]) && NdotH >= nbl::hlsl::numeric_limits<scalar_type>::min);
165165
}
166166

167167
vector_type V;

0 commit comments

Comments
 (0)