Skip to content

Commit 4c0536f

Browse files
committed
Integrated concepts into intrinsics.hlsl functions and fixed some emulated_float64_t stuff
1 parent 2fb8252 commit 4c0536f

File tree

11 files changed

+262
-140
lines changed

11 files changed

+262
-140
lines changed

include/nbl/builtin/hlsl/concepts/core.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ template<typename T>
3232
NBL_BOOL_CONCEPT FloatingPoint = nbl::hlsl::is_floating_point_v<T>;
3333

3434
template <typename T>
35-
NBL_BOOL_CONCEPT scalar = FloatingPoint<T> || Integral<T>;
35+
NBL_BOOL_CONCEPT Scalar = nbl::hlsl::is_scalar_v<T>;
3636

3737
template<typename T>
3838
NBL_BOOL_CONCEPT IntegralScalar = nbl::hlsl::is_integral_v<T> && nbl::hlsl::is_scalar_v<T>;

include/nbl/builtin/hlsl/concepts/matrix.hlsl

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,8 @@ namespace concepts
1818
template<typename T>
1919
NBL_BOOL_CONCEPT Matrix = is_matrix<T>::value;
2020

21-
#define NBL_CONCEPT_NAME Matricial
22-
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)
23-
#define NBL_CONCEPT_TPLT_PRM_NAMES (T)
24-
NBL_CONCEPT_BEGIN(0)
25-
NBL_CONCEPT_END(
26-
((NBL_CONCEPT_REQ_TYPE)(matrix_traits<T>::scalar_type))
27-
((NBL_CONCEPT_REQ_TYPE)(matrix_traits<T>::row_type))
28-
((NBL_CONCEPT_REQ_TYPE)(matrix_traits<T>::transposed_type))
29-
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((matrix_traits<T>::RowCount), ::nbl::hlsl::is_integral_v))
30-
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((matrix_traits<T>::ColumnCount), ::nbl::hlsl::is_integral_v))
31-
// TODO: fix
32-
//((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((matrix_traits<T>::Square), ::nbl::hlsl::is_same_v, bool))
33-
);
34-
#include <nbl/builtin/hlsl/concepts/__end.hlsl>
35-
36-
#undef NBL_CONCEPT_NAME
37-
#undef NBL_CONCEPT_TPLT_PRM_KINDS
38-
#undef NBL_CONCEPT_TPLT_PRM_NAMES
21+
template<typename T>
22+
NBL_BOOL_CONCEPT Matricial = matrix_traits<T>::IsMatrix;
3923

4024
}
4125
}

include/nbl/builtin/hlsl/concepts/vector.hlsl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ template<typename T>
2828
NBL_BOOL_CONCEPT SignedIntVector = concepts::Vector<T> && concepts::SignedIntegralScalar<typename vector_traits<T>::scalar_type>;
2929

3030
//! Concept for native vectors and vector like structs.
31-
//! The only requirement for a structure to be Vectorial is that a correct template specialization of the `vector_traits` structure should be created for it.
32-
//#define NBL_CONCEPT_NAME Vectorial
33-
//#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)
34-
//#define NBL_CONCEPT_TPLT_PRM_NAMES (T)
35-
//
36-
//NBL_CONCEPT_BEGIN(0)
37-
//NBL_CONCEPT_END
38-
//(
39-
// ((NBL_CONCEPT_REQ_TYPE)(vector_traits<T>::scalar_type))
40-
// ((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((vector_traits<T>::Dimension), ::nbl::hlsl::is_integral_v))
41-
// // TODO: fix
42-
// //((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((vector_traits<T>::IsVector), ::nbl::hlsl::is_same_v, bool))
43-
//) && vector_traits<T>::isVector;
44-
4531
template<typename T>
4632
NBL_BOOL_CONCEPT Vectorial = vector_traits<T>::IsVector;
4733

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

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
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
{
2223
namespace 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

7052
template<typename T NBL_STRUCT_CONSTRAINABLE>
7153
struct 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>
161147
struct find_msb_helper;
162148

163149
template<>
@@ -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>
276262
struct find_lsb_helper;
277263

278264
template<>
@@ -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>
439425
struct 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+
456446
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>)) >
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>
498489
struct bitCount_helper;
499490

500491
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>) >
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
558549
template<typename Vector NBL_STRUCT_CONSTRAINABLE>
559550
struct 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

575566
template<typename Vector NBL_STRUCT_CONSTRAINABLE>
576567
struct 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>
744736
struct inverse_helper;
745737

746738
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) >
749741
{
750742
static SquareMatrix __call(NBL_CONST_REF_ARG(SquareMatrix) mat)
751743
{

0 commit comments

Comments
 (0)