Skip to content

Commit d3090af

Browse files
committed
rgb19e7 bug cpp test
1 parent 3309419 commit d3090af

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

examples_tests/22.RaytracedAO/main.cpp

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,192 @@
1818
using namespace nbl;
1919
using namespace core;
2020

21+
//#define TEST_FORMAT
22+
23+
#ifdef TEST_FORMAT
24+
struct Test_RGB19E7
25+
{
26+
static constexpr const char* name = "rgb19e7";
27+
28+
static uint64_t acc_;
29+
static core::vectorSIMDf fetch()
30+
{
31+
auto rgb = core::rgb19e7_to_rgb32f(acc_);
32+
return core::vectorSIMDf(rgb.x, rgb.y, rgb.z);
33+
}
34+
static void store(const core::vectorSIMDf& x)
35+
{
36+
acc_ = core::rgb32f_to_rgb19e7(x.pointer);
37+
}
38+
};
39+
uint64_t Test_RGB19E7::acc_ = core::rgb32f_to_rgb19e7(0.f, 0.f, 0.f);
40+
struct Test_Expected
41+
{
42+
static constexpr const char* name = "expected";
43+
44+
static core::vectorSIMDf acc_;
45+
static core::vectorSIMDf fetch()
46+
{
47+
return acc_;
48+
}
49+
static void store(const core::vectorSIMDf& x)
50+
{
51+
acc_ = x;
52+
}
53+
};
54+
core::vectorSIMDf Test_Expected::acc_ = core::vectorSIMDf(0.f);
55+
void print(const core::vectorSIMDf v)
56+
{
57+
std::cout << v.x << ", " << v.y << ", " << v.z << std::endl;
58+
}
59+
60+
// raygen=0, resolve=1
61+
template <typename Test>
62+
auto test_()
63+
{
64+
const core::vectorSIMDf em(0.f, 0.5f, 0.25f);
65+
const core::vectorSIMDf dir(0.0f);
66+
for (int i = 0; i <= 30; ++i)
67+
{
68+
std::cout << "i = " << i << std::endl;
69+
float rcp = 1.f / float(i);
70+
if (rcp < 1.f)
71+
{
72+
auto acc = Test::fetch();
73+
acc += core::vectorSIMDf(em / float(i - 1));
74+
Test::store(acc);
75+
}
76+
else Test::store(em);
77+
//std::cout << "step 1: "; print(Test::fetch());
78+
79+
if (rcp < 1.f)
80+
{
81+
auto acc = Test::fetch();
82+
acc += (dir - acc) * rcp;
83+
Test::store(acc);
84+
}
85+
else
86+
{
87+
auto acc = Test::fetch();
88+
acc += dir;
89+
Test::store(acc);
90+
}
91+
std::cout << "step 2: "; print(Test::fetch());
92+
}
93+
94+
return Test::fetch();
95+
}
96+
template <typename T>
97+
auto test()
98+
{
99+
const auto result = test_<T>();
100+
std::cout << std::left << std::setw(15) << T::name << " result = " << result.x << ", " << result.y << ", " << result.z << ";\n";
101+
return result;
102+
}
103+
104+
int main()
105+
{
106+
test<Test_RGB19E7>();
107+
/*auto error = core::abs(test<Test_Expected>() - test<Test_RGB19E7>());
108+
std::cout << std::setprecision(15) << error.x << ", " << error.y << ", " << error.z << "\n";*/
109+
return 0;
110+
}
111+
112+
#else
113+
114+
struct uvec2
115+
{
116+
uvec2() : x{ 0,0 } {}
117+
uvec2(uint32_t xx, uint32_t yy) : x{ xx,yy } {}
118+
uvec2(uint64_t xx) : u64(xx) {}
119+
120+
operator uint64_t() const { return *reinterpret_cast<const uint64_t*>(x); }
121+
122+
uint32_t& operator[](int a) { return x[a]; }
123+
private:
124+
union {
125+
uint32_t x[2];
126+
uint64_t u64;
127+
};
128+
};
129+
130+
core::rgb32f decode(uvec2 x)
131+
{
132+
int _exp = int((bitfieldExtract(x[1], 25, 7) - 63u) - 19u);
133+
float scale = exp2(float(_exp));
134+
rgb32f v;
135+
v.x = float(bitfieldExtract(x[0], 0, 19));
136+
v.y = float(bitfieldInsert(bitfieldExtract(x[0], 19, 13), bitfieldExtract(x[1], 0, 6), 13, 6));
137+
v.z = float(bitfieldExtract(x[1], 6, 19));
138+
v.x *= scale;
139+
v.y *= scale;
140+
v.z *= scale;
141+
return v;
142+
}
143+
uvec2 encode(rgb32f col)
144+
{
145+
rgb32f clamped;
146+
clamped.x = clamp(col.x, 0.f, 35184304979968.0f);
147+
clamped.y = clamp(col.y, 0.f, 35184304979968.0f);
148+
clamped.z = clamp(col.z, 0.f, 35184304979968.0f);
149+
float maxrgb = max(max(clamped.x, clamped.y), clamped.z);
150+
int f32_exp = ((floatBitsToInt(maxrgb) >> 23) & 255) - 127;
151+
int shared_exp = clamp(f32_exp, -64, 64) + 1;
152+
uint32_t mantissas[3];// = uvec3((clamped * exp2(float(19 - shared_exp))) + vec3(0.5));
153+
float scale = ::exp2(19 - shared_exp);
154+
uint32_t maxm = maxrgb*scale + 0.5f;
155+
bool need = (maxm == float(1u<<19));
156+
scale = need ? (scale*0.5f) : scale;
157+
shared_exp = need ? (shared_exp+1) : shared_exp;
158+
mantissas[0] = uint32_t((clamped.x * scale) + 0.5f);
159+
mantissas[1] = uint32_t((clamped.y * scale) + 0.5f);
160+
mantissas[2] = uint32_t((clamped.z * scale) + 0.5f);
161+
uvec2 encoded;
162+
encoded[0] = bitfieldInsert(mantissas[0], mantissas[1], 19, 13);
163+
encoded[1] = bitfieldInsert(mantissas[1] >> 13, mantissas[2], 6, 19) | ((shared_exp + 63) << 25);
164+
return encoded;
165+
}
166+
167+
168+
21169
int main()
22170
{
171+
float rgb32_[3]{ 0.f, 0.333333333f, 0.f };
172+
rgb32f rgb32; memcpy(&rgb32.x, rgb32_, 12);
173+
const uvec2 rgb19e7_frame109_input = encode(rgb32); //uvec2(2863136768, 2080374826);
174+
175+
const float rcp = 0.14286f;
176+
177+
/*uint32_t rgb_[2]{ 2863136768, 2080374826 }; // frame 108
178+
//uint32_t rgb_[2]{ 0, 2080374784 }; // frame 109 step 1
179+
uint64_t* rgb = reinterpret_cast<uint64_t*>(rgb_);
180+
auto _19e7 = core::rgb19e7_to_rgb32f(*rgb);
181+
float t = 1.f;
182+
t /= 6.f;
183+
_19e7.y += t;
184+
*rgb = core::rgb32f_to_rgb19e7(&_19e7.x); // step 1 store (end)
185+
// step 2 start
186+
_19e7 = core::rgb19e7_to_rgb32f(*rgb);
187+
_19e7.y += (0.f - _19e7.y) * rcp;
188+
*rgb = core::rgb32f_to_rgb19e7(&_19e7.x);
189+
_19e7 = core::rgb19e7_to_rgb32f(*rgb);
190+
printf("");
191+
*/
192+
uvec2 rgb19e7 = rgb19e7_frame109_input;
193+
auto acc = decode(rgb19e7);
194+
float t = 1.f;
195+
t /= 6.f;
196+
acc.y += t;
197+
rgb19e7 = encode(acc); // step 1 store (end)
198+
// step 2
199+
acc = decode(rgb19e7);
200+
acc.y += (0.f - acc.y) * rcp;
201+
rgb19e7 = encode(acc);
202+
// put into fbo
203+
rgb32f fbo = decode(rgb19e7);
204+
printf("");
205+
206+
23207
// create device with full flexibility over creation parameters
24208
// you can add more parameters if desired, check nbl::SIrrlichtCreationParameters
25209
nbl::SIrrlichtCreationParameters params;
@@ -314,3 +498,4 @@ int main()
314498

315499
return 0;
316500
}
501+
#endif

0 commit comments

Comments
 (0)