Skip to content

Commit 9cec232

Browse files
Merge branch 'get_rid_of_raystack' into raytracing
2 parents 5cc95a8 + 8727b9d commit 9cec232

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

examples_tests/42.FragmentShaderPathTracer/common.glsl

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,6 @@ struct Ray_t
346346
MutableRay_t _mutable;
347347
Payload_t _payload;
348348
};
349-
#define MAX_STACK_SIZE 1
350-
int stackPtr = 0;
351-
Ray_t rayStack[MAX_STACK_SIZE];
352349

353350

354351
bool anyHitProgram(in ImmutableRay_t _immutable)
@@ -381,15 +378,15 @@ vec2 SampleSphericalMap(vec3 v)
381378
return uv;
382379
}
383380

384-
void missProgram()
381+
void missProgram(in ImmutableRay_t _immutable, inout Payload_t _payload)
385382
{
386-
vec3 finalContribution = rayStack[stackPtr]._payload.throughput;
383+
vec3 finalContribution = _payload.throughput;
387384
//#define USE_ENVMAP
388385
// true miss
389-
if (rayStack[stackPtr]._immutable.maxT>=FLT_MAX)
386+
if (_immutable.maxT>=FLT_MAX)
390387
{
391388
#ifdef USE_ENVMAP
392-
vec2 uv = SampleSphericalMap(rayStack[stackPtr]._immutable.direction);
389+
vec2 uv = SampleSphericalMap(_immutable.direction);
393390
finalContribution *= textureLod(envMap, uv, 0.0).rgb;
394391
#else
395392
const vec3 kConstantEnvLightRadiance = vec3(0.15, 0.21, 0.3);
@@ -398,9 +395,9 @@ void missProgram()
398395
}
399396
else
400397
{
401-
finalContribution *= rayStack[stackPtr]._payload.otherTechniqueHeuristic;
398+
finalContribution *= _payload.otherTechniqueHeuristic;
402399
}
403-
rayStack[stackPtr]._payload.accumulation += finalContribution;
400+
_payload.accumulation += finalContribution;
404401
}
405402

406403
#include <nbl/builtin/glsl/bxdf/brdf/diffuse/oren_nayar.glsl>
@@ -503,8 +500,8 @@ mat2x3 rand3d(in uint protoDimension, in uint _sample, inout nbl_glsl_xoroshiro6
503500
return retval;
504501
}
505502

506-
bool traceRay(in ImmutableRay_t _immutable);
507-
void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64star_state_t scramble_state);
503+
bool traceRay(in ImmutableRay_t _immutable, inout MutableRay_t _mutable);
504+
bool closestHitProgram(inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state);
508505

