Skip to content

Commit 1d8aa31

Browse files
committed
wipe CIESProfile::sphericalDirToRadians and use polar.hlsl
1 parent ff2558b commit 1d8aa31

File tree

4 files changed

+23
-56
lines changed

4 files changed

+23
-56
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct OctahedralTransform
2424
using vector3_type = vector<T, 3>;
2525

2626
// F : [0, 1]^2 -> S^2
27-
static vector3_type uvToDir(const vector2_type uv)
27+
static vector3_type uvToDir(NBL_CONST_REF_ARG(vector2_type) uv)
2828
{
2929
vector3_type p = vector3_type((uv * scalar_type(2) - scalar_type(1)), scalar_type(0));
3030
const scalar_type a_x = abs(p.x); const scalar_type a_y = abs(p.y);
@@ -41,9 +41,9 @@ struct OctahedralTransform
4141
}
4242

4343
// F^-1 : S^2 -> [-1, 1]^2
44-
static vector2_type dirToNDC(vector3_type dir)
44+
static vector2_type dirToNDC(NBL_CONST_REF_ARG(vector3_type) d)
4545
{
46-
dir = hlsl::normalize(dir);
46+
scalar_type dir = hlsl::normalize(d);
4747
const scalar_type sum = hlsl::dot(vector3_type(scalar_type(1), scalar_type(1), scalar_type(1)), abs(dir));
4848
vector3_type s = dir / sum;
4949

@@ -61,7 +61,7 @@ struct OctahedralTransform
6161
// where halfMinusHalfPixel = 0.5-0.5/texSize
6262
// and texSize.x >= 1, texSize.y >= 1
6363
// NOTE/TODO: not best place to keep it here
64-
static vector2_type toCornerSampledUV(vector3_type dir, vector2_type halfMinusHalfPixel)
64+
static vector2_type toCornerSampledUV(NBL_CONST_REF_ARG(vector3_type) dir, NBL_CONST_REF_ARG(vector2_type) halfMinusHalfPixel)
6565
{
6666
// note: cornerSampled(NDC*0.5+0.5) = NDC*0.5*(1-1/texSize)+0.5
6767
return dirToNDC(dir) * halfMinusHalfPixel + scalar_type(0.5);

include/nbl/builtin/hlsl/math/polar.hlsl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,38 @@ namespace hlsl
1414
namespace math
1515
{
1616

17-
template<typename T>
17+
template<typename T = float32_t>
1818
struct Polar
1919
{
2020
using scalar_type = T;
2121
using vector2_type = vector<T, 2>;
2222
using vector3_type = vector<T, 3>;
2323

24-
// should be normalized
25-
static Polar<T> createFromCartesian(const vector3_type coords)
24+
// input must be normalized
25+
static Polar<T> createFromCartesian(NBL_CONST_REF_ARG(vector3_type) dir)
2626
{
2727
Polar<T> retval;
28-
retval.theta = hlsl::acos(coords.z);
29-
retval.phi = hlsl::atan2(coords.y, coords.x);
28+
retval.theta = acos(dir.z);
29+
retval.phi = atan2(dir.y, dir.x);
3030
return retval;
3131
}
3232

33-
static vector3_type ToCartesian(const scalar_type theta, const scalar_type phi)
33+
static vector3_type ToCartesian(NBL_CONST_REF_ARG(scalar_type) theta, NBL_CONST_REF_ARG(scalar_type) phi)
3434
{
35-
return vector<T, 3>(hlsl::cos(phi) * hlsl::cos(theta),
36-
hlsl::sin(phi) * hlsl::cos(theta),
37-
hlsl::sin(theta));
35+
return vector<T, 3>(
36+
cos(phi) * cos(theta),
37+
sin(phi) * cos(theta),
38+
sin(theta)
39+
);
3840
}
3941

4042
vector3_type getCartesian()
4143
{
4244
return ToCartesian(theta, phi);
4345
}
4446

45-
scalar_type theta;
46-
scalar_type phi;
47+
scalar_type theta; //! polar angle in range [0, PI]
48+
scalar_type phi; //! azimuthal angle in range [-PI, PI]
4749
};
4850

4951
}

src/nbl/asset/utils/CIESProfile.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <atomic>
44
#include "nbl/asset/filters/CBasicImageFilterCommon.h"
55
#include "nbl/builtin/hlsl/math/octahedral.hlsl"
6+
#include "nbl/builtin/hlsl/math/polar.hlsl"
67

78
using namespace nbl;
89
using namespace asset;
@@ -83,14 +84,6 @@ const CIESProfile::IES_STORAGE_FORMAT CIESProfile::sample(IES_STORAGE_FORMAT the
8384
return s0 * (1.0 - u) + s1 * u;
8485
}
8586

86-
inline std::pair<float, float> CIESProfile::sphericalDirToRadians(const core::vectorSIMDf& dir)
87-
{
88-
const float theta = std::acos(std::clamp<float>(dir.z, -1.f, 1.f));
89-
const float phi = std::atan2(dir.y, dir.x);
90-
91-
return { theta, phi };
92-
}
93-
9487
template<class ExecutionPolicy>
9588
core::smart_refctd_ptr<asset::ICPUImageView> CIESProfile::createIESTexture(ExecutionPolicy&& policy, const float flatten, const bool fullDomainFlatten, uint32_t width, uint32_t height) const
9689
{
@@ -172,15 +165,15 @@ core::smart_refctd_ptr<asset::ICPUImageView> CIESProfile::createIESTexture(Execu
172165
// 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.
173166

174167
using Octahedral = hlsl::math::OctahedralTransform<hlsl::float32_t>;
168+
using Polar = hlsl::math::Polar<hlsl::float32_t>;
175169
const auto uv = Octahedral::vector2_type(position.x * vertInv, position.y * horiInv);
176170
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);
179-
const auto intensity = sample(theta, phi);
180-
171+
const auto polar = Polar::createFromCartesian(dir);
172+
const auto intensity = sample(polar.theta, polar.phi);
173+
181174
//! blend the IES texture with "flatten"
182175
double blendV = intensity * (1.0 - flatten);
183-
if (fullDomainFlatten && domainLo<=theta && theta<=domainHi || intensity >0.0)
176+
if (fullDomainFlatten && domainLo<= polar.theta && polar.theta<=domainHi || intensity >0.0)
184177
blendV += flattenTarget * flatten;
185178

186179
blendV *= maxValueRecip;

src/nbl/asset/utils/CIESProfile.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,34 +89,6 @@ namespace nbl
8989
private:
9090
CIESProfile(PhotometricType type, size_t hSize, size_t vSize)
9191
: type(type), version(V_SIZE), hAngles(hSize), vAngles(vSize), data(hSize* vSize) {}
92-
93-
// TODO for @Hazard, I would move it into separate file, we may use this abstraction somewhere too
94-
//! Returns spherical coordinates with physics convention in radians
95-
/*
96-
https://en.wikipedia.org/wiki/Spherical_coordinate_system#/media/File:3D_Spherical.svg
97-
Retval.first is "theta" polar angle in range [0, PI] & Retval.second "phi" is azimuthal angle
98-
in range [-PI, PI] range
99-
100-
Cartesian coordinates obtained from the spherical coordinates in Nabla
101-
are assumed to have radius equal to 1 and therefore always are
102-
103-
x = cos(phi)*sin(theta)
104-
y = sin(phi)*sin(theta)
105-
z = cos(theta)
106-
*/
107-
108-
static inline std::pair<float, float> sphericalDirToRadians(const core::vectorSIMDf& dir);
109-
110-
//! Octahedral coordinate mapping is following
111-
/*
112-
center is Z-
113-
U+ from center is X+
114-
V+ from center is Y+
115-
116-
when viewed as a texture, the net folds, and the apex where the seams join is Z+
117-
*/
118-
119-
static inline core::vectorSIMDf octahdronUVToDir(const float& u, const float& v);
12092

12193
void setCandelaValue(size_t i, size_t j, IES_STORAGE_FORMAT val) { data[vAngles.size() * i + j] = val; }
12294

0 commit comments

Comments
 (0)