UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BufferedData.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// HEADER_UNIT_SKIP - Internal
6
7namespace Chaos
8{
14 template<typename DataType>
16 {
17 public:
18
20 : SyncCounter(0)
21 , BufferIndex(0)
22 {
23 DataSyncCounts[0] = 0;
24 DataSyncCounts[1] = 0;
25 }
26
30 void Flip()
31 {
32 BufferIndex.Store(GetGameDataIndex());
33 }
34
38 const DataType& GetGameDataForRead() const
39 {
40 return Data[GetGameDataIndex()];
41 }
42
46 const DataType& GetPhysicsDataForRead() const
47 {
48 return Data[GetPhysicsDataIndex()];
49 }
50
55 {
56 return DataSyncCounts[GetGameDataIndex()];
57 }
58
63 {
64 return DataSyncCounts[GetPhysicsDataIndex()];
65 }
66
72 {
73 return Data[GetGameDataIndex()];
74 }
75
81 {
82 int32 DataIndex = GetPhysicsDataIndex();
83 DataSyncCounts[DataIndex] = ++SyncCounter;
84 return Data[DataIndex];
85 }
86
92 DataType& Get(int32 InIndex)
93 {
94 checkSlow(InIndex == 0 || InIndex == 1);
95 return Data[InIndex];
96 }
97
98 private:
99
100 int32 GetPhysicsDataIndex() const
101 {
102 return BufferIndex.Load();
103 }
104
105 int32 GetGameDataIndex() const
106 {
107 return GetPhysicsDataIndex() == 1 ? 0 : 1;
108 }
109
110 // Counter used to identify writes
111 uint32 SyncCounter;
112 // Counter values for each side of the buffer
113 uint32 DataSyncCounts[2];
114
115 // Atomic index for accessing the buffer sides
116 TAtomic<int32> BufferIndex;
117
118 // The actual data type stored
119 DataType Data[2];
120 };
121
131 template<typename ResourceType>
133 {
134 public:
135
136 template<typename... ArgTypes>
138 : ResourceLock()
139 , Buffer{{Args...},{Forward<ArgTypes>(Args)...}}
140 , ReadIndex(0)
141 {}
142
147 void Swap()
148 {
149 ResourceLock.WriteLock();
150
151 ReadIndex = (ReadIndex + 1) % 2;
152
153 ResourceLock.WriteUnlock();
154 }
155
157 const ResourceType& GetRead() const
158 {
159 ResourceLock.ReadLock();
160 return Buffer[ReadIndex];
161 }
162
164 void ReleaseRead() const
165 {
166 ResourceLock.ReadUnlock();
167 }
168
170 ResourceType& GetWritable()
171 {
172 return Buffer[(ReadIndex + 1) % 2];
173 }
175 const ResourceType& GetReadable() const
176 {
177 return Buffer[(ReadIndex + 1) % 2];
178 }
179
180 private:
181
183 mutable FRWLock ResourceLock;
184
186 ResourceType Buffer[2];
187
189 int32 ReadIndex;
190 };
191}
#define checkSlow(expr)
Definition AssertionMacros.h:332
EInPlace
Definition CoreMiscDefines.h:162
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_t uint32
Definition binka_ue_file_header.h:6
Definition BufferedData.h:16
DataType & Get(int32 InIndex)
Definition BufferedData.h:92
TBufferedData()
Definition BufferedData.h:19
int32 GetPhysicsDataSyncCount() const
Definition BufferedData.h:62
DataType & GetGameDataForWrite()
Definition BufferedData.h:71
void Flip()
Definition BufferedData.h:30
const DataType & GetPhysicsDataForRead() const
Definition BufferedData.h:46
int32 GetGameDataSyncCount() const
Definition BufferedData.h:54
DataType & GetPhysicsDataForWrite()
Definition BufferedData.h:80
const DataType & GetGameDataForRead() const
Definition BufferedData.h:38
Definition BufferedData.h:133
void ReleaseRead() const
Definition BufferedData.h:164
const ResourceType & GetReadable() const
Definition BufferedData.h:175
void Swap()
Definition BufferedData.h:147
const ResourceType & GetRead() const
Definition BufferedData.h:157
TChaosReadWriteResource(EInPlace, ArgTypes &&... Args)
Definition BufferedData.h:137
ResourceType & GetWritable()
Definition BufferedData.h:170
Definition Atomic.h:538
Definition CriticalSection.h:14
Definition SkeletalMeshComponent.h:307