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