Skip to content

Commit

Permalink
Fix issue #89
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
madmann91 committed Jul 8, 2024
1 parent 77a08ca commit ce037a7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/bvh/v2/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type>(prim_count_bits);
static constexpr Type max_first_id = make_bitmask<Type>(bits - prim_count_bits);

static_assert(PrimCountBits < Bits);

Expand All @@ -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<Type>(first_id) << prim_count_bits) |
(static_cast<Type>(prim_count) & max_prim_count))
{
assert(first_id <= make_bitmask<Type>(Bits - PrimCountBits));
assert(prim_count <= max_prim_count);
assert(first_id <= static_cast<size_t>(max_first_id));
assert(prim_count <= static_cast<size_t>(max_prim_count));
}
};

Expand Down

0 comments on commit ce037a7

Please sign in to comment.