Skip to content

Commit 8a71f40

Browse files
committed
avoid promoting 1d fresnel to 3d just to dot product, added private func for that
1 parent 8f9a114 commit 8a71f40

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ struct SCookTorrance
128128
return cache.isValid(orientedEta);
129129
}
130130

131+
template<class Interaction=conditional_t<IsAnisotropic,anisotropic_interaction_type,isotropic_interaction_type>,
132+
typename C=bool_constant<!fresnel_type::ReturnsMonochrome> NBL_FUNC_REQUIRES(C::value && !fresnel_type::ReturnsMonochrome)
133+
static scalar_type __getScaledReflectance(NBL_CONST_REF_ARG(fresnel_type) orientedFresnel, NBL_CONST_REF_ARG(Interaction) interaction, scalar_type clampedVdotH)
134+
{
135+
spectral_type throughputWeights = interaction.getLuminosityContributionHint();
136+
return hlsl::dot<spectral_type>(impl::__implicit_promote<spectral_type, typename fresnel_type::vector_type>::__call(orientedFresnel(clampedVdotH)), throughputWeights);
137+
}
138+
template<class Interaction=conditional_t<IsAnisotropic,anisotropic_interaction_type,isotropic_interaction_type>,
139+
typename C=bool_constant<fresnel_type::ReturnsMonochrome> NBL_FUNC_REQUIRES(C::value && fresnel_type::ReturnsMonochrome)
140+
static scalar_type __getScaledReflectance(NBL_CONST_REF_ARG(fresnel_type) orientedFresnel, NBL_CONST_REF_ARG(Interaction) interaction, scalar_type clampedVdotH)
141+
{
142+
return orientedFresnel(clampedVdotH)[0];
143+
}
144+
131145
bool __dotIsUnity(const vector3_type a, const vector3_type b, const scalar_type value)
132146
{
133147
const scalar_type ab = hlsl::dot(a, b);
@@ -284,8 +298,7 @@ struct SCookTorrance
284298
assert(NdotV*VdotH >= scalar_type(0.0));
285299
}
286300

287-
spectral_type throughputWeights = interaction.getLuminosityContributionHint();
288-
const scalar_type reflectance = hlsl::dot(impl::__implicit_promote<spectral_type, typename fresnel_type::vector_type>::__call(_f(hlsl::abs(VdotH))), throughputWeights);
301+
const scalar_type reflectance = __getScaledReflectance(_f, interaction, hlsl::abs(VdotH));
289302

290303
scalar_type rcpChoiceProb;
291304
scalar_type z = u.z;
@@ -326,8 +339,7 @@ struct SCookTorrance
326339

