Skip to content

Commit 2812fef

Browse files
boot MIS
1 parent e6dd2cb commit 2812fef

File tree

2 files changed

+93
-21
lines changed

2 files changed

+93
-21
lines changed

examples_tests/42.FragmentShaderPathTracer/common.glsl

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ struct ImmutableRay_t
315315
vec3 origin;
316316
float maxT;
317317
vec3 direction;
318-
bool anyHit;
319318
#if defined(TRIANGLE_METHOD)||defined(RECTANGLE_METHOD)
320319
vec3 normalAtOrigin;
321320
bool wasBSDFAtOrigin;
@@ -348,12 +347,6 @@ struct Ray_t
348347
};
349348

350349

351-
bool anyHitProgram(in ImmutableRay_t _immutable)
352-
{
353-
return true;
354-
}
355-
356-
357350
#define INTERSECTION_ERROR_BOUND_LOG2 (-8.0)
358351
float getTolerance_common(in uint depth)
359352
{
@@ -500,7 +493,7 @@ mat2x3 rand3d(in uint protoDimension, in uint _sample, inout nbl_glsl_xoroshiro6
500493
return retval;
501494
}
502495

503-
bool traceRay(in ImmutableRay_t _immutable, inout MutableRay_t _mutable);
496+
void traceRay(in bool anyHit, in ImmutableRay_t _immutable, inout MutableRay_t _mutable);
504497
bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state);
505498

506499
void main()
@@ -548,7 +541,6 @@ void main()
548541
// for depth of field we could do another stochastic point-pick
549542
tmp = invMVP*tmp;
550543
ray._immutable.direction = normalize(tmp.xyz/tmp.w-camPos);
551-
ray._immutable.anyHit = false;
552544

553545
#if defined(TRIANGLE_METHOD)||defined(RECTANGLE_METHOD)
554546
ray._immutable.normalAtOrigin = vec3(0.0,0.0,0.0);
@@ -567,18 +559,15 @@ void main()
567559
for (int j=1; j<=MAX_DEPTH; j+=2)
568560
{
569561
const ImmutableRay_t _immutable = ray._immutable;
570-
bool anyHitType = traceRay(_immutable,ray._mutable);
562+
traceRay(false,_immutable,ray._mutable);
571563

572564
if (ray._mutable.intersectionT>=_immutable.maxT)
573565
{
574566
missProgram(_immutable,ray._payload);
575567
break;
576568
}
577-
else if (!anyHitType)
578-
{
579-
if (closestHitProgram(j,i,ray,scramble_state))
580-
break;
581-
}
569+
else if (closestHitProgram(j,i,ray,scramble_state))
570+
break;
582571
}
583572

584573
vec3 accumulation = ray._payload.accumulation;

examples_tests/42.FragmentShaderPathTracer/litBySphere.frag

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010

1111

1212

13-
bool traceRay(in ImmutableRay_t _immutable, inout MutableRay_t _mutable)
13+
void traceRay(in bool anyHit, in ImmutableRay_t _immutable, inout MutableRay_t _mutable)
1414
{
15-
const bool anyHit = _immutable.anyHit;
16-
1715
int objectID = -1;
1816
float intersectionT = _immutable.maxT;
1917
for (int i=0; i<SPHERE_COUNT; i++)
@@ -30,10 +28,9 @@ bool traceRay(in ImmutableRay_t _immutable, inout MutableRay_t _mutable)
3028
}
3129
_mutable.objectID = objectID;
3230
_mutable.intersectionT = intersectionT;
33-
// hit
34-
return anyHit;
3531
}
3632

