1313
1414namespace nbl ::hlsl
1515{
16+ // TODO: remove this macro and write stuff by hand, the aliasing stuff doesn't work
1617#define NBL_SIMPLE_GLM_PASSTHROUGH (HLSL_ID,GLSL_ID,...) template <typename ... Args>\
1718inline auto HLSL_ID (Args&&... args) \
1819{ \
@@ -44,7 +45,14 @@ NBL_SIMPLE_GLM_PASSTHROUGH(cross,cross)
4445NBL_SIMPLE_GLM_PASSTHROUGH (clamp,clamp)
4546
4647template <typename T>
47- inline typename scalar_type<T>::type dot (const T& lhs, const T& rhs) {return glm::dot (lhs,rhs);}
48+ inline scalar_type_t <T> dot (const T& lhs, const T& rhs)
49+ {
50+ scalar_type_t <T> retval = lhs[0 ]*rhs[0 ];
51+ // whatever has a `scalar_type` specialization should be a pure vector
52+ for (auto i=1 ; i<sizeof (T)/sizeof (retval); i++)
53+ retval += lhs[i]*rhs[i];
54+ return retval;
55+ }
4856
4957// determinant not defined cause its implemented via hidden friend
5058// https://stackoverflow.com/questions/67459950/why-is-a-friend-function-not-treated-as-a-member-of-a-namespace-of-a-class-it-wa
@@ -65,7 +73,25 @@ inline matrix<T,N,M> inverse(const matrix<T,N,M>& m)
6573 return reinterpret_cast <matrix<T,N,M>&>(glm::inverse (reinterpret_cast <typename matrix<T,N,M>::Base const &>(m)));
6674}
6775
68- NBL_SIMPLE_GLM_PASSTHROUGH (lerp,mix)
76+ template <typename T, typename U>
77+ inline T lerp (const T& x, const T& y, const U& a)
78+ {
79+ if constexpr (std::is_same_v<U,bool >)
80+ return a ? y:x;
81+ else
82+ {
83+ if constexpr (std::is_same_v<scalar_type_t <U>,bool >)
84+ {
85+ T retval;
86+ // whatever has a `scalar_type` specialization should be a pure vector
87+ for (auto i=0 ; i<sizeof (T)/sizeof (retval); i++)
88+ retval[i] = a[i] ? y[i] : x[i];
89+ return retval;
90+ }
91+ else
92+ return glm::mix<T,U>(x,y,a);
93+ }
94+ }
6995
7096// transpose not defined cause its implemented via hidden friend
7197template <typename T, uint16_t N, uint16_t M>
@@ -77,6 +103,7 @@ inline matrix<T,M,N> transpose(const matrix<T,N,M>& m)
77103#undef NBL_BIT_OP_GLM_PASSTHROUGH
78104#undef NBL_SIMPLE_GLM_PASSTHROUGH
79105
106+ // TODO: remove this macro and write stuff by hand, the aliasing stuff doesn't work
80107#define NBL_ALIAS_TEMPLATE_FUNCTION (origFunctionName, functionAlias ) \
81108template <typename ... Args> \
82109inline auto functionAlias (Args&&... args) -> decltype(origFunctionName(std::forward<Args>(args)...)) \
@@ -85,7 +112,13 @@ inline auto functionAlias(Args&&... args) -> decltype(origFunctionName(std::forw
85112}
86113
87114NBL_ALIAS_TEMPLATE_FUNCTION (std::min, min);
88- NBL_ALIAS_TEMPLATE_FUNCTION (std::max, max);
115+
116+ template <typename T>
117+ inline T max (const T& a, const T& b)
118+ {
119+ return lerp<T>(a,b,b>a);
120+ }
121+
89122NBL_ALIAS_TEMPLATE_FUNCTION (std::isnan, isnan);
90123NBL_ALIAS_TEMPLATE_FUNCTION (std::isinf, isinf);
91124NBL_ALIAS_TEMPLATE_FUNCTION (std::exp2, exp2);
0 commit comments