From ce037a75d20ce23e404103bcd64f8fef646aac04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ars=C3=A8ne=20P=C3=A9rard-Gayot?= Date: Mon, 8 Jul 2024 21:58:06 +0200 Subject: [PATCH] Fix issue #89 This simply makes all `Index` setters take `size_t` instead of `Index::Type`. The added benefit is that the cast is done once inside the `Index` class, instead of being done everywhere the setter is called. Thus, the check that the primitive count and first index value are representable also happens once in the same place. --- src/bvh/v2/index.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/bvh/v2/index.h b/src/bvh/v2/index.h index beec4ae..7120031 100644 --- a/src/bvh/v2/index.h +++ b/src/bvh/v2/index.h @@ -36,6 +36,7 @@ struct Index { static constexpr size_t bits = Bits; static constexpr size_t prim_count_bits = PrimCountBits; static constexpr Type max_prim_count = make_bitmask(prim_count_bits); + static constexpr Type max_first_id = make_bitmask(bits - prim_count_bits); static_assert(PrimCountBits < Bits); @@ -52,29 +53,31 @@ struct Index { 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(Type first_id) { + BVH_ALWAYS_INLINE void set_first_id(size_t first_id) { *this = Index { first_id, prim_count() }; } - BVH_ALWAYS_INLINE void set_prim_count(Type prim_count) { + BVH_ALWAYS_INLINE void set_prim_count(size_t prim_count) { *this = Index { first_id(), prim_count }; } - static BVH_ALWAYS_INLINE Index make_leaf(Type first_prim, Type prim_count) { + static BVH_ALWAYS_INLINE Index make_leaf(size_t first_prim, size_t prim_count) { assert(prim_count != 0); return Index { first_prim, prim_count }; } - static BVH_ALWAYS_INLINE Index make_inner(Type first_child) { + static BVH_ALWAYS_INLINE Index make_inner(size_t first_child) { return Index { first_child, 0 }; } private: - explicit Index(Type first_id, Type prim_count) - : value((first_id << prim_count_bits) | (prim_count & max_prim_count)) + explicit Index(size_t first_id, size_t prim_count) + : Index( + (static_cast(first_id) << prim_count_bits) | + (static_cast(prim_count) & max_prim_count)) { - assert(first_id <= make_bitmask(Bits - PrimCountBits)); - assert(prim_count <= max_prim_count); + assert(first_id <= static_cast(max_first_id)); + assert(prim_count <= static_cast(max_prim_count)); } };