Skip to content

Commit 3a3daf1

Browse files
committed
Clean up sampling functionality
1 parent 3d0c59f commit 3a3daf1

File tree

12 files changed

+389
-444
lines changed

12 files changed

+389
-444
lines changed

App/CL/bxdf.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ THE SOFTWARE.
2323
#define BXDF_CL
2424

2525
#include <../App/CL/utils.cl>
26-
#include <../App/CL/random.cl>
2726
#include <../App/CL/texture.cl>
2827
#include <../App/CL/payload.cl>
2928

App/CL/camera.cl

Lines changed: 36 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ THE SOFTWARE.
2222
#ifndef CAMERA_CL
2323
#define CAMERA_CL
2424

25+
#include <../App/CL/common.cl>
2526
#include <../App/CL/payload.cl>
26-
#include <../App/CL/random.cl>
2727
#include <../App/CL/sampling.cl>
2828
#include <../App/CL/utils.cl>
2929
#include <../App/CL/path.cl>
3030

31-
#define CMJ 1
32-
#define CMJ_DIM 4
33-
3431

3532
/// Ray generation kernel for perspective camera.
3633
/// Rays are generated from camera position to viewing plane
@@ -43,12 +40,11 @@ __kernel void PerspectiveCamera_GeneratePaths(
4340
int imgwidth,
4441
int imgheight,
4542
// RNG seed value
46-
int randseed,
43+
uint rngseed,
4744
// Output rays
4845
__global ray* rays,
49-
__global SobolSampler* samplers,
46+
__global uint const* random,
5047
__global uint const* sobolmat,
51-
int reset,
5248
int frame
5349
#ifndef NO_PATH_DATA
5450
,__global Path* paths
@@ -69,36 +65,22 @@ __kernel void PerspectiveCamera_GeneratePaths(
6965
__global Path* mypath = paths + globalid.y * imgwidth + globalid.x;
7066
#endif
7167

72-
// Prepare RNG
73-
Rng rng;
74-
InitRng(randseed + globalid.x * 157 + 10433 * globalid.y, &rng);
75-
76-
#if SOBOL == 1
77-
__global SobolSampler* sampler = samplers + globalid.y * imgwidth + globalid.x;
78-
79-
if (reset)
80-
{
81-
sampler->seq = 0;
82-
sampler->s0 = RandUint(&rng);
83-
}
84-
else
85-
{
86-
sampler->seq++;
87-
}
88-
89-
float2 sample0;
90-
sample0.x = SobolSampler_Sample1D(sampler->seq, kPixelX, sampler->s0, sobolmat);
91-
sample0.y = SobolSampler_Sample1D(sampler->seq, kPixelY, sampler->s0, sobolmat);
92-
#elif RANDOM == 1
93-
float2 sample0 = UniformSampler_Sample2D(&rng);
94-
#elif CMJ == 1
95-
// Pass defines current current light selection and current material selection
96-
int pass = frame / (CMJ_DIM * CMJ_DIM);
97-
int pattern0 = permute(globalid.y * imgwidth + globalid.x, (1024 * 1024), pass * 0xc13719e1);
98-
int subsample0 = permute(frame % (CMJ_DIM * CMJ_DIM), CMJ_DIM * CMJ_DIM, pattern0 * 0xc517e953);
99-
float2 sample0 = cmj(subsample0, CMJ_DIM, pattern0);
68+
Sampler sampler;
69+
#if SAMPLER == SOBOL
70+
uint scramble = random[globalid.x + imgwidth * globalid.y] * 0x1fe3434f;
71+
Sampler_Init(&sampler, frame, SAMPLE_DIM_CAMERA_OFFSET, scramble);
72+
#elif SAMPLER == RANDOM
73+
uint scramble = globalid.x + imgwidth * globalid.y * rngseed;
74+
Sampler_Init(&sampler, scramble);
75+
#elif SAMPLER == CMJ
76+
uint rnd = random[globalid.x + imgwidth * globalid.y];
77+
uint scramble = rnd * 0x1fe3434f * ((frame + 133 * rnd) / (CMJ_DIM * CMJ_DIM));
78+
Sampler_Init(&sampler, frame % (CMJ_DIM * CMJ_DIM), SAMPLE_DIM_CAMERA_OFFSET, scramble);
10079
#endif
10180

81+
// Generate sample
82+
float2 sample0 = Sampler_Sample2D(&sampler, SAMPLER_ARGS);
83+
10284
// Calculate [0..1] image plane sample
10385
float2 imgsample;
10486
imgsample.x = (float)globalid.x / imgwidth + sample0.x / imgwidth;
@@ -142,12 +124,12 @@ __kernel void PerspectiveCameraDof_GeneratePaths(
142124
int imgwidth,
143125
int imgheight,
144126
// RNG seed value
145-
int randseed,
127+
uint rngseed,
146128
// Output rays
147129
__global ray* rays,
148-
__global SobolSampler* samplers,
130+
__global uint* random,
149131
__global uint const* sobolmat,
150-
int reset
132+
int frame
151133
#ifndef NO_PATH_DATA
152134
, __global Path* paths
153135
#endif
@@ -167,36 +149,24 @@ __kernel void PerspectiveCameraDof_GeneratePaths(
167149
__global Path* mypath = paths + globalid.y * imgwidth + globalid.x;
168150
#endif
169151

170-
// Prepare RNG
171-
Rng rng;
172-
InitRng(randseed + globalid.x * 157 + 10433 * globalid.y, &rng);
173-
174-
//
175-
#ifdef SOBOL
176-
__global SobolSampler* sampler = samplers + globalid.y * imgwidth + globalid.x;
177-
178-
if (reset)
179-
{
180-
sampler->seq = 0;
181-
sampler->s0 = RandUint(&rng);
182-
}
183-
else
184-
{
185-
sampler->seq++;
186-
}
187-
188-
float2 sample0;
189-
sample0.x = SobolSampler_Sample1D(sampler->seq, kPixelX, sampler->s0, sobolmat);
190-
sample0.y = SobolSampler_Sample1D(sampler->seq, kPixelY, sampler->s0, sobolmat);
191-
192-
float2 sample1;
193-
sample1.x = SobolSampler_Sample1D(sampler->seq, kLensX, sampler->s0, sobolmat);
194-
sample1.y = SobolSampler_Sample1D(sampler->seq, kLensY, sampler->s0, sobolmat);
195-
#else
196-
float2 sample0 = UniformSampler_Sample2D(&rng);
197-
float2 sample1 = UniformSampler_Sample2D(&rng);
152+
Sampler sampler;
153+
#if SAMPLER == SOBOL
154+
uint scramble = random[globalid.x + imgwidth * globalid.y] * 0x1fe3434f;
155+
Sampler_Init(&sampler, frame, SAMPLE_DIM_CAMERA_OFFSET, scramble);
156+
#elif SAMPLER == RANDOM
157+
uint scramble = globalid.x + imgwidth * globalid.y * rngseed;
158+
Sampler_Init(&sampler, scramble);
159+
#elif SAMPLER == CMJ
160+
uint rnd = random[globalid.x + imgwidth * globalid.y];
161+
uint scramble = rnd * 0x1fe3434f * ((frame + 133 * rnd) / (CMJ_DIM * CMJ_DIM));
162+
Sampler_Init(&sampler, frame % (CMJ_DIM * CMJ_DIM), SAMPLE_DIM_CAMERA_OFFSET, scramble);
198163
#endif
199164

165+
// Generate pixel and lens samples
166+
float2 sample0 = Sampler_Sample2D(&sampler, SAMPLER_ARGS);
167+
float2 sample1 = Sampler_Sample2D(&sampler, SAMPLER_ARGS);
168+
169+
200170
// Calculate [0..1] image plane sample
201171
float2 imgsample;
202172
imgsample.x = (float)globalid.x / imgwidth + sample0.x / imgwidth;
Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,43 @@
1-
/**********************************************************************
2-
Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
3-
4-
Permission is hereby granted, free of charge, to any person obtaining a copy
5-
of this software and associated documentation files (the "Software"), to deal
6-
in the Software without restriction, including without limitation the rights
7-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8-
copies of the Software, and to permit persons to whom the Software is
9-
furnished to do so, subject to the following conditions:
10-
11-
The above copyright notice and this permission notice shall be included in
12-
all copies or substantial portions of the Software.
13-
14-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20-
THE SOFTWARE.
21-
********************************************************************/
22-
#ifndef RANDOM_CL
23-
#define RANDOM_CL
24-
25-
/// Pseudo-random number generator state
26-
typedef struct _Rng
27-
{
28-
uint val;
29-
} Rng;
30-
31-
/// Hash function
32-
uint WangHash(uint seed)
33-
{
34-
seed = (seed ^ 61) ^ (seed >> 16);
35-
seed *= 9;
36-
seed = seed ^ (seed >> 4);
37-
seed *= 0x27d4eb2d;
38-
seed = seed ^ (seed >> 15);
39-
return seed;
40-
}
41-
42-
/// Return random unsigned
43-
uint RandUint(Rng* rng)
44-
{
45-
rng->val = WangHash(1664525U * rng->val + 1013904223U);
46-
return rng->val;
47-
}
48-
49-
/// Return random float
50-
float RandFloat(Rng* rng)
51-
{
52-
return ((float)RandUint(rng)) / 0xffffffffU;
53-
}
54-
55-
/// Initialize RNG
56-
void InitRng(uint seed, Rng* rng)
57-
{
58-
rng->val = WangHash(seed);
59-
for (int i=0;i< 100;++i)
60-
RandFloat(rng);
61-
}
62-
63-
#endif // RANDOM_CL
64-
65-
1+
/**********************************************************************
2+
Copyright (c) 2016 Advanced Micro Devices, Inc. All rights reserved.
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
********************************************************************/
22+
#ifndef COMMON_CL
23+
#define COMMON_CL
24+
25+
26+
#define CRAZY_LOW_THROUGHPUT 0.0f
27+
#define CRAZY_HIGH_RADIANCE 3.f
28+
#define CRAZY_HIGH_DISTANCE 1000000.f
29+
#define CRAZY_LOW_DISTANCE 0.001f
30+
#define REASONABLE_RADIANCE(x) (clamp((x), 0.f, CRAZY_HIGH_RADIANCE))
31+
#define NON_BLACK(x) (length(x) > 0.f)
32+
33+
#define MULTISCATTER
34+
35+
#define RANDOM 1
36+
#define SOBOL 2
37+
#define CMJ 3
38+
39+
#define SAMPLER CMJ
40+
41+
#define CMJ_DIM 4
42+
43+
#endif // COMMON_CL

App/CL/integrator_ao.cl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <../App/CL/utils.cl>
2-
#include <../App/CL/random.cl>
32
#include <../App/CL/payload.cl>
43
#include <../App/CL/texture.cl>
54
#include <../App/CL/sampling.cl>

0 commit comments

Comments
 (0)