Skip to content

Commit 3519866

Browse files
committed
overwrite DG concept, added Dcorrelated to ggx ndf with optimizations
1 parent f93772e commit 3519866

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ struct getOrientedFresnel<F, true NBL_PARTIAL_REQ_BOT(fresnel::TwoSidedFresnel<F
119119
return fresnel.getReorientedFresnel(NdotV);
120120
}
121121
};
122+
123+
template<class N, class LS, class Interaction, class MicrofacetCache>
124+
struct overwrite_DG
125+
{
126+
using scalar_type = typename N::scalar_type;
127+
using quant_type = typename N::quant_type;
128+
using quant_query_type = typename N::quant_query_type;
129+
using g2g1_query_type = typename N::g2g1_query_type;
130+
NBL_CONSTEXPR_STATIC_INLINE bool HasOverwrite = ndf::NDF_CanOverwriteDG<N>;
131+
132+
template<typename C=bool_constant<!HasOverwrite> >
133+
static enable_if_t<C::value && !HasOverwrite, void> __call(NBL_REF_ARG(scalar_type) DG, N ndf, 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)
134+
{
135+
}
136+
template<typename C=bool_constant<HasOverwrite> >
137+
static enable_if_t<C::value && HasOverwrite, void> __call(NBL_REF_ARG(scalar_type) DG, N ndf, 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)
138+
{
139+
quant_type dg = ndf.template Dcorrelated<LS, Interaction, MicrofacetCache>(query, quant_query, _sample, interaction, cache);
140+
DG = dg.projectedLightMeasure;
141+
}
142+
};
122143
}
123144

124145
// N (NDF), F (fresnel)
@@ -148,16 +169,18 @@ struct SCookTorrance
148169
using quant_query_type = typename ndf_type::quant_query_type;
149170
quant_query_type qq = impl::quant_query_helper<ndf_type, fresnel_type, IsBSDF>::template __call<MicrofacetCache>(ndf, _f, cache);
150171

172+
using g2g1_query_type = typename ndf_type::g2g1_query_type;
173+
g2g1_query_type gq = ndf.template createG2G1Query<sample_type, Interaction>(_sample, interaction);
174+
151175
quant_type D = ndf.template D<sample_type, Interaction, MicrofacetCache>(qq, _sample, interaction, cache);
152176
scalar_type DG = D.projectedLightMeasure;
153177
if (D.microfacetMeasure < bit_cast<scalar_type>(numeric_limits<scalar_type>::infinity))
154-
{
155-
using g2g1_query_type = typename ndf_type::g2g1_query_type;
156-
g2g1_query_type gq = ndf.template createG2G1Query<sample_type, Interaction>(_sample, interaction);
157178
DG *= ndf.template correlated<sample_type, Interaction>(gq, _sample, interaction);
158-
}
159179
else
160180
return hlsl::promote<spectral_type>(0.0);
181+
182+
impl::overwrite_DG<ndf_type, sample_type, Interaction, MicrofacetCache>::__call(DG, ndf, gq, qq, _sample, interaction, cache);
183+
161184
scalar_type clampedVdotH = cache.getVdotH();
162185
NBL_IF_CONSTEXPR(IsBSDF)
163186
clampedVdotH = hlsl::abs(clampedVdotH);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ NBL_CONCEPT_END(
6262
#undef ndf
6363
#include <nbl/builtin/hlsl/concepts/__end.hlsl>
6464

65+
#define NBL_CONCEPT_NAME NDF_CanOverwriteDG
66+
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)
67+
#define NBL_CONCEPT_TPLT_PRM_NAMES (T)
68+
#define NBL_CONCEPT_PARAM_0 (ndf, T)
69+
#define NBL_CONCEPT_PARAM_1 (quant_query, typename T::quant_query_type)
70+
#define NBL_CONCEPT_PARAM_2 (_sample, dummy_impl::sample_t)
71+
#define NBL_CONCEPT_PARAM_3 (interaction, dummy_impl::interaction_t)
72+
#define NBL_CONCEPT_PARAM_4 (cache, dummy_impl::cache_t)
73+
#define NBL_CONCEPT_PARAM_5 (g2_query, typename T::g2g1_query_type)
74+
NBL_CONCEPT_BEGIN(6)
75+
#define ndf NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
76+
#define quant_query NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
77+
#define _sample NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_2
78+
#define interaction NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_3
79+
#define cache NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_4
80+
#define g2_query NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_5
81+
NBL_CONCEPT_END(
82+
((NBL_CONCEPT_REQ_TYPE_ALIAS_CONCEPT)(NDF, T))
83+
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((ndf.template Dcorrelated<dummy_impl::sample_t, dummy_impl::interaction_t, dummy_impl::cache_t>(g2_query, quant_query, _sample, interaction, cache)), ::nbl::hlsl::is_same_v, typename T::quant_type))
84+
);
85+
#undef g2_query
86+
#undef cache
87+
#undef interaction
88+
#undef _sample
89+
#undef quant_query
90+
#undef ndf
91+
#include <nbl/builtin/hlsl/concepts/__end.hlsl>
92+
6593

