Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/nbl/asset/utils/CQuantQuaternionCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#include "nbl/asset/utils/CDirQuantCacheBase.h"
#include "nbl/builtin/hlsl/math/quaternions.hlsl"


namespace nbl
Expand Down Expand Up @@ -68,4 +69,4 @@ class CQuantQuaternionCache : public CDirQuantCacheBase<impl::Projection,impl::Q

}
}
#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_UTILS_TRANSFORMATION_MATRIX_UTILS_INCLUDED_
#define _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_UTILS_TRANSFORMATION_MATRIX_UTILS_INCLUDED_
#include <nbl/builtin/hlsl/math/quaternions.hlsl>
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_BASIC_INCLUDED_
#define _NBL_BUILTIN_HLSL_MATH_LINALG_BASIC_INCLUDED_
// TODO: remove this header when deleting vectorSIMDf.hlsl
#ifndef __HLSL_VERSION
#include <nbl/core/math/glslFunctions.h>
#include "vectorSIMD.h"
#endif
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
#include <nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl>
#include <nbl/builtin/hlsl/macros.h>

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

template<typename T, int N>
inline matrix<T, 3, 3> getSub3x3(NBL_CONST_REF_ARG(matrix<T, N, 4>) mat)
namespace impl
{
template<uint16_t MOut, uint16_t MIn, typename T>
struct zero_expand_helper
{
static vector<T, MOut> __call(const vector<T, MIn> inVec)
{
return vector<T, MOut>(inVec, vector<T, MOut - MIn>(0));
}
};
template<uint16_t M, typename T>
struct zero_expand_helper<M,M,T>
{
return matrix<T, 3, 3>(mat);
static vector<T, M> __call(const vector<T, M> inVec)
{
return inVec;
}
};
}

template<uint16_t MOut, uint16_t MIn, typename T NBL_FUNC_REQUIRES(MOut >= MIn)
vector<T, MOut> zero_expand(vector<T, MIn> inVec)
{
return impl::zero_expand_helper<MOut, MIn, T>::__call(inVec);
}

template <uint16_t NOut, uint16_t MOut, uint16_t NIn, uint16_t MIn, typename T NBL_FUNC_REQUIRES(NOut >= NIn && MOut >= MIn)
matrix<T, NOut, MOut> promote_affine(const matrix<T, NIn, MIn> inMatrix)
{
matrix<T, NOut, MOut> retval;

using out_row_t = hlsl::vector<T, MOut>;

NBL_UNROLL for (uint32_t row_i = 0; row_i < NIn; row_i++)
{
retval[row_i] = zero_expand<MOut, MIn>(inMatrix[row_i]);
}
NBL_UNROLL for (uint32_t row_i = NIn; row_i < NOut; row_i++)
{
retval[row_i] = promote<out_row_t>(0.0);
if (row_i < MOut)
retval[row_i][row_i] = T(1.0);
}
return retval;
}

}
Expand Down Expand Up @@ -89,9 +129,28 @@ namespace impl
return retval;
}
};

template<typename ScalarTo, typename ScalarFrom, uint16_t N>
struct static_cast_helper<vector<ScalarTo, N>, vector<ScalarFrom, N>, void>
{
using To = vector<ScalarTo, N>;
using From = vector<ScalarFrom, N>;

static inline To cast(From vec)
{
To retval;

NBL_UNROLL for (int i = 0; i < N; ++i)
{
retval[i] = hlsl::_static_cast<ScalarTo>(vec[i]);
}

return retval;
}
};
}

}
}

#endif
#endif
24 changes: 23 additions & 1 deletion include/nbl/builtin/hlsl/math/linalg/fast_affine.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <nbl/builtin/hlsl/mpl.hlsl>
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
#include <nbl/builtin/hlsl/concepts.hlsl>
#include <nbl/builtin/hlsl/math/quaternions.hlsl>


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

template<typename T, uint32_t N>
inline void setRotation(NBL_REF_ARG(matrix<T, N, 4>) outMat, NBL_CONST_REF_ARG(math::quaternion<T>) quat)
{
static_assert(N == 3 || N == 4);
matrix<T, 3, 3> mat = _static_cast<matrix<T, 3, 3> >(quat);

outMat[0] = mat[0];
outMat[1] = mat[1];
outMat[2] = mat[2];
}

template<typename T, uint32_t N>
inline void setTranslation(NBL_REF_ARG(matrix<T, N, 4>) outMat, NBL_CONST_REF_ARG(vector<T, 3>) translation)
{
static_assert(N == 3 || N == 4);

outMat[0].w = translation.x;
outMat[1].w = translation.y;
outMat[2].w = translation.z;
}

// useful for fast computation of a Normal Matrix
template<typename T, int N>
struct cofactors_base;
Expand Down Expand Up @@ -173,4 +195,4 @@ Mat3x4 pseudoInverse3x4(NBL_CONST_REF_ARG(Mat3x4) tform)
}
}
}
#endif
#endif
91 changes: 1 addition & 90 deletions include/nbl/builtin/hlsl/math/linalg/transform.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <nbl/builtin/hlsl/mpl.hlsl>
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl>
#include <nbl/builtin/hlsl/concepts.hlsl>
#include <nbl/builtin/hlsl/math/linalg/basic.hlsl>

