13
13
14
14
namespace nbl ::hlsl
15
15
{
16
+ // TODO: remove this macro and write stuff by hand, the aliasing stuff doesn't work
16
17
#define NBL_SIMPLE_GLM_PASSTHROUGH (HLSL_ID,GLSL_ID,...) template <typename ... Args>\
17
18
inline auto HLSL_ID (Args&&... args) \
18
19
{ \
@@ -44,7 +45,14 @@ NBL_SIMPLE_GLM_PASSTHROUGH(cross,cross)
44
45
NBL_SIMPLE_GLM_PASSTHROUGH (clamp,clamp)
45
46
46
47
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
+ }
48
56
49
57
// determinant not defined cause its implemented via hidden friend
50
58
// 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)
65
73
return reinterpret_cast <matrix<T,N,M>&>(glm::inverse (reinterpret_cast <typename matrix<T,N,M>::Base const &>(m)));
66
74
}
67
75
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
+ }
69
95
70
96
// transpose not defined cause its implemented via hidden friend
71
97
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)
77
103
#undef NBL_BIT_OP_GLM_PASSTHROUGH
78
104
#undef NBL_SIMPLE_GLM_PASSTHROUGH
79
105
106
+ // TODO: remove this macro and write stuff by hand, the aliasing stuff doesn't work
80
107
#define NBL_ALIAS_TEMPLATE_FUNCTION (origFunctionName, functionAlias ) \
81
108
template <typename ... Args> \
82
109
inline auto functionAlias (Args&&... args) -> decltype(origFunctionName(std::forward<Args>(args)...)) \
@@ -85,7 +112,13 @@ inline auto functionAlias(Args&&... args) -> decltype(origFunctionName(std::forw
85
112
}
86
113
87
114
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
+
89
122
NBL_ALIAS_TEMPLATE_FUNCTION (std::isnan, isnan);
90
123
NBL_ALIAS_TEMPLATE_FUNCTION (std::isinf, isinf);
91
124
NBL_ALIAS_TEMPLATE_FUNCTION (std::exp2, exp2);
0 commit comments