From ceca6d38d0abf9b0ae95b7f280498d591f055d42 Mon Sep 17 00:00:00 2001 From: Abbas Garousi Date: Mon, 7 Sep 2026 18:41:42 +0330 Subject: [PATCH] Fix HLSL conversion warnings and integer conversion correctness --- examples_tests | 2 +- include/nbl/builtin/hlsl/algorithm.hlsl | 4 +- include/nbl/builtin/hlsl/bda/__ptr.hlsl | 4 +- .../nbl/builtin/hlsl/bda/bda_accessor.hlsl | 16 +++--- .../hlsl/bxdf/base/cook_torrance_base.hlsl | 2 +- include/nbl/builtin/hlsl/bxdf/common.hlsl | 3 ++ .../nbl/builtin/hlsl/cpp_compat/truncate.hlsl | 4 +- .../nbl/builtin/hlsl/emulated/float64_t.hlsl | 18 +++---- .../builtin/hlsl/emulated/float64_t_impl.hlsl | 51 ++++++------------- .../nbl/builtin/hlsl/emulated/int64_t.hlsl | 27 +++++----- .../nbl/builtin/hlsl/emulated/vector_t.hlsl | 4 +- .../nbl/builtin/hlsl/format/shared_exp.hlsl | 9 ++-- .../nbl/builtin/hlsl/glsl_compat/core.hlsl | 10 ++-- include/nbl/builtin/hlsl/ieee754.hlsl | 2 +- include/nbl/builtin/hlsl/math/intutil.hlsl | 4 +- include/nbl/builtin/hlsl/morton.hlsl | 4 +- .../hlsl/path_tracing/unidirectional.hlsl | 1 + .../builtin/hlsl/rwmc/CascadeAccumulator.hlsl | 5 +- include/nbl/builtin/hlsl/rwmc/resolve.hlsl | 2 +- .../hlsl/sampling/quantized_sequence.hlsl | 4 +- include/nbl/builtin/hlsl/sort/counting.hlsl | 2 +- include/nbl/builtin/hlsl/tgmath/impl.hlsl | 6 +++ include/nbl/builtin/hlsl/type_traits.hlsl | 4 +- .../builtin/hlsl/workgroup/arithmetic.hlsl | 2 +- .../nbl/builtin/hlsl/workgroup/ballot.hlsl | 10 ++-- include/nbl/builtin/hlsl/workgroup/basic.hlsl | 3 +- include/nbl/builtin/hlsl/workgroup/fft.hlsl | 2 +- .../builtin/hlsl/workgroup/shared_scan.hlsl | 6 +-- 28 files changed, 101 insertions(+), 110 deletions(-) diff --git a/examples_tests b/examples_tests index f981a82274..ec14907d44 160000 --- a/examples_tests +++ b/examples_tests @@ -1 +1 @@ -Subproject commit f981a8227449667de7452da2cbd7ea5b8910e218 +Subproject commit ec14907d44d99049424b32d6836159ac3d6e566e diff --git a/include/nbl/builtin/hlsl/algorithm.hlsl b/include/nbl/builtin/hlsl/algorithm.hlsl index 631001686e..ea911bfae1 100644 --- a/include/nbl/builtin/hlsl/algorithm.hlsl +++ b/include/nbl/builtin/hlsl/algorithm.hlsl @@ -135,9 +135,9 @@ struct bound_t bool compare(const typename Accessor::value_type lhs, const typename Accessor::value_type rhs) { if (IsUpper) - return !comp(rhs,lhs); + return !_static_cast(comp(rhs,lhs)); else - return comp(lhs,rhs); + return _static_cast(comp(lhs,rhs)); } void comp_step(NBL_REF_ARG(Accessor) accessor, const uint32_t testPoint, const uint32_t rightBegin) diff --git a/include/nbl/builtin/hlsl/bda/__ptr.hlsl b/include/nbl/builtin/hlsl/bda/__ptr.hlsl index f57add2465..a5bedd314c 100644 --- a/include/nbl/builtin/hlsl/bda/__ptr.hlsl +++ b/include/nbl/builtin/hlsl/bda/__ptr.hlsl @@ -68,12 +68,12 @@ struct __ptr __ptr operator+(int64_t i) { i *= sizeof(T); - return __ptr::create(spirv::bitcast(addr)+i); + return __ptr::create(spirv::bitcast(addr)+_static_cast(i)); } __ptr operator-(int64_t i) { i *= sizeof(T); - return __ptr::create(spirv::bitcast(addr)-i); + return __ptr::create(spirv::bitcast(addr)-_static_cast(i)); } }; diff --git a/include/nbl/builtin/hlsl/bda/bda_accessor.hlsl b/include/nbl/builtin/hlsl/bda/bda_accessor.hlsl index caefc814c5..7a22bf736d 100644 --- a/include/nbl/builtin/hlsl/bda/bda_accessor.hlsl +++ b/include/nbl/builtin/hlsl/bda/bda_accessor.hlsl @@ -45,19 +45,19 @@ struct BdaAccessor : impl::BdaAccessorBase T get(const uint64_t index) { - bda::__ptr target = ptr + index; + bda::__ptr target = ptr + _static_cast(index); return target.template deref().load(); } void get(const uint64_t index, NBL_REF_ARG(T) value) { - bda::__ptr target = ptr + index; + bda::__ptr target = ptr + _static_cast(index); value = target.template deref().load(); } void set(const uint64_t index, const T value) { - bda::__ptr target = ptr + index; + bda::__ptr target = ptr + _static_cast(index); return target.template deref().store(value); } @@ -65,7 +65,7 @@ struct BdaAccessor : impl::BdaAccessorBase enable_if_t && is_integral::value && (sizeof(T) == 4 || sizeof(T) == 8), T> atomicAdd(const uint64_t index, const T value) { - bda::__ptr target = ptr + index; + bda::__ptr target = ptr + _static_cast(index); return glsl::atomicAdd(target.template deref().ptr.value, value); } @@ -73,7 +73,7 @@ struct BdaAccessor : impl::BdaAccessorBase enable_if_t && is_integral::value && (sizeof(T) == 4 || sizeof(T) == 8), T> atomicSub(const uint64_t index, const T value) { - bda::__ptr target = ptr + index; + bda::__ptr target = ptr + _static_cast(index); return glsl::atomicSub(target.template deref().ptr.value, value); } @@ -94,19 +94,19 @@ struct DoubleBdaAccessor : impl::BdaAccessorBase T get(const uint64_t index) { - bda::__ptr target = inputPtr + index; + bda::__ptr target = inputPtr + _static_cast(index); return target.template deref().load(); } void get(const uint64_t index, NBL_REF_ARG(T) value) { - bda::__ptr target = inputPtr + index; + bda::__ptr target = inputPtr + _static_cast(index); value = target.template deref().load(); } void set(const uint64_t index, const T value) { - bda::__ptr target = outputPtr + index; + bda::__ptr target = outputPtr + _static_cast(index); return target.template deref().store(value); } diff --git a/include/nbl/builtin/hlsl/bxdf/base/cook_torrance_base.hlsl b/include/nbl/builtin/hlsl/bxdf/base/cook_torrance_base.hlsl index 8ed0d6ac92..1ab199d36b 100644 --- a/include/nbl/builtin/hlsl/bxdf/base/cook_torrance_base.hlsl +++ b/include/nbl/builtin/hlsl/bxdf/base/cook_torrance_base.hlsl @@ -43,7 +43,7 @@ struct quant_query_helper template static quant_query_type __call(NBL_CONST_REF_ARG(N) ndf, NBL_CONST_REF_ARG(F) fresnel, NBL_CONST_REF_ARG(I) interaction, NBL_CONST_REF_ARG(C) cache) { - typename N::scalar_type dummy; + typename N::scalar_type dummy = typename N::scalar_type(0); return ndf.template createQuantQuery(interaction, cache, dummy); } }; diff --git a/include/nbl/builtin/hlsl/bxdf/common.hlsl b/include/nbl/builtin/hlsl/bxdf/common.hlsl index d9631d415f..889d62635e 100644 --- a/include/nbl/builtin/hlsl/bxdf/common.hlsl +++ b/include/nbl/builtin/hlsl/bxdf/common.hlsl @@ -1171,11 +1171,14 @@ struct beta // currently throws a boost preprocess error, see: https://github.com/Devsh-Graphics-Programming/Nabla/issues/932 // #ifdef __HLSL_VERSION + #pragma warning(suppress: 4068) #pragma dxc diagnostic push + #pragma warning(suppress: 4068) #pragma dxc diagnostic ignored "-Wliteral-range" // #endif const T thresholds[4] = { 0, 5e5, 1e6, 1e15 }; // threshold values gotten from testing when the function returns nan/inf/1 // #ifdef __HLSL_VERSION + #pragma warning(suppress: 4068) #pragma dxc diagnostic pop // #endif if (x+y > thresholds[mpl::find_lsb_v]) diff --git a/include/nbl/builtin/hlsl/cpp_compat/truncate.hlsl b/include/nbl/builtin/hlsl/cpp_compat/truncate.hlsl index ffe3d12641..fd2cab0249 100644 --- a/include/nbl/builtin/hlsl/cpp_compat/truncate.hlsl +++ b/include/nbl/builtin/hlsl/cpp_compat/truncate.hlsl @@ -23,7 +23,7 @@ struct Truncate { NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(U) v) { - return T(v); + return _static_cast(v); } }; @@ -36,7 +36,7 @@ struct Truncate::scalar_type> setter; To output; [[unroll]] - for (int i = 0; i < vector_traits::Dimension; ++i) + for (uint32_t i = 0; i < vector_traits::Dimension; ++i) setter(output, i, getter(v, i)); return output; } diff --git a/include/nbl/builtin/hlsl/emulated/float64_t.hlsl b/include/nbl/builtin/hlsl/emulated/float64_t.hlsl index 0ffb0e61a0..d4736578a0 100644 --- a/include/nbl/builtin/hlsl/emulated/float64_t.hlsl +++ b/include/nbl/builtin/hlsl/emulated/float64_t.hlsl @@ -107,8 +107,8 @@ namespace hlsl return bit_cast(rhs.data); } - const int lhsBiasedExp = ieee754::extractBiasedExponent(data); - const int rhsBiasedExp = ieee754::extractBiasedExponent(rhs.data); + const int lhsBiasedExp = _static_cast(ieee754::extractBiasedExponent(data)); + const int rhsBiasedExp = _static_cast(ieee754::extractBiasedExponent(rhs.data)); uint64_t lhsSign = ieee754::extractSignPreserveBitPattern(data); uint64_t rhsSign = ieee754::extractSignPreserveBitPattern(rhs.data); @@ -138,7 +138,7 @@ namespace hlsl const int expDiff = lhsBiasedExp - rhsBiasedExp; int exp = max(lhsBiasedExp, rhsBiasedExp) - ieee754::traits::exponentBias; - const uint32_t shiftAmount = abs(expDiff); + const uint32_t shiftAmount = _static_cast(abs(expDiff)); if (expDiff < 0) { @@ -244,8 +244,8 @@ namespace hlsl this_t retval = this_t::create(0ull); - int lhsBiasedExp = ieee754::extractBiasedExponent(data); - int rhsBiasedExp = ieee754::extractBiasedExponent(rhs.data); + int lhsBiasedExp = _static_cast(ieee754::extractBiasedExponent(data)); + int rhsBiasedExp = _static_cast(ieee754::extractBiasedExponent(rhs.data)); int exp = int(lhsBiasedExp + rhsBiasedExp) - ieee754::traits::exponentBias; @@ -289,8 +289,8 @@ namespace hlsl { const uint64_t sign = (data ^ rhs.data) & ieee754::traits::signMask; - int lhsBiasedExp = ieee754::extractBiasedExponent(data); - int rhsBiasedExp = ieee754::extractBiasedExponent(rhs.data); + int lhsBiasedExp = _static_cast(ieee754::extractBiasedExponent(data)); + int rhsBiasedExp = _static_cast(ieee754::extractBiasedExponent(rhs.data)); if(!FastMath) { @@ -324,7 +324,7 @@ namespace hlsl { const int shiftAmount = 52 - msb; assert(shiftAmount >= 0); - mantissa <<= shiftAmount; + mantissa <<= _static_cast(shiftAmount); exp -= shiftAmount; } @@ -478,7 +478,7 @@ NBL_CONSTEXPR_FUNC __VA_ARGS__ replaceBiasedExponent(__VA_ARGS__ x, typename uns template <>\ NBL_CONSTEXPR_FUNC __VA_ARGS__ fastMulExp2(__VA_ARGS__ x, int n)\ {\ - return __VA_ARGS__(replaceBiasedExponent(x.data, extractBiasedExponent(x) + uint32_t(n)));\ + return __VA_ARGS__(replaceBiasedExponent(x.data, extractBiasedExponent(x) + _static_cast(n)));\ }\ \ template <>\ diff --git a/include/nbl/builtin/hlsl/emulated/float64_t_impl.hlsl b/include/nbl/builtin/hlsl/emulated/float64_t_impl.hlsl index df785e3e8f..836a55621e 100644 --- a/include/nbl/builtin/hlsl/emulated/float64_t_impl.hlsl +++ b/include/nbl/builtin/hlsl/emulated/float64_t_impl.hlsl @@ -59,10 +59,10 @@ inline uint64_t castFloat32ToStorageType(float32_t val) if (hlsl::isinf(val)) return ieee754::traits::inf | sign; uint32_t asUint = ieee754::impl::bitCastToUintType(val); - const int f32BiasedExp = int(ieee754::extractBiasedExponent(val)); + const int f32BiasedExp = _static_cast(ieee754::extractBiasedExponent(val)); if (f32BiasedExp == 0) return sign; - const uint64_t biasedExp = uint64_t(f32BiasedExp - ieee754::traits::exponentBias + ieee754::traits::exponentBias) << (ieee754::traits::mantissaBitCnt); + const uint64_t biasedExp = _static_cast(f32BiasedExp - ieee754::traits::exponentBias + ieee754::traits::exponentBias) << (ieee754::traits::mantissaBitCnt); const uint64_t mantissa = (uint64_t(ieee754::traits::mantissaMask) & asUint) << (ieee754::traits::mantissaBitCnt - ieee754::traits::mantissaBitCnt); return sign | biasedExp | mantissa; @@ -85,46 +85,24 @@ inline uint64_t reinterpretAsFloat64BitPattern(T); template<> inline uint64_t reinterpretAsFloat64BitPattern(uint64_t val) { - if (isZero(val)) + if (val == 0ull) return val; - int exp = findMSB(val); + const int exp = findMSB(val); uint64_t mantissa; - int shiftCnt = 52 - exp; + const int shiftCnt = 52 - exp; if (shiftCnt >= 0) { - mantissa = val << shiftCnt; + mantissa = val << _static_cast(shiftCnt); } else { - const int shiftCntAbs = -shiftCnt; - uint64_t roundingBit = 1ull << (shiftCnt - 1); - uint64_t stickyBitMask = roundingBit - 1; - uint64_t stickyBit = val & stickyBitMask; - - mantissa = val >> shiftCntAbs; - - if ((val & roundingBit) && (!stickyBit)) - { - bool isEven = mantissa & 1; - if (!isEven) - mantissa++; - } - else if ((val & roundingBit) && (stickyBit || (mantissa & 1))) - val += roundingBit; - - //val += (1ull << (shiftCnt)) - 1; - //mantissa = val >> shiftCntAbs; - - if (mantissa & 1ull << 53) - { - mantissa >>= 1; - exp++; - } + // Integer conversions use the same round-toward-zero mode as arithmetic. + mantissa = val >> _static_cast(-shiftCnt); } mantissa &= ieee754::traits::mantissaMask; - const uint64_t biasedExp = uint64_t(ieee754::traits::exponentBias + exp) << ieee754::traits::mantissaBitCnt; + const uint64_t biasedExp = _static_cast(ieee754::traits::exponentBias + exp) << ieee754::traits::mantissaBitCnt; return biasedExp | mantissa; }; @@ -132,8 +110,9 @@ inline uint64_t reinterpretAsFloat64BitPattern(uint64_t val) template<> inline uint64_t reinterpretAsFloat64BitPattern(int64_t val) { - const uint64_t sign = val & ieee754::traits::signMask; - const uint64_t absVal = uint64_t(abs(val)); + const uint64_t bits = _static_cast(val); + const uint64_t sign = bits & ieee754::traits::signMask; + const uint64_t absVal = val < 0 ? 0ull - bits : bits; return sign | reinterpretAsFloat64BitPattern(absVal); }; @@ -297,8 +276,8 @@ inline uint64_t subMantissas128NormalizeResult(const uint64_t greaterNumberManti if (shiftAmount < 0) { shiftAmount = -shiftAmount; - diffHigh <<= shiftAmount; - const uint64_t shiftedOutBits = diffLow >> (64 - shiftAmount); + diffHigh <<= _static_cast(shiftAmount); + const uint64_t shiftedOutBits = diffLow >> _static_cast(64 - shiftAmount); diffHigh |= shiftedOutBits; } @@ -308,4 +287,4 @@ inline uint64_t subMantissas128NormalizeResult(const uint64_t greaterNumberManti } } } -#endif \ No newline at end of file +#endif diff --git a/include/nbl/builtin/hlsl/emulated/int64_t.hlsl b/include/nbl/builtin/hlsl/emulated/int64_t.hlsl index 4fa2014607..328584d882 100644 --- a/include/nbl/builtin/hlsl/emulated/int64_t.hlsl +++ b/include/nbl/builtin/hlsl/emulated/int64_t.hlsl @@ -259,8 +259,11 @@ struct arithmetic_right_shift_operator { const bool bigShift = bits >= ComponentBitWidth; // Shift that completely rewrites MSB const uint32_t shift = bigShift ? bits - ComponentBitWidth : ComponentBitWidth - bits; - const type_t shifted = type_t::create(bigShift ? vector(uint32_t(int32_t(operand.__getMSB()) >> shift), int32_t(operand.__getMSB()) < 0 ? ~uint32_t(0) : uint32_t(0)) - : vector((operand.__getMSB() << shift) | (operand.__getLSB() >> bits), uint32_t(int32_t(operand.__getMSB()) >> bits))); + const int32_t msbSigned = _static_cast(operand.__getMSB()); + const int32_t shiftSigned = _static_cast(shift); + const int32_t bitsSigned = _static_cast(bits); + const type_t shifted = type_t::create(bigShift ? vector(_static_cast(msbSigned >> shiftSigned), msbSigned < 0 ? ~uint32_t(0) : uint32_t(0)) + : vector((operand.__getMSB() << shift) | (operand.__getLSB() >> bits), _static_cast(msbSigned >> bitsSigned))); return select(bool(bits), shifted, operand); } @@ -382,27 +385,21 @@ NBL_CONSTEXPR_INLINE_NSPC_SCOPE_VAR emulated_int64_t minus_assign -struct NBL_ADD_STD make_unsigned : type_identity {}; +struct nbl::hlsl::make_unsigned : type_identity {}; template<> -struct NBL_ADD_STD make_unsigned : type_identity {}; +struct nbl::hlsl::make_unsigned : type_identity {}; template<> -struct NBL_ADD_STD make_signed : type_identity {}; +struct nbl::hlsl::make_signed : type_identity {}; template<> -struct NBL_ADD_STD make_signed : type_identity {}; - -#undef NBL_ADD_STD +struct nbl::hlsl::make_signed : type_identity {}; diff --git a/include/nbl/builtin/hlsl/emulated/vector_t.hlsl b/include/nbl/builtin/hlsl/emulated/vector_t.hlsl index 2192d348b9..e278831128 100644 --- a/include/nbl/builtin/hlsl/emulated/vector_t.hlsl +++ b/include/nbl/builtin/hlsl/emulated/vector_t.hlsl @@ -546,7 +546,7 @@ struct static_cast_helper, emulated_vector_t(getter(vec, i))); return output; @@ -564,7 +564,7 @@ struct static_cast_helper, emulated_vecto array_set setter;\ OutputVecType output;\ [[unroll]]\ - for (int i = 0; i < N; ++i)\ + for (uint32_t i = 0; i < N; ++i)\ setter(output, i, _static_cast(getter(vec, i)));\ return output;\ }\ diff --git a/include/nbl/builtin/hlsl/format/shared_exp.hlsl b/include/nbl/builtin/hlsl/format/shared_exp.hlsl index ef09cd8a0f..c57e40c6ac 100644 --- a/include/nbl/builtin/hlsl/format/shared_exp.hlsl +++ b/include/nbl/builtin/hlsl/format/shared_exp.hlsl @@ -184,12 +184,15 @@ struct static_cast_helper< mantissaShifts[i] = min(clampedSharedExponentDecBias+uint16_t(-limits_t::min_exponent)-exponentsDecBias[i],uint16_t(numeric_limits::digits)); // finally lets re-bias our exponent (it will always be positive), note the -1 because IEEE754 floats reserve the lowest exponent values for denorm - const uint16_t sharedExponentEncBias = int16_t(clampedSharedExponentDecBias+int16_t(-limits_t::min_exponent))-uint16_t(1-numeric_limits::min_exponent); + const int16_t biasOffset = _static_cast(-limits_t::min_exponent); + const int16_t encBiasSigned = _static_cast(clampedSharedExponentDecBias) + biasOffset; + const int16_t finalBias = _static_cast(1 - numeric_limits::min_exponent); + const uint16_t sharedExponentEncBias = _static_cast(encBiasSigned - finalBias); // T retval; retval.storage = storage_t(sharedExponentEncBias)<<(limits_t::digits*3); - const decode_bits_t dec_MantissaMask = (decode_bits_t(1)<(val[i])&dec_MantissaMask; @@ -201,7 +204,7 @@ struct static_cast_helper< if (limits_t::is_signed) { // doing ops on smaller integers is faster - decode_bits_t SignMask = 0x1<<(sizeof(decode_t)*8-1); + decode_bits_t SignMask = 0x1u<<(sizeof(decode_t)*8-1); decode_bits_t signs = bit_cast(val[0])&SignMask; for (uint16_t i=1; i<_Components; i++) signs |= (bit_cast(val[i])&SignMask)>>i; diff --git a/include/nbl/builtin/hlsl/glsl_compat/core.hlsl b/include/nbl/builtin/hlsl/glsl_compat/core.hlsl index c2450e57aa..088aa4fdc0 100644 --- a/include/nbl/builtin/hlsl/glsl_compat/core.hlsl +++ b/include/nbl/builtin/hlsl/glsl_compat/core.hlsl @@ -255,9 +255,9 @@ struct bitfieldInsert(1u) << bits) - _static_cast(1u); - const T shifted_mask = mask << offset; - return (base & ~shifted_mask) | ((insert & mask) << T(offset)); + const T mask = (_static_cast(1u) << _static_cast(bits)) - _static_cast(1u); + const T shifted_mask = mask << _static_cast(offset); + return (base & ~shifted_mask) | ((insert & mask) << _static_cast(offset)); } }; @@ -267,7 +267,7 @@ struct bitfieldExtract> T(offsetBits)) & T((T(1u) << numBits) - T(1u)); + const T ret = (val >> _static_cast(offsetBits)) & _static_cast((_static_cast(1u) << _static_cast(numBits)) - _static_cast(1u)); if (ret & (T(1u) << (numBits-1u))) ret |= T(~0ull) << numBits; return ret; @@ -280,7 +280,7 @@ struct bitfieldExtract> T(offsetBits)) & T((T(1u) << numBits) - T(1u)); + return (val >> _static_cast(offsetBits)) & _static_cast((_static_cast(1u) << _static_cast(numBits)) - _static_cast(1u)); } }; } diff --git a/include/nbl/builtin/hlsl/ieee754.hlsl b/include/nbl/builtin/hlsl/ieee754.hlsl index 1cbd3fc7ee..46f19536ca 100644 --- a/include/nbl/builtin/hlsl/ieee754.hlsl +++ b/include/nbl/builtin/hlsl/ieee754.hlsl @@ -86,7 +86,7 @@ template inline int extractExponent(T x) { using AsFloat = typename float_of_size::type; - return int(extractBiasedExponent(x)) - traits::exponentBias; + return _static_cast(extractBiasedExponent(x)) - traits::exponentBias; } template diff --git a/include/nbl/builtin/hlsl/math/intutil.hlsl b/include/nbl/builtin/hlsl/math/intutil.hlsl index 7394e03ae4..d057d90421 100644 --- a/include/nbl/builtin/hlsl/math/intutil.hlsl +++ b/include/nbl/builtin/hlsl/math/intutil.hlsl @@ -25,13 +25,13 @@ NBL_CONSTEXPR_FORCED_INLINE_FUNC bool isPoT(Integer value) template) NBL_CONSTEXPR_FORCED_INLINE_FUNC Integer roundUpToPoT(Integer value) { - return Integer(0x1u) << Integer(1 + hlsl::findMSB(value - Integer(1))); // this wont result in constexpr because findMSB is not one + return Integer(0x1u) << _static_cast(1 + hlsl::findMSB(value - Integer(1))); // this wont result in constexpr because findMSB is not one } template) NBL_CONSTEXPR_FORCED_INLINE_FUNC Integer roundDownToPoT(Integer value) { - return Integer(0x1u) << hlsl::findMSB(value); + return Integer(0x1u) << _static_cast(hlsl::findMSB(value)); } template) diff --git a/include/nbl/builtin/hlsl/morton.hlsl b/include/nbl/builtin/hlsl/morton.hlsl index 9ba33ffb3d..29f331051d 100644 --- a/include/nbl/builtin/hlsl/morton.hlsl +++ b/include/nbl/builtin/hlsl/morton.hlsl @@ -620,9 +620,9 @@ struct arithmetic_right_shift_operator > // To avoid branching, we left-shift each coordinate to put the MSB (of the encoded Morton) at the position of the MSB (of the `scalar_t` used for the decoded coordinate), // then right-shift again to get correct sign on each coordinate // The number of bits we shift by to put MSB of Morton at MSB of `scalar_t` is the difference between the bitwidth of `scalar_t` and Bits - const scalar_t ShiftFactor = scalar_t(8 * sizeof(scalar_t) - Bits); + const scalar_t ShiftFactor = _static_cast(8 * sizeof(scalar_t) - Bits); cartesian <<= ShiftFactor; - cartesian >>= ShiftFactor + scalar_t(bits); + cartesian >>= ShiftFactor + _static_cast(bits); return type_t::create(cartesian); } }; diff --git a/include/nbl/builtin/hlsl/path_tracing/unidirectional.hlsl b/include/nbl/builtin/hlsl/path_tracing/unidirectional.hlsl index 4b85a9581e..2d6823cf6f 100644 --- a/include/nbl/builtin/hlsl/path_tracing/unidirectional.hlsl +++ b/include/nbl/builtin/hlsl/path_tracing/unidirectional.hlsl @@ -8,6 +8,7 @@ #include #include #include +#include namespace nbl { diff --git a/include/nbl/builtin/hlsl/rwmc/CascadeAccumulator.hlsl b/include/nbl/builtin/hlsl/rwmc/CascadeAccumulator.hlsl index 003b52fcdf..c67f0387f9 100644 --- a/include/nbl/builtin/hlsl/rwmc/CascadeAccumulator.hlsl +++ b/include/nbl/builtin/hlsl/rwmc/CascadeAccumulator.hlsl @@ -77,7 +77,8 @@ struct CascadeAccumulator } // most of this code is stolen from https://cg.ivd.kit.edu/publications/2018/rwmc/tool/split.cpp - void addSample(const sample_count_type sampleCount, input_sample_type _sample) + template + void addSample(const SampleCountType sampleCount, input_sample_type _sample) { const uint16_t lastCascade = accumulation.getLastCascade(); @@ -97,7 +98,7 @@ struct CascadeAccumulator if (cascade > lastCascade) lowerCascadeWeight = hlsl::exp2(_static_cast(splattingParameters.BrightSampleLumaBias - log2Luma)); - accumulation.addSampleIntoCascadeEntry(_sample, lowerCascadeIndex, lowerCascadeWeight, higherCascadeWeight, sampleCount); + accumulation.addSampleIntoCascadeEntry(_sample, lowerCascadeIndex, lowerCascadeWeight, higherCascadeWeight, _static_cast(sampleCount)); } diff --git a/include/nbl/builtin/hlsl/rwmc/resolve.hlsl b/include/nbl/builtin/hlsl/rwmc/resolve.hlsl index 348e26bdf3..250a86e5ce 100644 --- a/include/nbl/builtin/hlsl/rwmc/resolve.hlsl +++ b/include/nbl/builtin/hlsl/rwmc/resolve.hlsl @@ -101,7 +101,7 @@ struct SResolver if (notLastCascade) { reciprocalBaseI *= params.reciprocalBase; - next = __sampleCascade(acc, coord, int16_t(i + 1), reciprocalBaseI); + next = __sampleCascade(acc, coord, _static_cast(i + 1), reciprocalBaseI); } scalar_t reliability = 1.f; diff --git a/include/nbl/builtin/hlsl/sampling/quantized_sequence.hlsl b/include/nbl/builtin/hlsl/sampling/quantized_sequence.hlsl index db575e0250..845e4af382 100644 --- a/include/nbl/builtin/hlsl/sampling/quantized_sequence.hlsl +++ b/include/nbl/builtin/hlsl/sampling/quantized_sequence.hlsl @@ -57,7 +57,7 @@ struct encode_helper { uniform_storage_type asuint; NBL_UNROLL for(uint16_t i = 0; i < Dim; i++) - asuint[i] = uniform_storage_scalar_type(unormvec[i] * UNormMultiplier); + asuint[i] = _static_cast(unormvec[i] * UNormMultiplier); NBL_IF_CONSTEXPR(Dim==1) return sequence_type::create(asuint[0]); else @@ -80,7 +80,7 @@ struct decode_before_scramble_helper uvec_type seqVal; NBL_UNROLL for(uint16_t i = 0; i < Dim; i++) seqVal[i] = val.get(i) << Q::DiscardBits; // restore high bits - seqVal ^= scrambleKey; + (void)(seqVal ^= scrambleKey); // glm declares operator^= [[nodiscard]] even though it returns a self-reference return return_type(seqVal) * bit_cast >(UNormConstant); } diff --git a/include/nbl/builtin/hlsl/sort/counting.hlsl b/include/nbl/builtin/hlsl/sort/counting.hlsl index 1cd916ccc8..13aad4c1fc 100644 --- a/include/nbl/builtin/hlsl/sort/counting.hlsl +++ b/include/nbl/builtin/hlsl/sort/counting.hlsl @@ -91,7 +91,7 @@ struct counting const bool is_last_wg_invocation = tid == (GroupSize-_static_cast(1)); const static uint16_t RoundedKeyBucketCount = (KeyBucketCount-_static_cast(1))/GroupSize+_static_cast(1); - for (int i = 1; i < RoundedKeyBucketCount; i++) + for (uint16_t i = 1; i < RoundedKeyBucketCount; i++) { uint32_t keyBucketStart = GroupSize * i; uint32_t vid = tid + keyBucketStart; diff --git a/include/nbl/builtin/hlsl/tgmath/impl.hlsl b/include/nbl/builtin/hlsl/tgmath/impl.hlsl index 0c1dc2f458..f7162230fd 100644 --- a/include/nbl/builtin/hlsl/tgmath/impl.hlsl +++ b/include/nbl/builtin/hlsl/tgmath/impl.hlsl @@ -545,11 +545,14 @@ struct l2gamma_helper) > { // currently throws a boost preprocess error, see: https://github.com/Devsh-Graphics-Programming/Nabla/issues/932 // #ifdef __HLSL_VERSION + #pragma warning(suppress: 4068) #pragma dxc diagnostic push + #pragma warning(suppress: 4068) #pragma dxc diagnostic ignored "-Wliteral-range" // #endif const T thresholds[4] = { 0, 5e4, 1e36, 1e305 }; // threshold values gotten from testing when the function returns nan/inf // #ifdef __HLSL_VERSION + #pragma warning(suppress: 4068) #pragma dxc diagnostic pop // #endif if (x > thresholds[mpl::find_lsb_v]) @@ -595,11 +598,14 @@ struct beta_helper) > { // currently throws a boost preprocess error, see: https://github.com/Devsh-Graphics-Programming/Nabla/issues/932 // #ifdef __HLSL_VERSION + #pragma warning(suppress: 4068) #pragma dxc diagnostic push + #pragma warning(suppress: 4068) #pragma dxc diagnostic ignored "-Wliteral-range" // #endif const T thresholds[4] = { 0, 2e4, 1e6, 1e15 }; // threshold values gotten from testing when the function returns nan/inf/1 // #ifdef __HLSL_VERSION + #pragma warning(suppress: 4068) #pragma dxc diagnostic pop // #endif if (v1+v2 > thresholds[mpl::find_lsb_v]) diff --git a/include/nbl/builtin/hlsl/type_traits.hlsl b/include/nbl/builtin/hlsl/type_traits.hlsl index 92b62fdaa8..883b21cb92 100644 --- a/include/nbl/builtin/hlsl/type_traits.hlsl +++ b/include/nbl/builtin/hlsl/type_traits.hlsl @@ -616,10 +616,10 @@ template using remove_extent = std::remove_extent; template using remove_all_extents = std::remove_all_extents; template -using make_signed = std::make_signed; +struct make_signed : std::make_signed {}; template -using make_unsigned = std::make_unsigned; +struct make_unsigned : std::make_unsigned {}; #endif diff --git a/include/nbl/builtin/hlsl/workgroup/arithmetic.hlsl b/include/nbl/builtin/hlsl/workgroup/arithmetic.hlsl index f8c5c75cb0..2d013f0234 100644 --- a/include/nbl/builtin/hlsl/workgroup/arithmetic.hlsl +++ b/include/nbl/builtin/hlsl/workgroup/arithmetic.hlsl @@ -92,7 +92,7 @@ uint16_t ballotCountedBitDWORD(NBL_REF_ARG(BallotAccessor) ballotAccessor) const uint16_t Remainder = ItemCount&_static_cast(31); if (Remainder!=0 && index==DWORDCount-1) bitfield &= (0x1<(countbits(bitfield)); } return 0; } diff --git a/include/nbl/builtin/hlsl/workgroup/ballot.hlsl b/include/nbl/builtin/hlsl/workgroup/ballot.hlsl index bcfdde8c4b..175ed054cf 100644 --- a/include/nbl/builtin/hlsl/workgroup/ballot.hlsl +++ b/include/nbl/builtin/hlsl/workgroup/ballot.hlsl @@ -42,8 +42,8 @@ struct ballot { if (glsl::subgroupElect()) { - const uint16_t SubgroupSizeLog2 = uint16_t(glsl::gl_SubgroupSizeLog2()); - const uint16_t subgroupID = uint16_t(glsl::gl_SubgroupID()); + const uint16_t SubgroupSizeLog2 = _static_cast(glsl::gl_SubgroupSizeLog2()); + const uint16_t subgroupID = _static_cast(glsl::gl_SubgroupID()); const uint16_t shift = (subgroupID<>(5-SubgroupSizeLog2),subgroupBallot[0]< template static void __call(const uint32_t4 subgroupBallot, const uint16_t subgroupInvocation, NBL_REF_ARG(Accessor) accessor) { - const uint16_t SubgroupSizeLog2 = uint16_t(glsl::gl_SubgroupSizeLog2()); - const uint16_t subgroupID = uint16_t(glsl::gl_SubgroupID()); + const uint16_t SubgroupSizeLog2 = _static_cast(glsl::gl_SubgroupSizeLog2()); + const uint16_t subgroupID = _static_cast(glsl::gl_SubgroupID()); const uint16_t destIx = subgroupID<<(SubgroupSizeLog2-5); const uint16_t UsefulComponents = uint16_t(0x1u)<<(SubgroupSizeLog2-5); @@ -89,7 +89,7 @@ template void ballot(const bool value, NBL_REF_ARG(Accessor) accessor) { const uint32_t4 bitfield = glsl::subgroupBallot(value); - const uint16_t subgroupInvocation = uint16_t(glsl::gl_SubgroupInvocationID()); + const uint16_t subgroupInvocation = _static_cast(glsl::gl_SubgroupInvocationID()); if (glsl::gl_SubgroupSizeLog2()<4) impl::ballot::template __call(bitfield,subgroupInvocation,accessor); diff --git a/include/nbl/builtin/hlsl/workgroup/basic.hlsl b/include/nbl/builtin/hlsl/workgroup/basic.hlsl index 3467b46407..2edf08d76e 100644 --- a/include/nbl/builtin/hlsl/workgroup/basic.hlsl +++ b/include/nbl/builtin/hlsl/workgroup/basic.hlsl @@ -5,6 +5,7 @@ #define _NBL_BUILTIN_HLSL_WORKGROUP_BASIC_INCLUDED_ #include "nbl/builtin/hlsl/glsl_compat/subgroup_ballot.hlsl" +#include "nbl/builtin/hlsl/type_traits.hlsl" //! all functions must be called in uniform control flow (all workgroup invocations active) namespace nbl @@ -25,7 +26,7 @@ uint16_t Volume() uint16_t SubgroupContiguousIndex() { - const uint16_t retval = (uint16_t(glsl::gl_SubgroupID())<((glsl::gl_SubgroupID()<(1u + findMSB(_static_cast(min(FFTLength / 2, actualMaxWorkgroupSize) - 1u))); + const uint16_t workgroupSizeLog2 = _static_cast(1u + _static_cast(findMSB(_static_cast(min(FFTLength / 2, actualMaxWorkgroupSize) - 1u)))); // Parameters are valid if the workgroup size is at most half of the FFT Length and at least as big as the smallest subgroup that can be launched if ((FFTLength >> workgroupSizeLog2) <= 1 || minSubgroupSize > (1u << workgroupSizeLog2)) diff --git a/include/nbl/builtin/hlsl/workgroup/shared_scan.hlsl b/include/nbl/builtin/hlsl/workgroup/shared_scan.hlsl index b52d083733..dab64c3d66 100644 --- a/include/nbl/builtin/hlsl/workgroup/shared_scan.hlsl +++ b/include/nbl/builtin/hlsl/workgroup/shared_scan.hlsl @@ -91,7 +91,7 @@ struct scan// : reduce https://github.com/microsoft/DirectXShad { __base.template __call(value,scratchAccessor); - const uint16_t subgroupID = uint16_t(glsl::gl_SubgroupID()); + const uint16_t subgroupID = _static_cast(glsl::gl_SubgroupID()); // abuse integer wraparound to map 0 to 0xffffu const uint16_t prevSubgroupID = subgroupID-_static_cast(1); @@ -99,7 +99,7 @@ struct scan// : reduce https://github.com/microsoft/DirectXShad const uint16_t lastInvocation = ItemCount-_static_cast(1); if(lastInvocation>=uint16_t(glsl::gl_SubgroupSize())) { - const uint16_t subgroupSizeLog2 = uint16_t(glsl::gl_SubgroupSizeLog2()); + const uint16_t subgroupSizeLog2 = _static_cast(glsl::gl_SubgroupSizeLog2()); // different than Upsweep cause we need to translate high level inclusive scans into exclusive on the fly, so we get the value of the subgroup behind our own in each level const uint16_t storeLoadIndexDiff = SubgroupContiguousIndex()-prevSubgroupID; @@ -108,7 +108,7 @@ struct scan// : reduce https://github.com/microsoft/DirectXShad #define scanStoreIndex __base.scanLoadIndex // we sloop over levels from highest to penultimate // as we iterate some previously active (higher level) invocations hold their exclusive prefix sum in `lastLevelScan` - const uint16_t temp = uint16_t(firstbithigh(uint32_t(lastInvocation))/subgroupSizeLog2); // doing division then multiplication might be optimized away by the compiler + const uint16_t temp = _static_cast(firstbithigh(uint32_t(lastInvocation))/subgroupSizeLog2); // doing division then multiplication might be optimized away by the compiler const uint16_t initialLogShift = temp*subgroupSizeLog2; // TODO: later [unroll(scan_levels::value-1)] [unroll(1)]