1.3.72
 
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) override;
92
93 // ========== Ray Launching ==========
94 void launchDirectRays(const RayTracingLaunchParams &params) override;
95 void launchDiffuseRays(const RayTracingLaunchParams &params) override;
96 void launchCameraRays(const RayTracingLaunchParams &params) override;
97 void launchPixelLabelRays(const RayTracingLaunchParams &params) override;
98
99 // ========== Results Retrieval ==========
100 void getRadiationResults(RayTracingResults &results) override;
101 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;
102
103 // ========== Buffer Management Utilities ==========
104 void zeroRadiationBuffers(size_t launch_band_count) override;
105 void zeroScatterBuffers() override;
106 void zeroCameraPixelBuffers(const helios::int2 &resolution) override;
107 void copyScatterToRadiation() override;
108 void uploadRadiationOut(const std::vector<float> &radiation_out_top, const std::vector<float> &radiation_out_bottom) override;
109 void uploadCameraScatterBuffers(const std::vector<float> &scatter_top_cam, const std::vector<float> &scatter_bottom_cam) override;
110 void zeroCameraScatterBuffers(size_t launch_band_count) override;
111 void uploadSourceFluxes(const std::vector<float> &fluxes) override;
112 void uploadSourceFluxesCam(const std::vector<float> &fluxes_cam) override;
113
114 // ========== Diagnostics ==========
115 void queryGPUMemory() const override;
116 std::string getBackendName() const override {
117 return "Vulkan Compute (software BVH)";
118 }
119
120 private:
121 // Vulkan device - either owned (production) or borrowed (test shared device)
122 VulkanDevice *device;
123 bool owns_device; // true = we own device, false = borrowed from test singleton
124 BVHBuilder bvh_builder;
125
126 // BVH data
127 std::vector<BVHNode> bvh_nodes;
128
129 // Vulkan buffers (managed by VMA)
130 struct Buffer {
131 VkBuffer buffer = VK_NULL_HANDLE;
132 VmaAllocation allocation = VK_NULL_HANDLE;
133 VkDeviceSize size = 0;
134 };
135
136 // Geometry buffers (Set 0)
137 Buffer bvh_buffer;
138 Buffer primitive_indices_buffer;
139 Buffer transform_matrices_buffer;
140 Buffer primitive_types_buffer;
141 Buffer primitive_uuids_buffer;
142 Buffer primitive_positions_buffer;
143 Buffer object_subdivisions_buffer;
144 Buffer twosided_flag_buffer;
145 Buffer patch_vertices_buffer;
146 Buffer triangle_vertices_buffer;
147 Buffer normal_buffer;
148 Buffer mask_data_buffer;
149 Buffer mask_sizes_buffer;
150 Buffer mask_offsets_buffer;
151 Buffer mask_IDs_buffer;
152 Buffer uv_data_buffer;
153 Buffer uv_IDs_buffer;
154 Buffer bbox_vertices_buffer;
155
156 // Material/Source buffers (Set 1)
157 Buffer source_positions_buffer;
158 Buffer source_types_buffer;
159 Buffer source_rotations_buffer;
160 Buffer source_widths_buffer;
161 Buffer source_fluxes_buffer;
162 Buffer reflectivity_buffer;
163 Buffer transmissivity_buffer;
164 Buffer specular_exponent_buffer;
165 Buffer specular_scale_buffer;
166 Buffer source_fluxes_cam_buffer;
167 Buffer band_map_buffer;
168
169 // Result buffers (Set 2)
170 Buffer radiation_in_buffer;
171 Buffer radiation_out_top_buffer;
172 Buffer radiation_out_bottom_buffer;
173 Buffer scatter_top_buffer;
174 Buffer scatter_bottom_buffer;
175 Buffer camera_radiation_buffer;
176 Buffer camera_pixel_label_buffer;
177 Buffer camera_pixel_depth_buffer;
178 Buffer camera_scatter_top_buffer;
179 Buffer camera_scatter_bottom_buffer;
180 Buffer radiation_specular_buffer;
181
182 // Sky parameter buffers (Set 3)
183 Buffer diffuse_flux_buffer;
184 Buffer diffuse_peak_dir_buffer;
185 Buffer diffuse_extinction_buffer;
186 Buffer diffuse_dist_norm_buffer;
187 Buffer sky_radiance_params_buffer;
188 Buffer camera_sky_radiance_buffer;
189 Buffer solar_disk_radiance_buffer;
190
191 // Debug/profiling buffers (Set 4)
192 Buffer debug_counters_buffer;
193
194 // Descriptor sets
195 VkDescriptorPool descriptor_pool = VK_NULL_HANDLE;
196 VkDescriptorSetLayout set_layout_geometry = VK_NULL_HANDLE;
197 VkDescriptorSetLayout set_layout_materials = VK_NULL_HANDLE;
198 VkDescriptorSetLayout set_layout_results = VK_NULL_HANDLE;
199 VkDescriptorSetLayout set_layout_sky = VK_NULL_HANDLE;
200 VkDescriptorSetLayout set_layout_debug = VK_NULL_HANDLE; // Debug counters
201 VkDescriptorSet set_geometry = VK_NULL_HANDLE;
202 VkDescriptorSet set_materials = VK_NULL_HANDLE;
203 VkDescriptorSet set_results = VK_NULL_HANDLE;
204 VkDescriptorSet set_sky = VK_NULL_HANDLE;
205 VkDescriptorSet set_debug = VK_NULL_HANDLE; // Debug counters
206
207 // Cached sky/sun parameters (for camera push constants)
208 helios::vec3 cached_sun_direction;
209 float cached_solar_disk_cos_angle = 0.0f;
210
211 // Compute pipelines
212 VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
213 VkPipeline pipeline_direct = VK_NULL_HANDLE;
214 VkPipeline pipeline_diffuse = VK_NULL_HANDLE;
215 VkPipeline pipeline_camera = VK_NULL_HANDLE;
216 VkPipeline pipeline_pixel_label = VK_NULL_HANDLE;
217
218 // Command resources - separate for transfer and compute to avoid synchronization issues
219 VkCommandPool command_pool = VK_NULL_HANDLE;
220 VkCommandBuffer transfer_command_buffer = VK_NULL_HANDLE; // For buffer uploads/downloads
221 VkCommandBuffer compute_command_buffer = VK_NULL_HANDLE; // For compute shader dispatches
222 VkFence transfer_fence = VK_NULL_HANDLE; // For synchronizing buffer operations
223 VkFence compute_fence = VK_NULL_HANDLE; // For synchronizing compute operations
224
225 // Timestamp queries for GPU profiling
226 VkQueryPool timestamp_query_pool = VK_NULL_HANDLE;
227 float timestamp_period = 1.0f; // Nanoseconds per timestamp unit
228
229 // Geometry cache
230 size_t primitive_count = 0;
231 size_t band_count = 0; // Global band count (material buffer stride)
232 size_t source_count = 0;
233
234 // Periodic boundary state
235 uint32_t bbox_count = 0;
236 float periodic_flag_x = 0;
237 float periodic_flag_y = 0;
238 float domain_bounds[4] = {};
239
240 // Per-launch band tracking (for radiation I/O buffers)
241 uint32_t launch_band_count = 0; // Current runBand() band count (set by zeroRadiationBuffers)
242 std::vector<uint32_t> launch_to_global_band; // Maps launch band index → global band index
243
244 // Descriptor set update tracking
245 bool descriptors_dirty = false;
246
250 Buffer createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaMemoryUsage mem_usage);
251
255 void destroyBuffer(Buffer &buffer);
256
260 void uploadBufferData(Buffer &buffer, const void *data, size_t size);
261
265 void downloadBufferData(const Buffer &buffer, void *data, size_t size);
266
270 void zeroBuffer(Buffer &buffer);
271
275 void createDescriptorSets();
276
280 void createPipelines();
281
285 VkShaderModule loadShader(const std::string &filename);
286
290 void createCommandResources();
291
295 void updateDescriptorSets();
296 };
297
298} // namespace helios
299
300#endif // VULKAN_COMPUTE_BACKEND_H