This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.cc
133 lines (116 loc) · 3.97 KB
/
renderer.cc
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
// Copyright (c) 2016 Kai Luo. All rights reserved.
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <atomic>
#include <chrono>
#include <thread>
#include "CIE.h"
#include "color.h"
#include "core_math.h"
#include "integrator.h"
#include "kl/logger.h"
#include "renderer.h"
#include "scene.h"
namespace zLi {
const Float Renderer::SampleRadius = 0.5;
Renderer::Renderer(const std::string &scene_file, int film_width,
int film_height, int spp)
: scene_file_(scene_file), film_width_(film_width),
film_height_(film_height), render_job_(0), stopped_(false), spp_(spp) {
xyY_chan_ = std::make_shared<kl::Chan<RenderResult>>();
}
Spectrum Renderer::SampleSpectrumAt(Float x, Float y) {
Float l = Float{std::max(film_width_ + 2 * SampleRadius,
film_height_ + 2 * SampleRadius)};
Float tx = (film_width_ + 2 * SampleRadius) / (2 * l);
Float ty = (film_height_ + 2 * SampleRadius) / (2 * l);
// normalize
Float nx = (x + SampleRadius) / l - tx;
Float ny = ty - (y + SampleRadius) / l;
// DEBUG("screen axis: (%f,%f), normalized axis: (%f,%f)", x, y, nx, ny);
Ray ray = scene_->GenerateRay(nx, ny);
// DEBUG("ray direction: (%f, %f, %f)", ray.d.x, ray.d.y, ray.d.z);
return PathIntegrator::Li(*scene_, ray);
}
// static xyYColor PrimaryColors[] = {
// xyYColor(0.64, 0.33, 0.2126), xyYColor(0.3, 0.6, 0.7152),
// xyYColor(0.15, 0.06, 0.0722),
// xyYColor(0.3127, 0.329, 1.0), // red, green, blue, white
// };
void Renderer::AddToxyYChan(int i, int j, const Spectrum &s) {
xyY_chan_->Push(RenderResult{
.x = i, .y = j, .xyY = s.ToxyY(),
});
}
void Renderer::Work(int i, int j) {
// DEBUG("working on pixel (%d,%d)", i, j);
Spectrum res(0);
for (int k = 0; k < spp_; ++k) {
auto sx = SampleRadius * (2 * UniformSample() - 1);
auto sy = SampleRadius * (2 * UniformSample() - 1);
// DEBUG("sample offset (%f, %f)", sx, sy);
Float x = i + sx;
Float y = j + sy;
assert(std::abs(x - i) <= SampleRadius);
assert(std::abs(y - j) <= SampleRadius);
// DEBUG("pixel sample (%f, %f)", x, y);
Spectrum s = SampleSpectrumAt(x, y);
Float r = std::sqrt((x - i) * (x - i) + (y - j) * (y - j));
s *= filter_.f(r) * (1.f / spp_);
res += s;
}
AddToxyYChan(i, j, res);
// add to film
assert(film_);
film_->Set(i, j, std::move(res));
}
void Renderer::Stop() { stopped_.store(true); }
bool Renderer::Stopped() { return stopped_; }
kl::Result<void> Renderer::SlowRender() {
int n = render_job_++;
while (!stopped_ && n < film_width_ * film_height_) {
KL_INFO("start working on No.%d job", n);
Work(n / film_height_, n % film_height_);
n = render_job_++;
}
stopped_.store(true);
return kl::Ok();
}
kl::Result<void> Renderer::ParallelRender() {
std::vector<std::thread> workers;
auto start = std::chrono::high_resolution_clock::now();
for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) {
workers.push_back(std::thread([this] {
while (!stopped_) {
int n = render_job_++;
KL_INFO("start working on No.%d job", n);
if (n >= film_width_ * film_height_) {
break;
}
Work(n / film_height_, n % film_height_);
}
}));
}
for (auto &w : workers) {
w.join();
}
std::chrono::duration<Float> diff =
std::chrono::high_resolution_clock::now() - start;
KL_DEBUG("time elapse: %fs", diff.count());
stopped_.store(true);
return kl::Ok();
}
kl::Result<void> Renderer::Render() {
auto res = Scene::SceneFromJson(scene_file_);
if (!res) {
KL_DEBUG("can't load scene file: %s", scene_file_.c_str());
return kl::Err("can't load scene file: %s", scene_file_.c_str());
}
scene_ = std::make_unique<Scene>(std::move(*res));
film_ = std::make_unique<Film>(film_width_, film_height_);
return ParallelRender();
// return SlowRender();
}
Film Renderer::MoveRenderResult() { return std::move(*film_); }
Renderer::~Renderer() {}
} // namespace zLi