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