Skip to content

Commit c95d042

Browse files
committed
Fixes
1 parent f176060 commit c95d042

File tree

13 files changed

+225
-218
lines changed

13 files changed

+225
-218
lines changed

include/nbl/builtin/hlsl/concepts/core.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ NBL_BOOL_CONCEPT FloatingPointScalar = nbl::hlsl::is_floating_point_v<T> && nbl:
6767
namespace impl
6868
{
6969
template<typename T>
70-
struct IsEmulatingFloatingPointType
70+
struct is_emulating_floating_point_scalar
7171
{
72-
static const bool value = nbl::hlsl::is_floating_point_v<T>;
72+
NBL_CONSTEXPR_STATIC_INLINE bool value = FloatingPointScalar<T>;
7373
};
7474
}
7575

7676
//! Floating point types are native floating point types or types that imitate native floating point types (for example emulated_float64_t)
7777
template<typename T>
78-
NBL_BOOL_CONCEPT FloatingPointLikeScalar = impl::IsEmulatingFloatingPointType<T>::value;
78+
NBL_BOOL_CONCEPT FloatingPointLikeScalar = impl::is_emulating_floating_point_scalar<T>::value;
7979

8080
}
8181
}

include/nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl

Lines changed: 122 additions & 119 deletions
Large diffs are not rendered by default.

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ namespace nbl
2323
namespace hlsl
2424
{
2525

26-
#ifdef __HLSL_VERSION
2726
template<typename T>
28-
#else
29-
template<typename T>
30-
#endif
3127
inline typename cpp_compat_intrinsics_impl::bitCount_helper<T>::return_t bitCount(NBL_CONST_REF_ARG(T) val)
3228
{
3329
return cpp_compat_intrinsics_impl::bitCount_helper<T>::__call(val);
@@ -39,18 +35,12 @@ T cross(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs)
3935
return cpp_compat_intrinsics_impl::cross_helper<T>::__call(lhs, rhs);
4036
}
4137

42-
template<typename T, typename U>
43-
typename cpp_compat_intrinsics_impl::clamp_helper<T, U>::return_t clamp(NBL_CONST_REF_ARG(T) val, NBL_CONST_REF_ARG(U) min, NBL_CONST_REF_ARG(U) max)
38+
template<typename T>
39+
typename cpp_compat_intrinsics_impl::clamp_helper<T>::return_t clamp(NBL_CONST_REF_ARG(T) val, NBL_CONST_REF_ARG(T) _min, NBL_CONST_REF_ARG(T) _max)
4440
{
45-
return cpp_compat_intrinsics_impl::clamp_helper<T, U>::__call(val, min, max);
41+
return cpp_compat_intrinsics_impl::clamp_helper<T>::__call(val, _min, _max);
4642
}
4743

48-
//template<typename Vectorial>
49-
//Vectorial clamp(NBL_CONST_REF_ARG(Vectorial) val, NBL_CONST_REF_ARG(typename vector_traits<Vectorial>::scalar_type) min, NBL_CONST_REF_ARG(typename vector_traits<Vectorial>::scalar_type) max)
50-
//{
51-
// return cpp_compat_intrinsics_impl::clamp_helper<Vectorial>::__call(val, min, max);
52-
//}
53-
5444
template<typename FloatingPointVectorial>
5545
typename vector_traits<FloatingPointVectorial>::scalar_type length(NBL_CONST_REF_ARG(FloatingPointVectorial) vec)
5646
{
@@ -71,7 +61,7 @@ typename vector_traits<Vectorial>::scalar_type dot(NBL_CONST_REF_ARG(Vectorial)
7161

7262
// determinant not defined cause its implemented via hidden friend
7363
// https://stackoverflow.com/questions/67459950/why-is-a-friend-function-not-treated-as-a-member-of-a-namespace-of-a-class-it-wa
74-
template<typename Matrix>
64+
template<typename Matrix NBL_FUNC_REQUIRES(concepts::Matricial<Matrix>)
7565
inline typename matrix_traits<Matrix>::scalar_type determinant(NBL_CONST_REF_ARG(Matrix) mat)
7666
{
7767
return cpp_compat_intrinsics_impl::determinant_helper<Matrix>::__call(mat);
@@ -90,21 +80,21 @@ inline typename cpp_compat_intrinsics_impl::find_msb_helper<T>::return_t findMSB
9080
}
9181

9282
// inverse not defined cause its implemented via hidden friend
93-
template<typename Matrix>
83+
template<typename Matrix NBL_FUNC_REQUIRES(concepts::Matricial<Matrix>)
9484
inline Matrix inverse(NBL_CONST_REF_ARG(Matrix) mat)
9585
{
9686
return cpp_compat_intrinsics_impl::inverse_helper<Matrix>::__call(mat);
9787
}
9888

9989
// transpose not defined cause its implemented via hidden friend
100-
template<typename Matrix>
90+
template<typename Matrix NBL_FUNC_REQUIRES(concepts::Matricial<Matrix>)
10191
inline typename matrix_traits<Matrix>::transposed_type transpose(NBL_CONST_REF_ARG(Matrix) m)
10292
{
10393
return cpp_compat_intrinsics_impl::transpose_helper<Matrix>::__call(m);
10494
}
10595

10696
template<typename LhsT, typename RhsT>
107-
mul_output_t<LhsT, RhsT> mul(LhsT lhs, RhsT rhs)
97+
inline typename cpp_compat_intrinsics_impl::mul_helper<LhsT, RhsT>::return_t mul(LhsT lhs, RhsT rhs)
10898
{
10999
return cpp_compat_intrinsics_impl::mul_helper<LhsT, RhsT>::__call(lhs, rhs);
110100
}

include/nbl/builtin/hlsl/emulated/float64_t.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,9 @@ namespace concepts
594594
namespace impl
595595
{
596596
template<bool FastMath, bool FlushDenormToZero>
597-
struct IsEmulatingFloatingPointType<emulated_float64_t<FastMath, FlushDenormToZero> >
597+
struct is_emulating_floating_point_scalar<emulated_float64_t<FastMath, FlushDenormToZero> >
598598
{
599-
static const bool value = true;
599+
NBL_CONSTEXPR_STATIC_INLINE value = true;
600600
};
601601
}
602602
}

include/nbl/builtin/hlsl/ieee754.hlsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ NBL_CONSTEXPR_INLINE_FUNC FloatingPoint flipSign(FloatingPoint val)
152152
return bit_cast<FloatingPoint>(asUint ^ ieee754::traits<AsFloat>::signMask);
153153
}
154154

155+
template <typename FloatingPoint NBL_FUNC_REQUIRES(concepts::FloatingPointLikeScalar<FloatingPoint>)
156+
NBL_CONSTEXPR_INLINE_FUNC FloatingPoint flipSign(FloatingPoint val, bool flip)
157+
{
158+
return flip ? flipSign(val) : val;
159+
}
160+
155161
}
156162
}
157163
}

