Skip to content

Commit 3b9658b

Browse files
committed
Corrections
1 parent 2a7953e commit 3b9658b

File tree

2 files changed

+69
-32
lines changed

2 files changed

+69
-32
lines changed

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

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ namespace hlsl
2020
{
2121

2222
template<typename Integer>
23-
int bitCount(NBL_CONST_REF_ARG(Integer) val)
23+
inline int bitCount(NBL_CONST_REF_ARG(Integer) val)
2424
{
2525
#ifdef __HLSL_VERSION
26+
if (sizeof(Integer) == 8u)
27+
{
28+
uint32_t lowBits = val;
29+
uint32_t highBits = val >> 32u;
30+
31+
return countbits(lowBits) + countbits(highBits);
32+
}
33+
2634
return countbits(val);
35+
2736
#else
2837
return glm::bitCount(val);
2938
#endif
@@ -49,7 +58,7 @@ T clamp(NBL_CONST_REF_ARG(T) val, NBL_CONST_REF_ARG(T) min, NBL_CONST_REF_ARG(T)
4958
#endif
5059
}
5160

52-
namespace dot_product_impl
61+
namespace cpp_compat_intrinsics_impl
5362
{
5463
template<typename T>
5564
struct dot_helper
@@ -100,18 +109,19 @@ DEFINE_BUILTIN_VECTOR_SPECIALIZATION(float64_t, BUILTIN_VECTOR_SPECIALIZATION_RE
100109
template<typename T>
101110
typename vector_traits<T>::scalar_type dot(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs)
102111
{
103-
return dot_product_impl::dot_helper<T>::dot(lhs, rhs);
112+
return cpp_compat_intrinsics_impl::dot_helper<T>::dot(lhs, rhs);
104113
}
105114

115+
// TODO: for clearer error messages, use concepts to ensure that input type is a square matrix
106116
// determinant not defined cause its implemented via hidden friend
107117
// https://stackoverflow.com/questions/67459950/why-is-a-friend-function-not-treated-as-a-member-of-a-namespace-of-a-class-it-wa
108-
template<typename T, uint16_t N, uint16_t M>
109-
inline T determinant(NBL_CONST_REF_ARG(matrix<T, N, M>) m)
118+
template<typename T, uint16_t N>
119+
inline T determinant(NBL_CONST_REF_ARG(matrix<T, N, N>) m)
110120
{
111121
#ifdef __HLSL_VERSION
112-
122+
spirv::determinant(m);
113123
#else
114-
return glm::determinant(reinterpret_cast<typename matrix<T, N, M>::Base const&>(m));
124+
return glm::determinant(reinterpret_cast<typename matrix<T, N, N>::Base const&>(m));
115125
#endif
116126
}
117127

@@ -169,7 +179,7 @@ int findMSB(NBL_CONST_REF_ARG(Integer) val)
169179
}
170180

171181
// TODO: some of the functions in this header should move to `tgmath`
172-
template<typename T> //requires ::nbl::hlsl::is_floating_point_v<T>
182+
template<typename T>
173183
inline T floor(NBL_CONST_REF_ARG(T) val)
174184
{
175185
#ifdef __HLSL_VERSION
@@ -191,28 +201,52 @@ inline matrix<T, N, M> inverse(NBL_CONST_REF_ARG(matrix<T, N, M>) m)
191201
#endif
192202
}
193203

204+
namespace cpp_compat_intrinsics_impl
205+
{
206+
207+
// TODO: concept requiring T to be a float
194208
template<typename T, typename U>
195-
inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(U) a)
209+
struct lerp_helper
196210
{
211+
static inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(U) a)
212+
{
197213
#ifdef __HLSL_VERSION
198-
return spirv::fMix(x, y, a);
214+
return spirv::fMix(x, y, a);
199215
#else
200-
if constexpr (std::is_same_v<U, bool>)
216+
return glm::mix<T, U>(x, y, a);
217+
#endif
218+
}
219+
};
220+
221+
template<typename T>
222+
struct lerp_helper<T, bool>
223+
{
224+
static inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(bool) a)
225+
{
201226
return a ? y : x;
202-
else
227+
}
228+
};
229+
230+
template<typename T, int N>
231+
struct lerp_helper<vector<T, N>, vector<bool, N> >
232+
{
233+
using output_vec_t = vector<T, N>;
234+
235+
static inline output_vec_t lerp(NBL_CONST_REF_ARG(output_vec_t) x, NBL_CONST_REF_ARG(output_vec_t) y, NBL_CONST_REF_ARG(vector<bool, N>) a)
203236
{
204-
if constexpr (std::is_same_v<scalar_type_t<U>, bool>)
205-
{
206-
T retval;
207-
// whatever has a `scalar_type` specialization should be a pure vector
208-
for (auto i = 0; i < sizeof(a) / sizeof(scalar_type_t<U>); i++)
209-
retval[i] = a[i] ? y[i] : x[i];
210-
return retval;
211-
}
212-
else
213-
return glm::mix<T, U>(x, y, a);
237+
output_vec_t retval;
238+
for (uint32_t i = 0; i < vector_traits<output_vec_t>::Dimension; i++)
239+
retval[i] = a[i] ? y[i] : x[i];
240+
return retval;
214241
}
215-
#endif
242+
};
243+
244+
}
245+
246+
template<typename T, typename U>
247+
inline T lerp(NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) y, NBL_CONST_REF_ARG(U) a)
248+
{
249+
return cpp_compat_intrinsics_impl::lerp_helper<T, U>::lerp(x, y, a);
216250
}
217251

