UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ChaosDDContext.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
8#include "Misc/ScopeLock.h"
9
10#if CHAOS_DEBUG_DRAW
11
12namespace ChaosDD::Private
13{
14 class FChaosDDContext;
15}
16
17UE_DECLARE_THREAD_SINGLETON_TLS(ChaosDD::Private::FChaosDDContext, CHAOS_API)
18
19namespace ChaosDD::Private
20{
21 //
22 // A thread-local debug draw context used to access the queue to draw to
23 // for any thread on which debug draw has been set up.
24 //
25 class FChaosDDContext : public TThreadSingleton<FChaosDDContext>
26 {
27 public:
29
30 // Whether the debug draw system is enabled
31 static bool IsDebugDrawEnabled()
32 {
33 return bDebugDrawEnabled;
34 }
35
36 // Enable/Disable the debug draw system
37 static void SetIsDebugDrawEnabled(bool bInEnabled)
38 {
40 }
41
42 // Write access to the debug draw frame for the current thread
43 static FChaosDDFrameWriter GetWriter()
44 {
46 {
47 return FChaosDDFrameWriter(Get().GetFrame());
48 }
49 return FChaosDDFrameWriter({});
50 }
51
52 // For internal use - collect all out-of-frame debug draw commands for rendering
54
55 private:
56 friend class FChaosDDScene;
57 friend class FChaosDDTaskContext;
58 friend class FChaosDDTaskParentContext;
59 friend class FChaosDDTimelineContext;
60
61 const FChaosDDFramePtr& GetFrame() const
62 {
63 // The frame we should be drawing into on this thread
64 if (!Frame.IsValid())
65 {
66 // If there is no Context set up on this thread we fall back to a global frame that
67 // is tied to the game thread. If debug draw commands are queued while the game thread
68 // is rendering the DDScene, the commands will be split across frames resulting in flicker
69 // @todo(chaos): ideally we don't have a global frame - try to get rid of it
70 return GetGlobalFrame();
71 }
72
73 return Frame;
74 }
75
76 // Global frame management
78 static void CreateGlobalFrame();
79 static void SetGlobalDrawRegion(const FSphere3d& InDrawRegion);
81
82 // The frame to draw to on this thread (or null)
84
85 // Whether the system is enabled
86 static CHAOS_API bool bDebugDrawEnabled;
87
88 // Global frame: fallback for out-of-context debug draw
92 };
93
94 //
95 // Initializes the FChaosDDContext for a thread that owns a timeline
96 //
97 // This starts a new frame (debug draw buffer) and sets up the FChaosDDContext for this thread.
98 // The active context should be accessed via FChaosDDContext::GetWriter(). FChaosDDTimelineContext
99 // is not directly used other than to instantiate.
100 //
102 {
103 public:
104 CHAOS_API void BeginFrame(const FChaosDDTimelinePtr& InTimeline, double InTime, double InDt);
105 CHAOS_API void EndFrame();
106
107 private:
109 FChaosDDFramePtr PreviousFrame;
110 bool bInContext = false;
111 };
112
113 //
114 // A scoped wrapper for FChaosDDTimelineContext
115 //
117 {
118 public:
121
122 private:
124 };
125
126 //
127 // Used to propagate a debug draw context to a child thread.
128 // To use:
129 // - put a FChaosDDTaskParentContext on the stack on the parent thread
130 // - pass the FChaosDDTaskParentContext to the child thread
131 // - put FChaosDDScopeTaskContext(ParentContext) on the child thread
132 // (Search for FChaosDDScopeTaskContext for examples.)
133 //
135 {
136 public:
138 private:
139 friend class FChaosDDTaskContext;
141 };
142
143 //
144 // Initializes the FChaosDDContext for a task thread.
145 // Assumes that the task is kicked off from a thread that has an active debug draw context,
146 // which should be passed into this context. Any debug draws from the task will go to the
147 // same frame as the parent context.
148 //
149 // The active context is accessed via FChaosDDContext::GetWriter() (and not this object).
150 //
151 // NOTE: This is only intended to be used for tasks which will be awaited before the end
152 // of the frame (truly asynchronous tasks would need their own timeline, or just set up
153 // a context that writes to the global frame)
154 //
155 // @todo(chaos): ChaosDDFrame should track how many contexts it is referenced by and
156 // assert that it is not active when we end the frame.
157 //
159 {
160 public:
162 CHAOS_API void EndThread();
163
164 private:
165 FChaosDDFramePtr PreviousFrame;
166 bool bInContext = false;
167 };
168
169 //
170 // A scoped wrapper for FChaosDDTaskContext
171 //
173 {
174 public:
177
178 private:
180 };
181}
182
183#endif
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
#define UE_DECLARE_THREAD_SINGLETON_TLS(Type, Api)
Definition ThreadSingleton.h:35
Definition ThreadSingleton.h:44
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426