/
renderer_invmap_pssmlt.cpp
342 lines (282 loc) · 11.2 KB
/
renderer_invmap_pssmlt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
Lightmetrica - A modern, research-oriented renderer
Copyright (c) 2015 Hisanari Otsu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "inversemaputils.h"
#define INVERSEMAP_PSSMLT_DEBUG_SIMPLIFY_BDPT 0
LM_NAMESPACE_BEGIN
class PSSMLTState
{
private:
int maxNumVertices_;
std::vector<Float> usL_; // For light subpath
std::vector<Float> usE_; // For eye subpath
public:
PSSMLTState() {}
PSSMLTState(Random* rng, int maxNumVertices)
: maxNumVertices_(maxNumVertices)
{
const auto numStates = maxNumVertices * 3;
usL_.assign(numStates, 0_f);
usE_.assign(numStates, 0_f);
for (auto& u : usE_) u = rng->Next();
for (auto& u : usL_) u = rng->Next();
}
PSSMLTState(const PSSMLTState& o)
: maxNumVertices_(o.maxNumVertices_)
, usL_(o.usL_)
, usE_(o.usE_)
{}
public:
auto Swap(PSSMLTState& o) -> void
{
assert(maxNumVertices_ == o.maxNumVertices_);
usL_.swap(o.usL_);
usE_.swap(o.usE_);
}
public:
// Large step mutation
auto LargeStep(Random* rng) const -> PSSMLTState
{
PSSMLTState next(*this);
for (auto& u : next.usE_) u = rng->Next();
for (auto& u : next.usL_) u = rng->Next();
return next;
}
// Small step mutation
auto SmallStep(Random* rng) const -> PSSMLTState
{
const auto Perturb = [](Random& rng, const Float u, const Float s1, const Float s2)
{
Float result;
Float r = rng.Next();
if (r < 0.5_f)
{
r = r * 2_f;
result = u + s2 * std::exp(-std::log(s2 / s1) * r);
if (result > 1_f) result -= 1_f;
}
else
{
r = (r - 0.5_f) * 2_f;
result = u - s2 * std::exp(-std::log(s2 / s1) * r);
if (result < 0_f) result += 1_f;
}
return result;
};
const auto s1 = 1_f / 256_f;
const auto s2 = 1_f / 16_f;
PSSMLTState next(*this);
for (size_t i = 0; i < usE_.size(); i++) next.usE_[i] = Perturb(*rng, usE_[i], s1, s2);
for (size_t i = 0; i < usL_.size(); i++) next.usL_[i] = Perturb(*rng, usL_[i], s1, s2);
return next;
}
// Map a primary sample to a set of paths. Returns empty vector if failed.
struct CachedPaths
{
struct CachedPath
{
int s, t;
Path path;
SPD Cstar; // Caches unweighed contribution
Float w; // Caches MIS weight
};
std::vector<CachedPath> ps;
auto ScalarContrb() const -> Float
{
SPD C;
for (const auto& p : ps) C += p.Cstar * p.w;
return InversemapUtils::ScalarContrb(C);
}
};
auto InvCDF(const Scene3* scene) const -> CachedPaths
{
Subpath subpathE;
Subpath subpathL;
subpathE.SampleSubpathWithPrimarySamples(scene, usE_, TransportDirection::EL, maxNumVertices_);
subpathL.SampleSubpathWithPrimarySamples(scene, usL_, TransportDirection::LE, maxNumVertices_);
CachedPaths paths;
const int nL = (int)(subpathL.vertices.size());
const int nE = (int)(subpathE.vertices.size());
for (int n = 2; n <= nE + nL; n++)
{
if (n > maxNumVertices_) { continue; }
const int minS = Math::Max(0, n - nE);
const int maxS = Math::Min(nL, n);
for (int s = minS; s <= maxS; s++)
{
const int t = n - s;
CachedPaths::CachedPath p;
p.s = s;
p.t = t;
if (!p.path.ConnectSubpaths(scene, subpathL, subpathE, s, t)) { continue; }
p.Cstar = p.path.EvaluateUnweightContribution(scene, s);
if (p.Cstar.Black())
{
continue;
}
p.w = p.path.EvaluateMISWeight(scene, s);
paths.ps.push_back(std::move(p));
}
}
return paths;
}
};
///! Primary sample space metropolis light transport (BDPT path sampler)
class Renderer_Invmap_PSSMLT final : public Renderer
{
public:
LM_IMPL_CLASS(Renderer_Invmap_PSSMLT, Renderer);
public:
int maxNumVertices_;
long long numMutations_;
double renderTime_;
long long numSeedSamples_;
double seedRenderTime_;
Float largeStepProb_;
#if INVERSEMAP_OMIT_NORMALIZATION
Float normalization_;
#endif
Float s1_;
Float s2_;
public:
LM_IMPL_F(Initialize) = [this](const PropertyNode* prop) -> bool
{
if (!prop->ChildAs<int>("max_num_vertices", maxNumVertices_)) return false;
numMutations_ = prop->ChildAs<long long>("num_mutations", 0);
renderTime_ = prop->ChildAs<double>("render_time", -1);
numSeedSamples_ = prop->ChildAs<long long>("num_seed_samples", 0);
seedRenderTime_ = prop->ChildAs<double>("seed_render_time", -1);
largeStepProb_ = prop->ChildAs<Float>("large_step_prob", 0.5_f);
#if INVERSEMAP_OMIT_NORMALIZATION
normalization_ = prop->ChildAs<Float>("normalization", 1_f);
#endif
return true;
};
LM_IMPL_F(Render) = [this](const Scene* scene_, Random* initRng, const std::string& outputPath) -> void
{
const auto* scene = static_cast<const Scene3*>(scene_);
auto* film = static_cast<const Sensor*>(scene->GetSensor()->emitter)->GetFilm();
// --------------------------------------------------------------------------------
#pragma region Compute normalization factor
#if INVERSEMAP_OMIT_NORMALIZATION
const auto b = normalization_;
#else
throw std::runtime_error("TBA");
#endif
// --------------------------------------------------------------------------------
#pragma region Rendering
{
LM_LOG_INFO("Rendering");
LM_LOG_INDENTER();
// --------------------------------------------------------------------------------
// Thread-specific context
struct Context
{
Random rng;
Film::UniquePtr film{ nullptr, nullptr };
PSSMLTState currState;
};
std::vector<Context> contexts(Parallel::GetNumThreads());
for (auto& ctx : contexts)
{
ctx.rng.SetSeed(initRng->NextUInt());
ctx.film = ComponentFactory::Clone<Film>(film);
// Initial state
while (true)
{
// Generate initial path with bidirectional path tracing
PSSMLTState state(initRng, maxNumVertices_);
const auto paths = state.InvCDF(scene);
if (paths.ps.empty())
{
continue;
}
ctx.currState = std::move(state);
break;
}
}
// --------------------------------------------------------------------------------
const auto processed = Parallel::For({ renderTime_ < 0 ? ParallelMode::Samples : ParallelMode::Time, numMutations_, renderTime_ }, [&](long long index, int threadid, bool init) -> void
{
auto& ctx = contexts[threadid];
// --------------------------------------------------------------------------------
#pragma region Mutation in primary sample space
{
// Mutate
auto propState = ctx.rng.Next() < largeStepProb_
? ctx.currState.LargeStep(&ctx.rng)
: ctx.currState.SmallStep(&ctx.rng);
// Paths
const auto currPs = ctx.currState.InvCDF(scene);
const auto propPs = propState.InvCDF(scene);
#if INVERSEMAP_PSSMLT_DEBUG_SIMPLIFY_BDPT
// Always accept
ctx.currState.Swap(propState);
#else
// Scalar contributions
const auto currC = currPs.ScalarContrb();
const auto propC = propPs.ScalarContrb();
// MH update
const auto A = currC == 0_f ? 1_f : Math::Min(1_f, propC / currC);
if (ctx.rng.Next() < A)
{
ctx.currState.Swap(propState);
}
#endif
}
#pragma endregion
// --------------------------------------------------------------------------------
#pragma region Accumulate contribution
{
const auto ps = ctx.currState.InvCDF(scene);
const auto I = ps.ScalarContrb();
for (const auto& p : ps.ps)
{
const auto C = p.Cstar * p.w;
#if INVERSEMAP_PSSMLT_DEBUG_SIMPLIFY_BDPT
ctx.film->Splat(p.path.RasterPosition(), C);
#else
ctx.film->Splat(p.path.RasterPosition(), C * (b / I));
#endif
}
}
#pragma endregion
});
// --------------------------------------------------------------------------------
// Gather & Rescale
film->Clear();
for (auto& ctx : contexts)
{
film->Accumulate(ctx.film.get());
}
film->Rescale((Float)(film->Width() * film->Height()) / processed);
}
#pragma endregion
// --------------------------------------------------------------------------------
#pragma region Save image
{
LM_LOG_INFO("Saving image");
LM_LOG_INDENTER();
film->Save(outputPath);
}
#pragma endregion
};
};
LM_COMPONENT_REGISTER_IMPL(Renderer_Invmap_PSSMLT, "renderer::invmap_pssmlt");
LM_NAMESPACE_END