218252
// transpose not defined cause its implemented via hidden friend
@@ -232,14 +266,18 @@ inline T min(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
232266
#ifdef __HLSL_VERSION
233267
min(a, b);
234268
#else
235-
return std::min(a, b);
269+
return glm::min(a, b);
236270
#endif
237271
}
238272

239273
template<typename T>
240274
inline T max(NBL_CONST_REF_ARG(T) a, NBL_CONST_REF_ARG(T) b)
241275
{
242-
return lerp<T>(a, b, b > a);
276+
#ifdef __HLSL_VERSION
277+
max(a, b);
278+
#else
279+
return glm::max(a, b);
280+
#endif
243281
}
244282

245283
template<typename FloatingPoint>
@@ -289,6 +327,7 @@ DEFINE_EXP2_SPECIALIZATION(uint64_t)
289327
template<typename FloatingPoint>
290328
inline FloatingPoint rsqrt(FloatingPoint x)
291329
{
330+
// TODO: https://stackoverflow.com/a/62239778
292331
#ifdef __HLSL_VERSION
293332
return spirv::inverseSqrt(x);
294333
#else

include/nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ template<typename Integral>
1818
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindILsb, "GLSL.std.450")]]
1919
enable_if_t<is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4), Integral> findILsb(Integral value);
2020

21-
template<typename Integral>
2221
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindSMsb, "GLSL.std.450")]]
23-
enable_if_t<is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4), Integral> findSMsb(Integral value);
22+
int32_t findSMsb(int32_t value);
2423

25-
template<typename Integral>
2624
[[vk::ext_instruction(GLSLstd450::GLSLstd450FindUMsb, "GLSL.std.450")]]
27-
enable_if_t<is_integral_v<Integral> && (sizeof(scalar_type_t<Integral>) == 4), Integral> findUMsb(Integral value);
25+
uint32_t findUMsb(uint32_t value);
2826

2927
template<typename FloatingPoint>
3028
[[vk::ext_instruction(GLSLstd450::GLSLstd450Exp2, "GLSL.std.450")]]
@@ -44,11 +42,11 @@ enable_if_t<is_floating_point_v<FloatingPoint>, vector<FloatingPoint, 3> > cross
4442

4543
template<typename FloatingPoint>
4644
[[vk::ext_instruction(GLSLstd450::GLSLstd450FMix, "GLSL.std.450")]]
47-
enable_if_t<is_floating_point_v<FloatingPoint>, FloatingPoint> fMix(FloatingPoint val);
45+
enable_if_t<is_floating_point_v<FloatingPoint>, FloatingPoint> fMix(FloatingPoint val, FloatingPoint min, FloatingPoint max);
4846

49-
template<typename SquareMatrix>
47+
template<typename T, int N>
5048
[[vk::ext_instruction(GLSLstd450::GLSLstd450Determinant, "GLSL.std.450")]]
51-
SquareMatrix determinant(in SquareMatrix mat);
49+
T determinant(in matrix<T, N, N> mat);
5250

5351
}
5452
}

0 commit comments

Comments
 (0)