1.3.77
 
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,
111 const std::vector<float> &camera_diffuse_flux,
112 const std::vector<uint32_t> &band_emission_flag) override;
113
114 // Ray launching
115 void launchDirectRays(const RayTracingLaunchParams &params) override;
116 void launchDiffuseRays(const RayTracingLaunchParams &params) override;
117 void launchCameraRays(const RayTracingLaunchParams &params) override;
118 void launchPixelLabelRays(const RayTracingLaunchParams &params) override;
119
120 // Results retrieval
121 void getRadiationResults(RayTracingResults &results) override;
122 void getCameraResults(std::vector<float> &pixel_data, std::vector<uint> &pixel_labels,
123 std::vector<float> &pixel_depths, uint camera_id,
124 const helios::int2 &resolution) override;
125
126 // Buffer utilities
127 void zeroRadiationBuffers(size_t launch_band_count) override;
128 void zeroScatterBuffers() override;
129 void zeroCameraPixelBuffers(const helios::int2 &resolution) override;
130 void copyScatterToRadiation() override;
131 void uploadRadiationOut(const std::vector<float> &radiation_out_top,
132 const std::vector<float> &radiation_out_bottom) override;
133 void uploadCameraScatterBuffers(const std::vector<float> &scatter_top_cam,
134 const std::vector<float> &scatter_bottom_cam) override;
135 void zeroCameraScatterBuffers(size_t launch_band_count) override;
136 void uploadSourceFluxes(const std::vector<float> &fluxes) override;
137 void uploadSourceFluxesCam(const std::vector<float> &fluxes_cam) override;
138
139 // Diagnostics
140 void queryGPUMemory() const override;
141 std::string getBackendName() const override { return "OptiX 8.1"; }
142
143 private:
144 // ---- OptiX pipeline objects ----
145 OptixDeviceContext optix_context = nullptr;
146 OptixModule optix_module = nullptr;
147 OptixPipeline optix_pipeline = nullptr;
148 CUstream cuda_stream = nullptr;
149
150 // Program groups (4 raygen + 4 miss + 4 closest-hit + 6 intersection)
151 OptixProgramGroup pg_raygen_direct = nullptr;
152 OptixProgramGroup pg_raygen_diffuse = nullptr;
153 OptixProgramGroup pg_raygen_camera = nullptr;
154 OptixProgramGroup pg_raygen_pixel_label = nullptr;
155
156 OptixProgramGroup pg_miss_direct = nullptr;
157 OptixProgramGroup pg_miss_diffuse = nullptr;
158 OptixProgramGroup pg_miss_camera = nullptr;
159 OptixProgramGroup pg_miss_pixel_label = nullptr;
160
161 OptixProgramGroup pg_hit_direct = nullptr;
162 OptixProgramGroup pg_hit_diffuse = nullptr;
163 OptixProgramGroup pg_hit_camera = nullptr;
164 OptixProgramGroup pg_hit_pixel_label = nullptr;
165
166 // ---- GAS / acceleration structure ----
167 OptixTraversableHandle gas_handle = 0;
168 CUdeviceptr d_gas_output = 0;
169
170 // ---- SBT ----
171 OptixShaderBindingTable sbt = {};
172 CUdeviceptr d_raygen_records = 0;
173 CUdeviceptr d_miss_records = 0;
174 CUdeviceptr d_hitgroup_records= 0;
175
176 // Per-raygen device pointers (byte offsets into d_raygen_records; set in buildSBT)
177 CUdeviceptr d_raygen_record_direct = 0;
178 CUdeviceptr d_raygen_record_diffuse = 0;
179 CUdeviceptr d_raygen_record_camera = 0;
180 CUdeviceptr d_raygen_record_pixel_label = 0;
181
182 // ---- Launch params (device copy) ----
183 OptiX8LaunchParams h_params = {};
184 CUdeviceptr d_params = 0;
185
186 // ---- Geometry device buffers ----
187 CUdeviceptr d_transform_matrix = 0;
188 CUdeviceptr d_primitive_type = 0;
189 CUdeviceptr d_primitive_positions = 0;
190 CUdeviceptr d_primitiveID = 0;
191 CUdeviceptr d_objectID = 0;
192 CUdeviceptr d_object_subdivisions = 0;
193 CUdeviceptr d_twosided_flag = 0;
194 CUdeviceptr d_primitive_solid_fraction = 0;
195
196 // Per-type geometry
197 CUdeviceptr d_patch_vertices = 0;
198 CUdeviceptr d_patch_UUIDs = 0;
199 CUdeviceptr d_triangle_vertices = 0;
200 CUdeviceptr d_triangle_UUIDs = 0;
201 CUdeviceptr d_disk_centers = 0;
202 CUdeviceptr d_disk_radii = 0;
203 CUdeviceptr d_disk_normals = 0;
204 CUdeviceptr d_disk_UUIDs = 0;
205 CUdeviceptr d_tile_vertices = 0;
206 CUdeviceptr d_tile_UUIDs = 0;
207 CUdeviceptr d_voxel_vertices = 0;
208 CUdeviceptr d_voxel_UUIDs = 0;
209 CUdeviceptr d_bbox_vertices = 0;
210 CUdeviceptr d_bbox_UUIDs = 0;
211
212 // Global UUID array: d_primitive_uuid[global_pos] = UUID
213 CUdeviceptr d_primitive_uuid_arr = 0;
214
215 // AABB buffer for GAS build (one AABB per primitive)
216 CUdeviceptr d_aabbs = 0;
217
218 // ---- Material device buffers ----
219 CUdeviceptr d_rho = 0;
220 CUdeviceptr d_tau = 0;
221 CUdeviceptr d_rho_cam = 0;
222 CUdeviceptr d_tau_cam = 0;
223 CUdeviceptr d_specular_exponent = 0;
224 CUdeviceptr d_specular_scale = 0;
225 CUdeviceptr d_glass_n = 0;
226 CUdeviceptr d_glass_KL = 0;
227 CUdeviceptr d_is_glass = 0;
228
229 // ---- Radiation energy device buffers ----
230 CUdeviceptr d_radiation_in = 0;
231 CUdeviceptr d_radiation_out_top = 0;
232 CUdeviceptr d_radiation_out_bottom = 0;
233 CUdeviceptr d_scatter_buff_top = 0;
234 CUdeviceptr d_scatter_buff_bottom = 0;
235 CUdeviceptr d_radiation_in_camera = 0;
236 CUdeviceptr d_scatter_buff_top_cam = 0;
237 CUdeviceptr d_scatter_buff_bottom_cam= 0;
238 CUdeviceptr d_radiation_specular = 0;
239 CUdeviceptr d_Rsky = 0;
240
241 // ---- Camera device buffers ----
242 CUdeviceptr d_camera_pixel_label = 0;
243 CUdeviceptr d_camera_pixel_depth = 0;
244
245 // ---- Source device buffers ----
246 CUdeviceptr d_source_positions = 0;
247 CUdeviceptr d_source_rotations = 0;
248 CUdeviceptr d_source_widths = 0;
249 CUdeviceptr d_source_types = 0;
250 CUdeviceptr d_source_fluxes = 0;
251 CUdeviceptr d_source_fluxes_cam = 0;
252
253 // ---- Diffuse/sky device buffers ----
254 CUdeviceptr d_diffuse_flux = 0;
255 CUdeviceptr d_diffuse_extinction = 0;
256 CUdeviceptr d_diffuse_peak_dir = 0;
257 CUdeviceptr d_diffuse_dist_norm = 0;
258 CUdeviceptr d_sky_radiance_params = 0;
259 CUdeviceptr d_camera_sky_radiance = 0;
260 CUdeviceptr d_solar_disk_radiance = 0;
261 CUdeviceptr d_camera_diffuse_flux = 0;
262 CUdeviceptr d_band_emission_flag = 0;
263
264 // ---- Band launch flag buffer ----
265 CUdeviceptr d_band_launch_flag = 0;
266
267 // ---- Texture/mask device buffers ----
268 CUdeviceptr d_mask_data = 0;
269 CUdeviceptr d_mask_offsets = 0;
270 CUdeviceptr d_mask_sizes = 0;
271 CUdeviceptr d_mask_IDs = 0;
272 CUdeviceptr d_uv_data = 0;
273 CUdeviceptr d_uv_IDs = 0;
274
275 // ---- State ----
276 bool is_initialized = false;
277 size_t current_primitive_count = 0;
278 size_t current_patch_count = 0;
279 size_t current_triangle_count = 0;
280 size_t current_disk_count = 0;
281 size_t current_tile_count = 0;
282 size_t current_voxel_count = 0;
283 size_t current_bbox_count = 0;
284 size_t current_source_count = 0;
285 size_t current_band_count = 0;
286 size_t current_camera_count = 0;
287 size_t current_launch_band_count = 0;
288 uint32_t current_camera_launch_id = 0xFFFFFFFFu;
289
290 // ---- Private helper methods ----
291
293 void freeCUdeviceptr(CUdeviceptr &ptr);
294
296 void freeGeometryBuffers();
297
299 void freeMaterialBuffers();
300
302 void buildAABBs(const RayTracingGeometry &geometry);
303
305 void buildGAS(uint32_t Nprimitives);
306
308 void buildSBT();
309
311 void uploadLaunchParams();
312
314 void applyLaunchParams(const RayTracingLaunchParams &params);
315
317 void reallocDevice(CUdeviceptr &ptr, size_t bytes);
318
320 std::vector<float> downloadFloat(CUdeviceptr ptr, size_t count) const;
321
323 std::vector<uint32_t> downloadUInt32(CUdeviceptr ptr, size_t count) const;
324
326 std::string findDeviceCodeFile() const;
327 };
328
329} // namespace helios
330
331#endif // OPTIX8_BACKEND_H