Skip to content

Commit ead1199

Browse files
committed
Implemented frac and modf
1 parent 8dd6918 commit ead1199

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ template<typename T NBL_STRUCT_CONSTRAINABLE>
6161
struct any_helper;
6262
template<typename T, uint16_t Bits NBL_STRUCT_CONSTRAINABLE>
6363
struct bitReverseAs_helper;
64+
template<typename T NBL_STRUCT_CONSTRAINABLE>
65+
struct frac_helper;
6466

6567
#ifdef __HLSL_VERSION // HLSL only specializations
6668
template<typename T, typename U> NBL_PARTIAL_REQ_TOP(always_true<decltype(spirv::fClamp<T>(experimental::declval<T>(), experimental::declval<U>(), experimental::declval<U>()))>)
@@ -112,6 +114,7 @@ AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(transpose_helper, transpose, T)
112114
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(length_helper, length, T)
113115
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(normalize_helper, normalize, T)
114116
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(rsqrt_helper, inverseSqrt, T)
117+
AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(frac_helper, fract, T)
115118

116119
template<typename UInt64> NBL_PARTIAL_REQ_TOP(is_same_v<UInt64, uint64_t>)
117120
struct find_msb_helper<UInt64 NBL_PARTIAL_REQ_BOT(is_same_v<UInt64, uint64_t>) >
@@ -297,6 +300,17 @@ struct rsqrt_helper<FloatingPoint>
297300
}
298301
};
299302

303+
template<typename T>
304+
requires concepts::FloatingPointScalar<T>
305+
struct frac_helper<T>
306+
{
307+
using return_t = T;
308+
static inline return_t __call(const T x)
309+
{
310+
return x - std::floor(x);
311+
}
312+
};
313+
300314
#endif // C++ only specializations
301315

302316
// C++ and HLSL specializations
@@ -514,12 +528,19 @@ struct HELPER_NAME<T NBL_PARTIAL_REQ_BOT(REQUIREMENT) >\
514528
}\
515529
};
516530

517-
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(rsqrt_helper, concepts::FloatingPointVectorial<T>, T)
531+
#ifdef __HLSL_VERSION
532+
// SPIR-V already defines specializations for builtin vector types
533+
#define VECTOR_SPECIALIZATION_CONCEPT concepts::Vectorial<T> && !is_vector_v<T>
534+
#else
535+
#define VECTOR_SPECIALIZATION_CONCEPT concepts::Vectorial<T>
536+
#endif
518537

538+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(rsqrt_helper, concepts::FloatingPointVectorial<T> && VECTOR_SPECIALIZATION_CONCEPT, T)
539+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(frac_helper, VECTOR_SPECIALIZATION_CONCEPT,T)
519540
#define INT32_VECTOR_TYPE vector<int32_t, hlsl::vector_traits<T>::Dimension>
520-
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(bitCount_helper, concepts::Vectorial<T>, INT32_VECTOR_TYPE)
521-
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(find_msb_helper, concepts::Vectorial<T>, INT32_VECTOR_TYPE)
522-
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(find_lsb_helper, concepts::Vectorial<T>, INT32_VECTOR_TYPE)
541+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(bitCount_helper, VECTOR_SPECIALIZATION_CONCEPT, INT32_VECTOR_TYPE)
542+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(find_msb_helper, VECTOR_SPECIALIZATION_CONCEPT, INT32_VECTOR_TYPE)
543+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(find_lsb_helper, VECTOR_SPECIALIZATION_CONCEPT, INT32_VECTOR_TYPE)
523544
#undef INT32_VECTOR_TYPE
524545
#undef AUTO_SPECIALIZE_HELPER_FOR_VECTOR
525546

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ inline bool any(Vector vec)
175175
return cpp_compat_intrinsics_impl::any_helper<Vector>::__call(vec);
176176
}
177177

178+
/**
179+
* @brief Returns x - floor(x).
180+
*
181+
* @tparam T type of the value to operate on.
182+
*
183+
* @param [in] val The value to operate on.
184+
*/
185+
template<typename T>
186+
inline T frac(NBL_CONST_REF_ARG(T) val)
187+
{
188+
return cpp_compat_intrinsics_impl::frac_helper<T>::__call(val);
189+
}
190+
178191
}
179192
}
180193

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ template<typename T NBL_STRUCT_CONSTRAINABLE>
6565
struct sqrt_helper;
6666
template<typename T, typename U NBL_STRUCT_CONSTRAINABLE>
6767
struct lerp_helper;
68+
template<typename T NBL_STRUCT_CONSTRAINABLE>
69+
struct modf_helper;
6870

