Skip to content

Commit 2eadc3b

Browse files
committed
wipe CIESProfile::octahdronUVToDir and use HLSL version
1 parent 12d608f commit 2eadc3b

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

include/nbl/builtin/hlsl/math/octahedral.hlsl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,55 @@ namespace math
1616
{
1717

1818
// Octahedral Transform, maps 3D direction vectors to 2D square and vice versa
19-
template<typename T = float64_t>
19+
template<typename T = float32_t>
2020
struct OctahedralTransform
2121
{
2222
using scalar_type = T;
2323
using vector2_type = vector<T, 2>;
2424
using vector3_type = vector<T, 3>;
2525

26-
// F : [-1, 1]^2 -> S^2
27-
static vector3_type eval(const vector2_type ndc)
26+
// F : [0, 1]^2 -> S^2
27+
static vector3_type uvToDir(const vector2_type uv)
2828
{
29-
vector3_type p = vector3_type(ndc.xy, scalar_type(0));
30-
const vector2_type a = abs(p.xy);
29+
vector3_type p = vector3_type((uv * scalar_type(2) - scalar_type(1)), scalar_type(0));
30+
const scalar_type a_x = abs(p.x); const scalar_type a_y = abs(p.y);
3131

32-
p.z = scalar_type(1) - a.x - a.y;
32+
p.z = scalar_type(1) - a_x - a_y;
3333

34-
if (p.z < scalar_type(0))
35-
p.xy = hlsl::sign(p.xy) * (scalar_type(1) - abs(p.yx));
34+
if (p.z < scalar_type(0))
35+
{
36+
p.x = (p.x < scalar_type(0) ? scalar_type(-1) : scalar_type(1)) * (scalar_type(1) - a_y);
37+
p.y = (p.y < scalar_type(0) ? scalar_type(-1) : scalar_type(1)) * (scalar_type(1) - a_x);
38+
}
3639

3740
return hlsl::normalize(p);
3841
}
3942

4043
// F^-1 : S^2 -> [-1, 1]^2
41-
static vector2_type inverse(vector3_type dir)
44+
static vector2_type dirToNDC(vector3_type dir)
4245
{
4346
dir = hlsl::normalize(dir);
4447
const scalar_type sum = hlsl::dot(vector3_type(scalar_type(1), scalar_type(1), scalar_type(1)), abs(dir));
4548
vector3_type s = dir / sum;
4649

4750
if (s.z < scalar_type(0))
48-
s.xy = hlsl::sign(s.xy) * (scalar_type(1) - abs(s.yx));
51+
{
52+
s.x = (s.x < scalar_type(0) ? scalar_type(-1) : scalar_type(1)) * (scalar_type(1) - abs(s.y));
53+
s.y = (s.y < scalar_type(0) ? scalar_type(-1) : scalar_type(1)) * (scalar_type(1) - abs(s.x));
54+
}
4955

5056
return s.xy;
5157
}
5258

53-
// transforms direction vector into UV (for corner sampling)
59+
// transforms direction vector into UV for corner sampling
5460
// dir in S^2, halfMinusHalfPixel in [0, 0.5)^2,
5561
// where halfMinusHalfPixel = 0.5-0.5/texSize
5662
// and texSize.x >= 1, texSize.y >= 1
57-
// NOTE/TODO: not best place to keep it here imo
63+
// NOTE/TODO: not best place to keep it here
5864
static vector2_type toCornerSampledUV(vector3_type dir, vector2_type halfMinusHalfPixel)
5965
{
6066
// note: cornerSampled(NDC*0.5+0.5) = NDC*0.5*(1-1/texSize)+0.5
61-
return inverse(dir) * halfMinusHalfPixel + scalar_type(0.5);
67+
return dirToNDC(dir) * halfMinusHalfPixel + scalar_type(0.5);
6268
}
6369
};
6470

src/nbl/asset/utils/CIESProfile.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <atomic>
44
#include "nbl/asset/filters/CBasicImageFilterCommon.h"
5+
#include "nbl/builtin/hlsl/math/octahedral.hlsl"
56

67
using namespace nbl;
78
using namespace asset;
@@ -82,20 +83,6 @@ const CIESProfile::IES_STORAGE_FORMAT CIESProfile::sample(IES_STORAGE_FORMAT the
8283
return s0 * (1.0 - u) + s1 * u;
8384
}
8485

85-
inline core::vectorSIMDf CIESProfile::octahdronUVToDir(const float& u, const float& v)
86-
{
87-
core::vectorSIMDf pos = core::vectorSIMDf(2 * (u - 0.5), 2 * (v - 0.5), 0.0);
88-
float abs_x = core::abs(pos.x), abs_y = core::abs(pos.y);
89-
pos.z = 1.0 - abs_x - abs_y;
90-
if (pos.z < 0.0) {
91-
pos.x = (pos.x<0.f ? (-1.f):1.f) * (1.0 - abs_y);
92-
pos.y = (pos.y<0.f ? (-1.f):1.f) * (1.0 - abs_x);
93-
}
94-
95-
return core::normalize(pos);
96-
}
97-
98-
9986
inline std::pair<float, float> CIESProfile::sphericalDirToRadians(const core::vectorSIMDf& dir)
10087
{
10188
const float theta = std::acos(std::clamp<float>(dir.z, -1.f, 1.f));
@@ -183,8 +170,12 @@ core::smart_refctd_ptr<asset::ICPUImageView> CIESProfile::createIESTexture(Execu
183170
{
184171
// We don't currently support generating IES images that exploit symmetries or reduced domains, all are full octahederal mappings of a sphere.
185172
// If we did, we'd rely on MIRROR and CLAMP samplers to do some of the work for us while handling the discontinuity due to corner sampling.
186-
const auto dir = octahdronUVToDir(position.x * vertInv, position.y * horiInv);
187-
const auto [theta, phi] = sphericalDirToRadians(dir);
173+
174+
using Octahedral = hlsl::math::OctahedralTransform<hlsl::float32_t>;
175+
const auto uv = Octahedral::vector2_type(position.x * vertInv, position.y * horiInv);
176+
const auto dir = Octahedral::uvToDir(uv);
177+
const auto tmp = core::vectorSIMDf(dir.x, dir.y, dir.z);
178+
const auto [theta, phi] = sphericalDirToRadians(tmp);
188179
const auto intensity = sample(theta, phi);
189180

190181
//! blend the IES texture with "flatten"

0 commit comments

Comments
 (0)