327340
NBL_IF_CONSTEXPR(IsBSDF)
328341
{
329-
spectral_type throughputWeights = interaction.getLuminosityContributionHint();
330-
const scalar_type reflectance = hlsl::dot(impl::__implicit_promote<spectral_type, typename fresnel_type::vector_type>::__call(_f(hlsl::abs(cache.getVdotH()))), throughputWeights);
342+
const scalar_type reflectance = __getScaledReflectance(_f, interaction, hlsl::abs(cache.getVdotH()));
331343
return hlsl::mix(reflectance, scalar_type(1.0) - reflectance, cache.isTransmission()) * DG1.projectedLightMeasure;
332344
}
333345
else
@@ -375,9 +387,8 @@ struct SCookTorrance
375387
spectral_type quo;
376388
NBL_IF_CONSTEXPR(IsBSDF)
377389
{
378-
spectral_type throughputWeights = interaction.getLuminosityContributionHint();
390+
const scalar_type scaled_reflectance = __getScaledReflectance(_f, interaction, hlsl::abs(cache.getVdotH()));
379391
spectral_type reflectance = impl::__implicit_promote<spectral_type, typename fresnel_type::vector_type>::__call(_f(hlsl::abs(cache.getVdotH())));
380-
const scalar_type scaled_reflectance = hlsl::dot(reflectance, throughputWeights);
381392
quo = hlsl::mix(reflectance / scaled_reflectance,
382393
(hlsl::promote<spectral_type>(1.0) - reflectance) / (scalar_type(1.0) - scaled_reflectance), cache.isTransmission()) * G2_over_G1;
383394
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,16 @@ struct Schlick
345345
using vector_type = T;
346346
using eta_type = vector_type;
347347

348+
NBL_CONSTEXPR_STATIC_INLINE bool ReturnsMonochrome = vector_traits<vector_type>::Dimension == 1;
349+
348350
static Schlick<T> create(NBL_CONST_REF_ARG(T) F0)
349351
{
350352
Schlick<T> retval;
351353
retval.F0 = F0;
352354
return retval;
353355
}
354356

355-
T operator()(const scalar_type clampedCosTheta)
357+
T operator()(const scalar_type clampedCosTheta) NBL_CONST_MEMBER_FUNC
356358
{
357359
assert(clampedCosTheta >= scalar_type(0.0));
358360
assert(hlsl::all(hlsl::promote<T>(0.02) < F0 && F0 <= hlsl::promote<T>(1.0)));
@@ -379,6 +381,8 @@ struct Conductor
379381
using vector_type = T;
380382
using eta_type = vector_type;
381383

384+
NBL_CONSTEXPR_STATIC_INLINE bool ReturnsMonochrome = vector_traits<vector_type>::Dimension == 1;
385+
382386
static Conductor<T> create(NBL_CONST_REF_ARG(T) eta, NBL_CONST_REF_ARG(T) etak)
383387
{
384388
Conductor<T> retval;
@@ -412,7 +416,7 @@ struct Conductor
412416
Rp = (rp_common - etaCosTwice) / (rp_common + etaCosTwice);
413417
}
414418

415-
T operator()(const scalar_type clampedCosTheta)
419+
T operator()(const scalar_type clampedCosTheta) NBL_CONST_MEMBER_FUNC
416420
{
417421
T rs2, rp2;
418422
__polarized(eta, etaLen2, hlsl::promote<T>(clampedCosTheta), rp2, rs2);
@@ -440,6 +444,8 @@ struct Dielectric
440444
using vector_type = T;
441445
using eta_type = vector_type;
442446

447+
NBL_CONSTEXPR_STATIC_INLINE bool ReturnsMonochrome = vector_traits<vector_type>::Dimension == 1;
448+
443449
static Dielectric<T> create(NBL_CONST_REF_ARG(OrientedEtas<T>) orientedEta)
444450
{
445451
Dielectric<T> retval;
@@ -470,7 +476,7 @@ struct Dielectric
470476
return (rs2 + rp2) * hlsl::promote<T>(0.5);
471477
}
472478

473-
T operator()(const scalar_type clampedCosTheta)
479+
T operator()(const scalar_type clampedCosTheta) NBL_CONST_MEMBER_FUNC
474480
{
475481
return __call(orientedEta2, clampedCosTheta);
476482
}
@@ -664,7 +670,9 @@ struct Iridescent<T, false NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVector
664670
using eta_type = vector_type;
665671
using base_type = impl::iridescent_base<T, false>;
666672

667-
T operator()(const scalar_type clampedCosTheta)
673+
NBL_CONSTEXPR_STATIC_INLINE bool ReturnsMonochrome = vector_traits<vector_type>::Dimension == 1;
674+
675+
T operator()(const scalar_type clampedCosTheta) NBL_CONST_MEMBER_FUNC
668676
{
669677
return impl::iridescent_helper<T,false>::template __call<base_type>(__base, clampedCosTheta);
670678
}
@@ -690,7 +698,9 @@ struct Iridescent<T, true NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVectori
690698
using eta_type = vector<scalar_type, 1>;
691699
using base_type = impl::iridescent_base<T, true>;
692700

693-
T operator()(const scalar_type clampedCosTheta)
701+
NBL_CONSTEXPR_STATIC_INLINE bool ReturnsMonochrome = vector_traits<vector_type>::Dimension == 1;
702+
703+
T operator()(const scalar_type clampedCosTheta) NBL_CONST_MEMBER_FUNC
694704
{
695705
return impl::iridescent_helper<T,true>::template __call<base_type>(__base, clampedCosTheta);
696706
}

0 commit comments

Comments
 (0)