6694
#define NBL_HLSL_BXDF_ANISOTROPIC_COND_DECLS(IS_ANISO) template<class Interaction>\
6795
NBL_CONSTEXPR_STATIC_INLINE bool RequiredInteraction = IS_ANISO ? surface_interactions::Anisotropic<Interaction> : surface_interactions::Isotropic<Interaction>;\

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,29 +260,55 @@ struct GGX
260260
quant_type D(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)
261261
{
262262
scalar_type d = __ndf_base.template D<MicrofacetCache>(cache);
263+
return createDualMeasureQuantity<T, reflect_refract, quant_query_type>(d, interaction.getNdotV(BxDFClampMode::BCM_ABS), _sample.getNdotL(BxDFClampMode::BCM_ABS), quant_query);
264+
}
265+
266+
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
267+
quant_type DG1(NBL_CONST_REF_ARG(dg1_query_type) query, NBL_CONST_REF_ARG(quant_query_type) quant_query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
268+
{
269+
scalar_type dg1_over_2NdotV = query.getNdfwoNumerator() * query.getG1over2NdotV();
263270
quant_type dmq;
264-
dmq.microfacetMeasure = d;
271+
dmq.microfacetMeasure = scalar_type(2.0) * interaction.getNdotV(_clamp) * dg1_over_2NdotV;
265272

266273
NBL_IF_CONSTEXPR(SupportsTransmission)
267274
{
268275
const scalar_type VdotHLdotH = quant_query.getVdotHLdotH();
269276
const bool transmitted = reflect_refract==MTT_REFRACT || (reflect_refract!=MTT_REFLECT && VdotHLdotH < scalar_type(0.0));
270-
scalar_type NdotL_over_denominator = _sample.getNdotL(BxDFClampMode::BCM_ABS);
277+
dmq.projectedLightMeasure = hlsl::mix(scalar_type(0.5),scalar_type(2.0),transmitted) * dg1_over_2NdotV;
271278
if (transmitted)
272-
NdotL_over_denominator *= scalar_type(4.0) * VdotHLdotH * quant_query.getNeg_rcp2_VdotH_etaLdotH();
273-
dmq.projectedLightMeasure = d * NdotL_over_denominator;
279+
dmq.projectedLightMeasure *= VdotHLdotH * quant_query.getNeg_rcp2_VdotH_etaLdotH();
274280
}
275281
else
276-
dmq.projectedLightMeasure = d * _sample.getNdotL(BxDFClampMode::BCM_ABS);
282+
dmq.projectedLightMeasure = scalar_type(0.5) * dg1_over_2NdotV;
277283
return dmq;
278284
}
279285

280286
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
281-
quant_type DG1(NBL_CONST_REF_ARG(dg1_query_type) query, NBL_CONST_REF_ARG(quant_query_type) quant_query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
287+
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)
288+
{
289+
// without numerator, numerator is 2 * NdotV * NdotL, we factor out 4 * NdotV * NdotL, hence 0.5
290+
scalar_type Vterm = _sample.getNdotL(_clamp) * query.getDevshV();
291+
scalar_type Lterm = interaction.getNdotV(_clamp) * query.getDevshL();
292+
return scalar_type(0.5) / (Vterm + Lterm);
293+
}
294+
295+
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
296+
scalar_type correlated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
297+
{
298+
return scalar_type(4.0) * interaction.getNdotV(_clamp) * _sample.getNdotL(_clamp) * correlated_wo_numerator<LS, Interaction>(query, _sample, interaction);
299+
}
300+
301+
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)
302+
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)
282303
{
283-
scalar_type dg1 = scalar_type(0.5) * query.getNdfwoNumerator() * query.getG1over2NdotV();
304+
scalar_type dg = __ndf_base.template D<MicrofacetCache>(cache);
305+
if (dg < bit_cast<scalar_type>(numeric_limits<scalar_type>::infinity))
306+
dg *= correlated_wo_numerator<LS, Interaction>(query, _sample, interaction);
307+
else
308+
dg = scalar_type(0.0);
309+
284310
quant_type dmq;
285-
dmq.microfacetMeasure = dg1; // note: microfacetMeasure/4NdotV
311+
dmq.microfacetMeasure = dg;
286312

287313
NBL_IF_CONSTEXPR(SupportsTransmission)
288314
{
@@ -291,22 +317,13 @@ struct GGX
291317
scalar_type NdotL_over_denominator = _sample.getNdotL(BxDFClampMode::BCM_ABS);
292318
if (transmitted)
293319
NdotL_over_denominator *= scalar_type(4.0) * VdotHLdotH * quant_query.getNeg_rcp2_VdotH_etaLdotH();
294-
dmq.projectedLightMeasure = dg1;// TODO: figure this out * NdotL_over_denominator;
320+
dmq.projectedLightMeasure = dg * NdotL_over_denominator;
295321
}
296322
else
297-
dmq.projectedLightMeasure = dg1;// TODO: figure this out * _sample.getNdotL(BxDFClampMode::BCM_ABS);
323+
dmq.projectedLightMeasure = dg * _sample.getNdotL(BxDFClampMode::BCM_ABS);
298324
return dmq;
299325
}
300326

301-
template<class LS, class Interaction NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction>)
302-
scalar_type correlated(NBL_CONST_REF_ARG(g2g1_query_type) query, NBL_CONST_REF_ARG(LS) _sample, NBL_CONST_REF_ARG(Interaction) interaction)
303-
{
304-
// without numerator, numerator is 2 * NdotV * NdotL, we factor out 4 * NdotV * NdotL, hence 0.5
305-
scalar_type Vterm = _sample.getNdotL(_clamp) * query.getDevshV();
306-
scalar_type Lterm = interaction.getNdotV(_clamp) * query.getDevshL();
307-
return scalar_type(0.5) / (Vterm + Lterm);
308-
}
309-
310327
template<class LS, class Interaction, class MicrofacetCache NBL_FUNC_REQUIRES(LightSample<LS> && RequiredInteraction<Interaction> && RequiredMicrofacetCache<MicrofacetCache>)
311328
scalar_type G2_over_G1(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)
312329
{

0 commit comments

Comments
 (0)