|
9 | 9 | #include <nbl/builtin/hlsl/spirv_intrinsics/glsl.std.450.hlsl>
|
10 | 10 | #include <nbl/builtin/hlsl/ieee754.hlsl>
|
11 | 11 |
|
| 12 | +#ifndef __HLSL_VERSION |
| 13 | +#include <bitset> |
| 14 | +#endif |
| 15 | + |
12 | 16 | namespace nbl
|
13 | 17 | {
|
14 | 18 | namespace hlsl
|
@@ -360,6 +364,114 @@ struct mul_helper
|
360 | 364 | }
|
361 | 365 | };
|
362 | 366 |
|
| 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 | + |
363 | 475 | }
|
364 | 476 | }
|
365 | 477 | }
|
|
0 commit comments