Skip to content

Commit d41ab5b

Browse files
Merge pull request #994 from Devsh-Graphics-Programming/mCleanup
Minor post master merge clean-up
2 parents 5bb8c6a + b1ef469 commit d41ab5b

14 files changed

Lines changed: 144 additions & 349 deletions

File tree

include/nbl/asset/utils/CQuantQuaternionCache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
#include "nbl/asset/utils/CDirQuantCacheBase.h"
10+
#include "nbl/builtin/hlsl/math/quaternions.hlsl"
1011

1112

1213
namespace nbl
@@ -68,4 +69,4 @@ class CQuantQuaternionCache : public CDirQuantCacheBase<impl::Projection,impl::Q
6869

6970
}
7071
}
71-
#endif
72+
#endif

include/nbl/builtin/hlsl/math/linalg/matrix_utils/transformation_matrix_utils.hlsl renamed to include/nbl/builtin/hlsl/math/linalg/basic.hlsl

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_UTILS_TRANSFORMATION_MATRIX_UTILS_INCLUDED_
2-
#define _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_UTILS_TRANSFORMATION_MATRIX_UTILS_INCLUDED_
3-
#include <nbl/builtin/hlsl/math/quaternions.hlsl>
1+
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_BASIC_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_MATH_LINALG_BASIC_INCLUDED_
43
// TODO: remove this header when deleting vectorSIMDf.hlsl
54
#ifndef __HLSL_VERSION
65
#include <nbl/core/math/glslFunctions.h>
76
#include "vectorSIMD.h"
87
#endif
8+
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
99
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
1010
#include <nbl/builtin/hlsl/macros.h>
1111

@@ -53,10 +53,50 @@ inline matrix<T, NOut, MOut> truncate(const NBL_CONST_REF_ARG(matrix<T, NIn, MIn
5353
return retval;
5454
}
5555

56-
template<typename T, int N>
57-
inline matrix<T, 3, 3> getSub3x3(NBL_CONST_REF_ARG(matrix<T, N, 4>) mat)
56+
namespace impl
57+
{
58+
template<uint16_t MOut, uint16_t MIn, typename T>
59+
struct zero_expand_helper
60+
{
61+
static vector<T, MOut> __call(const vector<T, MIn> inVec)
62+
{
63+
return vector<T, MOut>(inVec, vector<T, MOut - MIn>(0));
64+
}
65+
};
66+
template<uint16_t M, typename T>
67+
struct zero_expand_helper<M,M,T>
5868
{
59-
return matrix<T, 3, 3>(mat);
69+
static vector<T, M> __call(const vector<T, M> inVec)
70+
{
71+
return inVec;
72+
}
73+
};
74+
}
75+
76+
template<uint16_t MOut, uint16_t MIn, typename T NBL_FUNC_REQUIRES(MOut >= MIn)
77+
vector<T, MOut> zero_expand(vector<T, MIn> inVec)
78+
{
79+
return impl::zero_expand_helper<MOut, MIn, T>::__call(inVec);
80+
}
81+
82+
template <uint16_t NOut, uint16_t MOut, uint16_t NIn, uint16_t MIn, typename T NBL_FUNC_REQUIRES(NOut >= NIn && MOut >= MIn)
83+
matrix<T, NOut, MOut> promote_affine(const matrix<T, NIn, MIn> inMatrix)
84+
{
85+
matrix<T, NOut, MOut> retval;
86+
87+
using out_row_t = hlsl::vector<T, MOut>;
88+
89+
NBL_UNROLL for (uint32_t row_i = 0; row_i < NIn; row_i++)
90+
{
91+
retval[row_i] = zero_expand<MOut, MIn>(inMatrix[row_i]);
92+
}
93+
NBL_UNROLL for (uint32_t row_i = NIn; row_i < NOut; row_i++)
94+
{
95+
retval[row_i] = promote<out_row_t>(0.0);
96+
if (row_i < MOut)
97+
retval[row_i][row_i] = T(1.0);
98+
}
99+
return retval;
60100
}
61101

62102
}
@@ -89,9 +129,28 @@ namespace impl
89129
return retval;
90130
}
91131
};
132+
133+
template<typename ScalarTo, typename ScalarFrom, uint16_t N>
134+
struct static_cast_helper<vector<ScalarTo, N>, vector<ScalarFrom, N>, void>
135+
{
136+
using To = vector<ScalarTo, N>;
137+
using From = vector<ScalarFrom, N>;
138+
139+
static inline To cast(From vec)
140+
{
141+
To retval;
142+
143+
NBL_UNROLL for (int i = 0; i < N; ++i)
144+
{
145+
retval[i] = hlsl::_static_cast<ScalarTo>(vec[i]);
146+
}
147+
148+
return retval;
149+
}
150+
};
92151
}
93152

