Skip to content

Commit 4802c3b

Browse files
committed
Beautify code for eye/light sampling
1 parent 3a97aea commit 4802c3b

File tree

3 files changed

+71
-62
lines changed

3 files changed

+71
-62
lines changed

include/slg/engines/pathtracer.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,25 @@ class PathTracerThreadState {
5555
const bool useFilmSplat = false);
5656
virtual ~PathTracerThreadState();
5757

58+
SamplerRef GetEyeSampler() { return *eyeSampler; }
59+
SamplerRef GetLightSampler() { return *lightSampler; }
60+
5861
luxrays::IntersectionDevice *device;
5962

60-
const SamplerUPtr& eyeSampler;
61-
const SamplerUPtr& lightSampler;
6263
SceneConstRef scene;
6364
FilmRef GetFilm() { return film; }
6465
FilmConstRef GetFilm() const { return film; }
6566
const VarianceClamping *varianceClamping;
6667

67-
std::vector<SampleResult> & GetEyeSampleResults() { return eyeSampleResults; }
68-
std::vector<SampleResult> & GetLightSampleResults() { return lightSampleResults; }
68+
std::vector<SampleResult> & GetEyeSampleResults() { return std::ref(eyeSampleResults); }
69+
std::vector<SampleResult> & GetLightSampleResults() { return std::ref(lightSampleResults); }
6970

7071
// Used for hybrid rendering
7172
double eyeSampleCount, lightSampleCount;
7273

7374
private:
75+
const SamplerUPtr& eyeSampler;
76+
const SamplerUPtr& lightSampler;
7477
FilmRef film;
7578
std::vector<SampleResult> eyeSampleResults, lightSampleResults;
7679
};

src/slg/engines/bakecpu/bakecputhread.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,29 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
144144

145145
// Pick a scene object to sample
146146
float sceneObjPickPdf;
147-
const u_int currentSceneObjIndex = engine->currentSceneObjsDist->SampleDiscrete(state.eyeSampler->GetSample(0), &sceneObjPickPdf);
147+
const u_int currentSceneObjIndex = engine->currentSceneObjsDist->SampleDiscrete(state.GetEyeSampler().GetSample(0), &sceneObjPickPdf);
148148
auto& sceneObj = *engine->currentSceneObjsToBake[currentSceneObjIndex];
149149
auto& mesh = sceneObj.GetExtMesh();
150150

151151
// Pick a triangle to sample
152152
float triPickPdf;
153-
const u_int triangleIndex = engine->currentSceneObjDist[currentSceneObjIndex]->SampleDiscrete(state.eyeSampler->GetSample(1), &triPickPdf);
153+
const u_int triangleIndex = engine->currentSceneObjDist[currentSceneObjIndex]->SampleDiscrete(state.GetEyeSampler().GetSample(1), &triPickPdf);
154154

155-
const float timeSample = state.eyeSampler->GetSample(4);
155+
const float timeSample = state.GetEyeSampler().GetSample(4);
156156
Transform localToWorld;
157157
mesh.GetLocal2World(timeSample, localToWorld);
158158

159159
// Origin
160160
Point samplePoint;
161161
float b0, b1, b2;
162-
mesh.Sample(localToWorld, triangleIndex, state.eyeSampler->GetSample(2), state.eyeSampler->GetSample(3),
162+
mesh.Sample(localToWorld, triangleIndex, state.GetEyeSampler().GetSample(2), state.GetEyeSampler().GetSample(3),
163163
&samplePoint, &b0, &b1, &b2);
164164

165165
const u_int sceneObjIndex = state.scene.GetObjects().GetSceneObjectIndex(sceneObj);
166166
const PathVolumeInfo volInfo;
167167
BSDF bsdf(state.scene, sceneObjIndex, triangleIndex,
168168
samplePoint, b1, b2,
169-
timeSample, state.eyeSampler->GetSample(pathTracer.eyeSampleSize), &volInfo);
169+
timeSample, state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize), &volInfo);
170170

