21 SLOptixPathtracer::SLOptixPathtracer()
23 name(
"OptiX path tracer");
26 SLOptixPathtracer::~SLOptixPathtracer()
28 SL_LOG(
"Destructor : ~SLOptixPathtracer");
32 OPTIX_CHECK(optixDenoiserDestroy(_optixDenoiser));
37 "Exception in ~SLOptixRaytracer: %s",
42 void SLOptixPathtracer::setupOptix()
44 _cameraModule = createModule(
"SLOptixPathtracerCamera.cu");
45 _shadingModule = createModule(
"SLOptixPathtracerShading.cu");
47 OptixProgramGroupDesc sample_raygen_desc = {};
48 sample_raygen_desc.kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
49 sample_raygen_desc.raygen.module = _cameraModule;
50 sample_raygen_desc.raygen.entryFunctionName =
"__raygen__sample_camera";
51 _pinhole_raygen_prog_group = createProgram(sample_raygen_desc);
53 OptixProgramGroupDesc sample_miss_desc = {};
54 sample_miss_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
55 sample_miss_desc.miss.module = _shadingModule;
56 sample_miss_desc.miss.entryFunctionName =
"__miss__sample";
57 _radiance_miss_group = createProgram(sample_miss_desc);
59 OptixProgramGroupDesc sample_hitgroup_desc = {};
60 sample_hitgroup_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
61 sample_hitgroup_desc.hitgroup.moduleAH = _shadingModule;
62 sample_hitgroup_desc.hitgroup.entryFunctionNameAH =
"__anyhit__radiance";
63 sample_hitgroup_desc.hitgroup.moduleCH = _shadingModule;
64 sample_hitgroup_desc.hitgroup.entryFunctionNameCH =
"__closesthit__radiance";
65 _radiance_hit_group = createProgram(sample_hitgroup_desc);
67 OptixProgramGroupDesc occlusion_miss_desc = {};
68 occlusion_miss_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
69 occlusion_miss_desc.miss.module =
nullptr;
70 occlusion_miss_desc.miss.entryFunctionName =
nullptr;
71 _occlusion_miss_group = createProgram(occlusion_miss_desc);
73 OptixProgramGroupDesc occlusion_hitgroup_desc = {};
74 occlusion_hitgroup_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
75 occlusion_hitgroup_desc.hitgroup.moduleAH =
nullptr;
76 occlusion_hitgroup_desc.hitgroup.entryFunctionNameAH =
nullptr;
77 occlusion_hitgroup_desc.hitgroup.moduleCH =
nullptr;
78 occlusion_hitgroup_desc.hitgroup.entryFunctionNameCH =
nullptr;
79 _occlusion_hit_group = createProgram(occlusion_hitgroup_desc);
81 OptixProgramGroup path_tracer_program_groups[] = {
82 _pinhole_raygen_prog_group,
84 _occlusion_miss_group,
89 _pipeline = createPipeline(path_tracer_program_groups, 5);
91 OptixDenoiserOptions denoiserOptions;
92 denoiserOptions.inputKind = OPTIX_DENOISER_INPUT_RGB;
93 denoiserOptions.pixelFormat = OPTIX_PIXEL_FORMAT_FLOAT4;
95 OPTIX_CHECK(optixDenoiserCreate(SLOptix::context,
99 OPTIX_CHECK(optixDenoiserSetModel(_optixDenoiser,
100 OPTIX_DENOISER_MODEL_KIND_LDR,
110 _imageBuffer.resize(_sv->scrW() * _sv->scrH() *
sizeof(float4));
111 _curandBuffer.resize(_sv->scrW() * _sv->scrH() *
sizeof(curandState));
113 _params.image =
reinterpret_cast<float4*
>(_imageBuffer.devicePointer());
114 _params.states =
reinterpret_cast<curandState*
>(_curandBuffer.devicePointer());
115 _params.width = _sv->
scrW();
116 _params.height = _sv->scrH();
117 _params.max_depth = _maxDepth;
118 _params.samples = _samples;
121 SLMesh::meshIndex = 0;
122 for (
auto mesh : meshes)
123 mesh->createMeshAccelerationStructure();
125 _sbtClassic = createShaderBindingTable(meshes,
false);
127 OPTIX_CHECK(optixDenoiserComputeMemoryResources(_optixDenoiser,
132 _denoserState.resize(_denoiserSizes.stateSizeInBytes);
133 _scratch.resize(_denoiserSizes.recommendedScratchSizeInBytes);
135 OPTIX_CHECK(optixDenoiserSetup(
140 _denoserState.devicePointer(),
141 _denoiserSizes.stateSizeInBytes,
142 _scratch.devicePointer(),
143 _denoiserSizes.recommendedScratchSizeInBytes));
153 scene->
root3D()->createInstanceAccelerationStructureFlat();
155 _params.handle = scene->
root3D()->optixTraversableHandle();
159 ortCamera cameraData{};
160 cameraData.eye = make_float3(eye);
161 cameraData.U = make_float3(u);
162 cameraData.V = make_float3(v);
163 cameraData.W = make_float3(w);
165 RayGenClassicSbtRecord rayGenSbtRecord;
166 _rayGenClassicBuffer.download(&rayGenSbtRecord);
167 OPTIX_CHECK(optixSbtRecordPackHeader(_pinhole_raygen_prog_group,
169 rayGenSbtRecord.data = cameraData;
170 _rayGenClassicBuffer.upload(&rayGenSbtRecord);
172 _params.seed = (
SLuint)time(
nullptr);
174 _paramsBuffer.upload(&_params);
177 SLbool SLOptixPathtracer::render()
190 _paramsBuffer.devicePointer(),
191 _paramsBuffer.size(),
197 CUDA_SYNC_CHECK(SLOptix::stream);
204 if (_denoiserEnabled)
206 OptixImage2D optixImage2D;
207 optixImage2D.data = _imageBuffer.devicePointer();
208 optixImage2D.width = _sv->scrW();
209 optixImage2D.height = _sv->scrH();
210 optixImage2D.rowStrideInBytes = _sv->scrW() *
sizeof(float4);
211 optixImage2D.pixelStrideInBytes = 0;
212 optixImage2D.format = OPTIX_PIXEL_FORMAT_FLOAT4;
214 OptixDenoiserParams denoiserParams;
215 denoiserParams.denoiseAlpha = 0;
216 denoiserParams.blendFactor = 0.0f;
217 denoiserParams.hdrIntensity = 0;
219 OPTIX_CHECK(optixDenoiserInvoke(
223 _denoserState.devicePointer(),
224 _denoiserSizes.stateSizeInBytes,
230 _scratch.devicePointer(),
231 _denoiserSizes.recommendedScratchSizeInBytes));
232 CUDA_SYNC_CHECK(SLOptix::stream);
vector< SLMesh * > SLVMesh
Toplevel holder of the assets meshes, materials, textures and shaders.
Active or visible camera node class.
void UVWFrame(SLVec3f &EYE, SLVec3f &U, SLVec3f &V, SLVec3f &W)
static unsigned int instanceIndex
???
The SLScene class represents the top level instance holding the scene structure.
void root3D(SLNode *root3D)
SceneView class represents a dynamic real time 3D view onto the scene.
void camera(SLCamera *camera)
void log(const char *tag, const char *format,...)
logs a formatted string platform independently