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 Create staging buffer for transfers
185 * @param size Size in bytes
186 * @return Host-visible staging buffer ready for transfers
187 */
188MAYAFLUX_API std::shared_ptr<VKBuffer> create_staging_buffer(size_t size);
189
190/**
191 * @brief Allocate a persistent host-visible staging buffer sized for
192 * repeated streaming uploads to an image of @p size bytes.
193 * Call once; pass the result to upload_image_streaming() every frame.
194 * @param size Byte footprint of the target image (use VKImage::get_size_bytes()).
195 * @return Initialised host-visible VKBuffer, or nullptr on failure.
196 */
197[[nodiscard]] MAYAFLUX_API std::shared_ptr<VKBuffer> create_image_staging_buffer(size_t size);
198
199/**
200 * @brief Check if buffer is device-local (staging needed)
201 * @param buffer Buffer to check
202 * @return True if buffer is device-local
203 */
204MAYAFLUX_API bool is_device_local(const std::shared_ptr<VKBuffer>& buffer);
205
206/**
207 * @brief Grow a GPU buffer (and its paired staging buffer) to fit @p required bytes.
208 * @param target Device-local or host-visible GPU buffer to resize.
209 * @param staging Paired staging buffer, or nullptr if the target is host-visible.
210 * @param required Bytes needed for the next upload.
211 * @param growth_factor Multiplier applied when a resize is triggered (default 1.5).
212 *
213 * No-op when @p target already has sufficient capacity. When a resize is necessary
214 * both buffers are grown to `required * growth_factor` so subsequent small
215 * increments do not trigger further allocations. Existing GPU data is not preserved
216 * (preserve_data = false) because the caller is about to overwrite it.
217 */
218MAYAFLUX_API void ensure_gpu_capacity(
219 const std::shared_ptr<VKBuffer>& target,
220 const std::shared_ptr<VKBuffer>& staging,
221 size_t required,
222 float growth_factor = k_buffer_growth_factor);
223
224/**
225 * @brief Upload data from DataAccess view to GPU buffer (precision-preserving)
226 * @tparam T View type (double, glm::dvec2, glm::dvec3, glm::vec3, float, etc.)
227 * @param accessor DataAccess instance providing the view
228 * @param target Target GPU buffer
229 * @param staging Optional staging buffer (auto-created if needed)
230 *
231 * Zero-copy when types match, automatic conversion cache when they don't.
232 * For AUDIO modalities, defaults to DOUBLE precision to preserve accuracy.
233 */
234template <typename T>
236 const Kakshya::DataAccess& accessor,
237 const std::shared_ptr<VKBuffer>& target,
238 const std::shared_ptr<VKBuffer>& staging = nullptr)
239{
240 auto view = accessor.view<T>();
241
242 const void* data_ptr = view.data();
243 size_t data_bytes = view.size() * sizeof(T);
244
245 if constexpr (std::is_same_v<T, double>) {
246 if (target->get_format() != vk::Format::eR64Sfloat) {
248 "Uploading double precision to buffer with format {}. Consider using R64Sfloat for audio.",
249 vk::to_string(target->get_format()));
250 }
251 }
252
253 upload_to_gpu(data_ptr, data_bytes, target, staging);
254}
255
256/**
257 * @brief Upload structured data with GLM double-precision types
258 * @tparam T GLM type (glm::dvec2, glm::dvec3, glm::dvec4 for double precision)
259 * @param accessor DataAccess with structured dimensions
260 * @param target Target GPU buffer
261 * @param staging Optional staging buffer
262 *
263 * Use this for high-precision structured data like audio samples interpreted
264 * as multi-dimensional vectors. Supports both single and double precision GLM types.
265 */
266template <typename T>
267 requires GlmType<T>
269 const Kakshya::DataAccess& accessor,
270 const std::shared_ptr<VKBuffer>& target,
271 const std::shared_ptr<VKBuffer>& staging = nullptr)
272{
273 if (!accessor.is_structured()) {
274 error<std::invalid_argument>(
277 std::source_location::current(),
278 "Cannot upload structured view from non-structured data");
279 }
280
281 auto structured_view = accessor.view<T>();
282 upload_to_gpu(structured_view.data(), structured_view.size_bytes(), target, staging);
283}
284
285/**
286 * @brief Download GPU buffer to DataAccess-compatible format (precision-preserving)
287 */
288template <typename T>
290 const std::shared_ptr<VKBuffer>& source,
291 Kakshya::DataVariant& target_variant,
292 const std::vector<Kakshya::DataDimension>& dimensions,
293 Kakshya::DataModality modality,
294 const std::shared_ptr<VKBuffer>& staging = nullptr)
295{
296 size_t element_count = source->get_size_bytes() / sizeof(T);
297
298 std::vector<T> temp_buffer(element_count);
299 download_from_gpu(source, temp_buffer, staging);
300
301 target_variant = std::move(temp_buffer);
302
303 return Kakshya::DataAccess(target_variant, dimensions, modality);
304}
305
306/**
307 * @brief Upload AudioBuffer to GPU (always double precision)
308 * @param audio_buffer Source audio buffer (double[])
309 * @param gpu_buffer Target GPU buffer (must support R64Sfloat format)
310 * @param staging Optional staging buffer (auto-created if needed)
311 *
312 * AudioBuffer is always double precision. This function ensures the GPU buffer
313 * is configured for double precision and performs a direct upload with no conversion.
314 *
315 * @throws std::runtime_error if gpu_buffer doesn't support double precision
316 */
318 const std::shared_ptr<AudioBuffer>& audio_buffer,
319 const std::shared_ptr<VKBuffer>& gpu_buffer,
320 const std::shared_ptr<VKBuffer>& staging = nullptr);
321
322/**
323 * @brief Download GPU buffer to AudioBuffer (expects double precision)
324 * @param gpu_buffer Source GPU buffer (should contain double precision data)
325 * @param audio_buffer Target audio buffer (always double[])
326 * @param staging Optional staging buffer (auto-created if needed)
327 *
328 * Downloads GPU data and copies to AudioBuffer. If the GPU buffer contains
329 * float data instead of double, DataAccess will handle the upconversion
330 * (though this is not recommended for audio precision).
331 */
333 const std::shared_ptr<VKBuffer>& gpu_buffer,
334 const std::shared_ptr<AudioBuffer>& audio_buffer,
335 const std::shared_ptr<VKBuffer>& staging = nullptr);
336
337} // namespace MayaFlux::Buffers
#define MF_WARN(comp, ctx,...)
Range size
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
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