include/nbl/builtin/hlsl/impl/tgmath_impl.hlsl

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct exp2_helper;
5454
template<typename T NBL_STRUCT_CONSTRAINABLE>
5555
struct log_helper;
5656
template<typename T NBL_STRUCT_CONSTRAINABLE>
57+
struct log2_helper;
58+
template<typename T NBL_STRUCT_CONSTRAINABLE>
5759
struct abs_helper;
5860
template<typename T NBL_STRUCT_CONSTRAINABLE>
5961
struct cos_helper;
@@ -64,7 +66,7 @@ struct acos_helper;
6466
template<typename T NBL_STRUCT_CONSTRAINABLE>
6567
struct sqrt_helper;
6668
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
67-
struct lerp_helper;
69+
struct mix_helper;
6870
template<typename T NBL_STRUCT_CONSTRAINABLE>
6971
struct modf_helper;
7072

@@ -82,12 +84,23 @@ struct HELPER_NAME<T NBL_PARTIAL_REQ_BOT(always_true<decltype(spirv::SPIRV_FUNCT
8284
};
8385

8486
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(sin_helper, sin, T)
85-
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(cos_helper, cos, T)
87+
//AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(cos_helper, cos, T)
88+
89+
template<typename T> NBL_PARTIAL_REQ_TOP(is_same_v<decltype(spirv::cos<T>(experimental::declval<T>())), T>)
90+
struct cos_helper<T NBL_PARTIAL_REQ_BOT(is_same_v<decltype(spirv::cos<T>(experimental::declval<T>())), T>) >
91+
{
92+
static T __call(T arg)
93+
{
94+
return spirv::cos<T>(arg);
95+
}
96+
};
97+
8698
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(acos_helper, acos, T)
8799
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(abs_helper, sAbs, T)
88100
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(abs_helper, fAbs, T)
89101
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(sqrt_helper, sqrt, T)
90102
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(log_helper, log, T)
103+
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(log2_helper, log2, T)
91104
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(exp2_helper, exp2, T)
92105
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(exp_helper, exp, T)
93106
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(floor_helper, floor, T)
@@ -108,7 +121,7 @@ struct pow_helper<T NBL_PARTIAL_REQ_BOT(always_true<decltype(spirv::pow<T>(exper
108121
};
109122

110123
template<typename T, typename U> NBL_PARTIAL_REQ_TOP(always_true<decltype(spirv::fMix<T>(experimental::declval<T>(), experimental::declval<T>(), experimental::declval<U>()))>)
111-
struct lerp_helper<T, U NBL_PARTIAL_REQ_BOT(always_true<decltype(spirv::fMix<T>(experimental::declval<T>(), experimental::declval<T>(), experimental::declval<U>()))>) >
124+
struct mix_helper<T, U NBL_PARTIAL_REQ_BOT(always_true<decltype(spirv::fMix<T>(experimental::declval<T>(), experimental::declval<T>(), experimental::declval<U>()))>) >
112125
{
113126
using return_t = conditional_t<is_vector_v<T>, vector<typename vector_traits<T>::scalar_type, vector_traits<T>::Dimension>, T>;
114127
static inline return_t __call(const T x, const T y, const U a)
@@ -150,8 +163,33 @@ struct modf_helper<T NBL_PARTIAL_REQ_BOT(concepts::FloatingPoint<T> && is_vector
150163
}
151164
};
152165

166+
template<typename FloatingPoint>
167+
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointScalar<FloatingPoint>)
168+
struct erf_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<FloatingPoint>) >
169+
{
170+
static FloatingPoint __call(NBL_CONST_REF_ARG(FloatingPoint) _x)
171+
{
172+
const FloatingPoint a1 = 0.254829592;
173+
const FloatingPoint a2 = -0.284496736;
174+
const FloatingPoint a3 = 1.421413741;
175+
const FloatingPoint a4 = -1.453152027;
176+
const FloatingPoint a5 = 1.061405429;
177+
const FloatingPoint p = 0.3275911;
178+
179+
FloatingPoint sign = sign(_x);
180+
FloatingPoint x = abs(_x);
181+
182+
FloatingPoint t = 1.0 / (1.0 + p * x);
183+
FloatingPoint y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * exp(-x * x);
184+
185+
return sign * y;
186+
}
187+
};
188+
153189
#else // C++ only specializations
154190

191+
192+
// not giving an explicit template parameter to std function below because not every function used here is templated
155193
#define AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(HELPER_NAME, REQUIREMENT, STD_FUNCTION_NAME, RETURN_TYPE)\
156194
template<typename T>\
157195
requires REQUIREMENT \
@@ -170,6 +208,7 @@ AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(acos_helper, concepts::FloatingPointScalar<T
170208
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(sqrt_helper, concepts::FloatingPointScalar<T>, sqrt, T)
171209
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(abs_helper, concepts::Scalar<T>, abs, T)
172210
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(log_helper, concepts::Scalar<T>, log, T)
211+
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(log2_helper, concepts::FloatingPointScalar<T>, log2, T)
173212
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(exp2_helper, concepts::Scalar<T>, exp2, T)
174213
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(exp_helper, concepts::Scalar<T>, exp, T)
175214
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(floor_helper, concepts::FloatingPointScalar<T>, floor, T)
@@ -182,7 +221,7 @@ struct pow_helper<T>
182221
using return_t = T;
183222
static inline return_t __call(const T x, const T y)
184223
{
185-
return std::pow(x, y);
224+
return std::pow<T>(x, y);
186225
}
187226
};
188227

@@ -228,7 +267,7 @@ struct isnan_helper<T>
228267

229268
template<typename T, typename U>
230269
requires concepts::FloatingPoint<T> && (concepts::FloatingPoint<T> || concepts::Boolean<T>)
231-
struct lerp_helper<T, U>
270+
struct mix_helper<T, U>
232271
{
233272
using return_t = T;
234273
static inline return_t __call(const T x, const T y, const U a)
@@ -237,48 +276,29 @@ struct lerp_helper<T, U>
237276
}
238277
};
239278

