Skip to content

Commit adf93a4

Browse files
committed
minor changes to beckmann ndf usage
1 parent e7fcfa3 commit adf93a4

File tree

3 files changed

+70
-76
lines changed

3 files changed

+70
-76
lines changed

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

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,23 @@ NBL_PARTIAL_REQ_TOP(concepts::FloatingPointScalar<T>)
2929
struct Beckmann<T,false NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
3030
{
3131
using scalar_type = T;
32-
// using this_t = Beckmann<T,false>;
3332

34-
scalar_type D(scalar_type a2, scalar_type NdotH2)
33+
scalar_type D(scalar_type NdotH2)
3534
{
36-
scalar_type nom = exp<scalar_type>( (NdotH2 - scalar_type(1.0)) / (a2 * NdotH2) ); // exp(x) == exp2(x/log(2)) ?
35+
scalar_type nom = exp2<scalar_type>((NdotH2 - scalar_type(1.0)) / (log<scalar_type>(2.0) * a2 * NdotH2));
3736
scalar_type denom = a2 * NdotH2 * NdotH2;
3837
return numbers::inv_pi<scalar_type> * nom / denom;
3938
}
4039

4140
// brdf
42-
scalar_type DG1(scalar_type ndf, scalar_type maxNdotV, scalar_type lambda_V)
41+
scalar_type DG1(scalar_type ndf, scalar_type maxNdotV, scalar_type lambda_V, NBL_REF_ARG(scalar_type) onePlusLambda_V)
4342
{
4443
onePlusLambda_V = scalar_type(1.0) + lambda_V;
4544
return ndf::microfacet_to_light_measure_transform<scalar_type,false,ndf::MTT_REFLECT>::__call(ndf / onePlusLambda_V, maxNdotV);
4645
}
4746

4847
// bsdf
49-
scalar_type DG1(scalar_type ndf, scalar_type absNdotV, scalar_type lambda_V, bool transmitted, scalar_type VdotH, scalar_type LdotH, scalar_type VdotHLdotH, scalar_type orientedEta, scalar_type reflectance)
48+
scalar_type DG1(scalar_type ndf, scalar_type absNdotV, scalar_type lambda_V, bool transmitted, scalar_type VdotH, scalar_type LdotH, scalar_type VdotHLdotH, scalar_type orientedEta, scalar_type reflectance, NBL_REF_ARG(scalar_type) onePlusLambda_V)
5049
{
5150
onePlusLambda_V = scalar_type(1.0) + lambda_V;
5251
return ndf::microfacet_to_light_measure_transform<scalar_type,false,ndf::MTT_REFLECT_REFRACT>::__call(hlsl::mix(reflectance, scalar_type(1.0) - reflectance, transmitted) * ndf / onePlusLambda_V, absNdotV, transmitted, VdotH, LdotH, VdotHLdotH, orientedEta);
@@ -57,7 +56,7 @@ struct Beckmann<T,false NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
5756
return scalar_type(1.0) / (scalar_type(1.0) + lambda);
5857
}
5958

60-
scalar_type C2(scalar_type NdotX2, scalar_type a2)
59+
scalar_type C2(scalar_type NdotX2)
6160
{
6261
return NdotX2 / (a2 * (scalar_type(1.0) - NdotX2));
6362
}
@@ -70,31 +69,25 @@ struct Beckmann<T,false NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
7069
return hlsl::mix<scalar_type>(scalar_type(0.0), nom / denom, c < scalar_type(1.6));
7170
}
7271

73-
scalar_type Lambda(scalar_type NdotX2, scalar_type a2)
72+
scalar_type LambdaC2(scalar_type NdotX2)
7473
{
75-
return Lambda(C2(NdotX2, a2));
74+
return Lambda(C2(NdotX2));
7675
}
7776