509506
void main()
510507
{
@@ -534,11 +531,11 @@ void main()
534531
{
535532
nbl_glsl_xoroshiro64star_state_t scramble_state = scramble_start_state;
536533

537-
stackPtr = 0;
534+
Ray_t ray;
538535
// raygen
539536
{
540-
rayStack[stackPtr]._immutable.origin = camPos;
541-
rayStack[stackPtr]._immutable.maxT = FLT_MAX;
537+
ray._immutable.origin = camPos;
538+
ray._immutable.maxT = FLT_MAX;
542539

543540
vec4 tmp = NDC;
544541
// apply stochastic reconstruction filter
@@ -550,41 +547,42 @@ void main()
550547
tmp.xy += pixOffsetParam*nbl_glsl_BoxMullerTransform(remappedRand,1.5);
551548
// for depth of field we could do another stochastic point-pick
552549
tmp = invMVP*tmp;
553-
rayStack[stackPtr]._immutable.direction = normalize(tmp.xyz/tmp.w-camPos);
550+
ray._immutable.direction = normalize(tmp.xyz/tmp.w-camPos);
554551

555-
rayStack[stackPtr]._immutable.typeDepthSampleIx = bitfieldInsert(i,1,DEPTH_BITS_OFFSET,DEPTH_BITS_COUNT);
552+
ray._immutable.typeDepthSampleIx = bitfieldInsert(i,1,DEPTH_BITS_OFFSET,DEPTH_BITS_COUNT);
556553

557554
#if defined(TRIANGLE_METHOD)||defined(RECTANGLE_METHOD)
558-
rayStack[stackPtr]._immutable.normalAtOrigin = vec3(0.0,0.0,0.0);
559-
rayStack[stackPtr]._immutable.wasBSDFAtOrigin = false;
555+
ray._immutable.normalAtOrigin = vec3(0.0,0.0,0.0);
556+
ray._immutable.wasBSDFAtOrigin = false;
560557
#endif
561558

562-
rayStack[stackPtr]._payload.accumulation = vec3(0.0);
563-
rayStack[stackPtr]._payload.otherTechniqueHeuristic = 0.0; // needed for direct eye-light paths
564-
rayStack[stackPtr]._payload.throughput = vec3(1.0);
559+
ray._payload.accumulation = vec3(0.0);
560+
ray._payload.otherTechniqueHeuristic = 0.0; // needed for direct eye-light paths
561+
ray._payload.throughput = vec3(1.0);
565562
#ifdef KILL_DIFFUSE_SPECULAR_PATHS
566-
rayStack[stackPtr]._payload.hasDiffuse = false;
563+
ray._payload.hasDiffuse = false;
567564
#endif
568565
}
569566

570567
// trace
571-
while (stackPtr!=-1)
568+
for (int j=1; j<=MAX_DEPTH; j+=2)
572569
{
573-
ImmutableRay_t _immutable = rayStack[stackPtr]._immutable;
574-
bool anyHitType = traceRay(_immutable);
570+
const ImmutableRay_t _immutable = ray._immutable;
571+
bool anyHitType = traceRay(_immutable,ray._mutable);
575572

576-
if (rayStack[stackPtr]._mutable.intersectionT>=_immutable.maxT)
573+
if (ray._mutable.intersectionT>=_immutable.maxT)
577574
{
578-
missProgram();
575+
missProgram(_immutable,ray._payload);
576+
break;
579577
}
580578
else if (!anyHitType)
581579
{
582-
closestHitProgram(_immutable,scramble_state);
580+
if (closestHitProgram(ray,scramble_state))
581+
break;
583582
}
584-
stackPtr--;
585583
}
586584

587-
vec3 accumulation = rayStack[0]._payload.accumulation;
585+
vec3 accumulation = ray._payload.accumulation;
588586

589587
float rcpSampleSize = 1.0/float(i+1);
590588
color += (accumulation-color)*rcpSampleSize;

examples_tests/42.FragmentShaderPathTracer/litBySphere.frag

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212

13-
bool traceRay(in ImmutableRay_t _immutable)
13+
bool traceRay(in ImmutableRay_t _immutable, inout MutableRay_t _mutable)
1414
{
1515
const bool anyHit = bitfieldExtract(_immutable.typeDepthSampleIx,31,1)!=0;
1616

@@ -28,8 +28,8 @@ bool traceRay(in ImmutableRay_t _immutable)
2828
//if (anyHit && closerIntersection && anyHitProgram(_immutable))
2929
//break;
3030
}
31-
rayStack[stackPtr]._mutable.objectID = objectID;
32-
rayStack[stackPtr]._mutable.intersectionT = intersectionT;
31+
_mutable.objectID = objectID;
32+
_mutable.intersectionT = intersectionT;
3333
// hit
3434
return anyHit;
3535
}
@@ -82,18 +82,18 @@ nbl_glsl_LightSample nbl_glsl_light_generate_and_remainder_and_pdf(out vec3 rema
8282
}
8383

8484

85-
void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64star_state_t scramble_state)
85+
bool closestHitProgram(inout Ray_t ray, inout nbl_glsl_xoroshiro64star_state_t scramble_state)
8686
{
87-
const MutableRay_t mutable = rayStack[stackPtr]._mutable;
87+
const MutableRay_t _mutable = ray._mutable;
8888

89-
Sphere sphere = spheres[mutable.objectID];
90-
vec3 intersection = _immutable.origin+_immutable.direction*mutable.intersectionT;
89+
Sphere sphere = spheres[_mutable.objectID];
90+
vec3 intersection = ray._immutable.origin+ray._immutable.direction*_mutable.intersectionT;
9191

9292
nbl_glsl_AnisotropicViewSurfaceInteraction interaction;
9393
{
9494
nbl_glsl_IsotropicViewSurfaceInteraction isotropic;
9595

96-
isotropic.V.dir = -_immutable.direction;
96+
isotropic.V.dir = -ray._immutable.direction;
9797
//isotropic.V.dPosdScreen = screw that
9898
isotropic.N = Sphere_getNormal(sphere,intersection);
9999
isotropic.NdotV = dot(isotropic.V.dir,isotropic.N);
@@ -105,22 +105,22 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
105105
const uint bsdfLightIDs = sphere.bsdfLightIDs;
106106
const uint lightID = bitfieldExtract(bsdfLightIDs,16,16);
107107

108-
vec3 throughput = rayStack[stackPtr]._payload.throughput;
108+
vec3 throughput = ray._payload.throughput;
109109

110110
// finish MIS
111111
if (lightID!=INVALID_ID_16BIT) // has emissive
112112
{
113113
float lightPdf;
114-
vec3 lightVal = nbl_glsl_light_deferred_eval_and_prob(lightPdf,sphere,_immutable.origin,interaction,lights[lightID]);
115-
rayStack[stackPtr]._payload.accumulation += throughput*lightVal/(1.0+lightPdf*lightPdf*rayStack[stackPtr]._payload.otherTechniqueHeuristic);
114+
vec3 lightVal = nbl_glsl_light_deferred_eval_and_prob(lightPdf,sphere,ray._immutable.origin,interaction,lights[lightID]);
115+
ray._payload.accumulation += throughput*lightVal/(1.0+lightPdf*lightPdf*ray._payload.otherTechniqueHeuristic);
116116
}
117117

118-
const int sampleIx = bitfieldExtract(_immutable.typeDepthSampleIx,0,DEPTH_BITS_OFFSET);
119-
const int depth = bitfieldExtract(_immutable.typeDepthSampleIx,DEPTH_BITS_OFFSET,DEPTH_BITS_COUNT);
118+
const int sampleIx = bitfieldExtract(ray._immutable.typeDepthSampleIx,0,DEPTH_BITS_OFFSET);
119+
const int depth = bitfieldExtract(ray._immutable.typeDepthSampleIx,DEPTH_BITS_OFFSET,DEPTH_BITS_COUNT);
120120

121121
// check if we even have a BSDF at all
122122
uint bsdfID = bitfieldExtract(bsdfLightIDs,0,16);
123-
if (depth<MAX_DEPTH && bsdfID!=INVALID_ID_16BIT)
123+
if (bsdfID!=INVALID_ID_16BIT)
124124
{
125125
// common preload
126126
BSDFNode bsdf = bsdfs[bsdfID];
@@ -129,11 +129,11 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
129129
#ifdef KILL_DIFFUSE_SPECULAR_PATHS
130130
if (BSDFNode_isNotDiffuse(bsdf))
131131
{
132-
if (rayStack[stackPtr]._payload.hasDiffuse)
133-
return;
132+
if (ray._payload.hasDiffuse)
133+
return true;
134134
}
135135
else
136-
rayStack[stackPtr]._payload.hasDiffuse = true;
136+
ray._payload.hasDiffuse = true;
137137
#endif
138138

139139

@@ -189,21 +189,22 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
189189
const float lumaThroughputThreshold = bsdfPdfThreshold;
190190
if (bsdfPdf>bsdfPdfThreshold && (!doNEE || bsdfPdf<FLT_MAX) && getLuma(throughput)>lumaThroughputThreshold)
191191
{
192-
rayStack[stackPtr]._payload.throughput = throughput*rcpChoiceProb;
192+
ray._payload.throughput = throughput*rcpChoiceProb;
193193

194194
float heuristicFactor = rcpChoiceProb-1.0; // weightNonGenerator/weightGenerator
195195
heuristicFactor /= doNEE ? lightPdf:bsdfPdf; // weightNonGenerator/(weightGenerator*probGenerated)
196196
heuristicFactor *= heuristicFactor; // (weightNonGenerator/(weightGenerator*probGenerated))^2
197197
if (doNEE)
198198
heuristicFactor = 1.0/(1.0/bsdfPdf+heuristicFactor*bsdfPdf); // numerically stable, don't touch
199-
rayStack[stackPtr]._payload.otherTechniqueHeuristic = heuristicFactor;
199+
ray._payload.otherTechniqueHeuristic = heuristicFactor;
200200

201201
// trace new ray
202-
rayStack[stackPtr]._immutable.origin = intersection+_sample.L*(doNEE ? maxT:1.0/*kSceneSize*/)*getStartTolerance(depth);
203-
rayStack[stackPtr]._immutable.maxT = maxT;
204-
rayStack[stackPtr]._immutable.direction = _sample.L;
205-
rayStack[stackPtr]._immutable.typeDepthSampleIx = bitfieldInsert(sampleIx,depth+2,DEPTH_BITS_OFFSET,DEPTH_BITS_COUNT)|(doNEE ? ANY_HIT_FLAG:0);
206-
stackPtr++;
202+
ray._immutable.origin = intersection+_sample.L*(doNEE ? maxT:1.0/*kSceneSize*/)*getStartTolerance(depth);
203+
ray._immutable.maxT = maxT;
204+
ray._immutable.direction = _sample.L;
205+
ray._immutable.typeDepthSampleIx = bitfieldInsert(sampleIx,depth+2,DEPTH_BITS_OFFSET,DEPTH_BITS_COUNT)|(doNEE ? ANY_HIT_FLAG:0);
206+
return false;
207207
}
208208
}
209+
return true;
209210
}

0 commit comments

Comments
 (0)