94153
}
95154
}
96155

97-
#endif
156+
#endif

include/nbl/builtin/hlsl/math/linalg/fast_affine.hlsl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <nbl/builtin/hlsl/mpl.hlsl>
99
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
1010
#include <nbl/builtin/hlsl/concepts.hlsl>
11+
#include <nbl/builtin/hlsl/math/quaternions.hlsl>
1112

1213

1314
namespace nbl
@@ -77,6 +78,27 @@ vector<T,N> promoted_mul(NBL_CONST_REF_ARG(matrix<T,N,M>) lhs, const vector<T,P>
7778
return retval;
7879
}
7980

81+
template<typename T, uint32_t N>
82+
inline void setRotation(NBL_REF_ARG(matrix<T, N, 4>) outMat, NBL_CONST_REF_ARG(math::quaternion<T>) quat)
83+
{
84+
static_assert(N == 3 || N == 4);
85+
matrix<T, 3, 3> mat = _static_cast<matrix<T, 3, 3> >(quat);
86+
87+
outMat[0] = mat[0];
88+
outMat[1] = mat[1];
89+
outMat[2] = mat[2];
90+
}
91+
92+
template<typename T, uint32_t N>
93+
inline void setTranslation(NBL_REF_ARG(matrix<T, N, 4>) outMat, NBL_CONST_REF_ARG(vector<T, 3>) translation)
94+
{
95+
static_assert(N == 3 || N == 4);
96+
97+
outMat[0].w = translation.x;
98+
outMat[1].w = translation.y;
99+
outMat[2].w = translation.z;
100+
}
101+
80102
// useful for fast computation of a Normal Matrix
81103
template<typename T, int N>
82104
struct cofactors_base;
@@ -173,4 +195,4 @@ Mat3x4 pseudoInverse3x4(NBL_CONST_REF_ARG(Mat3x4) tform)
173195
}
174196
}
175197
}
176-
#endif
198+
#endif

include/nbl/builtin/hlsl/math/linalg/transform.hlsl

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <nbl/builtin/hlsl/mpl.hlsl>
88
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
99
#include <nbl/builtin/hlsl/concepts.hlsl>
10+
#include <nbl/builtin/hlsl/math/linalg/basic.hlsl>
1011

