MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SDFFieldProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7namespace MayaFlux::Buffers {
8
9/**
10 * @class SDFFieldProcessor
11 * @brief Preprocessor that evaluates an SDF field into a corner grid on the GPU.
12 *
13 * Dispatches sdf_field.comp, which includes sdf_primitives.glsl and writes
14 * one float per grid corner into the grid buffer owned by SDFPrepProcessor.
15 * SDFMeshProcessor (flat slot) reads this buffer as its SDF input.
16 *
17 * Receives the grid buffer at construction so it can bind it without
18 * depending on the attached buffer's own storage.
19 *
20 * Push constant layout (must match sdf_field.comp exactly, 48 bytes):
21 * offset 0 vec3 bounds_min
22 * offset 12 float time
23 * offset 16 vec3 step
24 * offset 28 uint res_x
25 * offset 32 uint res_y
26 * offset 36 uint res_z
27 * offset 40 uint _pad[2]
28 *
29 * Usage — wired by ComputeMeshBuffer::setup_processors when GPU field mode
30 * is active:
31 * @code
32 * chain->add_preprocessor(m_field_processor, self);
33 * @endcode
34 */
35class MAYAFLUX_API SDFFieldProcessor : public ComputeProcessor {
36public:
37 /**
38 * @param grid_buf HOST_STORAGE buffer sized to (res_x+1)*(res_y+1)*(res_z+1) floats.
39 * Owned and zeroed each frame by SDFPrepProcessor.
40 * @param bounds_min World-space minimum corner.
41 * @param bounds_max World-space maximum corner.
42 * @param res_x Cell count along X.
43 * @param res_y Cell count along Y.
44 * @param res_z Cell count along Z.
45 */
47 std::shared_ptr<VKBuffer> grid_buf,
48 const glm::vec3& bounds_min,
49 const glm::vec3& bounds_max,
50 uint32_t res_x,
51 uint32_t res_y,
52 uint32_t res_z,
53 std::string shader = "sdf_field_gyroid.comp.spv");
54
55 /**
56 * @brief Evaluate an authored field function instead of a shader file.
57 *
58 * The shader is generated from @p field: its function is emitted as written
59 * and a kernel body calls it at every grid corner. Time and the parameter
60 * block reach it through set_time() and set_params(), so animating a field
61 * recompiles nothing.
62 *
63 * @param grid_buf HOST_STORAGE buffer sized to (res_x+1)*(res_y+1)*(res_z+1) floats.
64 * @param field Parsed field function, from MF_FIELD, MF_FIELD_T or MF_FIELD_P.
65 * @param bounds_min World-space minimum corner.
66 * @param bounds_max World-space maximum corner.
67 * @param res_x Cell count along X.
68 * @param res_y Cell count along Y.
69 * @param res_z Cell count along Z.
70 */
72 std::shared_ptr<VKBuffer> grid_buf,
73 const Kinesis::FieldSource& field,
74 const glm::vec3& bounds_min,
75 const glm::vec3& bounds_max,
76 uint32_t res_x,
77 uint32_t res_y,
78 uint32_t res_z);
79
80 ~SDFFieldProcessor() override = default;
81
82 /** @brief Replace the evaluation volume. */
83 void set_bounds(const glm::vec3& bounds_min, const glm::vec3& bounds_max);
84
85 /** @brief Advance the time uniform (drives animation in sdf_field.comp). */
86 void set_time(float t)
87 {
88 m_pc.time = t;
89 m_field_pc.time = t;
90 }
91 [[nodiscard]] float get_time() const { return m_pc.time; }
92
93 /** @brief Set the extra float parameters **/
94 void set_param0(float v) { m_pc._pad[0] = std::bit_cast<uint32_t>(v); }
95 void set_param1(float v) { m_pc._pad[1] = std::bit_cast<uint32_t>(v); }
96
97 /** @brief Set the parameter block passed to a parametric field. */
98 void set_params(const glm::vec4& params)
99 {
100 m_field_pc.params[0] = params.x;
101 m_field_pc.params[1] = params.y;
102 m_field_pc.params[2] = params.z;
103 m_field_pc.params[3] = params.w;
104 }
105
106protected:
107 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
108 bool on_before_execute(
110 const std::shared_ptr<VKBuffer>& buffer) override;
111
112private:
113 struct PC {
114 glm::vec3 bounds_min;
115 float time { 0.0F };
116 glm::vec3 step;
117 uint32_t res_x;
118 uint32_t res_y;
119 uint32_t res_z;
120 uint32_t _pad[2] {};
121 };
122 static_assert(sizeof(PC) % 16 == 0);
123
124 struct FieldPC {
125 float bounds_min[3] {};
126 float time { 0.0F };
127 float step[3] {};
128 uint32_t res_x {};
129 uint32_t res_y {};
130 uint32_t res_z {};
131 float params[4] {};
132 };
133 static_assert(sizeof(FieldPC) == 56);
134
137 bool m_from_field { false };
138 std::shared_ptr<VKBuffer> m_grid_buf;
139 uint32_t m_res_x;
140 uint32_t m_res_y;
141 uint32_t m_res_z;
142
144};
145
146} // namespace MayaFlux::Buffers
std::shared_ptr< NetworkGeometryBuffer > buffer
Specialized ShaderProcessor for Compute Pipelines.
void set_time(float t)
Advance the time uniform (drives animation in sdf_field.comp).
void set_params(const glm::vec4 &params)
Set the parameter block passed to a parametric field.
std::shared_ptr< VKBuffer > m_grid_buf
void set_param0(float v)
Set the extra float parameters.
Preprocessor that evaluates an SDF field into a corner grid on the GPU.
Parsed representation of a stringified field lambda.