Skip to content

Commit 9c27e01

Browse files
refactor the individual shaders of ex 42
1 parent 33826a7 commit 9c27e01

File tree

4 files changed

+42
-64
lines changed

4 files changed

+42
-64
lines changed

examples_tests/42.FragmentShaderPathTracer/common.glsl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ float Sphere_getSolidAngle(in Sphere sphere, in vec3 origin)
8080
}
8181

8282

83+
Sphere spheres[SPHERE_COUNT] = {
84+
Sphere_Sphere(vec3(0.0,-100.5,-1.0),100.0,0u,INVALID_ID_16BIT),
85+
Sphere_Sphere(vec3(2.0,0.0,-1.0),0.5,1u,INVALID_ID_16BIT),
86+
Sphere_Sphere(vec3(0.0,0.0,-1.0),0.5,2u,INVALID_ID_16BIT),
87+
Sphere_Sphere(vec3(-2.0,0.0,-1.0),0.5,3u,INVALID_ID_16BIT),
88+
Sphere_Sphere(vec3(2.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
89+
Sphere_Sphere(vec3(0.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
90+
Sphere_Sphere(vec3(-2.0,0.0,1.0),0.5,5u,INVALID_ID_16BIT),
91+
Sphere_Sphere(vec3(0.5,1.0,0.5),0.5,6u,INVALID_ID_16BIT)
92+
#if SPHERE_COUNT>8
93+
,Sphere_Sphere(vec3(-1.5,1.5,0.0),0.3,INVALID_ID_16BIT,0u)
94+
#endif
95+
};
96+
8397

8498
struct Triangle
8599
{
@@ -278,6 +292,20 @@ float scene_getLightChoicePdf(in Light light)
278292
}
279293

280294

295+
#define LIGHT_COUNT 1
296+
Light lights[LIGHT_COUNT] =
297+
{
298+
{
299+
vec3(30.0,25.0,15.0),
300+
#if defined(TRIANGLE_METHOD)||defined(RECTANGLE_METHOD)
301+
0u
302+
#else
303+
8u
304+
#endif
305+
}
306+
};
307+
308+
281309

282310
#define ANY_HIT_FLAG (-2147483648)
283311
#define DEPTH_BITS_COUNT 8

examples_tests/42.FragmentShaderPathTracer/litByRectangle.frag

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
#version 430 core
22
#extension GL_GOOGLE_include_directive : require
33

4+
#define SPHERE_COUNT 8
45
#define RECTANGLE_METHOD 0 // 0 area sampling, 1 solid angle sampling, 2 approximate projected solid angle sampling
56
#include "common.glsl"
67

7-
#define SPHERE_COUNT 8
8-
Sphere spheres[SPHERE_COUNT] = {
9-
Sphere_Sphere(vec3(0.0,-100.5,-1.0),100.0,0u,INVALID_ID_16BIT),
10-
Sphere_Sphere(vec3(2.0,0.0,-1.0),0.5,1u,INVALID_ID_16BIT),
11-
Sphere_Sphere(vec3(0.0,0.0,-1.0),0.5,2u,INVALID_ID_16BIT),
12-
Sphere_Sphere(vec3(-2.0,0.0,-1.0),0.5,3u,INVALID_ID_16BIT),
13-
Sphere_Sphere(vec3(2.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
14-
Sphere_Sphere(vec3(0.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
15-
Sphere_Sphere(vec3(-2.0,0.0,1.0),0.5,5u,INVALID_ID_16BIT),
16-
Sphere_Sphere(vec3(0.5,1.0,0.5),0.5,6u,INVALID_ID_16BIT)
17-
};
188
#define RECTANGLE_COUNT 1
199
Rectangle rectangles[RECTANGLE_COUNT] = {
2010
Rectangle_Rectangle(vec3(-1.8,0.35,0.3),vec3(0.6,0.0,-0.3),vec3(0.3,0.45,-0.6),INVALID_ID_16BIT,0u)
2111
};
2212

2313

24-
#define LIGHT_COUNT 1
25-
Light lights[LIGHT_COUNT] = {
26-
{vec3(30.0,25.0,15.0),0u}
27-
};
28-
29-
3014
bool traceRay(in ImmutableRay_t _immutable)
3115
{
3216
const bool anyHit = bitfieldExtract(_immutable.typeDepthSampleIx,31,1)!=0;
@@ -221,11 +205,11 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
221205
#endif
222206

223207

224-
const float bsdfGeneratorProbability = BSDFNode_getMISWeight(bsdf);
225-
vec3 epsilon = rand3d(depth,sampleIx,scramble_state);
208+
const float bsdfGeneratorProbability = BSDFNode_getMISWeight(bsdf);
209+
mat2x3 epsilon = rand3d(depth,sampleIx,scramble_state);
226210

227211
float rcpChoiceProb;
228-
const bool doNEE = nbl_glsl_partitionRandVariable(bsdfGeneratorProbability,epsilon.z,rcpChoiceProb);
212+
const bool doNEE = nbl_glsl_partitionRandVariable(bsdfGeneratorProbability,epsilon[0].z,rcpChoiceProb);
229213

230214

231215
float maxT;
@@ -240,7 +224,7 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
240224
_sample = nbl_glsl_light_generate_and_remainder_and_pdf(
241225
lightRemainder,lightPdf,maxT,
242226
intersection,interaction,
243-
isBSDF,epsilon,depth
227+
isBSDF,epsilon[0],depth
244228
);
245229
throughput *= lightRemainder;
246230
}
@@ -258,7 +242,7 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
258242
else
259243
{
260244
maxT = FLT_MAX;
261-
_sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon,bsdf,monochromeEta,_cache);
245+
_sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon[0],bsdf,monochromeEta,_cache);
262246
}
263247

264248
// do a cool trick and always compute the bsdf parts this way! (no divergence)

examples_tests/42.FragmentShaderPathTracer/litBySphere.frag

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,8 @@
55
#version 430 core
66
#extension GL_GOOGLE_include_directive : require
77

8-
#include "common.glsl"
9-
108
#define SPHERE_COUNT 9
11-
Sphere spheres[SPHERE_COUNT] = {
12-
Sphere_Sphere(vec3(0.0,-100.5,-1.0),100.0,0u,INVALID_ID_16BIT),
13-
Sphere_Sphere(vec3(2.0,0.0,-1.0),0.5,1u,INVALID_ID_16BIT),
14-
Sphere_Sphere(vec3(0.0,0.0,-1.0),0.5,2u,INVALID_ID_16BIT),
15-
Sphere_Sphere(vec3(-2.0,0.0,-1.0),0.5,3u,INVALID_ID_16BIT),
16-
Sphere_Sphere(vec3(2.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
17-
Sphere_Sphere(vec3(0.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
18-
Sphere_Sphere(vec3(-2.0,0.0,1.0),0.5,5u,INVALID_ID_16BIT),
19-
Sphere_Sphere(vec3(0.5,1.0,0.5),0.5,6u,INVALID_ID_16BIT),
20-
Sphere_Sphere(vec3(-1.5,1.5,0.0),0.3,INVALID_ID_16BIT,0u)
21-
};
22-
23-
24-
Light lights[LIGHT_COUNT] = {
25-
{vec3(30.0,25.0,15.0),8u}
26-
};
9+
#include "common.glsl"
2710

2811

2912

@@ -154,7 +137,7 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
154137
#endif
155138

156139

157-
const float bsdfGeneratorProbability = BSDFNode_getMISWeight(bsdf);
140+
const float bsdfGeneratorProbability = BSDFNode_getMISWeight(bsdf);
158141
mat2x3 epsilon = rand3d(depth,sampleIx,scramble_state);
159142

160143
float rcpChoiceProb;

examples_tests/42.FragmentShaderPathTracer/litByTriangle.frag

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,15 @@
55
#version 430 core
66
#extension GL_GOOGLE_include_directive : require
77

8+
#define SPHERE_COUNT 8
89
#define TRIANGLE_METHOD 2 // 0 area sampling, 1 solid angle sampling, 2 approximate projected solid angle sampling
910
#include "common.glsl"
1011

11-
#define SPHERE_COUNT 8
12-
Sphere spheres[SPHERE_COUNT] = {
13-
Sphere_Sphere(vec3(0.0,-100.5,-1.0),100.0,0u,INVALID_ID_16BIT),
14-
Sphere_Sphere(vec3(2.0,0.0,-1.0),0.5,1u,INVALID_ID_16BIT),
15-
Sphere_Sphere(vec3(0.0,0.0,-1.0),0.5,2u,INVALID_ID_16BIT),
16-
Sphere_Sphere(vec3(-2.0,0.0,-1.0),0.5,3u,INVALID_ID_16BIT),
17-
Sphere_Sphere(vec3(2.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
18-
Sphere_Sphere(vec3(0.0,0.0,1.0),0.5,4u,INVALID_ID_16BIT),
19-
Sphere_Sphere(vec3(-2.0,0.0,1.0),0.5,5u,INVALID_ID_16BIT),
20-
Sphere_Sphere(vec3(0.5,1.0,0.5),0.5,6u,INVALID_ID_16BIT)
21-
};
2212
#define TRIANGLE_COUNT 1
2313
Triangle triangles[TRIANGLE_COUNT] = {
2414
Triangle_Triangle(mat3(vec3(-1.8,0.35,0.3),vec3(-1.2,0.35,0.0),vec3(-1.5,0.8,-0.3)),INVALID_ID_16BIT,0u)
2515
};
2616

27-
28-
#define LIGHT_COUNT 1
29-
Light lights[LIGHT_COUNT] = {
30-
{vec3(30.0,25.0,15.0),0u}
31-
};
32-
33-
3417
bool traceRay(in ImmutableRay_t _immutable)
3518
{
3619
const bool anyHit = bitfieldExtract(_immutable.typeDepthSampleIx,31,1)!=0;
@@ -68,7 +51,7 @@ bool traceRay(in ImmutableRay_t _immutable)
6851
}
6952

7053

71-
#include <irr/builtin/glsl/sampling/projected_spherical_triangle.glsl>
54+
#include <nbl/builtin/glsl/sampling/projected_spherical_triangle.glsl>
7255

7356

7457
// the interaction here is the interaction at the illuminator-end of the ray, not the receiver
@@ -228,10 +211,10 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
228211

229212

230213
const float bsdfGeneratorProbability = BSDFNode_getMISWeight(bsdf);
231-
vec3 epsilon = rand3d(depth,sampleIx,scramble_state);
214+
mat2x3 epsilon = rand3d(depth,sampleIx,scramble_state);
232215

233216
float rcpChoiceProb;
234-
const bool doNEE = nbl_glsl_partitionRandVariable(bsdfGeneratorProbability,epsilon.z,rcpChoiceProb);
217+
const bool doNEE = nbl_glsl_partitionRandVariable(bsdfGeneratorProbability,epsilon[0].z,rcpChoiceProb);
235218

236219

237220
float maxT;
@@ -246,7 +229,7 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
246229
_sample = nbl_glsl_light_generate_and_remainder_and_pdf(
247230
lightRemainder,lightPdf,maxT,
248231
intersection,interaction,
249-
isBSDF,epsilon,depth
232+
isBSDF,epsilon[0],depth
250233
);
251234
throughput *= lightRemainder;
252235
}
@@ -264,7 +247,7 @@ void closestHitProgram(in ImmutableRay_t _immutable, inout nbl_glsl_xoroshiro64s
264247
else
265248
{
266249
maxT = FLT_MAX;
267-
_sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon,bsdf,monochromeEta,_cache);
250+
_sample = nbl_glsl_bsdf_cos_generate(interaction,epsilon[0],bsdf,monochromeEta,_cache);
268251
}
269252

270253
// do a cool trick and always compute the bsdf parts this way! (no divergence)

0 commit comments

Comments
 (0)