1.3.72
 
Loading...
Searching...
No Matches
OptiX6Backend.h
Go to the documentation of this file.
1
16#ifndef OPTIX6_BACKEND_H
17#define OPTIX6_BACKEND_H
18
19#include "RayTracingBackend.h"
20
21// OptiX 6.5 includes
22#include <optix.h>
23#include <optixu/optixpp_namespace.h>
24#include <optixu/optixu_vector_functions.h>
25#include <optixu/optixu_vector_types.h>
26
27namespace helios {
28
29 // Ray type enumeration
30 enum RayType { RAYTYPE_DIRECT = 0, RAYTYPE_DIFFUSE = 1, RAYTYPE_CAMERA = 2, RAYTYPE_PIXEL_LABEL = 3 };
31
32// OptiX error checking macros
33#define RT_CHECK_ERROR(func) \
34 do { \
35 RTresult code = func; \
36 if (code != RT_SUCCESS) \
37 sutilHandleError(OptiX_Context, code, __FILE__, __LINE__); \
38 } while (0)
39
40#define RT_CHECK_ERROR_NOEXIT(func) \
41 do { \
42 RTresult code = func; \
43 if (code != RT_SUCCESS) { \
44 const char *message; \
45 rtContextGetErrorString(OptiX_Context, code, &message); \
46 std::cerr << "WARNING (OptiX cleanup): " << message << " (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; \
47 } \
48 } while (0)
49
57 public:
59 ~OptiX6Backend() override;
60
69 static bool probe() noexcept;
70
71 // Lifecycle
72 void initialize() override;
73 void shutdown() override;
74
75 // Geometry management
76 void updateGeometry(const RayTracingGeometry &geometry) override;
77 void buildAccelerationStructure() override;
78
79 // Material/optical properties
80 void updateMaterials(const RayTracingMaterial &materials) override;
81
82 // Radiation sources
83 void updateSources(const std::vector<RayTracingSource> &sources) override;
84
85 // Diffuse/sky radiation
86 void updateDiffuseRadiation(const std::vector<float> &flux, const std::vector<float> &extinction, const std::vector<helios::vec3> &peak_dir, const std::vector<float> &dist_norm, const std::vector<float> &sky_energy) override;
87
88 void updateSkyModel(const std::vector<helios::vec4> &sky_radiance_params, const std::vector<float> &camera_sky_radiance, const helios::vec3 &sun_direction, const std::vector<float> &solar_disk_radiance, float solar_disk_cos_angle) override;
89
90 // Ray launching
91 void launchDirectRays(const RayTracingLaunchParams &params) override;
92 void launchDiffuseRays(const RayTracingLaunchParams &params) override;
93 void launchCameraRays(const RayTracingLaunchParams &params) override;
94 void launchPixelLabelRays(const RayTracingLaunchParams &params) override;
95
96 // Results retrieval
97 void getRadiationResults(RayTracingResults &results) override;
98 void getCameraResults(std::vector<float> &pixel_data, std::vector<uint> &pixel_labels, std::vector<float> &pixel_depths, uint camera_id, const helios::int2 &resolution) override;
99
100 // Buffer utilities
101 void zeroRadiationBuffers(size_t launch_band_count) override;
102 void zeroScatterBuffers() override;
103 void zeroCameraPixelBuffers(const helios::int2 &resolution) override;
104 void copyScatterToRadiation() override;
105 void uploadRadiationOut(const std::vector<float> &radiation_out_top, const std::vector<float> &radiation_out_bottom) override;
106 void uploadCameraScatterBuffers(const std::vector<float> &scatter_top_cam, const std::vector<float> &scatter_bottom_cam) override;
107 void zeroCameraScatterBuffers(size_t launch_band_count) override;
108 void uploadSourceFluxes(const std::vector<float> &fluxes) override;
109 void uploadSourceFluxesCam(const std::vector<float> &fluxes_cam) override;
110
111 // Diagnostics
112 void queryGPUMemory() const override;
113 std::string getBackendName() const override;
114
115 private:
116 // OptiX context and core objects
117 RTcontext OptiX_Context;
118
119 // Ray generation programs (4 ray types)
120 RTprogram direct_raygen;
121 RTprogram diffuse_raygen;
122 RTprogram camera_raygen;
123 RTprogram pixel_label_raygen;
124
125 // Geometry and acceleration structures
126 RTgeometrygroup base_geometry_group;
127 RTacceleration base_acceleration;
128 RTgroup top_level_group;
129 RTacceleration top_level_acceleration;
130 RTtransform transform;
131
132 // Geometry objects (6 primitive types)
133 RTgeometry patch_geometry;
134 RTgeometry triangle_geometry;
135 RTgeometry disk_geometry;
136 RTgeometry tile_geometry;
137 RTgeometry voxel_geometry;
138 RTgeometry bbox_geometry;
139
140 // Geometry instances
141 RTgeometryinstance patch_geometryinstance;
142 RTgeometryinstance triangle_geometryinstance;
143 RTgeometryinstance disk_geometryinstance;
144 RTgeometryinstance tile_geometryinstance;
145 RTgeometryinstance voxel_geometryinstance;
146 RTgeometryinstance bbox_geometryinstance;
147
148 // Materials (one per primitive type)
149 RTmaterial patch_material;
150 RTmaterial triangle_material;
151 RTmaterial disk_material;
152 RTmaterial tile_material;
153 RTmaterial voxel_material;
154 RTmaterial bbox_material;
155
156 // Intersection programs
157 RTprogram rectangle_intersect;
158 RTprogram rectangle_bounds;
159 RTprogram triangle_intersect;
160 RTprogram triangle_bounds;
161 RTprogram disk_intersect;
162 RTprogram disk_bounds;
163 RTprogram tile_intersect;
164 RTprogram tile_bounds;
165 RTprogram voxel_intersect;
166 RTprogram voxel_bounds;
167 RTprogram bbox_intersect;
168 RTprogram bbox_bounds;
169
170 // Hit programs
171 RTprogram closest_hit_direct;
172 RTprogram closest_hit_diffuse;
173 RTprogram closest_hit_camera;
174 RTprogram closest_hit_pixel_label;
175 RTprogram miss_direct;
176 RTprogram miss_diffuse;
177 RTprogram miss_camera;
178
179 // Buffers: Geometry/Topology (17 buffers)
180 RTbuffer patch_vertices_RTbuffer;
181 RTvariable patch_vertices_RTvariable;
182 RTbuffer triangle_vertices_RTbuffer;
183 RTvariable triangle_vertices_RTvariable;
184 RTbuffer disk_centers_RTbuffer;
185 RTvariable disk_centers_RTvariable;
186 RTbuffer disk_radii_RTbuffer;
187 RTvariable disk_radii_RTvariable;
188 RTbuffer disk_normals_RTbuffer;
189 RTvariable disk_normals_RTvariable;
190 RTbuffer tile_vertices_RTbuffer;
191 RTvariable tile_vertices_RTvariable;
192 RTbuffer voxel_vertices_RTbuffer;
193 RTvariable voxel_vertices_RTvariable;
194 RTbuffer bbox_vertices_RTbuffer;
195 RTvariable bbox_vertices_RTvariable;
196 RTbuffer object_subdivisions_RTbuffer;
197 RTvariable object_subdivisions_RTvariable;
198 RTbuffer primitive_type_RTbuffer;
199 RTvariable primitive_type_RTvariable;
200 RTbuffer primitive_solid_fraction_RTbuffer;
201 RTvariable primitive_solid_fraction_RTvariable;
202
203 // Buffers: UUIDs/Mapping (12 buffers)
204 RTbuffer patch_UUID_RTbuffer;
205 RTvariable patch_UUID_RTvariable;
206 RTbuffer triangle_UUID_RTbuffer;
207 RTvariable triangle_UUID_RTvariable;
208 RTbuffer disk_UUID_RTbuffer;
209 RTvariable disk_UUID_RTvariable;
210 RTbuffer tile_UUID_RTbuffer;
211 RTvariable tile_UUID_RTvariable;
212 RTbuffer voxel_UUID_RTbuffer;
213 RTvariable voxel_UUID_RTvariable;
214 RTbuffer bbox_UUID_RTbuffer;
215 RTvariable bbox_UUID_RTvariable;
216 RTbuffer objectID_RTbuffer;
217 RTvariable objectID_RTvariable;
218 RTbuffer primitiveID_RTbuffer;
219 RTvariable primitiveID_RTvariable;
220 RTbuffer primitive_positions_RTbuffer;
221 RTvariable primitive_positions_RTvariable;
222 RTbuffer twosided_flag_RTbuffer;
223 RTvariable twosided_flag_RTvariable;
224
225 // Buffers: Material Properties (8 buffers)
226 RTbuffer rho_RTbuffer;
227 RTvariable rho_RTvariable;
228 RTbuffer tau_RTbuffer;
229 RTvariable tau_RTvariable;
230 RTbuffer rho_cam_RTbuffer;
231 RTvariable rho_cam_RTvariable;
232 RTbuffer tau_cam_RTbuffer;
233 RTvariable tau_cam_RTvariable;
234 RTbuffer specular_exponent_RTbuffer;
235 RTvariable specular_exponent_RTvariable;
236 RTbuffer specular_scale_RTbuffer;
237 RTvariable specular_scale_RTvariable;
238 RTbuffer transform_matrix_RTbuffer;
239 RTvariable transform_matrix_RTvariable;
240
241 // Buffers: Radiation Energy (10 buffers)
242 RTbuffer radiation_in_RTbuffer;
243 RTvariable radiation_in_RTvariable;
244 RTbuffer radiation_out_top_RTbuffer;
245 RTvariable radiation_out_top_RTvariable;
246 RTbuffer radiation_out_bottom_RTbuffer;
247 RTvariable radiation_out_bottom_RTvariable;
248 RTbuffer Rsky_RTbuffer;
249 RTvariable Rsky_RTvariable;
250 RTbuffer radiation_specular_RTbuffer;
251 RTvariable radiation_specular_RTvariable;
252 RTbuffer scatter_buff_top_RTbuffer;
253 RTvariable scatter_buff_top_RTvariable;
254 RTbuffer scatter_buff_bottom_RTbuffer;
255 RTvariable scatter_buff_bottom_RTvariable;
256 RTbuffer radiation_in_camera_RTbuffer;
257 RTvariable radiation_in_camera_RTvariable;
258 RTbuffer scatter_buff_top_cam_RTbuffer;
259 RTvariable scatter_buff_top_cam_RTvariable;
260 RTbuffer scatter_buff_bottom_cam_RTbuffer;
261 RTvariable scatter_buff_bottom_cam_RTvariable;
262
263 // Buffers: Camera (3 buffers)
264 RTbuffer camera_pixel_label_RTbuffer;
265 RTvariable camera_pixel_label_RTvariable;
266 RTbuffer camera_pixel_depth_RTbuffer;
267 RTvariable camera_pixel_depth_RTvariable;
268
269 // Buffers: Diffuse/Sky (5 buffers)
270 RTbuffer diffuse_flux_RTbuffer;
271 RTvariable diffuse_flux_RTvariable;
272 RTbuffer diffuse_extinction_RTbuffer;
273 RTvariable diffuse_extinction_RTvariable;
274 RTbuffer diffuse_peak_dir_RTbuffer;
275 RTvariable diffuse_peak_dir_RTvariable;
276 RTbuffer diffuse_dist_norm_RTbuffer;
277 RTvariable diffuse_dist_norm_RTvariable;
278 RTbuffer sky_radiance_params_RTbuffer;
279 RTvariable sky_radiance_params_RTvariable;
280 RTbuffer camera_sky_radiance_RTbuffer;
281 RTvariable camera_sky_radiance_RTvariable;
282 RTbuffer solar_disk_radiance_RTbuffer;
283 RTvariable solar_disk_radiance_RTvariable;
284
285 // Buffers: Sources/Texture (9 buffers)
286 RTbuffer source_positions_RTbuffer;
287 RTvariable source_positions_RTvariable;
288 RTbuffer source_types_RTbuffer;
289 RTvariable source_types_RTvariable;
290 RTbuffer source_fluxes_RTbuffer;
291 RTvariable source_fluxes_RTvariable;
292 RTbuffer source_fluxes_cam_RTbuffer;
293 RTvariable source_fluxes_cam_RTvariable;
294 RTbuffer source_widths_RTbuffer;
295 RTvariable source_widths_RTvariable;
296 RTbuffer source_rotations_RTbuffer;
297 RTvariable source_rotations_RTvariable;
298 RTbuffer band_launch_flag_RTbuffer;
299 RTvariable band_launch_flag_RTvariable;
300 RTbuffer max_scatters_RTbuffer;
301 RTvariable max_scatters_RTvariable;
302
303 // Buffers: Texture/Masking (5 buffers)
304 RTbuffer maskdata_RTbuffer;
305 RTvariable maskdata_RTvariable;
306 RTbuffer masksize_RTbuffer;
307 RTvariable masksize_RTvariable;
308 RTbuffer maskID_RTbuffer;
309 RTvariable maskID_RTvariable;
310 RTbuffer uvdata_RTbuffer;
311 RTvariable uvdata_RTvariable;
312 RTbuffer uvID_RTbuffer;
313 RTvariable uvID_RTvariable;
314
315 // RT Variables (non-buffer parameters)
316 RTvariable direct_ray_type_RTvariable;
317 RTvariable diffuse_ray_type_RTvariable;
318 RTvariable camera_ray_type_RTvariable;
319 RTvariable pixel_label_ray_type_RTvariable;
320 RTvariable random_seed_RTvariable;
321 RTvariable launch_offset_RTvariable;
322 RTvariable launch_face_RTvariable;
323 RTvariable Nprimitives_RTvariable;
324 RTvariable bbox_UUID_base_RTvariable;
325 RTvariable Nsources_RTvariable;
326 RTvariable Nbands_global_RTvariable;
327 RTvariable Nbands_launch_RTvariable;
328 RTvariable Ncameras_RTvariable;
329 RTvariable periodic_flag_RTvariable;
330 RTvariable sun_direction_RTvariable;
331 RTvariable solar_disk_cos_angle_RTvariable;
332 RTvariable camera_position_RTvariable;
333 RTvariable camera_direction_RTvariable;
334 RTvariable camera_lens_diameter_RTvariable;
335 RTvariable camera_focal_length_RTvariable;
336 RTvariable FOV_aspect_ratio_RTvariable;
337 RTvariable camera_HFOV_RTvariable;
338 RTvariable camera_resolution_RTvariable;
339 RTvariable camera_viewplane_length_RTvariable;
340 RTvariable camera_pixel_solid_angle_RTvariable;
341 RTvariable camera_pixel_offset_x_RTvariable;
342 RTvariable camera_pixel_offset_y_RTvariable;
343 RTvariable camera_ID_RTvariable;
344 RTvariable camera_resolution_full_RTvariable;
345 RTvariable specular_reflection_enabled_RTvariable;
346 RTvariable scattering_iteration_RTvariable;
347
348 // Helper methods for buffer management (will be moved from RadiationModel)
349 void addBuffer(const char *name, RTbuffer &buffer, RTvariable &variable, RTbuffertype type, RTformat format, size_t dimension);
350 void zeroBuffer1D(RTbuffer &buffer, size_t bsize);
351 void zeroBuffer2D(RTbuffer &buffer, const helios::int2 &bsize);
352 void initializeBuffer1Df(RTbuffer &buffer, const std::vector<float> &array);
353 void initializeBuffer1Dui(RTbuffer &buffer, const std::vector<uint> &array);
354 void initializeBuffer1Di(RTbuffer &buffer, const std::vector<int> &array);
355 void initializeBuffer1Dchar(RTbuffer &buffer, const std::vector<char> &array);
356 void initializeBuffer1Dbool(RTbuffer &buffer, const std::vector<bool> &array);
357 void initializeBuffer1Dfloat2(RTbuffer &buffer, const std::vector<helios::vec2> &array);
358 void initializeBuffer1Dfloat3(RTbuffer &buffer, const std::vector<helios::vec3> &array);
359 void initializeBuffer1Dfloat4(RTbuffer &buffer, const std::vector<helios::vec4> &array);
360 void initializeBuffer1Dint2(RTbuffer &buffer, const std::vector<helios::int2> &array);
361 void initializeBuffer2Df(RTbuffer &buffer, const std::vector<std::vector<float>> &array);
362 void initializeBuffer2Dui(RTbuffer &buffer, const std::vector<std::vector<uint>> &array);
363 void initializeBuffer2Di(RTbuffer &buffer, const std::vector<std::vector<int>> &array);
364 void initializeBuffer2Dfloat2(RTbuffer &buffer, const std::vector<std::vector<helios::vec2>> &array);
365 void initializeBuffer2Dfloat3(RTbuffer &buffer, const std::vector<std::vector<helios::vec3>> &array);
366 void initializeBuffer2Dfloat3(RTbuffer &buffer, const std::vector<std::vector<optix::float3>> &array);
367 void initializeBuffer3Dbool(RTbuffer &buffer, const std::vector<std::vector<std::vector<bool>>> &array);
368 void copyBuffer1D(RTbuffer &source, RTbuffer &dest);
369 std::vector<float> getOptiXbufferData(RTbuffer buffer);
370 std::vector<uint> getOptiXbufferData_ui(RTbuffer buffer);
371
372 // Conversion methods: backend-agnostic → OptiX
373 void geometryToBuffers(const RayTracingGeometry &geometry);
374 void materialsToBuffers(const RayTracingMaterial &materials);
375 void sourcesToBuffers(const std::vector<RayTracingSource> &sources);
376 void diffuseToBuffers(const std::vector<float> &flux, const std::vector<float> &extinction, const std::vector<helios::vec3> &peak_dir, const std::vector<float> &dist_norm, const std::vector<float> &sky_energy);
377 void skyModelToBuffers(const std::vector<helios::vec4> &sky_radiance_params, const std::vector<float> &camera_sky_radiance, const helios::vec3 &sun_direction, const std::vector<float> &solar_disk_radiance, float solar_disk_cos_angle);
378 void launchParamsToVariables(const RayTracingLaunchParams &params);
379
380 // Extraction methods: OptiX → backend-agnostic
381 void buffersToResults(RayTracingResults &results);
382
383 // Internal state tracking
384 bool is_initialized = false;
385 size_t current_primitive_count = 0;
386 size_t current_patch_count = 0;
387 size_t current_triangle_count = 0;
388 size_t current_disk_count = 0;
389 size_t current_tile_count = 0;
390 size_t current_voxel_count = 0;
391 size_t current_bbox_count = 0;
392 size_t current_source_count = 0;
393 size_t current_band_count = 0;
394 size_t current_camera_count = 0;
395 uint32_t current_camera_launch_id = UINT32_MAX;
396 size_t current_launch_band_count = 0;
397 };
398
399} // namespace helios
400
401#endif // OPTIX6_BACKEND_H