Skip to content

Commit fc416d4

Browse files
author
devsh
committed
GLM lerp and dot can't be just plain forwarded to because they refuse to work on non-floats
Also the passthrough macros don't work because they mess with default values, template paramter explicit provision, etc. should be removed
1 parent a21bdc0 commit fc416d4

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

include/nbl/builtin/hlsl/cpp_compat/intrinsics.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace 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>\
1718
inline auto HLSL_ID(Args&&... args) \
1819
{ \
@@ -44,7 +45,14 @@ NBL_SIMPLE_GLM_PASSTHROUGH(cross,cross)
4445
NBL_SIMPLE_GLM_PASSTHROUGH(clamp,clamp)
4546

4647
template<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
7197
template<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) \
81108
template<typename... Args> \
82109
inline 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

87114
NBL_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+
89122
NBL_ALIAS_TEMPLATE_FUNCTION(std::isnan, isnan);
90123
NBL_ALIAS_TEMPLATE_FUNCTION(std::isinf, isinf);
91124
NBL_ALIAS_TEMPLATE_FUNCTION(std::exp2, exp2);

0 commit comments

Comments
 (0)