78-
scalar_type correlated(scalar_type a2, scalar_type NdotV2, scalar_type NdotL2)
77+
scalar_type correlated(scalar_type NdotV2, scalar_type NdotL2)
7978
{
80-
scalar_type c2 = C2(NdotV2, a2);
81-
scalar_type L_v = Lambda(c2);
82-
c2 = C2(NdotL2, a2);
83-
scalar_type L_l = Lambda(c2);
79+
scalar_type L_v = LambdaC2(NdotV2);
80+
scalar_type L_l = LambdaC2(NdotL2);
8481
return scalar_type(1.0) / (scalar_type(1.0) + L_v + L_l);
8582
}
8683

87-
scalar_type G2_over_G1(scalar_type a2, bool transmitted, scalar_type NdotL2, scalar_type lambdaV_plus_one)
84+
scalar_type G2_over_G1(bool transmitted, scalar_type NdotL2, scalar_type lambdaV_plus_one)
8885
{
89-
scalar_type lambdaL = Lambda(NdotL2, a2);
90-
return hlsl::mix(
91-
lambdaV_plus_one / (lambdaV_plus_one + lambdaL),
92-
lambdaV_plus_one * hlsl::beta<scalar_type>(lambdaV_plus_one, scalar_type(1.0) + lambdaL),
93-
transmitted
94-
);
86+
scalar_type lambdaL = LambdaC2(NdotL2);
87+
return lambdaV_plus_one * hlsl::mix(scalar_type(1.0)/(lambdaV_plus_one + lambdaL), hlsl::beta<scalar_type>(lambdaV_plus_one, scalar_type(1.0) + lambdaL), transmitted);
9588
}
9689

97-
scalar_type onePlusLambda_V;
90+
scalar_type a2;
9891
};
9992

10093

@@ -104,26 +97,26 @@ struct Beckmann<T,true NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
10497
{
10598
using scalar_type = T;
10699

107-
scalar_type D(scalar_type ax, scalar_type ay, scalar_type ax2, scalar_type ay2, scalar_type TdotH2, scalar_type BdotH2, scalar_type NdotH2)
100+
scalar_type D(scalar_type TdotH2, scalar_type BdotH2, scalar_type NdotH2)
108101
{
102+
const scalar_type ax2 = ax*ax;
103+
const scalar_type ay2 = ay*ay;
109104
scalar_type nom = exp<scalar_type>(-(TdotH2 / ax2 + BdotH2 / ay2) / NdotH2);
110105
scalar_type denom = ax * ay * NdotH2 * NdotH2;
111106
return numbers::inv_pi<scalar_type> * nom / denom;
112107
}
113108

114-
scalar_type DG1(scalar_type ndf, scalar_type maxNdotV, scalar_type lambda_V)
109+
scalar_type DG1(scalar_type ndf, scalar_type maxNdotV, scalar_type lambda_V, NBL_REF_ARG(scalar_type) onePlusLambda_V)
115110
{
116111
Beckmann<T,false> beckmann;
117-
scalar_type dg = beckmann.DG1(ndf, maxNdotV, lambda_V);
118-
onePlusLambda_V = beckmann.onePlusLambda_V;
112+
scalar_type dg = beckmann.DG1(ndf, maxNdotV, lambda_V, onePlusLambda_V);
119113
return dg;
120114
}
121115

122-
scalar_type DG1(scalar_type ndf, scalar_type absNdotV, scalar_type lambda_V, bool transmitted, scalar_type VdotH, scalar_type LdotH, scalar_type VdotHLdotH, scalar_type orientedEta, scalar_type reflectance)
116+
scalar_type DG1(scalar_type ndf, scalar_type absNdotV, scalar_type lambda_V, bool transmitted, scalar_type VdotH, scalar_type LdotH, scalar_type VdotHLdotH, scalar_type orientedEta, scalar_type reflectance, NBL_REF_ARG(scalar_type) onePlusLambda_V)
123117
{
124118
Beckmann<T,false> beckmann;
125-
scalar_type dg = beckmann.DG1(ndf, absNdotV, lambda_V, transmitted, VdotH, LdotH, VdotHLdotH, orientedEta, reflectance);
126-
onePlusLambda_V = beckmann.onePlusLambda_V;
119+
scalar_type dg = beckmann.DG1(ndf, absNdotV, lambda_V, transmitted, VdotH, LdotH, VdotHLdotH, orientedEta, reflectance, onePlusLambda_V);
127120
return dg;
128121
}
129122

@@ -132,8 +125,10 @@ struct Beckmann<T,true NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
132125
return scalar_type(1.0) / (scalar_type(1.0) + lambda);
133126
}
134127

