9 void floats_to_waveform(std::span<const float> src, std::byte* dst)
11 const size_t count = src.size();
12 auto* out =
reinterpret_cast<glm::vec3*
>(dst);
13 const float inv = (
count > 1) ? (2.0F /
static_cast<float>(
count - 1)) : 0.0F;
15 for (
size_t i = 0; i <
count; ++i) {
16 out[i] = glm::vec3(
static_cast<float>(i) * inv - 1.0F, src[i], 0.0F);
20 VertexLayout position_only_layout(uint32_t
count)
23 layout.vertex_count =
count;
24 layout.stride_bytes =
sizeof(glm::vec3);
26 .offset_in_vertex = 0,
27 .name =
"position" });
31 VertexLayout position_2d_layout(uint32_t
count)
34 layout.vertex_count =
count;
35 layout.stride_bytes =
sizeof(glm::vec2);
37 .offset_in_vertex = 0,
38 .name =
"position2d" });
42 VertexLayout position_w_layout(uint32_t
count)
45 layout.vertex_count =
count;
46 layout.stride_bytes =
sizeof(glm::vec4);
48 .offset_in_vertex = 0,
49 .name =
"position_w" });
53 VertexLayout waveform_layout(uint32_t
count)
56 layout.vertex_count =
count;
57 layout.stride_bytes =
sizeof(glm::vec3);
59 .offset_in_vertex = 0,
60 .name =
"position" });
73 void write_point_vertices(
74 std::span<const glm::vec3> positions,
75 const VertexAccessConfig& cfg,
78 for (
size_t i = 0; i < positions.size(); ++i) {
79 std::byte* v = dst + i * 60;
80 std::memcpy(v, &positions[i], 12);
81 std::memcpy(v + 12, &cfg.default_color, 12);
82 std::memcpy(v + 24, &cfg.default_size, 4);
83 std::memcpy(v + 28, &cfg.default_uv, 8);
84 std::memcpy(v + 36, &cfg.default_normal, 12);
85 std::memcpy(v + 48, &cfg.default_tangent, 12);
94 void write_line_vertices(
95 std::span<const glm::vec3> positions,
96 const VertexAccessConfig& cfg,
99 for (
size_t i = 0; i < positions.size(); ++i) {
100 std::byte* v = dst + i * 60;
101 std::memcpy(v, &positions[i], 12);
102 std::memcpy(v + 12, &cfg.default_color, 12);
103 std::memcpy(v + 24, &cfg.default_thickness, 4);
104 std::memcpy(v + 28, &cfg.default_uv, 8);
105 std::memcpy(v + 36, &cfg.default_normal, 12);
106 std::memcpy(v + 48, &cfg.default_tangent, 12);
115 void write_mesh_vertices(
116 std::span<const glm::vec3> positions,
117 const VertexAccessConfig& cfg,
120 for (
size_t i = 0; i < positions.size(); ++i) {
121 std::byte* v = dst + i * 60;
122 std::memcpy(v, &positions[i], 12);
123 std::memcpy(v + 12, &cfg.default_color, 12);
124 std::memcpy(v + 24, &cfg.default_weight, 4);
125 std::memcpy(v + 28, &cfg.default_uv, 8);
126 std::memcpy(v + 36, &cfg.default_normal, 12);
127 std::memcpy(v + 48, &cfg.default_tangent, 12);
141 std::vector<glm::vec3> extract_positions(
const DataVariant& variant)
143 return std::visit([variant](
const auto& vec) -> std::vector<glm::vec3> {
144 using V =
typename std::decay_t<
decltype(vec)>::value_type;
145 const size_t count = vec.size();
151 if constexpr (std::is_same_v<V, glm::vec3>) {
152 return std::vector<glm::vec3>(vec.begin(), vec.end());
155 if constexpr (std::is_same_v<V, glm::vec2>) {
156 std::vector<glm::vec3> out(
count);
157 for (
size_t i = 0; i <
count; ++i) {
158 out[i] = glm::vec3(vec[i].x, vec[i].y, 0.0F);
163 if constexpr (std::is_same_v<V, glm::vec4>) {
164 std::vector<glm::vec3> out(
count);
165 for (
size_t i = 0; i <
count; ++i) {
166 out[i] = glm::vec3(vec[i].x, vec[i].y, vec[i].z);
171 if constexpr (DecimalData<V> || IntegerData<V>) {
172 std::vector<float> floats;
173 extract_from_variant<float>(variant, floats);
175 const float inv =
count > 1
176 ? 2.0F /
static_cast<float>(
count - 1)
179 std::vector<glm::vec3> out(
count);
180 for (
size_t i = 0; i <
count; ++i) {
181 out[i] = glm::vec3(
static_cast<float>(i) * inv - 1.0F,
187 if constexpr (std::is_same_v<V, std::complex<float>>) {
188 std::vector<float> magnitudes;
189 extract_from_variant<float>(variant, magnitudes,
192 constexpr float inv_pi = std::numbers::inv_pi_v<float>;
193 const float inv =
count > 1
194 ? 2.0F /
static_cast<float>(
count - 1)
197 std::vector<glm::vec3> out(
count);
198 for (
size_t i = 0; i <
count; ++i) {
199 out[i] = glm::vec3(
static_cast<float>(i) * inv - 1.0F,
201 std::arg(vec[i]) * inv_pi);
216 return std::visit([&variant](
const auto& vec) -> std::optional<VertexAccess> {
217 using V =
typename std::decay_t<
decltype(vec)>::value_type;
218 const size_t count = vec.size();
222 "as_vertex_access: empty variant");
226 if constexpr (std::is_same_v<V, glm::vec3>) {
228 position_only_layout(
static_cast<uint32_t
>(
count)) };
231 if constexpr (std::is_same_v<V, glm::vec2>) {
233 position_2d_layout(
static_cast<uint32_t
>(
count)) };
236 if constexpr (std::is_same_v<V, glm::vec4>) {
238 position_w_layout(
static_cast<uint32_t
>(
count)) };
241 if constexpr (DecimalData<V> || IntegerData<V>) {
242 if constexpr (std::is_same_v<V, double>) {
244 "as_vertex_access: double narrowed to float for vertex waveform");
247 std::vector<float> floats;
248 extract_from_variant<float>(variant, floats);
255 va.
layout = waveform_layout(
static_cast<uint32_t
>(
count));
259 if constexpr (std::is_same_v<V, std::complex<float>>) {
260 std::vector<float> magnitudes;
261 extract_from_variant<float>(variant, magnitudes,
264 constexpr float inv_pi = std::numbers::inv_pi_v<float>;
265 const float inv = (
count > 1) ? (2.0F /
static_cast<float>(
count - 1)) : 0.0F;
271 for (
size_t i = 0; i <
count; ++i) {
273 static_cast<float>(i) * inv - 1.0F,
275 std::arg(vec[i]) * inv_pi);
280 va.
layout = waveform_layout(
static_cast<uint32_t
>(
count));
284 if constexpr (std::is_same_v<V, std::complex<double>>) {
286 "as_vertex_access: complex<double> rejected: convert to "
287 "complex<float> or extract components manually");
291 if constexpr (std::is_same_v<V, glm::mat4>) {
293 "as_vertex_access: glm::mat4 rejected: unpack to vec4 columns first");
306 auto positions = extract_positions(variant);
307 if (positions.empty()) {
309 "as_point_vertex_access: unsupported or empty variant");
313 const auto count =
static_cast<uint32_t
>(positions.size());
329 auto positions = extract_positions(variant);
330 if (positions.empty()) {
332 "as_line_vertex_access: unsupported or empty variant");
336 const auto count =
static_cast<uint32_t
>(positions.size());
351 auto positions = extract_positions(variant);
352 if (positions.empty()) {
354 "as_mesh_vertex_access: unsupported or empty variant");
358 const auto count =
static_cast<uint32_t
>(positions.size());
#define MF_ERROR(comp, ctx,...)
#define MF_TRACE(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
@ Runtime
General runtime operations (default fallback)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
std::optional< VertexAccess > as_line_vertex_access(const DataVariant &variant, const VertexAccessConfig &config)
Convert DataVariant to line-vertex-compatible bytes.
std::optional< VertexAccess > as_mesh_vertex_access(const DataVariant &variant, const VertexAccessConfig &config)
Convert DataVariant to mesh-vertex-compatible bytes.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
@ MAGNITUDE
|z| = sqrt(real² + imag²)
std::optional< VertexAccess > as_vertex_access(const DataVariant &variant)
Extract a VertexAccess from a DataVariant.
std::optional< VertexAccess > as_point_vertex_access(const DataVariant &variant, const VertexAccessConfig &config)
Convert DataVariant to point-vertex-compatible bytes.
Default attribute values for shader-compatible vertex conversion.
std::vector< std::byte > conversion_buffer
Conversion buffer.
Memory-compatible view of a DataVariant for vertex buffer upload.
uint32_t vertex_count
Total number of vertices in this buffer.
static VertexLayout for_lines(uint32_t stride=60)
Factory: layout for LineVertex (position, color, thickness, uv, normal, tangent)
static VertexLayout for_meshes(uint32_t stride=60)
Factory: layout for MeshVertex (position, color, weight, uv, normal, tangent)
static VertexLayout for_points(uint32_t stride=60)
Factory: layout for PointVertex (position, color, size, uv, normal, tangent)