MayaFlux 0.5.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{
52 m_grid->swap_generation();
53 }
54
56}
57
59{
62 }
63
64 auto& data = get_push_constant_data();
65 if (data.size() < m_config.push_constant_size) {
66 data.resize(m_config.push_constant_size);
67 }
68
69 const GridExtent extent {
70 .width = m_grid->get_grid_width(),
71 .height = m_grid->get_grid_height()
72 };
73 std::memcpy(data.data(), &extent, sizeof(GridExtent));
74}
75
76void RelaxationStepProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
77{
79 m_grid = std::dynamic_pointer_cast<RelaxationGridBuffer>(buffer);
80
81 if (m_grid) {
82 set_workgroup_size(16, 16, 1);
84 uint32_t gx = (m_grid->get_grid_width() + 15) / 16;
85 uint32_t gy = (m_grid->get_grid_height() + 15) / 16;
86 set_manual_dispatch(gx, gy, 1);
88 }
89}
90
97
100 const std::shared_ptr<VKBuffer>& buffer)
101{
102 if (!std::dynamic_pointer_cast<RelaxationGridBuffer>(buffer)) {
103 return false;
104 }
105
107}
108
111 const std::shared_ptr<VKBuffer>& buffer)
112{
113 auto grid = std::dynamic_pointer_cast<RelaxationGridBuffer>(buffer);
114 if (!grid) {
115 return;
116 }
117
118 if (!grid->consume_snapshot_request()) {
119 return;
120 }
121
122 auto& resources = grid->get_buffer_resources();
123 const auto& front = resources.back_buffers[grid->front_index()];
124
125 std::vector<uint8_t> bytes(grid->get_state_bytes());
126
127 auto buffer_service = Registry::BackendRegistry::instance()
129
130 if (!m_snapshot_staging || m_snapshot_staging->get_size_bytes() < bytes.size()) {
132 }
133
134 auto handle = buffer_service->copy_buffer_fenced(
135 static_cast<void*>(front.buffer),
136 static_cast<void*>(m_snapshot_staging->get_buffer()),
137 bytes.size(), 0, 0);
138
139 buffer_service->wait_fenced(handle);
140
141 auto& staging_resources = m_snapshot_staging->get_buffer_resources();
142 buffer_service->invalidate_range(staging_resources.memory, 0, bytes.size());
143
144 std::memcpy(bytes.data(), staging_resources.mapped_ptr, bytes.size());
145 buffer_service->release_fenced(handle);
146
147 grid->snapshot_source()->signal(bytes);
148}
149
150} // namespace MayaFlux::Buffers
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_after_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer) override
Swap front/back generation index and, if requested, stage a snapshot download of the buffer just writ...
void on_attach(const std::shared_ptr< Buffer > &buffer) override
When attached to a RelaxationGridBuffer, sets up the dispatch configuration to cover the entire grid ...
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 staging.
bool on_before_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer) override
Write the state_in / state_out descriptor bindings for the current front/back assignment,...
std::shared_ptr< VKBuffer > m_snapshot_staging
Lazily-created, reused host-visible staging buffer for snapshot downloads.
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
Write the state descriptors for the current front/back assignment, then run the normal shader process...
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.
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
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
std::shared_ptr< VKBuffer > create_staging_buffer(size_t size)
Create staging buffer for transfers.
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.
Backend buffer management service interface.