135-
scalar_type C2(scalar_type TdotX2, scalar_type BdotX2, scalar_type NdotX2, scalar_type ax2, scalar_type ay2)
128+
scalar_type C2(scalar_type TdotX2, scalar_type BdotX2, scalar_type NdotX2)
136129
{
130+
const scalar_type ax2 = ax*ax;
131+
const scalar_type ay2 = ay*ay;
137132
return NdotX2 / (TdotX2 * ax2 + BdotX2 * ay2);
138133
}
139134

@@ -145,32 +140,26 @@ struct Beckmann<T,true NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
145140
return hlsl::mix<scalar_type>(scalar_type(0.0), nom / denom, c < scalar_type(1.6));
146141
}
147142

148-
scalar_type Lambda(scalar_type TdotX2, scalar_type BdotX2, scalar_type NdotX2, scalar_type ax2, scalar_type ay2)
143+
scalar_type LambdaC2(scalar_type TdotX2, scalar_type BdotX2, scalar_type NdotX2)
149144
{
150-
return Lambda(C2(TdotX2, BdotX2, NdotX2, ax2, ay2));
145+
return Lambda(C2(TdotX2, BdotX2, NdotX2));
151146
}
152147

153-
scalar_type correlated(scalar_type ax2, scalar_type ay2, scalar_type TdotV2, scalar_type BdotV2, scalar_type NdotV2, scalar_type TdotL2, scalar_type BdotL2, scalar_type NdotL2)
148+
scalar_type correlated(scalar_type TdotV2, scalar_type BdotV2, scalar_type NdotV2, scalar_type TdotL2, scalar_type BdotL2, scalar_type NdotL2)
154149
{
155-
scalar_type c2 = C2(TdotV2, BdotV2, NdotV2, ax2, ay2);
156-
scalar_type L_v = Lambda(c2);
157-
c2 = C2(TdotL2, BdotL2, NdotL2, ax2, ay2);
158-
scalar_type L_l = Lambda(c2);
159-
return G1(L_v + L_l);
150+
scalar_type L_v = LambdaC2(TdotV2, BdotV2, NdotV2);
151+
scalar_type L_l = LambdaC2(TdotL2, BdotL2, NdotL2);
152+
return scalar_type(1.0) / (scalar_type(1.0) + L_v + L_l);
160153
}
161154

162-
scalar_type G2_over_G1(scalar_type ax2, scalar_type ay2, bool transmitted, scalar_type TdotL2, scalar_type BdotL2, scalar_type NdotL2, scalar_type lambdaV_plus_one)
155+
scalar_type G2_over_G1(bool transmitted, scalar_type TdotL2, scalar_type BdotL2, scalar_type NdotL2, scalar_type lambdaV_plus_one)
163156
{
164-
scalar_type c2 = C2(TdotL2, BdotL2, NdotL2, ax2, ay2);
165-
scalar_type lambdaL = Lambda(c2);
166-
return hlsl::mix(
167-
lambdaV_plus_one / (lambdaV_plus_one + lambdaL),
168-
lambdaV_plus_one * hlsl::beta<scalar_type>(lambdaV_plus_one, scalar_type(1.0) + lambdaL),
169-
transmitted
170-
);
157+
scalar_type lambdaL = LambdaC2(TdotL2, BdotL2, NdotL2);
158+
return lambdaV_plus_one * hlsl::mix(scalar_type(1.0)/(lambdaV_plus_one + lambdaL), hlsl::beta<scalar_type>(lambdaV_plus_one, scalar_type(1.0) + lambdaL), transmitted);
171159
}
172160

