-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.cpp
154 lines (124 loc) · 4.39 KB
/
renderer.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
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <memory>
#include <sys/sysinfo.h> // number of cpu
//#include <execution>
//#include <tbb/tbb.h>
#include "renderer.h" // inteface to this module
#include "image.h"
#include "utils.h"
#include "raytracer.h"
#include "random.h"
#include "constants.h"
#include "camera.h"
#include "scene.h"
#include "thread-pool.h"
using std::cout;
using std::endl;
struct Renderer {
Image image{frame_columns, frame_rows};
CameraRegular camera{{-3,7,2}, {0,0,0}, {0,0,1}, 40, float{frame_columns} / frame_rows /* aspect */};
RTCDevice device;
std::unique_ptr<RayTracer> rt;
glm::vec3 getSample(float xf_rand, float yf_rand)
{
RTCRayHit rayh = initRayHit();
camera.getRay(xf_rand/frame_columns, yf_rand/frame_rows, rayh.ray);
return rt->rayColor(rayh);
}
void renderRegion(std::size_t x_start, std::size_t x_stop,
std::size_t y_start, std::size_t y_stop)
{
for (std::size_t yn = y_start; yn < y_stop; yn++) {
for (std::size_t xn = x_start; xn < x_stop; xn++) {
const float xf = static_cast<const float>(xn);
const float yf = static_cast<const float>(yn);
glm::vec3 color {0.0};
for (int s = 0; s < samples; s++) {
const float yf_rand = yf + randomns::get01();
const float xf_rand = xf + randomns::get01();
color += getSample(xf_rand, yf_rand);
}
color /= samples;
color = glm::vec3{glm::clamp(0.0f, 1.0f, color.x),
glm::clamp(0.0f, 1.0f, color.y),
glm::clamp(0.0f, 1.0f, color.z)};
color = glm::vec3{std::sqrt(color.x), std::sqrt(color.y), std::sqrt(color.z)};
// Sample position (0,0) is top left corner of mage, but image.setColor(0,0) is
// position of top left corner. We need to invert Y axis here.
image.setColor(xn, frame_rows - 1 - yn, color);
}
}
}
void renderTile(std::size_t tile) {
TileInfo t{tile};
renderRegion(t.c_start, t.c_stop, t.r_start, t.r_stop);
}
void renderAllWithOMP() {
#pragma omp parallel for schedule(dynamic, 1)
for (std::size_t tile = 0; tile < tiles_rows*tiles_columns; tile++) {
renderTile(tile);
}
image.writeToFileBMP("output.bmp");
}
void renderAllWithThreadPool() {
ThreadPool pool{get_nprocs()};
for (std::size_t tile = 0; tile < tiles_rows*tiles_columns; tile++) {
pool.pushTask([this, tile](int){
renderTile(tile);
});
}
pool.shutdown();
// Different experiments with parallelisation
// std::vector<std::size_t> tiles_list(tiles_rows*tiles_columns);
// for (std::size_t i = 0; i < tiles_list.size(); i++) {
// tiles_list[i] = i;
// }
//tbb::parallel_for(std::size_t(0), std::size_t(tiles_rows*tiles_columns), )
//std::for_each(std::execution::par, std::begin(tiles_list), std::end(tiles_list), render_tile);
image.writeToFileBMP("output.bmp");
}
void writeImage(const char* path) {
image.writeToFileBMP(path);
}
Renderer() {
device = rtcNewDevice("threads=1,isa=avx");
if (!device) {
throw "rtcNewDevice";
}
//RTCScene scene = cubeX2InstanceScene(device);
//RTCScene scene = cubeScene(device);
//RTCScene scene = spheresScene(device);
auto [scene, geometry_id, data] = spheresSmallScene(device);
rt = std::make_unique<RayTracer>(scene, std::move(data));
}
~Renderer() {
// TODO: free buffers?
// Release device and all referenced things.
rtcReleaseDevice(device);
}
};
//========================================
// class DRenderer
// Simple interface to Renderer class.
//========================================
DRenderer::DRenderer(): r{new Renderer{}}
{
}
DRenderer::~DRenderer() {
delete r;
}
void DRenderer::renderAllTiles() {
r->renderAllWithOMP();
//r->renderAllWithThreadPool();
}
void DRenderer::renderTile(int tile_id) {
r->renderTile(tile_id);
}
void DRenderer::writeImage() {
r->writeImage("output.bmp");
}
Image& DRenderer::getImageRef() {
return r->image;
}