14 target->get_modality());
16 auto [ptr, bytes, format_hint] = accessor.
gpu_buffer();
18 if (bytes > target->get_size_bytes()) {
19 error<std::runtime_error>(
22 std::source_location::current(),
23 "Upload data size {} exceeds buffer capacity {}",
24 bytes, target->get_size_bytes());
27 auto& target_resources = target->get_buffer_resources();
28 void* mapped = target_resources.mapped_ptr;
30 error<std::runtime_error>(
33 std::source_location::current(),
34 "Host-visible buffer has no mapped pointer");
37 std::memcpy(mapped, ptr, bytes);
39 target->mark_dirty_range(0, bytes);
44 if (!buffer_service) {
45 error<std::runtime_error>(
48 std::source_location::current(),
49 "upload_host_visible requires a valid buffer service");
52 auto dirty_ranges = target->get_and_clear_dirty_ranges();
53 for (
auto& [offset, size] : dirty_ranges) {
54 buffer_service->flush_range(
55 target_resources.memory,
66 target->get_modality());
68 auto [ptr, bytes, format_hint] = accessor.
gpu_buffer();
70 if (bytes > target->get_size_bytes()) {
71 error<std::runtime_error>(
74 std::source_location::current(),
75 "Upload data size {} exceeds buffer capacity {}",
76 bytes, target->get_size_bytes());
79 auto& staging_resources = staging_buffer->get_buffer_resources();
81 void* staging_mapped = staging_resources.mapped_ptr;
82 if (!staging_mapped) {
83 error<std::runtime_error>(
86 std::source_location::current(),
87 "Staging buffer has no mapped pointer");
90 std::memcpy(staging_mapped, ptr, bytes);
91 staging_buffer->mark_dirty_range(0, bytes);
96 if (!buffer_service) {
97 error<std::runtime_error>(
100 std::source_location::current(),
101 "upload_host_visible requires a valid buffer service");
104 auto dirty_ranges = staging_buffer->get_and_clear_dirty_ranges();
105 for (
auto& [offset, size] : dirty_ranges) {
106 buffer_service->flush_range(
107 staging_resources.memory,
112 buffer_service->execute_immediate([&](
void* ptr) {
113 vk::BufferCopy copy_region;
114 copy_region.srcOffset = 0;
115 copy_region.dstOffset = 0;
116 copy_region.size = bytes;
118 vk::CommandBuffer cmd(
static_cast<VkCommandBuffer
>(ptr));
121 staging_buffer->get_buffer(),
122 target->get_buffer(),
129 auto& source_resources = source->get_buffer_resources();
130 void* mapped = source_resources.mapped_ptr;
132 error<std::runtime_error>(
135 std::source_location::current(),
136 "Host-visible buffer has no mapped pointer");
139 source->mark_invalid_range(0, source->get_size_bytes());
144 if (!buffer_service) {
145 error<std::runtime_error>(
148 std::source_location::current(),
149 "upload_host_visible requires a valid buffer service");
152 auto invalid_ranges = source->get_and_clear_invalid_ranges();
153 for (
auto& [offset, size] : invalid_ranges) {
154 buffer_service->invalidate_range(
155 source_resources.memory,
160 std::vector<uint8_t> raw_bytes(source->get_size_bytes());
161 std::memcpy(raw_bytes.data(), mapped, source->get_size_bytes());
163 std::dynamic_pointer_cast<VKBuffer>(target)->set_data({ raw_bytes });
166void download_device_local(
const std::shared_ptr<VKBuffer>& source,
const std::shared_ptr<VKBuffer>& target,
const std::shared_ptr<VKBuffer>& staging_buffer)
171 if (!buffer_service) {
172 error<std::runtime_error>(
175 std::source_location::current(),
176 "download_device_local requires a valid buffer service");
179 vk::BufferCopy copy_region;
180 copy_region.srcOffset = 0;
181 copy_region.dstOffset = 0;
182 copy_region.size = source->get_size_bytes();
184 buffer_service->execute_immediate([&](
void* ptr) {
185 vk::CommandBuffer cmd(
static_cast<VkCommandBuffer
>(ptr));
188 source->get_buffer(),
189 staging_buffer->get_buffer(),
193 staging_buffer->mark_invalid_range(0, source->get_size_bytes());
195 auto& staging_resources = staging_buffer->get_buffer_resources();
196 auto invalid_ranges = staging_buffer->get_and_clear_invalid_ranges();
197 for (
auto& [offset, size] : invalid_ranges) {
198 buffer_service->invalidate_range(
199 staging_resources.memory,
204 void* staging_mapped = staging_resources.mapped_ptr;
205 if (!staging_mapped) {
206 error<std::runtime_error>(
209 std::source_location::current(),
210 "Staging buffer has no mapped pointer");
213 std::vector<uint8_t> raw_bytes(source->get_size_bytes());
214 std::memcpy(raw_bytes.data(), staging_mapped, source->get_size_bytes());
216 std::dynamic_pointer_cast<VKBuffer>(target)->set_data({ raw_bytes });
221 return buffer && !buffer->is_host_visible();
226 auto buffer = std::make_shared<VKBuffer>(
233 if (!buffer_service) {
234 error<std::runtime_error>(
237 std::source_location::current(),
238 "create_staging_buffer requires a valid buffer service");
241 buffer_service->initialize_buffer(buffer);
248 auto buf = std::make_shared<VKBuffer>(
256 if (!buffer_service) {
257 error<std::runtime_error>(
260 std::source_location::current(),
261 "create_image_staging_buffer requires a valid buffer service");
264 buffer_service->initialize_buffer(buf);
267 "create_image_staging_buffer: allocated {} bytes", size);
275 const std::shared_ptr<VKBuffer>& target,
276 const std::shared_ptr<VKBuffer>& staging)
279 error<std::invalid_argument>(
282 std::source_location::current(),
283 "upload_to_gpu: target buffer is null");
290 std::vector<uint8_t> raw_bytes(size);
291 std::memcpy(raw_bytes.data(), data, size);
294 if (target->is_host_visible()) {
297 std::shared_ptr<VKBuffer> staging_buf = staging;
308 const std::shared_ptr<VKBuffer>& source,
311 const std::shared_ptr<VKBuffer>& staging)
314 error<std::invalid_argument>(
317 std::source_location::current(),
318 "download_from_gpu: source buffer is null");
325 auto temp_target = std::make_shared<VKBuffer>(
331 if (!buffer_service) {
332 error<std::runtime_error>(
335 std::source_location::current(),
336 "download_from_gpu requires a valid buffer service");
339 buffer_service->initialize_buffer(temp_target);
341 if (source->is_host_visible()) {
344 std::shared_ptr<VKBuffer> staging_buf = staging;
353 auto temp_data = temp_target->get_data();
355 if (temp_data.empty()) {
356 error<std::runtime_error>(
359 std::source_location::current(),
360 "download_from_gpu: failed to retrieve data from temporary buffer");
363 if (temp_data.size() > 1) {
365 "download_from_gpu: unexpected multiple data variants in temporary buffer. Only the first will be used.");
371 source->get_modality());
373 auto [ptr, bytes, format_hint] = accessor.
gpu_buffer();
375 std::memcpy(data, ptr, std::min(size, bytes));
379 const std::shared_ptr<AudioBuffer>& audio_buffer,
380 const std::shared_ptr<VKBuffer>& gpu_buffer,
381 const std::shared_ptr<VKBuffer>& staging)
383 if (gpu_buffer->get_format() != vk::Format::eR64Sfloat && gpu_buffer->get_format() != vk::Format::eUndefined) {
385 "GPU buffer format is {} but audio requires R64Sfloat for double precision. "
386 "Create VKBuffer with DataModality::AUDIO_1D or AUDIO_MULTICHANNEL.",
387 vk::to_string(gpu_buffer->get_format()));
388 error<std::runtime_error>(
391 std::source_location::current(),
392 "GPU buffer format mismatch for audio upload");
395 auto& audio_data = audio_buffer->get_data();
397 if (audio_data.empty()) {
399 "AudioBuffer contains no data to upload");
403 const void* data_ptr = audio_data.data();
404 size_t data_bytes = audio_data.size() *
sizeof(double);
409 "Uploaded {} bytes of double-precision audio to GPU", data_bytes);
413 const std::shared_ptr<VKBuffer>& gpu_buffer,
414 const std::shared_ptr<AudioBuffer>& audio_buffer,
415 const std::shared_ptr<VKBuffer>& staging)
419 auto dimensions = std::vector<Kakshya::DataDimension> {
421 gpu_buffer->get_size_bytes() /
sizeof(
double),
425 gpu_buffer, downloaded_data, dimensions,
428 auto double_view = accessor.
view<
double>();
430 audio_buffer->get_data() = std::vector<double>(double_view.begin(), double_view.end());
433 "Downloaded {} samples of double-precision audio from GPU", double_view.size());
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
@ STAGING
Host-visible staging buffer (CPU-writable)
auto gpu_buffer() const
Get raw buffer info for GPU upload.
auto view() const
Get explicit typed view of data.
Type-erased accessor for NDData with semantic view construction.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
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_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.
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 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 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.
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.
@ AUDIO_1D
1D audio signal
@ UNKNOWN
Unknown or undefined modality.
@ IMAGE_COLOR
2D RGB/RGBA image
static DataDimension time(uint64_t samples, std::string name="time")
Convenience constructor for a temporal (time) dimension.
Backend buffer management service interface.