UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PBDActiveView.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Containers/Array.h"
5#include "Algo/BinarySearch.h"
7#include "Chaos/Vector.h"
8
9namespace Chaos
10{
11 class FActiveViewRange final
12 {
13 public:
15 : bIsActive(0)
16 , Offset(0)
17 {}
18
19 explicit FActiveViewRange(int32 InOffset, bool bActive = false)
20 : bIsActive(bActive ? 1 : 0)
21 , Offset((uint32)InOffset)
22 {}
23
24 bool IsActive() const
25 {
26 return !!bIsActive && Offset > 0;
27 }
28
30 {
31 return (int32)Offset;
32 }
33
34 void SetActive(bool bActivate)
35 {
36 bIsActive = bActivate;
37 }
38
39 private:
40 uint32 bIsActive : 1;
41 uint32 Offset : 31;
42 };
43
44 // Index based view, specialized for working with several ranges within a same array such as particles.
45 template<typename TItemsType>
47 {
48 public:
52
53 // Return all items, including those not in the view.
54 TItemsType& GetItems() const { return Items; }
55
56 // Add a new active (or inactive) range at the end of the list, and return its offset.
57 int32 AddRange(int32 NumItems, bool bActivate = true);
58
59 // Return the number of items in the range starting at the specified offset.
61
62 // Activate (or deactivate) the range starting at the specified offset.
63 void ActivateRange(int32 Offset, bool bActivate);
64
65 // Execute the specified function on all active items.
67
68 // Execute the specified function in parallel, on all items for each active range (sequential range, parallel items). Set MinParallelSize to run sequential on the smaller ranges.
70
71 // Execute the specified function in nested parallel for loops, on all items for each active range (parallel range, parallel items). Set MinParallelSize to run sequential on the smaller ranges.
73
74 // Execute the specified function in sequence for all active range. Callee responsible for inner loop.
76
77 // Execute the specified function in parallel for all active ranges. Callee responsible for inner loop.
79
80 // Remove all ranges above the current given size.
81 void Reset(int32 Offset = 0);
82
83 // Return whether there is any active range in the view.
84 bool HasActiveRange() const;
85
86 // Return the total number of active items from all active ranges.
88
89 // Return a list of pair (offset, range) of all active ranges.
91
92 // Return internal ranges.
94 UE_DEPRECATED(5.6, "This method has been deprecated as the underlying type of Ranges has changed. Use GetAllRanges instead.")
96
97 int32 GetNumRanges() const { return Ranges.Num(); }
98
99 private:
100
101 int32 GetRangeIndex(int32 Offset) const
102 {
103 // Binary search upper bound range of this offset
104 const int32 Index = Algo::UpperBound(Ranges, FActiveViewRange(Offset), [](const FActiveViewRange A, const FActiveViewRange B) { return A.GetOffset() < B.GetOffset(); });
105 check(Ranges.IsValidIndex(Index));
106 return Index;
107 }
108
109 TItemsType& Items;
111 };
112
113 template <class TItemsType>
115 {
116 const int32 Offset = Ranges.Num() ? Ranges.Last().GetOffset() : 0;
117 if (NumItems)
118 {
119 const int32 Size = Offset + NumItems;
120 Ranges.Emplace(Size, bActivate);
121 }
122 return Offset;
123 }
124
125 template <class TItemsType>
127 {
128 const int32 Index = GetRangeIndex(Offset);
129 // Return size regardless or activation state
130 return Ranges[Index].GetOffset() - Offset;
131 }
132
133 template <class TItemsType>
135 {
136 const int32 Index = GetRangeIndex(Offset);
137 Ranges[Index].SetActive(bActivate);
138 }
139
140 template <class TItemsType>
142 {
143 int32 Offset = 0;
144 for (const FActiveViewRange Range : Ranges)
145 {
146 if (Range.IsActive())
147 {
148 // Active range
149 for (int32 Index = Offset; Index < Range.GetOffset(); ++Index)
150 {
151 Function(Items, Index);
152 }
153 }
154 Offset = Range.GetOffset();
155 }
156 }
157
158 template <class TItemsType>
160 {
161 int32 Offset = 0;
162 for (const FActiveViewRange Range : Ranges)
163 {
164 if (Range.IsActive())
165 {
166 // Active range
167 const int32 RangeSize = Range.GetOffset() - Offset;
168 PhysicsParallelFor(RangeSize, [this, Offset, &Function](int32 Index)
169 {
170 Function(Items, Offset + Index);
171 }, /*bForceSingleThreaded =*/ RangeSize < MinParallelBatchSize);
172 }
173 Offset = Range.GetOffset();
174 }
175 }
176
177 template <class TItemsType>
179 {
181
183 {
184 const int32 Offset = ActiveRanges[RangeIndex][0];
185 const int32 RangeSize = ActiveRanges[RangeIndex][1] - Offset;
186 PhysicsParallelFor(RangeSize, [this, Offset, &Function](int32 Index)
187 {
188 Function(Items, Offset + Index);
189 }, /*bForceSingleThreaded =*/ RangeSize < MinParallelBatchSize);
191 }
192
193 template <class TItemsType>
195 {
196 int32 Offset = 0;
197 for (const FActiveViewRange Range : Ranges)
198 {
199 if (Range.IsActive())
200 {
201 // Active Range
202 Function(Items, Offset, Range.GetOffset());
203 }
204 Offset = Range.GetOffset();
205 }
206 }
207
208 template <class TItemsType>
210 {
212
214 {
215 const TVector<int32, 2>& ActiveRange = ActiveRanges[RangeIndex];
216 const int32 Offset = ActiveRange[0];
217 const int32 Range = ActiveRange[1];
218 Function(Items, Offset, Range);
220 }
221
222 template <class TItemsType>
224 {
225 for (int32 Index = 0; Index < Ranges.Num(); ++Index)
226 {
227 if (Ranges[Index].GetOffset() > Offset)
228 {
229 Ranges.SetNum(Index);
230 break;
231 }
232 }
233 }
234
235 template <class TItemsType>
237 {
238 for (const FActiveViewRange Range : Ranges)
239 {
240 if (Range.IsActive())
241 {
242 return true;
243 }
244 }
245 return false;
246 }
247
248 template <class TItemsType>
250 {
251 int32 ActiveSize = 0;
252 int32 Offset = 0;
253 for (const FActiveViewRange Range : Ranges)
254 {
255 if (Range.IsActive())
256 {
257 ActiveSize += Range.GetOffset() - Offset;
258 }
259 Offset = Range.GetOffset();
260 }
261 return ActiveSize;
262 }
263
264 template <class TItemsType>
266 {
268
269 int32 Offset = 0;
270 for (const FActiveViewRange Range : Ranges)
271 {
272 if (Range.IsActive())
273 {
274 ActiveRanges.Add(TVector<int32, 2>(Offset, Range.GetOffset()));
275 }
276 Offset = Range.GetOffset();
277 }
278 return ActiveRanges;
279 }
280}
#define check(expr)
Definition AssertionMacros.h:314
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32 Offset
Definition VulkanMemory.cpp:4033
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition PBDActiveView.h:12
void SetActive(bool bActivate)
Definition PBDActiveView.h:34
FActiveViewRange(int32 InOffset, bool bActive=false)
Definition PBDActiveView.h:19
FActiveViewRange()
Definition PBDActiveView.h:14
int32 GetOffset() const
Definition PBDActiveView.h:29
bool IsActive() const
Definition PBDActiveView.h:24
Definition PBDActiveView.h:47
TConstArrayView< FActiveViewRange > GetAllRanges() const
Definition PBDActiveView.h:93
int32 AddRange(int32 NumItems, bool bActivate=true)
Definition PBDActiveView.h:114
void Reset(int32 Offset=0)
Definition PBDActiveView.h:223
int32 GetActiveSize() const
Definition PBDActiveView.h:249
void ParallelFor(TFunctionRef< void(TItemsType &, int32)> Function, bool bForceSingleThreadedRange, int32 MinParallelSize=TNumericLimits< int32 >::Max()) const
Definition PBDActiveView.h:178
void RangeFor(TFunctionRef< void(TItemsType &, int32, int32)> Function) const
Definition PBDActiveView.h:194
TArray< TVector< int32, 2 >, TInlineAllocator< 8 > > GetActiveRanges() const
Definition PBDActiveView.h:265
int32 GetNumRanges() const
Definition PBDActiveView.h:97
void ParallelFor(TFunctionRef< void(TItemsType &, int32)> Function, int32 MinParallelSize=TNumericLimits< int32 >::Max()) const
Definition PBDActiveView.h:159
TItemsType & GetItems() const
Definition PBDActiveView.h:54
int32 GetRangeSize(int32 Offset) const
Definition PBDActiveView.h:126
TPBDActiveView(TItemsType &InItems)
Definition PBDActiveView.h:49
bool HasActiveRange() const
Definition PBDActiveView.h:236
void RangeFor(TFunctionRef< void(TItemsType &, int32, int32)> Function, bool bForceSingleThreadedRange) const
Definition PBDActiveView.h:209
void SequentialFor(TFunctionRef< void(TItemsType &, int32)> Function) const
Definition PBDActiveView.h:141
void ActivateRange(int32 Offset, bool bActivate)
Definition PBDActiveView.h:134
TConstArrayView< int32 > GetRanges() const
Definition PBDActiveView.h:95
Definition Vector.h:41
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition AssetRegistryState.h:50
Definition ContainerAllocationPolicies.h:894
UE_REWRITE auto UpperBound(const RangeType &Range, const ValueType &Value, SortPredicateType SortPredicate) -> decltype(GetNum(Range))
Definition BinarySearch.h:133
Definition SkeletalMeshComponent.h:307
void CHAOS_API PhysicsParallelFor(int32 InNum, TFunctionRef< void(int32)> InCallable, bool bForceSingleThreaded=false)
Definition Parallel.cpp:55
U16 Index
Definition radfft.cpp:71
Definition NumericLimits.h:41