namespace nbl
{
Expand All @@ -17,83 +18,6 @@ namespace math
namespace linalg
{

/// Builds a rotation 3 * 3 matrix created from an axis vector and an angle.
///
/// @param angle Rotation angle expressed in radians.
/// @param axis Rotation axis, must be normalized.
///
/// @tparam T A floating-point scalar type
template <typename T>
matrix<T, 3, 3> rotation_mat(T angle, const vector<T, 3> axis)
{
const T a = angle;
const T c = cos(a);
const T s = sin(a);

vector<T, 3> temp = hlsl::promote<vector<T, 3> >((T(1.0) - c) * axis);

matrix<T, 3, 3> rotation;
rotation[0][0] = c + temp[0] * axis[0];
rotation[0][1] = temp[1] * axis[0] - s * axis[2];
rotation[0][2] = temp[2] * axis[0] + s * axis[1];

rotation[1][0] = temp[0] * axis[1] + s * axis[2];
rotation[1][1] = c + temp[1] * axis[1];
rotation[1][2] = temp[2] * axis[1] - s * axis[0];

rotation[2][0] = temp[0] * axis[2] - s * axis[1];
rotation[2][1] = temp[1] * axis[2] + s * axis[0];
rotation[2][2] = c + temp[2] * axis[2];

return rotation;
}

namespace impl
{
template<uint16_t MOut, uint16_t MIn, typename T>
struct zero_expand_helper
{
static vector<T, MOut> __call(const vector<T, MIn> inVec)
{
return vector<T, MOut>(inVec, vector<T, MOut - MIn>(0));
}
};
template<uint16_t M, typename T>
struct zero_expand_helper<M,M,T>
{
static vector<T, M> __call(const vector<T, M> inVec)
{
return inVec;
}
};
}

template<uint16_t MOut, uint16_t MIn, typename T NBL_FUNC_REQUIRES(MOut >= MIn)
vector<T, MOut> zero_expand(vector<T, MIn> inVec)
{
return impl::zero_expand_helper<MOut, MIn, T>::__call(inVec);
}

template <uint16_t NOut, uint16_t MOut, uint16_t NIn, uint16_t MIn, typename T NBL_FUNC_REQUIRES(NOut >= NIn && MOut >= MIn)
matrix<T, NOut, MOut> promote_affine(const matrix<T, NIn, MIn> inMatrix)
{
matrix<T, NOut, MOut> retval;

using out_row_t = hlsl::vector<T, MOut>;

NBL_UNROLL for (uint32_t row_i = 0; row_i < NIn; row_i++)
{
retval[row_i] = zero_expand<MOut, MIn>(inMatrix[row_i]);
}
NBL_UNROLL for (uint32_t row_i = NIn; row_i < NOut; row_i++)
{
retval[row_i] = promote<out_row_t>(0.0);
if (row_i < MOut)
retval[row_i][row_i] = T(1.0);
}
return retval;
}

// /Arek: glm:: for normalize till dot product is fixed (ambiguity with glm namespace + linker issues)
template<typename T>
inline matrix<T, 3, 4> lhLookAt(
Expand Down Expand Up @@ -131,19 +55,6 @@ inline matrix<T, 3, 4> rhLookAt(
return r;
}

template<typename T, int16_t N, int16_t M, int16_t VecN>
inline void setTranslation(NBL_REF_ARG(matrix<T, N, M>) outMat, NBL_CONST_REF_ARG(vector<T, VecN>) translation)
{
// TODO: not sure if it will be compatible with hlsl
static_assert(M > 0 && N > 0);
static_assert(M >= VecN);

NBL_CONSTEXPR int16_t indexOfTheLastRowComponent = M - 1;

for(int i = 0; i < VecN; ++i)
outMat[i][indexOfTheLastRowComponent] = translation[i];
}

}
}
}
Expand Down
12 changes: 6 additions & 6 deletions include/nbl/builtin/hlsl/math/quaternions.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "nbl/builtin/hlsl/cpp_compat.hlsl"
#include "nbl/builtin/hlsl/tgmath.hlsl"
#include "nbl/builtin/hlsl/math/linalg/matrix_runtime_traits.hlsl"
#include "nbl/builtin/hlsl/matrix_utils/matrix_runtime_traits.hlsl"