33+
#if 0
3734
// the interaction here is the interaction at the illuminator-end of the ray, not the receiver
3835
vec3 nbl_glsl_light_deferred_eval_and_prob(out float pdf, in Sphere sphere, in vec3 origin, in nbl_glsl_AnisotropicViewSurfaceInteraction interaction, in Light light)
3936
{
@@ -80,8 +77,93 @@ nbl_glsl_LightSample nbl_glsl_light_generate_and_remainder_and_pdf(out vec3 rema
8077

8178
return nbl_glsl_createLightSample(L,interaction);
8279
}
80+
#endif
81+
82+
bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state)
83+
{
84+
const MutableRay_t _mutable = ray._mutable;
85+
86+
Sphere sphere = spheres[_mutable.objectID];
87+
const uint bsdfLightIDs = sphere.bsdfLightIDs;
88+
89+
vec3 throughput = ray._payload.throughput;
90+
91+
// add emissive
92+
const uint lightID = bitfieldExtract(bsdfLightIDs,16,16);
93+
if (lightID!=INVALID_ID_16BIT) // has emissive
94+
ray._payload.accumulation += throughput*Light_getRadiance(lights[lightID]);
95+
96+
// check if we even have a BSDF at all
97+
uint bsdfID = bitfieldExtract(bsdfLightIDs,0,16);
98+
if (bsdfID!=INVALID_ID_16BIT)
99+
{
100+
BSDFNode bsdf = bsdfs[bsdfID];
101+
#ifdef KILL_DIFFUSE_SPECULAR_PATHS
102+
if (BSDFNode_isNotDiffuse(bsdf))
103+
{
104+
if (ray._payload.hasDiffuse)
105+
return true;
106+
}
107+
else
108+
ray._payload.hasDiffuse = true;
109+
#endif
110+
111+
//rand
112+
mat2x3 epsilon = rand3d(depth,_sample,scramble_state);
113+
114+
// intersection stuffs
115+
const vec3 intersection = ray._immutable.origin+ray._immutable.direction*_mutable.intersectionT;
116+
117+
nbl_glsl_AnisotropicViewSurfaceInteraction interaction;
118+
{
119+
nbl_glsl_IsotropicViewSurfaceInteraction isotropic;
83120

121+
isotropic.V.dir = -ray._immutable.direction;
122+
//isotropic.V.dPosdScreen = screw that
123+
isotropic.N = Sphere_getNormal(sphere,intersection);
124+
isotropic.NdotV = dot(isotropic.V.dir,isotropic.N);
125+
isotropic.NdotV_squared = isotropic.NdotV*isotropic.NdotV;
84126

127+
interaction = nbl_glsl_calcAnisotropicInteraction(isotropic);
128+
}
129+
130+
131+
132+
133+
float maxT = FLT_MAX;
134+
nbl_glsl_AnisotropicMicrofacetCache _cache;
135+
136+
// do I need this?
137+
const vec3 throughputCIE_Y = transpose(nbl_glsl_sRGBtoXYZ)[1]*throughput;
138+
const float monochromeEta = dot(throughputCIE_Y,BSDFNode_getEta(bsdf)[0])/(throughputCIE_Y.r+throughputCIE_Y.g+throughputCIE_Y.b);
139+
140+
// sample BSDF
141+
float bsdfPdf; vec3 bsdfSampleL;
142+
{
143+
nbl_glsl_LightSample _sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon[0],bsdf,monochromeEta,_cache);
144+
// the value of the bsdf divided by the probability of the sample being generated
145+
throughput *= nbl_glsl_bsdf_cos_remainder_and_pdf(bsdfPdf,_sample,interaction,bsdf,monochromeEta,_cache);
146+
//
147+
bsdfSampleL = _sample.L;
148+
}
149+
150+
const float bsdfPdfThreshold = 0.0001;
151+
// OETF smallest perceptible value
152+
const float lumaThroughputThreshold = getLuma(nbl_glsl_eotf_sRGB(vec3(1.0)/255.0));
153+
if (bsdfPdf>bsdfPdfThreshold && getLuma(throughput)>lumaThroughputThreshold)
154+
{
155+
ray._payload.throughput = throughput;
156+
157+
// trace new ray
158+
ray._immutable.origin = intersection+bsdfSampleL*(1.0/*kSceneSize*/)*getStartTolerance(depth);
159+
ray._immutable.maxT = maxT;
160+
ray._immutable.direction = bsdfSampleL;
161+
return false;
162+
}
163+
}
164+
return true;
165+
}
166+
#if 0
85167
bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state)
86168
{
87169
const MutableRay_t _mutable = ray._mutable;
@@ -204,4 +286,5 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
204286
}
205287
}
206288
return true;
207-
}
289+
}
290+
#endif

0 commit comments

Comments
 (0)