Skip to content

Commit 589b7df

Browse files
committed
moved around some redundant calculations in generate for optimization
1 parent 79c80cd commit 589b7df

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct checkValid<F, false>
7777
using scalar_type = typename F::scalar_type;
7878

7979
template<class Sample, class Interaction, class MicrofacetCache>
80-
static bool __call(NBL_CONST_REF_ARG(F) fresnel, NBL_CONST_REF_ARG(Sample) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
80+
static bool __call(NBL_CONST_REF_ARG(F) orientedFresnel, NBL_CONST_REF_ARG(Sample) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
8181
{
8282
return _sample.getNdotL() > numeric_limits<scalar_type>::min && interaction.getNdotV() > numeric_limits<scalar_type>::min;
8383
}
@@ -86,12 +86,14 @@ struct checkValid<F, false>
8686
template<class F>
8787
struct checkValid<F, true>
8888
{
89+
using scalar_type = typename F::scalar_type;
8990
using vector_type = typename F::vector_type; // expect monochrome
9091

9192
template<class Sample, class Interaction, class MicrofacetCache>
92-
static bool __call(NBL_CONST_REF_ARG(F) fresnel, NBL_CONST_REF_ARG(Sample) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
93+
static bool __call(NBL_CONST_REF_ARG(F) orientedFresnel, NBL_CONST_REF_ARG(Sample) _sample, NBL_CONST_REF_ARG(Interaction) interaction, NBL_CONST_REF_ARG(MicrofacetCache) cache)
9394
{
94-
return cache.isValid(fresnel.getRefractionOrientedEta());
95+
fresnel::OrientedEtas<vector_type> orientedEta = fresnel::OrientedEtas<vector_type>::create(scalar_type(1.0), hlsl::promote<vector_type>(orientedFresnel.getRefractionOrientedEta()));
96+
return cache.isValid(orientedEta);
9597
}
9698
};
9799

@@ -268,12 +270,7 @@ struct SCookTorrance
268270
scalar_type rcpChoiceProb;
269271
scalar_type z = u.z;
270272
bool transmitted = math::partitionRandVariable(reflectance, z, rcpChoiceProb);
271-
272-
ray_dir_info_type V = interaction.getV();
273-
const vector3_type H = hlsl::mul(interaction.getFromTangentSpace(), localH);
274-
assert(hlsl::abs(hlsl::length(H) - scalar_type(1.0)) < scalar_type(1e-4));
275-
Refract<scalar_type> r = Refract<scalar_type>::create(V.getDirection(), H);
276-
const scalar_type LdotH = hlsl::mix(VdotH, r.getNdotT(rcpEta.value2[0]), transmitted);
273+
const scalar_type LdotH = hlsl::mix(VdotH, ieee754::copySign(hlsl::sqrt(rcpEta.value2[0]*VdotH*VdotH + scalar_type(1.0) - rcpEta.value2[0]), -VdotH), transmitted);
277274

278275
// fail if samples have invalid paths
279276
const scalar_type NdotL = hlsl::mix(scalar_type(2.0) * VdotH * localH.z - NdotV,
@@ -284,7 +281,12 @@ struct SCookTorrance
284281
return sample_type::createInvalid(); // should check if sample direction is invalid
285282

286283
cache = anisocache_type::createPartial(VdotH, LdotH, localH.z, transmitted, rcpEta);
287-
assert(cache.isValid(_f.getRefractionOrientedEta()));
284+
assert(cache.isValid(fresnel::OrientedEtas<vector_type>::create(scalar_type(1.0), hlsl::promote<vector_type>(_f.getRefractionOrientedEta()))));
285+
286+
ray_dir_info_type V = interaction.getV();
287+
const vector3_type H = hlsl::mul(interaction.getFromTangentSpace(), localH);
288+
assert(hlsl::abs(hlsl::length(H) - scalar_type(1.0)) < scalar_type(1e-4));
289+
Refract<scalar_type> r = Refract<scalar_type>::create(V.getDirection(), H);
288290

289291
struct reflect_refract_wrapper // so we don't recalculate LdotH
290292
{
@@ -364,7 +366,7 @@ struct SCookTorrance
364366
fresnel_type _f = impl::getOrientedFresnel<fresnel_type, IsBSDF>::__call(fresnel, interaction.getNdotV());
365367

366368
const bool valid = impl::checkValid<fresnel_type, IsBSDF>::template __call<sample_type, Interaction, MicrofacetCache>(_f, _sample, interaction, cache);
367-
assert(valid);
369+
assert(valid); // expect the generated sample to always be valid, different checks for brdf and btdf
368370

369371
scalar_type G2_over_G1 = scalar_type(1.0);
370372
if (_pdf < bit_cast<scalar_type>(numeric_limits<scalar_type>::infinity))

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ struct SLightSample
501501
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)
502502
#define NBL_CONCEPT_TPLT_PRM_NAMES (T)
503503
#define NBL_CONCEPT_PARAM_0 (cache, T)
504-
#define NBL_CONCEPT_PARAM_1 (eta, typename T::scalar_type)
504+
#define NBL_CONCEPT_PARAM_1 (eta, fresnel::OrientedEtas<vector<typename T::scalar_type,1> >)
505505
NBL_CONCEPT_BEGIN(2)
506506
#define cache NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
507507
#define eta NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
@@ -674,10 +674,9 @@ struct SIsotropicMicrofacetCache
674674
// similar with BSDF sampling, as fresnel can be high while reflection can be invalid, or low while refraction would be invalid too
675675
bool isTransmission() NBL_CONST_MEMBER_FUNC { return getVdotHLdotH() < scalar_type(0.0); }
676676

677-
bool isValid(const scalar_type eta) NBL_CONST_MEMBER_FUNC
677+
bool isValid(NBL_CONST_REF_ARG(fresnel::OrientedEtas<monochrome_type>) orientedEtas) NBL_CONST_MEMBER_FUNC
678678
{
679-
fresnel::OrientedEtas<monochrome_type> orientedEta = fresnel::OrientedEtas<monochrome_type>::create(scalar_type(1.0), hlsl::promote<monochrome_type>(eta));
680-
return ComputeMicrofacetNormal<scalar_type>::isValidMicrofacet(isTransmission(), VdotL, absNdotH, orientedEta);
679+
return ComputeMicrofacetNormal<scalar_type>::isValidMicrofacet(isTransmission(), VdotL, absNdotH, orientedEtas);
681680
}
682681

683682
scalar_type getVdotL() NBL_CONST_MEMBER_FUNC { return VdotL; }
@@ -860,9 +859,9 @@ struct SAnisotropicMicrofacetCache
860859
scalar_type getNdotH2() NBL_CONST_MEMBER_FUNC { return iso_cache.getNdotH2(); }
861860
bool isTransmission() NBL_CONST_MEMBER_FUNC { return iso_cache.isTransmission(); }
862861

863-
bool isValid(const scalar_type eta) NBL_CONST_MEMBER_FUNC
862+
bool isValid(NBL_CONST_REF_ARG(fresnel::OrientedEtas<monochrome_type>) orientedEtas) NBL_CONST_MEMBER_FUNC
864863
{
865-
return iso_cache.isValid(eta);
864+
return iso_cache.isValid(orientedEtas);
866865
}
867866

868867
scalar_type getTdotH() NBL_CONST_MEMBER_FUNC { return TdotH; }

0 commit comments

Comments
 (0)