UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UVSpaceAllocator.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6
7struct FRect;
8struct Rect;
9struct FMD5Hash;
10
11namespace UE
12{
13namespace Geometry
14{
15
16//
17// GeometryProcessing port of MeshUtilitiesCommon FAllocator2D
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
67 void Clear();
68
69 bool Find( FRect& Rect );
70 bool Test( FRect Rect );
71 void Alloc( FRect Rect );
72
75 bool Test( FRect Rect, const FUVSpaceAllocator& Other );
76 void Alloc( FRect Rect, const FUVSpaceAllocator& 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
82 void CreateUsedSegments();
83 void MergeRun( FRun& Run, const FRun& OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution /* Resolution along the axis the run belongs to */, uint32 PerpendicularResolution );
84 void MergeSegments( const FRect& Rect, const FUVSpaceAllocator& Other );
85
86 void FlipX( const FRect& Rect );
87 void FlipY( const FRect& Rect );
88
89 uint32 GetUsedTexels() const;
90
92
93 // Take control of the copy to reduce the amount of data movement to the strict minimum
95
96 // Allow to visualize the content in ascii for debugging purpose. (i.e Watch or Immediate window).
97 FString ToString() const;
98
99 // Get the MD5 hash of the rasterized content
100 FMD5Hash GetRasterMD5() const;
101
104
105protected:
106 bool TestOneRun( const FRun& Run, const FRun& OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution, uint32& OutFailedLength ) const;
107 bool TestAllRows( const FRect& Rect, const FUVSpaceAllocator& Other, uint32& FailedLength ) const;
108 bool TestAllColumns( const FRect& Rect, const FUVSpaceAllocator& Other, uint32& FailedLength ) const;
110 void InitSegments();
111 void AddUsedSegment( FRun& Run, uint32 StartPos, uint32 Length );
112
113 // Enforce that those cannot be changed in flight
114 const EMode Mode;
118
121 TArray< FRun > Rows; // Represent rows in the grid
122 TArray< FRun > Columns; // Represent columns in the grid (used when version >= Segments2D).
124
125 // Index inside rows that will be sorted by rows with longest used segment first
127 // Index inside columns that will be sorted by columns with longest used segment first
129};
130
131// Returns non-zero if set
133{
134 return Bits[ (x >> 6) + y * Pitch ] & ( 1ull << ( x & 63 ) );
135}
136
138{
139 Bits[ (x >> 6) + y * Pitch ] |= ( 1ull << ( x & 63 ) );
140
141 // Keep track of the rasterized dimension to optimize operations on that area only
142 if (y >= RasterHeight)
143 {
144 RasterHeight = y + 1;
145 }
146
147 if (x >= RasterWidth)
148 {
149 RasterWidth = x + 1;
150 }
151}
152
154{
155 Bits[ (x >> 6) + y * Pitch ] &= ~( 1ull << ( x & 63 ) );
156}
157
159{
160 for( uint32 y = Rect.Y; y < Rect.Y + Rect.H; y++ )
161 {
162 for( uint32 x = Rect.X; x < Rect.X + Rect.W; x++ )
163 {
164 if( GetBit( x, y ) )
165 {
166 return false;
167 }
168 }
169 }
170
171 return true;
172}
173
175{
176 const uint32 LowShift = Rect.X & 63;
177 const uint32 HighShift = 64 - LowShift;
178
179 for( uint32 y = 0; y < Rect.H; y++ )
180 {
181#if 1
182 uint32 ThisIndex = (Rect.X >> 6) + (y + Rect.Y) * Pitch;
183 uint32 OtherIndex = y * Pitch;
184
185 // Test a uint64 at a time
186 for( uint32 x = 0; x < Rect.W; x += 64 )
187 {
188 // no need to zero out HighInt on wrap around because Other will always be zero outside Rect.
189 uint64 LowInt = Bits[ ThisIndex ];
190 uint64 HighInt = Bits[ ThisIndex + 1 ];
191
194
195 if( ThisInt & OtherInt )
196 {
197 return false;
198 }
199
200 ThisIndex++;
201 OtherIndex++;
202 }
203#else
204 for( uint32 x = 0; x < Rect.W; x++ )
205 {
206 if( Other.GetBit( x, y ) && GetBit( x + Rect.X, y + Rect.Y ) )
207 {
208 return false;
209 }
210 }
211#endif
212 }
213
214 return true;
215}
216
217
218} // end namespace UE::Geometry
219} // end namespace UE
#define FORCEINLINE
Definition AndroidPlatform.h:140
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
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition AssetRegistryState.h:50
Definition UVSpaceAllocator.h:20
TArray< uint16 > SortedRowsIndex
Definition UVSpaceAllocator.h:126
TArray< uint64 > Bits
Definition UVSpaceAllocator.h:123
void SetBit(uint32 x, uint32 y)
Definition UVSpaceAllocator.h:137
void MergeRun(FRun &Run, const FRun &OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution, uint32 PerpendicularResolution)
Definition UVSpaceAllocator.cpp:686
bool Test(FRect Rect)
Definition UVSpaceAllocator.h:158
uint32 GetRasterHeight() const
Definition UVSpaceAllocator.h:103
void ClearBit(uint32 x, uint32 y)
Definition UVSpaceAllocator.h:153
void AddUsedSegment(FRun &Run, uint32 StartPos, uint32 Length)
Definition UVSpaceAllocator.cpp:673
void InitRuns(TArray< FRun > &Runs, uint32 PrimaryResolution, uint32 PerpendicularRasterSize)
Definition UVSpaceAllocator.cpp:522
uint32 GetUsedTexels() const
Definition UVSpaceAllocator.cpp:271
FMD5Hash GetRasterMD5() const
Definition UVSpaceAllocator.cpp:98
void InitSegments()
Definition UVSpaceAllocator.cpp:550
const uint32 Pitch
Definition UVSpaceAllocator.h:117
uint32 RasterHeight
Definition UVSpaceAllocator.h:120
TArray< FRun > Rows
Definition UVSpaceAllocator.h:121
TArray< FRun > Columns
Definition UVSpaceAllocator.h:122
void FlipY(const FRect &Rect)
Definition UVSpaceAllocator.cpp:479
bool FindBitByBit(FRect &Rect, const FUVSpaceAllocator &Other)
Definition UVSpaceAllocator.cpp:191
void Clear()
Definition UVSpaceAllocator.cpp:40
bool TestOneRun(const FRun &Run, const FRun &OtherRun, uint32 RectOffset, uint32 RectLength, uint32 PrimaryResolution, uint32 &OutFailedLength) const
Definition UVSpaceAllocator.cpp:355
FUVSpaceAllocator & operator=(const FUVSpaceAllocator &Other)
Definition UVSpaceAllocator.cpp:66
uint64 GetBit(uint32 x, uint32 y) const
Definition UVSpaceAllocator.h:132
bool TestAllColumns(const FRect &Rect, const FUVSpaceAllocator &Other, uint32 &FailedLength) const
Definition UVSpaceAllocator.cpp:336
EMode
Definition UVSpaceAllocator.h:23
const uint32 Height
Definition UVSpaceAllocator.h:116
uint32 GetRasterWidth() const
Definition UVSpaceAllocator.h:102
void CopyRuns(TArray< FRun > &Runs, const TArray< FRun > &OtherRuns, int32 MaxSize)
Definition UVSpaceAllocator.cpp:51
const uint32 Width
Definition UVSpaceAllocator.h:115
void Alloc(FRect Rect)
Definition UVSpaceAllocator.cpp:290
bool FindWithSegments(FRect &Rect, const FUVSpaceAllocator &Other, TFunctionRef< bool(const FUVSpaceAllocator::FRect &)> IsBestRect) const
Definition UVSpaceAllocator.cpp:209
void FlipX(const FRect &Rect)
Definition UVSpaceAllocator.cpp:433
void MergeSegments(const FRect &Rect, const FUVSpaceAllocator &Other)
Definition UVSpaceAllocator.cpp:772
uint32 RasterWidth
Definition UVSpaceAllocator.h:119
TArray< uint16 > SortedColumnsIndex
Definition UVSpaceAllocator.h:128
FString ToString() const
Definition UVSpaceAllocator.cpp:137
const EMode Mode
Definition UVSpaceAllocator.h:114
bool TestAllRows(const FRect &Rect, const FUVSpaceAllocator &Other, uint32 &FailedLength) const
Definition UVSpaceAllocator.cpp:317
void CreateUsedSegments()
Definition UVSpaceAllocator.cpp:557
Definition TestUtils.cpp:8
Definition AdvancedWidgetsModule.cpp:13
Definition SecureHash.h:133
Definition ReporterGraph.h:47
Definition LinuxPlatformSplash.cpp:43
Definition UVSpaceAllocator.h:33
uint32 W
Definition UVSpaceAllocator.h:36
uint32 H
Definition UVSpaceAllocator.h:37
uint32 Y
Definition UVSpaceAllocator.h:35
uint32 X
Definition UVSpaceAllocator.h:34
Definition UVSpaceAllocator.h:49
TArray< uint16 > FreeSegmentsLookup
Definition UVSpaceAllocator.h:60
uint32 LongestSegment
Definition UVSpaceAllocator.h:50
TArray< FSegment > Segments
Definition UVSpaceAllocator.h:51
Definition UVSpaceAllocator.h:41
uint32 StartPos
Definition UVSpaceAllocator.h:42
bool operator<(const FSegment &Other) const
Definition UVSpaceAllocator.h:45
uint32 Length
Definition UVSpaceAllocator.h:43