UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ChaosDDFrame.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
6#include "Containers/Map.h"
7#include "HAL/PlatformTLS.h"
9#include "Math/Box.h"
10#include "Math/Sphere.h"
11
12
13#if CHAOS_DEBUG_DRAW
14
15namespace ChaosDD::Private
16{
17 //
18 // A frame is a sequence of debug draw commands.
19 // A command is just a functor that uses a DD Renderer and can be a simple as drawing
20 // a line, or as complex as drawing a set of rigid bodies, constraints, etc.
21 //
22 // E.g., See FChaosDDLine, FChaosDDSphere, FChaosDDParticle
23 //
25
26 //
27 // A single frame of debug draw data
28 //
29 // @todo(chaos): move commands to a per-thread buffer and eliminate write locks
30 //
31 class FChaosDDFrame : public TSharedFromThis<FChaosDDFrame>
32 {
33 public:
36 , FrameIndex(InFrameIndex)
37 , Time(InTime)
38 , Dt(InDt)
41 , CommandCost(0)
42 {
44 {
45 Commands.Reserve(InCommandQueueLength);
46 LatentCommands.Reserve(InCommandQueueLength);
47 }
48 BuildName();
49 }
50
51 virtual ~FChaosDDFrame()
52 {
53 }
54
55 const FString& GetName() const
56 {
57 return Name;
58 }
59
61 {
62 return Timeline.Pin();
63 }
64
65 int64 GetFrameIndex() const
66 {
67 return FrameIndex;
68 }
69
70 double GetTime() const
71 {
72 return Time;
73 }
74
75 double GetDt() const
76 {
77 return Dt;
78 }
79
81 {
83 }
84
85 const FSphere3d& GetDrawRegion() const
86 {
87 return DrawRegion;
88 }
89
90 bool IsInDrawRegion(const FVector& InPos) const
91 {
92 if (DrawRegion.W > 0.0)
93 {
95 }
96 return true;
97 }
98
99 bool IsInDrawRegion(const FSphere3d& InSphere) const
100 {
101 if (DrawRegion.W > 0.0)
102 {
103 return DrawRegion.Intersects(InSphere);
104 }
105 return true;
106 }
107
108 bool IsInDrawRegion(const FBox3d& InBox) const
109 {
110 if (DrawRegion.W > 0.0)
111 {
112 const double BoxDistanceSq = InBox.ComputeSquaredDistanceToPoint(DrawRegion.Center);
114 }
115 return true;
116 }
117
119 {
121 }
122
123 int32 GetCommandBudget() const
124 {
125 return CommandBudget;
126 }
127
128 int32 GetCommandCost() const
129 {
130 return CommandCost;
131 }
132
133 bool WasCommandBudgetExceeded() const
134 {
135 return (CommandCost > CommandBudget) && (CommandBudget > 0);
136 }
137
139 {
141
143
144 // A budget of zero means infinite
145 if ((CommandCost <= CommandBudget) || (CommandBudget == 0))
146 {
147 return true;
148 }
149
150 return false;
151 }
152
153 void EnqueueCommand(FChaosDDCommand&& InCommand)
154 {
156
157 Commands.Emplace(MoveTemp(InCommand));
158 }
159
160 void EnqueueLatentCommand(const Chaos::FLatentDrawCommand& InCommand)
161 {
163
164 LatentCommands.Add(InCommand);
165 }
166
167 int32 GetNumCommands() const
168 {
170
171 return Commands.Num();
172 }
173
175 {
177
178 return LatentCommands.Num();
179 }
180
181 // VisitorType = void(const FChaosDDCommand& Command)
182 template<typename VisitorType>
183 void VisitCommands(const VisitorType& Visitor)
184 {
186
187 for (const FChaosDDCommand& Command : Commands)
188 {
189 Visitor(Command);
190 }
191 }
192
193 // VisitorType = void(const Chaos::FLatentDrawCommand& Command)
194 template<typename VisitorType>
195 void VisitLatentCommands(const VisitorType& Visitor)
196 {
198
199 for (const Chaos::FLatentDrawCommand& Command : LatentCommands)
200 {
201 Visitor(Command);
202 }
203 }
204
205 // Used by the global frame to prevent render while queuing commands
206 virtual void BeginWrite()
207 {
208 }
209
210 // Used by the global frame to prevent render while queuing commands
211 virtual void EndWrite()
212 {
213 }
214
215 // Used by the global frame to extract all debug draw commands so far into a new frame for rendering
217 {
218 const int32 CommandQueueLength = Commands.Max();
219 const int32 LatentCommandQueueLength = LatentCommands.Max();
220
222
223 Commands.Reset(CommandQueueLength);
224 LatentCommands.Reset(LatentCommandQueueLength);
225 CommandCost = 0;
226
228 }
229
230 protected:
231 friend class FChaosDDGlobalFrame;
232
235 , FrameIndex(Other.FrameIndex)
236 , Time(Other.Time)
237 , Dt(Other.Dt)
241 , Commands(MoveTemp(Other.Commands))
242 , LatentCommands(MoveTemp(Other.LatentCommands))
243 {
244 BuildName();
245 }
246
247 // Implemented in ChaosDDTimeline.h
248 void BuildName();
249
251 int64 FrameIndex;
252 double Time;
253 double Dt;
257
258 FString Name;
259
260 // Debug draw commands
262
263 // Legacy debug draw commands (see FDebugDrawQueue)
265
267 };
268
269 //
270 // A special frame used for out-of-frame debug draw. All debug draw commands from a thread that does not
271 // have a context set up will use the global frame. This global frame suffers will be flickery because
272 // the render may occur while enqueueing a set of related debug draw commands.
273 //
274 // @todo(chaos): eventually all threads that want debug draw should have a valid frame and this will be redundant.
275 //
277 {
278 public:
279
282 {
283 }
284
285 virtual void BeginWrite() override
286 {
287 FrameWriteCS.Lock();
288 }
289
290 virtual void EndWrite() override
291 {
292 FrameWriteCS.Unlock();
293 }
294
295 // Create a new frame containing the accumulated commands and reset this frame
296 virtual FChaosDDFramePtr ExtractFrame() override
297 {
299
300 return FChaosDDFrame::ExtractFrame();
301 }
302
303 protected:
304
306 };
307
308 //
309 // Used to write to a debug draw frame.
310 // Currently this writes to the Frame's draw buffer and holds a lock
311 // preventing the frame from being Ended. Eventually this will be a
312 // per-thread buffer to avoid the need for locks.
313 //
315 {
316 public:
318 : Frame(InFrame)
319 {
320 if (Frame.IsValid())
321 {
322 Frame->BeginWrite();
323 }
324 }
325
327 {
328 if (Frame.IsValid())
329 {
330 Frame->EndWrite();
331 }
332 }
333
335 {
336 if (Frame.IsValid())
337 {
338 return Frame->GetDrawRegion();
339 }
340 return FSphere3d(FVector3d(0), 0);
341 }
342
343 bool IsInDrawRegion(const FVector& InPos) const
344 {
345 if (Frame.IsValid())
346 {
347 return Frame->IsInDrawRegion(InPos);
348 }
349 return false;
350 }
351
352 bool IsInDrawRegion(const FSphere3d& InSphere) const
353 {
354 if (Frame.IsValid())
355 {
356 return Frame->IsInDrawRegion(InSphere);
357 }
358 return false;
359 }
360
361 bool IsInDrawRegion(const FBox3d& InBox) const
362 {
363 if (Frame.IsValid())
364 {
365 return Frame->IsInDrawRegion(InBox);
366 }
367 return false;
368 }
369
371 {
372 if (Frame.IsValid())
373 {
374 return Frame->AddToCost(InCost);
375 }
376 return false;
377 }
378
380 {
381 if (Frame.IsValid())
382 {
383 if (Frame->IsInDrawRegion(InBox) && Frame->AddToCost(InCost))
384 {
385 Frame->EnqueueCommand(MoveTemp(InCommand));
386 return true;
387 }
388 }
389 return false;
390 }
391
392 void EnqueueCommand(FChaosDDCommand&& InCommand)
393 {
394 if (Frame.IsValid())
395 {
396 Frame->EnqueueCommand(MoveTemp(InCommand));
397 }
398 }
399
400 void EnqueueLatentCommand(const Chaos::FLatentDrawCommand& InCommand)
401 {
402 if (Frame.IsValid())
403 {
404 Frame->EnqueueLatentCommand(InCommand);
405 }
406 }
407
408 private:
410 };
411}
412
413#endif
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
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
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
UE::Math::TSphere< double > FSphere3d
Definition MathFwd.h:67
UE::Math::TVector< double > FVector3d
Definition MathFwd.h:60
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
Definition ScopeLock.h:141
Definition Array.h:670
Definition AndroidPlatformMisc.h:14
Definition SharedPointer.h:1640
IMAGECORE_API const TCHAR * GetName(Type Format)
Definition ImageCore.cpp:1378
@ Visitor
Definition XmppMultiUserChat.h:94
static constexpr UE_FORCEINLINE_HINT T Square(const T A)
Definition UnrealMathUtility.h:578
bool IsInside(const TSphere< T > &Other, T Tolerance=UE_KINDA_SMALL_NUMBER) const
Definition Sphere.h:158