MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
FormaBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Core {
6class Window;
7}
8
9namespace MayaFlux::Buffers {
10class FormaProcessor;
11class RenderProcessor;
12
13/**
14 * @class FormaBuffer
15 * @brief VKBuffer for 2D screen-space Forma geometry.
16 *
17 * Encodes the invariants that hold for all Forma geometry:
18 * - No depth test, no depth write, no view transform.
19 * - Alpha blending enabled on the RenderProcessor.
20 * - Topology fixed at construction (determines default shaders).
21 *
22 * Provides write_bytes() as the clean write interface for Mapped::sync(),
23 * replacing the raw set_data path on plain VKBuffer.
24 *
25 * Usage:
26 * @code
27 * auto buf = std::make_shared<FormaBuffer>(
28 * initial_capacity_bytes,
29 * Portal::Graphics::PrimitiveTopology::TRIANGLE_STRIP);
30 * buf->setup_processors(ProcessingToken::GRAPHICS_BACKEND);
31 * buf->setup_rendering({ .target_window = window });
32 * @endcode
33 */
34class MAYAFLUX_API FormaBuffer : public Buffers::VKBuffer {
35public:
36 /**
37 * @brief Construct a FormaBuffer.
38 * @param capacity_bytes Initial buffer capacity. Size the buffer for the
39 * maximum expected vertex count from the geometry
40 * function. write_bytes() will warn if exceeded.
41 * @param topology Primitive topology. Determines default shaders
42 * selected in setup_rendering().
43 */
45 size_t capacity_bytes,
47
48 /**
49 * @brief Construct a FormaBuffer.
50 * @param topology Primitive topology. Determines default shaders
51 * selected in setup_rendering().
52 */
55
56 ~FormaBuffer() override = default;
57
58 FormaBuffer(const FormaBuffer&) = delete;
62
63 /**
64 * @brief Register with the graphics subsystem.
65 * Creates a BufferUploadProcessor as default processor.
66 */
67 void setup_processors(ProcessingToken token) override;
68
69 /**
70 * @brief Attach a RenderProcessor targeting the given window.
71 *
72 * Selects default shaders based on topology set at construction.
73 * Always enables alpha blending and disables depth testing.
74 * Caller may override shaders via config fields.
75 */
76 void setup_rendering(const RenderConfig& config);
77
78 /**
79 * @brief Write vertex bytes into the buffer.
80 *
81 * Called by Mapped::sync() each time the geometry function produces
82 * new vertex data. Copies bytes into the mapped host-visible region
83 * and marks the dirty range for flush on the next graphics tick.
84 *
85 * @param bytes Raw interleaved vertex bytes. Must not exceed the
86 * capacity set at construction.
87 */
88 void submit(const std::vector<uint8_t>& bytes);
89
90 /**
91 * @brief Supply a texture to be bound on the next graphics tick.
92 *
93 * The image is owned externally — a TextBuffer's VKImage from
94 * InkPress, a loaded asset, a render target, anything. FormaProcessor
95 * forwards it to the RenderProcessor on the next processing_function
96 * call. Calling again replaces the previous binding.
97 *
98 * @param image GPU image to bind. nullptr clears the binding.
99 * @param binding Descriptor name in the shader (e.g. "texSampler").
100 */
101 void set_texture(std::shared_ptr<Core::VKImage> image, std::string binding);
102
103 /**
104 * @brief Bind a texture to a slot in the multi-texture array at runtime.
105 *
106 * Valid only when setup_rendering was called with additional_textures.
107 * @p array_index is 0-based into the array declared at setup time:
108 * index 0 = weight 1, index 1 = weight 2, etc.
109 *
110 * @param array_index Zero-based index into the textures[] array.
111 * @param image GPU image. Must be GPU-resident.
112 */
113 void bind_texture(uint32_t array_index, const std::shared_ptr<Core::VKImage>& image);
114
115 [[nodiscard]] Portal::Graphics::PrimitiveTopology topology() const { return m_topology; }
116
117 /**
118 * @brief Submit a contiguous sequence of vertices.
119 *
120 * Accepts any contiguous range of trivially-copyable vertex structs:
121 * std::array, std::vector, std::span, or a raw pointer + count pair.
122 * Reinterprets the data as bytes and forwards to the primary submit().
123 *
124 * @tparam V Vertex type. Must be trivially copyable.
125 * @param vertices Contiguous range of V.
126 */
127 template <typename V>
128 requires std::ranges::contiguous_range<V>
129 && std::is_trivially_copyable_v<std::ranges::range_value_t<V>>
130 void submit(const V& vertices)
131 {
132 const auto* src = reinterpret_cast<const uint8_t*>(std::ranges::data(vertices));
133 const size_t n = std::ranges::size(vertices) * sizeof(std::ranges::range_value_t<V>);
134 submit(std::vector<uint8_t>(src, src + n));
135 }
136
137 /**
138 * @brief Submit a single vertex struct.
139 *
140 * Convenience for point-list buffers and single-primitive cases where
141 * constructing a container for one vertex is unnecessary.
142 *
143 * @tparam V Vertex type. Must be trivially copyable.
144 * @param vertex The vertex to write.
145 */
146 template <typename V>
147 requires std::is_trivially_copyable_v<V>
148 && (!std::ranges::range<V>)
149 void submit(const V& vertex)
150 {
151 const auto* src = reinterpret_cast<const uint8_t*>(&vertex);
152 submit(std::vector<uint8_t>(src, src + sizeof(V)));
153 }
154
155private:
157 std::shared_ptr<Buffers::FormaProcessor> m_processor;
158
159 /// @brief Geometry bytes from submit() before setup_processors(). Drained in setup_processors().
160 std::vector<uint8_t> m_pending_geometry;
161
162 /// @brief Textures from set_texture() before setup_processors(). Drained in setup_processors().
163 std::vector<std::pair<std::shared_ptr<Core::VKImage>, std::string>> m_pending_textures;
164};
165
166} // namespace MayaFlux::Buffers
IO::ImageData image
Definition Decoder.cpp:57
std::vector< uint8_t > m_pending_geometry
Geometry bytes from submit() before setup_processors(). Drained in setup_processors().
FormaBuffer & operator=(FormaBuffer &&)=delete
std::shared_ptr< Buffers::FormaProcessor > m_processor
Portal::Graphics::PrimitiveTopology topology() const
FormaBuffer(FormaBuffer &&)=delete
FormaBuffer & operator=(const FormaBuffer &)=delete
~FormaBuffer() override=default
Portal::Graphics::PrimitiveTopology m_topology
void submit(const V &vertices)
Submit a contiguous sequence of vertices.
FormaBuffer(const FormaBuffer &)=delete
std::vector< std::pair< std::shared_ptr< Core::VKImage >, std::string > > m_pending_textures
Textures from set_texture() before setup_processors(). Drained in setup_processors().
void submit(const V &vertex)
Submit a single vertex struct.
VKBuffer for 2D screen-space Forma geometry.
Vulkan-backed buffer wrapper used in processing chains.
Definition VKBuffer.hpp:67
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
PrimitiveTopology
Vertex assembly primitive topology.
Unified rendering configuration for graphics buffers.