6971
#ifdef __HLSL_VERSION
7072

@@ -115,6 +117,39 @@ struct lerp_helper<T, U NBL_PARTIAL_REQ_BOT(always_true<decltype(spirv::fMix<T>(
115117
}
116118
};
117119

120+
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::FloatingPointScalar<T>)
121+
struct modf_helper<T NBL_PARTIAL_REQ_BOT(concepts::FloatingPointScalar<T>) >
122+
{
123+
using return_t = T;
124+
static inline return_t __call(const T x)
125+
{
126+
T tmp = abs_helper<T>::__call(x);
127+
tmp = spirv::fract<T>(tmp);
128+
if (x < 0)
129+
tmp *= -1;
130+
131+
return tmp;
132+
}
133+
};
134+
135+
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::FloatingPoint<T> && is_vector_v<T>)
136+
struct modf_helper<T NBL_PARTIAL_REQ_BOT(concepts::FloatingPoint<T> && is_vector_v<T>) >
137+
{
138+
using return_t = T;
139+
static inline return_t __call(const T x)
140+
{
141+
using traits = hlsl::vector_traits<T>;
142+
array_get<T, typename traits::scalar_type> getter;
143+
array_set<T, typename traits::scalar_type> setter;
144+
145+
return_t output;
146+
for (uint32_t i = 0; i < traits::Dimension; ++i)
147+
setter(output, i, modf_helper<typename traits::scalar_type>::__call(getter(x, i)));
148+
149+
return output;
150+
}
151+
};
152+
118153
#else // C++ only specializations
119154

120155
#define AUTO_SPECIALIZE_TRIVIAL_CASE_HELPER(HELPER_NAME, REQUIREMENT, STD_FUNCTION_NAME, RETURN_TYPE)\
@@ -151,6 +186,18 @@ struct pow_helper<T>
151186
}
152187
};
153188

189+
template<typename T>
190+
requires concepts::FloatingPointScalar<T>
191+
struct modf_helper<T>
192+
{
193+
using return_t = T;
194+
static inline return_t __call(const T x)
195+
{
196+
T tmp;
197+
return std::modf(x, &tmp);
198+
}
199+
};
200+
154201
template<typename T>
155202
requires concepts::FloatingPointScalar<T>
156203
struct isinf_helper<T>
@@ -307,6 +354,7 @@ AUTO_SPECIALIZE_HELPER_FOR_VECTOR(isnan_helper, INT_VECTOR_RETURN_TYPE)
307354
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(cos_helper, T)
308355
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(sin_helper, T)
309356
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(acos_helper, T)
357+
AUTO_SPECIALIZE_HELPER_FOR_VECTOR(modf_helper, T)
310358

311359
#undef INT_VECTOR_RETURN_TYPE
312360
#undef AUTO_SPECIALIZE_HELPER_FOR_VECTOR

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ template<typename T NBL_FUNC_REQUIRES(concepts::FloatingPointVectorOrScalar32or1
164164
[[vk::ext_instruction(GLSLstd450Acos, "GLSL.std.450")]]
165165
T acos(T val);
166166

167+
template<typename T NBL_FUNC_REQUIRES(concepts::FloatingPointVectorOrScalar<T>)
168+
[[vk::ext_instruction(GLSLstd450Fract, "GLSL.std.450")]]
169+
T fract(T val);
170+
167171
}
168172
}
169173
}

include/nbl/builtin/hlsl/tgmath.hlsl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ inline T acos(NBL_CONST_REF_ARG(T) val)
114114
return tgmath_impl::acos_helper<T>::__call(val);
115115
}
116116

117+
/**
118+
* @brief Returns fractional part of given floating-point value.
119+
*
120+
* @tparam T type of the value to operate on.
121+
*
122+
* @param [in] val The value to retrive fractional part from.
123+
*/
124+
template<typename T>
125+
inline T modf(NBL_CONST_REF_ARG(T) val)
126+
{
127+
return tgmath_impl::modf_helper<T>::__call(val);
128+
}
129+
117130
}
118131
}
119132

0 commit comments

Comments
 (0)