171171
//--------------------------------------------------------------------------
172172
// Set up the sample result
@@ -191,11 +191,11 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
191191

192192
const PathTracer::DirectLightResult directLightResult = pathTracer.DirectLightSampling(state.device, state.scene,
193193
timeSample,
194-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 3),
195-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 4),
196-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 5),
197-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 6),
198-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 7),
194+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 3),
195+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 4),
196+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 5),
197+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 6),
198+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 7),
199199
pathInfo,
200200
Spectrum(1.f), bsdf, &sampleResult);
201201

@@ -216,8 +216,8 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
216216
float bsdfPdfW;
217217
BSDFEvent bsdfEvent;
218218
const Spectrum bsdfSample = bsdf.Sample(&sampledDir,
219-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 1),
220-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 2),
219+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 1),
220+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 2),
221221
&bsdfPdfW, &cosSampledDir, &bsdfEvent);
222222

223223
pathInfo.AddVertex(bsdf, bsdfEvent, bsdfPdfW, pathTracer.hybridBackForwardGlossinessThreshold);
@@ -235,7 +235,7 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
235235

236236
pathTracer.RenderEyePath(
237237
state.device, state.scene,
238-
*state.eyeSampler,
238+
state.GetEyeSampler(),
239239
pathInfo,
240240
eyeRay,
241241
bsdfSample,
@@ -259,11 +259,11 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
259259

260260
const PathTracer::DirectLightResult directLightResult = pathTracer.DirectLightSampling(state.device, state.scene,
261261
timeSample,
262-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 3),
263-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 4),
264-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 5),
265-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 6),
266-
state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 7),
262+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 3),
263+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 4),
264+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 5),
265+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 6),
266+
state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 7),
267267
pathInfo,
268268
Spectrum(1.f), bsdf, &sampleResult,
269269
false);
@@ -280,8 +280,8 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
280280
//--------------------------------------------------------------
281281

282282
// Ray direction
283-
const float u1 = state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 1);
284-
const float u2 = state.eyeSampler->GetSample(pathTracer.eyeSampleSize + 2);
283+
const float u1 = state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 1);
284+
const float u2 = state.GetEyeSampler().GetSample(pathTracer.eyeSampleSize + 2);
285285
const Vector localSampledDir = UniformSampleHemisphere(u1, u2);
286286
const Vector sampledDir = bsdf.GetFrame().ToWorld(localSampledDir);
287287

