MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GpuDataFormat.cpp
Go to the documentation of this file.
1#include "GpuDataFormat.hpp"
2
3namespace MayaFlux::Kakshya {
4
6{
7 switch (fmt) {
9 return 1;
11 return 2;
15 return 4;
18 return 8;
20 return 12;
23 return 16;
25 return 24;
27 return 32;
29 return 64;
30 default:
31 return 0;
32 }
33}
34
36{
37 switch (fmt) {
39 return 1;
41 return 2;
45 return 4;
48 return 8;
53 return 16;
56 return 32;
57 default:
58 return 0;
59 }
60}
61
63{
64 switch (fmt) {
71 return 1;
74 return 2;
77 return 3;
80 return 4;
82 return 16;
83 default:
84 return 0;
85 }
86}
87
88GpuBlockLayout gpu_block_layout(std::span<const GpuDataFormat> fields)
89{
90 GpuBlockLayout layout;
91 layout.offsets.reserve(fields.size());
92
93 size_t end = 0;
94 size_t widest = 4;
95 for (const GpuDataFormat field : fields) {
96 const size_t align = gpu_data_format_align(field);
97 if (align == 0) {
98 layout.offsets.push_back(static_cast<uint32_t>(end));
99 continue;
100 }
101
102 end = (end + align - 1) / align * align;
103 layout.offsets.push_back(static_cast<uint32_t>(end));
104 end += gpu_data_format_bytes(field);
105 widest = std::max(widest, align);
106 }
107
108 layout.size = static_cast<uint32_t>((end + widest - 1) / widest * widest);
109 return layout;
110}
111
112size_t gpu_value_write(const GpuValue& value, std::span<std::byte> out) noexcept
113{
114 return std::visit([&](const auto& held) -> size_t {
115 if (out.size() < sizeof(held)) {
116 return 0;
117 }
118 std::memcpy(out.data(), &held, sizeof(held));
119 return sizeof(held);
120 },
121 value);
122}
123
124namespace {
125
126 template <typename Component>
127 void store_components(std::span<const double> components, std::byte* out) noexcept
128 {
129 for (size_t i = 0; i < components.size(); ++i) {
130 const Component converted = saturate_cast<Component>(components[i]);
131 std::memcpy(out + i * sizeof(Component), &converted, sizeof(Component));
132 }
133 }
134
135} // namespace
136
139 std::span<const double> components,
140 std::span<std::byte> out) noexcept
141{
142 if (components.size() != gpu_data_format_components(dst) || out.size() < gpu_data_format_bytes(dst)) {
143 return false;
144 }
145
146 switch (dst) {
152 store_components<float>(components, out.data());
153 return true;
158 store_components<double>(components, out.data());
159 return true;
161 store_components<int32_t>(components, out.data());
162 return true;
164 store_components<uint32_t>(components, out.data());
165 return true;
167 store_components<uint8_t>(components, out.data());
168 return true;
170 store_components<uint16_t>(components, out.data());
171 return true;
172 default:
173 return false;
174 }
175}
176
177} // namespace MayaFlux::Kakshya
uint32_t * dst
std::vector< float > * out
glm::vec3 value
std::variant< float, glm::vec2, glm::vec3, glm::vec4, double, glm::dvec2, glm::dvec3, glm::dvec4, int32_t, uint32_t, uint8_t, uint16_t, glm::mat4 > GpuValue
One value of any GpuDataFormat.
bool pack_gpu_components(GpuDataFormat dst, std::span< const double > components, std::span< std::byte > out) noexcept
Writes scalar components into out as one element of dst.
size_t gpu_data_format_bytes(GpuDataFormat fmt) noexcept
Byte size of one element of a GpuDataFormat.
size_t gpu_data_format_align(GpuDataFormat fmt) noexcept
Base alignment in bytes of one element, following std430 rules.
size_t gpu_data_format_components(GpuDataFormat fmt) noexcept
Number of scalar components in one element of a GpuDataFormat.
GpuBlockLayout gpu_block_layout(std::span< const GpuDataFormat > fields)
Lay out fields in declaration order under std430 rules.
size_t gpu_value_write(const GpuValue &value, std::span< std::byte > out) noexcept
Copies the held value's bytes into out, unconverted.
GpuDataFormat
GPU data formats with explicit precision levels.
@ VEC3_F64
glm::dvec3 (64-bit components)
@ FLOAT64
64-bit double (audio precision)
@ UINT16
uint16_t, R16F raw half-float storage, packed formats
@ VEC2_F64
glm::dvec2 (64-bit components)
@ VEC2_F32
glm::vec2 (32-bit components)
@ VEC4_F32
glm::vec4 (32-bit components)
@ VEC4_F64
glm::dvec4 (64-bit components)
@ FLOAT32
32-bit float (standard GPU)
@ MAT4_F32
glm::mat4 (column-major), not a sampled image format
@ VEC3_F32
glm::vec3 (32-bit components), not a sampled image format
@ UINT8
uint8_t, R8 / RGBA8 texel data
std::vector< uint32_t > offsets
Member offsets and total size of a block of GpuDataFormat fields.