MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
BufferService.hpp
Go to the documentation of this file.
1#pragma once
2
4
5/**
6 * @brief Backend buffer management service interface
7 *
8 * Defines GPU/backend buffer operations that any graphics backend must provide.
9 * Implementations are backend-specific (Vulkan, OpenGL, Metal, DirectX, etc.).
10 * All handles are opaque to maintain backend independence while providing type safety.
11 */
12struct MAYAFLUX_API BufferService {
13 /**
14 * @brief Initialize a buffer object
15 * @param buffer Buffer handle to initialize
16 */
17 std::function<void(const std::shared_ptr<void>&)> initialize_buffer;
18
19 /**
20 * @brief Allocate a raw buffer/memory pair without an owning VKBuffer.
21 * @param size_bytes Buffer capacity in bytes.
22 * @param usage Opaque backend usage flags (vk::BufferUsageFlags, cast to void* representation by convention).
23 * @param memory_properties Opaque backend memory property flags.
24 * @param host_visible Whether to map the allocation and populate the mapped pointer output.
25 * @param out_buffer Receives the created buffer handle (opaque).
26 * @param out_memory Receives the bound memory handle (opaque).
27 * @param out_mapped_ptr Receives the mapped host pointer, or nullptr if not host-visible.
28 */
29 std::function<void(size_t, uint32_t, uint32_t, bool, void*&, void*&, void*&)> allocate_raw_buffer;
30
31 /**
32 * @brief Destroy a buffer and free its associated memory
33 * @param buffer Buffer handle to destroy
34 *
35 * Automatically handles cleanup of both buffer and memory resources.
36 * Safe to call with invalid/null handles (no-op).
37 */
38 std::function<void(const std::shared_ptr<void>&)> destroy_buffer;
39
40 /**
41 * @brief Map buffer memory to host-visible pointer
42 * @param memory Memory handle to map
43 * @param offset Offset in bytes from memory start
44 * @param size Size in bytes to map (or 0 for entire range)
45 * @return Host-visible pointer to mapped memory
46 *
47 * Only valid for host-visible memory. Pointer remains valid until
48 * unmap_buffer() is called. Multiple maps of the same memory are
49 * backend-specific behavior.
50 */
51 std::function<void*(void*, size_t, size_t)> map_buffer;
52
53 /**
54 * @brief Unmap previously mapped buffer memory
55 * @param memory Memory handle to unmap
56 *
57 * Invalidates the pointer returned by map_buffer(). Host writes may
58 * not be visible to device until flush_range() is called.
59 */
60 std::function<void(void*)> unmap_buffer;
61
62 /**
63 * @brief Copy a contiguous byte range from one device buffer to another.
64 * @param src Source buffer handle (opaque to caller).
65 * @param dst Destination buffer handle (opaque to caller).
66 * @param size Byte count to copy.
67 * @param src_offset Byte offset into source.
68 * @param dst_offset Byte offset into destination.
69 *
70 * Recorded into a single-use command buffer and submitted synchronously.
71 * Caller must ensure both buffers are device-accessible.
72 */
73 std::function<void(void*, void*, size_t, size_t, size_t)> copy_buffer;
74
75 /**
76 * @brief Flush mapped memory range (make host writes visible to device)
77 * @param memory Memory handle
78 * @param offset Offset in bytes
79 * @param size Size in bytes to flush (or 0 for entire mapped range)
80 *
81 * Required for non-coherent host-visible memory after CPU writes.
82 * For coherent memory, this is typically a no-op but may still
83 * provide ordering guarantees.
84 */
85 std::function<void(void*, size_t, size_t)> flush_range;
86
87 /**
88 * @brief Query the device address of a buffer
89 * @param buffer Buffer handle: must have been initialized with a BDA-capable usage
90 * @return Device address, or 0 if the buffer does not support device addressing
91 */
92 std::function<uint64_t(const std::shared_ptr<void>&)> get_buffer_device_address;
93
94 /**
95 * @brief Invalidate mapped memory range (make device writes visible to host)
96 * @param memory Memory handle
97 * @param offset Offset in bytes
98 * @param size Size in bytes to invalidate (or 0 for entire mapped range)
99 *
100 * Required for non-coherent host-visible memory before CPU reads.
101 * Ensures device writes are visible to the host.
102 */
103 std::function<void(void*, size_t, size_t)> invalidate_range;
104
105 /**
106 * @brief Execute commands immediately with synchronization
107 * @param recorder Lambda that records commands into command buffer
108 *
109 * The backend handles:
110 * - Command buffer allocation
111 * - Begin/end recording
112 * - Queue submission
113 * - Fence wait for completion
114 *
115 * Blocks until GPU operations complete. Thread-safe.
116 *
117 * Example:
118 * service->execute_immediate([&](CommandBuffer cmd) {
119 * // Record copy, clear, or other operations
120 * });
121 */
122 std::function<void(std::function<void(void*)>)> execute_immediate;
123
124 /**
125 * @brief Record commands for deferred execution
126 * @param recorder Lambda that records commands into command buffer
127 *
128 * Commands are batched and submitted later by backend for optimal
129 * performance. Does not block. Thread-safe.
130 *
131 * Use for operations where immediate completion is not required.
132 * Backend determines when to flush batched commands.
133 */
134 std::function<void(std::function<void(void*)>)> record_deferred;
135
136 /**
137 * @brief Submit commands with a fence. Non-blocking.
138 * @param recorder Lambda that records commands into command buffer.
139 * @return Opaque handle tracking the submission.
140 *
141 * Allocates a command buffer and fence, runs the recorder, submits to
142 * the graphics queue with the fence attached, and returns immediately.
143 * The returned handle owns the cmd buffer + fence lifetime.
144 *
145 * Use wait_fenced() to block until the submission completes, then
146 * release_fenced() to free the underlying resources.
147 *
148 * Unlike execute_immediate, does not drain the graphics queue. Safe
149 * to invoke from any thread; blocks only the thread that calls
150 * wait_fenced().
151 */
152 std::function<std::shared_ptr<void>(std::function<void(void*)>)> execute_fenced;
153
154 /**
155 * @brief Wait for a fenced submission to complete.
156 * @param handle Handle returned by execute_fenced.
157 *
158 * Blocks the calling thread until the fence signals. Safe to call
159 * from any thread. No-op if handle is null or already waited.
160 */
161 std::function<void(const std::shared_ptr<void>&)> wait_fenced;
162
163 /**
164 * @brief Release resources associated with a fenced submission.
165 * @param handle Handle returned by execute_fenced.
166 *
167 * Destroys the fence and frees the command buffer. Caller must have
168 * already observed completion via wait_fenced() (or equivalent) before
169 * calling this. No-op if handle is null.
170 */
171 std::function<void(const std::shared_ptr<void>&)> release_fenced;
172
173 /**
174 * @brief Copy a byte range between two device buffers without blocking the graphics queue.
175 * @param src Source buffer handle (opaque VkBuffer).
176 * @param dst Destination buffer handle (opaque VkBuffer).
177 * @param size Byte count to copy.
178 * @param src_offset Byte offset into source.
179 * @param dst_offset Byte offset into destination.
180 * @return Opaque fence handle. Pass to wait_fenced(), then release_fenced().
181 */
182 std::function<std::shared_ptr<void>(void*, void*, size_t, size_t, size_t)> copy_buffer_fenced;
183};
184
185} // namespace MayaFlux::Registry::Services
std::function< void(const std::shared_ptr< void > &)> initialize_buffer
Initialize a buffer object.
std::function< void(size_t, uint32_t, uint32_t, bool, void *&, void *&, void *&)> allocate_raw_buffer
Allocate a raw buffer/memory pair without an owning VKBuffer.
std::function< void(void *)> unmap_buffer
Unmap previously mapped buffer memory.
std::function< void(const std::shared_ptr< void > &)> wait_fenced
Wait for a fenced submission to complete.
std::function< std::shared_ptr< void >(void *, void *, size_t, size_t, size_t)> copy_buffer_fenced
Copy a byte range between two device buffers without blocking the graphics queue.
std::function< void(void *, void *, size_t, size_t, size_t)> copy_buffer
Copy a contiguous byte range from one device buffer to another.
std::function< void(const std::shared_ptr< void > &)> release_fenced
Release resources associated with a fenced submission.
std::function< std::shared_ptr< void >(std::function< void(void *)>)> execute_fenced
Submit commands with a fence.
std::function< void(const std::shared_ptr< void > &)> destroy_buffer
Destroy a buffer and free its associated memory.
std::function< void(void *, size_t, size_t)> invalidate_range
Invalidate mapped memory range (make device writes visible to host)
std::function< void(void *, size_t, size_t)> flush_range
Flush mapped memory range (make host writes visible to device)
std::function< void(std::function< void(void *)>)> record_deferred
Record commands for deferred execution.
std::function< void(std::function< void(void *)>)> execute_immediate
Execute commands immediately with synchronization.
std::function< uint64_t(const std::shared_ptr< void > &)> get_buffer_device_address
Query the device address of a buffer.
Backend buffer management service interface.