-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
305 lines (277 loc) · 10.8 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
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
#include <iostream>
#include <chrono>
#include "rasterization.hpp"
using namespace pipeline3D;
struct TextureData
{
double u;
double v;
TextureData() = default;
TextureData(double u, double v) : u(u), v(v) {}
};
struct TextureShader : public VertexShader<Pixel, TextureData, TextureShader>
{
BitmapImage texture;
TextureShader(BitmapImage &&texture) : texture(std::forward<BitmapImage>(texture)) {}
/**
* @brief The following functions defines the interpolation needed for vertices whose extra data is TextureData.
*
* @param v1
* @param v2
* @param w
* @return Vertex<TextureData>
*/
Vertex<TextureData> interpolate(const Vertex<TextureData> &v1,
const Vertex<TextureData> &v2,
double w)
{
const double w2 = (1.0f - w);
return Vertex<TextureData>(
(w * v1.x + w2 * v2.x), // x,
(w * v1.y + w2 * v2.y), // y,
(w * v1.z + w2 * v2.z), // z,
TextureData({
v1.vertexData.u * w + v2.vertexData.u * w2,
v1.vertexData.v * w + v2.vertexData.v * w2,
})); // VertexData
}
/**
* @brief The following function defines the perspective correction needed for vertices whose extra data is TextureData.
*
* @param v
*/
void perspectiveCorrect(Vertex<TextureData> &v)
{
v.z = 1.0f / v.z;
v.x *= v.z;
v.y *= v.z;
v.vertexData.u *= v.z;
v.vertexData.v *= v.z;
}
Pixel shade(Vertex<TextureData> v)
{
Pixel got = texture.pixel_for_uv(v.vertexData.u, v.vertexData.v);
return {got.r, got.g, got.b};
}
};
struct PixelShader : public VertexShader<Pixel, Pixel, PixelShader>
{
/**
* @brief The following functions defines the interpolation needed for vertices whose extra data
* is Pixel, meaning that it will be used to interpolate colored vertices.
*
*/
Vertex<Pixel> interpolate(const Vertex<Pixel> &v1,
const Vertex<Pixel> &v2,
double w)
{
const double w2 = (1.0f - w);
return Vertex<Pixel>(
(w * v1.x + w2 * v2.x), // x,
(w * v1.y + w2 * v2.y), // y,
(w * v1.z + w2 * v2.z), // z,
Pixel({static_cast<uint8_t>(v1.vertexData.r * w + v2.vertexData.r * w2),
static_cast<uint8_t>(v1.vertexData.g * w + v2.vertexData.g * w2),
static_cast<uint8_t>(v1.vertexData.b * w + v2.vertexData.b * w2)})); // VertexData
}
/**
* @brief The following function defines the perspective correction needed for vertices whose extra data is Pixel, meaning
* that it will be used to interpolate colored vertices.
*
* @param v
*/
void perspectiveCorrect(Vertex<Pixel> &v)
{
v.z = 1.0f / v.z;
v.x *= v.z;
v.y *= v.z;
}
Pixel shade(Vertex<Pixel> v)
{
return v.vertexData;
}
};
int main()
{
const bool benchmark = false;
const bool isObjectLevelMultithreaded = false;
const bool isPolygonLevelMultithreaded = false;
const bool isRenderScanlineLevelMultithreaded = true;
constexpr bool isMultithreaded = isObjectLevelMultithreaded || isPolygonLevelMultithreaded || isRenderScanlineLevelMultithreaded;
const bool writeOut = true;
// Working formats: 1000, 4000, 5000, 6000, 10000
const int w = 8000;
const int h = 8000;
const int fieldSize = w * h;
constexpr int numberOfThreads = 25;
Rasterizer<Pixel, double> rasterizer;
rasterizer.set_perspective_projection(-1, 1, -1, 1, 1, 2);
std::vector<Pixel> target(fieldSize, Pixel({0, 0, 0}));
std::vector<double> zBuffer(fieldSize, 1.0e8f);
rasterizer.set_target(w, h, &target[0], &zBuffer[0]);
constexpr Pixel red({255, 0, 0});
constexpr Pixel green({0, 255, 0});
constexpr Pixel blue({0, 0, 255});
constexpr Pixel yellow({255, 255, 0});
constexpr float slope = -0.2f;
// gradient
const Vertex<Pixel>
v1 = {1.2, -0.5, 2.5f + slope * (1 - 1), blue},
v2 = {1, 1, 1.5f + slope * (1 + 1), yellow},
v3 = {-0.2, 1, 2.5f - +slope * (-1 + 1), red},
v4 = {0.2, 0.2, 1.5f + slope * (-1 - 1), green};
// neon
const Vertex<TextureData>
d1 = {-1, -1.2, 1.5f + slope * (1 - 1), {0, 0}},
d2 = {-1, 0.2, 1.5f + slope * (1 + 1), {0, 1}},
d3 = {0.1, -0., 1.5f - +slope * (-1 + 1), {1, 1}},
d4 = {-0., -1.3, 1.5f + slope * (-1 - 1), {1, 0}};
// bear
const Vertex<TextureData>
p1 = {0.2, -1, 1.5f + slope * (1 - 1), {0, 0}},
p2 = {-0., 0.3, 1.5f + slope * (1 + 1), {0, 1}},
p3 = {1.3, 0.2, 1.5f - +slope * (-1 + 1), {1, 1}},
p4 = {1.4, -1, 1.5f + slope * (-1 - 1), {1, 0}};
// heinstein
const Vertex<TextureData>
l1 = {-1, 0., 1.5f + slope * (1 - 1), {0, 0}},
l2 = {-1, 1, 1.5f + slope * (1 + 1), {0, 1}},
l3 = {0.15, 1, 1.5f - +slope * (-1 + 1), {1, 1}},
l4 = {0., 0.2, 1.5f + slope * (-1 - 1), {1, 0}};
// bear 2 (overlapped by heinstein)
const Vertex<TextureData>
m1 = {-1, 0., 2.5f + slope * (1 - 1), {0, 0}},
m2 = {-1, 1, 2.5f + slope * (1 + 1), {0, 1}},
m3 = {0.15, 1, 1.5f - +slope * (-1 + 1), {1, 1}},
m4 = {0., 0.2, 1.5f + slope * (-1 - 1), {1, 0}};
ObjectTemplate<Pixel, TextureShader, TextureData> heinsteinMuralesObject(
//!--------------------EACH PATHNAME needs to be overwritten with the path where the images are stored.-----------------------------------
TextureShader(BitmapImage("............................./heinsteinMurales.bmp")),
{
{l1, l2, l3},
{l4, l1, l3},
});
ObjectTemplate<Pixel, TextureShader, TextureData> neonWorldObject(
//!--------------------EACH PATHNAME needs to be overwritten with the path where the images are stored.-----------------------------------
TextureShader(BitmapImage("............................./neonWorld4K.bmp")),
{
{d1, d2, d3},
{d4, d1, d3},
});
ObjectTemplate<Pixel, PixelShader, Pixel> gradientObject(
PixelShader(),
{
{v1, v2, v3},
{v4, v1, v3},
});
ObjectTemplate<Pixel, TextureShader, TextureData> bearObject(
//!--------------------EACH PATHNAME needs to be overwritten with the path where the images are stored.-----------------------------------
TextureShader(BitmapImage("............................./bear8k.bmp")),
{
{p1, p2, p3},
{p4, p1, p3},
});
ObjectTemplate<Pixel, TextureShader, TextureData> bearObject2(
//!--------------------EACH PATHNAME needs to be overwritten with the path where the images are stored.-----------------------------------
TextureShader(BitmapImage("............................./bear8k.bmp")),
{
{m1, m2, m3},
{m4, m1, m3},
});
Scene<Pixel> scene({&gradientObject, &bearObject, &neonWorldObject, &bearObject2, &heinsteinMuralesObject});
if (benchmark)
{
const uint8_t testCycles = 30;
std::cout << "starting benchmark for " << w << "x" << h << " target" << std::endl;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
// 25 Threads perform better with a Windows10 + Intel i7_10875H mobile (8 core/ 16 Threads)
thread_pool_manager<std::mutex *> pool(numberOfThreads);
if (isMultithreaded)
{
std::cout << "Number of threads created: " << pool.getPoolSize() << std::endl;
pool.protection = new std::mutex[fieldSize];
start_time = std::chrono::high_resolution_clock::now();
for (uint8_t i = 0; i < testCycles; i++)
{
scene.render(rasterizer, pool, isObjectLevelMultithreaded, isPolygonLevelMultithreaded, isRenderScanlineLevelMultithreaded);
}
pool.shutdown();
// protection method deallocation.
delete[] pool.protection;
std::chrono::time_point<std::chrono::high_resolution_clock> end_time = std::chrono::high_resolution_clock::now();
float elapsed_time = std::chrono::duration<float>(end_time - start_time).count();
std::cout << "elapsed time: " << elapsed_time << std::endl;
std::cout << "Done Rendering" << std::endl;
// final print.
if (writeOut)
{
//!--------------------This pathnames must be overwritten with the desired output path.-----------------------------------
stbi_write_bmp("............................./out_scene.bmp", w, h, 3, &target[0]);
}
}
else
{
pool.shutdown();
start_time = std::chrono::high_resolution_clock::now();
for (uint8_t i = 0; i < testCycles; i++)
{
scene.render(rasterizer, pool, isObjectLevelMultithreaded, isPolygonLevelMultithreaded, isRenderScanlineLevelMultithreaded);
}
std::chrono::time_point<std::chrono::high_resolution_clock> end_time = std::chrono::high_resolution_clock::now();
float elapsed_time = std::chrono::duration<float>(end_time - start_time).count();
std::cout << "elapsed time: " << elapsed_time << std::endl;
std::cout << "Done Rendering" << std::endl;
// final print.
if (writeOut)
{
//!--------------------This pathnames must be overwritten with the desired output path.-----------------------------------
stbi_write_bmp("............................./out_scene.bmp", w, h, 3, &target[0]);
}
}
}
else
{
// 25 Threads perform better with a Windows10 + Intel i7_10875H mobile (8 core/ 16 Threads)
thread_pool_manager<std::mutex *> pool(numberOfThreads);
std::cout << "starting benchmark for " << w << "x" << h << " target" << std::endl;
std::cout << "Write on file is: " << writeOut << std::endl;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
if (isMultithreaded)
{
std::cout << "Number of threads created: " << pool.getPoolSize() << std::endl;
pool.protection = new std::mutex[fieldSize];
start_time = std::chrono::high_resolution_clock::now();
scene.render(rasterizer, pool, isObjectLevelMultithreaded, isPolygonLevelMultithreaded, isRenderScanlineLevelMultithreaded);
pool.shutdown();
// protection method deallocation.
delete[] pool.protection;
std::chrono::time_point<std::chrono::high_resolution_clock> end_time = std::chrono::high_resolution_clock::now();
float elapsed_time = std::chrono::duration<float>(end_time - start_time).count();
std::cout << "elapsed time: " << elapsed_time << std::endl;
std::cout << "Done Rendering" << std::endl;
// final print.
if (writeOut)
{
//!--------------------This pathnames must be overwritten with the desired output path.-----------------------------------
stbi_write_bmp("............................./out_scene.bmp", w, h, 3, &target[0]);
}
}
else
{
pool.shutdown();
start_time = std::chrono::high_resolution_clock::now();
scene.render(rasterizer, pool, isObjectLevelMultithreaded, isPolygonLevelMultithreaded, isRenderScanlineLevelMultithreaded);
auto end_time = std::chrono::high_resolution_clock::now();
float elapsed_time = std::chrono::duration<float>(end_time - start_time).count();
std::cout << "elapsed time: " << elapsed_time << std::endl;
std::cout << "Done Rendering" << std::endl;
// final print.
if (writeOut)
{
//!--------------------This pathnames must be overwritten with the desired output path.-----------------------------------
stbi_write_bmp("............................./out_scene.bmp", w, h, 3, &target[0]);
}
}
}
return 0;
}