88#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
99#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450 .hlsl>
1010#include <nbl/builtin/hlsl/ieee754.hlsl>
11- #include <nbl/builtin/hlsl/concepts.hlsl>
11+ #include <nbl/builtin/hlsl/concepts/core .hlsl>
1212#include <nbl/builtin/hlsl/concepts/vector .hlsl>
13+ #include <nbl/builtin/hlsl/concepts/matrix .hlsl>
1314
1415#ifndef __HLSL_VERSION
1516#include <bitset>
@@ -21,69 +22,54 @@ namespace hlsl
2122{
2223namespace cpp_compat_intrinsics_impl
2324{
24- template<typename T>
25- struct dot_helper
25+
26+ // DOT
27+
28+ template<typename T NBL_STRUCT_CONSTRAINABLE>
29+ struct dot_helper;
30+
31+ template<typename Vectorial>
32+ NBL_PARTIAL_REQ_TOP (concepts::Vectorial<Vectorial>)
33+ struct dot_helper<Vectorial NBL_PARTIAL_REQ_BOT (concepts::Vectorial<Vectorial>) >
2634{
27- using scalar_type = typename vector_traits<T>::scalar_type;
35+ using scalar_type = typename vector_traits<Vectorial>::scalar_type;
36+ static const uint32_t ArrayDim = 3 ; // vector_traits<Vectorial>::Dimension;
2837
29- static inline scalar_type __call (NBL_CONST_REF_ARG (T ) lhs, NBL_CONST_REF_ARG (T ) rhs)
38+ static inline scalar_type __call (NBL_CONST_REF_ARG (Vectorial ) lhs, NBL_CONST_REF_ARG (Vectorial ) rhs)
3039 {
31- static array_get<T, scalar_type> getter;
32- scalar_type retval = getter (lhs, 0 ) * getter (rhs, 0 );
40+ static array_get<Vectorial, scalar_type> getter;
3341
34- static const uint32_t ArrayDim = vector_traits<T>::Dimension ;
42+ scalar_type retval = getter (lhs, 0 ) * getter (rhs, 0 ) ;
3543 for (uint32_t i = 1 ; i < ArrayDim; ++i)
3644 retval = retval + getter (lhs, i) * getter (rhs, i);
3745
3846 return retval;
3947 }
4048};
4149
42- #define DEFINE_BUILTIN_VECTOR_SPECIALIZATION (FLOAT_TYPE, RETURN_VALUE)\
43- template<uint32_t N>\
44- struct dot_helper<vector <FLOAT_TYPE, N> >\
45- {\
46- using VectorType = vector <FLOAT_TYPE, N>;\
47- using ScalarType = typename vector_traits<VectorType>::scalar_type;\
48- \
49- static inline ScalarType __call (NBL_CONST_REF_ARG (VectorType) lhs, NBL_CONST_REF_ARG (VectorType) rhs)\
50- {\
51- return RETURN_VALUE;\
52- }\
53- };\
54-
55- #ifdef __HLSL_VERSION
56- #define BUILTIN_VECTOR_SPECIALIZATION_RET_VAL dot (lhs, rhs)
57- #else
58- #define BUILTIN_VECTOR_SPECIALIZATION_RET_VAL glm::dot (lhs, rhs)
59- #endif
60-
61- DEFINE_BUILTIN_VECTOR_SPECIALIZATION (float16_t, BUILTIN_VECTOR_SPECIALIZATION_RET_VAL)
62- DEFINE_BUILTIN_VECTOR_SPECIALIZATION (float32_t, BUILTIN_VECTOR_SPECIALIZATION_RET_VAL)
63- DEFINE_BUILTIN_VECTOR_SPECIALIZATION (float64_t, BUILTIN_VECTOR_SPECIALIZATION_RET_VAL)
64-
65- #undef BUILTIN_VECTOR_SPECIALIZATION_RET_VAL
66- #undef DEFINE_BUILTIN_VECTOR_SPECIALIZATION
67-
6850// CROSS
6951
7052template<typename T NBL_STRUCT_CONSTRAINABLE>
7153struct cross_helper;
7254
73- //! this specialization will work only with hlsl::vector<T, 3> type
74- template<typename FloatingPointVector>
75- NBL_PARTIAL_REQ_TOP (concepts::FloatingPointVector<FloatingPointVector> && (vector_traits<FloatingPointVector>::Dimension == 3 ))
76- struct cross_helper<FloatingPointVector NBL_PARTIAL_REQ_BOT (concepts::FloatingPointVector<FloatingPointVector> && (vector_traits<FloatingPointVector>::Dimension == 3 )) >
55+ template<typename FloatingPointLikeVectorial>
56+ NBL_PARTIAL_REQ_TOP (concepts::FloatingPointLikeVectorial<FloatingPointLikeVectorial> && (vector_traits<FloatingPointLikeVectorial>::Dimension == 3 ))
57+ struct cross_helper<FloatingPointLikeVectorial NBL_PARTIAL_REQ_BOT (concepts::FloatingPointLikeVectorial<FloatingPointLikeVectorial> && (vector_traits<FloatingPointLikeVectorial>::Dimension == 3 )) >
7758{
78- static FloatingPointVector __call (NBL_CONST_REF_ARG (FloatingPointVector ) lhs, NBL_CONST_REF_ARG (FloatingPointVector ) rhs)
59+ static FloatingPointLikeVectorial __call (NBL_CONST_REF_ARG (FloatingPointLikeVectorial ) lhs, NBL_CONST_REF_ARG (FloatingPointLikeVectorial ) rhs)
7960 {
8061#ifdef __HLSL_VERSION
81- return spirv::cross (lhs, rhs);
62+ if (hlsl::is_vector_v<FloatingPointLikeVectorial>)
63+ return spirv::cross (lhs, rhs);
8264#else
83- FloatingPointVector output;
84- output.x = lhs[1 ] * rhs[2 ] - rhs[1 ] * lhs[2 ];
85- output.y = lhs[2 ] * rhs[0 ] - rhs[2 ] * lhs[0 ];
86- output.z = lhs[0 ] * rhs[1 ] - rhs[0 ] * lhs[1 ];
65+ using traits = hlsl::vector_traits<FloatingPointLikeVectorial>;
66+ array_get<FloatingPointLikeVectorial, typename traits::scalar_type> getter;
67+ array_set<FloatingPointLikeVectorial, typename traits::scalar_type> setter;
68+
69+ FloatingPointLikeVectorial output;
70+ setter (output, 0 , getter (lhs, 1 ) * getter (rhs, 2 ) - getter (rhs, 1 ) * getter (lhs, 2 ));
71+ setter (output, 1 , getter (lhs, 2 ) * getter (rhs, 0 ) - getter (rhs, 2 ) * getter (lhs, 0 ));
72+ setter (output, 2 , getter (lhs, 0 ) * getter (rhs, 1 ) - getter (rhs, 0 ) * getter (lhs, 1 ));
8773
8874 return output;
8975#endif
@@ -157,7 +143,7 @@ struct clamp_helper<Vector NBL_PARTIAL_REQ_BOT(is_vector_v<Vector>) >
157143
158144// FIND_MSB
159145
160- template<typename Integer>
146+ template<typename Integer NBL_STRUCT_CONSTRAINABLE >
161147struct find_msb_helper;
162148
163149template<>
@@ -272,7 +258,7 @@ struct find_msb_helper<EnumType>
272258
273259// FIND_LSB
274260
275- template<typename Integer>
261+ template<typename Integer NBL_STRUCT_CONSTRAINABLE >
276262struct find_lsb_helper;
277263
278264template<>
@@ -435,26 +421,31 @@ struct bitReverse_helper<Vector NBL_PARTIAL_REQ_BOT(concepts::Vectorial<Vector>)
435421 }
436422};
437423
438- template<typename Matrix>
424+ template<typename Matrix NBL_STRUCT_CONSTRAINABLE >
439425struct transpose_helper;
440426
441- template<typename T, int N, int M>
442- struct transpose_helper<matrix <T, N, M> >
427+ template<typename Matrix>
428+ NBL_PARTIAL_REQ_TOP (concepts::Matrix<Matrix>)
429+ struct transpose_helper<Matrix NBL_PARTIAL_REQ_BOT (concepts::Matrix<Matrix>) >
443430{
444- using transposed_t = typename matrix_traits<matrix <T, N, M> >::transposed_type;
431+ using transposed_t = typename matrix_traits<Matrix >::transposed_type;
445432
446- static transposed_t __call (NBL_CONST_REF_ARG (matrix <T, N, M> ) m)
433+ static transposed_t __call (NBL_CONST_REF_ARG (Matrix ) m)
447434 {
448435#ifdef __HLSL_VERSION
449436 return spirv::transpose (m);
450437#else
451- return reinterpret_cast<transposed_t&>(glm::transpose (reinterpret_cast<typename matrix <T, N, M> ::Base const &>(m)));
438+ return reinterpret_cast<transposed_t&>(glm::transpose (reinterpret_cast<typename Matrix ::Base const &>(m)));
452439#endif
453440 }
454441};
455442
443+ template<typename LhsT, typename RhsT NBL_STRUCT_CONSTRAINABLE>
444+ struct mul_helper;
445+
456446template<typename LhsT, typename RhsT>
457- struct mul_helper
447+ NBL_PARTIAL_REQ_TOP (concepts::Matrix<LhsT> && (concepts::Matrix<RhsT> || concepts::Vector<RhsT>))
448+ struct mul_helper<LhsT, RhsT NBL_PARTIAL_REQ_BOT (concepts::Matrix<LhsT> && (concepts::Matrix<RhsT> || concepts::Vector<RhsT>)) >
458449{
459450 static inline mul_output_t<LhsT, RhsT> __call (LhsT lhs, RhsT rhs)
460451 {
@@ -498,8 +489,8 @@ template<typename Integer NBL_STRUCT_CONSTRAINABLE>
498489struct bitCount_helper;
499490
500491template<typename Integer>
501- NBL_PARTIAL_REQ_TOP (is_integral_v <Integer>)
502- struct bitCount_helper<Integer NBL_PARTIAL_REQ_BOT (is_integral_v <Integer>) >
492+ NBL_PARTIAL_REQ_TOP (concepts::IntegralScalar <Integer>)
493+ struct bitCount_helper<Integer NBL_PARTIAL_REQ_BOT (concepts::IntegralScalar <Integer>) >
503494{
504495 static bitcount_output_t<Integer> __call (NBL_CONST_REF_ARG (Integer) val)
505496 {
@@ -513,7 +504,6 @@ struct bitCount_helper<Integer NBL_PARTIAL_REQ_BOT(is_integral_v<Integer>) >
513504 }
514505
515506 return spirv::bitCount (val);
516-
517507#else
518508 using UnsignedInteger = typename hlsl::unsigned_integer_of_size_t<sizeof (Integer)>;
519509 constexpr int32_t BitCnt = sizeof (Integer) * 8u;
@@ -555,6 +545,7 @@ struct bitCount_helper<EnumT>
555545};
556546#endif
557547
548+ // TODO: length_helper partial specialization for emulated_float64_t
558549template<typename Vector NBL_STRUCT_CONSTRAINABLE>
559550struct length_helper;
560551
@@ -567,24 +558,25 @@ struct length_helper<Vector NBL_PARTIAL_REQ_BOT(concepts::FloatingPointVector<Ve
567558#ifdef __HLSL_VERSION
568559 return spirv::length (vec);
569560#else
570- return std::sqrt (dot_helper <Vector>::__call (vec, vec));
561+ return std::sqrt (length_helper <Vector>::__call (vec, vec));
571562#endif
572563 }
573564};
574565
575566template<typename Vector NBL_STRUCT_CONSTRAINABLE>
576567struct normalize_helper;
577568
578- template<typename Vector >
579- NBL_PARTIAL_REQ_TOP (concepts::FloatingPointVector<Vector >)
580- struct normalize_helper<Vector NBL_PARTIAL_REQ_BOT (concepts::FloatingPointVector<Vector >) >
569+ template<typename Vectorial >
570+ NBL_PARTIAL_REQ_TOP (concepts::FloatingPointLikeVectorial<Vectorial >)
571+ struct normalize_helper<Vectorial NBL_PARTIAL_REQ_BOT (concepts::FloatingPointLikeVectorial<Vectorial >) >
581572{
582- static inline Vector __call (NBL_CONST_REF_ARG (Vector ) vec)
573+ static inline Vectorial __call (NBL_CONST_REF_ARG (Vectorial ) vec)
583574 {
584575#ifdef __HLSL_VERSION
585- return spirv::normalize (vec);
576+ if (is_vector_v<Vectorial>)
577+ return spirv::normalize (vec);
586578#else
587- return vec / length_helper<Vector >::__call (vec);
579+ return vec / length_helper<Vectorial >::__call (vec);
588580#endif
589581 }
590582};
@@ -744,8 +736,8 @@ template<typename T NBL_STRUCT_CONSTRAINABLE>
744736struct inverse_helper;
745737
746738template<typename SquareMatrix>
747- NBL_PARTIAL_REQ_TOP (matrix_traits<SquareMatrix>::Square)
748- struct inverse_helper<SquareMatrix NBL_PARTIAL_REQ_BOT (matrix_traits<SquareMatrix>::Square) >
739+ NBL_PARTIAL_REQ_TOP (concepts::Matrix<SquareMatrix> && matrix_traits<SquareMatrix>::Square)
740+ struct inverse_helper<SquareMatrix NBL_PARTIAL_REQ_BOT (concepts::Matrix<SquareMatrix> && matrix_traits<SquareMatrix>::Square) >
749741{
750742 static SquareMatrix __call (NBL_CONST_REF_ARG (SquareMatrix) mat)
751743 {
0 commit comments