Skip to content

Commit 1a829f0

Browse files
committed
CMJ experiment
1 parent 2ba56d9 commit 1a829f0

File tree

9 files changed

+136
-37
lines changed

9 files changed

+136
-37
lines changed

App/CL/camera.cl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ THE SOFTWARE.
2828
#include <../App/CL/utils.cl>
2929
#include <../App/CL/path.cl>
3030

31+
//#define SOBOL
3132

3233

3334

App/CL/integrator_pt.cl

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ THE SOFTWARE.
4343
#define CRAZY_LOW_THROUGHPUT 0.0f
4444
#define CRAZY_HIGH_RADIANCE 3.f
4545
#define CRAZY_HIGH_DISTANCE 1000000.f
46-
#define CRAZY_LOW_DISTANCE 0.001f
46+
#define CRAZY_LOW_DISTANCE 0.01f
4747
#define REASONABLE_RADIANCE(x) (clamp((x), 0.f, CRAZY_HIGH_RADIANCE))
4848
#define NON_BLACK(x) (length(x) > 0.f)
4949

50+
#define CMJ 1
51+
5052

5153
// This kernel only handles scattered paths.
5254
// It applies direct illumination and generates
@@ -235,6 +237,8 @@ __kernel void ShadeVolume(
235237
}
236238
}
237239

