8
8
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
9
9
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450 .hlsl>
10
10
#include <nbl/builtin/hlsl/ieee754.hlsl>
11
- #include <nbl/builtin/hlsl/concepts.hlsl>
11
+ #include <nbl/builtin/hlsl/concepts/core .hlsl>
12
12
#include <nbl/builtin/hlsl/concepts/vector .hlsl>
13
+ #include <nbl/builtin/hlsl/concepts/matrix .hlsl>
13
14
14
15
#ifndef __HLSL_VERSION
15
16
#include <bitset>
@@ -21,69 +22,54 @@ namespace hlsl
21
22
{
22
23
namespace cpp_compat_intrinsics_impl
23
24
{
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>) >
26
34
{
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;
28
37
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)
30
39
{
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;
33
41
34
- static const uint32_t ArrayDim = vector_traits<T>::Dimension ;
42
+ scalar_type retval = getter (lhs, 0 ) * getter (rhs, 0 ) ;
35
43
for (uint32_t i = 1 ; i < ArrayDim; ++i)
36
44
retval = retval + getter (lhs, i) * getter (rhs, i);
37
45
38
46
return retval;
39
47
}
40
48
};
41
49
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
-
68
50
// CROSS
69
51
70
52
template<typename T NBL_STRUCT_CONSTRAINABLE>
71
53
struct cross_helper;
72
54
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 )) >
77
58
{
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)
79
60
{
80
61
#ifdef __HLSL_VERSION
81
- return spirv::cross (lhs, rhs);
62
+ if (hlsl::is_vector_v<FloatingPointLikeVectorial>)
63
+ return spirv::cross (lhs, rhs);
82
64
#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 ));
87
73
88
74
return output;
89
75
#endif
@@ -157,7 +143,7 @@ struct clamp_helper<Vector NBL_PARTIAL_REQ_BOT(is_vector_v<Vector>) >
157
143
158
144
// FIND_MSB
159
145
160
- template<typename Integer>
146
+ template<typename Integer NBL_STRUCT_CONSTRAINABLE >
161
147
struct find_msb_helper;
162
148
163
149
template<>
@@ -272,7 +258,7 @@ struct find_msb_helper<EnumType>
272
258
273
259
// FIND_LSB
274
260
275
- template<typename Integer>
261
+ template<typename Integer NBL_STRUCT_CONSTRAINABLE >
276
262
struct find_lsb_helper;
277
263
278
264
template<>
@@ -435,26 +421,31 @@ struct bitReverse_helper<Vector NBL_PARTIAL_REQ_BOT(concepts::Vectorial<Vector>)
435
421
}
436
422
};
437
423
438
- template<typename Matrix>
424
+ template<typename Matrix NBL_STRUCT_CONSTRAINABLE >
439
425
struct transpose_helper;
440
426
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>) >
443
430
{
444
- using transposed_t = typename matrix_traits<matrix <T, N, M> >::transposed_type;
431
+ using transposed_t = typename matrix_traits<Matrix >::transposed_type;
445
432
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)
447
434
{
448
435
#ifdef __HLSL_VERSION
449
436
return spirv::transpose (m);
450
437
#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)));
452
439
#endif
453
440
}
454
441
};
455
442
443
+ template<typename LhsT, typename RhsT NBL_STRUCT_CONSTRAINABLE>
444
+ struct mul_helper;
445
+
456
446
template<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>)) >
458
449
{
459
450
static inline mul_output_t<LhsT, RhsT> __call (LhsT lhs, RhsT rhs)
460
451
{
@@ -498,8 +489,8 @@ template<typename Integer NBL_STRUCT_CONSTRAINABLE>
498
489
struct bitCount_helper;
499
490
500
491
template<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>) >
503
494
{
504
495
static bitcount_output_t<Integer> __call (NBL_CONST_REF_ARG (Integer) val)
505
496
{
@@ -513,7 +504,6 @@ struct bitCount_helper<Integer NBL_PARTIAL_REQ_BOT(is_integral_v<Integer>) >
513
504
}
514
505
515
506
return spirv::bitCount (val);
516
-
517
507
#else
518
508
using UnsignedInteger = typename hlsl::unsigned_integer_of_size_t<sizeof (Integer)>;
519
509
constexpr int32_t BitCnt = sizeof (Integer) * 8u;
@@ -555,6 +545,7 @@ struct bitCount_helper<EnumT>
555
545
};
556
546
#endif
557
547
548
+ // TODO: length_helper partial specialization for emulated_float64_t
558
549
template<typename Vector NBL_STRUCT_CONSTRAINABLE>
559
550
struct length_helper;
560
551
@@ -567,24 +558,25 @@ struct length_helper<Vector NBL_PARTIAL_REQ_BOT(concepts::FloatingPointVector<Ve
567
558
#ifdef __HLSL_VERSION
568
559
return spirv::length (vec);
569
560
#else
570
- return std::sqrt (dot_helper <Vector>::__call (vec, vec));
561
+ return std::sqrt (length_helper <Vector>::__call (vec, vec));
571
562
#endif
572
563
}
573
564
};
574
565
575
566
template<typename Vector NBL_STRUCT_CONSTRAINABLE>
576
567
struct normalize_helper;
577
568
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 >) >
581
572
{
582
- static inline Vector __call (NBL_CONST_REF_ARG (Vector ) vec)
573
+ static inline Vectorial __call (NBL_CONST_REF_ARG (Vectorial ) vec)
583
574
{
584
575
#ifdef __HLSL_VERSION
585
- return spirv::normalize (vec);
576
+ if (is_vector_v<Vectorial>)
577
+ return spirv::normalize (vec);
586
578
#else
587
- return vec / length_helper<Vector >::__call (vec);
579
+ return vec / length_helper<Vectorial >::__call (vec);
588
580
#endif
589
581
}
590
582
};
@@ -744,8 +736,8 @@ template<typename T NBL_STRUCT_CONSTRAINABLE>
744
736
struct inverse_helper;
745
737
746
738
template<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) >
749
741
{
750
742
static SquareMatrix __call (NBL_CONST_REF_ARG (SquareMatrix) mat)
751
743
{
0 commit comments