UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ChaosVDContextProvider.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "Containers/Array.h"
10
11#if WITH_CHAOS_VISUAL_DEBUGGER
12
14
15enum class EChaosVDContextType : int32
16{
17 Any,
18 Solver,
19 Query,
21};
22
24{
25 None = 0,
26 Resimulated = 1 << 0
27};
29
30
31struct FChaosVDContext
32{
33 int32 OwnerID = INDEX_NONE;
36 int32 Attributes = 0;
37
39 {
41 }
42
43 FORCEINLINE bool IsDataChannelEnabled() const { return CurrentDataChannel->IsChannelEnabled(); }
44
45private:
47};
48
50
51
52class FChaosVDThreadContext : public TThreadSingleton<FChaosVDThreadContext>
53{
54public:
55
56 virtual ~FChaosVDThreadContext() override
57 {
58 if (RecordingStoppedHandle.IsValid())
59 {
60 FChaosVDRuntimeModule::RemoveRecordingStopCallback(RecordingStoppedHandle);
61 }
62 }
63
67 CHAOS_API bool GetCurrentContext(FChaosVDContext& OutContext);
68
74 CHAOS_API const FChaosVDContext* GetCurrentContext(EChaosVDContextType Type);
75
80 CHAOS_API const FChaosVDContext* GetCurrentContext();
81
83 CHAOS_API void PushContext(const FChaosVDContext& InContext);
84
86 CHAOS_API void PopContext();
87
88protected:
90
92 {
93 checkf(BufferAccessCounter == 0, TEXT("The CVD buffer is already in use!"));
94
95 if (!RecordingStoppedHandle.IsValid())
96 {
97 // If we access the buffer, make sure we subscribe to the recording stop callback
98 // So we free any memory we keep allocated
99 RecordingStoppedHandle = FChaosVDRuntimeModule::RegisterRecordingStopCallback(FChaosVDRecordingStateChangedDelegate::FDelegate::CreateLambda([this](){ CVDDataBuffer.Empty(); }));
100 }
101
102 return CVDDataBuffer;
103 }
104
106
109
111};
112
114{
116 {
117 if (!ensure(Context))
118 {
119 return false;
120 }
121
122 return Context->IsDataChannelEnabled();
123 }
124}
125
130{
132 {
133 FChaosVDThreadContext::Get().PushContext(InCVDContext);
134 }
135
137 {
138 FChaosVDThreadContext::Get().PopContext();
139 }
140};
141
143{
145 {
146 if (const FChaosVDContext* CVDContextData = FChaosVDThreadContext::Get().GetCurrentContext())
147 {
149 NewContext.SetDataChannel(NewDataChannel);
150
151 FChaosVDThreadContext::Get().PushContext(MoveTemp(NewContext));
152 }
153 }
154
156 {
157 FChaosVDThreadContext::Get().PopContext();
158 }
159};
160
165{
167 {
168 FChaosVDThreadContext::Get().BufferAccessCounter++;
169
170 // This is close to the max amount of data we can Trace as a single trace event
171 // Some times we will go over and some times this be more than we need
172 // but keeping this amount allocated on average reduces the allocation cost when tracing.
173 // TODO: There might be a better value for this, we need to do more testing
176 }
177
179 {
180 // Clear the buffer on exit but maintain allocated a minimum amount of memory
183
184 FChaosVDThreadContext::Get().BufferAccessCounter--;
185 }
186
188};
189
190#ifndef CVD_GET_CURRENT_CONTEXT
191 #define CVD_GET_CURRENT_CONTEXT(OutContext) \
192 FChaosVDThreadContext::Get().GetCurrentContext(OutContext);
193#endif
194
195#ifndef CVD_GET_WRAPPED_CURRENT_CONTEXT
196 #define CVD_GET_WRAPPED_CURRENT_CONTEXT(OutWrappedContext) \
197 FChaosVDContext CurrentContext; \
198 FChaosVDThreadContext::Get().GetCurrentContext(CurrentContext); \
199 OutWrappedContext = FChaosVDContextWrapper(CurrentContext);
200#endif
201
202#ifndef CVD_SCOPE_CONTEXT
203 #define CVD_SCOPE_CONTEXT(InContext) \
204 FChaosVDScopeContext CVDScope(InContext);
205#endif
206
207#ifndef CVD_SCOPED_DATA_CHANNEL_OVERRIDE
208 #define CVD_SCOPED_DATA_CHANNEL_OVERRIDE(DataChannel) \
209 FChaosCVDScopedDataChannelOverride CVDDC_Scope_Override##DataChannel(DataChannel);
210#endif
211
212#else // WITH_CHAOS_VISUAL_DEBUGGER
213
214#ifndef CVD_GET_CURRENT_CONTEXT
215 #define CVD_GET_CURRENT_CONTEXT(OutContext)
216#endif
217
218#ifndef CVD_SCOPE_CONTEXT
219 #define CVD_SCOPE_CONTEXT(InContext)
220#endif
221
222#ifndef CVD_GET_WRAPPED_CURRENT_CONTEXT
223 #define CVD_GET_WRAPPED_CURRENT_CONTEXT(OutWrappedContext)
224#endif
225
226#ifndef CVD_SCOPED_CONTEXT_DATA_CHANNEL_OVERRIDE
227 #define CVD_SCOPED_CONTEXT_DATA_CHANNEL_OVERRIDE(DataChannel)
228#endif
229
230#endif // WITH_CHAOS_VISUAL_DEBUGGER
231
233{
237
238#if WITH_CHAOS_VISUAL_DEBUGGER
241 {
242 }
243
245#endif
246};
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define ensure( InExpression)
Definition AssertionMacros.h:464
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define TEXT(x)
Definition Platform.h:1272
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
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
#define UE_DECLARE_THREAD_SINGLETON_TLS(Type, Api)
Definition ThreadSingleton.h:35
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition IDelegateInstance.h:14
Definition Array.h:670
Definition SharedPointer.h:153
Definition ThreadSingleton.h:44
Definition ChaosVDDataWrapperUtils.cpp:19
Type
Definition PawnAction_Move.h:11
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
Definition ChaosVDContextProvider.h:233
FChaosVDContextWrapper()
Definition ChaosVDContextProvider.h:234
Definition NumericLimits.h:41