ITADN

Fix narrowing conversion from `unsigned __int64` to `size_t`

#104Closedluadebug 创建于 2025-06-16
L
luadebugcommented
MSVC 2022 x86 would result error & wont build. Issue present since `5df510bb0ac8b23b29e68fb69ad190b3cfae4d441d44a4f2ad5d63e7e0752a22` commit. ``` @programdir\modules\private\action\build\object.lua:100: @programdir\modules\core\tools\cl.lua:741: bvh.cpp src\bvh/v2/index.h(61): error C2398: Element '1': conversion from 'unsigned __int64' to 'size_t' requires a narrowing conversion src\bvh/v2/index.h(61): note: the template instantiation context (the oldest one first) is src\bvh\v2\c_api\bvh.cpp(24): note: see reference to class template instantiation 'bvh::v2::Bvh<bvh::v2::Node<double,2,64,4>>' being compiled src\bvh/v2/bvh.h(18): note: see reference to class template instantiation 'bvh::v2::Node<double,2,64,4>' being compiled src\bvh/v2/node.h(37): note: see reference to class template instantiation 'bvh::v2::Index<64,4>' being compiled src\bvh/v2/index.h(60): note: while compiling class template member function 'void bvh::v2::Index<64,4>::set_prim_count(size_t)' src\bvh/v2/c_api/bvh_impl.h(185): note: see the first reference to 'bvh::v2::Index<64,4>::set_prim_count' in 'bvh::v2::c_api::bvh_node_set_prim_count' ``` One of possible workarounds ```cpp #ifndef BVH_V2_INDEX_H #define BVH_V2_INDEX_H #include "bvh/v2/utils.h" #include <cassert> #include <cstddef> namespace bvh::v2 { /// Packed index data structure. This index can either refer to a range of primitives for a BVH /// leaf, or to the children of a BVH node. In either case, the index corresponds to a contiguous /// range, which means that: /// /// - For leaves, primitives in a BVH node should be accessed via: /// /// size_t begin = index.first_id(); /// size_t end = begin + index.prim_count(); /// for (size_t i = begin; i < end; ++i) { /// size_t prim_id = bvh.prim_ids[i]; /// // ... /// } /// /// Note that for efficiency, reordering the original data to avoid the indirection via /// `bvh.prim_ids` is preferable. /// /// - For inner nodes, children should be accessed via: /// /// auto& left_child = bvh.nodes[index.first_id()]; /// auto& right_child = bvh.nodes[index.first_id() + 1]; /// template <size_t Bits, size_t PrimCountBits> struct Index { using Type = UnsignedIntType<Bits>; static constexpr size_t bits = Bits; static constexpr size_t prim_count_bits = PrimCountBits; static constexpr Type max_prim_count = make_bitmask<Type>(prim_count_bits); static constexpr Type max_first_id = make_bitmask<Type>(bits - prim_count_bits); static_assert(PrimCountBits < Bits); Type value; Index() = default; explicit Index(Type value) : value(value) {} bool operator == (const Index&) const = default; bool operator != (const Index&) const = default; BVH_ALWAYS_INLINE Type first_id() const { return value >> prim_count_bits; } BVH_ALWAYS_INLINE Type prim_count() const { return value & max_prim_count; } BVH_ALWAYS_INLINE bool is_leaf() const { return prim_count() != 0; } BVH_ALWAYS_INLINE bool is_inner() const { return !is_leaf(); } BVH_ALWAYS_INLINE void set_first_id(size_t first_id) { *this = make_leaf(first_id, prim_count()); } BVH_ALWAYS_INLINE void set_prim_count(size_t prim_count) { *this = make_leaf(first_id(), prim_count); } static BVH_ALWAYS_INLINE Index make_leaf(size_t first_prim, size_t prim_count) { assert(prim_count != 0); return Index { (static_cast<Type>(first_prim) << prim_count_bits) | (static_cast<Type>(prim_count) & max_prim_count) }; } static BVH_ALWAYS_INLINE Index make_inner(size_t first_child) { return Index { static_cast<Type>(first_child) << prim_count_bits }; } }; } // namespace bvh::v2 #endif ``` I made PR https://github.com/madmann91/bvh/pull/105
关闭于 2025-06-17 2 条评论