Blender V5.0
BLI_task_size_hints.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <optional>
12
13#include "BLI_index_range.hh"
14#include "BLI_span.hh"
15
16namespace blender::threading {
17
25 public:
37
39
40 protected:
42};
43
44namespace detail {
45
52
54 public:
55 std::optional<int64_t> full_size;
56
61
63 virtual void lookup_individual_sizes(IndexRange /*range*/,
64 MutableSpan<int64_t> r_sizes) const = 0;
65};
66
74
75template<typename Fn>
77 private:
78 Fn fn_;
79
80 public:
81 TaskSizeHints_IndividualLookupFn(Fn fn, const std::optional<int64_t> full_size)
83 {
84 }
85
86 void lookup_individual_sizes(const IndexRange range, MutableSpan<int64_t> r_sizes) const override
87 {
88 fn_(range, r_sizes);
89 }
90};
91
92template<typename Fn>
94 private:
95 Fn fn_;
96
97 public:
101
102 int64_t lookup_accumulated_size(const IndexRange range) const override
103 {
104 return fn_(range);
105 }
106};
107
108} // namespace detail
109
110inline bool use_single_thread(const TaskSizeHints &size_hints,
111 const IndexRange range,
112 const int64_t threshold)
113{
114#ifdef __GNUC__ /* False positive warning with GCC. */
115# pragma GCC diagnostic push
116# pragma GCC diagnostic ignored "-Warray-bounds"
117#endif
118 switch (size_hints.type) {
120 const int64_t size = static_cast<const detail::TaskSizeHints_Static &>(size_hints).size;
121 return size * range.size() <= threshold;
122 }
124 const std::optional<int64_t> &full_size =
125 static_cast<const detail::TaskSizeHints_IndividualLookup &>(size_hints).full_size;
126 if (full_size.has_value()) {
127 if (*full_size <= threshold) {
128 return true;
129 }
130 }
131 return false;
132 }
134 const int64_t accumulated_size =
135 static_cast<const detail::TaskSizeHints_AccumulatedLookup &>(size_hints)
136 .lookup_accumulated_size(range);
137 return accumulated_size <= threshold;
138 }
139 }
140#ifdef __GNUC__
141# pragma GCC diagnostic pop
142#endif
144 return true;
145}
146
156template<typename Fn>
157inline auto individual_task_sizes(Fn &&fn, const std::optional<int64_t> full_size = std::nullopt)
158{
159 auto array_fn = [fn = std::forward<Fn>(fn)](const IndexRange range,
160 MutableSpan<int64_t> r_sizes) {
161 for (const int64_t i : range.index_range()) {
162 r_sizes[i] = fn(range[i]);
163 }
164 };
166 full_size);
167}
168
177template<typename Fn> inline auto accumulated_task_sizes(Fn &&fn)
178{
180}
181
182} // namespace blender::threading
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
long long int int64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
constexpr int64_t size() const
int64_t lookup_accumulated_size(const IndexRange range) const override
virtual int64_t lookup_accumulated_size(IndexRange range) const =0
TaskSizeHints_IndividualLookupFn(Fn fn, const std::optional< int64_t > full_size)
void lookup_individual_sizes(const IndexRange range, MutableSpan< int64_t > r_sizes) const override
TaskSizeHints_IndividualLookup(std::optional< int64_t > full_size)
virtual void lookup_individual_sizes(IndexRange, MutableSpan< int64_t > r_sizes) const =0
auto individual_task_sizes(Fn &&fn, const std::optional< int64_t > full_size=std::nullopt)
auto accumulated_task_sizes(Fn &&fn)
bool use_single_thread(const TaskSizeHints &size_hints, const IndexRange range, const int64_t threshold)
i
Definition text_draw.cc:230