1.3.77
 
Loading...
Searching...
No Matches
VulkanComputeBackend.h
Go to the documentation of this file.
1
16#ifndef VULKAN_COMPUTE_BACKEND_H
17#define VULKAN_COMPUTE_BACKEND_H
18
19#include "RayTracingBackend.h"
20#include "VulkanDevice.h"
21#include "BVHBuilder.h"
22#include <vulkan/vulkan.h>
23
24// Suppress nullability warnings from VMA header on macOS
25#ifdef __clang__
26#pragma clang diagnostic push
27#pragma clang diagnostic ignored "-Wnullability-completeness"
28#endif
29#include <vk_mem_alloc.h>
30#ifdef __clang__
31#pragma clang diagnostic pop
32#endif
33
34#include <memory>
35#include <unordered_map>
36
37namespace helios {
38
48 public:
50
59 static bool probe() noexcept;
60
70 explicit VulkanComputeBackend(VulkanDevice *external_device);
71
72 ~VulkanComputeBackend() override;
73
74 // ========== Lifecycle Management ==========
75 void initialize() override;
76 void shutdown() override;
77
78 // ========== Geometry Management ==========
79 void updateGeometry(const RayTracingGeometry &geometry) override;
80 void buildAccelerationStructure() override;
81
82 // ========== Material/Optical Properties ==========
83 void updateMaterials(const RayTracingMaterial &materials) override;
84
85 // ========== Radiation Sources ==========
86 void updateSources(const std::vector<RayTracingSource> &sources) override;
87
88 // ========== Diffuse/Sky Radiation ==========
89 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;
90
91 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,
92 const std::vector<float> &camera_diffuse_flux, const std::vector<uint32_t> &band_emission_flag) override;
93
94 // ========== Ray Launching ==========
95 void launchDirectRays(const RayTracingLaunchParams &params) override;
96 void launchDiffuseRays(const RayTracingLaunchParams &params) override;
97 void launchCameraRays(const RayTracingLaunchParams &params) override;
98 void launchPixelLabelRays(const RayTracingLaunchParams &params) override;
99
100 // ========== Results Retrieval ==========
101 void getRadiationResults(RayTracingResults &results) override;
102 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;
103
104 // ========== Buffer Management Utilities ==========
105 void zeroRadiationBuffers(size_t launch_band_count) override;
106 void zeroScatterBuffers() override;
107 void zeroCameraPixelBuffers(const helios::int2 &resolution) override;
108 void copyScatterToRadiation() override;
109 void uploadRadiationOut(const std::vector<float> &radiation_out_top, const std::vector<float> &radiation_out_bottom) override;
110 void uploadCameraScatterBuffers(const std::vector<float> &scatter_top_cam, const std::vector<float> &scatter_bottom_cam) override;
111 void zeroCameraScatterBuffers(size_t launch_band_count) override;
112 void uploadSourceFluxes(const std::vector<float> &fluxes) override;
113 void uploadSourceFluxesCam(const std::vector<float> &fluxes_cam) override;
114
115 // ========== Diagnostics ==========
116 void queryGPUMemory() const override;
117 std::string getBackendName() const override {
118 return "Vulkan Compute (software BVH)";
119 }
120
121 private:
122 // Vulkan device - either owned (production) or borrowed (test shared device)
123 VulkanDevice *device;
124 bool owns_device; // true = we own device, false = borrowed from test singleton
125 BVHBuilder bvh_builder;
126
127 // BVH data
128 std::vector<BVHNode> bvh_nodes;
129
130 // Vulkan buffers (managed by VMA)
131 struct Buffer {
132 VkBuffer buffer = VK_NULL_HANDLE;
133 VmaAllocation allocation = VK_NULL_HANDLE;
134 VkDeviceSize size = 0;
135 };
136
137 // Geometry buffers (Set 0)
138 Buffer bvh_buffer;
139 Buffer primitive_indices_buffer;
140 Buffer transform_matrices_buffer;
141 Buffer primitive_types_buffer;
142 Buffer primitive_uuids_buffer;
143 Buffer primitive_positions_buffer;
144 Buffer object_subdivisions_buffer;
145 Buffer twosided_flag_buffer;
146 Buffer patch_vertices_buffer;
147 Buffer triangle_vertices_buffer;
148 Buffer normal_buffer;
149 Buffer mask_data_buffer;
150 Buffer mask_sizes_buffer;
151 Buffer mask_offsets_buffer;
152 Buffer mask_IDs_buffer;
153 Buffer uv_data_buffer;
154 Buffer uv_IDs_buffer;
155 Buffer bbox_vertices_buffer;
156
157 // Material/Source buffers (Set 1)
158 Buffer source_positions_buffer;
159 Buffer source_types_buffer;
160 Buffer source_rotations_buffer;
161 Buffer source_widths_buffer;
162 Buffer source_fluxes_buffer;
163 Buffer reflectivity_buffer;
164 Buffer transmissivity_buffer;
165 Buffer specular_exponent_buffer;
166 Buffer specular_scale_buffer;
167 Buffer source_fluxes_cam_buffer;
168 Buffer band_map_buffer;
169 Buffer is_glass_buffer;
170 Buffer glass_n_buffer;
171 Buffer glass_KL_buffer;
172
173 // Result buffers (Set 2)
174 Buffer radiation_in_buffer;
175 Buffer radiation_out_top_buffer;
176 Buffer radiation_out_bottom_buffer;
177 Buffer scatter_top_buffer;
178 Buffer scatter_bottom_buffer;
179 Buffer camera_radiation_buffer;
180 Buffer camera_pixel_label_buffer;
181 Buffer camera_pixel_depth_buffer;
182 Buffer camera_scatter_top_buffer;
183 Buffer camera_scatter_bottom_buffer;
184 Buffer radiation_specular_buffer;
185
186 // Sky parameter buffers (Set 3)
187 Buffer diffuse_flux_buffer;
188 Buffer diffuse_peak_dir_buffer;
189 Buffer diffuse_extinction_buffer;
190 Buffer diffuse_dist_norm_buffer;
191 Buffer sky_radiance_params_buffer;
192 Buffer camera_sky_radiance_buffer;
193 Buffer solar_disk_radiance_buffer;
194 Buffer camera_diffuse_flux_buffer;
195 Buffer band_emission_flag_buffer;
196
197 // Debug/profiling buffers (Set 4)
198 Buffer debug_counters_buffer;
199
200 // Descriptor sets
201 VkDescriptorPool descriptor_pool = VK_NULL_HANDLE;
202 VkDescriptorSetLayout set_layout_geometry = VK_NULL_HANDLE;
203 VkDescriptorSetLayout set_layout_materials = VK_NULL_HANDLE;
204 VkDescriptorSetLayout set_layout_results = VK_NULL_HANDLE;
205 VkDescriptorSetLayout set_layout_sky = VK_NULL_HANDLE;
206 VkDescriptorSetLayout set_layout_debug = VK_NULL_HANDLE; // Debug counters
207 VkDescriptorSet set_geometry = VK_NULL_HANDLE;
208 VkDescriptorSet set_materials = VK_NULL_HANDLE;
209 VkDescriptorSet set_results = VK_NULL_HANDLE;
210 VkDescriptorSet set_sky = VK_NULL_HANDLE;
211 VkDescriptorSet set_debug = VK_NULL_HANDLE; // Debug counters
212
213 // Cached sky/sun parameters (for camera push constants)
214 helios::vec3 cached_sun_direction;
215 float cached_solar_disk_cos_angle = 0.0f;
216
217 // Compute pipelines
218 VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
219 VkPipeline pipeline_direct = VK_NULL_HANDLE;
220 VkPipeline pipeline_diffuse = VK_NULL_HANDLE;
221 VkPipeline pipeline_camera = VK_NULL_HANDLE;
222 VkPipeline pipeline_pixel_label = VK_NULL_HANDLE;
223
224 // Command resources - separate for transfer and compute to avoid synchronization issues
225 VkCommandPool command_pool = VK_NULL_HANDLE;
226 VkCommandBuffer transfer_command_buffer = VK_NULL_HANDLE; // For buffer uploads/downloads
227 VkCommandBuffer compute_command_buffer = VK_NULL_HANDLE; // For compute shader dispatches
228 VkFence transfer_fence = VK_NULL_HANDLE; // For synchronizing buffer operations
229 VkFence compute_fence = VK_NULL_HANDLE; // For synchronizing compute operations
230
231 // Timestamp queries for GPU profiling
232 VkQueryPool timestamp_query_pool = VK_NULL_HANDLE;
233 float timestamp_period = 1.0f; // Nanoseconds per timestamp unit
234
235 // Geometry cache
236 size_t primitive_count = 0;
237 size_t band_count = 0; // Global band count (material buffer stride)
238 size_t source_count = 0;
239
240 // Periodic boundary state
241 uint32_t bbox_count = 0;
242 float periodic_flag_x = 0;
243 float periodic_flag_y = 0;
244 float domain_bounds[4] = {};
245
246 // Per-launch band tracking (for radiation I/O buffers)
247 uint32_t launch_band_count = 0; // Current runBand() band count (set by zeroRadiationBuffers)
248 std::vector<uint32_t> launch_to_global_band; // Maps launch band index → global band index
249
250 // Descriptor set update tracking
251 bool descriptors_dirty = false;
252
256 Buffer createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaMemoryUsage mem_usage);
257
261 void destroyBuffer(Buffer &buffer);
262
266 void uploadBufferData(Buffer &buffer, const void *data, size_t size);
267
271 void downloadBufferData(const Buffer &buffer, void *data, size_t size);
272
276 void zeroBuffer(Buffer &buffer);
277
281 void createDescriptorSets();
282
286 void createPipelines();
287
291 VkShaderModule loadShader(const std::string &filename);
292
296 void createCommandResources();
297
301 void updateDescriptorSets();
302 };
303
304} // namespace helios
305
306#endif // VULKAN_COMPUTE_BACKEND_H