Skip to content

Commit fbdc596

Browse files
committed
Add nodiscard attributes in various key places (issue #99)
Warning: This change is breaking the API, since the precomputed triangle intersection function now no longer modifies the ray in place. The traversal routine is expected to update the ray `tmax` value in order to achieve good performance.
1 parent d5d4498 commit fbdc596

File tree

12 files changed

+31
-32
lines changed

12 files changed

+31
-32
lines changed

src/bvh/v2/binned_sah_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class BinnedSahBuilder : public TopDownSahBuilder<Node> {
2929
public:
3030
using typename TopDownSahBuilder<Node>::Config;
3131

32-
BVH_ALWAYS_INLINE static Bvh<Node> build(
32+
[[nodiscard]] BVH_ALWAYS_INLINE static Bvh<Node> build(
3333
std::span<const BBox> bboxes,
3434
std::span<const Vec> centers,
3535
const Config& config = {})

src/bvh/v2/bvh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct Bvh {
5454
BVH_ALWAYS_INLINE const Node& get_root() const { return nodes[0]; }
5555

5656
/// Extracts the BVH rooted at the given node index.
57-
inline Bvh extract_bvh(size_t root_id) const;
57+
[[nodiscard]] inline Bvh extract_bvh(size_t root_id) const;
5858

5959
/// Traverses the BVH from the given index in `start` using the provided stack. Every leaf
6060
/// encountered on the way is processed using the given `LeafFn` function, and every pair of
@@ -85,7 +85,7 @@ struct Bvh {
8585
inline void serialize(OutputStream&) const;
8686

8787
template <typename IndexType = typename Index::Type>
88-
static inline Bvh deserialize(InputStream&);
88+
[[nodiscard]] static inline Bvh deserialize(InputStream&);
8989
};
9090

9191
template <typename Node>

src/bvh/v2/default_builder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DefaultBuilder {
3030
};
3131

3232
/// Build a BVH in parallel using the given thread pool.
33-
BVH_ALWAYS_INLINE static Bvh<Node> build(
33+
[[nodiscard]] BVH_ALWAYS_INLINE static Bvh<Node> build(
3434
ThreadPool& thread_pool,
3535
std::span<const BBox> bboxes,
3636
std::span<const Vec> centers,
@@ -46,7 +46,7 @@ class DefaultBuilder {
4646
}
4747

4848
/// Build a BVH in a single-thread.
49-
BVH_ALWAYS_INLINE static Bvh<Node> build(
49+
[[nodiscard]] BVH_ALWAYS_INLINE static Bvh<Node> build(
5050
std::span<const BBox> bboxes,
5151
std::span<const Vec> centers,
5252
const Config& config = {})

src/bvh/v2/executor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ namespace bvh::v2 {
1313
template <typename Derived>
1414
struct Executor {
1515
template <typename Loop>
16-
inline void for_each(size_t begin, size_t end, const Loop& loop) {
16+
BVH_ALWAYS_INLINE void for_each(size_t begin, size_t end, const Loop& loop) {
1717
return static_cast<Derived*>(this)->for_each(begin, end, loop);
1818
}
1919

2020
template <typename T, typename Reduce, typename Join>
21-
inline T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join& join) {
21+
BVH_ALWAYS_INLINE T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join& join) {
2222
return static_cast<Derived*>(this)->reduce(begin, end, init, reduce, join);
2323
}
2424
};
2525

2626
/// Executor that executes serially.
2727
struct SequentialExecutor : Executor<SequentialExecutor> {
2828
template <typename Loop>
29-
void for_each(size_t begin, size_t end, const Loop& loop) {
29+
BVH_ALWAYS_INLINE void for_each(size_t begin, size_t end, const Loop& loop) {
3030
loop(begin, end);
3131
}
3232

3333
template <typename T, typename Reduce, typename Join>
34-
T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join&) {
34+
BVH_ALWAYS_INLINE T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join&) {
3535
T result(init);
3636
reduce(result, begin, end);
3737
return result;
@@ -48,7 +48,7 @@ struct ParallelExecutor : Executor<ParallelExecutor> {
4848
{}
4949

5050
template <typename Loop>
51-
void for_each(size_t begin, size_t end, const Loop& loop) {
51+
BVH_ALWAYS_INLINE void for_each(size_t begin, size_t end, const Loop& loop) {
5252
if (end - begin < parallel_threshold)
5353
return loop(begin, end);
5454

@@ -61,7 +61,7 @@ struct ParallelExecutor : Executor<ParallelExecutor> {
6161
}
6262

6363
template <typename T, typename Reduce, typename Join>
64-
T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join& join) {
64+
BVH_ALWAYS_INLINE T reduce(size_t begin, size_t end, const T& init, const Reduce& reduce, const Join& join) {
6565
if (end - begin < parallel_threshold) {
6666
T result(init);
6767
reduce(result, begin, end);

src/bvh/v2/mini_tree_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class MiniTreeBuilder {
4444

4545
/// Starts building a BVH with the given primitive data. The build algorithm is multi-threaded,
4646
/// and runs on the given thread pool.
47-
BVH_ALWAYS_INLINE static Bvh<Node> build(
47+
[[nodiscard]] BVH_ALWAYS_INLINE static Bvh<Node> build(
4848
ThreadPool& thread_pool,
4949
std::span<const BBox> bboxes,
5050
std::span<const Vec> centers,

src/bvh/v2/node.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct Node {
6565
}
6666

6767
/// Robust ray-node intersection routine. See "Robust BVH Ray Traversal", by T. Ize.
68-
BVH_ALWAYS_INLINE std::pair<T, T> intersect_robust(
68+
[[nodiscard]] BVH_ALWAYS_INLINE std::pair<T, T> intersect_robust(
6969
const Ray<T, Dim>& ray,
7070
const Vec<T, Dim>& inv_dir,
7171
const Vec<T, Dim>& inv_dir_pad,
@@ -76,7 +76,7 @@ struct Node {
7676
return make_intersection_result(ray, tmin, tmax);
7777
}
7878

79-
BVH_ALWAYS_INLINE std::pair<T, T> intersect_fast(
79+
[[nodiscard]] BVH_ALWAYS_INLINE std::pair<T, T> intersect_fast(
8080
const Ray<T, Dim>& ray,
8181
const Vec<T, Dim>& inv_dir,
8282
const Vec<T, Dim>& inv_org,
@@ -93,7 +93,7 @@ struct Node {
9393
stream.write(index.value);
9494
}
9595

96-
static BVH_ALWAYS_INLINE Node deserialize(InputStream& stream) {
96+
[[nodiscard]] static BVH_ALWAYS_INLINE Node deserialize(InputStream& stream) {
9797
Node node;
9898
for (auto& bound : node.bounds)
9999
bound = stream.read<T>();

src/bvh/v2/sphere.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct Sphere {
2929
/// Intersects a ray with the sphere. If the ray is normalized, a dot product can be saved by
3030
/// setting `AssumeNormalized` to true.
3131
template <bool AssumeNormalized = false>
32-
BVH_ALWAYS_INLINE std::optional<std::pair<T, T>> intersect(const Ray<T, N>& ray) const {
32+
[[nodiscard]] BVH_ALWAYS_INLINE std::optional<std::pair<T, T>> intersect(const Ray<T, N>& ray) const {
3333
auto oc = ray.org - center;
3434
auto a = AssumeNormalized ? static_cast<T>(1.) : dot(ray.dir, ray.dir);
3535
auto b = static_cast<T>(2.) * dot(ray.dir, oc);

src/bvh/v2/sweep_sah_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SweepSahBuilder : public TopDownSahBuilder<Node> {
2727
public:
2828
using typename TopDownSahBuilder<Node>::Config;
2929

30-
BVH_ALWAYS_INLINE static Bvh<Node> build(
30+
[[nodiscard]] BVH_ALWAYS_INLINE static Bvh<Node> build(
3131
std::span<const BBox> bboxes,
3232
std::span<const Vec> centers,
3333
const Config& config = {})

src/bvh/v2/tri.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "bvh/v2/bbox.h"
77

88
#include <limits>
9-
#include <utility>
9+
#include <tuple>
1010
#include <optional>
1111

1212
namespace bvh::v2 {
@@ -44,17 +44,16 @@ struct PrecomputedTri {
4444
BVH_ALWAYS_INLINE BBox<T, 3> get_bbox() const { return convert_to_tri().get_bbox(); }
4545
BVH_ALWAYS_INLINE Vec<T, 3> get_center() const { return convert_to_tri().get_center(); }
4646

47-
/// Returns a pair containing the barycentric coordinates of the hit point if the given ray
48-
/// intersects the triangle, otherwise returns nothing. The distance at which the ray intersects
49-
/// the triangle is set in `ray.tmax`. The tolerance can be adjusted to account for numerical
50-
/// precision issues.
51-
BVH_ALWAYS_INLINE std::optional<std::pair<T, T>> intersect(
52-
Ray<T, 3>& ray,
47+
/// Returns a tuple containing the distance at which the ray intersects the triangle, and the
48+
/// barycentric coordinates of the hit point if the given ray intersects the triangle, otherwise
49+
/// returns nothing. The tolerance can be adjusted to account for numerical precision issues.
50+
[[nodiscard]] BVH_ALWAYS_INLINE std::optional<std::tuple<T, T, T>> intersect(
51+
const Ray<T, 3>& ray,
5352
T tolerance = -std::numeric_limits<T>::epsilon()) const;
5453
};
5554

5655
template <typename T>
57-
std::optional<std::pair<T, T>> PrecomputedTri<T>::intersect(Ray<T, 3>& ray, T tolerance) const {
56+
std::optional<std::tuple<T, T, T>> PrecomputedTri<T>::intersect(const Ray<T, 3>& ray, T tolerance) const {
5857
auto c = p0 - ray.org;
5958
auto r = cross(ray.dir, c);
6059
auto inv_det = static_cast<T>(1.) / dot(n, ray.dir);
@@ -67,10 +66,8 @@ std::optional<std::pair<T, T>> PrecomputedTri<T>::intersect(Ray<T, 3>& ray, T to
6766
// when one of t, u, or v is a NaN
6867
if (u >= tolerance && v >= tolerance && w >= tolerance) {
6968
auto t = dot(n, c) * inv_det;
70-
if (t >= ray.tmin && t <= ray.tmax) {
71-
ray.tmax = t;
72-
return std::make_optional(std::pair<T, T> { u, v });
73-
}
69+
if (t >= ray.tmin && t <= ray.tmax)
70+
return std::make_optional(std::make_tuple(t, u, v));
7471
}
7572

7673
return std::nullopt;

src/bvh/v2/vec.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ BVH_ALWAYS_INLINE T length(const Vec<T, N>& v) {
123123
}
124124

125125
template <typename T, size_t N>
126-
BVH_ALWAYS_INLINE Vec<T, N> normalize(const Vec<T, N>& v) {
126+
[[nodiscard]] BVH_ALWAYS_INLINE Vec<T, N> normalize(const Vec<T, N>& v) {
127127
return v * (static_cast<T>(1.) / length(v));
128128
}
129129

0 commit comments

Comments
 (0)