1112
namespace nbl
1213
{
@@ -17,83 +18,6 @@ namespace math
1718
namespace linalg
1819
{
1920

20-
/// Builds a rotation 3 * 3 matrix created from an axis vector and an angle.
21-
///
22-
/// @param angle Rotation angle expressed in radians.
23-
/// @param axis Rotation axis, must be normalized.
24-
///
25-
/// @tparam T A floating-point scalar type
26-
template <typename T>
27-
matrix<T, 3, 3> rotation_mat(T angle, const vector<T, 3> axis)
28-
{
29-
const T a = angle;
30-
const T c = cos(a);
31-
const T s = sin(a);
32-
33-
vector<T, 3> temp = hlsl::promote<vector<T, 3> >((T(1.0) - c) * axis);
34-
35-
matrix<T, 3, 3> rotation;
36-
rotation[0][0] = c + temp[0] * axis[0];
37-
rotation[0][1] = temp[1] * axis[0] - s * axis[2];
38-
rotation[0][2] = temp[2] * axis[0] + s * axis[1];
39-
40-
rotation[1][0] = temp[0] * axis[1] + s * axis[2];
41-
rotation[1][1] = c + temp[1] * axis[1];
42-
rotation[1][2] = temp[2] * axis[1] - s * axis[0];
43-
44-
rotation[2][0] = temp[0] * axis[2] - s * axis[1];
45-
rotation[2][1] = temp[1] * axis[2] + s * axis[0];
46-
rotation[2][2] = c + temp[2] * axis[2];
47-
48-
return rotation;
49-
}
50-
51-
namespace impl
52-
{
53-
template<uint16_t MOut, uint16_t MIn, typename T>
54-
struct zero_expand_helper
55-
{
56-
static vector<T, MOut> __call(const vector<T, MIn> inVec)
57-
{
58-
return vector<T, MOut>(inVec, vector<T, MOut - MIn>(0));
59-
}
60-
};
61-
template<uint16_t M, typename T>
62-
struct zero_expand_helper<M,M,T>
63-
{
64-
static vector<T, M> __call(const vector<T, M> inVec)
65-
{
66-
return inVec;
67-
}
68-
};
69-
}
70-
71-
template<uint16_t MOut, uint16_t MIn, typename T NBL_FUNC_REQUIRES(MOut >= MIn)
72-
vector<T, MOut> zero_expand(vector<T, MIn> inVec)
73-
{
74-
return impl::zero_expand_helper<MOut, MIn, T>::__call(inVec);
75-
}
76-
77-
template <uint16_t NOut, uint16_t MOut, uint16_t NIn, uint16_t MIn, typename T NBL_FUNC_REQUIRES(NOut >= NIn && MOut >= MIn)
78-
matrix<T, NOut, MOut> promote_affine(const matrix<T, NIn, MIn> inMatrix)
79-
{
80-
matrix<T, NOut, MOut> retval;
81-
82-
using out_row_t = hlsl::vector<T, MOut>;
83-
84-
NBL_UNROLL for (uint32_t row_i = 0; row_i < NIn; row_i++)
85-
{
86-
retval[row_i] = zero_expand<MOut, MIn>(inMatrix[row_i]);
87-
}
88-
NBL_UNROLL for (uint32_t row_i = NIn; row_i < NOut; row_i++)
89-
{
90-
retval[row_i] = promote<out_row_t>(0.0);
91-
if (row_i < MOut)
92-
retval[row_i][row_i] = T(1.0);
93-
}
94-
return retval;
95-
}
96-
9721
// /Arek: glm:: for normalize till dot product is fixed (ambiguity with glm namespace + linker issues)
9822
template<typename T>
9923
inline matrix<T, 3, 4> lhLookAt(
@@ -131,19 +55,6 @@ inline matrix<T, 3, 4> rhLookAt(
13155
return r;
13256
}
13357

134-
template<typename T, int16_t N, int16_t M, int16_t VecN>
135-
inline void setTranslation(NBL_REF_ARG(matrix<T, N, M>) outMat, NBL_CONST_REF_ARG(vector<T, VecN>) translation)
136-
{
137-
// TODO: not sure if it will be compatible with hlsl
138-
static_assert(M > 0 && N > 0);
139-
static_assert(M >= VecN);
140-
141-
NBL_CONSTEXPR int16_t indexOfTheLastRowComponent = M - 1;
142-
143-
for(int i = 0; i < VecN; ++i)
144-
outMat[i][indexOfTheLastRowComponent] = translation[i];
145-
}
146-
14758
}
14859
}
14960
}

include/nbl/builtin/hlsl/math/quaternions.hlsl

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

77
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
88
#include "nbl/builtin/hlsl/tgmath.hlsl"
9-
#include "nbl/builtin/hlsl/math/linalg/matrix_runtime_traits.hlsl"
9+
#include "nbl/builtin/hlsl/matrix_utils/matrix_runtime_traits.hlsl"
1010

