UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Allocator2D.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
7#include "CoreMinimal.h"
8#include "HAL/Platform.h"
10#include "MeshUtilitiesCommon.h" // For ELightmapUVVersion
11
12struct FMD5Hash;
13struct FRect;
14struct Rect;
15template <typename FuncType> class TFunctionRef;
16
17#define DEBUG_LAYOUT_STATS 0
18
20{
21public:
22 enum class EMode
23 {
24 // In this mode, segments represents free space
25 // Used for the layout merging usedsegments
27 // In this mode, segments represents used space
28 // Used for the rasterization of charts.
30 };
31
32 struct FRect
33 {
38 };
39
40 struct FSegment
41 {
44
45 bool operator<( const FSegment& Other ) const { return StartPos < Other.StartPos; }
46 };
47
48 struct FRun
49 {
52
53 // Contains mapping from pixel position to first segment index in search range.
54 // Only computed when we're in FreeSegments mode to help TestOneRun find
55 // the proper segment in O(c) at the expense of a likely cache miss.
56 // We'll use a threshold to use this method when the number of iterations
57 // saved is worth the cache miss.
58 // Obviously, using a uint16 here will reduce cache misses but impose
59 // an hopefully enough limitation of texture size 65536x65536 (4GB).
61 };
62
63public:
65
66 // Must clear before using
68
70 bool Test( FRect Rect );
72
75 bool Test( FRect Rect, const FAllocator2D& Other );
77
78 uint64 GetBit( uint32 x, uint32 y ) const;
79 void SetBit( uint32 x, uint32 y );
80 void ClearBit( uint32 x, uint32 y );
81
83 MESHUTILITIESCOMMON_API void MergeRun( FRun& Run, const FRun& OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution /* Resolution along the axis the run belongs to */, uint32 PerpendicularResolution );
85
88
90
92
93 // Take control of the copy assignment to reduce the amount of data movement to the strict minimum
94 FAllocator2D(const FAllocator2D& Other) = default;
96
97 // Allow to visualize the content in ascii for debugging purpose. (i.e Watch or Immediate window).
98 MESHUTILITIESCOMMON_API FString ToString() const;
99
100 // Get the MD5 hash of the rasterized content
102
105
108
109protected:
115 MESHUTILITIESCOMMON_API void AddUsedSegment( FRun& Run, uint32 StartPos, uint32 Length );
116
117 // Enforce that those cannot be changed in flight
118 const EMode Mode;
123
126 TArray< FRun > Rows; // Represent rows in the grid
127 TArray< FRun > Columns; // Represent columns in the grid (used when version >= Segments2D).
129
130 // Index inside rows that will be sorted by rows with longest used segment first
132 // Index inside columns that will be sorted by columns with longest used segment first
134
135private:
136 // Store iteration stats of the principal algorithms.
137 struct FStats
138 {
139 struct FStat
140 {
141#if DEBUG_LAYOUT_STATS
142 inline void operator++(int) { Value++; }
143 inline void operator+=(uint64 InValue) { Value += InValue; }
144 inline uint64 GetValue() const { return Value; }
145 private:
146 uint64 Value = 0;
147#else
148 // These will be optimized away when stats are not activated
149 inline void operator++(int) { }
150 inline void operator+=(uint64 InValue) { }
151 inline uint64 GetValue() const { return 0; }
152#endif
153 };
154
155 FStat FindWithSegmentsIterationsY;
156 FStat FindWithSegmentsIterationsX;
157 FStat FindWithSegmentsMovedPastPreviousBest;
158 FStat TestAllRowsIterationsY;
159 FStat FreeSegmentLookupCount;
160 FStat FreeSegmentRangeIterations;
161 FStat FreeSegmentFutureIterations;
162 FStat FreeSegmentFutureHit;
163 FStat FreeSegmentFutureHitStep;
164 FStat FreeSegmentFutureMiss;
165 FStat FreeSegmentFutureMissStep;
166
167 void Reset()
168 {
169 FPlatformMemory::Memzero(this, sizeof(FStats));
170 }
171 };
172
173 mutable FStats Stats;
174};
175
176// Returns non-zero if set
178{
179 return Bits[ (x >> 6) + y * Pitch ] & ( 1ull << ( x & 63 ) );
180}
181
183{
184 Bits[ (x >> 6) + y * Pitch ] |= ( 1ull << ( x & 63 ) );
185
186 // Keep track of the rasterized dimension to optimize operations on that area only
187 if (y >= RasterHeight)
188 {
189 RasterHeight = y + 1;
190 }
191
192 if (x >= RasterWidth)
193 {
194 RasterWidth = x + 1;
195 }
196}
197
199{
200 Bits[ (x >> 6) + y * Pitch ] &= ~( 1ull << ( x & 63 ) );
201}
202
204{
205 for( uint32 y = Rect.Y; y < Rect.Y + Rect.H; y++ )
206 {
207 for( uint32 x = Rect.X; x < Rect.X + Rect.W; x++ )
208 {
209 if( GetBit( x, y ) )
210 {
211 return false;
212 }
213 }
214 }
215
216 return true;
217}
218
220{
221 const uint32 LowShift = Rect.X & 63;
222 const uint32 HighShift = 64 - LowShift;
223
224 for( uint32 y = 0; y < Rect.H; y++ )
225 {
226#if 1
227 uint32 ThisIndex = (Rect.X >> 6) + (y + Rect.Y) * Pitch;
228 uint32 OtherIndex = y * Pitch;
229
230 // Test a uint64 at a time
231 for( uint32 x = 0; x < Rect.W; x += 64 )
232 {
233 // no need to zero out HighInt on wrap around because Other will always be zero outside Rect.
234 uint64 LowInt = Bits[ ThisIndex ];
235 uint64 HighInt = Bits[ ThisIndex + 1 ];
236
239
240 if( ThisInt & OtherInt )
241 {
242 return false;
243 }
244
245 ThisIndex++;
246 OtherIndex++;
247 }
248#else
249 for( uint32 x = 0; x < Rect.W; x++ )
250 {
251 if( Other.GetBit( x, y ) && GetBit( x + Rect.X, y + Rect.Y ) )
252 {
253 return false;
254 }
255 }
256#endif
257 }
258
259 return true;
260}
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
ELightmapUVVersion
Definition MeshUtilitiesCommon.h:8
UE::Stats::FStats FStats
Definition StatsSystem.h:56
FStringBuilderBase & operator+=(FStringBuilderBase &Builder, ANSICHAR Char)
Definition StringBuilder.h:582
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Allocator2D.h:20
MESHUTILITIESCOMMON_API FMD5Hash GetRasterMD5() const
Definition Allocator2D.cpp:103
TArray< uint16 > SortedRowsIndex
Definition Allocator2D.h:131
MESHUTILITIESCOMMON_API bool TestAllColumns(const FRect &Rect, const FAllocator2D &Other, uint32 &FailedLength) const
Definition Allocator2D.cpp:353
MESHUTILITIESCOMMON_API bool FindWithSegments(FRect &Rect, const FAllocator2D &Other, TFunctionRef< bool(const FAllocator2D::FRect &)> IsBestRect) const
Definition Allocator2D.cpp:214
uint32 GetRasterHeight() const
Definition Allocator2D.h:104
MESHUTILITIESCOMMON_API uint32 GetUsedTexels() const
Definition Allocator2D.cpp:286
MESHUTILITIESCOMMON_API void FlipX(const FRect &Rect)
Definition Allocator2D.cpp:458
MESHUTILITIESCOMMON_API void CreateUsedSegments()
Definition Allocator2D.cpp:580
const uint32 Height
Definition Allocator2D.h:120
TArray< uint64 > Bits
Definition Allocator2D.h:128
const ELightmapUVVersion LayoutVersion
Definition Allocator2D.h:122
MESHUTILITIESCOMMON_API bool TestOneRun(const FRun &Run, const FRun &OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution, uint32 &OutFailedLength) const
Definition Allocator2D.cpp:372
MESHUTILITIESCOMMON_API void AddUsedSegment(FRun &Run, uint32 StartPos, uint32 Length)
Definition Allocator2D.cpp:699
MESHUTILITIESCOMMON_API void Alloc(FRect Rect)
Definition Allocator2D.cpp:305
MESHUTILITIESCOMMON_API void InitSegments()
Definition Allocator2D.cpp:570
MESHUTILITIESCOMMON_API void MergeSegments(const FRect &Rect, const FAllocator2D &Other)
Definition Allocator2D.cpp:798
MESHUTILITIESCOMMON_API bool TestAllRows(const FRect &Rect, const FAllocator2D &Other, uint32 &FailedLength) const
Definition Allocator2D.cpp:332
uint64 GetBit(uint32 x, uint32 y) const
Definition Allocator2D.h:177
MESHUTILITIESCOMMON_API FString ToString() const
Definition Allocator2D.cpp:142
bool Test(FRect Rect)
Definition Allocator2D.h:203
MESHUTILITIESCOMMON_API void MergeRun(FRun &Run, const FRun &OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution, uint32 PerpendicularResolution)
Definition Allocator2D.cpp:712
MESHUTILITIESCOMMON_API void PublishStats(int32 ChartIndex, int32 Orientation, bool bFound, const FRect &Rect, const FRect &BestRect, const FMD5Hash &ChartMD5, TFunctionRef< bool(const FAllocator2D::FRect &)> IsBestRect)
Definition Allocator2D.cpp:824
MESHUTILITIESCOMMON_API void ResetStats()
Definition Allocator2D.cpp:817
void SetBit(uint32 x, uint32 y)
Definition Allocator2D.h:182
MESHUTILITIESCOMMON_API FAllocator2D & operator=(const FAllocator2D &Other)
Definition Allocator2D.cpp:68
FAllocator2D(const FAllocator2D &Other)=default
uint32 RasterWidth
Definition Allocator2D.h:124
MESHUTILITIESCOMMON_API void InitRuns(TArray< FRun > &Runs, uint32 PrimaryResolution, uint32 PerpendicularRasterSize)
Definition Allocator2D.cpp:542
const EMode Mode
Definition Allocator2D.h:118
const uint32 Pitch
Definition Allocator2D.h:121
TArray< FRun > Columns
Definition Allocator2D.h:127
uint32 GetRasterWidth() const
Definition Allocator2D.h:103
TArray< uint16 > SortedColumnsIndex
Definition Allocator2D.h:133
MESHUTILITIESCOMMON_API void CopyRuns(TArray< FRun > &Runs, const TArray< FRun > &OtherRuns, int32 MaxSize)
Definition Allocator2D.cpp:53
EMode
Definition Allocator2D.h:23
TArray< FRun > Rows
Definition Allocator2D.h:126
MESHUTILITIESCOMMON_API bool FindBitByBit(FRect &Rect, const FAllocator2D &Other)
Definition Allocator2D.cpp:196
void ClearBit(uint32 x, uint32 y)
Definition Allocator2D.h:198
const uint32 Width
Definition Allocator2D.h:119
MESHUTILITIESCOMMON_API void FlipY(const FRect &Rect)
Definition Allocator2D.cpp:505
uint32 RasterHeight
Definition Allocator2D.h:125
MESHUTILITIESCOMMON_API void Clear()
Definition Allocator2D.cpp:42
Definition AssetRegistryState.h:50
Definition TestUtils.cpp:8
Definition Allocator2D.h:33
uint32 X
Definition Allocator2D.h:34
uint32 Y
Definition Allocator2D.h:35
uint32 H
Definition Allocator2D.h:37
uint32 W
Definition Allocator2D.h:36
Definition Allocator2D.h:49
TArray< FSegment > Segments
Definition Allocator2D.h:51
uint32 LongestSegment
Definition Allocator2D.h:50
TArray< uint16 > FreeSegmentsLookup
Definition Allocator2D.h:60
Definition Allocator2D.h:41
uint32 StartPos
Definition Allocator2D.h:42
bool operator<(const FSegment &Other) const
Definition Allocator2D.h:45
uint32 Length
Definition Allocator2D.h:43
Definition Allocator2D.h:140
uint64 GetValue() const
Definition Allocator2D.h:151
void operator+=(uint64 InValue)
Definition Allocator2D.h:150
void operator++(int)
Definition Allocator2D.h:149
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition GenericPlatformMemory.h:586
Definition SecureHash.h:133
Definition ReporterGraph.h:47
Definition LinuxPlatformSplash.cpp:43
Definition StatsSystem.h:13