173-
scalar_type onePlusLambda_V;
161+
scalar_type ax;
162+
scalar_type ay;
174163
};
175164

176165
}

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,23 @@ struct SBeckmannBxDF
142142
{
143143
scalar_type a2 = A.x*A.x;
144144
ndf::Beckmann<scalar_type, false> beckmann_ndf;
145-
scalar_type NG = beckmann_ndf.D(a2, params.getNdotH2());
145+
beckmann_ndf.a2 = a2;
146+
scalar_type NG = beckmann_ndf.D(params.getNdotH2());
146147
if (a2 > numeric_limits<scalar_type>::min)
147148
{
148-
NG *= beckmann_ndf.correlated(a2, params.getNdotV2(), params.getNdotL2());
149+
NG *= beckmann_ndf.correlated(params.getNdotV2(), params.getNdotL2());
149150
}
150151
return NG;
151152
}
152153
scalar_type __eval_DG_wo_clamps(NBL_CONST_REF_ARG(params_anisotropic_t) params)
153154
{
154-
const scalar_type ax2 = A.x*A.x;
155-
const scalar_type ay2 = A.y*A.y;
156155
ndf::Beckmann<scalar_type, true> beckmann_ndf;
157-
scalar_type NG = beckmann_ndf.D(A.x, A.y, ax2, ay2, params.getTdotH2(), params.getBdotH2(), params.getNdotH2());
156+
beckmann_ndf.ax = A.x;
157+
beckmann_ndf.ay = A.y;
158+
scalar_type NG = beckmann_ndf.D(params.getTdotH2(), params.getBdotH2(), params.getNdotH2());
158159
if (hlsl::any<vector<bool, 2> >(A > (vector2_type)numeric_limits<scalar_type>::min))
159160
{
160-
NG *= beckmann_ndf.correlated(ax2, ay2, params.getTdotV2(), params.getBdotV2(), params.getNdotV2(), params.getTdotL2(), params.getBdotL2(), params.getNdotL2());
161+
NG *= beckmann_ndf.correlated(params.getTdotV2(), params.getBdotV2(), params.getNdotV2(), params.getTdotL2(), params.getBdotL2(), params.getNdotL2());
161162
}
162163
return NG;
163164
}
@@ -281,26 +282,26 @@ struct SBeckmannBxDF
281282
scalar_type ndf, lambda;
282283
scalar_type a2 = A.x*A.x;
283284
ndf::Beckmann<scalar_type, false> beckmann_ndf;
284-
ndf = beckmann_ndf.D(a2, params.getNdotH2());
285+
beckmann_ndf.a2 = a2;
286+
ndf = beckmann_ndf.D(params.getNdotH2());
285287

286-
lambda = beckmann_ndf.Lambda(params.getNdotV2(), a2);
288+
lambda = beckmann_ndf.LambdaC2(params.getNdotV2());
287289

288-
scalar_type _pdf = beckmann_ndf.DG1(ndf, params.getNdotVUnclamped(), lambda);
289-
onePlusLambda_V = beckmann_ndf.onePlusLambda_V;
290+
scalar_type _pdf = beckmann_ndf.DG1(ndf, params.getNdotVUnclamped(), lambda, onePlusLambda_V);
290291

