Skip to content

Commit 70f2870

Browse files
committed
Fixed bitcount, added normalize
1 parent a640976 commit 70f2870

File tree

4 files changed

+125
-16
lines changed

4 files changed

+125
-16
lines changed

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
1010
#include <nbl/builtin/hlsl/ieee754.hlsl>
1111

12+
#ifndef __HLSL_VERSION
13+
#include <bitset>
14+
#endif
15+
1216
namespace nbl
1317
{
1418
namespace hlsl
@@ -360,6 +364,114 @@ struct mul_helper
360364
}
361365
};
362366

367+
// TODO: some struct that with other functions, since more functions will need that..
368+
template<typename T NBL_STRUCT_CONSTRAINABLE>
369+
struct bitcount_output;
370+
371+
template<typename Integer>
372+
NBL_PARTIAL_REQ_TOP(hlsl::is_integral_v<Integer> && hlsl::is_scalar_v<Integer>)
373+
struct bitcount_output<Integer NBL_PARTIAL_REQ_BOT(hlsl::is_integral_v<Integer>&& hlsl::is_scalar_v<Integer>) >
374+
{
375+
using type = int32_t;
376+
};
377+
378+
template<typename IntegerVector>
379+
NBL_PARTIAL_REQ_TOP(hlsl::is_integral_v<IntegerVector> && hlsl::is_vector_v<IntegerVector>)
380+
struct bitcount_output<IntegerVector NBL_PARTIAL_REQ_BOT(hlsl::is_integral_v<IntegerVector> && hlsl::is_vector_v<IntegerVector>) >
381+
{
382+
using type = vector<int32_t, hlsl::vector_traits<IntegerVector>::Dimension>;
383+
};
384+
385+
#ifndef __HLSL_VERSION
386+
template<typename EnumT>
387+
requires std::is_enum_v<EnumT>
388+
struct bitcount_output<EnumT NBL_PARTIAL_REQ_BOT(hlsl::is_enum_v<EnumT>) >
389+
{
390+
using type = int32_t;
391+
};
392+
#endif
393+
394+
template<typename T>
395+
using bitcount_output_t = typename bitcount_output<T>::type;
396+
397+
template<typename Integer NBL_STRUCT_CONSTRAINABLE>
398+
struct bitCount_helper;
399+
400+
template<typename Integer>
401+
NBL_PARTIAL_REQ_TOP(hlsl::is_integral_v<Integer>&& hlsl::is_scalar_v<Integer>)
402+
struct bitCount_helper<Integer NBL_PARTIAL_REQ_BOT(hlsl::is_integral_v<Integer>&& hlsl::is_scalar_v<Integer>) >
403+
{
404+
static bitcount_output_t<Integer> __call(NBL_CONST_REF_ARG(Integer) val)
405+
{
406+
#ifdef __HLSL_VERSION
407+
if (sizeof(Integer) == 8u)
408+
{
409+
uint32_t lowBits = uint32_t(val);
410+
uint32_t highBits = uint32_t(uint64_t(val) >> 32u);
411+
412+
return countbits(lowBits) + countbits(highBits);
413+
}
414+
415+
return spirv::bitCount(val);
416+
417+
#else
418+
using UnsignedInteger = typename hlsl::unsigned_integer_of_size_t<sizeof(Integer)>;
419+
constexpr int32_t BitCnt = sizeof(Integer) * 8u;
420+
std::bitset<BitCnt> bitset(static_cast<UnsignedInteger>(val));
421+
return bitset.count();
422+
#endif
423+
}
424+
};
425+
426+
template<typename Vector>
427+
NBL_PARTIAL_REQ_TOP(hlsl::is_integral_v<Vector> && hlsl::is_vector_v<Vector>)
428+
struct bitCount_helper<Vector NBL_PARTIAL_REQ_BOT(hlsl::is_integral_v<Vector> && hlsl::is_vector_v<Vector>) >
429+
{
430+
static bitcount_output_t<Vector> __call(NBL_CONST_REF_ARG(Vector) vec)
431+
{
432+
using traits = hlsl::vector_traits<Vector>;
433+
array_get<Vector, typename traits::scalar_type> getter;
434+
array_set<Vector, typename traits::scalar_type> setter;
435+
436+
Vector output;
437+
for (uint32_t i = 0; i < traits::Dimension; ++i)
438+
setter(output, i, bitCount_helper<typename traits::scalar_type>::__call(getter(vec, i)));
439+
440+
return output;
441+
}
442+
};
443+
444+
#ifndef __HLSL_VERSION
445+
template<typename EnumT>
446+
requires std::is_enum_v<EnumT>
447+
struct bitCount_helper<EnumT>
448+
{
449+
using underlying_t = std::underlying_type_t<EnumT>;
450+
451+
static bitcount_output_t<EnumT> __call(NBL_CONST_REF_ARG(EnumT) val)
452+
{
453+
return bitCount_helper<const underlying_t>::__call(reinterpret_cast<const underlying_t&>(val));
454+
}
455+
};
456+
#endif
457+
458+
template<typename Vector NBL_STRUCT_CONSTRAINABLE>
459+
struct normalize_helper;
460+
461+
template<typename Vector>
462+
NBL_PARTIAL_REQ_TOP(hlsl::is_floating_point_v<Vector> && hlsl::is_vector_v<Vector>)
463+
struct normalize_helper<Vector NBL_PARTIAL_REQ_BOT(hlsl::is_floating_point_v<Vector> && hlsl::is_vector_v<Vector>) >
464+
{
465+
static inline Vector __call(NBL_CONST_REF_ARG(Vector) vec)
466+
{
467+
#ifdef __HLSL_VERSION
468+
return normalize(vec);
469+
#else
470+
return vec / std::sqrt(dot_helper<Vector>::__call(vec, vec));
471+
#endif
472+
}
473+
};
474+
363475
}
364476
}
365477
}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,9 @@ namespace hlsl
2020
{
2121

2222
template<typename Integer>
23-
inline int bitCount(NBL_CONST_REF_ARG(Integer) val)
23+
inline cpp_compat_intrinsics_impl::bitcount_output_t<Integer> bitCount(NBL_CONST_REF_ARG(Integer) val)
2424
{
25-
#ifdef __HLSL_VERSION
26-
if (sizeof(Integer) == 8u)
27-
{
28-
uint32_t lowBits = uint32_t(val);
29-
uint32_t highBits = uint32_t(uint64_t(val) >> 32u);
30-
31-
return countbits(lowBits) + countbits(highBits);
32-
}
33-
34-
return countbits(val);
35-
36-
#else
37-
return glm::bitCount(val);
38-
#endif
25+
return cpp_compat_intrinsics_impl::bitCount_helper<Integer>::__call(val);
3926
}
4027

4128
template<typename T>
@@ -58,6 +45,12 @@ T clamp(NBL_CONST_REF_ARG(T) val, NBL_CONST_REF_ARG(T) min, NBL_CONST_REF_ARG(T)
5845
#endif
5946
}
6047

48+
template<typename Vector>
49+
Vector normalize(NBL_CONST_REF_ARG(Vector) vec)
50+
{
51+
return cpp_compat_intrinsics_impl::normalize_helper<Vector>::__call(vec);
52+
}
53+
6154
template<typename T>
6255
typename vector_traits<T>::scalar_type dot(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs)
6356
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ template<typename Matrix>
246246
[[vk::ext_instruction( spv::OpTranspose )]]
247247
Matrix transpose(Matrix mat);
248248

249+
template<typename Integral>
250+
[[vk::ext_instruction(spv::OpBitCount)]]
251+
enable_if_t<is_integral_v<Integral>, Integral> bitCount(Integral mat);
252+
249253
}
250254

251255
#endif

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/macros.h")
214214
# utility
215215
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/array_accessors.hlsl")
216216
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/vector_utils/vector_traits.hlsl")
217-
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/matrix_utils/matrix_utils.hlsl")
217+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/matrix_utils/matrix_traits.hlsl")
218218
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/matrix_utils/mul_output_t.hlsl")
219219
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/tgmath.hlsl")
220220
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/impl/tgmath_impl.hlsl")

0 commit comments

Comments
 (0)