39 bool operator==(
const Layout&)
const noexcept =
default;
46 case ElementType::Half:
49 case ElementType::UInt8:
56constexpr int components(
const Layout& l)
noexcept
58 return std::clamp(l.components, 1, 4);
64 return int64_t(componentBytes(l.type)) * components(l);
67constexpr int64_t strideOf(
const Layout& l)
noexcept
75 const int64_t room = bytes - std::max<int64_t>(l.offset, 0);
79 return (room - e) / strideOf(l) + 1;
102 bool operator==(
const Sampling&)
const noexcept =
default;
111 int64_t available, int64_t first, int64_t max_elements, int64_t element_bytes,
112 int64_t cap_bytes,
bool overview)
noexcept
115 s.first = std::clamp<int64_t>(first, 0, std::max<int64_t>(available, 0));
116 s.window = std::max<int64_t>(available, 0) - s.first;
119 const int64_t fit = std::max<int64_t>(1, cap_bytes / std::max<int64_t>(element_bytes, 1));
120 const int64_t asked = std::max<int64_t>(1, max_elements);
121 const int64_t limit = std::min(asked, fit);
122 s.capped = fit < asked && s.window > fit;
123 if(overview && s.window > limit)
125 s.step = (s.window + limit - 1) / limit;
126 s.count = (s.window + s.step - 1) / s.step;
129 s.count = std::min(s.window, limit);
139 int64_t stride, int64_t element_bytes, int64_t misalign, int64_t cap_bytes)
noexcept
141 const int64_t room = cap_bytes - element_bytes - misalign - 3;
144 return room / std::max<int64_t>(stride, 1) + 1;
150 return t == ElementType::Int || t == ElementType::UInt || t == ElementType::UInt8;
156 const uint32_t sign = uint32_t(h & 0x8000u) << 16;
157 const uint32_t exponent = (h >> 10) & 0x1fu;
158 const uint32_t mantissa = h & 0x3ffu;
161 const float f = std::ldexp(
float(mantissa), -24);
162 return sign ? -f : f;
165 return std::bit_cast<float>(sign | 0x7f800000u | (mantissa << 13));
166 return std::bit_cast<float>(sign | ((exponent + 112u) << 23) | (mantissa << 13));
174 case ElementType::Float: {
176 std::memcpy(&v, p, 4);
179 case ElementType::Half: {
181 std::memcpy(&v, p, 2);
184 case ElementType::Int: {
186 std::memcpy(&v, p, 4);
189 case ElementType::UInt: {
191 std::memcpy(&v, p, 4);
196 case ElementType::UInt8:
208decode(
const Layout& l, std::span<const unsigned char> bytes, std::span<float> out)
noexcept
210 const int n = components(l);
211 const int cb = componentBytes(l.type);
212 const int64_t stride = strideOf(l);
213 const int64_t count = std::min(
214 elementCount(l, int64_t(bytes.size())), int64_t(out.size()) / n);
215 const unsigned char* src = bytes.data() + std::max<int64_t>(l.offset, 0);
216 float* dst = out.data();
217 for(int64_t i = 0; i < count; i++, src += stride)
218 for(
int c = 0; c < n; c++)
Definition BufferDecode.hpp:16
constexpr Sampling sampleElements(int64_t available, int64_t first, int64_t max_elements, int64_t element_bytes, int64_t cap_bytes, bool overview) noexcept
Definition BufferDecode.hpp:110
int64_t decode(const Layout &l, std::span< const unsigned char > bytes, std::span< float > out) noexcept
Definition BufferDecode.hpp:208
constexpr int64_t elementBytes(const Layout &l) noexcept
Bytes one element covers.
Definition BufferDecode.hpp:62
constexpr int64_t max_view_elements
Elements a view reads at most.
Definition BufferDecode.hpp:86
ElementType
Definition BufferDecode.hpp:18
@ UNorm8
0 to 255 read as 0 to 1
float halfToFloat(uint16_t h) noexcept
IEEE 754 binary16 to binary32, subnormals, infinities and NaN included.
Definition BufferDecode.hpp:154
constexpr int64_t max_readback_bytes
Bytes one readback holds at most, whatever the layout and the controls.
Definition BufferDecode.hpp:83
constexpr bool integral(ElementType t) noexcept
Whether the values are integers, to print them as such.
Definition BufferDecode.hpp:148
constexpr int64_t elementCount(const Layout &l, int64_t bytes) noexcept
Whole elements in bytes bytes, the first at the layout's offset.
Definition BufferDecode.hpp:73
float readComponent(ElementType t, const unsigned char *p) noexcept
One component at p, which needs no alignment.
Definition BufferDecode.hpp:170
constexpr int64_t contiguousFit(int64_t stride, int64_t element_bytes, int64_t misalign, int64_t cap_bytes) noexcept
Definition BufferDecode.hpp:138
Definition BufferDecode.hpp:29
int64_t stride
Definition BufferDecode.hpp:35
int64_t offset
Bytes before the first element.
Definition BufferDecode.hpp:37
int components
1 to 4
Definition BufferDecode.hpp:32
Which elements of a buffer a view reads.
Definition BufferDecode.hpp:90
int64_t window
Elements from the first to the end of the buffer.
Definition BufferDecode.hpp:98
int64_t step
Elements from one read to the next: 1 reads them all.
Definition BufferDecode.hpp:94
int64_t count
Elements read.
Definition BufferDecode.hpp:96
bool capped
Whether the bytes a readback holds kept count below what was asked.
Definition BufferDecode.hpp:100
int64_t first
Index of the first element read.
Definition BufferDecode.hpp:92