1111
namespace nbl
1212
{
@@ -102,12 +102,12 @@ struct quaternion
102102

103103
static this_t create(NBL_CONST_REF_ARG(matrix_type) _m, const bool dontAssertValidMatrix=false)
104104
{
105-
scalar_type uniformScaleSq;
105+
scalar_type uniformColumnSqNorm;
106106
{
107107
// only orthogonal and uniform scale mats can be converted
108108
linalg::RuntimeTraits<matrix_type> traits = linalg::RuntimeTraits<matrix_type>::create(_m);
109-
bool valid = traits.orthogonal && !hlsl::isnan(traits.uniformScaleSq);
110-
uniformScaleSq = traits.uniformScaleSq;
109+
bool valid = traits.orthogonal && !hlsl::isnan(traits.uniformColumnSqNorm);
110+
uniformColumnSqNorm = traits.uniformColumnSqNorm;
111111

112112
if (dontAssertValidMatrix)
113113
{
@@ -121,14 +121,14 @@ struct quaternion
121121
else
122122
assert(valid);
123123
}
124-
if (uniformScaleSq < numeric_limits<scalar_type>::min)
124+
if (uniformColumnSqNorm < numeric_limits<scalar_type>::min)
125125
{
126126
this_t retval;
127127
retval.data = hlsl::promote<data_type>(bit_cast<scalar_type>(numeric_limits<scalar_type>::quiet_NaN));
128128
return retval;
129129
}
130130

131-
const scalar_type uniformScale = hlsl::sqrt(uniformScaleSq);
131+
const scalar_type uniformScale = hlsl::sqrt(uniformColumnSqNorm);
132132
matrix_type m = _m;
133133
m /= uniformScale;
134134

include/nbl/builtin/hlsl/math/thin_lens_projection.hlsl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,30 @@ inline matrix<FloatingPoint, 4, 4> lhProjectionOrthoMatrix(FloatingPoint widthOf
8282

8383
}
8484
}
85+
86+
template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
87+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixPerspectiveFovRH(FloatingPoint fieldOfViewRadians, FloatingPoint aspectRatio, FloatingPoint zNear, FloatingPoint zFar)
88+
{
89+
return math::thin_lens::rhPerspectiveFovMatrix(fieldOfViewRadians, aspectRatio, zNear, zFar);
90+
}
91+
template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
92+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixPerspectiveFovLH(FloatingPoint fieldOfViewRadians, FloatingPoint aspectRatio, FloatingPoint zNear, FloatingPoint zFar)
93+
{
94+
return math::thin_lens::lhPerspectiveFovMatrix(fieldOfViewRadians, aspectRatio, zNear, zFar);
95+
}
96+
97+
template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
98+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixOrthoRH(FloatingPoint widthOfViewVolume, FloatingPoint heightOfViewVolume, FloatingPoint zNear, FloatingPoint zFar)
99+
{
100+
return math::thin_lens::rhProjectionOrthoMatrix(widthOfViewVolume, heightOfViewVolume, zNear, zFar);
101+
}
102+
template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
103+
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixOrthoLH(FloatingPoint widthOfViewVolume, FloatingPoint heightOfViewVolume, FloatingPoint zNear, FloatingPoint zFar)
104+
{
105+
return math::thin_lens::lhProjectionOrthoMatrix(widthOfViewVolume, heightOfViewVolume, zNear, zFar);
106+
}
107+
85108
}
86109
}
87110

88-
#endif
111+
#endif

include/nbl/builtin/hlsl/math/linalg/matrix_runtime_traits.hlsl renamed to include/nbl/builtin/hlsl/matrix_utils/matrix_runtime_traits.hlsl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_RUNTIME_TRAITS_INCLUDED_
5-
#define _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_RUNTIME_TRAITS_INCLUDED_
4+
#ifndef _NBL_BUILTIN_HLSL_MATRIX_UTILS_MATRIX_RUNTIME_TRAITS_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_MATRIX_UTILS_MATRIX_RUNTIME_TRAITS_INCLUDED_
66

77
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
88
#include "nbl/builtin/hlsl/tgmath.hlsl"
@@ -38,25 +38,25 @@ struct RuntimeTraits
3838
}
3939
{
4040
const matrix_t m_T = hlsl::transpose(m);
41-
scalar_t uniformScaleSq = hlsl::dot(m_T[0], m_T[0]);
41+
scalar_t uniformColumnSqNorm = hlsl::dot(m_T[0], m_T[0]);
4242
NBL_UNROLL for (uint16_t i = 1; i < N; i++)
4343
{
44-
if (!testing::relativeApproxCompare(hlsl::dot(m_T[i], m_T[i]), uniformScaleSq, 1e-4))
44+
if (!testing::relativeApproxCompare(hlsl::dot(m_T[i], m_T[i]), uniformColumnSqNorm, 1e-4))
4545
{
46-
uniformScaleSq = bit_cast<scalar_t>(numeric_limits<scalar_t>::quiet_NaN);
46+
uniformColumnSqNorm = bit_cast<scalar_t>(numeric_limits<scalar_t>::quiet_NaN);
4747
break;
4848
}
4949
}
5050

51-
retval.uniformScaleSq = uniformScaleSq;
52-
retval.orthonormal = retval.orthogonal && testing::relativeApproxCompare(uniformScaleSq, scalar_t(1.0), 1e-5);
51+
retval.uniformColumnSqNorm = uniformColumnSqNorm;
52+
retval.orthonormal = retval.orthogonal && testing::relativeApproxCompare(uniformColumnSqNorm, scalar_t(1.0), 1e-5);
5353
}
5454
return retval;
5555
}
5656

5757
bool invertible;
5858
bool orthogonal;
59-
scalar_t uniformScaleSq; // TODO: rename to `uniformColumnSqNorm` and move this whole header to `nbl/builtin/hlsl/matrix_utils/` and associated namespace
59+
scalar_t uniformColumnSqNorm;
6060
bool orthonormal;
6161
};
6262

0 commit comments

Comments
 (0)