MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextureAccess.cpp
Go to the documentation of this file.
1#include "TextureAccess.hpp"
2
4
5namespace MayaFlux::Kakshya {
6
7//==============================================================================
8// TextureAccess helpers
9//==============================================================================
10
11uint32_t TextureAccess::channel_count() const noexcept
12{
13 switch (format) {
20 return 1;
23 return 2;
26 return 3;
29 return 4;
30 default:
31 return 0;
32 }
33}
34
35//==============================================================================
36// as_texture_access
37//==============================================================================
38
39std::optional<TextureAccess> as_texture_access(const DataVariant& variant)
40{
41 return std::visit([](const auto& vec) -> std::optional<TextureAccess> {
42 using T = typename std::decay_t<decltype(vec)>::value_type;
43
44 if (vec.empty()) {
46 "as_texture_access: empty variant");
47 return std::nullopt;
48 }
49
50 // ---- rejected types ------------------------------------------------
51
52 if constexpr (std::is_same_v<T, std::complex<double>>) {
54 "as_texture_access: complex<double> (RG64F) is not a sampled "
55 "image format in Vulkan. Use complex<float> for RG32F.");
56 return std::nullopt;
57 }
58
59 if constexpr (std::is_same_v<T, glm::mat4>) {
61 "as_texture_access: mat4 layout is ambiguous for texel upload. "
62 "Unpack each column to a vec4 and use vector<glm::vec4>.");
63 return std::nullopt;
64 }
65
66 // ---- zero-copy direct mappings ------------------------------------
67
68 if constexpr (std::is_same_v<T, uint8_t>) {
69 return TextureAccess {
70 .data_ptr = vec.data(),
71 .byte_count = vec.size() * sizeof(T),
72 .format = GpuDataFormat::UINT8
73 };
74 }
75
76 if constexpr (std::is_same_v<T, uint16_t>) {
77 return TextureAccess {
78 .data_ptr = vec.data(),
79 .byte_count = vec.size() * sizeof(T),
80 .format = GpuDataFormat::UINT16
81 };
82 }
83
84 if constexpr (std::is_same_v<T, uint32_t>) {
85 return TextureAccess {
86 .data_ptr = vec.data(),
87 .byte_count = vec.size() * sizeof(T),
88 .format = GpuDataFormat::UINT32
89 };
90 }
91
92 if constexpr (std::is_same_v<T, float>) {
93 return TextureAccess {
94 .data_ptr = vec.data(),
95 .byte_count = vec.size() * sizeof(T),
97 };
98 }
99
100 if constexpr (std::is_same_v<T, glm::vec2>
101 || std::is_same_v<T, std::complex<float>>) {
102 return TextureAccess {
103 .data_ptr = vec.data(),
104 .byte_count = vec.size() * sizeof(T),
106 };
107 }
108
109 if constexpr (std::is_same_v<T, glm::vec4>) {
110 return TextureAccess {
111 .data_ptr = vec.data(),
112 .byte_count = vec.size() * sizeof(T),
114 };
115 }
116
117 // ---- narrowing: double → float -------------------------------------
118
119 if constexpr (std::is_same_v<T, double>) {
121 "as_texture_access: double narrowed to float for texel upload. "
122 "Precision beyond float range is lost.");
123 TextureAccess acc;
125 acc.conversion_buffer.resize(vec.size() * sizeof(float));
126 auto* dst = reinterpret_cast<float*>(acc.conversion_buffer.data());
127 for (size_t i = 0; i < vec.size(); ++i) {
128 dst[i] = static_cast<float>(vec[i]);
129 }
130 acc.data_ptr = acc.conversion_buffer.data();
131 acc.byte_count = acc.conversion_buffer.size();
132 return acc;
133 }
134
135 // ---- promotion: vec3 → vec4 (W = 0) --------------------------------
136
137 if constexpr (std::is_same_v<T, glm::vec3>) {
139 "as_texture_access: vec3 promoted to vec4 (W=0). "
140 "RGB32F is not a universally-supported sampled image format in Vulkan.");
141 TextureAccess acc;
143 acc.conversion_buffer.resize(vec.size() * sizeof(glm::vec4));
144 auto* dst = reinterpret_cast<glm::vec4*>(acc.conversion_buffer.data());
145 for (size_t i = 0; i < vec.size(); ++i) {
146 dst[i] = glm::vec4(vec[i], 0.0F);
147 }
148 acc.data_ptr = acc.conversion_buffer.data();
149 acc.byte_count = acc.conversion_buffer.size();
150 return acc;
151 }
152
153 // ---- unreachable for current DataVariant definition ----------------
155 "as_texture_access: unhandled variant type {}",
156 typeid(T).name());
157 return std::nullopt;
158 },
159 variant);
160}
161
162} // namespace MayaFlux::Kakshya
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
@ 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:102
std::optional< TextureAccess > as_texture_access(const DataVariant &variant)
Extract a TextureAccess from a DataVariant.
std::vector< std::byte > conversion_buffer
Promotion buffer.
uint32_t channel_count() const noexcept
Number of channels (components per texel).
Memory-compatible view of a DataVariant for texel upload.