Skip to content

Commit 965e9fc

Browse files
committed
fix masking/shadowing conditions usage, especially for microfacet normals transmission
1 parent 6e84d79 commit 965e9fc

3 files changed

Lines changed: 20 additions & 24 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct ShadowingMethod<T, PNS_SCHUSSLER>
3030
using vector3_type = vector<scalar_type, 3>;
3131
using matrix3x3_type = matrix<scalar_type, 3, 3>;
3232

33-
static scalar_type G1(const scalar_type clampedNdotL, const scalar_type NdotNp, const scalar_type clampedNpdotL, const scalar_type clampedNtdotL)
33+
static scalar_type G1(const scalar_type clampedNdotL, const scalar_type NdotNp, const scalar_type clampedNpdotL, const scalar_type clampedNtdotL, const bool isTangentFacet=false)
3434
{
3535
const scalar_type sinThetaNp = hlsl::sqrt(hlsl::max(1.0 - NdotNp * NdotNp, 0.0));
3636
return hlsl::min(scalar_type(1.0),
@@ -61,10 +61,10 @@ struct ShadowingMethod<T, PNS_YINING>
6161
using vector3_type = vector<scalar_type, 3>;
6262
using matrix3x3_type = matrix<scalar_type, 3, 3>;
6363

64-
static scalar_type G1(const scalar_type clampedNdotL, const scalar_type NdotNp, const scalar_type clampedNpdotL, const scalar_type clampedNtdotL)
64+
static scalar_type G1(const scalar_type clampedNdotL, const scalar_type NdotNp, const scalar_type clampedNpdotL, const scalar_type clampedNtdotL, const bool isTangentFacet=false)
6565
{
6666
const scalar_type g = hlsl::min(scalar_type(1.0),
67-
clampedNdotL / (clampedNpdotL * NdotNp)
67+
clampedNdotL / (hlsl::mix(clampedNpdotL, clampedNtdotL, isTangentFacet) * NdotNp)
6868
);
6969
const scalar_type g2 = g * g;
7070
return -g2 * g + g2 + g;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ struct SMicrofacetNormals<Config, BRDF, P, 1 NBL_PARTIAL_REQ_BOT(config_concepts
451451
typename bxdf_type::anisotropic_interaction_type interaction_t = bxdf_type::anisotropic_interaction_type::create(iso_t);
452452

453453
const scalar_type shadowing_t = shadowing_method_type::G1(hlsl::max(scalar_type(0.0), NdotL), hlsl::dot(shadingNormal, Nt),
454-
NpdotL, hlsl::max(scalar_type(0.0), NtdotL));
454+
NpdotL, hlsl::max(scalar_type(0.0), NtdotL), true);
455455

456456
value_weight_type eval_t = nested_brdf.evalAndWeight(sample_t, interaction_t);
457457
eval += eval_t.value() * (scalar_type(1.0) - lambda_p) * shadowing_t;
@@ -601,7 +601,7 @@ struct SMicrofacetNormals<Config, BRDF, P, 1 NBL_PARTIAL_REQ_BOT(config_concepts
601601
{
602602
const vector3_type L = _sample.getL().getDirection();
603603
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_MAX);
604-
quo *= shadowing_method_type::G1(hlsl::max(scalar_type(0.0), hlsl::dot(shadingNormal, L)), hlsl::max(scalar_type(0.0), hlsl::dot(shadingNormal, Nt)), NpdotL, hlsl::max(scalar_type(0.0), hlsl::dot(Nt, L)));
604+
quo *= shadowing_method_type::G1(hlsl::max(scalar_type(0.0), hlsl::dot(shadingNormal, L)), hlsl::max(scalar_type(0.0), hlsl::dot(shadingNormal, Nt)), NpdotL, hlsl::max(scalar_type(0.0), hlsl::dot(Nt, L)), true);
605605
}
606606

607607
return quotient_weight_type::create(quo, forwardPdf(_sample, interaction, _cache));

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,18 @@ struct SMicrofacetNormals<Config, BRDF, P, 1 NBL_PARTIAL_REQ_BOT(config_concepts
9898
return nested_bsdf.evalAndWeight(sample_N, interaction_N);
9999
}
100100

101-
const scalar_type NdotV = hlsl::dot(shadingNormal, V.getDirection());
102-
const vector3_type upperHemisphereV = ieee754::flipSignIfRHSNegative<vector3_type>(V.getDirection(), hlsl::promote<vector3_type>(NdotV));
103101
const vector3_type Nt = shadowing_method_type::computeNt(Np, shadingBasis);
104-
const scalar_type NpdotV = hlsl::dot(Np, upperHemisphereV);
105-
const scalar_type NtdotV = hlsl::dot(Nt, upperHemisphereV);
102+
const scalar_type NpdotV = interaction.getNdotV();
103+
const scalar_type NtdotV = hlsl::dot(Nt, V.getDirection());
106104
spectral_type eval = hlsl::promote<spectral_type>(0.0);
107105

108106
const vector3_type L = _sample.getL().getDirection();
109107
const scalar_type NdotL = hlsl::dot(shadingNormal, L);
110-
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_MAX);
108+
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_ABS);
111109
const scalar_type NtdotL = hlsl::dot(Nt, L);
112-
const scalar_type lambda_p = shadowing_method_type::lambdaP(NdotNp, hlsl::max(scalar_type(0.0), NpdotV), hlsl::max(scalar_type(0.0), NtdotV));
113-
const scalar_type shadowing = shadowing_method_type::G1(hlsl::max(scalar_type(0.0), NdotL), NdotNp,
114-
NpdotL, hlsl::max(scalar_type(0.0), NtdotL));
110+
const scalar_type lambda_p = shadowing_method_type::lambdaP(NdotNp, hlsl::abs(NpdotV), hlsl::abs(NtdotV));
111+
const scalar_type shadowing = shadowing_method_type::G1(hlsl::abs(NdotL), NdotNp,
112+
NpdotL, hlsl::abs(NtdotL));
115113

116114
// i -> p -> o
117115
{
@@ -128,8 +126,8 @@ struct SMicrofacetNormals<Config, BRDF, P, 1 NBL_PARTIAL_REQ_BOT(config_concepts
128126
iso_t.luminosityContributionHint = interaction.getLuminosityContributionHint();
129127
typename bxdf_type::anisotropic_interaction_type interaction_t = bxdf_type::anisotropic_interaction_type::create(iso_t);
130128

131-
const scalar_type shadowing_t = shadowing_method_type::G1(hlsl::max(scalar_type(0.0), NdotL), hlsl::dot(shadingNormal, Nt),
132-
NpdotL, hlsl::max(scalar_type(0.0), NtdotL));
129+
const scalar_type shadowing_t = shadowing_method_type::G1(hlsl::abs(NdotL), hlsl::dot(shadingNormal, Nt),
130+
NpdotL, hlsl::abs(NtdotL), true);
133131

134132
value_weight_type eval_t = nested_bsdf.evalAndWeight(sample_t, interaction_t);
135133
eval += eval_t.value() * (scalar_type(1.0) - lambda_p) * shadowing_t;
@@ -208,23 +206,21 @@ struct SMicrofacetNormals<Config, BRDF, P, 1 NBL_PARTIAL_REQ_BOT(config_concepts
208206
return nested_bsdf.forwardPdf(sample_N, interaction_N, __createChildCache(sample_N, interaction_N));
209207
}
210208

211-
const scalar_type NdotV = hlsl::dot(shadingNormal, V.getDirection());
212-
const vector3_type upperHemisphereV = ieee754::flipSignIfRHSNegative<vector3_type>(V.getDirection(), hlsl::promote<vector3_type>(NdotV));
213209
const vector3_type Nt = shadowing_method_type::computeNt(Np, shadingBasis);
214-
const scalar_type NpdotV = hlsl::dot(Np, upperHemisphereV);
215-
const scalar_type NtdotV = hlsl::dot(Nt, upperHemisphereV);
210+
const scalar_type NpdotV = interaction.getNdotV();
211+
const scalar_type NtdotV = hlsl::dot(Nt, V.getDirection());
216212

217213
scalar_type pdf = scalar_type(0.0);
218-
const scalar_type lambda_p = shadowing_method_type::lambdaP(NdotNp, hlsl::max(scalar_type(0.0), NpdotV), hlsl::max(scalar_type(0.0), NtdotV));
214+
const scalar_type lambda_p = shadowing_method_type::lambdaP(NdotNp, hlsl::abs(NpdotV), hlsl::abs(NtdotV));
219215

220216
if (lambda_p > scalar_type(0.0))
221217
{
222218
const vector3_type L = _sample.getL().getDirection();
223219
const scalar_type NdotL = hlsl::dot(shadingNormal, L);
224-
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_MAX);
220+
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_ABS);
225221
const scalar_type NtdotL = hlsl::dot(Nt, L);
226222
pdf += lambda_p * nested_bsdf.forwardPdf(_sample, interaction, _cache.aniso_cache)
227-
* shadowing_method_type::G1(hlsl::max(scalar_type(0.0), NdotL), NdotNp, NpdotL, hlsl::max(scalar_type(0.0), NtdotL));
223+
* shadowing_method_type::G1(hlsl::abs(NdotL), NdotNp, NpdotL, hlsl::abs(NtdotL));
228224
}
229225

230226
if (lambda_p < scalar_type(1.0) && NtdotV > numeric_limits<scalar_type>::min)
@@ -282,8 +278,8 @@ struct SMicrofacetNormals<Config, BRDF, P, 1 NBL_PARTIAL_REQ_BOT(config_concepts
282278
if (_cache.sampleIsShadowed)
283279
{
284280
const vector3_type L = _sample.getL().getDirection();
285-
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_MAX);
286-
quo *= shadowing_method_type::G1(hlsl::max(scalar_type(0.0), hlsl::dot(shadingNormal, L)), hlsl::max(scalar_type(0.0), hlsl::dot(shadingNormal, Nt)), NpdotL, hlsl::max(scalar_type(0.0), hlsl::dot(Nt, L)));
281+
const scalar_type NpdotL = _sample.getNdotL(BxDFClampMode::BCM_ABS);
282+
quo *= shadowing_method_type::G1(hlsl::abs(hlsl::dot(shadingNormal, L)), hlsl::abs(hlsl::dot(shadingNormal, Nt)), NpdotL, hlsl::abs(hlsl::dot(Nt, L)), true);
287283
}
288284

289285
return quotient_weight_type::create(quo, forwardPdf(_sample, interaction, _cache));

0 commit comments

Comments
 (0)