Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
781df86
split out new quaternion hlsl stuff over from hlsl path tracer example
keptsecret Dec 2, 2025
506c8a5
Merge branch 'master' into new_quaternion_hlsl
keptsecret Dec 11, 2025
c2fa565
Merge branch 'master' into new_quaternion_hlsl
keptsecret Dec 11, 2025
1bf0616
removed redundant constructors, some reorganize quaternions
keptsecret Dec 11, 2025
8745a33
added some checks to create from rot mat
keptsecret Dec 11, 2025
2a8451d
moved normalize, static_cast to helper specializations, norm and unno…
keptsecret Dec 12, 2025
a93fa26
fix some quaternion bugs
keptsecret Dec 12, 2025
35e78b9
merge master, fix conflicts
keptsecret Jan 5, 2026
2f33aa0
some fixes to quaternions
keptsecret Jan 6, 2026
a22d46a
implement quaternion slerp (might need optimizing?)
keptsecret Jan 7, 2026
f71cca1
minor optimization to slerp
keptsecret Jan 7, 2026
c39c78a
fix create from rotation matrix
keptsecret Jan 7, 2026
0b180c8
force constructor type with requires to avoid dxc implicit conversions
keptsecret Jan 7, 2026
de1b0d1
fixes to transformVector and other minor fixes
keptsecret Jan 7, 2026
96ef95d
added matrix runtime traits for checking orthogonality, uniform scale
keptsecret Jan 8, 2026
ca322c8
Merge branch 'master' into new_quaternion_hlsl
keptsecret Jan 13, 2026
a0acd35
matrix runtime traits stores uniform scale squared, changed calculati…
keptsecret Jan 13, 2026
d1c4a89
added more static_casts and new partial spec for flipIfRHSNegative
keptsecret Jan 13, 2026
e8a6488
account for no scale in transform vector
keptsecret Jan 13, 2026
c07365f
more comment info for pitch yaw roll, removed normalize truncated quat
keptsecret Jan 14, 2026
2aa275e
create from matrix restore scale correctly
keptsecret Jan 14, 2026
3869cb1
minor bug fixes to quaternions
keptsecret Jan 15, 2026
6853ca3
Merge branch 'master' into new_quaternion_hlsl
keptsecret Jan 15, 2026
a602fba
add some operators to hlsl::matrix in C++
Jan 15, 2026
4aa2368
factor out uniform scale from mat before convert
keptsecret Jan 15, 2026
5f02325
new vector comparison by orientation
keptsecret Jan 15, 2026
11f7f2e
return nan quaternion if uniform scale is 0
keptsecret Jan 15, 2026
266cd71
account for negative orientation, added check for 0 length vectors
keptsecret Jan 16, 2026
ce371c7
fixes create from matrix by using correct row-column indexing
keptsecret Jan 16, 2026
c8df31a
fix glm not liking unary - on swizzle
keptsecret Jan 16, 2026
f4b0b43
do quaternion inverse not as member function
keptsecret Jan 16, 2026
5c53ae5
added a vector length compare
keptsecret Jan 16, 2026
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
11 changes: 9 additions & 2 deletions include/nbl/builtin/hlsl/cpp_compat/matrix.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ struct matrix final : private glm::mat<N,M,T>
return *this;
}

friend matrix operator+(matrix const& lhs, matrix const& rhs){ return matrix(reinterpret_cast<Base const&>(lhs) + reinterpret_cast<Base const&>(rhs)); }
friend matrix operator-(matrix const& lhs, matrix const& rhs){ return matrix(reinterpret_cast<Base const&>(lhs) - reinterpret_cast<Base const&>(rhs)); }
// not sure how to forward this
//inline friend matrix operator*(matrix const& lhs, T rhs) {return matrix(reinterpret_cast<Base const&>(lhs)*rhs);}

// scalar compound assignment multiply and divide
inline matrix& operator*=(const T rhs) {return reinterpret_cast<matrix&>(Base::template operator*=(rhs));}
inline matrix& operator/=(const T rhs) {return reinterpret_cast<matrix&>(Base::template operator/=(rhs));}

inline friend matrix operator+(matrix const& lhs, matrix const& rhs){ return matrix(reinterpret_cast<Base const&>(lhs) + reinterpret_cast<Base const&>(rhs)); }
inline friend matrix operator-(matrix const& lhs, matrix const& rhs){ return matrix(reinterpret_cast<Base const&>(lhs) - reinterpret_cast<Base const&>(rhs)); }

template<uint16_t K>
inline friend matrix<T, N, K> mul(matrix const& lhs, matrix<T, M, K> const& rhs)
Expand Down
36 changes: 29 additions & 7 deletions include/nbl/builtin/hlsl/ieee754.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ struct flipSign_helper<Vectorial, BoolVector NBL_PARTIAL_REQ_BOT(concepts::Float
}
};

template <typename T NBL_STRUCT_CONSTRAINABLE>
template <typename T, typename U NBL_STRUCT_CONSTRAINABLE>
struct flipSignIfRHSNegative_helper;

