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 Flush mapped memory range (make host writes visible to device)
52 * @param memory Memory handle
53 * @param offset Offset in bytes
54 * @param size Size in bytes to flush (or 0 for entire mapped range)
55 *
56 * Required for non-coherent host-visible memory after CPU writes.
57 * For coherent memory, this is typically a no-op but may still
58 * provide ordering guarantees.
59 */
60 std::function<void(void*, size_t, size_t)> flush_range;
61
62 /**
63 * @brief Query the device address of a buffer
64 * @param buffer Buffer handle: must have been initialized with a BDA-capable usage
65 * @return Device address, or 0 if the buffer does not support device addressing
66 */
67 std::function<uint64_t(const std::shared_ptr<void>&)> get_buffer_device_address;
68
69 /**
70 * @brief Invalidate mapped memory range (make device writes visible to host)
71 * @param memory Memory handle
72 * @param offset Offset in bytes
73 * @param size Size in bytes to invalidate (or 0 for entire mapped range)
74 *
75 * Required for non-coherent host-visible memory before CPU reads.
76 * Ensures device writes are visible to the host.
77 */
78 std::function<void(void*, size_t, size_t)> invalidate_range;
79
80 /**
81 * @brief Execute commands immediately with synchronization
82 * @param recorder Lambda that records commands into command buffer
83 *
84 * The backend handles:
85 * - Command buffer allocation
86 * - Begin/end recording
87 * - Queue submission
88 * - Fence wait for completion
89 *
90 * Blocks until GPU operations complete. Thread-safe.
91 *
92 * Example:
93 * service->execute_immediate([&](CommandBuffer cmd) {
94 * // Record copy, clear, or other operations
95 * });
96 */
97 std::function<void(std::function<void(void*)>)> execute_immediate;
98
99 /**
100 * @brief Record commands for deferred execution
101 * @param recorder Lambda that records commands into command buffer
102 *
103 * Commands are batched and submitted later by backend for optimal
104 * performance. Does not block. Thread-safe.
105 *
106 * Use for operations where immediate completion is not required.
107 * Backend determines when to flush batched commands.
108 */
109 std::function<void(std::function<void(void*)>)> record_deferred;
110
111 /**
112 * @brief Submit commands with a fence. Non-blocking.
113 * @param recorder Lambda that records commands into command buffer.
114 * @return Opaque handle tracking the submission.
115 *
116 * Allocates a command buffer and fence, runs the recorder, submits to
117 * the graphics queue with the fence attached, and returns immediately.
118 * The returned handle owns the cmd buffer + fence lifetime.
119 *
120 * Use wait_fenced() to block until the submission completes, then
121 * release_fenced() to free the underlying resources.
122 *
123 * Unlike execute_immediate, does not drain the graphics queue. Safe
124 * to invoke from any thread; blocks only the thread that calls
125 * wait_fenced().
126 */
127 std::function<std::shared_ptr<void>(std::function<void(void*)>)> execute_fenced;
128
129 /**
130 * @brief Wait for a fenced submission to complete.
131 * @param handle Handle returned by execute_fenced.
132 *
133 * Blocks the calling thread until the fence signals. Safe to call
134 * from any thread. No-op if handle is null or already waited.
135 */
136 std::function<void(const std::shared_ptr<void>&)> wait_fenced;
137
138 /**
139 * @brief Release resources associated with a fenced submission.
140 * @param handle Handle returned by execute_fenced.
141 *
142 * Destroys the fence and frees the command buffer. Caller must have
143 * already observed completion via wait_fenced() (or equivalent) before
144 * calling this. No-op if handle is null.
145 */
146 std::function<void(const std::shared_ptr<void>&)> release_fenced;
147};
148
149} // 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< 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.