This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
235 lines (203 loc) · 5.1 KB
/
main.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
#include "Scene/Scene.hpp"
#include "Image/PNG.hpp"
#include "Profiler/Profiler.hpp"
#include "Menger/Menger.hpp"
#include <iostream>
#include <chrono>
#include <atomic>
#include <thread>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
using namespace isim;
// Thread pools
struct renderJob
{
float l;
float r;
float t;
float b;
float focal;
Vector startPos;
Vector up;
Vector reversedDir;
Vector forward;
std::uint32_t width;
std::uint32_t height;
Color* pixels;
std::uint32_t startHeight;
std::uint32_t endHeight;
};
struct renderResult
{
Color color;
std::uint32_t pos;
};
static Scene* renderScene = nullptr;
static std::vector<std::thread> threadsPool;
static int threadsCount;
static bool terminate = false;
static std::queue<renderJob> renderQueue;
static std::mutex renderQueueMutex;
static std::condition_variable renderQueueMutexCv;
static int renderDone;
static std::mutex resultQueueMutex;
static std::condition_variable resultQueueMutexCv;
void render_loop(int threadId)
{
while (true)
{
renderJob job;
{
std::unique_lock<std::mutex> lock(renderQueueMutex);
renderQueueMutexCv.wait(lock, []{return !renderQueue.empty();});
if (terminate)
{
return;
}
job = renderQueue.front();
renderQueue.pop();
}
auto MAX_DEPTH = renderScene->getDepth();
for (std::uint32_t x = 0; x < job.width; x++)
{
for (std::uint32_t y = job.startHeight; y < job.endHeight; y++)
{
float u = job.l + ((job.r-job.l)*(x + 0.5)) / job.width;
float v = job.b + ((job.t-job.b)*(y + 0.5)) / job.height;
auto pos = (job.height - 1 - y) * job.width + x;
Ray ray(job.startPos, job.reversedDir * -job.focal + job.up * u + job.forward * v);
ray.threadId = threadId;
job.pixels[pos] = renderScene->castRay(ray, MAX_DEPTH);
}
}
renderResult result;
{
std::unique_lock<std::mutex> lock(resultQueueMutex);
renderDone++;
resultQueueMutexCv.notify_all();
}
}
}
int pool_amount()
{
return threadsCount;
}
void setup_pool(Scene* scene)
{
std::cout << "Setting up rendering pool...";
for (unsigned int i = 0; i < std::thread::hardware_concurrency(); i++)
{
std::cout << "\nCreated render thread: " << i;
threadsPool.push_back(std::thread(render_loop, threadsCount++));
}
std::cout << std::endl;
renderScene = scene;
}
void shutdown_pool()
{
{
std::unique_lock<std::mutex> lock(renderQueueMutex);
renderJob emptyJob;
renderQueue.push(emptyJob);
terminate = true;
renderQueueMutexCv.notify_all();
}
std::cout << "Shutting down render pool..." << std::endl;
for (auto& thread: threadsPool)
{
if (thread.joinable())
{
thread.join();
}
}
}
const double RADIAN = 0.01745329251;
void snapshot(const std::unique_ptr<Camera>& camera, const std::unique_ptr<PNG>& png);
int main(int argc, char** argv)
{
if (argc != 3 || !argv[1][0] || !argv[2][0])
{
return 0;
}
std::fstream file(argv[1]);
assert(file.is_open());
std::unique_ptr<PNG> png = std::make_unique<PNG>(argv[2], 2560, 1440);
Scene scene(nlohmann::json::parse(file));
setup_pool(&scene);
auto& camera = *scene.getCameras().begin();
auto& objs = scene.getObjects();
for (auto& obj : objs)
{
auto sponge = dynamic_cast<const Menger*>(obj.get());
if (sponge != nullptr)
{
auto ratio = (((sponge->getPosition() - camera->getPosition()).GetLength())/sponge->getSideLength());
int nbIter = 0;
if (ratio >= 1.0)
{
nbIter = std::floor(1.0 / ratio * 10) + 2;
}
else
{
nbIter = std::floor((1.0 - ratio) / 0.1) + 10;
}
((Menger*)sponge)->setIterations(nbIter);
}
}
snapshot(camera, png);
shutdown_pool();
}
void snapshot(const std::unique_ptr<Camera>& camera, const std::unique_ptr<PNG>& png)
{
float hh = std::tan(camera->getFov() * RADIAN / 2.0) * camera->getFocal();
float hw = static_cast<float>(png->GetWidth()) / static_cast<float>(png->GetHeight()) * hh;
float l = -hw, r = hw, t = hh, b = -hh;
auto width = png->GetWidth(), height = png->GetHeight();
Vector reversedDir = camera->getPosition() - camera->getFocus();
reversedDir.Normalize();
Vector up = camera->getUp().Cross(reversedDir);
up.Normalize();
Vector forward = reversedDir.Cross(up);
forward.Normalize();
renderJob job;
job.l = l;
job.r = r;
job.t = t;
job.b = b;
job.startPos = camera->getPosition();
job.focal = camera->getFocal();
job.up = up;
job.reversedDir = reversedDir;
job.forward = forward;
job.height = height;
job.width = width;
job.pixels = png->GetPixels();
auto threadCount = pool_amount();
{
std::unique_lock<std::mutex> lock(resultQueueMutex);
renderDone = 0;
for (auto i = 0; i < threadCount; i++)
{
auto chunkSize = height / threadCount;
job.startHeight = chunkSize * i;
job.endHeight = (i == threadCount) ? height : chunkSize * (i + 1);
std::unique_lock<std::mutex> lock(renderQueueMutex);
renderQueue.push(job);
renderQueueMutexCv.notify_one();
}
}
for (auto k = 0; k < threadCount;)
{
{
std::unique_lock<std::mutex> lock(resultQueueMutex);
resultQueueMutexCv.wait(lock, [t=threadCount]
{
return renderDone == t;
});
k = renderDone;
}
}
png->OnUpdate();
}