Skip to content

Commit 9ea21b4

Browse files
refactor traceRay
1 parent 2812fef commit 9ea21b4

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

examples_tests/42.FragmentShaderPathTracer/common.glsl

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ Light lights[LIGHT_COUNT] =
313313
struct ImmutableRay_t
314314
{
315315
vec3 origin;
316-
float maxT;
317316
vec3 direction;
318317
#if defined(TRIANGLE_METHOD)||defined(RECTANGLE_METHOD)
319318
vec3 normalAtOrigin;
@@ -375,21 +374,13 @@ void missProgram(in ImmutableRay_t _immutable, inout Payload_t _payload)
375374
{
376375
vec3 finalContribution = _payload.throughput;
377376
//#define USE_ENVMAP
378-
// true miss
379-
if (_immutable.maxT>=FLT_MAX)
380-
{
381-
#ifdef USE_ENVMAP
382-
vec2 uv = SampleSphericalMap(_immutable.direction);
383-
finalContribution *= textureLod(envMap, uv, 0.0).rgb;
384-
#else
385-
const vec3 kConstantEnvLightRadiance = vec3(0.15, 0.21, 0.3);
386-
finalContribution *= kConstantEnvLightRadiance;
387-
#endif
388-
}
389-
else
390-
{
391-
finalContribution *= _payload.otherTechniqueHeuristic;
392-
}
377+
#ifdef USE_ENVMAP
378+
vec2 uv = SampleSphericalMap(_immutable.direction);
379+
finalContribution *= textureLod(envMap, uv, 0.0).rgb;
380+
#else
381+
const vec3 kConstantEnvLightRadiance = vec3(0.15, 0.21, 0.3);
382+
finalContribution *= kConstantEnvLightRadiance;
383+
#endif
393384
_payload.accumulation += finalContribution;
394385
}
395386

@@ -493,7 +484,7 @@ mat2x3 rand3d(in uint protoDimension, in uint _sample, inout nbl_glsl_xoroshiro6
493484
return retval;
494485
}
495486

496-
void traceRay(in bool anyHit, in ImmutableRay_t _immutable, inout MutableRay_t _mutable);
487+
int traceRay(inout float intersectionT, in ImmutableRay_t _immutable);
497488
bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state);
498489

499490
void main()
@@ -520,6 +511,7 @@ void main()
520511

521512
vec3 color = vec3(0.0);
522513
float meanLumaSquared = 0.0;
514+
// TODO: if we collapse the nested for loop, then all GPUs will get `MAX_DEPTH` factor speedup, not just NV with separate PC
523515
for (int i=0; i<SAMPLES; i++)
524516
{
525517
nbl_glsl_xoroshiro64star_state_t scramble_state = scramble_start_state;
@@ -528,7 +520,6 @@ void main()
528520
// raygen
529521
{
530522
ray._immutable.origin = camPos;
531-
ray._immutable.maxT = FLT_MAX;
532523

533524
vec4 tmp = NDC;
534525
// apply stochastic reconstruction filter
@@ -555,19 +546,20 @@ void main()
555546
#endif
556547
}
557548

558-
// trace
559-
for (int j=1; j<=MAX_DEPTH; j+=2)
549+
// bounces
560550
{
561-
const ImmutableRay_t _immutable = ray._immutable;
562-
traceRay(false,_immutable,ray._mutable);
563-
564-
if (ray._mutable.intersectionT>=_immutable.maxT)
551+
bool hit = true; bool rayAlive = true;
552+
for (int d=1; d<=MAX_DEPTH && hit && rayAlive; d+=2)
565553
{
566-
missProgram(_immutable,ray._payload);
567-
break;
554+
ray._mutable.intersectionT = FLT_MAX;
555+
ray._mutable.objectID = traceRay(ray._mutable.intersectionT,ray._immutable);
556+
hit = ray._mutable.objectID!=-1;
557+
if (hit)
558+
rayAlive = closestHitProgram(3u, i, ray, scramble_state);
568559
}
569-
else if (closestHitProgram(j,i,ray,scramble_state))
570-
break;
560+
// was last trace a miss?
561+
if (!hit)
562+
missProgram(ray._immutable,ray._payload);
571563
}
572564

573565
vec3 accumulation = ray._payload.accumulation;

examples_tests/42.FragmentShaderPathTracer/litBySphere.frag

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

1111

1212

13-
void traceRay(in bool anyHit, in ImmutableRay_t _immutable, inout MutableRay_t _mutable)
13+
int traceRay(inout float intersectionT, in ImmutableRay_t _immutable)
1414
{
1515
int objectID = -1;
16-
float intersectionT = _immutable.maxT;
1716
for (int i=0; i<SPHERE_COUNT; i++)
1817
{
1918
float t = Sphere_intersect(spheres[i],_immutable.origin,_immutable.direction);
@@ -26,8 +25,7 @@ void traceRay(in bool anyHit, in ImmutableRay_t _immutable, inout MutableRay_t _
2625
//if (anyHit && closerIntersection && anyHitProgram(_immutable))
2726
//break;
2827
}
29-
_mutable.objectID = objectID;
30-
_mutable.intersectionT = intersectionT;
28+
return objectID;
3129
}
3230

3331
#if 0
@@ -87,12 +85,12 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
8785
const uint bsdfLightIDs = sphere.bsdfLightIDs;
8886

8987
vec3 throughput = ray._payload.throughput;
90-
88+
#if 1
9189
// add emissive
9290
const uint lightID = bitfieldExtract(bsdfLightIDs,16,16);
9391
if (lightID!=INVALID_ID_16BIT) // has emissive
9492
ray._payload.accumulation += throughput*Light_getRadiance(lights[lightID]);
95-
93+
#endif
9694
// check if we even have a BSDF at all
9795
uint bsdfID = bitfieldExtract(bsdfLightIDs,0,16);
9896
if (bsdfID!=INVALID_ID_16BIT)
@@ -130,7 +128,6 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
130128

131129

132130

133-
float maxT = FLT_MAX;
134131
nbl_glsl_AnisotropicMicrofacetCache _cache;
135132

136133
// do I need this?
@@ -156,12 +153,11 @@ bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nb
156153

157154
// trace new ray
158155
ray._immutable.origin = intersection+bsdfSampleL*(1.0/*kSceneSize*/)*getStartTolerance(depth);
159-
ray._immutable.maxT = maxT;
160156
ray._immutable.direction = bsdfSampleL;
161-
return false;
157+
return true;
162158
}
163159
}
164-
return true;
160+
return false;
165161
}
166162
#if 0
167163
bool closestHitProgram(in uint depth, in uint _sample, inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state)

0 commit comments

Comments
 (0)