240-
#endif
241-
242-
// C++ and HLSL specializations
243-
244279
template<typename FloatingPoint>
245280
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointScalar<FloatingPoint>)
246281
struct erf_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<FloatingPoint>) >
247282
{
248-
static FloatingPoint __call(NBL_CONST_REF_ARG(FloatingPoint) _x)
283+
static FloatingPoint __call(NBL_CONST_REF_ARG(FloatingPoint) x)
249284
{
250-
#ifdef __HLSL_VERSION
251-
const FloatingPoint a1 = 0.254829592;
252-
const FloatingPoint a2 = -0.284496736;
253-
const FloatingPoint a3 = 1.421413741;
254-
const FloatingPoint a4 = -1.453152027;
255-
const FloatingPoint a5 = 1.061405429;
256-
const FloatingPoint p = 0.3275911;
285+
return std::erf<FloatingPoint>(x);
286+
}
287+
};
257288

258-
FloatingPoint sign = sign(_x);
259-
FloatingPoint x = abs(_x);
289+
#endif // C++ only specializations
260290

261-
FloatingPoint t = 1.0 / (1.0 + p * x);
262-
FloatingPoint y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * exp(-x * x);
291+
// C++ and HLSL specializations
263292

264-
return sign * y;
265-
#else
266-
return std::erf(_x);
267-
#endif
268-
}
269-
};
270293
template<typename FloatingPoint>
271294
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointScalar<FloatingPoint>)
272295
struct erfInv_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<FloatingPoint>) >
273296
{
274297
static FloatingPoint __call(NBL_CONST_REF_ARG(FloatingPoint) _x)
275298
{
276299
FloatingPoint x = clamp<FloatingPoint>(_x, -0.99999, 0.99999);
277-
#ifdef __HLSL_VERSION
278-
FloatingPoint w = -log((1.0 - x) * (1.0 + x));
279-
#else
280-
FloatingPoint w = -std::log((1.0 - x) * (1.0 + x));
281-
#endif
300+
301+
FloatingPoint w = -log_helper<FloatingPoint>::__call((1.0 - x) * (1.0 + x));
282302
FloatingPoint p;
283303
if (w < 5.0)
284304
{
@@ -295,11 +315,7 @@ struct erfInv_helper<FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointSc
295315
}
296316
else
297317
{
298-
#ifdef __HLSL_VERSION
299-
w = sqrt(w) - 3.0;
300-
#else
301-
w = std::sqrt(w) - 3.0;
302-
#endif
318+
w = sqrt_helper<FloatingPoint>::__call(w) - 3.0;
303319
p = -0.000200214257;
304320
p = 0.000100950558 + p * w;
305321
p = 0.00134934322 + p * w;
@@ -345,6 +361,7 @@ struct HELPER_NAME<T NBL_PARTIAL_REQ_BOT(VECTOR_SPECIALIZATION_CONCEPT) >\
345361
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(sqrt_helper, T)
346362
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(abs_helper, T)
347363
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(log_helper, T)
364+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(log2_helper, T)
348365
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(exp2_helper, T)
349366
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(exp_helper, T)
350367
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(floor_helper, T)

include/nbl/builtin/hlsl/matrix_utils/mul_output_t.hlsl

Lines changed: 0 additions & 33 deletions
This file was deleted.

include/nbl/builtin/hlsl/shapes/beziers.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ struct Quadratic
412412
// p'(1/2) = 2(C-A)
413413

414414
// exponent so large it would wipe the mantissa on any relative operation
415+
// should be exp2<float_t>(numeric_limits<float_t>::digits) ater tgmath has an exp2
415416
const float_t PARAMETER_THRESHOLD = exp2(24.0f);
416417
Candidates candidates;
417418

include/nbl/builtin/hlsl/spirv_intrinsics/core.hlsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ template<typename Integral>
258258
[[vk::ext_instruction(spv::OpBitCount)]]
259259
enable_if_t<is_integral_v<Integral>, Integral> bitCount(Integral mat);
260260

261+
template<typename BooleanVector>
262+
[[vk::ext_instruction(spv::OpAll)]]
263+
enable_if_t<is_vector_v<BooleanVector> && is_same_v<typename vector_traits<BooleanVector>::scalar_type, bool>, BooleanVector> all(BooleanVector vec);
264+
265+
template<typename BooleanVector>
266+
[[vk::ext_instruction(spv::OpAny)]]
267+
enable_if_t<is_vector_v<BooleanVector>&& is_same_v<typename vector_traits<BooleanVector>::scalar_type, bool>, BooleanVector> any(BooleanVector vec);
268+
261269
}
262270

263271
#endif

0 commit comments

Comments
 (0)