Loading...
Searching...
No Matches
ArrayToTexture.hpp
1#pragma once
2
3#include <ossia/detail/pod_vector.hpp>
4
5#include <boost/container/vector.hpp>
6
7#include <halp/controls.hpp>
8#include <halp/geometry.hpp>
9#include <halp/meta.hpp>
10#include <halp/texture.hpp>
11
12#include <algorithm>
13#include <cstring>
14
15#if (!defined(__linux__) && !defined(_MSC_VER))
16#define SCORE_LIBC_HAS_FLOAT16 1
17#elif (defined(__linux__) && defined(__GLIBC__))
18#if __GLIBC_PREREQ(2, 40)
19#define SCORE_LIBC_HAS_FLOAT16 1
20#endif
21#endif
22
23// when you enter the least obvious syntax competition and your opponent is clang builtins
24#if defined(__is_identifier) && !defined(_MSC_VER)
25#if (!__is_identifier(_Float16)) && defined(__FLT16_MANT_DIG__)
26#define SCORE_COMPILER_HAS_FLOAT16 1
27#endif
28#endif
29
30namespace Threedim
31{
33{
34public:
35 halp_meta(name, "Array to texture")
36 halp_meta(category, "Visuals/Utilities")
37 halp_meta(c_name, "array_to_texture")
38 halp_meta(manual_url, "https://ossia.io/score-docs/processes/array-to-texture.html")
39 halp_meta(uuid, "bb5dc513-3430-4671-8c74-2bba78e53709")
40
41 struct ins
42 {
43 struct : halp::val_port<"Input", std::vector<float>>
44 {
45 void update(ArrayToTexture& self) { self.recreate(); }
46 } in;
47 struct : halp::xy_spinboxes_i32<"Size">
48 {
49 void update(ArrayToTexture& self) { self.recreate(); }
50 } size;
51 struct : halp::enum_t<halp::custom_texture::texture_format, "Format">
52 {
53 void update(ArrayToTexture& self) { self.recreate(); }
54 } format;
55 } inputs;
56
57 struct
58 {
59 halp::texture_output<"Output", halp::custom_texture> main;
60 } outputs;
61
62 void recreate()
63 {
64 const auto format = inputs.format;
65 const auto sz = inputs.size.value;
66 outputs.main.texture.request_format = format;
67 outputs.main.create(sz.x, sz.y);
68 std::size_t to_copy = sz.x * sz.y * outputs.main.texture.components(format);
69 const auto& value = inputs.in.value;
70 to_copy = std::min(to_copy, value.size());
71
72 auto* out = outputs.main.texture.bytes;
73 using enum halp::custom_texture::texture_format;
74 switch(format)
75 {
76 case RGBA8:
77 case BGRA8:
78 case R8:
79 case RG8:
80 case RED_OR_ALPHA8:
81 case R8UI:
82 std::copy_n(value.data(), to_copy, (uint8_t*)out);
83 break;
84
85 case R16:
86 case RG16:
87 std::copy_n(value.data(), to_copy, (uint16_t*)out);
88 break;
89
90 case R32UI:
91 case RG32UI:
92 case RGBA32UI:
93 std::copy_n(value.data(), to_copy, (uint32_t*)out);
94 break;
95
96 case R32F:
97 case RGBA32F:
98 std::copy_n(value.data(), to_copy, (float*)out);
99 break;
100
101 case R16F:
102 case RGBA16F:
103
104#if (SCORE_LIBC_HAS_FLOAT16 && SCORE_COMPILER_HAS_FLOAT16)
105 std::copy_n(value.data(), to_copy, (_Float16*)out);
106#else
107 {
108 // No native _Float16: software IEEE 754 binary16 conversion
109 // (round-to-nearest-even) so the upload is still meaningful.
110 auto* dst = (uint16_t*)out;
111 for(std::size_t i = 0; i < to_copy; i++)
112 {
113 const float f = value.data()[i];
114 uint32_t x;
115 std::memcpy(&x, &f, 4);
116 const uint32_t sign = (x >> 16) & 0x8000;
117 x &= 0x7FFFFFFF;
118 if(x >= 0x47800000) // overflow / inf / NaN -> inf (or NaN)
119 dst[i] = sign | 0x7C00 | (x > 0x7F800000 ? 0x200 : 0);
120 else if(x < 0x38800000) // too small for a normal half
121 {
122 const uint32_t shift = 126 - (x >> 23); // >= 14
123 uint32_t half = 0;
124 if(shift <= 24)
125 {
126 const uint32_t mant = (x & 0x7FFFFF) | 0x800000;
127 half = mant >> shift;
128 half += ((mant >> (shift - 1)) & 1)
129 && ((mant & ((1u << (shift - 1)) - 1)) || (half & 1));
130 }
131 dst[i] = sign | half;
132 }
133 else
134 {
135 uint32_t half = ((x - 0x38000000) >> 13);
136 half += ((x >> 12) & 1) && ((x & 0xFFF) || (half & 1));
137 dst[i] = sign | half;
138 }
139 }
140 }
141#endif
142 break;
143
144 default:
145 break;
146 }
147 outputs.main.upload();
148 }
149};
150
151}
Definition ArrayToTexture.hpp:33
Definition ArrayToTexture.hpp:42