/
optixPathTracer.cpp
630 lines (510 loc) · 20.8 KB
/
optixPathTracer.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//-----------------------------------------------------------------------------
//
// optixPathTracer: simple interactive path tracer
//
//-----------------------------------------------------------------------------
#ifdef __APPLE__
# include <GLUT/glut.h>
#else
# include <GL/glew.h>
# if defined( _WIN32 )
# include <GL/wglew.h>
# include <GL/freeglut.h>
# else
# include <GL/glut.h>
# endif
#endif
#include <optixu/optixpp_namespace.h>
#include <optixu/optixu_math_stream_namespace.h>
#include "point_light.hpp"
#include "optixPathTracer.h"
#include <sutil.h>
#include <Arcball.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdint.h>
#include <chrono>
#include <experimental/filesystem>
#include <utils.hpp>
using namespace optix;
const char* const SAMPLE_NAME = "optixPathTracer";
//------------------------------------------------------------------------------
//
// Globals
//
//------------------------------------------------------------------------------
Context context = 0;
uint32_t width = 512;
uint32_t height = 512;
bool use_pbo = true;
bool progressive = false;
unsigned int frame_number = 1;
unsigned int sqrt_num_samples = 10;
int rr_begin_depth = 1;
Program pgram_intersection = 0;
Program pgram_bounding_box = 0;
// Camera state
float3 camera_up;
float3 camera_lookat;
float3 camera_eye;
Matrix4x4 camera_rotate;
bool camera_changed = true;
sutil::Arcball arcball;
// Mouse state
int2 mouse_prev_pos;
int mouse_button;
//------------------------------------------------------------------------------
//
// Forward decls
//
//------------------------------------------------------------------------------
Buffer getOutputBuffer();
void destroyContext();
void registerExitHandler();
void createContext();
void loadGeometry();
void setupCamera();
void updateCamera();
void glutInitialize( int* argc, char** argv );
void glutRun();
void glutDisplay();
void glutKeyboardPress( unsigned char k, int x, int y );
void glutMousePress( int button, int state, int x, int y );
void glutMouseMotion( int x, int y);
void glutResize( int w, int h );
//------------------------------------------------------------------------------
//
// Helper functions
//
//------------------------------------------------------------------------------
Buffer getOutputBuffer()
{
return context[ "output_buffer" ]->getBuffer();
}
void destroyContext()
{
if( context )
{
context->destroy();
context = 0;
}
}
void registerExitHandler()
{
// register shutdown handler
#ifdef _WIN32
glutCloseFunc( destroyContext ); // this function is freeglut-only
#else
atexit( destroyContext );
#endif
}
void setMaterial(
GeometryInstance& gi,
Material material,
const std::string& color_name,
const float3& color)
{
gi->addMaterial(material);
gi[color_name]->setFloat(color);
}
GeometryInstance createParallelogram(
const float3& anchor,
const float3& offset1,
const float3& offset2)
{
Geometry parallelogram = context->createGeometry();
parallelogram->setPrimitiveCount( 1u );
parallelogram->setIntersectionProgram( pgram_intersection );
parallelogram->setBoundingBoxProgram( pgram_bounding_box );
float3 normal = normalize( cross( offset1, offset2 ) );
float d = dot( normal, anchor );
float4 plane = make_float4( normal, d );
float3 v1 = offset1 / dot( offset1, offset1 );
float3 v2 = offset2 / dot( offset2, offset2 );
parallelogram["plane"]->setFloat( plane );
parallelogram["anchor"]->setFloat( anchor );
parallelogram["v1"]->setFloat( v1 );
parallelogram["v2"]->setFloat( v2 );
GeometryInstance gi = context->createGeometryInstance();
gi->setGeometry(parallelogram);
return gi;
}
void createContext()
{
context = Context::create();
context->setRayTypeCount( 2 );
context->setEntryPointCount( 1 );
context->setStackSize( 1800 );
context[ "scene_epsilon" ]->setFloat( 1.e-3f );
context[ "pathtrace_ray_type" ]->setUint( 0u );
context[ "pathtrace_shadow_ray_type" ]->setUint( 1u );
context[ "rr_begin_depth" ]->setUint( rr_begin_depth );
Buffer buffer = sutil::createOutputBuffer( context, RT_FORMAT_FLOAT4, width, height, use_pbo );
context["output_buffer"]->set( buffer );
// Setup programs
const char *ptx = sutil::getPtxString( SAMPLE_NAME, "../optixPathTracer.cu" );
context->setRayGenerationProgram( 0, context->createProgramFromPTXString( ptx, "pathtrace_camera" ) );
context->setExceptionProgram( 0, context->createProgramFromPTXString( ptx, "exception" ) );
context->setMissProgram( 0, context->createProgramFromPTXString( ptx, "miss" ) );
context[ "sqrt_num_samples" ]->setUint( sqrt_num_samples );
context[ "bad_color" ]->setFloat( 1000000.0f, 0.0f, 1000000.0f ); // Super magenta to make sure it doesn't get averaged out in the progressive rendering.
context[ "bg_color" ]->setFloat( make_float3(0.0f) );
}
void loadLight()
{
// Light buffer
ParallelogramLight light;
light.corner = make_float3(343.0f, 548.6f, 227.0f);
light.v1 = make_float3(-130.0f, 0.0f, 0.0f);
light.v2 = make_float3(0.0f, 0.0f, 105.0f);
light.normal = normalize(cross(light.v1, light.v2));
light.emission = make_float3(15.0f, 15.0f, 5.0f);
Buffer light_buffer = context->createBuffer(RT_BUFFER_INPUT);
light_buffer->setFormat(RT_FORMAT_USER);
light_buffer->setElementSize(sizeof(ParallelogramLight));
light_buffer->setSize(1u);
memcpy(light_buffer->map(), &light, sizeof(light));
light_buffer->unmap();
context["lights"]->setBuffer(light_buffer);
grpt::point_light plight(make_float3(343.0f, 548.6f, 227.0f), make_float3(15.f, 15.f, 15.f));
optix::Buffer plight_buffer = context->createBuffer(RT_BUFFER_INPUT);
plight_buffer->setFormat(RT_FORMAT_USER);
plight_buffer->setElementSize(sizeof(grpt::point_light));
plight_buffer->setSize(0);
memcpy(plight_buffer->map(), &light, sizeof(plight));
plight_buffer->unmap();
context["point_lights"]->setBuffer(plight_buffer);
}
void loadGeometry()
{
auto current_path = std::experimental::filesystem::current_path();
auto dir_home = current_path.parent_path();
auto lamb = dir_home.parent_path() / "src" / "shading_models" / "lambertian.cu";
std::cout << lamb.string() << '\n';
assert(std::experimental::filesystem::exists(lamb));
// Set up material
Material diffuse = context->createMaterial();
const char *ptx = sutil::getPtxString( SAMPLE_NAME, "../src/shading_models/lambertian.cu" );
Program diffuse_ch = context->createProgramFromPTXString( ptx, "diffuse" );
Program diffuse_ah = context->createProgramFromPTXString( ptx, "shadow" );
diffuse->setClosestHitProgram( 0, diffuse_ch );
diffuse->setAnyHitProgram( 1, diffuse_ah );
Material diffuse_light = context->createMaterial();
Program diffuse_em = context->createProgramFromPTXString( ptx, "diffuseEmitter" );
diffuse_light->setClosestHitProgram( 0, diffuse_em );
// Set up parallelogram programs
ptx = sutil::getPtxString( SAMPLE_NAME, "../parallelogram.cu" );
pgram_bounding_box = context->createProgramFromPTXString( ptx, "bounds" );
pgram_intersection = context->createProgramFromPTXString( ptx, "intersect" );
// create geometry instances
std::vector<GeometryInstance> gis;
const float3 white = make_float3( 0.8f, 0.8f, 0.8f );
const float3 green = make_float3( 0.05f, 0.8f, 0.05f );
const float3 red = make_float3( 0.8f, 0.05f, 0.05f );
const float3 light_em = make_float3( 15.0f, 15.0f, 5.0f );
// Floor
gis.push_back( createParallelogram( make_float3( 0.0f, 0.0f, 0.0f ),
make_float3( 0.0f, 0.0f, 559.2f ),
make_float3( 556.0f, 0.0f, 0.0f ) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
// Ceiling
gis.push_back( createParallelogram( make_float3( 0.0f, 548.8f, 0.0f ),
make_float3( 556.0f, 0.0f, 0.0f ),
make_float3( 0.0f, 0.0f, 559.2f ) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
// Back wall
gis.push_back( createParallelogram( make_float3( 0.0f, 0.0f, 559.2f),
make_float3( 0.0f, 548.8f, 0.0f),
make_float3( 556.0f, 0.0f, 0.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
// Right wall
gis.push_back( createParallelogram( make_float3( 0.0f, 0.0f, 0.0f ),
make_float3( 0.0f, 548.8f, 0.0f ),
make_float3( 0.0f, 0.0f, 559.2f ) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", green);
// Left wall
gis.push_back( createParallelogram( make_float3( 556.0f, 0.0f, 0.0f ),
make_float3( 0.0f, 0.0f, 559.2f ),
make_float3( 0.0f, 548.8f, 0.0f ) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", red);
// Short block
gis.push_back( createParallelogram( make_float3( 130.0f, 165.0f, 65.0f),
make_float3( -48.0f, 0.0f, 160.0f),
make_float3( 160.0f, 0.0f, 49.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 290.0f, 0.0f, 114.0f),
make_float3( 0.0f, 165.0f, 0.0f),
make_float3( -50.0f, 0.0f, 158.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 130.0f, 0.0f, 65.0f),
make_float3( 0.0f, 165.0f, 0.0f),
make_float3( 160.0f, 0.0f, 49.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 82.0f, 0.0f, 225.0f),
make_float3( 0.0f, 165.0f, 0.0f),
make_float3( 48.0f, 0.0f, -160.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 240.0f, 0.0f, 272.0f),
make_float3( 0.0f, 165.0f, 0.0f),
make_float3( -158.0f, 0.0f, -47.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
// Tall block
gis.push_back( createParallelogram( make_float3( 423.0f, 330.0f, 247.0f),
make_float3( -158.0f, 0.0f, 49.0f),
make_float3( 49.0f, 0.0f, 159.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 423.0f, 0.0f, 247.0f),
make_float3( 0.0f, 330.0f, 0.0f),
make_float3( 49.0f, 0.0f, 159.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 472.0f, 0.0f, 406.0f),
make_float3( 0.0f, 330.0f, 0.0f),
make_float3( -158.0f, 0.0f, 50.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 314.0f, 0.0f, 456.0f),
make_float3( 0.0f, 330.0f, 0.0f),
make_float3( -49.0f, 0.0f, -160.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
gis.push_back( createParallelogram( make_float3( 265.0f, 0.0f, 296.0f),
make_float3( 0.0f, 330.0f, 0.0f),
make_float3( 158.0f, 0.0f, -49.0f) ) );
setMaterial(gis.back(), diffuse, "diffuse_color", white);
// Create shadow group (no light)
GeometryGroup shadow_group = context->createGeometryGroup(gis.begin(), gis.end());
shadow_group->setAcceleration( context->createAcceleration( "Trbvh" ) );
context["top_shadower"]->set( shadow_group );
// Light
gis.push_back( createParallelogram( make_float3( 343.0f, 548.6f, 227.0f),
make_float3( -130.0f, 0.0f, 0.0f),
make_float3( 0.0f, 0.0f, 105.0f) ) );
setMaterial(gis.back(), diffuse_light, "emission_color", light_em);
// Create geometry group
GeometryGroup geometry_group = context->createGeometryGroup(gis.begin(), gis.end());
geometry_group->setAcceleration( context->createAcceleration( "Trbvh" ) );
context["top_object"]->set( geometry_group );
}
void setupCamera()
{
camera_eye = make_float3( 278.0f, 273.0f, -900.0f );
camera_lookat = make_float3( 278.0f, 273.0f, 0.0f );
camera_up = make_float3( 0.0f, 1.0f, 0.0f );
camera_rotate = Matrix4x4::identity();
}
void updateCamera()
{
const float fov = 35.0f;
const float aspect_ratio = static_cast<float>(width) / static_cast<float>(height);
float3 camera_u, camera_v, camera_w;
sutil::calculateCameraVariables(
camera_eye, camera_lookat, camera_up, fov, aspect_ratio,
camera_u, camera_v, camera_w, /*fov_is_vertical*/ true );
const Matrix4x4 frame = Matrix4x4::fromBasis(
normalize( camera_u ),
normalize( camera_v ),
normalize( -camera_w ),
camera_lookat);
const Matrix4x4 frame_inv = frame.inverse();
// Apply camera rotation twice to match old SDK behavior
const Matrix4x4 trans = frame*camera_rotate*camera_rotate*frame_inv;
camera_eye = make_float3( trans*make_float4( camera_eye, 1.0f ) );
camera_lookat = make_float3( trans*make_float4( camera_lookat, 1.0f ) );
camera_up = make_float3( trans*make_float4( camera_up, 0.0f ) );
sutil::calculateCameraVariables(
camera_eye, camera_lookat, camera_up, fov, aspect_ratio,
camera_u, camera_v, camera_w, true );
camera_rotate = Matrix4x4::identity();
if( camera_changed ) // reset accumulation
frame_number = 1;
camera_changed = false;
context[ "frame_number" ]->setUint( frame_number++ );
context[ "eye"]->setFloat( camera_eye );
context[ "U" ]->setFloat( camera_u );
context[ "V" ]->setFloat( camera_v );
context[ "W" ]->setFloat( camera_w );
}
void glutInitialize( int* argc, char** argv )
{
glutInit( argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_ALPHA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( width, height );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( SAMPLE_NAME );
glutHideWindow();
}
void glutRun()
{
// Initialize GL state
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 0, 1, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
glutShowWindow();
glutReshapeWindow( width, height);
// register glut callbacks
glutDisplayFunc( glutDisplay );
glutIdleFunc( glutDisplay );
glutReshapeFunc( glutResize );
glutKeyboardFunc( glutKeyboardPress );
glutMouseFunc( glutMousePress );
glutMotionFunc( glutMouseMotion );
registerExitHandler();
glutMainLoop();
}
//------------------------------------------------------------------------------
//
// GLUT callbacks
//
//------------------------------------------------------------------------------
void glutDisplay()
{
updateCamera();
context->launch( 0, width, height );
sutil::displayBufferGL( getOutputBuffer() );
{
static unsigned frame_count = 0;
sutil::displayFps( frame_count++ );
}
glutSwapBuffers();
}
void glutKeyboardPress( unsigned char k, int x, int y )
{
switch( k )
{
case( 'q' ):
case( 27 ): // ESC
{
destroyContext();
exit(0);
}
case( 's' ):
{
const std::string outputImage = std::string(SAMPLE_NAME) + ".ppm";
std::cerr << "Saving current frame to '" << outputImage << "'\n";
sutil::displayBufferPPM( outputImage.c_str(), getOutputBuffer(), false );
break;
}
}
}
void glutMousePress( int button, int state, int x, int y )
{
if( state == GLUT_DOWN )
{
mouse_button = button;
mouse_prev_pos = make_int2( x, y );
}
else
{
// nothing
}
}
void glutMouseMotion( int x, int y)
{
if( mouse_button == GLUT_RIGHT_BUTTON )
{
const float dx = static_cast<float>( x - mouse_prev_pos.x ) /
static_cast<float>( width );
const float dy = static_cast<float>( y - mouse_prev_pos.y ) /
static_cast<float>( height );
const float dmax = fabsf( dx ) > fabs( dy ) ? dx : dy;
const float scale = std::min<float>( dmax, 0.9f );
camera_eye = camera_eye + (camera_lookat - camera_eye)*scale;
camera_changed = true;
}
else if( mouse_button == GLUT_LEFT_BUTTON )
{
const float2 from = { static_cast<float>(mouse_prev_pos.x),
static_cast<float>(mouse_prev_pos.y) };
const float2 to = { static_cast<float>(x),
static_cast<float>(y) };
const float2 a = { from.x / width, from.y / height };
const float2 b = { to.x / width, to.y / height };
camera_rotate = arcball.rotate( b, a );
camera_changed = true;
}
mouse_prev_pos = make_int2( x, y );
}
void glutResize( int w, int h )
{
if ( w == (int)width && h == (int)height ) return;
camera_changed = true;
width = w;
height = h;
sutil::ensureMinimumSize(width, height);
sutil::resizeBuffer( getOutputBuffer(), width, height );
glViewport(0, 0, width, height);
glutPostRedisplay();
}
//------------------------------------------------------------------------------
//
// Main
//
//------------------------------------------------------------------------------
int main( int argc, char** argv )
{
std::string out_file;
for( int i=1; i<argc; ++i )
{
const std::string arg( argv[i] );
if( arg == "-h" || arg == "--help" )
{
grpt::utils::printUsageAndExit( argv[0], SAMPLE_NAME);
}
else if( arg == "-f" || arg == "--file" )
{
if( i == argc-1 )
{
std::cerr << "Option '" << arg << "' requires additional argument.\n";
grpt::utils::printUsageAndExit( argv[0], SAMPLE_NAME );
}
out_file = argv[++i];
}
else if( arg == "-n" || arg == "--nopbo" )
{
use_pbo = false;
}
else if( arg == "-p" || arg == "--progressive" )
{
progressive = true;
}
else
{
std::cerr << "Unknown option '" << arg << "'\n";
grpt::utils::printUsageAndExit( argv[0], SAMPLE_NAME );
}
}
try
{
std::cout << "hi\n";
// glutInitialize( &argc, argv );
#ifndef __APPLE__
glewInit();
#endif
createContext();
setupCamera();
loadLight();
loadGeometry();
context->validate();
if ( progressive )
{
glutRun();
}
else
{
std::cout << "hi" << '\n';
auto begin = std::chrono::system_clock::now();
updateCamera();
context->launch( 0, width, height );
auto end = std::chrono::system_clock::now();
std::cout << "Rendering " << sqrt_num_samples * sqrt_num_samples << " samples per pixel took : " <<
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << " ms.\n";
sutil::displayBufferPPM( std::string{"../output" + std::to_string(sqrt_num_samples) + ".ppm"}.c_str(), getOutputBuffer(), false );
std::cout << getOutputBuffer() << '\n';
destroyContext();
}
return 0;
}
SUTIL_CATCH( context->get() )
}