MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RaymarchProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace MayaFlux::Buffers {
7
8/**
9 * @class RaymarchProcessor
10 * @brief Stages a scalar field and its march parameters onto the buffer a
11 * RenderProcessor draws, so the fragment stage integrates the field
12 * through a proxy volume.
13 *
14 * Dispatches nothing and records no commands. Each cycle it resolves a
15 * vk::Buffer from its field source, writes a DescriptorBindingInfo for it
16 * into the attached buffer's pipeline context, and writes its parameter
17 * block as a staged push constant fragment. RenderProcessor unifies both
18 * into the pipeline it builds and updates the descriptor before binding
19 * the set, which is the designed path for handing resources to the
20 * renderer without a processor of its own.
21 *
22 * The field source is a callable rather than a handle because a
23 * double-buffered field's read slot moves whenever an upstream stage
24 * swaps. Calling it fresh each cycle picks that up. It is also what keeps
25 * this class free of any dependency on VolumeGridBuffer: any callable
26 * returning a valid handle to a float array of the lattice's cell count
27 * works, whatever owns it.
28 *
29 * Set 1 binding 0 by default. Set 0 is the engine's ViewTransform
30 * reservation and cannot be used.
31 */
32class MAYAFLUX_API RaymarchProcessor : public VKBufferProcessor {
33public:
34 /**
35 * @brief Callable resolving the handle to sample this cycle.
36 *
37 * Returning a null handle skips the cycle's staging, leaving the
38 * previous cycle's descriptor in place.
39 */
40 using FieldSource = std::function<vk::Buffer()>;
41
42 /**
43 * @struct MarchParams
44 * @brief Parameter block staged as a push constant fragment.
45 *
46 * Offsets are fixed by this declaration. The fragment is staged at
47 * offset zero, so these are absolute offsets in the render pipeline's
48 * push constant range.
49 */
50 struct MarchParams {
51 uint32_t width;
52 uint32_t height;
53 uint32_t depth;
54 uint32_t max_steps;
63 float cool_r;
64 float cool_g;
65 float cool_b;
67 float hot_r;
68 float hot_g;
69 float hot_b;
70 float emission;
71 float threshold;
72 float pad0;
73 float pad1;
74 float pad2;
75 };
76
77 static_assert(sizeof(MarchParams) == 96);
78 static_assert(sizeof(MarchParams) % 16 == 0);
79
80 /**
81 * @brief Construct a march staging processor.
82 * @param source Callable resolving the field handle each cycle.
83 * @param field_bytes Byte size of one slot of that field.
84 * @param lattice Discretization the field is stored over. Must match
85 * the lattice the field was allocated against.
86 * @param set Descriptor set index. Must not be zero.
87 * @param binding Binding index within that set.
88 */
90 FieldSource source,
91 size_t field_bytes,
92 Kinesis::Lattice3D lattice,
93 uint32_t set = 1,
94 uint32_t binding = 0);
95
96 ~RaymarchProcessor() override = default;
97
98 /**
99 * @brief Set the sample count along the longest ray through the volume.
100 * @param steps Step count. Higher resolves thin structure, linearly
101 * more expensive per covered pixel.
102 */
103 void set_max_steps(uint32_t steps);
104
105 /**
106 * @brief Set the step length as a fraction of one cell.
107 * @param scale Below one oversamples, above one undersamples and bands.
108 */
109 void set_step_scale(float scale);
110
111 /**
112 * @brief Set the multiplier applied to every sample before integration.
113 * @param scale Raises or lowers apparent opacity without touching the field.
114 */
115 void set_density_scale(float scale);
116
117 /**
118 * @brief Set the extinction coefficient.
119 * @param absorption Higher makes the volume opaque in a shorter distance.
120 */
121 void set_absorption(float absorption);
122
123 /**
124 * @brief Set the emission colours interpolated by sample value.
125 * @param cool Colour at the threshold.
126 * @param hot Colour at and above unity.
127 */
128 void set_emission_ramp(const glm::vec3& cool, const glm::vec3& hot);
129
130 /**
131 * @brief Set the emissive strength.
132 * @param emission Zero gives pure absorption, a density shadow.
133 */
134 void set_emission(float emission);
135
136 /**
137 * @brief Set the sample value below which a step contributes nothing.
138 * @param threshold Cutoff. Suppresses the smeared tail advection leaves.
139 */
140 void set_threshold(float threshold);
141
142 /** @brief The lattice the sampled field is stored over. */
143 [[nodiscard]] const Kinesis::Lattice3D& get_lattice() const { return m_lattice; }
144
145 /** @brief The staged parameter block. */
146 [[nodiscard]] const MarchParams& get_params() const { return m_params; }
147
148 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
149 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
150
151private:
152 /**
153 * @brief Write the lattice-derived words of the parameter block.
154 */
155 void write_lattice_params();
156
157 /**
158 * @brief Insert or replace the descriptor entry for the field handle.
159 * @param buffer Buffer whose pipeline context is staged onto.
160 * @param handle Handle resolved this cycle.
161 */
162 void stage_descriptor(const std::shared_ptr<VKBuffer>& buffer, vk::Buffer handle);
163
164 /**
165 * @brief Insert or replace the push constant fragment.
166 * @param buffer Buffer whose pipeline context is staged onto.
167 */
168 void stage_params(const std::shared_ptr<VKBuffer>& buffer);
169
173 uint32_t m_set;
174 uint32_t m_binding;
175
176 MarchParams m_params {};
177};
178
179} // namespace MayaFlux::Buffers
float scale
float threshold
const MarchParams & get_params() const
The staged parameter block.
std::function< vk::Buffer()> FieldSource
Callable resolving the handle to sample this cycle.
const Kinesis::Lattice3D & get_lattice() const
The lattice the sampled field is stored over.
Stages a scalar field and its march parameters onto the buffer a RenderProcessor draws,...
Parameter block staged as a push constant fragment.
A regular subdivision of an AABB3D into a cell count per axis.
Definition Lattice.hpp:25