MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VertexAccess.cpp
Go to the documentation of this file.
1#include "VertexAccess.hpp"
2
4
5namespace MayaFlux::Kakshya {
6
7namespace {
8
9 constexpr size_t k_vertex_bytes = 68;
10
11 // Canonical field layout: vec3 pos(12) | vec4 color(16) | float scalar(4) | vec2 uv(8) | vec3 normal(12) | vec4 tangent(16)
12 // Channel slot -> (offset, size)
13 constexpr std::array<std::pair<size_t, size_t>, 6> k_channel_layout { {
14 { 0, 12 }, // vec3 position
15 { 12, 16 }, // vec4 color
16 { 28, 4 }, // float scalar
17 { 32, 8 }, // vec2 uv
18 { 40, 12 }, // vec3 normal
19 { 52, 16 }, // vec4 tangent
20 } };
21
22 /**
23 * Returns (data pointer, element count, element size in bytes) for a channel variant.
24 * Element size is the sizeof of the stored type, not the canonical field size.
25 */
26 struct ChannelView {
27 const uint8_t* ptr;
28 size_t count;
30 };
31
32 ChannelView channel_view(const DataVariant& v)
33 {
34 return std::visit([](const auto& vec) -> ChannelView {
35 using V = typename std::decay_t<decltype(vec)>::value_type;
36 if (vec.empty())
37 return { .ptr = nullptr, .count = 0, .element_bytes = sizeof(V) };
38 return {
39 reinterpret_cast<const uint8_t*>(vec.data()),
40 vec.size(),
41 sizeof(V)
42 };
43 },
44 v);
45 }
46
47 void assemble_vertices(
48 std::span<const DataVariant> channels,
49 size_t count,
50 const VertexAccessConfig& cfg,
51 float slot24_default,
52 std::byte* dst)
53 {
54 const glm::vec4 default_tangent(cfg.default_tangent, 1.0F);
55 const void* defaults[6] = {
56 nullptr,
57 &cfg.default_color,
58 &slot24_default,
59 &cfg.default_uv,
60 &cfg.default_normal,
61 &default_tangent,
62 };
63
64 for (size_t i = 0; i < count; ++i) {
65 std::byte* v = dst + i * k_vertex_bytes;
66 for (size_t s = 0; s < 6; ++s) {
67 auto [dst_off, dst_sz] = k_channel_layout[s];
68 if (s < channels.size()) {
69 auto cv = channel_view(channels[s]);
70 // copy min(src element size, dst field size) bytes, zero-pad remainder
71 const size_t copy_sz = std::min(cv.element_bytes, dst_sz);
72 std::memcpy(v + dst_off, cv.ptr + i * cv.element_bytes, copy_sz);
73 if (copy_sz < dst_sz) {
74 std::memset(v + dst_off + copy_sz, 0, dst_sz - copy_sz);
75 if ((s == 1 || s == 5) && copy_sz == sizeof(glm::vec3)) {
76 const float opaque = 1.0F;
77 std::memcpy(v + dst_off + copy_sz, &opaque, sizeof(float));
78 }
79 }
80 } else {
81 std::memcpy(v + dst_off, defaults[s], dst_sz);
82 }
83 }
84 }
85 }
86
87} // namespace
88
89std::optional<VertexAccess> as_vertex_access(
90 std::span<const DataVariant> channels,
91 const VertexAccessConfig& config)
92{
93 return as_point_vertex_access(channels, config);
94}
95
96std::optional<VertexAccess> as_point_vertex_access(
97 std::span<const DataVariant> channels,
98 const VertexAccessConfig& config)
99{
100 if (channels.empty()) {
102 "as_point_vertex_access: no channels supplied");
103 return std::nullopt;
104 }
105
106 // Zero-copy path: single uint8_t channel of N*68 bytes is already interleaved.
107 if (channels.size() == 1) {
108 if (const auto* b = std::get_if<std::vector<uint8_t>>(&channels[0])) {
109 if (b->size() % k_vertex_bytes != 0) {
111 "as_point_vertex_access: uint8_t byte count {} not a multiple of 68",
112 b->size());
113 return std::nullopt;
114 }
115 const auto count = static_cast<uint32_t>(b->size() / k_vertex_bytes);
116 auto layout = VertexLayout::for_points();
117 layout.vertex_count = count;
118 return VertexAccess { .data_ptr = b->data(), .byte_count = b->size(), .layout = layout };
119 }
120 }
121
122 const size_t count = channel_view(channels[0]).count;
123 if (count == 0) {
125 "as_point_vertex_access: position channel (slot 0) is empty or unsupported");
126 return std::nullopt;
127 }
128
129 VertexAccess va;
130 va.conversion_buffer.resize(count * k_vertex_bytes);
131 assemble_vertices(channels, count, config, config.default_size,
132 va.conversion_buffer.data());
133 va.data_ptr = va.conversion_buffer.data();
134 va.byte_count = va.conversion_buffer.size();
136 va.layout.vertex_count = static_cast<uint32_t>(count);
137 return va;
138}
139
140std::optional<VertexAccess> as_line_vertex_access(
141 std::span<const DataVariant> channels,
142 const VertexAccessConfig& config)
143{
144 if (channels.empty()) {
146 "as_line_vertex_access: no channels supplied");
147 return std::nullopt;
148 }
149
150 if (channels.size() == 1) {
151 if (const auto* b = std::get_if<std::vector<uint8_t>>(&channels[0])) {
152 if (b->size() % k_vertex_bytes != 0) {
154 "as_line_vertex_access: uint8_t byte count {} not a multiple of 68",
155 b->size());
156 return std::nullopt;
157 }
158 const auto count = static_cast<uint32_t>(b->size() / k_vertex_bytes);
159 auto layout = VertexLayout::for_lines();
160 layout.vertex_count = count;
161 return VertexAccess { .data_ptr = b->data(), .byte_count = b->size(), .layout = layout };
162 }
163 }
164
165 const size_t count = channel_view(channels[0]).count;
166 if (count == 0) {
168 "as_line_vertex_access: position channel (slot 0) is empty or unsupported");
169 return std::nullopt;
170 }
171
172 VertexAccess va;
173 va.conversion_buffer.resize(count * k_vertex_bytes);
174 assemble_vertices(channels, count, config, config.default_thickness,
175 va.conversion_buffer.data());
176 va.data_ptr = va.conversion_buffer.data();
177 va.byte_count = va.conversion_buffer.size();
179 va.layout.vertex_count = static_cast<uint32_t>(count);
180 return va;
181}
182
183std::optional<VertexAccess> as_mesh_vertex_access(
184 std::span<const DataVariant> channels,
185 const VertexAccessConfig& config)
186{
187 if (channels.empty()) {
189 "as_mesh_vertex_access: no channels supplied");
190 return std::nullopt;
191 }
192
193 if (channels.size() == 1) {
194 if (const auto* b = std::get_if<std::vector<uint8_t>>(&channels[0])) {
195 if (b->size() % k_vertex_bytes != 0) {
197 "as_mesh_vertex_access: uint8_t byte count {} not a multiple of 68",
198 b->size());
199 return std::nullopt;
200 }
201 const auto count = static_cast<uint32_t>(b->size() / k_vertex_bytes);
202 auto layout = VertexLayout::for_meshes();
203 layout.vertex_count = count;
204 return VertexAccess { .data_ptr = b->data(), .byte_count = b->size(), .layout = layout };
205 }
206 }
207
208 const size_t count = channel_view(channels[0]).count;
209 if (count == 0) {
211 "as_mesh_vertex_access: position channel (slot 0) is empty or unsupported");
212 return std::nullopt;
213 }
214
215 VertexAccess va;
216 va.conversion_buffer.resize(count * k_vertex_bytes);
217 assemble_vertices(channels, count, config, config.default_weight,
218 va.conversion_buffer.data());
219 va.data_ptr = va.conversion_buffer.data();
220 va.byte_count = va.conversion_buffer.size();
222 va.layout.vertex_count = static_cast<uint32_t>(count);
223 return va;
224}
225
226} // namespace MayaFlux::Kakshya
#define MF_ERROR(comp, ctx,...)
uint32_t * dst
size_t b
size_t count
const uint8_t * ptr
size_t element_bytes
uint32_t channels
@ Runtime
General runtime operations (default fallback)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
std::optional< VertexAccess > as_line_vertex_access(std::span< const DataVariant > channels, const VertexAccessConfig &config)
Assemble line-vertex-compatible bytes from one or more data channels.
std::optional< VertexAccess > as_point_vertex_access(std::span< const DataVariant > channels, const VertexAccessConfig &config)
Assemble point-vertex-compatible bytes from one or more data channels.
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.
Definition NDData.hpp:70
std::optional< VertexAccess > as_mesh_vertex_access(std::span< const DataVariant > channels, const VertexAccessConfig &config)
Assemble mesh-vertex-compatible bytes from one or more data channels.
std::optional< VertexAccess > as_vertex_access(std::span< const DataVariant > channels, const VertexAccessConfig &config)
Extract a VertexAccess from a DataVariant.
Default attribute values for shader-compatible vertex conversion.
std::vector< std::byte > conversion_buffer
Conversion buffer.
Memory-compatible view of channel data assembled into full 68-byte vertices.
uint32_t vertex_count
Total number of vertices in this buffer.
static VertexLayout for_points(uint32_t stride=68)
Factory: layout for PointVertex (position, color, size, uv, normal, tangent)
static VertexLayout for_meshes(uint32_t stride=68)
Factory: layout for MeshVertex (position, color, weight, uv, normal, tangent)
static VertexLayout for_lines(uint32_t stride=68)
Factory: layout for LineVertex (position, color, thickness, uv, normal, tangent)