1.3.72
 
Loading...
Searching...
No Matches
OptiX8Backend.h
Go to the documentation of this file.
1
16#ifndef OPTIX8_BACKEND_H
17#define OPTIX8_BACKEND_H
18
19#include "RayTracingBackend.h"
20#include "../optix8/OptiX8LaunchParams.h"
21
22// CUDA runtime
23#include <cuda_runtime.h>
24
25// OptiX 8.1 host API
26#include <optix.h>
27#include <optix_stubs.h>
28#include <optix_stack_size.h>
29
30// Standard library
31#include <vector>
32#include <string>
33
34// CUDA error checking macro
35#define CUDA_CHECK(call) \
36 do { \
37 cudaError_t rc = call; \
38 if (rc != cudaSuccess) { \
39 helios_runtime_error("CUDA error in " + std::string(__FILE__) + ":" + \
40 std::to_string(__LINE__) + ": " + cudaGetErrorString(rc)); \
41 } \
42 } while (0)
43
44// OptiX error checking macro
45#define OPTIX_CHECK(call) \
46 do { \
47 OptixResult rc = call; \
48 if (rc != OPTIX_SUCCESS) { \
49 helios_runtime_error("OptiX error in " + std::string(__FILE__) + ":" + \
50 std::to_string(__LINE__) + ": " + optixGetErrorName(rc) + \
51 " (" + optixGetErrorString(rc) + ")"); \
52 } \
53 } while (0)
54
55
56namespace helios {
57
72 public:
74 ~OptiX8Backend() override;
75
84 static bool probe() noexcept;
85
86 // Lifecycle
87 void initialize() override;
88 void shutdown() override;
89
90 // Geometry management
91 void updateGeometry(const RayTracingGeometry &geometry) override;
92 void buildAccelerationStructure() override;
93
94 // Material/optical properties
95 void updateMaterials(const RayTracingMaterial &materials) override;
96
97 // Radiation sources
98 void updateSources(const std::vector<RayTracingSource> &sources) override;
99
100 // Diffuse/sky radiation
101 void updateDiffuseRadiation(const std::vector<float> &flux, const std::vector<float> &extinction,
102 const std::vector<helios::vec3> &peak_dir,
103 const std::vector<float> &dist_norm,
104 const std::vector<float> &sky_energy) override;
105
106 void updateSkyModel(const std::vector<helios::vec4> &sky_radiance_params,
107 const std::vector<float> &camera_sky_radiance,
108 const helios::vec3 &sun_direction,
109 const std::vector<float> &solar_disk_radiance,
110 float solar_disk_cos_angle) override;
111
112 // Ray launching
113 void launchDirectRays(const RayTracingLaunchParams &params) override;
114 void launchDiffuseRays(const RayTracingLaunchParams &params) override;
115 void launchCameraRays(const RayTracingLaunchParams &params) override;
116 void launchPixelLabelRays(const RayTracingLaunchParams &params) override;
117
118 // Results retrieval
119 void getRadiationResults(RayTracingResults &results) override;
120 void getCameraResults(std::vector<float> &pixel_data, std::vector<uint> &pixel_labels,
121 std::vector<float> &pixel_depths, uint camera_id,
122 const helios::int2 &resolution) override;
123
124 // Buffer utilities
125 void zeroRadiationBuffers(size_t launch_band_count) override;
126 void zeroScatterBuffers() override;
127 void zeroCameraPixelBuffers(const helios::int2 &resolution) override;
128 void copyScatterToRadiation() override;
129 void uploadRadiationOut(const std::vector<float> &radiation_out_top,
130 const std::vector<float> &radiation_out_bottom) override;
131 void uploadCameraScatterBuffers(const std::vector<float> &scatter_top_cam,
132 const std::vector<float> &scatter_bottom_cam) override;
133 void zeroCameraScatterBuffers(size_t launch_band_count) override;
134 void uploadSourceFluxes(const std::vector<float> &fluxes) override;
135 void uploadSourceFluxesCam(const std::vector<float> &fluxes_cam) override;
136
137 // Diagnostics
138 void queryGPUMemory() const override;
139 std::string getBackendName() const override { return "OptiX 8.1"; }
140
141 private:
142 // ---- OptiX pipeline objects ----
143 OptixDeviceContext optix_context = nullptr;
144 OptixModule optix_module = nullptr;
145 OptixPipeline optix_pipeline = nullptr;
146 CUstream cuda_stream = nullptr;
147
148 // Program groups (4 raygen + 4 miss + 4 closest-hit + 6 intersection)
149 OptixProgramGroup pg_raygen_direct = nullptr;
150 OptixProgramGroup pg_raygen_diffuse = nullptr;
151 OptixProgramGroup pg_raygen_camera = nullptr;
152 OptixProgramGroup pg_raygen_pixel_label = nullptr;
153
154 OptixProgramGroup pg_miss_direct = nullptr;
155 OptixProgramGroup pg_miss_diffuse = nullptr;
156 OptixProgramGroup pg_miss_camera = nullptr;
157 OptixProgramGroup pg_miss_pixel_label = nullptr;
158
159 OptixProgramGroup pg_hit_direct = nullptr;
160 OptixProgramGroup pg_hit_diffuse = nullptr;
161 OptixProgramGroup pg_hit_camera = nullptr;
162 OptixProgramGroup pg_hit_pixel_label = nullptr;
163
164 // ---- GAS / acceleration structure ----
165 OptixTraversableHandle gas_handle = 0;
166 CUdeviceptr d_gas_output = 0;
167
168 // ---- SBT ----
169 OptixShaderBindingTable sbt = {};
170 CUdeviceptr d_raygen_records = 0;
171 CUdeviceptr d_miss_records = 0;
172 CUdeviceptr d_hitgroup_records= 0;
173
174 // Per-raygen device pointers (byte offsets into d_raygen_records; set in buildSBT)
175 CUdeviceptr d_raygen_record_direct = 0;
176 CUdeviceptr d_raygen_record_diffuse = 0;
177 CUdeviceptr d_raygen_record_camera = 0;
178 CUdeviceptr d_raygen_record_pixel_label = 0;
179
180 // ---- Launch params (device copy) ----
181 OptiX8LaunchParams h_params = {};
182 CUdeviceptr d_params = 0;
183
184 // ---- Geometry device buffers ----
185 CUdeviceptr d_transform_matrix = 0;
186 CUdeviceptr d_primitive_type = 0;
187 CUdeviceptr d_primitive_positions = 0;
188 CUdeviceptr d_primitiveID = 0;
189 CUdeviceptr d_objectID = 0;
190 CUdeviceptr d_object_subdivisions = 0;
191 CUdeviceptr d_twosided_flag = 0;
192 CUdeviceptr d_primitive_solid_fraction = 0;
193
194 // Per-type geometry
195 CUdeviceptr d_patch_vertices = 0;
196 CUdeviceptr d_patch_UUIDs = 0;
197 CUdeviceptr d_triangle_vertices = 0;
198 CUdeviceptr d_triangle_UUIDs = 0;
199 CUdeviceptr d_disk_centers = 0;
200 CUdeviceptr d_disk_radii = 0;
201 CUdeviceptr d_disk_normals = 0;
202 CUdeviceptr d_disk_UUIDs = 0;
203 CUdeviceptr d_tile_vertices = 0;
204 CUdeviceptr d_tile_UUIDs = 0;
205 CUdeviceptr d_voxel_vertices = 0;
206 CUdeviceptr d_voxel_UUIDs = 0;
207 CUdeviceptr d_bbox_vertices = 0;
208 CUdeviceptr d_bbox_UUIDs = 0;
209
210 // Global UUID array: d_primitive_uuid[global_pos] = UUID
211 CUdeviceptr d_primitive_uuid_arr = 0;
212
213 // AABB buffer for GAS build (one AABB per primitive)
214 CUdeviceptr d_aabbs = 0;
215
216 // ---- Material device buffers ----
217 CUdeviceptr d_rho = 0;
218 CUdeviceptr d_tau = 0;
219 CUdeviceptr d_rho_cam = 0;
220 CUdeviceptr d_tau_cam = 0;
221 CUdeviceptr d_specular_exponent = 0;
222 CUdeviceptr d_specular_scale = 0;
223
224 // ---- Radiation energy device buffers ----
225 CUdeviceptr d_radiation_in = 0;
226 CUdeviceptr d_radiation_out_top = 0;
227 CUdeviceptr d_radiation_out_bottom = 0;
228 CUdeviceptr d_scatter_buff_top = 0;
229 CUdeviceptr d_scatter_buff_bottom = 0;
230 CUdeviceptr d_radiation_in_camera = 0;
231 CUdeviceptr d_scatter_buff_top_cam = 0;
232 CUdeviceptr d_scatter_buff_bottom_cam= 0;
233 CUdeviceptr d_radiation_specular = 0;
234 CUdeviceptr d_Rsky = 0;
235
236 // ---- Camera device buffers ----
237 CUdeviceptr d_camera_pixel_label = 0;
238 CUdeviceptr d_camera_pixel_depth = 0;
239
240 // ---- Source device buffers ----
241 CUdeviceptr d_source_positions = 0;
242 CUdeviceptr d_source_rotations = 0;
243 CUdeviceptr d_source_widths = 0;
244 CUdeviceptr d_source_types = 0;
245 CUdeviceptr d_source_fluxes = 0;
246 CUdeviceptr d_source_fluxes_cam = 0;
247
248 // ---- Diffuse/sky device buffers ----
249 CUdeviceptr d_diffuse_flux = 0;
250 CUdeviceptr d_diffuse_extinction = 0;
251 CUdeviceptr d_diffuse_peak_dir = 0;
252 CUdeviceptr d_diffuse_dist_norm = 0;
253 CUdeviceptr d_sky_radiance_params = 0;
254 CUdeviceptr d_camera_sky_radiance = 0;
255 CUdeviceptr d_solar_disk_radiance = 0;
256
257 // ---- Band launch flag buffer ----
258 CUdeviceptr d_band_launch_flag = 0;
259
260 // ---- Texture/mask device buffers ----
261 CUdeviceptr d_mask_data = 0;
262 CUdeviceptr d_mask_offsets = 0;
263 CUdeviceptr d_mask_sizes = 0;
264 CUdeviceptr d_mask_IDs = 0;
265 CUdeviceptr d_uv_data = 0;
266 CUdeviceptr d_uv_IDs = 0;
267
268 // ---- State ----
269 bool is_initialized = false;
270 size_t current_primitive_count = 0;
271 size_t current_patch_count = 0;
272 size_t current_triangle_count = 0;
273 size_t current_disk_count = 0;
274 size_t current_tile_count = 0;
275 size_t current_voxel_count = 0;
276 size_t current_bbox_count = 0;
277 size_t current_source_count = 0;
278 size_t current_band_count = 0;
279 size_t current_camera_count = 0;
280 size_t current_launch_band_count = 0;
281 uint32_t current_camera_launch_id = 0xFFFFFFFFu;
282
283 // ---- Private helper methods ----
284
286 void freeCUdeviceptr(CUdeviceptr &ptr);
287
289 void freeGeometryBuffers();
290
292 void freeMaterialBuffers();
293
295 void buildAABBs(const RayTracingGeometry &geometry);
296
298 void buildGAS(uint32_t Nprimitives);
299
301 void buildSBT();
302
304 void uploadLaunchParams();
305
307 void applyLaunchParams(const RayTracingLaunchParams &params);
308
310 void reallocDevice(CUdeviceptr &ptr, size_t bytes);
311
313 std::vector<float> downloadFloat(CUdeviceptr ptr, size_t count) const;
314
316 std::vector<uint32_t> downloadUInt32(CUdeviceptr ptr, size_t count) const;
317
319 std::string findDeviceCodeFile() const;
320 };
321
322} // namespace helios
323
324#endif // OPTIX8_BACKEND_H