namespace nbl
{
Expand Down Expand Up @@ -102,12 +102,12 @@ struct quaternion

static this_t create(NBL_CONST_REF_ARG(matrix_type) _m, const bool dontAssertValidMatrix=false)
{
scalar_type uniformScaleSq;
scalar_type uniformColumnSqNorm;
{
// only orthogonal and uniform scale mats can be converted
linalg::RuntimeTraits<matrix_type> traits = linalg::RuntimeTraits<matrix_type>::create(_m);
bool valid = traits.orthogonal && !hlsl::isnan(traits.uniformScaleSq);
uniformScaleSq = traits.uniformScaleSq;
bool valid = traits.orthogonal && !hlsl::isnan(traits.uniformColumnSqNorm);
uniformColumnSqNorm = traits.uniformColumnSqNorm;

if (dontAssertValidMatrix)
{
Expand All @@ -121,14 +121,14 @@ struct quaternion
else
assert(valid);
}
if (uniformScaleSq < numeric_limits<scalar_type>::min)
if (uniformColumnSqNorm < numeric_limits<scalar_type>::min)
{
this_t retval;
retval.data = hlsl::promote<data_type>(bit_cast<scalar_type>(numeric_limits<scalar_type>::quiet_NaN));
return retval;
}

const scalar_type uniformScale = hlsl::sqrt(uniformScaleSq);
const scalar_type uniformScale = hlsl::sqrt(uniformColumnSqNorm);
matrix_type m = _m;
m /= uniformScale;

Expand Down
25 changes: 24 additions & 1 deletion include/nbl/builtin/hlsl/math/thin_lens_projection.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,30 @@ inline matrix<FloatingPoint, 4, 4> lhProjectionOrthoMatrix(FloatingPoint widthOf

}
}

template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixPerspectiveFovRH(FloatingPoint fieldOfViewRadians, FloatingPoint aspectRatio, FloatingPoint zNear, FloatingPoint zFar)
{
return math::thin_lens::rhPerspectiveFovMatrix(fieldOfViewRadians, aspectRatio, zNear, zFar);
}
template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixPerspectiveFovLH(FloatingPoint fieldOfViewRadians, FloatingPoint aspectRatio, FloatingPoint zNear, FloatingPoint zFar)
{
return math::thin_lens::lhPerspectiveFovMatrix(fieldOfViewRadians, aspectRatio, zNear, zFar);
}

template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixOrthoRH(FloatingPoint widthOfViewVolume, FloatingPoint heightOfViewVolume, FloatingPoint zNear, FloatingPoint zFar)
{
return math::thin_lens::rhProjectionOrthoMatrix(widthOfViewVolume, heightOfViewVolume, zNear, zFar);
}
template<typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPoint<FloatingPoint>)
inline matrix<FloatingPoint, 4, 4> buildProjectionMatrixOrthoLH(FloatingPoint widthOfViewVolume, FloatingPoint heightOfViewVolume, FloatingPoint zNear, FloatingPoint zFar)
{
return math::thin_lens::lhProjectionOrthoMatrix(widthOfViewVolume, heightOfViewVolume, zNear, zFar);
}

}
}

#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_RUNTIME_TRAITS_INCLUDED_
#define _NBL_BUILTIN_HLSL_MATH_LINALG_MATRIX_RUNTIME_TRAITS_INCLUDED_
#ifndef _NBL_BUILTIN_HLSL_MATRIX_UTILS_MATRIX_RUNTIME_TRAITS_INCLUDED_
#define _NBL_BUILTIN_HLSL_MATRIX_UTILS_MATRIX_RUNTIME_TRAITS_INCLUDED_

#include "nbl/builtin/hlsl/cpp_compat.hlsl"
#include "nbl/builtin/hlsl/tgmath.hlsl"
Expand Down Expand Up @@ -38,25 +38,25 @@ struct RuntimeTraits
}
{
const matrix_t m_T = hlsl::transpose(m);
scalar_t uniformScaleSq = hlsl::dot(m_T[0], m_T[0]);
scalar_t uniformColumnSqNorm = hlsl::dot(m_T[0], m_T[0]);
NBL_UNROLL for (uint16_t i = 1; i < N; i++)
{
if (!testing::relativeApproxCompare(hlsl::dot(m_T[i], m_T[i]), uniformScaleSq, 1e-4))
if (!testing::relativeApproxCompare(hlsl::dot(m_T[i], m_T[i]), uniformColumnSqNorm, 1e-4))
{
uniformScaleSq = bit_cast<scalar_t>(numeric_limits<scalar_t>::quiet_NaN);
uniformColumnSqNorm = bit_cast<scalar_t>(numeric_limits<scalar_t>::quiet_NaN);
break;
}
}

retval.uniformScaleSq = uniformScaleSq;
retval.orthonormal = retval.orthogonal && testing::relativeApproxCompare(uniformScaleSq, scalar_t(1.0), 1e-5);
retval.uniformColumnSqNorm = uniformColumnSqNorm;
retval.orthonormal = retval.orthogonal && testing::relativeApproxCompare(uniformColumnSqNorm, scalar_t(1.0), 1e-5);
}
return retval;
}

bool invertible;
bool orthogonal;
scalar_t uniformScaleSq; // TODO: rename to `uniformColumnSqNorm` and move this whole header to `nbl/builtin/hlsl/matrix_utils/` and associated namespace
scalar_t uniformColumnSqNorm;
bool orthonormal;
};

Expand Down
Loading
Loading