291292
return _pdf;
292293
}
293294
scalar_type pdf(NBL_CONST_REF_ARG(params_anisotropic_t) params, NBL_REF_ARG(scalar_type) onePlusLambda_V)
294295
{
295296
scalar_type ndf, lambda;
296297
ndf::Beckmann<scalar_type, true> beckmann_ndf;
297-
ndf = beckmann_ndf.D(A.x, A.y, A.x*A.x, A.y*A.y, params.getTdotH2(), params.getBdotH2(), params.getNdotH2());
298+
beckmann_ndf.ax = A.x;
299+
beckmann_ndf.ay = A.y;
300+
ndf = beckmann_ndf.D(params.getTdotH2(), params.getBdotH2(), params.getNdotH2());
298301

299-
const scalar_type c2 = beckmann_ndf.C2(params.getTdotV2(), params.getBdotV2(), params.getNdotV2(), A.x, A.y);
300-
lambda = beckmann_ndf.Lambda(c2);
302+
lambda = beckmann_ndf.LambdaC2(params.getTdotV2(), params.getBdotV2(), params.getNdotV2());
301303

302-
scalar_type _pdf = beckmann_ndf.DG1(ndf, params.getNdotVUnclamped(), lambda);
303-
onePlusLambda_V = beckmann_ndf.onePlusLambda_V;
304+
scalar_type _pdf = beckmann_ndf.DG1(ndf, params.getNdotVUnclamped(), lambda, onePlusLambda_V);
304305

305306
return _pdf;
306307
}
@@ -325,7 +326,8 @@ struct SBeckmannBxDF
325326
spectral_type quo = (spectral_type)0.0;
326327
if (params.getNdotLUnclamped() > numeric_limits<scalar_type>::min && params.getNdotVUnclamped() > numeric_limits<scalar_type>::min)
327328
{
328-
scalar_type G2_over_G1 = beckmann_ndf.G2_over_G1(A.x*A.x, false, params.getNdotL2(), onePlusLambda_V);
329+
beckmann_ndf.a2 = A.x*A.x;
330+
scalar_type G2_over_G1 = beckmann_ndf.G2_over_G1(false, params.getNdotL2(), onePlusLambda_V);
329331
fresnel::Conductor<spectral_type> f = fresnel::Conductor<spectral_type>::create(ior0, ior1, params.getVdotH());
330332
const spectral_type reflectance = f();
331333
quo = reflectance * G2_over_G1;
@@ -342,7 +344,9 @@ struct SBeckmannBxDF
342344
spectral_type quo = (spectral_type)0.0;
343345
if (params.getNdotLUnclamped() > numeric_limits<scalar_type>::min && params.getNdotVUnclamped() > numeric_limits<scalar_type>::min)
344346
{
345-
scalar_type G2_over_G1 = beckmann_ndf.G2_over_G1(A.x*A.x, A.y*A.y, false, params.getTdotL2(), params.getBdotL2(), params.getNdotL2(), onePlusLambda_V);
347+
beckmann_ndf.ax = A.x;
348+
beckmann_ndf.ay = A.y;
349+
scalar_type G2_over_G1 = beckmann_ndf.G2_over_G1(false, params.getTdotL2(), params.getBdotL2(), params.getNdotL2(), onePlusLambda_V);
346350
fresnel::Conductor<spectral_type> f = fresnel::Conductor<spectral_type>::create(ior0, ior1, params.getVdotH());
347351
const spectral_type reflectance = f();
348352
quo = reflectance * G2_over_G1;

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,12 @@ struct SBeckmannDielectricBxDF
235235
scalar_type ndf, lambda;
236236
const scalar_type a2 = A.x*A.x;
237237
ndf::Beckmann<scalar_type, false> beckmann_ndf;
238-
ndf = beckmann_ndf.D(a2, params.getNdotH2());
238+
beckmann_ndf.a2 = a2;
239+
ndf = beckmann_ndf.D(params.getNdotH2());
239240

240-
lambda = beckmann_ndf.Lambda(params.getNdotV2(), a2);
241+
lambda = beckmann_ndf.LambdaC2(params.getNdotV2());
241242

242-
scalar_type _pdf = beckmann_ndf.DG1(ndf,params.getNdotV(),lambda,transmitted,params.getVdotH(),params.getLdotH(),VdotHLdotH,orientedEta.value[0],reflectance);
243-
onePlusLambda_V = beckmann_ndf.onePlusLambda_V;
243+
scalar_type _pdf = beckmann_ndf.DG1(ndf,params.getNdotV(),lambda,transmitted,params.getVdotH(),params.getLdotH(),VdotHLdotH,orientedEta.value[0],reflectance,onePlusLambda_V);
244244

245245
return _pdf;
246246
}
@@ -255,16 +255,14 @@ struct SBeckmannDielectricBxDF
255255
const scalar_type reflectance = fresnel::Dielectric<monochrome_type>::__call(orientedEta2, nbl::hlsl::abs<scalar_type>(params.getVdotH()))[0];
256256

257257
scalar_type ndf, lambda;
258-
const scalar_type ax2 = A.x*A.x;
259-
const scalar_type ay2 = A.y*A.y;
260258
ndf::Beckmann<scalar_type, true> beckmann_ndf;
261-
ndf = beckmann_ndf.D(A.x, A.y, ax2, ay2, params.getTdotH2(), params.getBdotH2(), params.getNdotH2());
259+
beckmann_ndf.ax = A.x;
260+
beckmann_ndf.ay = A.y;
261+
ndf = beckmann_ndf.D(params.getTdotH2(), params.getBdotH2(), params.getNdotH2());
262262

263-
scalar_type c2 = beckmann_ndf.C2(params.getTdotV2(), params.getBdotV2(), params.getNdotV2(), ax2, ay2);
264-
lambda = beckmann_ndf.Lambda(c2);
263+
lambda = beckmann_ndf.LambdaC2(params.getTdotV2(), params.getBdotV2(), params.getNdotV2());
265264

266-
scalar_type _pdf = beckmann_ndf.DG1(ndf,params.getNdotV(),lambda,transmitted,params.getVdotH(),params.getLdotH(),VdotHLdotH,orientedEta.value[0],reflectance);
267-
onePlusLambda_V = beckmann_ndf.onePlusLambda_V;
265+
scalar_type _pdf = beckmann_ndf.DG1(ndf,params.getNdotV(),lambda,transmitted,params.getVdotH(),params.getLdotH(),VdotHLdotH,orientedEta.value[0],reflectance,onePlusLambda_V);
268266

269267
return _pdf;
270268
}
@@ -288,7 +286,8 @@ struct SBeckmannDielectricBxDF
288286

