MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VertexInsertion.cpp
Go to the documentation of this file.
1#include "VertexInsertion.hpp"
2
4
5namespace MayaFlux::Kakshya {
6
7namespace {
8
9 template <typename T>
10 DataVariant gather(
11 std::span<const uint8_t> bytes,
12 uint32_t stride,
13 uint32_t offset,
14 size_t count)
15 {
16 std::vector<T> out(count);
17 for (size_t i = 0; i < count; ++i) {
18 std::memcpy(&out[i], bytes.data() + i * stride + offset, sizeof(T));
19 }
20 return out;
21 }
22
23 size_t element_size(DataModality modality) noexcept
24 {
25 switch (modality) {
30 return sizeof(glm::vec3);
32 return sizeof(glm::vec2);
34 return sizeof(glm::vec4);
36 return sizeof(float);
37 default:
38 return 0;
39 }
40 }
41
42 DataVariant gather_attribute(
43 DataModality modality,
44 std::span<const uint8_t> bytes,
45 uint32_t stride,
46 uint32_t offset,
47 size_t count)
48 {
49 switch (modality) {
51 return gather<glm::vec2>(bytes, stride, offset, count);
53 return gather<glm::vec4>(bytes, stride, offset, count);
55 return gather<float>(bytes, stride, offset, count);
56 default:
57 return gather<glm::vec3>(bytes, stride, offset, count);
58 }
59 }
60
61 const char* layout_fault(const VertexLayout& layout) noexcept
62 {
63 if (layout.stride_bytes == 0) {
64 return "layout stride_bytes is zero";
65 }
66 if (layout.attributes.empty()) {
67 return "layout has no attributes";
68 }
69 for (const auto& attr : layout.attributes) {
70 const size_t size = element_size(attr.component_modality);
71 if (size == 0) {
72 return "layout attribute has a modality with no vertex channel type";
73 }
74 if (static_cast<size_t>(attr.offset_in_vertex) + size > layout.stride_bytes) {
75 return "layout attribute extends past the stride";
76 }
77 }
78 return nullptr;
79 }
80
81} // namespace
82
84 : m_channels(channels)
85{
86}
87
88bool VertexInsertion::can_decode(const VertexLayout& layout) noexcept
89{
90 return layout_fault(layout) == nullptr;
91}
92
94 std::span<const uint8_t> vertex_bytes,
95 const VertexLayout& layout)
96{
97 if (const char* fault = layout_fault(layout)) {
99 "VertexInsertion::insert_interleaved: {}", fault);
100 return false;
101 }
102 if (vertex_bytes.empty() || vertex_bytes.size() % layout.stride_bytes != 0) {
104 "VertexInsertion::insert_interleaved: {} bytes is not a nonzero multiple of stride {}",
105 vertex_bytes.size(), layout.stride_bytes);
106 return false;
107 }
108
109 const size_t count = vertex_bytes.size() / layout.stride_bytes;
110
111 m_channels.clear();
112 m_channels.reserve(layout.attributes.size());
113 for (const auto& attr : layout.attributes) {
114 m_channels.push_back(gather_attribute(
115 attr.component_modality, vertex_bytes,
116 layout.stride_bytes, attr.offset_in_vertex, count));
117 }
118
120 m_layout.vertex_count = static_cast<uint32_t>(count);
121
123 "VertexInsertion::insert_interleaved: {} vertices into {} channels",
124 count, m_channels.size());
125 return true;
126}
127
129{
130 m_channels.clear();
131 m_layout = {};
132}
133
134} // namespace MayaFlux::Kakshya
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
uint64_t offset
Definition Composite.cpp:10
bool size
std::vector< SpatialAttribute > attributes
std::vector< float > * out
size_t count
uint32_t channels
static bool can_decode(const VertexLayout &layout) noexcept
Whether a layout can be split into typed channels.
void clear()
Empty the channel list and forget the last layout.
VertexInsertion(std::vector< DataVariant > &channels)
Construct with a mutable reference to the channel list.
std::vector< DataVariant > & m_channels
const VertexLayout & layout() const noexcept
Layout of the last successful insertion, with vertex_count set to the decoded count.
bool insert_interleaved(std::span< const uint8_t > vertex_bytes, const VertexLayout &layout)
Replace the channel list with the attributes of interleaved vertices.
@ Runtime
General runtime operations (default fallback)
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
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
DataModality
Data modality types for cross-modal analysis.
Definition NDData.hpp:132
@ SCALAR_F32
Single-channel float data.
uint32_t stride_bytes
Total bytes per vertex (stride in Vulkan terms) e.g., 3 floats (position) + 3 floats (normal) = 24 by...
uint32_t vertex_count
Total number of vertices in this buffer.
std::vector< VertexAttributeLayout > attributes
All attributes that make up one vertex Ordered by shader location (0, 1, 2, ...)
Complete description of vertex data layout in a buffer.