template <typename FloatingPoint>
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointLikeScalar<FloatingPoint>)
struct flipSignIfRHSNegative_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeScalar<FloatingPoint>) >
struct flipSignIfRHSNegative_helper<FloatingPoint, FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeScalar<FloatingPoint>) >
{
static FloatingPoint __call(FloatingPoint val, FloatingPoint flip)
{
Expand All @@ -222,7 +222,7 @@ struct flipSignIfRHSNegative_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::

template <typename Vectorial>
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointLikeVectorial<Vectorial>)
struct flipSignIfRHSNegative_helper<Vectorial NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVectorial<Vectorial>) >
struct flipSignIfRHSNegative_helper<Vectorial, Vectorial NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVectorial<Vectorial>) >
{
static Vectorial __call(Vectorial val, Vectorial flip)
{
Expand All @@ -232,7 +232,29 @@ struct flipSignIfRHSNegative_helper<Vectorial NBL_PARTIAL_REQ_BOT(concepts::Floa

Vectorial output;
for (uint32_t i = 0; i < traits_v::Dimension; ++i)
setter(output, i, flipSignIfRHSNegative_helper<typename traits_v::scalar_type>::__call(getter_v(val, i), getter_v(flip, i)));
setter(output, i, flipSignIfRHSNegative_helper<typename traits_v::scalar_type,typename traits_v::scalar_type>::__call(getter_v(val, i), getter_v(flip, i)));

return output;
}
};

template <typename Vectorial, typename FloatingPoint>
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointLikeVectorial<Vectorial> && concepts::FloatingPointLikeScalar<FloatingPoint>)
struct flipSignIfRHSNegative_helper<Vectorial, FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVectorial<Vectorial> && concepts::FloatingPointLikeScalar<FloatingPoint>) >
{
static Vectorial __call(Vectorial val, FloatingPoint flip)
{
using traits_v = hlsl::vector_traits<Vectorial>;
array_get<Vectorial, typename traits_v::scalar_type> getter_v;
array_set<Vectorial, typename traits_v::scalar_type> setter;

using AsFloat = typename float_of_size<sizeof(FloatingPoint)>::type;
using AsUint = typename unsigned_integer_of_size<sizeof(FloatingPoint)>::type;
const AsUint signBitFlip = ieee754::traits<AsFloat>::signMask & ieee754::impl::bitCastToUintType(flip);

Vectorial output;
for (uint32_t i = 0; i < traits_v::Dimension; ++i)
setter(output, i, bit_cast<FloatingPoint>(ieee754::impl::bitCastToUintType(getter_v(val, i)) ^ signBitFlip));

return output;
}
Expand All @@ -245,10 +267,10 @@ NBL_CONSTEXPR_FUNC T flipSign(T val, U flip)
return impl::flipSign_helper<T, U>::__call(val, flip);
}

template <typename T>
NBL_CONSTEXPR_FUNC T flipSignIfRHSNegative(T val, T flip)
template <typename T, typename U=T>
NBL_CONSTEXPR_FUNC T flipSignIfRHSNegative(T val, U flip)
{
return impl::flipSignIfRHSNegative_helper<T>::__call(val, flip);
return impl::flipSignIfRHSNegative_helper<T, U>::__call(val, flip);
}

template <typename T NBL_FUNC_REQUIRES(hlsl::is_floating_point_v<T>)
Expand Down
68 changes: 68 additions & 0 deletions include/nbl/builtin/hlsl/math/linalg/matrix_runtime_traits.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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_

#include "nbl/builtin/hlsl/cpp_compat.hlsl"
#include "nbl/builtin/hlsl/tgmath.hlsl"
#include "nbl/builtin/hlsl/testing/relative_approx_compare.hlsl"
#include "nbl/builtin/hlsl/concepts/matrix.hlsl"
#include "nbl/builtin/hlsl/matrix_utils/matrix_traits.hlsl"

namespace nbl
{
namespace hlsl
{
namespace math
{
namespace linalg
{

template<typename T NBL_PRIMARY_REQUIRES(concepts::Matricial<T> && matrix_traits<T>::Square)
struct RuntimeTraits
{
using matrix_t = T;
using scalar_t = typename matrix_traits<T>::scalar_type;
NBL_CONSTEXPR_STATIC_INLINE uint16_t N = matrix_traits<T>::RowCount;

static RuntimeTraits<matrix_t> create(const matrix_t m)
{
RuntimeTraits<matrix_t> retval;
retval.invertible = !testing::relativeApproxCompare(hlsl::determinant(m), scalar_t(0.0), 1e-5);
{
bool orthogonal = true;
NBL_UNROLL for (uint16_t i = 0; i < N; i++)
orthogonal = orthogonal && testing::relativeApproxCompare(hlsl::dot(m[i], m[(i+1)%N]), scalar_t(0.0), 1e-4);
retval.orthogonal = orthogonal;
}
{
const matrix_t m_T = hlsl::transpose(m);
scalar_t uniformScaleSq = 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))
{
uniformScaleSq = 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);
}
return retval;
}

bool invertible;
bool orthogonal;
scalar_t uniformScaleSq;
bool orthonormal;
};

}
}
}
}

#endif
Loading
Loading