289287
scalar_type quo;
290288
ndf::Beckmann<scalar_type, false> beckmann_ndf;
291-
quo = beckmann_ndf.G2_over_G1(A.x*A.x, transmitted, params.getNdotL2(), onePlusLambda_V);
289+
beckmann_ndf.a2 = A.x*A.x;
290+
quo = beckmann_ndf.G2_over_G1(transmitted, params.getNdotL2(), onePlusLambda_V);
292291

293292
return quotient_pdf_type::create(hlsl::promote<spectral_type>(quo), _pdf);
294293
}
@@ -300,7 +299,9 @@ struct SBeckmannDielectricBxDF
300299

301300
scalar_type quo;
302301
ndf::Beckmann<scalar_type, true> beckmann_ndf;
303-
quo = beckmann_ndf.G2_over_G1(A.x*A.x, A.y*A.y, transmitted, params.getTdotL2(), params.getBdotL2(), params.getNdotL2(), onePlusLambda_V);
302+
beckmann_ndf.ax = A.x;
303+
beckmann_ndf.ay = A.y;
304+
quo = beckmann_ndf.G2_over_G1(transmitted, params.getTdotL2(), params.getBdotL2(), params.getNdotL2(), onePlusLambda_V);
304305

305306
return quotient_pdf_type::create(hlsl::promote<spectral_type>(quo), _pdf);
306307
}

0 commit comments

Comments
 (0)