Skip to content

Commit 6fecfa9

Browse files
committed
Now every tgmath function utilize helpers
1 parent 949a972 commit 6fecfa9

File tree

5 files changed

+370
-112
lines changed

5 files changed

+370
-112
lines changed

include/nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,82 @@ struct max_helper<Vector NBL_PARTIAL_REQ_BOT(is_vector_v<Vector>) >
705705
}
706706
};
707707

708+
// DETERMINANT
709+
710+
template<typename T NBL_STRUCT_CONSTRAINABLE>
711+
struct determinant_helper;
712+
713+
template<typename SquareMatrix>
714+
NBL_PARTIAL_REQ_TOP(matrix_traits<SquareMatrix>::Square)
715+
struct determinant_helper<SquareMatrix NBL_PARTIAL_REQ_BOT(matrix_traits<SquareMatrix>::Square) >
716+
{
717+
static typename matrix_traits<SquareMatrix>::scalar_type __call(NBL_CONST_REF_ARG(SquareMatrix) mat)
718+
{
719+
#ifdef __HLSL_VERSION
720+
spirv::determinant(mat);
721+
#else
722+
return glm::determinant(reinterpret_cast<typename SquareMatrix::Base const&>(mat));
723+
#endif
724+
}
725+
};
726+
727+
// INVERSE
728+
729+
template<typename T NBL_STRUCT_CONSTRAINABLE>
730+
struct inverse_helper;
731+
732+
template<typename SquareMatrix>
733+
NBL_PARTIAL_REQ_TOP(matrix_traits<SquareMatrix>::Square)
734+
struct inverse_helper<SquareMatrix NBL_PARTIAL_REQ_BOT(matrix_traits<SquareMatrix>::Square) >
735+
{
736+
static SquareMatrix __call(NBL_CONST_REF_ARG(SquareMatrix) mat)
737+
{
738+
#ifdef __HLSL_VERSION
739+
return spirv::matrixInverse(mat);
740+
#else
741+
return reinterpret_cast<SquareMatrix&>(glm::inverse(reinterpret_cast<typename SquareMatrix::Base const&>(mat)));
742+
#endif
743+
}
744+
};
745+
746+
// RSQRT
747+
748+
template<typename T NBL_STRUCT_CONSTRAINABLE>
749+
struct rsqrt_helper;
750+
751+
template<typename FloatingPoint>
752+
NBL_PARTIAL_REQ_TOP(is_floating_point_v<FloatingPoint> && is_scalar_v<FloatingPoint>)
753+
struct rsqrt_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(is_floating_point_v<FloatingPoint> && is_scalar_v<FloatingPoint>) >
754+
{
755+
static FloatingPoint __call(NBL_CONST_REF_ARG(FloatingPoint) x)
756+
{
757+
// TODO: https://stackoverflow.com/a/62239778
758+
#ifdef __HLSL_VERSION
759+
return spirv::inverseSqrt(x);
760+
#else
761+
return 1.0f / std::sqrt(x);
762+
#endif
763+
}
764+
};
765+
766+
template<typename FloatingPointVector>
767+
NBL_PARTIAL_REQ_TOP(is_vector_v<FloatingPointVector> && is_floating_point_v<typename vector_traits<FloatingPointVector>::scalar_type>)
768+
struct rsqrt_helper<FloatingPointVector NBL_PARTIAL_REQ_BOT(is_vector_v<FloatingPointVector>&& is_floating_point_v<typename vector_traits<FloatingPointVector>::scalar_type>) >
769+
{
770+
static FloatingPointVector __call(NBL_CONST_REF_ARG(FloatingPointVector) x)
771+
{
772+
using traits = hlsl::vector_traits<FloatingPointVector>;
773+
array_get<FloatingPointVector, typename traits::scalar_type> getter;
774+
array_set<FloatingPointVector, typename traits::scalar_type> setter;
775+
776+
FloatingPointVector output;
777+
for (uint32_t i = 0; i < traits::Dimension; ++i)
778+
setter(output, i, rsqrt_helper<typename traits::scalar_type>::__call(getter(x, i)));
779+
780+
return output;
781+
}
782+
};
783+
708784
}
709785
}
710786
}

include/nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,12 @@ typename vector_traits<T>::scalar_type dot(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_R
6262
return cpp_compat_intrinsics_impl::dot_helper<T>::__call(lhs, rhs);
6363
}
6464

65-
// TODO: for clearer error messages, use concepts to ensure that input type is a square matrix
6665
// determinant not defined cause its implemented via hidden friend
6766
// https://stackoverflow.com/questions/67459950/why-is-a-friend-function-not-treated-as-a-member-of-a-namespace-of-a-class-it-wa
68-
template<typename T, uint16_t N>
69-
inline T determinant(NBL_CONST_REF_ARG(matrix<T, N, N>) m)
67+
template<typename Matrix>
68+
inline typename matrix_traits<Matrix>::scalar_type determinant(NBL_CONST_REF_ARG(Matrix) mat)
7069
{
71-
#ifdef __HLSL_VERSION
72-
spirv::determinant(m);
73-
#else
74-
return glm::determinant(reinterpret_cast<typename matrix<T, N, N>::Base const&>(m));
75-
#endif
70+
return cpp_compat_intrinsics_impl::determinant_helper<Matrix>::__call(mat);
7671
}
7772

7873
template<typename Integer>
@@ -87,16 +82,11 @@ inline typename cpp_compat_intrinsics_impl::find_msb_return_type<Integer>::type
8782
return cpp_compat_intrinsics_impl::find_msb_helper<Integer>::__call(val);
8883
}
8984

90-
// TODO: for clearer error messages, use concepts to ensure that input type is a square matrix
9185
// inverse not defined cause its implemented via hidden friend
92-
template<typename T, uint16_t N>
93-
inline matrix<T, N, N> inverse(NBL_CONST_REF_ARG(matrix<T, N, N>) m)
86+
template<typename Matrix>
87+
inline Matrix inverse(NBL_CONST_REF_ARG(Matrix) mat)
9488
{
95-
#ifdef __HLSL_VERSION
96-
return spirv::matrixInverse(m);
97-
#else
98-
return reinterpret_cast<matrix<T, N, N>&>(glm::inverse(reinterpret_cast<typename matrix<T, N, N>::Base const&>(m)));
99-
#endif
89+
return cpp_compat_intrinsics_impl::inverse_helper<Matrix>::__call(mat);
10090
}
10191

10292
// transpose not defined cause its implemented via hidden friend
@@ -128,12 +118,7 @@ inline T max(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
128118
template<typename FloatingPoint>
129119
inline FloatingPoint rsqrt(FloatingPoint x)
130120
{
131-
// TODO: https://stackoverflow.com/a/62239778
132-
#ifdef __HLSL_VERSION
133-
return spirv::inverseSqrt(x);
134-
#else
135-
return 1.0f / std::sqrt(x);
136-
#endif
121+
return cpp_compat_intrinsics_impl::rsqrt_helper<FloatingPoint>::__call(x);
137122
}
138123

139124
template<typename Integer>

0 commit comments

Comments
 (0)