1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2019 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Eric Brandt, Asher Elmquist
13 // =============================================================================
14 //
15 // =============================================================================
16 
17 #ifndef CHFILTEROPTIXRENDER_H
18 #define CHFILTEROPTIXRENDER_H
19 
20 #include <memory>
21 #include "chrono_sensor/filters/ChFilter.h"
22 #include "chrono_sensor/filters/ChFilterVisualize.h"
23 #include "chrono_sensor/optix/ChOptixUtils.h"
24 #include "chrono_sensor/sensors/ChOptixSensor.h"
25 #include "chrono_sensor/optix/ChOptixPipeline.h"
26 #include "chrono_sensor/optix/ChOptixDefinitions.h"
27 
28 namespace chrono {
29 namespace sensor {
30 
31 // forward declaration
32 class ChSensor;
33 
34 /// @addtogroup sensor_filters
35 /// @{
36 
37 class CH_SENSOR_API ChOptixDenoiser {
38   public:
39     ChOptixDenoiser(OptixDeviceContext context);
40     ~ChOptixDenoiser();
41     void Initialize(unsigned int w,
42                     unsigned int h,
43                     CUstream stream,
44                     half4* input_buffer,
45                     half4* albedo_buffer,
46                     half4* normal_buffer,
47                     half4* output_buffer);
48     void Execute();
49 
50   private:
51     CUstream m_cuda_stream;
52     OptixDenoiser m_denoiser = nullptr;
53     OptixDenoiserParams m_params = {};
54     CUdeviceptr md_intensity = 0;
55     CUdeviceptr md_scratch = 0;
56     uint32_t m_scratch_size = 0;
57     CUdeviceptr md_state = 0;
58     uint32_t m_state_size = 0;
59     std::vector<OptixImage2D> md_inputs;
60     OptixImage2D md_output;
61 };
62 
63 /// A filter that generates data for a ChOptixSensor
64 class CH_SENSOR_API ChFilterOptixRender : public ChFilter {
65   public:
66     /// Class constructor
67     ChFilterOptixRender();
68 
69     virtual ~ChFilterOptixRender();
70 
71     /// Apply function. Generates data for ChOptixSensors
72     /// @param pSensor A pointer to the sensor on which the filter is attached.
73     /// @param bufferInOut A buffer that is passed into the filter.
74     virtual void Apply();
75 
76     /// Initializes all data needed by the filter access apply function.
77     /// @param pSensor A pointer to the sensor.
78     /// @param bufferInOut A pointer to the process buffer
79     virtual void Initialize(std::shared_ptr<ChSensor> pSensor, std::shared_ptr<SensorBuffer>& bufferInOut);
80 
81   private:
82     /// Finds visual filters
83     std::shared_ptr<ChFilterVisualize> FindOnlyVisFilter(std::shared_ptr<ChSensor> pSensor);
84 
85     std::shared_ptr<SensorBuffer> m_bufferOut;
86     std::weak_ptr<ChOptixSensor> m_optixSensor;  ///< for holding a weak reference to parent sensor
87     CUstream m_cuda_stream;                      ///< reference to a cuda stream
88 
89     std::shared_ptr<ChOptixDenoiser> m_denoiser;  ///< denoiser in case there is global illumination
90     std::shared_ptr<curandState_t> m_rng;         ///< rng buffer for camera jitter or ray bounces
91 
92     // Special handles that will accessed by ChOptixEngine
93     OptixPipeline m_optix_pipeline;  ///< to hold reference to thte optix pipeline of this sensor
94     ContextParameters* m_optix_params;
95     std::shared_ptr<OptixShaderBindingTable> m_optix_sbt;
96     std::shared_ptr<Record<RaygenParameters>> m_raygen_record;  ///< ray generation record
97     float m_time_stamp;                                         ///< time stamp for when the data (render) was launched
98 
99     friend class ChOptixEngine;  ///< ChOptixEngine is allowed to set and use the private members
100 };
101 
102 /// @}
103 
104 }  // namespace sensor
105 }  // namespace chrono
106 
107 #endif
108