@@ -301,7 +301,7 @@ void BakeCPURenderThread::RenderEyeSample(const BakeMapInfo &mapInfo, PathTracer
301301

302302
const float NdotL = Dot(bsdf.hitPoint.shadeN, sampledDir);
303303
pathTracer.RenderEyePath(state.device, state.scene,
304-
*state.eyeSampler, pathInfo, eyeRay, Spectrum(NdotL * INV_PI / samplePdf),
304+
state.GetEyeSampler(), pathInfo, eyeRay, Spectrum(NdotL * INV_PI / samplePdf),
305305
state.GetEyeSampleResults());
306306
}
307307
break;
@@ -365,7 +365,7 @@ void BakeCPURenderThread::RenderLightSample(const BakeMapInfo &mapInfo, PathTrac
365365
const PathTracer::ConnectToEyeCallBackType connectToEyeCallBack = std::bind(
366366
&BakeCPURenderThread::RenderConnectToEyeCallBack, this, mapInfo, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5);
367367

368-
pathTracer.RenderLightSample(state.device, state.scene, state.GetFilm(), *state.lightSampler,
368+
pathTracer.RenderLightSample(state.device, state.scene, state.GetFilm(), state.GetLightSampler(),
369369
state.GetLightSampleResults(), connectToEyeCallBack);
370370
}
371371

@@ -374,21 +374,22 @@ void BakeCPURenderThread::RenderSample(const BakeMapInfo &mapInfo, PathTracerThr
374374
const PathTracer &pathTracer = engine->pathTracer;
375375

376376
// Check if I have to trace an eye or light path
377-
const SamplerUPtr& sampler =
378-
pathTracer.HasToRenderEyeSample(state) ? state.eyeSampler : state.lightSampler;
377+
bool eyeSampling = pathTracer.HasToRenderEyeSample(state);
378+
SamplerRef sampler =
379+
eyeSampling ? state.GetEyeSampler() : state.GetLightSampler();
379380

380-
std::vector<SampleResult>& sampleResults = pathTracer.HasToRenderEyeSample(state) ?
381+
std::vector<SampleResult>& sampleResults = eyeSampling ?
381382
state.GetEyeSampleResults() : state.GetLightSampleResults();
382383

383-
if (sampler == state.eyeSampler)
384+
if (eyeSampling)
384385
RenderEyeSample(mapInfo, state);
385386
else
386387
RenderLightSample(mapInfo, state);
387388

388389
// Variance clamping
389390
pathTracer.ApplyVarianceClamp(state, sampleResults);
390391

391-
sampler->NextSample(sampleResults);
392+
sampler.NextSample(sampleResults);
392393
}
393394

394395
void BakeCPURenderThread::RenderFunc(std::stop_token stop_token) {

src/slg/engines/pathtracer.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -976,40 +976,45 @@ void PathTracer::ApplyVarianceClamp(const PathTracerThreadState &state,
976976
}
977977

978978
void PathTracer::RenderSample(PathTracerThreadState &state) const {
979-
// Check if I have to trace an eye or light path
980-
Sampler *sampler;
981-
vector<SampleResult> *sampleResults;
982-
if (HasToRenderEyeSample(state)) {
983-
// Trace an eye path
984-
sampler = state.eyeSampler.get();
985-
sampleResults = &state.GetEyeSampleResults();
986-
} else {
987-
// Trace a light path
988-
sampler = state.lightSampler.get();
989-
sampleResults = &state.GetLightSampleResults();
990-
}
991979

992-
if (sampler == state.eyeSampler.get())
993-
RenderEyeSample(
994-
state.device,
995-
state.scene,
996-
state.GetFilm(),
997-
*state.eyeSampler,
998-
state.GetEyeSampleResults()
999-
);
1000-
else
1001-
RenderLightSample(
1002-
state.device,
1003-
state.scene,
1004-
state.GetFilm(),
1005-
*state.lightSampler,
1006-
state.GetLightSampleResults()
1007-
);
980+
auto Render = [&]() {
1008981

1009-
// Variance clamping
1010-
ApplyVarianceClamp(state, *sampleResults);
982+
// Check if I have to trace an eye or light path
983+
if (HasToRenderEyeSample(state)) {
984+
// Trace an eye path
985+
auto& sampler = state.GetEyeSampler();
986+
auto& sampleResults = state.GetEyeSampleResults();
987+
RenderEyeSample(
988+
state.device,
989+
state.scene,
990+
state.GetFilm(),
991+
sampler,
992+
sampleResults
993+
);
994+
return std::make_tuple(std::ref(sampler), std::ref(sampleResults));
995+
} else {
996+
// Otherwise trace a light path
997+
auto& sampler = state.GetLightSampler();
998+
auto& sampleResults = state.GetLightSampleResults();
999+
RenderLightSample(
1000+
state.device,
1001+
state.scene,
1002+
state.GetFilm(),
1003+
sampler,
1004+
sampleResults
1005+
);
1006+
return std::make_tuple(std::ref(sampler), std::ref(sampleResults));
1007+
}
1008+
}; // lambda
1009+
1010+
// Render sample
1011+
auto [sampler, sampleResults] = Render();
1012+
assert(&sampleResults == &state.GetEyeSampleResults() || &sampleResults == &state.GetLightSampleResults());
1013+
1014+
// Apply variance clamping
1015+
ApplyVarianceClamp(state, sampleResults);
10111016

1012-
sampler->NextSample(*sampleResults);
1017+
sampler.NextSample(sampleResults);
10131018
}
10141019

10151020
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)