MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RelaxationStepProcessor.cpp
Go to the documentation of this file.
3
5
9
10namespace MayaFlux::Buffers {
11
12RelaxationStepProcessor::RelaxationStepProcessor(const std::string& shader_path, uint32_t workgroup_x)
13 : ComputeProcessor(shader_path, workgroup_x)
14{
15 m_config.bindings["state_in"] = ShaderBinding(0, 0, vk::DescriptorType::eStorageBuffer);
16 m_config.bindings["state_out"] = ShaderBinding(0, 1, vk::DescriptorType::eStorageBuffer);
17}
18
20 : ComputeProcessor(spec)
21{
22 m_config.bindings["state_in"] = ShaderBinding(0, 0, vk::DescriptorType::eStorageBuffer);
23 m_config.bindings["state_out"] = ShaderBinding(0, 1, vk::DescriptorType::eStorageBuffer);
24}
25
26void RelaxationStepProcessor::write_state_descriptors(const std::shared_ptr<RelaxationGridBuffer>& grid)
27{
28 auto& resources = grid->get_buffer_resources();
29 if (resources.back_buffers.size() < 2) {
30 return;
31 }
32
33 if (m_descriptor_set_ids.empty()) {
34 return;
35 }
36
38 const size_t bytes = grid->get_state_bytes();
39 foundry.update_descriptor_buffer(
40 m_descriptor_set_ids[0], 0, vk::DescriptorType::eStorageBuffer,
41 resources.back_buffers[grid->front_index()].buffer, 0, bytes);
42
43 foundry.update_descriptor_buffer(
44 m_descriptor_set_ids[0], 1, vk::DescriptorType::eStorageBuffer,
45 resources.back_buffers[grid->back_index()].buffer, 0, bytes);
46}
47
48void RelaxationStepProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
49{
51 return;
52 }
53
54 m_dispatched = false;
55
58 }
59
61
62 if (!m_dispatched || !m_grid) {
63 return;
64 }
65
66 m_grid->swap_generation();
67
70 }
71}
72
74{
77 }
78
79 auto& data = get_push_constant_data();
80 if (data.size() < m_config.push_constant_size) {
81 data.resize(m_config.push_constant_size);
82 }
83
84 const GridExtent extent {
85 .width = m_grid->get_grid_width(),
86 .height = m_grid->get_grid_height()
87 };
88 std::memcpy(data.data(), &extent, sizeof(GridExtent));
89}
90
91void RelaxationStepProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
92{
94 m_grid = std::dynamic_pointer_cast<RelaxationGridBuffer>(buffer);
95
96 if (m_grid) {
97 set_workgroup_size(16, 16, 1);
99 uint32_t gx = (m_grid->get_grid_width() + 15) / 16;
100 uint32_t gy = (m_grid->get_grid_height() + 15) / 16;
101 set_manual_dispatch(gx, gy, 1);
103 }
104}
105
112
115 const std::shared_ptr<VKBuffer>& buffer)
116{
117 return std::dynamic_pointer_cast<RelaxationGridBuffer>(buffer) != nullptr;
118}
119
122 const std::shared_ptr<VKBuffer>& /*buffer*/,
123 uint32_t /*index*/)
124{
125 m_dispatched = true;
126 return true;
127}
128
129void RelaxationStepProcessor::on_dispatch_complete(const std::shared_ptr<VKBuffer>& /*buffer*/)
130{
131 if (m_grid) {
133 }
134}
135
137{
138 if (!m_grid->consume_snapshot_request()) {
139 return;
140 }
141
142 const auto& front = m_grid->get_buffer_resources().back_buffers[m_grid->front_index()];
143
144 std::vector<uint8_t> bytes(m_grid->get_state_bytes());
145 download_back_buffer(front, bytes.data(), bytes.size(), m_snapshot_staging);
146
147 m_grid->snapshot_source()->signal(bytes);
148}
149
150} // namespace MayaFlux::Buffers
std::shared_ptr< NetworkGeometryBuffer > buffer
virtual void on_attach(const std::shared_ptr< Buffer > &)
Called when this processor is attached to a buffer.
virtual void processing_function(const std::shared_ptr< Buffer > &buffer)=0
The core processing function that must be implemented by derived classes.
void set_workgroup_size(uint32_t x, uint32_t y=1, uint32_t z=1)
Set workgroup size (should match shader local_size)
void set_dispatch_mode(ShaderDispatchConfig::DispatchMode mode)
Set dispatch mode.
void set_manual_dispatch(uint32_t x, uint32_t y=1, uint32_t z=1)
Set manual dispatch group counts.
Specialized ShaderProcessor for Compute Pipelines.
void write_state_descriptors(const std::shared_ptr< RelaxationGridBuffer > &grid)
Issue direct ShaderFoundry descriptor writes for state_in and state_out against the grid's current fr...
void on_dispatch_complete(const std::shared_ptr< VKBuffer > &buffer) override
Serve a pending snapshot when a deferred dispatch completes.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Cover the grid with a 2D dispatch and write GridExtent.
StepPredicate m_step_predicate
Optional gate deciding whether a given cycle advances a generation.
std::shared_ptr< RelaxationGridBuffer > m_grid
The attached RelaxationGridBuffer, cached for descriptor writes and snapshot reads.
bool on_iteration(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer, uint32_t index) override
Record that this cycle reached a dispatch.
void serve_snapshot()
Copy the front state buffer to the host and signal RelaxationGridBuffer::snapshot_source(),...
bool on_before_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer) override
Accept only a RelaxationGridBuffer.
std::shared_ptr< VKBuffer > m_snapshot_staging
Fallback staging buffer for download_back_buffer, unused while back_buffers stays HostVisible | HostC...
RelaxationStepProcessor(const std::string &shader_path, uint32_t workgroup_x=16)
Construct a step processor for the given rule shader file.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
Run one cycle: evaluate the predicate, write the state descriptors, run the parent,...
bool m_dispatched
True when the current cycle recorded a dispatch.
void on_descriptors_created() override
Write the state_in / state_out descriptor bindings for the current front/back assignment.
void write_grid_extent_constants()
Size the push constant block to at least GridExtent and write the attached grid's dimensions into its...
const std::vector< uint8_t > & get_push_constant_data() const
Get current push constant data.
bool is_deferred_submission() const
Whether this processor submits asynchronously.
void set_push_constant_size()
Set push constant size from type.
bool are_descriptors_ready() const
Check if descriptors are initialized.
std::vector< Portal::Graphics::DescriptorSetID > m_descriptor_set_ids
void download_back_buffer(const VKBufferResources::GenerationSlot &slot, void *data, size_t size, std::shared_ptr< VKBuffer > &staging)
Download a raw back_buffers slot to host memory.
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
Leading push constant fields every relaxation rule shader receives: grid width then height,...
Describes how a VKBuffer binds to a shader descriptor.
std::unordered_map< std::string, ShaderBinding > bindings
Complete declarative description of a generated compute shader.