Skip to content

Commit 36baa31

Browse files
committed
Renamed intrinsics.h to intrinsics.hlsl
1 parent 4b34124 commit 36baa31

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#ifndef _NBL_BUILTIN_HLSL_CPP_COMPAT_INTRINSICS_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_CPP_COMPAT_INTRINSICS_INCLUDED_
3+
4+
#include <nbl/builtin/hlsl/cpp_compat/matrix.hlsl>
5+
#include <nbl/builtin/hlsl/type_traits.hlsl>
6+
7+
#ifndef __HLSL_VERSION
8+
#include <algorithm>
9+
#include <cmath>
10+
#include "nbl/core/util/bitflag.h"
11+
#endif
12+
13+
namespace nbl
14+
{
15+
namespace hlsl
16+
{
17+
18+
template<typename Integer>
19+
int bitCount(Integer val)
20+
{
21+
#ifdef __HLSL_VERSION
22+
23+
#else
24+
return glm::bitCount(val);
25+
#endif
26+
}
27+
28+
template<typename T>
29+
vector<T, 3> cross(NBL_CONST_REF_ARG(vector<T, 3>) lhs, NBL_CONST_REF_ARG(vector<T, 3>) rhs)
30+
{
31+
#ifdef __HLSL_VERSION
32+
33+
#else
34+
return glm::cross(val);
35+
#endif
36+
}
37+
38+
template<typename T>
39+
int bitCount(T val)
40+
{
41+
#ifdef __HLSL_VERSION
42+
43+
#else
44+
return glm::clamp(val);
45+
#endif
46+
}
47+
48+
namespace dot_product_impl
49+
{
50+
template<typename T>
51+
struct dot_helper
52+
{
53+
using scalar_type = typename vector_traits<T>::scalar_type;
54+
55+
static inline scalar_type dot(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs)
56+
{
57+
static array_get<T, scalar_type> getter;
58+
scalar_type retval = getter(lhs, 0) * getter(rhs, 0);
59+
60+
static const uint32_t ArrayDim = sizeof(T) / sizeof(scalar_type);
61+
for (uint32_t i = 1; i < ArrayDim; ++i)
62+
retval = retval + getter(lhs, i) * getter(rhs, i);
63+
64+
return retval;
65+
}
66+
};
67+
68+
#define DEFINE_BUILTIN_VECTOR_SPECIALIZATION(FLOAT_TYPE, RETURN_VALUE)\
69+
template<uint32_t N>\
70+
struct dot_helper<vector<FLOAT_TYPE, N> >\
71+
{\
72+
using VectorType = vector<FLOAT_TYPE, N>;\
73+
using ScalarType = typename vector_traits<VectorType>::scalar_type;\
74+
\
75+
static inline ScalarType dot(NBL_CONST_REF_ARG(VectorType) lhs, NBL_CONST_REF_ARG(VectorType) rhs)\
76+
{\
77+
return RETURN_VALUE;\
78+
}\
79+
};\
80+
81+
#ifdef __HLSL_VERSION
82+
#define BUILTIN_VECTOR_SPECIALIZATION_RET_VAL dot(lhs, rhs)
83+
#else
84+
#define BUILTIN_VECTOR_SPECIALIZATION_RET_VAL glm::dot(lhs, rhs)
85+
#endif
86+
87+
DEFINE_BUILTIN_VECTOR_SPECIALIZATION(float16_t, BUILTIN_VECTOR_SPECIALIZATION_RET_VAL)
88+
DEFINE_BUILTIN_VECTOR_SPECIALIZATION(float32_t, BUILTIN_VECTOR_SPECIALIZATION_RET_VAL)
89+
DEFINE_BUILTIN_VECTOR_SPECIALIZATION(float64_t, BUILTIN_VECTOR_SPECIALIZATION_RET_VAL)
90+
91+
#undef BUILTIN_VECTOR_SPECIALIZATION_RET_VAL
92+
#undef DEFINE_BUILTIN_VECTOR_SPECIALIZATION
93+
94+
}
95+
96+
template<typename T>
97+
typename vector_traits<T>::scalar_type dot(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs)
98+
{
99+
return dot_product_impl::dot_helper<T>::dot(lhs, rhs);
100+
}
101+
102+
// determinant not defined cause its implemented via hidden friend
103+
// https://stackoverflow.com/questions/67459950/why-is-a-friend-function-not-treated-as-a-member-of-a-namespace-of-a-class-it-wa
104+
template<typename T, uint16_t N, uint16_t M>
105+
inline T determinant(const matrix<T, N, M>& m)
106+
{
107+
#ifdef __HLSL_VERSION
108+
109+
#else
110+
return glm::determinant(reinterpret_cast<typename matrix<T, N, M>::Base const&>(m));
111+
#endif
112+
}
113+
114+
}
115+
}
116+
117+
#endif

0 commit comments

Comments
 (0)