1.3.77
 
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,
89 const std::vector<float> &camera_diffuse_flux, const std::vector<uint32_t> &band_emission_flag) override;
90
91 // Ray launching
92 void launchDirectRays(const RayTracingLaunchParams &params) override;
93 void launchDiffuseRays(const RayTracingLaunchParams &params) override;
94 void launchCameraRays(const RayTracingLaunchParams &params) override;
95 void launchPixelLabelRays(const RayTracingLaunchParams &params) override;
96
97 // Results retrieval
98 void getRadiationResults(RayTracingResults &results) override;
99 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;
100
101 // Buffer utilities
102 void zeroRadiationBuffers(size_t launch_band_count) override;
103 void zeroScatterBuffers() override;
104 void zeroCameraPixelBuffers(const helios::int2 &resolution) override;
105 void copyScatterToRadiation() override;
106 void uploadRadiationOut(const std::vector<float> &radiation_out_top, const std::vector<float> &radiation_out_bottom) override;
107 void uploadCameraScatterBuffers(const std::vector<float> &scatter_top_cam, const std::vector<float> &scatter_bottom_cam) override;
108 void zeroCameraScatterBuffers(size_t launch_band_count) override;
109 void uploadSourceFluxes(const std::vector<float> &fluxes) override;
110 void uploadSourceFluxesCam(const std::vector<float> &fluxes_cam) override;
111
112 // Diagnostics
113 void queryGPUMemory() const override;
114 std::string getBackendName() const override;
115
116 private:
117 // OptiX context and core objects
118 RTcontext OptiX_Context;
119
120 // Ray generation programs (4 ray types)
121 RTprogram direct_raygen;
122 RTprogram diffuse_raygen;
123 RTprogram camera_raygen;
124 RTprogram pixel_label_raygen;
125
126 // Geometry and acceleration structures
127 RTgeometrygroup base_geometry_group;
128 RTacceleration base_acceleration;
129 RTgroup top_level_group;
130 RTacceleration top_level_acceleration;
131 RTtransform transform;
132
133 // Geometry objects (6 primitive types)
134 RTgeometry patch_geometry;
135 RTgeometry triangle_geometry;
136 RTgeometry disk_geometry;
137 RTgeometry tile_geometry;
138 RTgeometry voxel_geometry;
139 RTgeometry bbox_geometry;
140
141 // Geometry instances
142 RTgeometryinstance patch_geometryinstance;
143 RTgeometryinstance triangle_geometryinstance;
144 RTgeometryinstance disk_geometryinstance;
145 RTgeometryinstance tile_geometryinstance;
146 RTgeometryinstance voxel_geometryinstance;
147 RTgeometryinstance bbox_geometryinstance;
148
149 // Materials (one per primitive type)
150 RTmaterial patch_material;
151 RTmaterial triangle_material;
152 RTmaterial disk_material;
153 RTmaterial tile_material;
154 RTmaterial voxel_material;
155 RTmaterial bbox_material;
156
157 // Intersection programs
158 RTprogram rectangle_intersect;
159 RTprogram rectangle_bounds;
160 RTprogram triangle_intersect;
161 RTprogram triangle_bounds;
162 RTprogram disk_intersect;
163 RTprogram disk_bounds;
164 RTprogram tile_intersect;
165 RTprogram tile_bounds;
166 RTprogram voxel_intersect;
167 RTprogram voxel_bounds;
168 RTprogram bbox_intersect;
169 RTprogram bbox_bounds;
170
171 // Hit programs
172 RTprogram closest_hit_direct;
173 RTprogram closest_hit_diffuse;
174 RTprogram closest_hit_camera;
175 RTprogram closest_hit_pixel_label;
176 RTprogram miss_direct;
177 RTprogram miss_diffuse;
178 RTprogram miss_camera;
179 RTprogram any_hit_direct;
180 RTprogram any_hit_diffuse;
181
182 // Buffers: Geometry/Topology (17 buffers)
183 RTbuffer patch_vertices_RTbuffer;
184 RTvariable patch_vertices_RTvariable;
185 RTbuffer triangle_vertices_RTbuffer;
186 RTvariable triangle_vertices_RTvariable;
187 RTbuffer disk_centers_RTbuffer;
188 RTvariable disk_centers_RTvariable;
189 RTbuffer disk_radii_RTbuffer;
190 RTvariable disk_radii_RTvariable;
191 RTbuffer disk_normals_RTbuffer;
192 RTvariable disk_normals_RTvariable;
193 RTbuffer tile_vertices_RTbuffer;
194 RTvariable tile_vertices_RTvariable;
195 RTbuffer voxel_vertices_RTbuffer;
196 RTvariable voxel_vertices_RTvariable;
197 RTbuffer bbox_vertices_RTbuffer;
198 RTvariable bbox_vertices_RTvariable;
199 RTbuffer object_subdivisions_RTbuffer;
200 RTvariable object_subdivisions_RTvariable;
201 RTbuffer primitive_type_RTbuffer;
202 RTvariable primitive_type_RTvariable;
203 RTbuffer primitive_solid_fraction_RTbuffer;
204 RTvariable primitive_solid_fraction_RTvariable;
205
206 // Buffers: UUIDs/Mapping (12 buffers)
207 RTbuffer patch_UUID_RTbuffer;
208 RTvariable patch_UUID_RTvariable;
209 RTbuffer triangle_UUID_RTbuffer;
210 RTvariable triangle_UUID_RTvariable;
211 RTbuffer disk_UUID_RTbuffer;
212 RTvariable disk_UUID_RTvariable;
213 RTbuffer tile_UUID_RTbuffer;
214 RTvariable tile_UUID_RTvariable;
215 RTbuffer voxel_UUID_RTbuffer;
216 RTvariable voxel_UUID_RTvariable;
217 RTbuffer bbox_UUID_RTbuffer;
218 RTvariable bbox_UUID_RTvariable;
219 RTbuffer objectID_RTbuffer;
220 RTvariable objectID_RTvariable;
221 RTbuffer primitiveID_RTbuffer;
222 RTvariable primitiveID_RTvariable;
223 RTbuffer primitive_positions_RTbuffer;
224 RTvariable primitive_positions_RTvariable;
225 RTbuffer twosided_flag_RTbuffer;
226 RTvariable twosided_flag_RTvariable;
227
228 // Buffers: Material Properties (8 buffers)
229 RTbuffer rho_RTbuffer;
230 RTvariable rho_RTvariable;
231 RTbuffer tau_RTbuffer;
232 RTvariable tau_RTvariable;
233 RTbuffer rho_cam_RTbuffer;
234 RTvariable rho_cam_RTvariable;
235 RTbuffer tau_cam_RTbuffer;
236 RTvariable tau_cam_RTvariable;
237 RTbuffer specular_exponent_RTbuffer;
238 RTvariable specular_exponent_RTvariable;
239 RTbuffer specular_scale_RTbuffer;
240 RTvariable specular_scale_RTvariable;
241 RTbuffer transform_matrix_RTbuffer;
242 RTvariable transform_matrix_RTvariable;
243 // Translucent cover (glass/plastic) material buffers (same layout as rho/tau)
244 RTbuffer glass_n_RTbuffer;
245 RTvariable glass_n_RTvariable;
246 RTbuffer glass_KL_RTbuffer;
247 RTvariable glass_KL_RTvariable;
248 RTbuffer is_glass_RTbuffer;
249 RTvariable is_glass_RTvariable;
250 RTvariable glass_enabled_RTvariable;
251
252 // Buffers: Radiation Energy (10 buffers)
253 RTbuffer radiation_in_RTbuffer;
254 RTvariable radiation_in_RTvariable;
255 RTbuffer radiation_out_top_RTbuffer;
256 RTvariable radiation_out_top_RTvariable;
257 RTbuffer radiation_out_bottom_RTbuffer;
258 RTvariable radiation_out_bottom_RTvariable;
259 RTbuffer Rsky_RTbuffer;
260 RTvariable Rsky_RTvariable;
261 RTbuffer radiation_specular_RTbuffer;
262 RTvariable radiation_specular_RTvariable;
263 RTbuffer scatter_buff_top_RTbuffer;
264 RTvariable scatter_buff_top_RTvariable;
265 RTbuffer scatter_buff_bottom_RTbuffer;
266 RTvariable scatter_buff_bottom_RTvariable;
267 RTbuffer radiation_in_camera_RTbuffer;
268 RTvariable radiation_in_camera_RTvariable;
269 RTbuffer scatter_buff_top_cam_RTbuffer;
270 RTvariable scatter_buff_top_cam_RTvariable;
271 RTbuffer scatter_buff_bottom_cam_RTbuffer;
272 RTvariable scatter_buff_bottom_cam_RTvariable;
273
274 // Buffers: Camera (3 buffers)
275 RTbuffer camera_pixel_label_RTbuffer;
276 RTvariable camera_pixel_label_RTvariable;
277 RTbuffer camera_pixel_depth_RTbuffer;
278 RTvariable camera_pixel_depth_RTvariable;
279
280 // Buffers: Diffuse/Sky (5 buffers)
281 RTbuffer diffuse_flux_RTbuffer;
282 RTvariable diffuse_flux_RTvariable;
283 RTbuffer diffuse_extinction_RTbuffer;
284 RTvariable diffuse_extinction_RTvariable;
285 RTbuffer diffuse_peak_dir_RTbuffer;
286 RTvariable diffuse_peak_dir_RTvariable;
287 RTbuffer diffuse_dist_norm_RTbuffer;
288 RTvariable diffuse_dist_norm_RTvariable;
289 RTbuffer sky_radiance_params_RTbuffer;
290 RTvariable sky_radiance_params_RTvariable;
291 RTbuffer camera_sky_radiance_RTbuffer;
292 RTvariable camera_sky_radiance_RTvariable;
293 RTbuffer solar_disk_radiance_RTbuffer;
294 RTvariable solar_disk_radiance_RTvariable;
295 RTbuffer camera_diffuse_flux_RTbuffer;
296 RTvariable camera_diffuse_flux_RTvariable;
297 RTbuffer band_emission_flag_RTbuffer;
298 RTvariable band_emission_flag_RTvariable;
299
300 // Buffers: Sources/Texture (9 buffers)
301 RTbuffer source_positions_RTbuffer;
302 RTvariable source_positions_RTvariable;
303 RTbuffer source_types_RTbuffer;
304 RTvariable source_types_RTvariable;
305 RTbuffer source_fluxes_RTbuffer;
306 RTvariable source_fluxes_RTvariable;
307 RTbuffer source_fluxes_cam_RTbuffer;
308 RTvariable source_fluxes_cam_RTvariable;
309 RTbuffer source_widths_RTbuffer;
310 RTvariable source_widths_RTvariable;
311 RTbuffer source_rotations_RTbuffer;
312 RTvariable source_rotations_RTvariable;
313 RTbuffer band_launch_flag_RTbuffer;
314 RTvariable band_launch_flag_RTvariable;
315 RTbuffer max_scatters_RTbuffer;
316 RTvariable max_scatters_RTvariable;
317
318 // Buffers: Texture/Masking (5 buffers)
319 RTbuffer maskdata_RTbuffer;
320 RTvariable maskdata_RTvariable;
321 RTbuffer masksize_RTbuffer;
322 RTvariable masksize_RTvariable;
323 RTbuffer maskID_RTbuffer;
324 RTvariable maskID_RTvariable;
325 RTbuffer uvdata_RTbuffer;
326 RTvariable uvdata_RTvariable;
327 RTbuffer uvID_RTbuffer;
328 RTvariable uvID_RTvariable;
329
330 // RT Variables (non-buffer parameters)
331 RTvariable direct_ray_type_RTvariable;
332 RTvariable diffuse_ray_type_RTvariable;
333 RTvariable camera_ray_type_RTvariable;
334 RTvariable pixel_label_ray_type_RTvariable;
335 RTvariable random_seed_RTvariable;
336 RTvariable launch_offset_RTvariable;
337 RTvariable launch_face_RTvariable;
338 RTvariable Nprimitives_RTvariable;
339 RTvariable bbox_UUID_base_RTvariable;
340 RTvariable Nsources_RTvariable;
341 RTvariable Nbands_global_RTvariable;
342 RTvariable Nbands_launch_RTvariable;
343 RTvariable Ncameras_RTvariable;
344 RTvariable periodic_flag_RTvariable;
345 RTvariable sun_direction_RTvariable;
346 RTvariable solar_disk_cos_angle_RTvariable;
347 RTvariable camera_position_RTvariable;
348 RTvariable camera_direction_RTvariable;
349 RTvariable camera_lens_diameter_RTvariable;
350 RTvariable camera_focal_length_RTvariable;
351 RTvariable FOV_aspect_ratio_RTvariable;
352 RTvariable camera_HFOV_RTvariable;
353 RTvariable camera_resolution_RTvariable;
354 RTvariable camera_viewplane_length_RTvariable;
355 RTvariable camera_pixel_solid_angle_RTvariable;
356 RTvariable camera_pixel_offset_x_RTvariable;
357 RTvariable camera_pixel_offset_y_RTvariable;
358 RTvariable camera_ID_RTvariable;
359 RTvariable camera_resolution_full_RTvariable;
360 RTvariable specular_reflection_enabled_RTvariable;
361 RTvariable scattering_iteration_RTvariable;
362
363 // Helper methods for buffer management (will be moved from RadiationModel)
364 void addBuffer(const char *name, RTbuffer &buffer, RTvariable &variable, RTbuffertype type, RTformat format, size_t dimension);
365 void zeroBuffer1D(RTbuffer &buffer, size_t bsize);
366 void zeroBuffer2D(RTbuffer &buffer, const helios::int2 &bsize);
367 void initializeBuffer1Df(RTbuffer &buffer, const std::vector<float> &array);
368 void initializeBuffer1Dui(RTbuffer &buffer, const std::vector<uint> &array);
369 void initializeBuffer1Di(RTbuffer &buffer, const std::vector<int> &array);
370 void initializeBuffer1Dchar(RTbuffer &buffer, const std::vector<char> &array);
371 void initializeBuffer1Dbool(RTbuffer &buffer, const std::vector<bool> &array);
372 void initializeBuffer1Dfloat2(RTbuffer &buffer, const std::vector<helios::vec2> &array);
373 void initializeBuffer1Dfloat3(RTbuffer &buffer, const std::vector<helios::vec3> &array);
374 void initializeBuffer1Dfloat4(RTbuffer &buffer, const std::vector<helios::vec4> &array);
375 void initializeBuffer1Dint2(RTbuffer &buffer, const std::vector<helios::int2> &array);
376 void initializeBuffer2Df(RTbuffer &buffer, const std::vector<std::vector<float>> &array);
377 void initializeBuffer2Dui(RTbuffer &buffer, const std::vector<std::vector<uint>> &array);
378 void initializeBuffer2Di(RTbuffer &buffer, const std::vector<std::vector<int>> &array);
379 void initializeBuffer2Dfloat2(RTbuffer &buffer, const std::vector<std::vector<helios::vec2>> &array);
380 void initializeBuffer2Dfloat3(RTbuffer &buffer, const std::vector<std::vector<helios::vec3>> &array);
381 void initializeBuffer2Dfloat3(RTbuffer &buffer, const std::vector<std::vector<optix::float3>> &array);
382 void initializeBuffer3Dbool(RTbuffer &buffer, const std::vector<std::vector<std::vector<bool>>> &array);
383 void copyBuffer1D(RTbuffer &source, RTbuffer &dest);
384 std::vector<float> getOptiXbufferData(RTbuffer buffer);
385 std::vector<uint> getOptiXbufferData_ui(RTbuffer buffer);
386
387 // Conversion methods: backend-agnostic → OptiX
388 void geometryToBuffers(const RayTracingGeometry &geometry);
389 void materialsToBuffers(const RayTracingMaterial &materials);
390 void sourcesToBuffers(const std::vector<RayTracingSource> &sources);
391 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);
392 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,
393 const std::vector<float> &camera_diffuse_flux, const std::vector<uint32_t> &band_emission_flag);
394 void launchParamsToVariables(const RayTracingLaunchParams &params);
395
396 // Extraction methods: OptiX → backend-agnostic
397 void buffersToResults(RayTracingResults &results);
398
399 // Internal state tracking
400 bool is_initialized = false;
401 size_t current_primitive_count = 0;
402 size_t current_patch_count = 0;
403 size_t current_triangle_count = 0;
404 size_t current_disk_count = 0;
405 size_t current_tile_count = 0;
406 size_t current_voxel_count = 0;
407 size_t current_bbox_count = 0;
408 size_t current_source_count = 0;
409 size_t current_band_count = 0;
410 size_t current_camera_count = 0;
411 uint32_t current_camera_launch_id = UINT32_MAX;
412 size_t current_launch_band_count = 0;
413 };
414
415} // namespace helios
416
417#endif // OPTIX6_BACKEND_H