MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Agent.hpp
Go to the documentation of this file.
1#pragma once
2
5
7
8#include "Sinks.hpp"
9
10namespace MayaFlux::Buffers {
11class VKBuffer;
12}
13
14namespace MayaFlux::Nexus {
15
16/**
17 * @class Agent
18 * @brief Object that both perceives nearby entities and acts on MayaFlux objects.
19 *
20 * Constructed with only a query radius, a perception function, and an influence
21 * function. Position is optional: call @c set_position before registering with
22 * @c Fabric if spatial behaviour is required. Without a position the spatial
23 * index is not consulted and @c spatial_results will be empty on each commit.
24 *
25 * On each commit the perception function is invoked first, then the influence
26 * function. Both receive contexts populated from the same spatial snapshot.
27 *
28 * The id is assigned by @c Fabric::wire and is stable for the object's lifetime.
29 */
30class Agent {
31public:
32 using InfluenceFn = std::function<void(const InfluenceContext&)>;
33 using PerceptionFn = std::function<void(const PerceptionContext&)>;
34
35 /**
36 * @brief Construct with query radius, perception function, and influence function.
37 * @param query_radius Radius passed to the spatial index on each commit.
38 * Ignored if no position has been set.
39 * @param perception Called first on every commit.
40 * @param influence Called second on every commit.
41 */
42 explicit Agent(float query_radius, PerceptionFn perception, InfluenceFn influence)
44 , m_perception_fn(std::move(perception))
45 , m_influence_fn(std::move(influence))
46 {
47 }
48
49 /**
50 * @brief Construct with named perception and influence functions.
51 * @param query_radius Radius passed to the spatial index on each commit.
52 * @param perception_fn_name Identifier for the perception function.
53 * @param perception Called first on every commit.
54 * @param influence_fn_name Identifier for the influence function.
55 * @param influence Called second on every commit.
56 */
58 std::string perception_fn_name, PerceptionFn perception,
59 std::string influence_fn_name, InfluenceFn influence)
62 , m_perception_fn(std::move(perception))
64 , m_influence_fn(std::move(influence))
65 {
66 }
67
68 /** @brief Identifier assigned to the perception function, empty if anonymous. */
69 [[nodiscard]] const std::string& perception_fn_name() const { return m_perception_fn_name; }
70
71 /** @brief Identifier assigned to the influence function, empty if anonymous. */
72 [[nodiscard]] const std::string& influence_fn_name() const { return m_influence_fn_name; }
73
74 /** @brief Set or replace the perception function's identifier. */
75 void set_perception_fn_name(std::string name) { m_perception_fn_name = std::move(name); }
76
77 /** @brief Set or replace the influence function's identifier. */
78 void set_influence_fn_name(std::string name) { m_influence_fn_name = std::move(name); }
79
80 /** @brief The perception function itself. */
81 [[nodiscard]] const PerceptionFn& perception_fn() const { return m_perception_fn; }
82
83 /** @brief The influence function itself. */
84 [[nodiscard]] const InfluenceFn& influence_fn() const { return m_influence_fn; }
85
86 /**
87 * @brief Return the current position, if set.
88 */
89 [[nodiscard]] const std::optional<glm::vec3>& position() const { return m_position; }
90
91 /**
92 * @brief Set the position, enabling spatial indexing and queries for this object.
93 * @param p World-space coordinates.
94 */
95 void set_position(const glm::vec3& p) { m_position = p; }
96
97 /**
98 * @brief Clear the position, removing this object from spatial operations.
99 */
100 void clear_position() { m_position.reset(); }
101
102 /**
103 * @brief Return the query radius.
104 */
105 [[nodiscard]] float query_radius() const { return m_query_radius; }
106
107 /**
108 * @brief Set the query radius.
109 * @param r New radius in world-space units.
110 */
111 void set_query_radius(float r) { m_query_radius = r; }
112
113 /**
114 * @brief Return the stable object id assigned by Fabric.
115 */
116 [[nodiscard]] uint32_t id() const { return m_id; }
117
118 // =========================================================================
119 // Output sinks
120 // =========================================================================
121
122 /** @brief Register an audio output on @p channel. */
124 {
126 }
127
128 /** @brief Register an audio output on @p channel with a producer function. */
130 std::function<Kakshya::DataVariant(const InfluenceContext&)> fn,
131 std::string fn_name = "")
132 {
133 add_audio_sink(m_audio_sinks, mgr, channel, std::move(fn), std::move(fn_name));
134 }
135
136 /** @brief Unregister the audio sink on @p channel. */
141
142 /** @brief Register a render output targeting @p window. */
144 {
145 add_render_sink(m_render_sinks, mgr, config, {}, "", m_position);
146 }
147
148 /** @brief Register a render output targeting @p window with a producer function. */
150 std::string fn_name, RenderFn fn)
151 {
152 add_render_sink(m_render_sinks, mgr, config, std::move(fn), std::move(fn_name), m_position);
153 }
154
155 [[nodiscard]] const std::vector<AudioSink>& audio_sinks() const { return m_audio_sinks; }
156 [[nodiscard]] const std::vector<RenderSink>& render_sinks() const { return m_render_sinks; }
157
158 /* @brief Return the render processor for the sink targeting @p window, or nullptr if not found. */
159 std::shared_ptr<Buffers::RenderProcessor> get_render_processor(
160 const std::shared_ptr<Core::Window>& window) const
161 {
162 auto it = std::ranges::find_if(m_render_sinks,
163 [&window](const RenderSink& s) { return s.window == window; });
164 return it != m_render_sinks.end() ? it->renderer : nullptr;
165 }
166
167 /** @brief Unregister the render sink targeting @p window. */
168 void remove_render(Buffers::BufferManager& mgr, const std::shared_ptr<Core::Window>& window)
169 {
171 }
172
173 /** @brief Push @p samples to all registered audio sinks. */
174 void set_audio_data(std::span<const double> samples)
175 {
177 }
178
179 /**
180 * @brief Push pre-resolved vertex bytes to all registered render sinks.
181 * @param data Pointer to vertex data.
182 * @param byte_count Total size in bytes.
183 * @param layout VertexLayout describing stride and attributes.
184 */
185 void set_vertices(const void* data, size_t byte_count,
186 const Kakshya::VertexLayout& layout)
187 {
188 push_vertices(m_render_sinks, data, byte_count, layout);
189 }
190
191 /**
192 * @brief Push typed vertex data to all registered render sinks.
193 * @tparam T One of Nodes::PointVertex, Nodes::LineVertex, Nodes::MeshVertex.
194 * @param vertices Span of vertex structs.
195 */
196 template <typename T>
197 void set_vertices(std::span<const T> vertices)
198 {
199 auto layout = Nodes::vertex_layout_for<T>();
200 layout.vertex_count = static_cast<uint32_t>(vertices.size());
201 push_vertices(m_render_sinks, vertices.data(),
202 vertices.size_bytes(), layout);
203 }
204
205 /** @brief Set the intensity, a general-purpose parameter for influence functions. */
206 void set_intensity(float i) { m_intensity = i; }
207
208 /** @brief Get the current intensity. */
209 [[nodiscard]] float intensity() const { return m_intensity; }
210
211 /** @brief Set the radius, a general-purpose parameter for influence functions. */
212 void set_radius(float r) { m_radius = r; }
213
214 /** @brief Get the current radius. */
215 [[nodiscard]] float radius() const { return m_radius; }
216
217 /** @brief Set the color, a general-purpose parameter for influence functions. */
218 void set_color(const glm::vec3& c) { m_color = c; }
219
220 /** @brief Clear the color, resetting it to an unset state. */
221 void clear_color() { m_color.reset(); }
222
223 /** @brief Get the current color, if set. */
224 [[nodiscard]] const std::optional<glm::vec3>& color() const { return m_color; }
225
226 /** @brief Set the size, a general-purpose parameter for influence functions. */
227 void set_size(float s) { m_size = s; }
228
229 /** @brief Clear the size, resetting it to an unset state. */
230 void clear_size() { m_size.reset(); }
231
232 /** @brief Get the current size, if set. */
233 [[nodiscard]] const std::optional<float>& size() const { return m_size; }
234
235 /**
236 * @brief Set the render processor to target for GPU-side influence delivery.
237 *
238 * Creates a UBO matching the InfluenceUBO layout, registers a binding
239 * named "u_influence" at set=1 binding=0 on the target processor,
240 * and binds the UBO. On each subsequent invoke(), the context fields
241 * are packed into the UBO automatically.
242 *
243 * @param proc Target render processor. Must outlive this Emitter or
244 * be cleared via clear_influence_target() first.
245 */
246 void set_influence_target(std::shared_ptr<Buffers::RenderProcessor> proc);
247
248 /**
249 * @brief Disconnect from the current influence target.
250 *
251 * Unbinds the "u_influence" descriptor from the target processor
252 * and releases the UBO.
253 */
255
256 /**
257 * @brief Return the current influence target, if set.
258 */
259 [[nodiscard]] std::weak_ptr<Buffers::RenderProcessor> influence_target() const
260 {
261 return m_influence_target;
262 }
263
264 /**
265 * @brief Invoke the perception function with the supplied context.
266 * @param ctx Populated context for this commit.
267 */
269 {
270 if (m_perception_fn) {
271 m_perception_fn(ctx);
272 }
273 }
274
275 /**
276 * @brief Invoke the influence function with the supplied context.
277 * @param ctx Populated context for this commit.
278 */
279 void invoke_influence(const InfluenceContext& ctx) const
280 {
281 if (m_influence_fn) {
282 m_influence_fn(ctx);
283 }
286 if (m_influence_ubo)
288 }
289
290private:
291 std::optional<glm::vec3> m_position;
292 std::optional<glm::vec3> m_color;
293 std::optional<float> m_size;
294 float m_intensity { 1.0F };
295 float m_radius { 1.0F };
296
297 std::shared_ptr<Buffers::RenderProcessor> m_influence_target;
298 std::shared_ptr<Buffers::VKBuffer> m_influence_ubo;
299
305 uint32_t m_id {};
306
307 mutable std::vector<AudioSink> m_audio_sinks;
308 mutable std::vector<RenderSink> m_render_sinks;
309
310 void upload_influence_ubo(const InfluenceContext& ctx) const;
311
312 friend class Fabric;
313};
314
315} // namespace MayaFlux::Nexus
uint32_t channel
std::string fn_name
Token-based multimodal buffer management system for unified data stream processing.
void set_color(const glm::vec3 &c)
Set the color, a general-purpose parameter for influence functions.
Definition Agent.hpp:218
float radius() const
Get the current radius.
Definition Agent.hpp:215
void set_influence_fn_name(std::string name)
Set or replace the influence function's identifier.
Definition Agent.hpp:78
const std::optional< glm::vec3 > & position() const
Return the current position, if set.
Definition Agent.hpp:89
Agent(float query_radius, PerceptionFn perception, InfluenceFn influence)
Construct with query radius, perception function, and influence function.
Definition Agent.hpp:42
void set_intensity(float i)
Set the intensity, a general-purpose parameter for influence functions.
Definition Agent.hpp:206
const std::optional< glm::vec3 > & color() const
Get the current color, if set.
Definition Agent.hpp:224
InfluenceFn m_influence_fn
Definition Agent.hpp:304
void invoke_influence(const InfluenceContext &ctx) const
Invoke the influence function with the supplied context.
Definition Agent.hpp:279
PerceptionFn m_perception_fn
Definition Agent.hpp:302
void remove_audio_sink(Buffers::BufferManager &mgr, uint32_t channel)
Unregister the audio sink on channel.
Definition Agent.hpp:137
void set_size(float s)
Set the size, a general-purpose parameter for influence functions.
Definition Agent.hpp:227
void upload_influence_ubo(const InfluenceContext &ctx) const
Definition Agent.cpp:41
uint32_t id() const
Return the stable object id assigned by Fabric.
Definition Agent.hpp:116
const std::vector< AudioSink > & audio_sinks() const
Definition Agent.hpp:155
std::vector< AudioSink > m_audio_sinks
Definition Agent.hpp:307
void clear_position()
Clear the position, removing this object from spatial operations.
Definition Agent.hpp:100
void render(Buffers::BufferManager &mgr, const Portal::Graphics::RenderConfig &config)
Register a render output targeting window.
Definition Agent.hpp:143
std::optional< float > m_size
Definition Agent.hpp:293
std::optional< glm::vec3 > m_position
Definition Agent.hpp:291
const InfluenceFn & influence_fn() const
The influence function itself.
Definition Agent.hpp:84
void clear_influence_target()
Disconnect from the current influence target.
Definition Agent.cpp:34
std::function< void(const PerceptionContext &)> PerceptionFn
Definition Agent.hpp:33
float intensity() const
Get the current intensity.
Definition Agent.hpp:209
void sink_audio(Buffers::BufferManager &mgr, uint32_t channel)
Register an audio output on channel.
Definition Agent.hpp:123
void invoke_perception(const PerceptionContext &ctx) const
Invoke the perception function with the supplied context.
Definition Agent.hpp:268
const std::string & influence_fn_name() const
Identifier assigned to the influence function, empty if anonymous.
Definition Agent.hpp:72
const std::optional< float > & size() const
Get the current size, if set.
Definition Agent.hpp:233
void clear_size()
Clear the size, resetting it to an unset state.
Definition Agent.hpp:230
std::shared_ptr< Buffers::RenderProcessor > m_influence_target
Definition Agent.hpp:297
void set_audio_data(std::span< const double > samples)
Push samples to all registered audio sinks.
Definition Agent.hpp:174
std::optional< glm::vec3 > m_color
Definition Agent.hpp:292
void render(Buffers::BufferManager &mgr, const Portal::Graphics::RenderConfig &config, std::string fn_name, RenderFn fn)
Register a render output targeting window with a producer function.
Definition Agent.hpp:149
void set_influence_target(std::shared_ptr< Buffers::RenderProcessor > proc)
Set the render processor to target for GPU-side influence delivery.
Definition Agent.cpp:9
void clear_color()
Clear the color, resetting it to an unset state.
Definition Agent.hpp:221
void set_vertices(const void *data, size_t byte_count, const Kakshya::VertexLayout &layout)
Push pre-resolved vertex bytes to all registered render sinks.
Definition Agent.hpp:185
std::shared_ptr< Buffers::RenderProcessor > get_render_processor(const std::shared_ptr< Core::Window > &window) const
Definition Agent.hpp:159
void set_query_radius(float r)
Set the query radius.
Definition Agent.hpp:111
std::string m_perception_fn_name
Definition Agent.hpp:301
void set_radius(float r)
Set the radius, a general-purpose parameter for influence functions.
Definition Agent.hpp:212
void set_perception_fn_name(std::string name)
Set or replace the perception function's identifier.
Definition Agent.hpp:75
void remove_render(Buffers::BufferManager &mgr, const std::shared_ptr< Core::Window > &window)
Unregister the render sink targeting window.
Definition Agent.hpp:168
const std::string & perception_fn_name() const
Identifier assigned to the perception function, empty if anonymous.
Definition Agent.hpp:69
std::weak_ptr< Buffers::RenderProcessor > influence_target() const
Return the current influence target, if set.
Definition Agent.hpp:259
Agent(float query_radius, std::string perception_fn_name, PerceptionFn perception, std::string influence_fn_name, InfluenceFn influence)
Construct with named perception and influence functions.
Definition Agent.hpp:57
std::string m_influence_fn_name
Definition Agent.hpp:303
const PerceptionFn & perception_fn() const
The perception function itself.
Definition Agent.hpp:81
std::vector< RenderSink > m_render_sinks
Definition Agent.hpp:308
float query_radius() const
Return the query radius.
Definition Agent.hpp:105
void set_vertices(std::span< const T > vertices)
Push typed vertex data to all registered render sinks.
Definition Agent.hpp:197
void set_position(const glm::vec3 &p)
Set the position, enabling spatial indexing and queries for this object.
Definition Agent.hpp:95
std::function< void(const InfluenceContext &)> InfluenceFn
Definition Agent.hpp:32
void sink_audio(Buffers::BufferManager &mgr, uint32_t channel, std::function< Kakshya::DataVariant(const InfluenceContext &)> fn, std::string fn_name="")
Register an audio output on channel with a producer function.
Definition Agent.hpp:129
std::shared_ptr< Buffers::VKBuffer > m_influence_ubo
Definition Agent.hpp:298
const std::vector< RenderSink > & render_sinks() const
Definition Agent.hpp:156
Object that both perceives nearby entities and acts on MayaFlux objects.
Definition Agent.hpp:30
Orchestrates spatial indexing and scheduling for Nexus objects.
Definition Fabric.hpp:37
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.
Definition NDData.hpp:76
void remove_render_sink(std::vector< RenderSink > &sinks, Buffers::BufferManager &mgr, const std::shared_ptr< Core::Window > &window)
Unregister and destroy the render sink targeting window.
Definition Sinks.cpp:194
void add_audio_sink(std::vector< AudioSink > &sinks, Buffers::BufferManager &mgr, uint32_t channel, std::function< Kakshya::DataVariant(const InfluenceContext &)> fn, std::string fn_name)
Create and register an audio sink on channel.
Definition Sinks.cpp:18
void dispatch_audio_sinks(std::vector< AudioSink > &sinks, const InfluenceContext &ctx)
For each sink that has a producer fn, call it and push the result.
Definition Sinks.cpp:71
void add_render_sink(std::vector< RenderSink > &sinks, Buffers::BufferManager &mgr, const Portal::Graphics::RenderConfig &config, RenderFn fn, std::string fn_name, const std::optional< glm::vec3 > &initial_position)
Create and register a render sink targeting window.
Definition Sinks.cpp:95
std::function< void(const InfluenceContext &)> RenderFn
Definition Sinks.hpp:28
void push_audio_data(std::vector< AudioSink > &sinks, std::span< const double > samples)
Push samples to every audio sink in sinks.
Definition Sinks.cpp:64
void remove_audio_sink(std::vector< AudioSink > &sinks, Buffers::BufferManager &mgr, uint32_t channel)
Unregister and destroy the audio sink on channel.
Definition Sinks.cpp:43
void push_vertices(std::vector< RenderSink > &sinks, const void *data, size_t byte_count, const Kakshya::VertexLayout &layout)
Push pre-resolved vertex bytes to every render sink.
Definition Sinks.cpp:215
void dispatch_render_sinks(std::vector< RenderSink > &sinks, const InfluenceContext &ctx)
For each sink that has a producer fn, call it and push the result.
Definition Sinks.cpp:225
Complete description of vertex data layout in a buffer.
Data passed to an Emitter or Agent influence function on each commit.
Data passed to a Sensor or Agent perception function on each commit.
std::shared_ptr< Core::Window > window
Definition Sinks.hpp:71
Holds the plumbing for one graphics output registered from a Nexus object.
Definition Sinks.hpp:65
Unified rendering configuration for graphics buffers.