MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
StagingUtils.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Buffers {
7
8class VKBuffer;
9class AudioBuffer;
10
11inline constexpr float k_buffer_growth_factor = 1.5F;
12
13/**
14 * @brief Upload data to a host-visible buffer
15 * @param target Target VKBuffer to upload data into
16 * @param data DataVariant containing the data to upload
17 *
18 * This function handles uploading data from a Kakshya::DataVariant into a
19 * host-visible VKBuffer. It maps the buffer memory, copies the data, and
20 * marks the buffer as dirty for synchronization.
21 */
22MAYAFLUX_API void upload_host_visible(const std::shared_ptr<VKBuffer>& target, const Kakshya::DataVariant& data);
23
24/**
25 * @brief Upload data to a device-local buffer using a staging buffer
26 * @param target Target VKBuffer to upload data into
27 * @param staging_buffer Host-visible staging VKBuffer used for the upload
28 * @param data DataVariant containing the data to upload
29 *
30 * This function handles uploading data from a Kakshya::DataVariant into a
31 * device-local VKBuffer by utilizing a staging buffer. It copies the data
32 * into the staging buffer, flushes it, and then issues a command to copy
33 * from the staging buffer to the target device-local buffer.
34 */
35MAYAFLUX_API void upload_device_local(const std::shared_ptr<VKBuffer>& target, const std::shared_ptr<VKBuffer>& staging_buffer, const Kakshya::DataVariant& data);
36
37/**
38 * @brief Download data from a host-visible buffer
39 * @param source Source VKBuffer to download data from
40 * @param target Target VKBuffer to store the downloaded data
41 *
42 * This function handles downloading data from a host-visible VKBuffer.
43 * It maps the buffer memory, copies the data into a CPU-accessible format,
44 * and updates the associated target buffer.
45 */
46MAYAFLUX_API void download_host_visible(const std::shared_ptr<VKBuffer>& source, const std::shared_ptr<VKBuffer>& target);
47
48/**
49 * @brief Download data from a device-local buffer using a staging buffer
50 * @param source Source VKBuffer to download data from
51 * @param target Target VKBuffer to store the downloaded data
52 * @param staging_buffer Host-visible staging VKBuffer used for the download
53 *
54 * This function handles downloading data from a device-local VKBuffer by
55 * utilizing a staging buffer. It issues a command to copy the data from
56 * the device-local buffer to the staging buffer, invalidates the staging
57 * buffer memory, and then copies the data into a CPU-accessible format
58 * to update the target buffer.
59 */
60MAYAFLUX_API void download_device_local(const std::shared_ptr<VKBuffer>& source, const std::shared_ptr<VKBuffer>& target, const std::shared_ptr<VKBuffer>& staging_buffer);
61
62/**
63 * @brief Upload raw data to GPU buffer (auto-detects host-visible vs device-local)
64 * @param data Source data pointer
65 * @param size Size in bytes
66 * @param target Target GPU buffer
67 * @param staging Optional staging buffer (created if needed for device-local)
68 *
69 * Convenience wrapper over StagingUtils that:
70 * - Converts raw pointer → DataVariant
71 * - Auto-detects if buffer is host-visible or device-local
72 * - Handles staging buffer creation if needed
73 */
74MAYAFLUX_API void upload_to_gpu(
75 const void* data,
76 size_t size,
77 const std::shared_ptr<VKBuffer>& target,
78 const std::shared_ptr<VKBuffer>& staging = nullptr);
79
80/**
81 * @brief Upload typed data to GPU buffer
82 * @tparam T Data type (float, double, int, etc.)
83 * @param data Source data span
84 * @param target Target GPU buffer
85 * @param staging Optional staging buffer
86 */
87template <typename T>
89 std::span<const T> data,
90 const std::shared_ptr<VKBuffer>& target,
91 const std::shared_ptr<VKBuffer>& staging = nullptr)
92{
93 upload_to_gpu(data.data(), data.size_bytes(), target, staging);
94}
95
96/**
97 * @brief Upload vector to GPU buffer
98 * @tparam T Data type
99 * @param data Source data vector
100 * @param target Target GPU buffer
101 * @param staging Optional staging buffer
102 */
103template <typename T>
105 const std::vector<T>& data,
106 const std::shared_ptr<VKBuffer>& target,
107 const std::shared_ptr<VKBuffer>& staging = nullptr)
108{
109 upload_to_gpu(std::span<const T>(data), target, staging);
110}
111
112/**
113 * @brief Upload @p size bytes to @p target, growing both buffers first if needed.
114 * @param data Source pointer.
115 * @param size Byte count to upload.
116 * @param target Destination GPU buffer.
117 * @param staging Paired staging buffer, or nullptr for host-visible targets.
118 * @param growth_factor Forwarded to ensure_gpu_capacity.
119 *
120 * Combines ensure_gpu_capacity() and upload_to_gpu() into a single call.
121 * The upload is clamped to the post-resize capacity, which is always >= @p size
122 * after ensure_gpu_capacity returns.
123 */
124MAYAFLUX_API void upload_resizing(
125 const void* data,
126 size_t size,
127 const std::shared_ptr<VKBuffer>& target,
128 const std::shared_ptr<VKBuffer>& staging,
129 float growth_factor = 1.5F);
130
131/**
132 * @brief Download from GPU buffer to raw data (auto-detects host-visible vs device-local)
133 * @param source Source GPU buffer
134 * @param data Destination data pointer
135 * @param size Size in bytes
136 * @param staging Optional staging buffer (created if needed for device-local)
137 *
138 * Convenience wrapper over StagingUtils that:
139 * - Auto-detects if buffer is host-visible or device-local
140 * - Handles staging buffer creation if needed
141 * - Copies data to destination pointer
142 */
143MAYAFLUX_API void download_from_gpu(
144 const std::shared_ptr<VKBuffer>& source,
145 void* data,
146 size_t size,
147 const std::shared_ptr<VKBuffer>& staging = nullptr);
148
149/**
150 * @brief Download from GPU buffer to typed span
151 * @tparam T Data type
152 * @param source Source GPU buffer
153 * @param data Destination data span
154 * @param staging Optional staging buffer
155 */
156template <typename T>
158 const std::shared_ptr<VKBuffer>& source,
159 std::span<T> data,
160 const std::shared_ptr<VKBuffer>& staging = nullptr)
161{
162 download_from_gpu(source, data.data(), data.size_bytes(), staging);
163}
164
165/**
166 * @brief Download from GPU buffer to vector
167 * @tparam T Data type
168 * @param source Source GPU buffer
169 * @param data Destination data vector (resized if needed)
170 * @param staging Optional staging buffer
171 */
172template <typename T>
174 const std::shared_ptr<VKBuffer>& source,
175 std::vector<T>& data,
176 const std::shared_ptr<VKBuffer>& staging = nullptr)
177{
178 size_t element_count = source->get_size_bytes() / sizeof(T);
179 data.resize(element_count);
180 download_from_gpu(source, std::span<T>(data), staging);
181}
182
183/**
184 * @brief Download from a device-local GPU buffer without stalling the graphics queue.
185 *
186 * Records a buffer copy into a fenced command buffer, waits on the fence from
187 * the calling thread, then memcpys from the mapped staging buffer into @p data.
188 * Unlike download_from_gpu, this does not call queue.waitIdle, making it safe
189 * to call from any thread that is not the graphics thread itself.
190 *
191 * @p staging is allocated and cached by the caller to avoid per-call Vulkan
192 * object churn. Pass the same staging buffer each frame; it is resized if
193 * @p size exceeds its current capacity.
194 *
195 * @param source Device-local source buffer.
196 * @param data Destination host pointer, at least @p size bytes.
197 * @param size Byte count to copy.
198 * @param staging Persistent host-visible staging buffer. Created via
199 * create_staging_buffer(). Resized in-place if too small.
200 */
201MAYAFLUX_API void download_from_gpu_async(
202 const std::shared_ptr<VKBuffer>& source,
203 void* data,
204 size_t size,
205 std::shared_ptr<VKBuffer>& staging);
206
207/**
208 * @brief Create staging buffer for transfers
209 * @param size Size in bytes
210 * @return Host-visible staging buffer ready for transfers
211 */
212MAYAFLUX_API std::shared_ptr<VKBuffer> create_staging_buffer(size_t size);
213
214/**
215 * @brief Allocate a persistent host-visible staging buffer sized for
216 * repeated streaming uploads to an image of @p size bytes.
217 * Call once; pass the result to upload_image_streaming() every frame.
218 * @param size Byte footprint of the target image (use VKImage::get_size_bytes()).
219 * @return Initialised host-visible VKBuffer, or nullptr on failure.
220 */
221[[nodiscard]] MAYAFLUX_API std::shared_ptr<VKBuffer> create_image_staging_buffer(size_t size);
222
223/**
224 * @brief Check if buffer is device-local (staging needed)
225 * @param buffer Buffer to check
226 * @return True if buffer is device-local
227 */
228MAYAFLUX_API bool is_device_local(const std::shared_ptr<VKBuffer>& buffer);
229
230/**
231 * @brief Grow a GPU buffer (and its paired staging buffer) to fit @p required bytes.
232 * @param target Device-local or host-visible GPU buffer to resize.
233 * @param staging Paired staging buffer, or nullptr if the target is host-visible.
234 * @param required Bytes needed for the next upload.
235 * @param growth_factor Multiplier applied when a resize is triggered (default 1.5).
236 *
237 * No-op when @p target already has sufficient capacity. When a resize is necessary
238 * both buffers are grown to `required * growth_factor` so subsequent small
239 * increments do not trigger further allocations. Existing GPU data is not preserved
240 * (preserve_data = false) because the caller is about to overwrite it.
241 */
242MAYAFLUX_API void ensure_gpu_capacity(
243 const std::shared_ptr<VKBuffer>& target,
244 const std::shared_ptr<VKBuffer>& staging,
245 size_t required,
246 float growth_factor = k_buffer_growth_factor);
247
248/**
249 * @brief Upload data from DataAccess view to GPU buffer (precision-preserving)
250 * @tparam T View type (double, glm::dvec2, glm::dvec3, glm::vec3, float, etc.)
251 * @param accessor DataAccess instance providing the view
252 * @param target Target GPU buffer
253 * @param staging Optional staging buffer (auto-created if needed)
254 *
255 * Zero-copy when types match, automatic conversion cache when they don't.
256 * For AUDIO modalities, defaults to DOUBLE precision to preserve accuracy.
257 */
258template <typename T>
260 const Kakshya::DataAccess& accessor,
261 const std::shared_ptr<VKBuffer>& target,
262 const std::shared_ptr<VKBuffer>& staging = nullptr)
263{
264 auto view = accessor.view<T>();
265
266 const void* data_ptr = view.data();
267 size_t data_bytes = view.size() * sizeof(T);
268
269 if constexpr (std::is_same_v<T, double>) {
270 const auto modality = target->get_modality();
271 if (modality != Kakshya::DataModality::AUDIO_1D
274 "Uploading double precision to buffer with modality {}. Consider using AUDIO_1D or AUDIO_MULTICHANNEL.",
276 }
277 }
278
279 upload_to_gpu(data_ptr, data_bytes, target, staging);
280}
281
282/**
283 * @brief Upload structured data with GLM double-precision types
284 * @tparam T GLM type (glm::dvec2, glm::dvec3, glm::dvec4 for double precision)
285 * @param accessor DataAccess with structured dimensions
286 * @param target Target GPU buffer
287 * @param staging Optional staging buffer
288 *
289 * Use this for high-precision structured data like audio samples interpreted
290 * as multi-dimensional vectors. Supports both single and double precision GLM types.
291 */
292template <typename T>
293 requires GlmType<T>
295 const Kakshya::DataAccess& accessor,
296 const std::shared_ptr<VKBuffer>& target,
297 const std::shared_ptr<VKBuffer>& staging = nullptr)
298{
299 if (!accessor.is_structured()) {
300 error<std::invalid_argument>(
303 std::source_location::current(),
304 "Cannot upload structured view from non-structured data");
305 }
306
307 auto structured_view = accessor.view<T>();
308 upload_to_gpu(structured_view.data(), structured_view.size_bytes(), target, staging);
309}
310
311/**
312 * @brief Download GPU buffer to DataAccess-compatible format (precision-preserving)
313 */
314template <typename T>
316 const std::shared_ptr<VKBuffer>& source,
317 Kakshya::DataVariant& target_variant,
318 const std::vector<Kakshya::DataDimension>& dimensions,
319 Kakshya::DataModality modality,
320 const std::shared_ptr<VKBuffer>& staging = nullptr)
321{
322 size_t element_count = source->get_size_bytes() / sizeof(T);
323
324 std::vector<T> temp_buffer(element_count);
325 download_from_gpu(source, temp_buffer, staging);
326
327 target_variant = std::move(temp_buffer);
328
329 return Kakshya::DataAccess(target_variant, dimensions, modality);
330}
331
332/**
333 * @brief Upload AudioBuffer to GPU (always double precision)
334 * @param audio_buffer Source audio buffer (double[])
335 * @param gpu_buffer Target GPU buffer (must support R64Sfloat format)
336 * @param staging Optional staging buffer (auto-created if needed)
337 *
338 * AudioBuffer is always double precision. This function ensures the GPU buffer
339 * is configured for double precision and performs a direct upload with no conversion.
340 *
341 * @throws std::runtime_error if gpu_buffer doesn't support double precision
342 */
344 const std::shared_ptr<AudioBuffer>& audio_buffer,
345 const std::shared_ptr<VKBuffer>& gpu_buffer,
346 const std::shared_ptr<VKBuffer>& staging = nullptr);
347
348/**
349 * @brief Download GPU buffer to AudioBuffer (expects double precision)
350 * @param gpu_buffer Source GPU buffer (should contain double precision data)
351 * @param audio_buffer Target audio buffer (always double[])
352 * @param staging Optional staging buffer (auto-created if needed)
353 *
354 * Downloads GPU data and copies to AudioBuffer. If the GPU buffer contains
355 * float data instead of double, DataAccess will handle the upconversion
356 * (though this is not recommended for audio precision).
357 */
359 const std::shared_ptr<VKBuffer>& gpu_buffer,
360 const std::shared_ptr<AudioBuffer>& audio_buffer,
361 const std::shared_ptr<VKBuffer>& staging = nullptr);
362
363} // namespace MayaFlux::Buffers
#define MF_WARN(comp, ctx,...)
auto view() const
Get explicit typed view of data.
Type-erased accessor for NDData with semantic view construction.
void upload_host_visible(const std::shared_ptr< VKBuffer > &target, const Kakshya::DataVariant &data)
Upload data to a host-visible buffer.
std::shared_ptr< VKBuffer > create_image_staging_buffer(size_t size)
Allocate a persistent host-visible staging buffer sized for repeated streaming uploads to an image of...
void upload_audio_to_gpu(const std::shared_ptr< AudioBuffer > &audio_buffer, const std::shared_ptr< VKBuffer > &gpu_buffer, const std::shared_ptr< VKBuffer > &staging)
Upload AudioBuffer to GPU (always double precision)
void upload_structured_view(const Kakshya::DataAccess &accessor, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging=nullptr)
Upload structured data with GLM double-precision types.
void upload_resizing(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging, float growth_factor)
Upload size bytes to target, growing both buffers first if needed.
void upload_device_local(const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging_buffer, const Kakshya::DataVariant &data)
Upload data to a device-local buffer using a staging buffer.
constexpr float k_buffer_growth_factor
void download_from_gpu_async(const std::shared_ptr< VKBuffer > &source, void *data, size_t size, std::shared_ptr< VKBuffer > &staging)
Download from a device-local GPU buffer without stalling the graphics queue.
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
bool is_device_local(const std::shared_ptr< VKBuffer > &buffer)
Check if buffer is device-local (staging needed)
void download_audio_from_gpu(const std::shared_ptr< VKBuffer > &gpu_buffer, const std::shared_ptr< AudioBuffer > &audio_buffer, const std::shared_ptr< VKBuffer > &staging)
Download GPU buffer to AudioBuffer (expects double precision)
void upload_from_view(const Kakshya::DataAccess &accessor, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging=nullptr)
Upload data from DataAccess view to GPU buffer (precision-preserving)
void download_from_gpu(const std::shared_ptr< VKBuffer > &source, void *data, size_t size, const std::shared_ptr< VKBuffer > &staging)
Download from GPU buffer to raw data (auto-detects host-visible vs device-local)
void download_host_visible(const std::shared_ptr< VKBuffer > &source, const std::shared_ptr< VKBuffer > &target)
Download data from a host-visible buffer.
void ensure_gpu_capacity(const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging, size_t required, float growth_factor)
Grow a GPU buffer (and its paired staging buffer) to fit required bytes.
void download_device_local(const std::shared_ptr< VKBuffer > &source, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging_buffer)
Download data from a device-local buffer using a staging buffer.
Kakshya::DataAccess download_to_view(const std::shared_ptr< VKBuffer > &source, Kakshya::DataVariant &target_variant, const std::vector< Kakshya::DataDimension > &dimensions, Kakshya::DataModality modality, const std::shared_ptr< VKBuffer > &staging=nullptr)
Download GPU buffer to DataAccess-compatible format (precision-preserving)
void upload_to_gpu(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging)
Upload raw data to GPU buffer (auto-detects host-visible vs device-local)
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
Definition NDData.hpp:76
DataModality
Data modality types for cross-modal analysis.
Definition NDData.hpp:81
@ AUDIO_MULTICHANNEL
Multi-channel audio.
std::string_view modality_to_string(DataModality modality)
Convert DataModality enum to string representation.
Definition NDData.cpp:83