-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmulti_level_instancing_main.cpp
767 lines (630 loc) · 31.9 KB
/
multi_level_instancing_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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
JP: このサンプルはマルチレベルのインスタンシングとモーショントランスフォームの使用方法を示します。
OptiXはシングルレベルのインスタンシングに加えて、マルチレベルインスタンシングをサポートします。
マルチレベルインスタンシングではIASに所属するインスタンスがGASだけではなく、
他のIASやトランスフォームを参照することができます。
モーショントランスフォームにはひとつのASをセットし、ASのある時間中の動きを記述します。
モーショントランスフォームを配下に持つIASは空間に加えて時間も考慮したレイトレース高速化機構を構築し、
効率的なモーションブラーのレンダリングに使用できます。
EN: This sample shows how to use multi-level instancing as well as motion transform.
OptiX supports multi-level instancing in addition to single-level instancing.
In the case of multi-level instancing, an instance belonging to an IAS can refer not only GAS but also
another IAS or a transform.
Motion transform has an AS and describes the motion of the AS during a time interval.
An IAS having a motion transform as a child builds an acceleration structure for raytracing which
consider time as well as space, enabling efficient motion blur rendering.
*/
#include "multi_level_instancing_shared.h"
#include "../common/obj_loader.h"
int32_t main(int32_t argc, const char* argv[]) try {
// ----------------------------------------------------------------
// JP: OptiXのコンテキストとパイプラインの設定。
// EN: Settings for OptiX context and pipeline.
CUcontext cuContext;
CUstream cuStream;
CUDADRV_CHECK(cuInit(0));
CUDADRV_CHECK(cuCtxCreate(&cuContext, 0, 0));
CUDADRV_CHECK(cuCtxSetCurrent(cuContext));
CUDADRV_CHECK(cuStreamCreate(&cuStream, 0));
optixu::Context optixContext = optixu::Context::create(cuContext);
optixu::Pipeline pipeline = optixContext.createPipeline();
// JP: このサンプルでは多段階のASとトランスフォームを使用する。
// インスタンスモーションブラーも使用するのでUseMotionBlur::Yesを指定する。
// EN: This sample uses multi-level AS and transforms.
// Specify UseMotionBlur::Yes since the sample also uses instance motion blur.
pipeline.setPipelineOptions(
Shared::MyPayloadSignature::numDwords,
optixu::calcSumDwords<float2>(),
"plp", sizeof(Shared::PipelineLaunchParameters),
OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_ANY,
OPTIX_EXCEPTION_FLAG_STACK_OVERFLOW | OPTIX_EXCEPTION_FLAG_TRACE_DEPTH,
OPTIX_PRIMITIVE_TYPE_FLAGS_TRIANGLE,
optixu::UseMotionBlur::Yes);
const std::vector<char> optixIr =
readBinaryFile(getExecutableDirectory() / "multi_level_instancing/ptxes/optix_kernels.optixir");
optixu::Module moduleOptiX = pipeline.createModuleFromOptixIR(
optixIr, OPTIX_COMPILE_DEFAULT_MAX_REGISTER_COUNT,
DEBUG_SELECT(OPTIX_COMPILE_OPTIMIZATION_LEVEL_0, OPTIX_COMPILE_OPTIMIZATION_DEFAULT),
DEBUG_SELECT(OPTIX_COMPILE_DEBUG_LEVEL_FULL, OPTIX_COMPILE_DEBUG_LEVEL_NONE));
optixu::Module emptyModule;
optixu::Program rayGenProgram = pipeline.createRayGenProgram(moduleOptiX, RT_RG_NAME_STR("raygen"));
//optixu::Program exceptionProgram = pipeline.createExceptionProgram(moduleOptiX, "__exception__print");
optixu::Program missProgram = pipeline.createMissProgram(moduleOptiX, RT_MS_NAME_STR("miss"));
optixu::HitProgramGroup hitProgramGroup = pipeline.createHitProgramGroupForTriangleIS(
moduleOptiX, RT_CH_NAME_STR("closesthit"),
emptyModule, nullptr);
// JP: このサンプルはRay Generation Programからしかレイトレースを行わないのでTrace Depthは1になる。
// EN: Trace depth is 1 because this sample trace rays only from the ray generation program.
pipeline.link(1);
pipeline.setRayGenerationProgram(rayGenProgram);
// If an exception program is not set but exception flags are set,
// the default exception program will by provided by OptiX.
//pipeline.setExceptionProgram(exceptionProgram);
pipeline.setNumMissRayTypes(Shared::NumRayTypes);
pipeline.setMissProgram(Shared::RayType_Primary, missProgram);
// JP: このプログラムは深いトラバーサルグラフを使用するため明示的な設定が必要。
// EN: This program uses a deep traversal graph depth, therefore requires explicit configuration.
{
// No direct callable programs at all.
uint32_t dcStackSizeFromTrav = 0;
uint32_t dcStackSizeFromState = 0;
// Possible Program Paths:
// RG - CH
// RG - MS
uint32_t ccStackSize = rayGenProgram.getStackSize() +
std::max(hitProgramGroup.getCHStackSize(), missProgram.getStackSize());
// The deepest path: IAS - IAS - SRTXfm - GAS
uint32_t maxTraversableGraphDepth = 4;
pipeline.setStackSize(dcStackSizeFromTrav, dcStackSizeFromState, ccStackSize, maxTraversableGraphDepth);
}
cudau::Buffer shaderBindingTable;
size_t sbtSize;
pipeline.generateShaderBindingTableLayout(&sbtSize);
shaderBindingTable.initialize(cuContext, cudau::BufferType::Device, sbtSize, 1);
shaderBindingTable.setMappedMemoryPersistent(true);
pipeline.setShaderBindingTable(shaderBindingTable, shaderBindingTable.getMappedPointer());
// END: Settings for OptiX context and pipeline.
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// JP: マテリアルのセットアップ。
// EN: Setup materials.
optixu::Material mat0 = optixContext.createMaterial();
mat0.setHitGroup(Shared::RayType_Primary, hitProgramGroup);
// END: Setup materials.
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// JP: シーンのセットアップ。
// EN: Setup a scene.
optixu::Scene scene = optixContext.createScene();
size_t maxSizeOfScratchBuffer = 0;
OptixAccelBufferSizes asMemReqs;
cudau::Buffer asBuildScratchMem;
// JP: このサンプルではマルチレベルインスタンシングやトランスフォームに焦点を当て、
// ほかをシンプルにするために1つのGASあたり1つのGeometryInstanceとする。
// EN: Use one GeometryInstance per GAS for simplicty and
// to focus on multi-level instancing and transforms in this sample.
struct Geometry {
cudau::TypedBuffer<Shared::Vertex> vertexBuffer;
cudau::TypedBuffer<Shared::Triangle> triangleBuffer;
optixu::GeometryInstance optixGeomInst;
optixu::GeometryAccelerationStructure optixGas;
cudau::Buffer gasMem;
size_t compactedSize;
void finalize() {
gasMem.finalize();
optixGas.destroy();
triangleBuffer.finalize();
vertexBuffer.finalize();
optixGeomInst.destroy();
}
};
Geometry room;
{
Shared::Vertex vertices[] = {
// floor
{ make_float3(-1.0f, -1.0f, -1.0f), make_float3(0, 1, 0), make_float2(0, 0) },
{ make_float3(-1.0f, -1.0f, 1.0f), make_float3(0, 1, 0), make_float2(0, 5) },
{ make_float3(1.0f, -1.0f, 1.0f), make_float3(0, 1, 0), make_float2(5, 5) },
{ make_float3(1.0f, -1.0f, -1.0f), make_float3(0, 1, 0), make_float2(5, 0) },
// back wall
{ make_float3(-1.0f, -1.0f, -1.0f), make_float3(0, 0, 1), make_float2(0, 0) },
{ make_float3(-1.0f, 1.0f, -1.0f), make_float3(0, 0, 1), make_float2(0, 1) },
{ make_float3(1.0f, 1.0f, -1.0f), make_float3(0, 0, 1), make_float2(1, 1) },
{ make_float3(1.0f, -1.0f, -1.0f), make_float3(0, 0, 1), make_float2(1, 0) },
// ceiling
{ make_float3(-1.0f, 1.0f, -1.0f), make_float3(0, -1, 0), make_float2(0, 0) },
{ make_float3(-1.0f, 1.0f, 1.0f), make_float3(0, -1, 0), make_float2(0, 1) },
{ make_float3(1.0f, 1.0f, 1.0f), make_float3(0, -1, 0), make_float2(1, 1) },
{ make_float3(1.0f, 1.0f, -1.0f), make_float3(0, -1, 0), make_float2(1, 0) },
// left wall
{ make_float3(-1.0f, -1.0f, -1.0f), make_float3(1, 0, 0), make_float2(0, 0) },
{ make_float3(-1.0f, 1.0f, -1.0f), make_float3(1, 0, 0), make_float2(0, 1) },
{ make_float3(-1.0f, 1.0f, 1.0f), make_float3(1, 0, 0), make_float2(1, 1) },
{ make_float3(-1.0f, -1.0f, 1.0f), make_float3(1, 0, 0), make_float2(1, 0) },
// right wall
{ make_float3(1.0f, -1.0f, -1.0f), make_float3(-1, 0, 0), make_float2(0, 0) },
{ make_float3(1.0f, 1.0f, -1.0f), make_float3(-1, 0, 0), make_float2(0, 1) },
{ make_float3(1.0f, 1.0f, 1.0f), make_float3(-1, 0, 0), make_float2(1, 1) },
{ make_float3(1.0f, -1.0f, 1.0f), make_float3(-1, 0, 0), make_float2(1, 0) },
};
Shared::Triangle triangles[] = {
// floor
{ 0, 1, 2 }, { 0, 2, 3 },
// back wall
{ 4, 7, 6 }, { 4, 6, 5 },
// ceiling
{ 8, 11, 10 }, { 8, 10, 9 },
// left wall
{ 15, 12, 13 }, { 15, 13, 14 },
// right wall
{ 16, 19, 18 }, { 16, 18, 17 }
};
room.vertexBuffer.initialize(cuContext, cudau::BufferType::Device, vertices, lengthof(vertices));
room.triangleBuffer.initialize(cuContext, cudau::BufferType::Device, triangles, lengthof(triangles));
Shared::GeometryData geomData = {};
geomData.vertexBuffer = room.vertexBuffer.getROBuffer<enableBufferOobCheck>();
geomData.triangleBuffer = room.triangleBuffer.getROBuffer<enableBufferOobCheck>();
room.optixGeomInst = scene.createGeometryInstance();
room.optixGeomInst.setVertexBuffer(room.vertexBuffer);
room.optixGeomInst.setTriangleBuffer(room.triangleBuffer);
room.optixGeomInst.setNumMaterials(1, optixu::BufferView());
room.optixGeomInst.setMaterial(0, 0, mat0);
room.optixGeomInst.setGeometryFlags(0, OPTIX_GEOMETRY_FLAG_NONE);
room.optixGeomInst.setUserData(geomData);
room.optixGas = scene.createGeometryAccelerationStructure();
room.optixGas.setConfiguration(
optixu::ASTradeoff::PreferFastTrace,
optixu::AllowUpdate::No,
optixu::AllowCompaction::Yes);
room.optixGas.setNumMaterialSets(1);
room.optixGas.setNumRayTypes(0, Shared::NumRayTypes);
room.optixGas.addChild(room.optixGeomInst);
room.optixGas.prepareForBuild(&asMemReqs);
room.gasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
}
Geometry areaLight;
{
Shared::Vertex vertices[] = {
{ make_float3(-0.25f, 0.0f, -0.25f), make_float3(0, -1, 0), make_float2(0, 0) },
{ make_float3(-0.25f, 0.0f, 0.25f), make_float3(0, -1, 0), make_float2(0, 1) },
{ make_float3(0.25f, 0.0f, 0.25f), make_float3(0, -1, 0), make_float2(1, 1) },
{ make_float3(0.25f, 0.0f, -0.25f), make_float3(0, -1, 0), make_float2(1, 0) },
};
Shared::Triangle triangles[] = {
{ 0, 1, 2 }, { 0, 2, 3 },
};
areaLight.vertexBuffer.initialize(cuContext, cudau::BufferType::Device, vertices, lengthof(vertices));
areaLight.triangleBuffer.initialize(cuContext, cudau::BufferType::Device, triangles, lengthof(triangles));
Shared::GeometryData geomData = {};
geomData.vertexBuffer = areaLight.vertexBuffer.getROBuffer<enableBufferOobCheck>();
geomData.triangleBuffer = areaLight.triangleBuffer.getROBuffer<enableBufferOobCheck>();
areaLight.optixGeomInst = scene.createGeometryInstance();
areaLight.optixGeomInst.setVertexBuffer(areaLight.vertexBuffer);
areaLight.optixGeomInst.setTriangleBuffer(areaLight.triangleBuffer);
areaLight.optixGeomInst.setNumMaterials(1, optixu::BufferView());
areaLight.optixGeomInst.setMaterial(0, 0, mat0);
areaLight.optixGeomInst.setGeometryFlags(0, OPTIX_GEOMETRY_FLAG_NONE);
areaLight.optixGeomInst.setUserData(geomData);
areaLight.optixGas = scene.createGeometryAccelerationStructure();
areaLight.optixGas.setConfiguration(
optixu::ASTradeoff::PreferFastTrace,
optixu::AllowUpdate::No,
optixu::AllowCompaction::Yes);
areaLight.optixGas.setNumMaterialSets(1);
areaLight.optixGas.setNumRayTypes(0, Shared::NumRayTypes);
areaLight.optixGas.addChild(areaLight.optixGeomInst);
areaLight.optixGas.prepareForBuild(&asMemReqs);
areaLight.gasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
}
Geometry bunny;
{
std::vector<Shared::Vertex> vertices;
std::vector<Shared::Triangle> triangles;
{
std::vector<obj::Vertex> objVertices;
std::vector<obj::Triangle> objTriangles;
obj::load("../../data/stanford_bunny_309_faces.obj", &objVertices, &objTriangles);
vertices.resize(objVertices.size());
for (int vIdx = 0; vIdx < objVertices.size(); ++vIdx) {
const obj::Vertex &objVertex = objVertices[vIdx];
vertices[vIdx] = Shared::Vertex{ objVertex.position, objVertex.normal, objVertex.texCoord };
}
static_assert(sizeof(Shared::Triangle) == sizeof(obj::Triangle),
"Assume triangle formats are the same.");
triangles.resize(objTriangles.size());
std::copy_n(reinterpret_cast<Shared::Triangle*>(objTriangles.data()),
triangles.size(), triangles.data());
}
bunny.vertexBuffer.initialize(cuContext, cudau::BufferType::Device, vertices);
bunny.triangleBuffer.initialize(cuContext, cudau::BufferType::Device, triangles);
Shared::GeometryData geomData = {};
geomData.vertexBuffer = bunny.vertexBuffer.getROBuffer<enableBufferOobCheck>();
geomData.triangleBuffer = bunny.triangleBuffer.getROBuffer<enableBufferOobCheck>();
bunny.optixGeomInst = scene.createGeometryInstance();
bunny.optixGeomInst.setVertexBuffer(bunny.vertexBuffer);
bunny.optixGeomInst.setTriangleBuffer(bunny.triangleBuffer);
bunny.optixGeomInst.setNumMaterials(1, optixu::BufferView());
bunny.optixGeomInst.setMaterial(0, 0, mat0);
bunny.optixGeomInst.setGeometryFlags(0, OPTIX_GEOMETRY_FLAG_NONE);
bunny.optixGeomInst.setUserData(geomData);
bunny.optixGas = scene.createGeometryAccelerationStructure();
bunny.optixGas.setConfiguration(
optixu::ASTradeoff::PreferFastTrace,
optixu::AllowUpdate::No,
optixu::AllowCompaction::Yes);
bunny.optixGas.setNumMaterialSets(1);
bunny.optixGas.setNumRayTypes(0, Shared::NumRayTypes);
bunny.optixGas.addChild(bunny.optixGeomInst);
bunny.optixGas.prepareForBuild(&asMemReqs);
bunny.gasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
}
Geometry cube;
{
std::vector<obj::Vertex> objVertices;
std::vector<obj::MaterialGroup> objMatGroups;
obj::load("../../data/subd_cube.obj", &objVertices, &objMatGroups, nullptr);
// JP: このサンプルではobjのマテリアルを区別しないのでグループをひとつにまとめる。
// EN: Combine groups into one because this sample doesn't distinguish obj materials.
std::vector<Shared::Vertex> vertices;
std::vector<Shared::Triangle> triangles;
{
vertices.resize(objVertices.size());
for (int vIdx = 0; vIdx < objVertices.size(); ++vIdx) {
const obj::Vertex &objVertex = objVertices[vIdx];
vertices[vIdx] = Shared::Vertex{ objVertex.position, objVertex.normal, objVertex.texCoord };
}
for (int mIdx = 0; mIdx < objMatGroups.size(); ++mIdx) {
const obj::MaterialGroup &matGroup = objMatGroups[mIdx];
uint32_t baseIndex = triangles.size();
triangles.resize(triangles.size() + matGroup.triangles.size());
std::copy_n(reinterpret_cast<const Shared::Triangle*>(matGroup.triangles.data()),
matGroup.triangles.size(),
triangles.data() + baseIndex);
}
}
cube.vertexBuffer.initialize(cuContext, cudau::BufferType::Device, vertices);
cube.triangleBuffer.initialize(cuContext, cudau::BufferType::Device, triangles);
Shared::GeometryData geomData = {};
geomData.vertexBuffer = cube.vertexBuffer.getROBuffer<enableBufferOobCheck>();
geomData.triangleBuffer = cube.triangleBuffer.getROBuffer<enableBufferOobCheck>();
cube.optixGeomInst = scene.createGeometryInstance();
cube.optixGeomInst.setVertexBuffer(cube.vertexBuffer);
cube.optixGeomInst.setTriangleBuffer(cube.triangleBuffer);
cube.optixGeomInst.setNumMaterials(1, optixu::BufferView());
cube.optixGeomInst.setMaterial(0, 0, mat0);
cube.optixGeomInst.setGeometryFlags(0, OPTIX_GEOMETRY_FLAG_NONE);
cube.optixGeomInst.setUserData(geomData);
cube.optixGas = scene.createGeometryAccelerationStructure();
cube.optixGas.setConfiguration(
optixu::ASTradeoff::PreferFastTrace,
optixu::AllowUpdate::No,
optixu::AllowCompaction::Yes);
cube.optixGas.setNumMaterialSets(1);
cube.optixGas.setNumRayTypes(0, Shared::NumRayTypes);
cube.optixGas.addChild(cube.optixGeomInst);
cube.optixGas.prepareForBuild(&asMemReqs);
cube.gasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
}
// JP: GAS/Transformインスタンスを作成する。
// EN: Create GAS/Transform instances.
optixu::Instance roomInst = scene.createInstance();
roomInst.setChild(room.optixGas);
float areaLightInstXfm[] = {
1, 0, 0, 0,
0, 1, 0, 0.9f,
0, 0, 1, 0
};
optixu::Instance areaLightInst = scene.createInstance();
areaLightInst.setChild(areaLight.optixGas);
areaLightInst.setTransform(areaLightInstXfm);
struct Transform {
struct SRT {
float3 s;
Quaternion o;
float3 t;
};
std::vector<SRT> srts;
optixu::Transform optixTransform;
cudau::Buffer* deviceMem; // TODO?: define move constructor.
};
std::vector<Transform> objectTransforms;
std::vector<optixu::Instance> objectInsts;
// Bunny
{
Transform tr;
Transform::SRT srt0;
srt0.s = make_float3(0.005f);
srt0.o = Quaternion(0, 0, 0, 1);
srt0.t = make_float3(-0.5f, -1.0f, 0);
tr.srts.push_back(srt0);
Transform::SRT srt1;
srt1.s = make_float3(0.005f);
srt1.o = Quaternion(0, 0, 0, 1);
srt1.t = make_float3(-0.5f, -1.0f + 0.2f, 0);
tr.srts.push_back(srt1);
Transform::SRT srt2;
srt2.s = make_float3(0.005f);
srt2.o = Quaternion(0, 0, 0, 1);
srt2.t = make_float3(-0.5f + 0.2f, -1.0f + 0.4f, 0);
tr.srts.push_back(srt2);
size_t trMemSize;
tr.optixTransform = scene.createTransform();
tr.optixTransform.setConfiguration(optixu::TransformType::SRTMotion, tr.srts.size(), &trMemSize);
tr.optixTransform.setMotionOptions(0.0f, 1.0f, OPTIX_MOTION_FLAG_NONE);
tr.optixTransform.setChild(bunny.optixGas);
for (int keyIdx = 0; keyIdx < tr.srts.size(); ++keyIdx)
tr.optixTransform.setSRTMotionKey(
keyIdx,
reinterpret_cast<float*>(&tr.srts[keyIdx].s),
reinterpret_cast<float*>(&tr.srts[keyIdx].o),
reinterpret_cast<float*>(&tr.srts[keyIdx].t));
tr.deviceMem = new cudau::Buffer;
tr.deviceMem->initialize(cuContext, cudau::BufferType::Device, trMemSize, 1);
objectTransforms.push_back(tr);
optixu::Instance inst = scene.createInstance();
inst.setChild(tr.optixTransform);
objectInsts.push_back(inst);
}
// Cube
{
Transform tr;
Transform::SRT srt0;
srt0.s = make_float3(0.25f);
srt0.o = Quaternion(0, 0, 0, 1);
srt0.t = make_float3(0, -0.5f, 0);
tr.srts.push_back(srt0);
Transform::SRT srt1;
srt1.s = make_float3(0.25f);
srt1.o = qRotateX(pi_v<float> / 2);
srt1.t = make_float3(0, -0.5f, 0);
tr.srts.push_back(srt1);
Transform::SRT srt2;
srt2.s = make_float3(0.25f);
srt2.o = qRotateX(pi_v<float>);
srt2.t = make_float3(0, -0.5f, 0);
tr.srts.push_back(srt2);
size_t trMemSize;
tr.optixTransform = scene.createTransform();
tr.optixTransform.setConfiguration(optixu::TransformType::SRTMotion, tr.srts.size(), &trMemSize);
tr.optixTransform.setMotionOptions(0.0f, 1.0f, OPTIX_MOTION_FLAG_NONE);
tr.optixTransform.setChild(cube.optixGas);
for (int keyIdx = 0; keyIdx < tr.srts.size(); ++keyIdx)
tr.optixTransform.setSRTMotionKey(
keyIdx,
reinterpret_cast<float*>(&tr.srts[keyIdx].s),
reinterpret_cast<float*>(&tr.srts[keyIdx].o),
reinterpret_cast<float*>(&tr.srts[keyIdx].t));
tr.deviceMem = new cudau::Buffer;
tr.deviceMem->initialize(cuContext, cudau::BufferType::Device, trMemSize, 1);
objectTransforms.push_back(tr);
optixu::Instance inst = scene.createInstance();
inst.setChild(tr.optixTransform);
Matrix3x3 rotMat = rotate3x3(pi_v<float> / 2, 0, 1, 0);
float instXfm[] = {
rotMat.m00, rotMat.m01, rotMat.m02, 0.5f,
rotMat.m10, rotMat.m11, rotMat.m12, 0.0f,
rotMat.m20, rotMat.m21, rotMat.m22, 0.0f,
};
inst.setTransform(instXfm);
objectInsts.push_back(inst);
}
// JP: 下位のInstance Acceleration Structureを生成する。
// EN: Create an instance acceleration structure of the lower layer.
optixu::InstanceAccelerationStructure lowerIas = scene.createInstanceAccelerationStructure();
cudau::Buffer iasMem;
cudau::TypedBuffer<OptixInstance> instanceBuffer;
constexpr uint32_t numMotionKeys = 3;
lowerIas.setConfiguration(optixu::ASTradeoff::PreferFastTrace);
lowerIas.setMotionOptions(numMotionKeys, 0.0f, 1.0f, OPTIX_MOTION_FLAG_NONE);
lowerIas.addChild(roomInst);
lowerIas.addChild(areaLightInst);
for (int i = 0; i < objectInsts.size(); ++i)
lowerIas.addChild(objectInsts[i]);
lowerIas.prepareForBuild(&asMemReqs);
iasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
instanceBuffer.initialize(cuContext, cudau::BufferType::Device, lowerIas.getNumChildren());
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
// JP: IASインスタンスを作成する。
// EN: Create IAS instances.
optixu::Instance topInstA = scene.createInstance();
topInstA.setChild(lowerIas);
float instATr[] = {
1, 0, 0, -1.25f,
0, 1, 0, -1.25f,
0, 0, 1, 0
};
topInstA.setTransform(instATr);
optixu::Instance topInstB = scene.createInstance();
topInstB.setChild(lowerIas);
float instBTr[] = {
1, 0, 0, 1.25f,
0, 1, 0, -1.25f,
0, 0, 1, 0
};
topInstB.setTransform(instBTr);
optixu::Instance topInstC = scene.createInstance();
topInstC.setChild(lowerIas);
float instCTr[] = {
1, 0, 0, -1.25f,
0, 1, 0, 1.25f,
0, 0, 1, 0
};
topInstC.setTransform(instCTr);
optixu::Instance topInstD = scene.createInstance();
topInstD.setChild(lowerIas);
float instDTr[] = {
1, 0, 0, 1.25f,
0, 1, 0, 1.25f,
0, 0, 1, 0
};
topInstD.setTransform(instDTr);
// JP: 最上位のInstance Acceleration Structureを生成する。
// EN: Create an instance acceleration structure of the top layer.
optixu::InstanceAccelerationStructure topIas = scene.createInstanceAccelerationStructure();
cudau::Buffer topIasMem;
cudau::TypedBuffer<OptixInstance> topInstanceBuffer;
topIas.setConfiguration(optixu::ASTradeoff::PreferFastTrace);
topIas.addChild(topInstA);
topIas.addChild(topInstB);
topIas.addChild(topInstC);
topIas.addChild(topInstD);
topIas.prepareForBuild(&asMemReqs);
topIasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
topInstanceBuffer.initialize(cuContext, cudau::BufferType::Device, topIas.getNumChildren());
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
// JP: ASビルド用のスクラッチメモリを確保する。
// EN: Allocate scratch memory for AS builds.
asBuildScratchMem.initialize(cuContext, cudau::BufferType::Device, maxSizeOfScratchBuffer, 1);
// JP: Geometry Acceleration Structureをビルドする。
// EN: Build geometry acceleration structures.
room.optixGas.rebuild(cuStream, room.gasMem, asBuildScratchMem);
areaLight.optixGas.rebuild(cuStream, areaLight.gasMem, asBuildScratchMem);
bunny.optixGas.rebuild(cuStream, bunny.gasMem, asBuildScratchMem);
cube.optixGas.rebuild(cuStream, cube.gasMem, asBuildScratchMem);
// JP: 静的なメッシュはコンパクションもしておく。
// 複数のメッシュのASをひとつのバッファーに詰めて記録する。
// EN: Perform compaction for static meshes.
// Record ASs of multiple meshes into single buffer back to back.
struct CompactedASInfo {
Geometry* geom;
size_t offset;
size_t size;
};
CompactedASInfo gasList[] = {
{ &room, 0, 0 },
{ &areaLight, 0, 0 },
{ &bunny, 0, 0 },
{ &cube, 0, 0 },
};
size_t compactedASMemOffset = 0;
for (int i = 0; i < lengthof(gasList); ++i) {
CompactedASInfo &info = gasList[i];
compactedASMemOffset = alignUp(compactedASMemOffset, OPTIX_ACCEL_BUFFER_BYTE_ALIGNMENT);
info.offset = compactedASMemOffset;
info.geom->optixGas.prepareForCompact(&info.size);
compactedASMemOffset += info.size;
}
cudau::Buffer compactedASMem;
compactedASMem.initialize(cuContext, cudau::BufferType::Device, compactedASMemOffset, 1);
for (int i = 0; i < lengthof(gasList); ++i) {
const CompactedASInfo &info = gasList[i];
info.geom->optixGas.compact(
cuStream,
optixu::BufferView(compactedASMem.getCUdeviceptr() + info.offset, info.size, 1));
}
// JP: removeUncompacted()はcompact()がデバイス上で完了するまでホスト側で待つので呼び出しを分けたほうが良い。
// EN: removeUncompacted() waits on host-side until the compact() completes on the device,
// so separating calls is recommended.
for (int i = 0; i < lengthof(gasList); ++i) {
gasList[i].geom->optixGas.removeUncompacted();
gasList[i].geom->gasMem.finalize();
}
// JP: Transformビルド時には子のTraversable Handleが確定している必要がある。
// EN: Traversable handle for the child must be fixed when building Transform.
for (int tIdx = 0; tIdx < objectTransforms.size(); ++tIdx) {
const Transform &tr = objectTransforms[tIdx];
tr.optixTransform.rebuild(cuStream, *tr.deviceMem);
}
// JP: IASビルド時には各インスタンスのTraversable HandleとShader Binding Table中のオフセットが
// 確定している必要がある。
// EN: Traversable handle and offset in the shader binding table must be fixed for each instance
// when building an IAS.
cudau::Buffer hitGroupSBT;
size_t hitGroupSbtSize;
scene.generateShaderBindingTableLayout(&hitGroupSbtSize);
hitGroupSBT.initialize(cuContext, cudau::BufferType::Device, hitGroupSbtSize, 1);
hitGroupSBT.setMappedMemoryPersistent(true);
lowerIas.rebuild(cuStream, instanceBuffer, iasMem, asBuildScratchMem);
OptixTraversableHandle travHandle = topIas.rebuild(cuStream, topInstanceBuffer, topIasMem, asBuildScratchMem);
CUDADRV_CHECK(cuStreamSynchronize(cuStream));
// END: Setup a scene.
// ----------------------------------------------------------------
constexpr uint32_t renderTargetSizeX = 1024;
constexpr uint32_t renderTargetSizeY = 1024;
optixu::HostBlockBuffer2D<Shared::PCG32RNG, 4> rngBuffer;
rngBuffer.initialize(cuContext, cudau::BufferType::Device, renderTargetSizeX, renderTargetSizeY);
{
std::mt19937 rng(50932423);
rngBuffer.map();
for (int y = 0; y < renderTargetSizeY; ++y) {
for (int x = 0; x < renderTargetSizeX; ++x) {
rngBuffer(x, y).setState(rng());
}
}
rngBuffer.unmap();
}
optixu::HostBlockBuffer2D<float4, 1> accumBuffer;
accumBuffer.initialize(cuContext, cudau::BufferType::Device, renderTargetSizeX, renderTargetSizeY);
Shared::PipelineLaunchParameters plp;
plp.travHandle = travHandle;
plp.imageSize.x = renderTargetSizeX;
plp.imageSize.y = renderTargetSizeY;
plp.rngBuffer = rngBuffer.getBlockBuffer2D();
plp.accumBuffer = accumBuffer.getBlockBuffer2D();
plp.timeBegin = 0.0f;
plp.timeEnd = 1.0f;
plp.numAccumFrames = 0;
plp.camera.fovY = 50 * pi_v<float> / 180;
plp.camera.aspect = static_cast<float>(renderTargetSizeX) / renderTargetSizeY;
plp.camera.position = make_float3(0, 0, 6.0);
plp.camera.orientation = rotateY3x3(pi_v<float>);
pipeline.setScene(scene);
pipeline.setHitGroupShaderBindingTable(hitGroupSBT, hitGroupSBT.getMappedPointer());
CUdeviceptr plpOnDevice;
CUDADRV_CHECK(cuMemAlloc(&plpOnDevice, sizeof(plp)));
for (int frameIndex = 0; frameIndex < 1024; ++frameIndex) {
plp.numAccumFrames = frameIndex;
CUDADRV_CHECK(cuMemcpyHtoDAsync(plpOnDevice, &plp, sizeof(plp), cuStream));
pipeline.launch(cuStream, plpOnDevice, renderTargetSizeX, renderTargetSizeY, 1);
}
CUDADRV_CHECK(cuStreamSynchronize(cuStream));
saveImage("output.png", accumBuffer, false, false);
CUDADRV_CHECK(cuMemFree(plpOnDevice));
rngBuffer.finalize();
accumBuffer.finalize();
hitGroupSBT.finalize();
compactedASMem.finalize();
asBuildScratchMem.finalize();
topInstanceBuffer.finalize();
topIasMem.finalize();
topIas.destroy();
topInstD.destroy();
topInstC.destroy();
topInstB.destroy();
topInstA.destroy();
instanceBuffer.finalize();
iasMem.finalize();
lowerIas.destroy();
for (int i = objectInsts.size() - 1; i >= 0; --i) {
objectInsts[i].destroy();
objectTransforms[i].deviceMem->finalize();
delete objectTransforms[i].deviceMem;
objectTransforms[i].optixTransform.destroy();
}
areaLightInst.destroy();
roomInst.destroy();
cube.finalize();
bunny.finalize();
areaLight.finalize();
room.finalize();
scene.destroy();
mat0.destroy();
shaderBindingTable.finalize();
hitProgramGroup.destroy();
missProgram.destroy();
rayGenProgram.destroy();
moduleOptiX.destroy();
pipeline.destroy();
optixContext.destroy();
CUDADRV_CHECK(cuStreamDestroy(cuStream));
CUDADRV_CHECK(cuCtxDestroy(cuContext));
return 0;
}
catch (const std::exception &ex) {
hpprintf("Error: %s\n", ex.what());
return -1;
}