SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLOptixAccelStruct.cpp
Go to the documentation of this file.
1 /**
2  * \file SLOptixAccelStruct.cpp
3  * \authors Nic Dorner
4  * \date October 2019
5  * \authors Nic Dorner
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifdef SL_HAS_OPTIX
12 # include <SLOptix.h>
13 # include <SLOptixAccelStruct.h>
14 # include <SLOptixRaytracer.h>
15 
16 //-----------------------------------------------------------------------------
17 SLOptixAccelStruct::SLOptixAccelStruct()
18 {
19  _accelBuildOptions.buildFlags = OPTIX_BUILD_FLAG_ALLOW_UPDATE |
20  OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS |
21  OPTIX_BUILD_FLAG_PREFER_FAST_TRACE;
22  _buffer = new SLOptixCudaBuffer<void>();
23 }
24 //-----------------------------------------------------------------------------
25 SLOptixAccelStruct::~SLOptixAccelStruct()
26 {
27  delete _buffer;
28 }
29 //-----------------------------------------------------------------------------
30 void SLOptixAccelStruct::buildAccelerationStructure()
31 {
32  OptixDeviceContext context = SLOptix::context;
33 
34  _accelBuildOptions.operation = OPTIX_BUILD_OPERATION_BUILD;
35 
36  OPTIX_CHECK(optixAccelComputeMemoryUsage(
37  context,
38  &_accelBuildOptions,
39  &_buildInput,
40  1, // num_build_inputs
41  &_accelBufferSizes));
42 
43  SLOptixCudaBuffer<void> temp_buffer = SLOptixCudaBuffer<void>();
44  temp_buffer.alloc(_accelBufferSizes.tempSizeInBytes);
45 
46  // non-compacted output
47  _buffer = new SLOptixCudaBuffer<void>();
48  _buffer->alloc(_accelBufferSizes.outputSizeInBytes);
49 
50  SLOptixCudaBuffer<OptixAabb> aabbBuffer = SLOptixCudaBuffer<OptixAabb>();
51  aabbBuffer.alloc(sizeof(OptixAabb));
52  SLOptixCudaBuffer<size_t> compactedSize = SLOptixCudaBuffer<size_t>();
53  compactedSize.alloc(sizeof(size_t));
54 
55  OptixAccelEmitDesc emitProperty[1];
56  emitProperty[0].type = OPTIX_PROPERTY_TYPE_AABBS;
57  emitProperty[0].result = aabbBuffer.devicePointer();
58 
59  OPTIX_CHECK(optixAccelBuild(
60  context,
61  SLOptix::stream, // CUDA stream
62  &_accelBuildOptions,
63  &_buildInput,
64  1, // num build inputs
65  temp_buffer.devicePointer(),
66  _accelBufferSizes.tempSizeInBytes,
67  _buffer->devicePointer(),
68  _accelBufferSizes.outputSizeInBytes,
69  &_handle,
70  emitProperty, // emitted property list
71  1 // num emitted properties
72  ));
73  CUDA_SYNC_CHECK(SLOptix::stream);
74 
75  OptixAabb aabb;
76  aabbBuffer.download(&aabb);
77 }
78 //-----------------------------------------------------------------------------
79 void SLOptixAccelStruct::updateAccelerationStructure()
80 {
81  OptixDeviceContext context = SLOptix::context;
82 
83  _accelBuildOptions.operation = OPTIX_BUILD_OPERATION_UPDATE;
84 
85  SLOptixCudaBuffer<void> temp_buffer = SLOptixCudaBuffer<void>();
86  temp_buffer.alloc(_accelBufferSizes.tempUpdateSizeInBytes);
87 
88  OPTIX_CHECK(optixAccelBuild(
89  context,
90  SLOptix::stream, // CUDA stream
91  &_accelBuildOptions,
92  &_buildInput,
93  1, // num build inputs
94  temp_buffer.devicePointer(),
95  _accelBufferSizes.tempUpdateSizeInBytes,
96  _buffer->devicePointer(),
97  _accelBufferSizes.outputSizeInBytes,
98  &_handle,
99  nullptr, // emitted property list
100  0 // num emitted properties
101  ));
102 }
103 //-----------------------------------------------------------------------------
104 #endif // SL_HAS_OPTIX