Skip to content

Commit 17fe8ee

Browse files
MIS reimplemented
1 parent c06f32c commit 17fe8ee

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

examples_tests/42.FragmentShaderPathTracer/common.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ float BSDFNode_getMISWeight(in BSDFNode bsdf)
246246
{
247247
const float alpha = BSDFNode_getRoughness(bsdf);
248248
const bool notDiffuse = BSDFNode_isNotDiffuse(bsdf);
249-
const float DIFFUSE_MIS_WEIGHT = 0.5;
250-
return (notDiffuse ? (alpha*alpha):1.0)*DIFFUSE_MIS_WEIGHT;
249+
const float DIFFUSE_MIS_NEE_PROB = 1.0;
250+
return (notDiffuse ? (1.0-alpha*alpha):1.0)*DIFFUSE_MIS_NEE_PROB;
251251
}
252252

253253
#include <nbl/builtin/glsl/colorspace/EOTF.glsl>

examples_tests/42.FragmentShaderPathTracer/litBySphere.frag

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
137137
const float monochromeEta = dot(throughputCIE_Y,BSDFNode_getEta(bsdf)[0])/(throughputCIE_Y.r+throughputCIE_Y.g+throughputCIE_Y.b);
138138

139139
// do NEE
140-
const float neeProbability = 1.0;//BSDFNode_getMISWeight(bsdf);
141-
const float neeProbability2 = neeProbability*neeProbability;
142-
if (true)
140+
const float neeProbability = BSDFNode_getMISWeight(bsdf);
141+
float rcpChoiceProb;
142+
if (!nbl_glsl_partitionRandVariable(neeProbability,epsilon[0].z,rcpChoiceProb))
143143
{
144144
vec3 neeContrib; float lightPdf, t;
145-
nbl_glsl_LightSample _sample = nbl_glsl_light_generate_and_remainder_and_pdf(neeContrib,lightPdf,t,intersection,interaction,epsilon[1],depth);
145+
nbl_glsl_LightSample _sample = nbl_glsl_light_generate_and_remainder_and_pdf(neeContrib,lightPdf,t,intersection,interaction,epsilon[0],depth);
146146
// We don't allow non watertight transmitters in this renderer
147147
bool validPath = _sample.NdotL>0.0;
148148
// but if we allowed non-watertight transmitters (single water surface), it would make sense just to apply this line by itself
@@ -152,7 +152,8 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
152152
{
153153
float bsdfPdf;
154154
neeContrib *= nbl_glsl_bsdf_cos_remainder_and_pdf(bsdfPdf,_sample,interaction,bsdf,monochromeEta,_cache)*throughput;
155-
neeContrib /= bsdfPdf/(lightPdf*lightPdf)+neeProbability2/bsdfPdf; // MIS weight
155+
const float oc = bsdfPdf*rcpChoiceProb;
156+
neeContrib /= 1.0/oc+oc/(lightPdf*lightPdf); // MIS weight
156157
if (bsdfPdf<FLT_MAX && getLuma(neeContrib)>lumaContributionThreshold && traceRay(t,intersection+_sample.L*t*getStartTolerance(depth),_sample.L)==-1)
157158
ray._payload.accumulation += neeContrib;
158159
}
@@ -162,7 +163,7 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
162163
float bsdfPdf; vec3 bsdfSampleL;
163164
{
164165
nbl_glsl_AnisotropicMicrofacetCache _cache;
165-
nbl_glsl_LightSample _sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon[0],bsdf,monochromeEta,_cache);
166+
nbl_glsl_LightSample _sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon[1],bsdf,monochromeEta,_cache);
166167
// the value of the bsdf divided by the probability of the sample being generated
167168
throughput *= nbl_glsl_bsdf_cos_remainder_and_pdf(bsdfPdf,_sample,interaction,bsdf,monochromeEta,_cache);
168169
//
@@ -174,7 +175,8 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
174175
if (bsdfPdf>bsdfPdfThreshold && getLuma(throughput)>lumaThroughputThreshold)
175176
{
176177
ray._payload.throughput = throughput;
177-
ray._payload.otherTechniqueHeuristic = neeProbability2/(bsdfPdf*bsdfPdf); // numerically stable, don't touch
178+
ray._payload.otherTechniqueHeuristic = neeProbability/bsdfPdf; // numerically stable, don't touch
179+
ray._payload.otherTechniqueHeuristic *= ray._payload.otherTechniqueHeuristic;
178180

179181
// trace new ray
180182
ray._immutable.origin = intersection+bsdfSampleL*(1.0/*kSceneSize*/)*getStartTolerance(depth);
@@ -183,7 +185,4 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
183185
}
184186
}
185187
return false;
186-
}
187-
#if 0
188-
const bool doNEE = nbl_glsl_partitionRandVariable(bsdfGeneratorProbability,epsilon[0].z,rcpChoiceProb);
189-
#endif
188+
}

0 commit comments

Comments
 (0)