-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsphere_primitive_main.cpp
More file actions
468 lines (363 loc) · 19.5 KB
/
sphere_primitive_main.cpp
File metadata and controls
468 lines (363 loc) · 19.5 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
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
/*
JP: このサンプルは球を扱う方法を示します。
球とレイの交叉判定はOptiXによって内部的に扱われます。
球は級の中心の頂点バッファーと球ごとの半径を表すバッファーから構成されます。
EN: This sample shows how to handle spheres.
Intersection test between a ray and a sphere is handled internally by OptiX.
Spheres consist of a vertex buffer of sphere centers and a buffer for the radius of each sphere.
*/
#include "sphere_primitive_shared.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));
#if CUDA_VERSION < 13000
CUDADRV_CHECK(cuCtxCreate(&cuContext, 0, 0));
#else
CUctxCreateParams cuCtxCreateParams = {};
CUDADRV_CHECK(cuCtxCreate(&cuContext, &cuCtxCreateParams, 0, 0));
#endif
CUDADRV_CHECK(cuCtxSetCurrent(cuContext));
CUDADRV_CHECK(cuStreamCreate(&cuStream, 0));
optixu::Context optixContext = optixu::Context::create(
cuContext, 4,
optixu::EnableValidation::DEBUG_SELECT(Yes, No));
optixu::Pipeline pipeline = optixContext.createPipeline();
// JP: 球との衝突判定を使うためプリミティブ種別のフラグを適切に設定する必要がある。
// 球のアトリビュートサイズは1Dword(float)。
// EN: Appropriately setting primitive type flags is required since this sample uses sphere intersection.
// The attribute size of spheres is 1 Dword (float).
optixu::PipelineOptions pipelineOptions;
pipelineOptions.payloadCountInDwords = Shared::MyPayloadSignature::numDwords;
pipelineOptions.attributeCountInDwords = std::max(
optixu::calcSumDwords<float2>(),
optixu::calcSumDwords<float>());
pipelineOptions.launchParamsVariableName = "plp";
pipelineOptions.sizeOfLaunchParams = sizeof(Shared::PipelineLaunchParameters);
pipelineOptions.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_LEVEL_INSTANCING;
pipelineOptions.exceptionFlags = OPTIX_EXCEPTION_FLAG_STACK_OVERFLOW | OPTIX_EXCEPTION_FLAG_TRACE_DEPTH;
pipelineOptions.supportedPrimitiveTypeFlags =
OPTIX_PRIMITIVE_TYPE_FLAGS_TRIANGLE |
OPTIX_PRIMITIVE_TYPE_FLAGS_SPHERE;
pipeline.setPipelineOptions(pipelineOptions);
const std::vector<char> optixIr =
readBinaryFile(getExecutableDirectory() / "sphere_primitive/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 hitProgramGroupForTriangles = pipeline.createHitProgramGroupForTriangleIS(
moduleOptiX, RT_CH_NAME_STR("closesthit"),
emptyModule, nullptr);
/*
JP: 球用のヒットグループを作成する。
球には三角形と同様、ビルトインのIntersection Programが使われるのでユーザーが指定する必要はない。
球を含むことになるASと同じビルド設定を予め指定しておく必要がある。
EN: Create a hit group for spheres.
Sphere uses a built-in intersection program similar to triangle,
so the user doesn't need to specify it.
The same build configuration as an AS having the sphere is required.
*/
constexpr optixu::ASTradeoff sphereASTradeOff = optixu::ASTradeoff::PreferFastTrace;
constexpr optixu::AllowUpdate sphereASUpdatable = optixu::AllowUpdate::No;
constexpr optixu::AllowCompaction sphereASCompactable = optixu::AllowCompaction::Yes;
constexpr auto useEmbeddedVertexData = optixu::AllowRandomVertexAccess(Shared::useEmbeddedVertexData);
optixu::HitProgramGroup hitProgramGroupForSpheres = pipeline.createHitProgramGroupForSphereIS(
moduleOptiX, RT_CH_NAME_STR("closesthit"),
emptyModule, nullptr,
sphereASTradeOff, sphereASUpdatable, sphereASCompactable, useEmbeddedVertexData);
// 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.setMissRayTypeCount(Shared::NumRayTypes);
pipeline.setMissProgram(Shared::RayType_Primary, missProgram);
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 matForTriangles = optixContext.createMaterial();
matForTriangles.setHitGroup(Shared::RayType_Primary, hitProgramGroupForTriangles);
optixu::Material matForSpheres = optixContext.createMaterial();
matForSpheres.setHitGroup(Shared::RayType_Primary, hitProgramGroupForSpheres);
// END: Setup materials.
// ----------------------------------------------------------------
// ----------------------------------------------------------------
// JP: シーンのセットアップ。
// EN: Setup a scene.
optixu::Scene scene = optixContext.createScene();
optixu::GeometryInstance roomGeomInst = scene.createGeometryInstance();
cudau::TypedBuffer<Shared::Vertex> roomVertexBuffer;
cudau::TypedBuffer<Shared::Triangle> roomTriangleBuffer;
{
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 }
};
roomVertexBuffer.initialize(cuContext, cudau::BufferType::Device, vertices, lengthof(vertices));
roomTriangleBuffer.initialize(cuContext, cudau::BufferType::Device, triangles, lengthof(triangles));
Shared::GeometryData geomData = {};
geomData.vertexBuffer = roomVertexBuffer.getROBuffer<enableBufferOobCheck>();
geomData.triangleBuffer = roomTriangleBuffer.getROBuffer<enableBufferOobCheck>();
roomGeomInst.setVertexBuffer(roomVertexBuffer);
roomGeomInst.setTriangleBuffer(roomTriangleBuffer);
roomGeomInst.setMaterialCount(1, optixu::BufferView());
roomGeomInst.setMaterial(0, 0, matForTriangles);
roomGeomInst.setGeometryFlags(0, OPTIX_GEOMETRY_FLAG_NONE);
roomGeomInst.setUserData(geomData);
}
// JP: 球用GeometryInstanceは生成時に指定する必要がある。
// EN: GeometryInstance for spheres requires to be specified at the creation.
// Spheres
optixu::GeometryInstance sphereGeomInst =
scene.createGeometryInstance(optixu::GeometryType::Spheres);
cudau::TypedBuffer<Shared::SphereParameter> sphereParamBuffer;
{
std::vector<Shared::SphereParameter> params;
{
std::mt19937 rng(390318410);
std::uniform_real_distribution<float> u01;
constexpr uint32_t numSpheres = 100;
params.resize(numSpheres);
for (int sphIdx = 0; sphIdx < numSpheres; ++sphIdx) {
Shared::SphereParameter ¶m = params[sphIdx];
param.center = float3(
-0.85f + 1.7f * u01(rng),
-0.85f + 1.7f * u01(rng),
-0.85f + 1.7f * u01(rng));
param.radius = 0.025f + 0.1f * u01(rng);
}
// Sphere intersector does not return back-face hits,
// so this sphere is not visible for the camera position of this sample.
Shared::SphereParameter param;
param.center = float3(0.0f);
param.radius = 5.0f;
params.push_back(param);
}
sphereParamBuffer.initialize(cuContext, cudau::BufferType::Device, params);
Shared::GeometryData geomData = {};
geomData.sphereParamBuffer = sphereParamBuffer.getROBuffer<enableBufferOobCheck>();
sphereGeomInst.setVertexBuffer(optixu::BufferView(
sphereParamBuffer.getCUdeviceptr() + offsetof(Shared::SphereParameter, center),
sphereParamBuffer.numElements(), sphereParamBuffer.stride()));
sphereGeomInst.setRadiusBuffer(optixu::BufferView(
sphereParamBuffer.getCUdeviceptr() + offsetof(Shared::SphereParameter, radius),
sphereParamBuffer.numElements(), sphereParamBuffer.stride()));
sphereGeomInst.setMaterial(0, 0, matForSpheres);
sphereGeomInst.setGeometryFlags(0, OPTIX_GEOMETRY_FLAG_NONE);
sphereGeomInst.setUserData(geomData);
}
size_t maxSizeOfScratchBuffer = 0;
OptixAccelBufferSizes asMemReqs;
cudau::Buffer asBuildScratchMem;
// JP: Geometry Acceleration Structureを生成する。
// EN: Create geometry acceleration structures.
optixu::GeometryAccelerationStructure roomGas = scene.createGeometryAccelerationStructure();
cudau::Buffer roomGasMem;
roomGas.setConfiguration(
optixu::ASTradeoff::PreferFastTrace,
optixu::AllowUpdate::No,
optixu::AllowCompaction::Yes);
roomGas.setMaterialSetCount(1);
roomGas.setRayTypeCount(0, Shared::NumRayTypes);
roomGas.addChild(roomGeomInst);
roomGas.prepareForBuild(&asMemReqs);
roomGasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
// JP: 球用のGASは三角形用のGASとは別にする必要がある。
// GAS生成時に球用であることを指定する。
// EN: GAS for spheres must be created separately with GAS for triangles.
// Specify that the GAS is for spheres at the creation.
optixu::GeometryAccelerationStructure spheresGas =
scene.createGeometryAccelerationStructure(optixu::GeometryType::Spheres);
cudau::Buffer spheresGasMem;
spheresGas.setConfiguration(
sphereASTradeOff, sphereASUpdatable, sphereASCompactable);
spheresGas.setMaterialSetCount(1);
spheresGas.setRayTypeCount(0, Shared::NumRayTypes);
spheresGas.addChild(sphereGeomInst);
spheresGas.prepareForBuild(&asMemReqs);
spheresGasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
maxSizeOfScratchBuffer = std::max(maxSizeOfScratchBuffer, asMemReqs.tempSizeInBytes);
// JP: GASを元にインスタンスを作成する。
// EN: Create instances based on GASs.
optixu::Instance roomInst = scene.createInstance();
roomInst.setChild(roomGas);
optixu::Instance spheresInst = scene.createInstance();
spheresInst.setChild(spheresGas);
// JP: Instance Acceleration Structureを生成する。
// EN: Create an instance acceleration structure.
optixu::InstanceAccelerationStructure ias = scene.createInstanceAccelerationStructure();
cudau::Buffer iasMem;
cudau::TypedBuffer<OptixInstance> instanceBuffer;
ias.setConfiguration(optixu::ASTradeoff::PreferFastTrace);
ias.addChild(roomInst);
ias.addChild(spheresInst);
ias.prepareForBuild(&asMemReqs);
iasMem.initialize(cuContext, cudau::BufferType::Device, asMemReqs.outputSizeInBytes, 1);
instanceBuffer.initialize(cuContext, cudau::BufferType::Device, ias.getChildCount());
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.
roomGas.rebuild(cuStream, roomGasMem, asBuildScratchMem);
spheresGas.rebuild(cuStream, spheresGasMem, asBuildScratchMem);
// JP: 静的なメッシュはコンパクションもしておく。
// 複数のメッシュのASをひとつのバッファーに詰めて記録する。
// EN: Perform compaction for static meshes.
// Record ASs of multiple meshes into single buffer back to back.
struct CompactedASInfo {
optixu::GeometryAccelerationStructure gas;
cudau::Buffer* mem;
size_t offset;
size_t size;
};
CompactedASInfo gasList[] = {
{ roomGas, &roomGasMem, 0, 0 },
{ spheresGas, &spheresGasMem, 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.gas.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.gas.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].gas.removeUncompacted();
gasList[i].mem->finalize();
}
cudau::Buffer hitGroupSBT;
size_t hitGroupSbtSize;
scene.generateShaderBindingTableLayout(&hitGroupSbtSize);
hitGroupSBT.initialize(cuContext, cudau::BufferType::Device, hitGroupSbtSize, 1);
hitGroupSBT.setMappedMemoryPersistent(true);
OptixTraversableHandle travHandle = ias.rebuild(cuStream, instanceBuffer, iasMem, asBuildScratchMem);
CUDADRV_CHECK(cuStreamSynchronize(cuStream));
// END: Setup a scene.
// ----------------------------------------------------------------
constexpr uint32_t renderTargetSizeX = 1024;
constexpr uint32_t renderTargetSizeY = 1024;
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.resultBuffer = accumBuffer.getBlockBuffer2D();
plp.camera.fovY = 50 * pi_v<float> / 180;
plp.camera.aspect = static_cast<float>(renderTargetSizeX) / renderTargetSizeY;
plp.camera.position = make_float3(0, 0, 3.5f);
plp.camera.orientation = rotateY3x3(pi_v<float>);
pipeline.setScene(scene);
pipeline.setHitGroupShaderBindingTable(hitGroupSBT, hitGroupSBT.getMappedPointer());
CUdeviceptr plpOnDevice;
CUDADRV_CHECK(cuMemAlloc(&plpOnDevice, sizeof(plp)));
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));
accumBuffer.finalize();
hitGroupSBT.finalize();
compactedASMem.finalize();
asBuildScratchMem.finalize();
instanceBuffer.finalize();
iasMem.finalize();
ias.destroy();
spheresInst.destroy();
roomInst.destroy();
spheresGasMem.finalize();
spheresGas.destroy();
roomGasMem.finalize();
roomGas.destroy();
sphereParamBuffer.finalize();
sphereGeomInst.destroy();
roomTriangleBuffer.finalize();
roomVertexBuffer.finalize();
roomGeomInst.destroy();
scene.destroy();
matForSpheres.destroy();
matForTriangles.destroy();
shaderBindingTable.finalize();
hitProgramGroupForSpheres.destroy();
hitProgramGroupForTriangles.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;
}