MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ as_texture_access()

MAYAFLUX_API std::optional< TextureAccess > MayaFlux::Kakshya::as_texture_access ( const DataVariant variant)

Extract a TextureAccess from a DataVariant.

Parameters
variantSource data.
Returns
Populated TextureAccess, or std::nullopt on incompatible type.

DataVariant active type → GpuDataFormat mapping: vector<uint8_t> → UINT8 vector<uint16_t> → UINT16 vector<uint32_t> → UINT32 vector<float> → FLOAT32 vector<double> → FLOAT32 (narrowed, warned) vector<complex<float>> → VEC2_F32 (identical layout to glm::vec2) vector<glm::vec2> → VEC2_F32 vector<glm::vec3> → VEC4_F32 (W=0 promotion, warned) vector<glm::vec4> → VEC4_F32 vector<complex<double>> → std::nullopt (logged error) vector<glm::mat4> → std::nullopt (logged error)

Definition at line 39 of file TextureAccess.cpp.

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()) {
45 MF_WARN(Journal::Component::Kakshya, Journal::Context::Runtime,
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>>) {
53 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime,
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>) {
60 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime,
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),
96 .format = GpuDataFormat::FLOAT32
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),
105 .format = GpuDataFormat::VEC2_F32
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),
113 .format = GpuDataFormat::VEC4_F32
114 };
115 }
116
117 // ---- narrowing: double → float -------------------------------------
118
119 if constexpr (std::is_same_v<T, double>) {
120 MF_WARN(Journal::Component::Kakshya, Journal::Context::Runtime,
121 "as_texture_access: double narrowed to float for texel upload. "
122 "Precision beyond float range is lost.");
123 TextureAccess acc;
124 acc.format = GpuDataFormat::FLOAT32;
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>) {
138 MF_WARN(Journal::Component::Kakshya, Journal::Context::Runtime,
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;
142 acc.format = GpuDataFormat::VEC4_F32;
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 ----------------
154 MF_ERROR(Journal::Component::Kakshya, Journal::Context::Runtime,
155 "as_texture_access: unhandled variant type {}",
156 typeid(T).name());
157 return std::nullopt;
158 },
159 variant);
160}
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)

References MayaFlux::Kakshya::TextureAccess::byte_count, MayaFlux::Kakshya::TextureAccess::conversion_buffer, MayaFlux::Kakshya::TextureAccess::data_ptr, FLOAT32, MayaFlux::Kakshya::TextureAccess::format, MayaFlux::Journal::Kakshya, MF_ERROR, MF_WARN, MayaFlux::Journal::Runtime, UINT16, UINT32, UINT8, VEC2_F32, and VEC4_F32.

Referenced by MayaFlux::Portal::Graphics::TextureLoom::create_2d(), MayaFlux::Buffers::TextureProcessor::update_pixels_if_dirty(), and MayaFlux::Buffers::DataWriteProcessor::upload_texture().

+ Here is the caller graph for this function: