MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RelaxationGridBuffer.cpp
Go to the documentation of this file.
4
7
9
11
14
15namespace MayaFlux::Buffers {
16
18 uint32_t width,
19 uint32_t height,
20 size_t cell_stride_bytes,
21 ShaderSource rule_source,
22 ShaderSource emit_source)
23 : VKBuffer(
24 static_cast<size_t>(width) * height * sizeof(Kakshya::Vertex),
25 Usage::VERTEX,
26 Kakshya::DataModality::VERTEX_POSITIONS_3D)
27 , m_width(width)
28 , m_height(height)
29 , m_cell_stride_bytes(cell_stride_bytes)
30 , m_rule_source(std::move(rule_source))
31 , m_emit_source(std::move(emit_source))
32{
33 auto buffer_service = Registry::BackendRegistry::instance()
35
36 if (!buffer_service) {
37 error<std::runtime_error>(
40 std::source_location::current(),
41 "RelaxationGridBuffer requires a valid BufferService");
42 }
43
44 const size_t state_bytes = get_state_bytes();
45 const auto usage_flags = static_cast<uint32_t>(
46 VkBufferUsageFlags(vk::BufferUsageFlagBits::eStorageBuffer));
47 const auto memory_flags = static_cast<uint32_t>(
48 VkMemoryPropertyFlags(vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent));
49
50 auto& resources = get_buffer_resources();
51 resources.back_buffers.resize(2);
52
53 for (uint32_t i = 0; i < 2; ++i) {
54 void* out_buffer = nullptr;
55 void* out_memory = nullptr;
56 void* out_mapped = nullptr;
57
58 buffer_service->allocate_raw_buffer(
59 state_bytes, usage_flags, memory_flags, true,
60 out_buffer, out_memory, out_mapped);
61
62 resources.back_buffers[i].buffer = static_cast<vk::Buffer>(static_cast<VkBuffer>(out_buffer));
63 resources.back_buffers[i].memory = static_cast<vk::DeviceMemory>(static_cast<VkDeviceMemory>(out_memory));
64 resources.back_buffers[i].mapped_ptr = out_mapped;
65 }
66
68 "RelaxationGridBuffer: {}x{} grid, {} bytes/cell, {} bytes/generation",
69 m_width, m_height, m_cell_stride_bytes, state_bytes);
70}
71
73{
75 layout.vertex_count = get_cell_count();
76 set_vertex_layout(layout);
77
78 m_step_processor = std::visit(
79 [](const auto& src) { return std::make_shared<RelaxationStepProcessor>(src); },
81
82 m_step_processor->set_processing_token(token);
84
85 auto chain = get_processing_chain();
86 if (!chain) {
87 chain = std::make_shared<BufferProcessingChain>();
89 }
90 chain->set_preferred_token(token);
91
92 m_emit_processor = std::visit(
93 [](const auto& src) { return std::make_shared<RelaxationEmitProcessor>(src); },
95 m_emit_processor->set_processing_token(token);
96 chain->add_processor(m_emit_processor, shared_from_this());
97
99 "RelaxationGridBuffer setup_processors: step + emit attached");
100}
101
103{
104 RenderConfig resolved = config;
106
107 if (resolved.vertex_shader.empty())
108 resolved.vertex_shader = "point.vert.spv";
109 if (resolved.fragment_shader.empty())
110 resolved.fragment_shader = "point.frag.spv";
111
112 ShaderConfig sc { resolved.vertex_shader };
113 apply_render_config(resolved, sc);
114
115 get_processing_chain()->add_final_processor(m_render_processor, shared_from_this());
116
118 "RelaxationGridBuffer: rendering configured");
119}
120
121void RelaxationGridBuffer::seed_state(const void* data, size_t size)
122{
123 if (size != get_state_bytes()) {
125 "seed_state size {} does not match expected {}", size, get_state_bytes());
126 return;
127 }
128
129 auto& resources = get_buffer_resources();
130 auto& front = resources.back_buffers[front_index()];
131
132 if (!front.mapped_ptr) {
134 "seed_state: front generation buffer has no mapped pointer");
135 return;
136 }
137
138 std::memcpy(front.mapped_ptr, data, size);
139}
140
141} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
uint32_t width
Definition Decoder.cpp:66
uint32_t height
void setup_rendering(const RenderConfig &config)
Attach a RenderProcessor drawing the emitted vertex output as ordinary untextured point geometry.
std::shared_ptr< RelaxationEmitProcessor > m_emit_processor
Emit stage, retained for parameter access.
ShaderSource m_emit_source
Either a path to a hand-written emit shader or a generated ShaderSpec.
RelaxationGridBuffer(uint32_t width, uint32_t height, size_t cell_stride_bytes, ShaderSource rule_source, ShaderSource emit_source)
Construct an unregistered double-buffered grid buffer.
void setup_processors(ProcessingToken token) override
Create and attach RelaxationStepProcessor (default processor) and RelaxationEmitProcessor (flat proce...
void seed_state(const void *data, size_t size)
Write initial state directly into the current front generation.
uint32_t get_cell_count() const
Total cell count, width * height.
std::shared_ptr< RelaxationStepProcessor > m_step_processor
Rule stage, retained for parameter access.
size_t get_state_bytes() const
Total byte size of one generation's state buffer.
size_t m_cell_stride_bytes
Size in bytes of one cell's state, fixed at construction.
ShaderSource m_rule_source
Either a path to a hand-written rule shader or a generated ShaderSpec.
std::variant< std::string, Portal::Graphics::ShaderSpec > ShaderSource
Either a shader file path or a generated ShaderSpec, resolved at construction time by setup_processor...
uint32_t front_index() const
Index into m_resources.back_buffers currently holding the most recently completed generation's state.
void set_vertex_layout(const Kakshya::VertexLayout &layout)
Set vertex layout for this buffer.
Definition VKBuffer.cpp:387
std::shared_ptr< Buffers::BufferProcessingChain > get_processing_chain() override
Access the buffer's processing chain.
Definition VKBuffer.cpp:263
void set_default_processor(const std::shared_ptr< BufferProcessor > &processor) override
Set the buffer's default processor.
Definition VKBuffer.cpp:247
void set_processing_chain(const std::shared_ptr< BufferProcessingChain > &chain, bool force=false) override
Replace the buffer's processing chain.
Definition VKBuffer.cpp:268
void apply_render_config(const RenderConfig &config, const ShaderConfig &shader_config)
Configure the internal m_render_processor from a RenderConfig.
Definition VKBuffer.cpp:336
std::shared_ptr< RenderProcessor > m_render_processor
Definition VKBuffer.hpp:620
const VKBufferResources & get_buffer_resources() const
Get all buffer resources at once (read-only)
Definition VKBuffer.hpp:341
Vulkan-backed buffer wrapper used in processing chains.
Definition VKBuffer.hpp:76
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
@ BufferManagement
Buffer Management (Buffers::BufferManager, creating buffers)
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.
BufferUsageHint
Semantic usage hint for buffer allocation and memory properties.
static VertexLayout for_raw(uint32_t stride=60)
Factory: layout for raw vertex data with common attributes.
Type-neutral vertex carrying the universal 60-byte attribute layout.
Unified rendering configuration for graphics buffers.
Backend buffer management service interface.