240+
#define CMJ_DIM 16
241+
238242
// Handle ray-surface interaction possibly generating path continuation.
239243
// This is only applied to non-scattered paths.
240244
__kernel void ShadeSurface(
@@ -280,6 +284,8 @@ __kernel void ShadeSurface(
280284
__global uint const* sobolmat,
281285
// Current bounce
282286
int bounce,
287+
// Frame
288+
int frame,
283289
// Volume data
284290
__global Volume const* volumes,
285291
// Shadow rays
@@ -329,7 +335,7 @@ __kernel void ShadeSurface(
329335

330336
// Fetch incoming ray direction
331337
float3 wi = -normalize(rays[hitidx].d.xyz);
332-
#ifdef SOBOL
338+
#if (SOBOL == 1)
333339
// Sample light
334340
__global SobolSampler* sampler = samplers + pixelidx;
335341

@@ -350,7 +356,7 @@ __kernel void ShadeSurface(
350356
sample3.y = SobolSampler_Sample1D(sampler->seq, GetSampleDim(bounce, kIndirectV), sampler->s0, sobolmat);
351357

352358
float sample4 = SobolSampler_Sample1D(sampler->seq, GetSampleDim(bounce, kRR), sampler->s0, sobolmat);
353-
#else
359+
#elif (RANDOM == 1)
354360
// Prepare RNG
355361
Rng rng;
356362
InitRng(rngseed + (globalid << 2) * 157 + 13, &rng);
@@ -359,6 +365,35 @@ __kernel void ShadeSurface(
359365
float2 sample2 = UniformSampler_Sample2D(&rng);
360366
float2 sample3 = UniformSampler_Sample2D(&rng);
361367
float sample4 = UniformSampler_Sample2D(&rng).x;
368+
#elif (CMJ == 1)
369+
Rng rng;
370+
InitRng(rngseed + (globalid << 2) * 157 + 13, &rng);
371+
float2 sample0 = UniformSampler_Sample2D(&rng);
372+
float2 sample1 = UniformSampler_Sample2D(&rng);
373+
float2 sample2 = UniformSampler_Sample2D(&rng);
374+
float2 sample3 = UniformSampler_Sample2D(&rng);
375+
float sample4 = UniformSampler_Sample2D(&rng).x;
376+
377+
int pass = frame / (CMJ_DIM * CMJ_DIM);
378+
int subsample0 = permute(frame % (CMJ_DIM * CMJ_DIM), CMJ_DIM * CMJ_DIM, (pixelidx + bounce) * 0xc517e953);
379+
int subsample1 = permute(frame % (CMJ_DIM * CMJ_DIM), CMJ_DIM * CMJ_DIM, (pixelidx + bounce + 1) * 0xc517e953);
380+
int subsample2 = permute(frame % (CMJ_DIM * CMJ_DIM), CMJ_DIM * CMJ_DIM, (pixelidx + bounce + 2) * 0xc517e953);
381+
int subsample3 = permute(frame % (CMJ_DIM * CMJ_DIM), CMJ_DIM * CMJ_DIM, (pixelidx + bounce + 3) * 0xc517e953);
382+
int subsample4 = permute(frame % (CMJ_DIM * CMJ_DIM), CMJ_DIM * CMJ_DIM, (pixelidx + bounce + 4) * 0xc517e953);
383+
384+
int pattern0 = (pass + bounce + pixelidx) % 13331;
385+
int pattern1 = (pass + bounce + pixelidx + 1) % 13331;
386+
int pattern2 = (pass + bounce + pixelidx + 2) % 13331;
387+
int pattern3 = (pass + bounce + pixelidx + 3) % 13331;
388+
int pattern4 = (pass + bounce + pixelidx + 4) % 13331;
389+
390+
sample0 = cmj(subsample0, CMJ_DIM, pattern0);
391+
sample1 = cmj(subsample1, CMJ_DIM, pattern1);
392+
sample2 = cmj(subsample2, CMJ_DIM, pattern2);
393+
sample3 = cmj(subsample3, CMJ_DIM, pattern2);
394+
sample4 = cmj(subsample4, CMJ_DIM, pattern2).x;
395+
#else
396+
362397
#endif
363398

364399
// Fill surface data

App/CL/material.cl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ THE SOFTWARE.
2727
#include <../App/CL/texture.cl>
2828
#include <../App/CL/payload.cl>
2929
#include <../App/CL/bxdf.cl>
30+
//#define SOBOL
3031

3132
void Material_Select(
3233
// Scene data

App/CL/sampling.cl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,64 @@ float SobolSampler_Sample1D(uint index, uint dimension, uint scramble, __global
205205
return result * (1.f / (1UL << 32));
206206
}
207207

208+
uint permute(uint i, uint l, uint p)
209+
{
210+
unsigned w = l - 1;
211+
w |= w >> 1;
212+
w |= w >> 2;
213+
w |= w >> 4;
214+
w |= w >> 8;
215+
w |= w >> 16;
216+
217+
do
218+
{
219+
i ^= p;
220+
i *= 0xe170893d;
221+
i ^= p >> 16;
222+
i ^= (i & w) >> 4;
223+
i ^= p >> 8;
224+
i *= 0x0929eb3f;
225+
i ^= p >> 23;
226+
i ^= (i & w) >> 1;
227+
i *= 1 | p >> 27;
228+
i *= 0x6935fa69;
229+
i ^= (i & w) >> 11;
230+
i *= 0x74dcb303;
231+
i ^= (i & w) >> 2;
232+
i *= 0x9e501cc3;
233+
i ^= (i & w) >> 2;
234+
i *= 0xc860a3df;
235+
i &= w;
236+
i ^= i >> 5;
237+
} while (i >= l);
238+
return (i + p) % l;
239+
}
240+
241+
float randfloat(uint i, uint p)
242+
{
243+
i ^= p;
244+
i ^= i >> 17;
245+
i ^= i >> 10;
246+
i *= 0xb36534e5;
247+
i ^= i >> 12;
248+
i ^= i >> 21;
249+
i *= 0x93fc4795;
250+
i ^= 0xdf6e307f;
251+
i ^= i >> 17;
252+
i *= 1 | p >> 18;
253+
return i * (1.0f / 4294967808.0f);
254+
}
255+
256+
float2 cmj(int s, int n, int p)
257+
{
258+
int sx = permute(s % n, n, p * 0xa511e9b3);
259+
int sy = permute(s / n, n, p * 0x63d83595);
260+
float jx = randfloat(s, p * 0xa399d265);
261+
float jy = randfloat(s, p * 0x711ad6a5);
262+
263+
return make_float2((s % n + (sy + jx) / n) / n,
264+
(s / n + (sx + jy) / n) / n);
265+
}
266+
208267

209268
#endif // SAMPLING_CL

App/PT/ptrenderer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ namespace Baikal
114114
, m_resetsampler(true)
115115
, m_scene_tracker(context, devidx)
116116
, m_num_bounces(num_bounces)
117+
, m_framecnt(0)
117118
{
118119
std::string buildopts;
119120

@@ -179,6 +180,9 @@ namespace Baikal
179180
auto api = m_scene_tracker.GetIntersectionApi();
180181
auto& clwscene = m_scene_tracker.CompileScene(scene, m_render_data->mat_collector, m_render_data->tex_collector);
181182

183+
if (m_resetsampler)
184+
m_framecnt = 0;
185+
182186
// Check output
183187
assert(m_output);
184188

@@ -247,6 +251,8 @@ namespace Baikal
247251
//
248252
m_context.Flush(0);
249253
}
254+
255+
++m_framecnt;
250256
}
251257

252258
void PtRenderer::SetOutput(Output* output)
@@ -386,6 +392,7 @@ namespace Baikal
386392
shadekernel.SetArg(argc++, m_render_data->samplers);
387393
shadekernel.SetArg(argc++, m_render_data->sobolmat);
388394
shadekernel.SetArg(argc++, pass);
395+
shadekernel.SetArg(argc++, m_framecnt);
389396
shadekernel.SetArg(argc++, scene.volumes);
390397
shadekernel.SetArg(argc++, m_render_data->shadowrays);
391398
shadekernel.SetArg(argc++, m_render_data->lightsamples);

App/PT/ptrenderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ namespace Baikal
113113

114114
private:
115115
int m_num_bounces;
116+
int m_framecnt;
116117
};
117118

118119
}

App/Scene/IO/scene_test_io.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ namespace Baikal
6565
normals[t].x=0; normals[t].y = -1; normals[t].z = 0;
6666
uvs[t].x=1; uvs[t].y = 1;
6767
++t;
68-
68+
6969
t = 0U;
7070
for(auto j = 0U; j < lat - 3; j++)
7171
for(auto i = 0U; i < lon - 1; i++)
@@ -223,24 +223,15 @@ namespace Baikal
223223
}
224224
else if (filename == "sphere+plane+area")
225225
{
226-
auto mesh = CreateSphere(64, 32, 2.f, float3(0.f, 2.2f, 0.f));
226+
auto mesh = CreateSphere(64, 32, 2.f, float3(0.f, 2.5f, 0.f));
227227
scene->AttachShape(mesh);
228228
scene->AttachAutoreleaseObject(mesh);
229-
230-
SingleBxdf* green = new SingleBxdf(SingleBxdf::BxdfType::kLambert);
231-
green->SetInputValue("albedo", 2.f * float4(0.1f, 0.2f, 0.1f, 1.f));
232-
233-
SingleBxdf* spec = new SingleBxdf(SingleBxdf::BxdfType::kMicrofacetGGX);
234-
spec->SetInputValue("albedo", float4(0.9f, 0.9f, 0.9f, 1.f));
235-
spec->SetInputValue("roughness", float4(0.002f, 0.002f, 0.002f, 1.f));
236-
237-
MultiBxdf* mix = new MultiBxdf(MultiBxdf::Type::kFresnelBlend);
238-
mix->SetInputValue("base_material", green);
239-
mix->SetInputValue("top_material", spec);
240-
mix->SetInputValue("ior", float4(1.33f, 1.33f, 1.33f, 1.33f));
241-
242-
mesh->SetMaterial(mix);
243-
229+
230+
SingleBxdf* grey = new SingleBxdf(SingleBxdf::BxdfType::kLambert);
231+
grey->SetInputValue("albedo", float4(0.7f, 0.7f, 0.7f, 1.f));
232+
grey->SetTwoSided(true);
233+
234+
244235
Mesh* floor = CreateQuad(
245236
{
246237
RadeonRays::float3(-8, 0, -8),
@@ -252,10 +243,11 @@ namespace Baikal
252243
scene->AttachShape(floor);
253244
scene->AttachAutoreleaseObject(floor);
254245

255-
floor->SetMaterial(green);
256-
246+
floor->SetMaterial(grey);
247+
mesh->SetMaterial(grey);
248+
257249
SingleBxdf* emissive = new SingleBxdf(SingleBxdf::BxdfType::kEmissive);
258-
emissive->SetInputValue("albedo", 5.f * float4(3.1f, 3.f, 2.8f, 1.f));
250+
emissive->SetInputValue("albedo", 2.f * float4(3.1f, 3.f, 2.8f, 1.f));
259251

260252
Mesh* light = CreateQuad(
261253
{
@@ -269,19 +261,17 @@ namespace Baikal
269261
scene->AttachAutoreleaseObject(light);
270262

271263
light->SetMaterial(emissive);
272-
264+
273265
AreaLight* l1 = new AreaLight(light, 0);
274266
AreaLight* l2 = new AreaLight(light, 1);
267+
275268
scene->AttachLight(l1);
276269
scene->AttachLight(l2);
277270
scene->AttachAutoreleaseObject(l1);
278271
scene->AttachAutoreleaseObject(l2);
279-
272+
280273
scene->AttachAutoreleaseObject(emissive);
281-
scene->AttachAutoreleaseObject(green);
282-
scene->AttachAutoreleaseObject(spec);
283-
scene->AttachAutoreleaseObject(mix);
284-
274+
scene->AttachAutoreleaseObject(grey);
285275
}
286276
else if (filename == "sphere+plane+ibl")
287277
{

App/Scene/scene_tracker.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,15 +970,14 @@ namespace Baikal
970970
{
971971
// TODO: optimize this linear search
972972
auto shape = static_cast<AreaLight const*>(light)->GetShape();
973-
974-
std::size_t idx = -1;
975-
973+
974+
std::size_t idx = 0;
976975
for (auto iter = scene.CreateShapeIterator(); iter->IsValid(); iter->Next(), ++idx)
977976
{
978977
if (iter->ItemAs<Shape const>() == shape)
979978
break;
980979
}
981-
980+
982981
clw_light->shapeidx = static_cast<int>(idx);
983982
clw_light->primidx = static_cast<int>(static_cast<AreaLight const*>(light)->GetPrimitiveIdx());
984983
break;

App/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ float g_ao_radius = 1.f;
107107
float g_envmapmul = 1.f;
108108
float g_cspeed = 10.25f;
109109

110-
float3 g_camera_pos = float3(0.f, 1.f, 4.f);
110+
float3 g_camera_pos = float3(0.f, 4.f, 12.f);
111111
float3 g_camera_at = float3(0.f, 1.f, 0.f);
112112
float3 g_camera_up = float3(0.f, 1.f, 0.f);
113113

@@ -348,8 +348,8 @@ void InitData()
348348

349349
{
350350
// Load OBJ scene
351-
std::unique_ptr<Baikal::SceneIo> scene_io(Baikal::SceneIo::CreateSceneIoObj());
352-
g_scene.reset(scene_io->LoadScene(filename, basepath));
351+
std::unique_ptr<Baikal::SceneIo> scene_io(Baikal::SceneIo::CreateSceneIoTest());
352+
g_scene.reset(scene_io->LoadScene("sphere+plane+area", basepath));
353353

354354
// Enable this to generate new materal mapping for a model
355355
#if 0
@@ -767,9 +767,15 @@ void Update()
767767
}*/
768768
}
769769

770-
if (g_num_samples == -1 || g_samplecount++ < g_num_samples)
770+
if (g_num_samples == -1 || g_samplecount < g_num_samples)
771771
{
772772
g_cfgs[g_primary].renderer->Render(*g_scene.get());
773+
++g_samplecount;
774+
}
775+
else if (g_samplecount == g_num_samples)
776+
{
777+
std::cout << "Target sample count reached\n";
778+
++g_samplecount;
773779
}
774780

775781
//if (std::chrono::duration_cast<std::chrono::seconds>(time - updatetime).count() > 1)

0 commit comments

Comments
 (0)