MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ComputeMeshBuffer.cpp
Go to the documentation of this file.
2
6
10
11namespace MayaFlux::Buffers {
12
13// ============================================================================
14// Construction
15// ============================================================================
16
19 const glm::vec3& bounds_min,
20 const glm::vec3& bounds_max,
21 uint32_t res_x,
22 uint32_t res_y,
23 uint32_t res_z,
24 float iso_level)
25 : VKBuffer(
26 worst_case_bytes(res_x, res_y, res_z),
27 Usage::VERTEX,
28 Kakshya::DataModality::VERTEX_POSITIONS_3D)
29 , m_res_x(std::max(res_x, 1U))
30 , m_res_y(std::max(res_y, 1U))
31 , m_res_z(std::max(res_z, 1U))
32 , m_field(std::move(field))
33 , m_bounds_min(bounds_min)
34 , m_bounds_max(bounds_max)
35 , m_iso_level(iso_level)
36{
38 "ComputeMeshBuffer: {}x{}x{} grid, {:.1f} MB worst-case",
40 static_cast<float>(get_size_bytes()) / (1024.F * 1024.F));
41}
42
44 const glm::vec3& bounds_min,
45 const glm::vec3& bounds_max,
46 uint32_t res_x,
47 uint32_t res_y,
48 uint32_t res_z,
49 float iso_level,
50 std::string field_shader)
51 : VKBuffer(
52 worst_case_bytes(res_x, res_y, res_z),
53 Usage::VERTEX,
54 Kakshya::DataModality::VERTEX_POSITIONS_3D)
55 , m_res_x(std::max(res_x, 1U))
56 , m_res_y(std::max(res_y, 1U))
57 , m_res_z(std::max(res_z, 1U))
58 , m_bounds_min(bounds_min)
59 , m_bounds_max(bounds_max)
60 , m_iso_level(iso_level)
61 , m_gpu_field(true)
62 , m_field_shader(std::move(field_shader))
63{
65 "ComputeMeshBuffer (GPU field): {}x{}x{} grid, {:.1f} MB worst-case",
67 static_cast<float>(get_size_bytes()) / (1024.F * 1024.F));
68}
69
70// ============================================================================
71// Processor setup
72// ============================================================================
73
75{
76 auto self = std::dynamic_pointer_cast<ComputeMeshBuffer>(shared_from_this());
77
79 layout.vertex_count = static_cast<uint32_t>(
81 set_vertex_layout(layout);
82
83 auto chain = get_processing_chain();
84 if (!chain) {
85 chain = std::make_shared<BufferProcessingChain>();
87 }
88 chain->set_preferred_token(token);
89
90 if (m_gpu_field) {
91 m_prep_processor = std::make_shared<SDFPrepProcessor>(m_res_x, m_res_y, m_res_z);
92 m_prep_processor->set_processing_token(token);
94
95 m_field_processor = std::make_shared<SDFFieldProcessor>(
96 m_prep_processor->grid_buf(),
100 m_field_processor->set_processing_token(token);
101 chain->add_preprocessor(m_field_processor, self);
102
103 m_sdf_processor = std::make_shared<SDFMeshProcessor>(
104 m_prep_processor->grid_buf(),
105 m_prep_processor->counter_buf(),
109
110 m_sdf_processor->set_processing_token(token);
111
112 chain->add_processor(m_sdf_processor, self);
113
114 } else {
115 m_sdf_processor = std::make_shared<SDFMeshProcessor>(
116 std::move(m_field),
120 m_sdf_processor->set_processing_token(token);
122 }
123}
124
126{
127 const bool textured = m_diffuse_texture != nullptr
128 || !config.default_texture_binding.empty();
129
130 RenderConfig resolved = config;
132
133 if (resolved.vertex_shader.empty())
134 resolved.vertex_shader = "triangle.vert.spv";
135
136 if (resolved.fragment_shader.empty()) {
137 resolved.fragment_shader = textured
138 ? "mesh_textured.frag.spv"
139 : "triangle.frag.spv";
140 }
141
142 ShaderConfig sc { resolved.vertex_shader };
143
144 if (textured) {
145 const std::string slot = resolved.default_texture_binding.empty()
147 : resolved.default_texture_binding;
148 sc.bindings[slot] = ShaderBinding(0, 1, vk::DescriptorType::eCombinedImageSampler);
149 }
150
151 apply_render_config(resolved, sc);
152
155
156 get_processing_chain()->add_final_processor(
157 m_render_processor, shared_from_this());
158
160 "ComputeMeshBuffer: rendering configured ({}textured)",
161 textured ? "" : "un");
162}
163
164// ============================================================================
165// Field and geometry control
166// ============================================================================
167
169{
170 if (m_sdf_processor) {
171 m_sdf_processor->set_field(std::move(field));
172 } else {
173 m_field = std::move(field);
174 }
175}
176
177void ComputeMeshBuffer::set_bounds(const glm::vec3& bounds_min, const glm::vec3& bounds_max)
178{
179 if (m_sdf_processor) {
180 m_sdf_processor->set_bounds(bounds_min, bounds_max);
181 } else {
182 m_bounds_min = bounds_min;
183 m_bounds_max = bounds_max;
184 }
185}
186
187void ComputeMeshBuffer::set_resolution(uint32_t res_x, uint32_t res_y, uint32_t res_z)
188{
189 m_res_x = std::max(res_x, 1U);
190 m_res_y = std::max(res_y, 1U);
191 m_res_z = std::max(res_z, 1U);
192 if (m_sdf_processor)
193 m_sdf_processor->set_resolution(m_res_x, m_res_y, m_res_z);
194}
195
197{
198 m_iso_level = iso_level;
199 if (m_sdf_processor)
200 m_sdf_processor->set_iso_level(iso_level);
201}
202
204{
205 if (m_sdf_processor)
206 m_sdf_processor->set_dirty();
207}
208
209// ============================================================================
210// Texture
211// ============================================================================
212
213void ComputeMeshBuffer::set_texture(std::shared_ptr<Core::VKImage> image, std::string binding)
214{
215 m_diffuse_texture = std::move(image);
216 m_diffuse_binding = std::move(binding);
217
220}
221
222} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
IO::ImageData image
Definition Decoder.cpp:57
void set_dirty()
Request a re-dispatch on the next graphics frame.
ComputeMeshBuffer(Kinesis::SpatialField field, const glm::vec3 &bounds_min, const glm::vec3 &bounds_max, uint32_t res_x, uint32_t res_y, uint32_t res_z, float iso_level=0.0F)
std::shared_ptr< SDFMeshProcessor > m_sdf_processor
std::shared_ptr< SDFFieldProcessor > m_field_processor
void set_bounds(const glm::vec3 &bounds_min, const glm::vec3 &bounds_max)
Replace the evaluation volume and mark dirty.
void set_texture(std::shared_ptr< Core::VKImage > image, std::string binding="diffuseTex")
Bind a diffuse texture sampled in the fragment shader.
void setup_rendering(const RenderConfig &config)
Create the RenderProcessor and attach it to the processing chain.
static size_t worst_case_bytes(uint32_t res_x, uint32_t res_y, uint32_t res_z) noexcept
std::shared_ptr< Core::VKImage > m_diffuse_texture
std::shared_ptr< SDFPrepProcessor > m_prep_processor
void set_iso_level(float iso_level)
Replace the iso_level threshold and mark dirty.
void setup_processors(ProcessingToken token) override
Setup processors with a processing token.
void set_resolution(uint32_t res_x, uint32_t res_y, uint32_t res_z)
Replace the grid resolution.
void set_field(Kinesis::SpatialField field)
Replace the scalar field and mark dirty.
void set_vertex_layout(const Kakshya::VertexLayout &layout)
Set vertex layout for this buffer.
Definition VKBuffer.cpp:410
std::shared_ptr< Buffers::BufferProcessingChain > get_processing_chain() override
Access the buffer's processing chain.
Definition VKBuffer.cpp:286
void set_default_processor(const std::shared_ptr< BufferProcessor > &processor) override
Set the buffer's default processor.
Definition VKBuffer.cpp:270
vk::DeviceSize get_size_bytes() const
Definition VKBuffer.hpp:279
void set_processing_chain(const std::shared_ptr< BufferProcessingChain > &chain, bool force=false) override
Replace the buffer's processing chain.
Definition VKBuffer.cpp:291
void apply_render_config(const RenderConfig &config, const ShaderConfig &shader_config)
Configure the internal m_render_processor from a RenderConfig.
Definition VKBuffer.cpp:359
std::shared_ptr< RenderProcessor > m_render_processor
Definition VKBuffer.hpp:618
Vulkan-backed buffer wrapper used in processing chains.
Definition VKBuffer.hpp:67
ProcessingToken
Bitfield enum defining processing characteristics and backend requirements for buffer operations.
@ Init
Engine/subsystem initialization.
@ Buffers
Buffers, Managers, processors and processing chains.
Describes how a VKBuffer binds to a shader descriptor.
Vertex type for indexed triangle mesh primitives (TRIANGLE_LIST topology)
static VertexLayout for_meshes(uint32_t stride=60)
Factory: layout for MeshVertex (position, color, weight, uv, normal, tangent)
Typed, composable, stateless callable from domain D to range R.
Definition Tendency.hpp:22
Unified rendering configuration for graphics buffers.