-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
354 lines (295 loc) · 11.1 KB
/
Copy pathmain.cpp
File metadata and controls
354 lines (295 loc) · 11.1 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
#include "./vec3.hpp"
#include "./color.hpp"
#include "./ray.hpp"
#include "./random.hpp"
#include "./camera.hpp"
#include "./intersection_record.hpp"
#include "./intersection_table_sphere.hpp"
#include "./intersection_table_collection.hpp"
#include "./material_dielectric.hpp"
#include "./material_lambertian.hpp"
#include "./material_metal.hpp"
#include <charconv>
#include <iostream>
#include <fstream>
#include <limits>
#include <memory>
#include <random>
#include <functional>
#include <atomic>
#include <thread>
#include <vector>
#include <cassert>
#include <cstring>
#include <cstdlib>
namespace rtow {
struct light_transport_simple_parameters
{
int spp;
int bounces;
};
color ray_color( const intersection_table& intersector, basic_random_generator& gen, const ray& r, int bounces );
void process_scanlines(
int start,
int end,
int height,
int width,
const camera& camera,
const intersection_table& world,
const light_transport_simple_parameters& p,
std::vector<color>& framebuffer );
} // namespace rtow
int main( int argc, char* argv[] )
{
using namespace rtow;
bool progress = false;
int height = 800;
float aspect_ratio = 3.f / 2.f;
int spp = 500;
int bounces = 50;
float vfov = 20.f;
int output_stream_index = -1;
auto hardware_concurrency = std::thread::hardware_concurrency();
unsigned int thread_count = hardware_concurrency;
std::ios::sync_with_stdio( false );
for ( int i = 0; i < argc; ++i )
{
if ( strstr( argv[i], "-h" ) || strstr( argv[i], "--height" ) )
{
const char* arg = argv[i + 1];
const std::size_t size = std::strlen( arg );
std::from_chars( arg, arg + size, height );
++i;
}
else if ( strstr( argv[i], "-as" ) || strstr( argv[i], "--aspect-ratio" ) )
{
const char* arg = argv[i + 1];
const std::size_t size = std::strlen( arg );
std::from_chars( arg, arg + size, aspect_ratio );
++i;
}
else if ( strstr( argv[i], "-spp" ) || strstr( argv[i], "--sample-per-pixel" ) )
{
const char* arg = argv[i + 1];
const std::size_t size = std::strlen( arg );
std::from_chars( arg, arg + size, spp );
++i;
}
else if ( strstr( argv[i], "--bounces" ) )
{
const char* arg = argv[i + 1];
const std::size_t size = std::strlen( arg );
std::from_chars( arg, arg + size, bounces );
++i;
}
else if ( strstr( argv[i], "--progress" ) )
{
progress = true;
}
else if ( strstr( argv[i], "-vfov" ) || strstr( argv[i], "--vertical-field-of-view" ) )
{
const char* arg = argv[i + 1];
const std::size_t size = std::strlen( arg );
std::from_chars( arg, arg + size, vfov );
++i;
}
else if ( strstr( argv[i], "-o" ) || strstr( argv[i], "--output" ) )
{
output_stream_index = i + 1;
++i;
}
else if ( strstr( argv[i], "-j" ) || strstr( argv[i], "--jobs" ) )
{
const char* arg = argv[i + 1];
const std::size_t size = std::strlen( arg );
int concurrency = 0;
std::from_chars( arg, arg + size, concurrency );
++i;
if ( concurrency == 0 )
{
thread_count = hardware_concurrency;
}
else if ( concurrency > 0 )
{
thread_count = concurrency;
}
else // if ( concurrency < 0 )
{
assert( hardware_concurrency > concurrency );
if ( hardware_concurrency > concurrency )
thread_count = hardware_concurrency - concurrency;
else
{
std::cerr << "concurrency request is too low (" << concurrency
<< ") vs actual hardware concurrency (" << hardware_concurrency
<< "), using a single thread instead" << std::endl;
thread_count = 1;
}
}
}
}
int width = static_cast<int>( aspect_ratio * height );
auto camera = create_camera( fov_camera_parameters {
.lookfrom = { 13.f, 2.f, 3.f },
.lookat = { 0.f, 0.f, 0.f },
.vup = vec3 { 0.f, 1.f, 0.f },
.vertical_fov = vfov,
.aspect_ratio = aspect_ratio,
.aperture = 0.1f,
.focus_distance = 10.f,
} );
basic_random_generator generator;
material_lambertian material_ground( color { 0.5f, 0.5f, 0.5f } );
material_dielectric material_dielectric( 1.5f );
material_lambertian material_left( color { 0.4f, 0.2f, 0.1f } );
material_metal material_right( color { 0.7f, 0.6f, 0.5f }, 0.f );
std::vector<material_metal> metals;
std::vector<material_lambertian> lambertians;
metals.reserve( 11 * 11 * 11 * 11 );
lambertians.reserve( 11 * 11 * 11 * 11 );
intersection_table_collection intersector_collection;
intersector_collection.emplace<intersection_table_sphere>( point { 0.f, -1000.f, 0.f }, 1000.f, &material_ground );
for ( int a = -11; a < 11; ++a )
{
for ( int b = -11; b < 11; ++b )
{
auto material_choice = random_float( generator );
vec3 center { a + 0.9f * random_float( generator ), 0.2f, b + 0.9f * random_float( generator ) };
if ( length( center - point { 4.f, 0.2f, 0.f } ) <= 0.9f )
continue;
if ( material_choice < 0.8f )
{
auto albedo = random_vec3( generator ) * random_vec3( generator );
auto& material = lambertians.emplace_back( albedo );
intersector_collection.emplace<intersection_table_sphere>( center, 0.2f, &material );
}
else if ( material_choice < 0.95f )
{
auto albedo = random_vec3( generator, 0.5f, 1.f );
auto fuzz = random_float( generator, 0.f, 0.5f );
auto& material = metals.emplace_back( albedo, fuzz );
intersector_collection.emplace<intersection_table_sphere>( center, 0.2f, &material );
}
else
{
auto albedo = random_vec3( generator, 0.5f, 1.f );
auto fuzz = random_float( generator, 0.f, 0.5f );
intersector_collection.emplace<intersection_table_sphere>( center, 0.2f, &material_dielectric );
}
}
}
intersector_collection.emplace<intersection_table_sphere>( point { 0.f, 1.f, 0.f }, 1.0f, &material_dielectric );
intersector_collection.emplace<intersection_table_sphere>( point { -4.f, 1.f, 0.f }, 1.0f, &material_left );
intersector_collection.emplace<intersection_table_sphere>( point { +4.f, 1.f, 0.f }, 1.0f, &material_right );
std::ostream* stream = nullptr;
std::ofstream output_file_stream;
if ( output_stream_index >= 0 )
{
output_file_stream.open( argv[output_stream_index], std::ios_base::out );
if ( !output_file_stream.is_open() )
return EXIT_FAILURE;
stream = &output_file_stream;
}
else
{
stream = &std::cout;
}
std::cerr << "Rendering image " << width << "x" << height << " (aspect ratio: " << aspect_ratio << ") [with "
<< thread_count << " concurrent threads]" << std::endl;
std::vector<std::thread> pool( thread_count );
auto chunk_size = height / thread_count;
auto chunk_extra = height % thread_count;
light_transport_simple_parameters p { .spp = spp, .bounces = bounces };
std::vector<color> framebuffer( height * width );
for ( auto idx = 0u; idx < thread_count; ++idx )
{
auto start = ( idx + 0 ) * chunk_size;
auto end = ( idx + 1 ) * chunk_size;
if ( idx == thread_count - 1 )
{
end += chunk_extra;
}
assert( start < end );
pool[idx] = std::thread(
process_scanlines,
start,
end,
height,
width,
camera,
std::cref( intersector_collection ),
p,
std::ref( framebuffer ) );
}
for ( std::thread& t : pool )
t.join();
pool.clear();
*stream << "P3\n" << width << ' ' << height << "\n255\n";
for ( int j = height - 1; j >= 0; --j )
{
for ( int i = 0; i < width; ++i )
{
auto index = j * width + i;
color& c = framebuffer.at( index );
write_color( *stream, c, spp );
}
}
std::cerr << "Done." << std::endl;
if ( output_stream_index >= 0 )
{
output_file_stream.close();
}
return EXIT_SUCCESS;
}
namespace rtow {
constexpr float kNear = 1e-3f;
constexpr float kFar = std::numeric_limits<float>::infinity();
void process_scanlines(
int start,
int end,
int height,
int width,
const camera& camera,
const intersection_table& world,
const light_transport_simple_parameters& p,
std::vector<color>& framebuffer )
{
basic_random_generator generator;
for ( int j = start; j < end; ++j )
{
for ( int i = 0; i < width; ++i )
{
color c { 0.f, 0.f, 0.f };
for ( int s = 0; s < p.spp; ++s )
{
auto u = float( i + random_float( generator ) ) / ( width - 1 );
auto v = float( j + random_float( generator ) ) / ( height - 1 );
ray r = camera.generate( generator, u, v );
c += ray_color( world, generator, r, p.bounces );
}
auto idx = j * width + i;
framebuffer.at( idx ) = c;
}
}
}
color ray_color( const intersection_table& intersector, basic_random_generator& gen, const ray& r, int bounces )
{
if ( bounces < 0 )
return { 0.f, 0.f, 0.f };
intersection_record record;
if ( intersector.intersect( r, kNear, kFar, record ) )
{
ray scattered;
color attenuation;
if ( record.material->scatter( r, record, gen, attenuation, scattered ) )
return attenuation * ray_color( intersector, gen, scattered, bounces - 1 );
else
return { 0.f, 0.f, 0.f };
}
constexpr color a { 1.f, 1.f, 1.f };
constexpr color b { .5f, .7f, 1.f };
vec3 unit = normalize( r.direction );
auto t = 0.5 * ( unit.y() + 1.f );
return a + t * ( b - a );
}
} // namespace rtow