MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VertexAccess.hpp
Go to the documentation of this file.
1#pragma once
2#include "VertexLayout.hpp"
3
4namespace MayaFlux::Kakshya {
5
6/**
7 * @struct VertexAccessConfig
8 * @brief Default attribute values for shader-compatible vertex conversion.
9 *
10 * Applied when DataVariant contains only positional data and the target
11 * layout requires additional attributes (color, size, thickness, uv).
12 */
13struct MAYAFLUX_API VertexAccessConfig {
14 glm::vec3 default_color { 1.0F, 1.0F, 1.0F };
15 float default_size { 3.0F };
16 float default_thickness { 1.0F };
17 float default_weight { 0.0F };
18 glm::vec2 default_uv { 0.0F, 0.0F };
19 glm::vec3 default_normal { 0.0F, 0.0F, 1.0F };
20 glm::vec3 default_tangent { 1.0F, 0.0F, 0.0F };
21};
22
23/**
24 * @struct VertexAccess
25 * @brief Memory-compatible view of a DataVariant for vertex buffer upload.
26 *
27 * Mirrors TextureAccess: describes what the data IS in memory terms —
28 * pointer, byte count, and a VertexLayout that faithfully represents
29 * stride and attribute semantics — with no knowledge of Vulkan, Portal,
30 * or concrete vertex structs (PointVertex, LineVertex).
31 *
32 * Zero-copy cases (data_ptr points directly into variant storage):
33 * vector<glm::vec3> position-only vertices, stride = 12
34 * vector<glm::vec2> 2D position vertices, stride = 8
35 * vector<glm::vec4> position + W attribute, stride = 16
36 *
37 * Conversion cases (conversion_buffer holds promoted data):
38 * vector<float> Y-displacement waveform: x = normalised index,
39 * y = value, z = 0. Output: vec3, stride = 12.
40 * vector<double> Same as float, narrowed to float in output.
41 * vector<uint8_t> Values normalised to [-1, 1] as Y displacement.
42 * vector<uint16_t> Same normalisation.
43 * vector<uint32_t> Same normalisation.
44 * vector<complex<float>> magnitude as Y, phase normalised to [0,1] as Z.
45 * Output: vec3, stride = 12.
46 *
47 * Rejected types (as_vertex_access returns std::nullopt, logs error):
48 * vector<complex<double>> No standard waveform interpretation; caller must
49 * convert to float complex or extract components.
50 * vector<glm::mat4> Ambiguous layout; caller must unpack to vec4 first.
51 */
52struct MAYAFLUX_API VertexAccess {
53 const void* data_ptr = nullptr;
54 size_t byte_count = 0;
56
57 /**
58 * @brief True when data_ptr points into conversion_buffer rather than
59 * the original variant storage.
60 */
61 [[nodiscard]] bool was_converted() const noexcept
62 {
63 return !conversion_buffer.empty();
64 }
65
66 /**
67 * @brief Conversion buffer. Non-empty only when a type conversion was
68 * performed (scalar waveform → vec3, complex → vec3, etc.).
69 * Keeps converted data alive for the lifetime of this struct.
70 */
71 std::vector<std::byte> conversion_buffer;
72};
73
74/**
75 * @brief Extract a VertexAccess from a DataVariant.
76 *
77 * Inspects the active variant type and produces a VertexAccess whose
78 * layout describes the resulting vertex data. Scalar and complex types
79 * are converted to a positional vec3 waveform representation. GLM vector
80 * types are passed through without copying.
81 *
82 * @param variant Source data.
83 * @return Populated VertexAccess, or std::nullopt on incompatible type.
84 */
85[[nodiscard]] MAYAFLUX_API std::optional<VertexAccess>
86as_vertex_access(const DataVariant& variant);
87
88/**
89 * @brief Convert DataVariant to point-vertex-compatible bytes.
90 *
91 * Output layout matches VertexLayout::for_points(): stride 28,
92 * position (vec3, offset 0), color (vec3, offset 12), size (float, offset 24),
93 * uv (vec2, offset 28), normal (vec3, offset 36), tangent (vec3, offset 48).
94 * Compatible with point.vert.spv without any user-defined shaders.
95 *
96 * All accepted DataVariant types produce a position per element:
97 * GLM types map position directly; scalar and integer types produce
98 * a waveform (x=normalised index, y=value, z=0). Color and size are
99 * filled from config defaults throughout.
100 *
101 * @param variant Source data.
102 * @param config Default attribute values (color, size).
103 * @return Populated VertexAccess, or std::nullopt on incompatible type.
104 */
105[[nodiscard]] MAYAFLUX_API std::optional<VertexAccess>
107 const VertexAccessConfig& config = {});
108
109/**
110 * @brief Convert DataVariant to line-vertex-compatible bytes.
111 *
112 * Output layout matches VertexLayout::for_lines(): stride 36,
113 * position (vec3, offset 0), color (vec3, offset 12),
114 * thickness (float, offset 24), uv (vec2, offset 28),
115 * normal (vec3, offset 36), tangent (vec3, offset 48).
116 * Compatible with line.vert.spv without any user-defined shaders.
117 *
118 * @param variant Source data.
119 * @param config Default attribute values (color, thickness, uv).
120 * @return Populated VertexAccess, or std::nullopt on incompatible type.
121 */
122[[nodiscard]] MAYAFLUX_API std::optional<VertexAccess>
124 const VertexAccessConfig& config = {});
125
126/**
127 * @brief Convert DataVariant to mesh-vertex-compatible bytes.
128 *
129 * Output layout matches VertexLayout::for_meshes(): stride 60,
130 * position (vec3, offset 0), color (vec3, offset 12),
131 * weight (float, offset 24), uv (vec2, offset 28),
132 * normal (vec3, offset 36), tangent (vec3, offset 48).
133 * Compatible with mesh.vert.spv without any user-defined shaders.
134 *
135 * @param variant Source data.
136 * @param config Default attribute values (color, weight, uv).
137 * @return Populated VertexAccess, or std::nullopt on incompatible type.
138 */
139[[nodiscard]] MAYAFLUX_API std::optional<VertexAccess>
141 const VertexAccessConfig& config = {});
142
143} // namespace MayaFlux::Kakshya
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.
Definition NDData.hpp:76
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.
bool was_converted() const noexcept
True when data_ptr points into conversion_buffer rather than the original variant storage.
Memory-compatible view of a DataVariant for vertex buffer upload.
Complete description of vertex data layout in a buffer.