MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VertexInsertion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "VertexLayout.hpp"
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @class VertexInsertion
9 * @brief Write counterpart to VertexAccess: splits interleaved vertex bytes into typed channels.
10 *
11 * Parallel to MeshInsertion and DataInsertion: holds a mutable reference to a
12 * channel list and populates it. Each VertexLayout attribute becomes one
13 * DataVariant, in layout order, holding the attribute for every vertex:
14 *
15 * VERTEX_POSITIONS_3D, NORMALS_3D, TANGENTS_3D, COLORS_RGB -> vector<glm::vec3>
16 * TEXTURE_COORDS_2D -> vector<glm::vec2>
17 * VERTEX_COLORS_RGBA -> vector<glm::vec4>
18 * SCALAR_F32 -> vector<float>
19 *
20 * For the canonical point, line and mesh layouts the channel order matches the
21 * slot order as_point_vertex_access() and its siblings consume, so a read
22 * channel list can be written back without reordering.
23 *
24 * Nothing is written unless the layout and byte count are both valid.
25 *
26 * Usage:
27 * @code
28 * std::vector<DataVariant> channels;
29 * VertexInsertion ins(channels);
30 * if (ins.insert_interleaved(bytes, layout)) {
31 * auto& positions = std::get<std::vector<glm::vec3>>(channels[0]);
32 * }
33 * @endcode
34 */
35class MAYAFLUX_API VertexInsertion {
36public:
37 /**
38 * @brief Construct with a mutable reference to the channel list.
39 * @param channels Receives one typed DataVariant per layout attribute.
40 */
41 explicit VertexInsertion(std::vector<DataVariant>& channels);
42
43 /**
44 * @brief Whether a layout can be split into typed channels.
45 *
46 * Requires a nonzero stride, at least one attribute, a supported modality
47 * per attribute, and every attribute lying within one stride.
48 */
49 [[nodiscard]] static bool can_decode(const VertexLayout& layout) noexcept;
50
51 /**
52 * @brief Replace the channel list with the attributes of interleaved vertices.
53 *
54 * The vertex count is derived from the byte count, not from
55 * layout.vertex_count, so a layout describing capacity cannot over-read.
56 *
57 * @param vertex_bytes Interleaved vertices; size must be a nonzero multiple of layout.stride_bytes.
58 * @param layout Attribute offsets and stride of @p vertex_bytes.
59 * @return False, with an error logged and the channel list untouched, when the layout or byte count is invalid.
60 */
61 bool insert_interleaved(
62 std::span<const uint8_t> vertex_bytes,
63 const VertexLayout& layout);
64
65 /**
66 * @brief Empty the channel list and forget the last layout.
67 */
68 void clear();
69
70 /**
71 * @brief Layout of the last successful insertion, with vertex_count set to the decoded count.
72 */
73 [[nodiscard]] const VertexLayout& layout() const noexcept { return m_layout; }
74
75 /**
76 * @brief Number of vertices decoded by the last successful insertion.
77 */
78 [[nodiscard]] uint32_t vertex_count() const noexcept { return m_layout.vertex_count; }
79
80private:
81 std::vector<DataVariant>& m_channels;
83};
84
85} // namespace MayaFlux::Kakshya
uint32_t channels
uint32_t vertex_count() const noexcept
Number of vertices decoded by the last successful insertion.
std::vector< DataVariant > & m_channels
const VertexLayout & layout() const noexcept
Layout of the last successful insertion, with vertex_count set to the decoded count.
Write counterpart to VertexAccess: splits interleaved vertex bytes into typed channels.
Complete description of vertex data layout in a buffer.