UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ISMPoolComponent.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "Containers/Map.h"
9#include "InstanceDataTypes.h"
10
11#include "ISMPoolComponent.generated.h"
12
13class AActor;
16
23{
25
38
40 void Reset()
41 {
44 GroupRanges.Empty();
46 }
47
49 bool IsEmpty() const
50 {
51 return GroupRanges.Num() == FreeList.Num();
52 }
53
56 {
57 return TotalInstanceCount;
58 }
59
62 {
63 // First check to see if we can recycle a group from the free list.
64 for (int32 Index = 0; Index < FreeList.Num(); ++Index)
65 {
66 const FInstanceGroupId GroupId = FreeList[Index];
67 // todo: Could also allow allocating a subrange if Count is less than the group range count.
68 if (Count == GroupRanges[GroupId].Count)
69 {
72 return GroupId;
73 }
74 }
75
76 // Create a new group.
77 const int32 StartIndex = TotalInstanceCount;
79 const FInstanceGroupId GroupId = GroupRanges.Add(FInstanceGroupRange(StartIndex, Count));
80 return GroupId;
81 }
82
85 {
86 // Remove the group by putting on free list for reuse.
87 // Actually removing it would require too much shuffling of the render instance index remapping.
88 TotalFreeInstanceCount += GroupRanges[GroupId].Count;
89 FreeList.Add(GroupId);
90 }
91
96};
97
102{
104 {
105 UseHISM = 1 << 1, // HISM is no longer supported. This flag is ignored.
111 AffectShadow = 1 << 7,
116 };
117
124 uint32 GroupHash = 0; // Optional, allows identical SMs to be separated into different groups for finer grained culling
125 float LodScale = 1.f;
128
130 {
131 return Flags == Other.Flags &&
132 NumCustomDataFloats == Other.NumCustomDataFloats &&
133 Position == Other.Position &&
134 StartCullDistance == Other.StartCullDistance &&
135 EndCullDistance == Other.EndCullDistance &&
136 MinLod == Other.MinLod &&
137 LodScale == Other.LodScale &&
138 Tags == Other.Tags &&
139 GroupHash == Other.GroupHash &&
140 StatsCategory == Other.StatsCategory;
141 }
142};
143
156
162{
167
169 {
170 if (!(Desc == Other.Desc))
171 {
172 return false;
173 }
175 {
176 return false;
177 }
178 if (MaterialsOverrides.Num() != Other.MaterialsOverrides.Num())
179 {
180 return false;
181 }
183 {
184 if (!MaterialsOverrides[MatIndex].HasSameIndexAndSerialNumber(Other.MaterialsOverrides[MatIndex]))
185 {
186 return false;
187 }
188 }
189 if (CustomPrimitiveData.Num() != Other.CustomPrimitiveData.Num())
190 {
191 return false;
192 }
193 for (int32 DataIndex = 0; DataIndex < CustomPrimitiveData.Num(); DataIndex++)
194 {
195 if (CustomPrimitiveData[DataIndex] != Other.CustomPrimitiveData[DataIndex])
196 {
197 return false;
198 }
199 }
200
201 return true;
202 }
203};
204
206{
207 uint32 CombinedHash = GetTypeHash(MeshInstance.StaticMesh);
208 CombinedHash = HashCombineFast(CombinedHash, GetTypeHash(MeshInstance.MaterialsOverrides.Num()));
210 {
211 CombinedHash = HashCombineFast(CombinedHash, GetTypeHash(Material));
212 }
213 for (const float CustomFloat : MeshInstance.CustomPrimitiveData)
214 {
215 CombinedHash = HashCombineFast(CombinedHash, GetTypeHash(CustomFloat));
216 }
217 CombinedHash = HashCombineFast(CombinedHash, GetTypeHash(MeshInstance.Desc));
218 return CombinedHash;
219}
220
231
232struct FISMPool;
233
257
277
278
281{
283
284 FISMPool();
285
287 FISMIndex GetOrAddISM(UISMPoolComponent* OwningComponent, const FISMPoolStaticMeshInstance& MeshInstance, bool& bOutISMCreated);
289 void RemoveISM(FISMIndex ISMIndex, bool bKeepAlive, bool bRecycle);
291 FISMPoolMeshInfo AddInstancesToISM(UISMPoolComponent* OwningComponent, const FISMPoolStaticMeshInstance& MeshInstance, int32 InstanceCount, TArrayView<const float> CustomDataFloats);
293 void RemoveInstancesFromISM(const FISMPoolMeshInfo& MeshInfo);
295 bool BatchUpdateInstancesTransforms(FISMPoolMeshInfo& MeshInfo, int32 StartInstanceIndex, TArrayView<const FTransform> NewInstancesTransforms, bool bWorldSpace, bool bMarkRenderStateDirty, bool bTeleport, bool bAllowPerInstanceRemoval);
297
299 void Clear();
300
302 void Tick(UISMPoolComponent* OwningComponent);
303
308
310
315
318
323
324 // Cached state of lifecycle cvars from the last Tick()
325 bool bCachedKeepAlive = false;
326 bool bCachedRecycle = false;
327
328 // Whether we force ISMs to use parent bounds and disable transform updates
330};
331
332
337UCLASS(meta = (BlueprintSpawnableComponent), MinimalAPI)
338class UISMPoolComponent: public USceneComponent
339{
341
342public:
344 using FMeshId = int32;
345
346 //~ Begin UActorComponent Interface
347 ISMPOOL_API virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
348 ISMPOOL_API virtual void GetResourceSizeEx(FResourceSizeEx& CumulativeResourceSize) override;
349 //~ End UActorComponent Interface
350
356 ISMPOOL_API FMeshGroupId CreateMeshGroup(bool bAllowPerInstanceRemoval = false);
357
359 ISMPOOL_API void DestroyMeshGroup(FMeshGroupId MeshGroupId);
360
362 ISMPOOL_API FMeshId AddMeshToGroup(FMeshGroupId MeshGroupId, const FISMPoolStaticMeshInstance& MeshInstance, int32 InstanceCount, TArrayView<const float> CustomDataFloats);
363
365 ISMPOOL_API bool BatchUpdateInstancesTransforms(FMeshGroupId MeshGroupId, FMeshId MeshId, int32 StartInstanceIndex, TArrayView<const FTransform> NewInstancesTransforms, bool bWorldSpace = false, bool bMarkRenderStateDirty = false, bool bTeleport = false);
366
367 UE_DEPRECATED(5.3, "BatchUpdateInstancesTransforms Array parameter version is deprecated, use the TArrayView version instead")
368 ISMPOOL_API bool BatchUpdateInstancesTransforms(FMeshGroupId MeshGroupId, FMeshId MeshId, int32 StartInstanceIndex, const TArray<FTransform>& NewInstancesTransforms, bool bWorldSpace = false, bool bMarkRenderStateDirty = false, bool bTeleport = false);
369
371 ISMPOOL_API bool BatchUpdateInstanceCustomData(FMeshGroupId MeshGroupId, int32 CustomFloatIndex, float CustomFloatValue);
372
377 ISMPOOL_API void PreallocateMeshInstance(const FISMPoolStaticMeshInstance& MeshInstance);
378
379 ISMPOOL_API void SetTickablePoolManagement(bool bEnablePoolManagement);
380
381 ISMPOOL_API void SetOverrideTransformUpdates(bool bOverrideUpdates);
382
383 ISMPOOL_API void UpdateAbsoluteTransforms(const FTransform& BaseTransform, EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport);
384
385private:
386 uint32 NextMeshGroupId = 0;
388 FISMPool Pool;
389
390 // Expose internals for debug draw support.
392};
EUpdateTransformFlags
Definition ActorComponent.h:95
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
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
ELevelTick
Definition EngineBaseTypes.h:70
ETeleportType
Definition EngineTypes.h:2401
uint32 GetTypeHash(const FISMPoolComponentDescription &Desc)
Definition ISMPoolComponent.h:144
#define GENERATED_UCLASS_BODY(...)
Definition ObjectMacros.h:768
#define UCLASS(...)
Definition ObjectMacros.h:776
constexpr uint32 HashCombineFast(uint32 A, uint32 B)
Definition TypeHash.h:74
uint32 GetArrayHash(const T *Ptr, uint64 Size, uint32 PreviousHash=0)
Definition TypeHash.h:200
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Actor.h:257
Definition NameTypes.h:617
Definition ArrayView.h:139
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
UE_FORCEINLINE_HINT void RemoveAtSwap(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2185
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
void Empty(SizeType Slack=0)
Definition Array.h:2273
Definition UnrealString.h.inl:34
Definition ISMPoolComponent.h:339
int32 FMeshGroupId
Definition ISMPoolComponent.h:343
int32 FMeshId
Definition ISMPoolComponent.h:344
Definition ISMPoolDebugDrawComponent.h:16
Definition InstancedStaticMeshComponent.h:158
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
Definition EngineBaseTypes.h:571
Definition ISMPoolComponent.h:102
EFlags
Definition ISMPoolComponent.h:104
@ AffectDynamicIndirectLighting
Definition ISMPoolComponent.h:113
@ WorldPositionOffsetWritesVelocity
Definition ISMPoolComponent.h:109
@ DistanceCullPrimitive
Definition ISMPoolComponent.h:115
@ GpuLodSelection
Definition ISMPoolComponent.h:106
@ StaticMobility
Definition ISMPoolComponent.h:108
@ EvaluateWorldPositionOffset
Definition ISMPoolComponent.h:110
@ ReverseCulling
Definition ISMPoolComponent.h:107
@ UseHISM
Definition ISMPoolComponent.h:105
@ AffectFarShadow
Definition ISMPoolComponent.h:114
@ AffectShadow
Definition ISMPoolComponent.h:111
@ AffectDistanceFieldLighting
Definition ISMPoolComponent.h:112
int32 MinLod
Definition ISMPoolComponent.h:123
int32 StartCullDistance
Definition ISMPoolComponent.h:121
uint32 Flags
Definition ISMPoolComponent.h:118
FName StatsCategory
Definition ISMPoolComponent.h:127
int32 NumCustomDataFloats
Definition ISMPoolComponent.h:119
int32 EndCullDistance
Definition ISMPoolComponent.h:122
TArray< FName > Tags
Definition ISMPoolComponent.h:126
uint32 GroupHash
Definition ISMPoolComponent.h:124
FVector Position
Definition ISMPoolComponent.h:120
float LodScale
Definition ISMPoolComponent.h:125
bool operator==(const FISMPoolComponentDescription &Other) const
Definition ISMPoolComponent.h:129
Definition ISMPoolComponent.h:260
FISMPoolInstanceGroups::FInstanceGroupId AddInstanceGroup(int32 InstanceCount, TArrayView< const float > CustomDataFloats)
Definition ISMPoolComponent.cpp:236
FISMPoolStaticMeshInstance MeshInstance
Definition ISMPoolComponent.h:269
TObjectPtr< UInstancedStaticMeshComponent > ISMComponent
Definition ISMPoolComponent.h:271
void InitISM(const FISMPoolStaticMeshInstance &InMeshInstance, bool bKeepAlive, bool bOverrideTransformUpdates=false)
Definition ISMPoolComponent.cpp:138
FISMPoolInstanceGroups InstanceGroups
Definition ISMPoolComponent.h:273
void CreateISM(USceneComponent *InOwningComponent)
Definition ISMPoolComponent.cpp:113
TArray< FPrimitiveInstanceId > InstanceIds
Definition ISMPoolComponent.h:275
Definition ISMPoolComponent.h:28
int32 Count
Definition ISMPoolComponent.h:36
int32 Start
Definition ISMPoolComponent.h:35
FInstanceGroupRange(int32 InStart, int32 InCount)
Definition ISMPoolComponent.h:29
Definition ISMPoolComponent.h:23
int32 TotalFreeInstanceCount
Definition ISMPoolComponent.h:93
bool IsEmpty() const
Definition ISMPoolComponent.h:49
int32 FInstanceGroupId
Definition ISMPoolComponent.h:24
FInstanceGroupId AddGroup(int32 Count)
Definition ISMPoolComponent.h:61
TArray< FInstanceGroupRange > GroupRanges
Definition ISMPoolComponent.h:94
void RemoveGroup(FInstanceGroupId GroupId)
Definition ISMPoolComponent.h:84
int32 TotalInstanceCount
Definition ISMPoolComponent.h:92
TArray< FInstanceGroupId > FreeList
Definition ISMPoolComponent.h:95
void Reset()
Definition ISMPoolComponent.h:40
int32 GetMaxInstanceIndex() const
Definition ISMPoolComponent.h:55
Definition ISMPoolComponent.h:239
bool bAllowPerInstanceRemoval
Definition ISMPoolComponent.h:255
int32 FMeshId
Definition ISMPoolComponent.h:240
bool BatchUpdateInstancesTransforms(FISMPool &ISMPool, FMeshId MeshId, int32 StartInstanceIndex, TArrayView< const FTransform > NewInstancesTransforms, bool bWorldSpace, bool bMarkRenderStateDirty, bool bTeleport)
Definition ISMPoolComponent.cpp:86
TArray< FISMPoolMeshInfo > MeshInfos
Definition ISMPoolComponent.h:252
FMeshId AddMesh(const FISMPoolStaticMeshInstance &MeshInstance, int32 InstanceCount, const FISMPoolMeshInfo &ISMInstanceInfo, TArrayView< const float > CustomDataFloats)
Definition ISMPoolComponent.cpp:73
void BatchUpdateInstanceCustomData(FISMPool &ISMPool, int32 CustomFloatIndex, float CustomFloatValue)
Definition ISMPoolComponent.cpp:96
void RemoveAllMeshes(FISMPool &ISMPool)
Definition ISMPoolComponent.cpp:104
Definition ISMPoolComponent.h:223
TArray< float > CustomData
Definition ISMPoolComponent.h:227
int32 ISMIndex
Definition ISMPoolComponent.h:224
FISMPoolInstanceGroups::FInstanceGroupId InstanceGroupIndex
Definition ISMPoolComponent.h:225
TArrayView< const float > CustomDataSlice(int32 InstanceIndex, int32 NumCustomDataFloatsPerInstance)
Definition ISMPoolComponent.cpp:67
void ShadowCopyCustomData(int32 InstanceCount, int32 NumCustomDataFloatsPerInstance, TArrayView< const float > CustomDataFloats)
Definition ISMPoolComponent.cpp:56
Definition ISMPoolComponent.h:162
FISMPoolComponentDescription Desc
Definition ISMPoolComponent.h:166
TWeakObjectPtr< UStaticMesh > StaticMesh
Definition ISMPoolComponent.h:163
TArray< float > CustomPrimitiveData
Definition ISMPoolComponent.h:165
TArray< TWeakObjectPtr< UMaterialInterface > > MaterialsOverrides
Definition ISMPoolComponent.h:164
bool operator==(const FISMPoolStaticMeshInstance &Other) const
Definition ISMPoolComponent.h:168
Definition ISMPoolComponent.h:281
TArray< int32 > FreeList
Definition ISMPoolComponent.h:320
void RemoveISM(FISMIndex ISMIndex, bool bKeepAlive, bool bRecycle)
Definition ISMPoolComponent.cpp:449
void BatchUpdateInstanceCustomData(FISMPoolMeshInfo const &MeshInfo, int32 CustomFloatIndex, float CustomFloatValue)
Definition ISMPoolComponent.cpp:381
bool bCachedKeepAlive
Definition ISMPoolComponent.h:325
int32 FISMIndex
Definition ISMPoolComponent.h:282
FISMPool()
Definition ISMPoolComponent.cpp:273
TArray< FISMPoolISM > ISMs
Definition ISMPoolComponent.h:312
TArray< int32 > FreeListISM
Definition ISMPoolComponent.h:322
void Clear()
Definition ISMPoolComponent.cpp:483
FISMPoolMeshInfo AddInstancesToISM(UISMPoolComponent *OwningComponent, const FISMPoolStaticMeshInstance &MeshInstance, int32 InstanceCount, TArrayView< const float > CustomDataFloats)
Definition ISMPoolComponent.cpp:314
bool bDisableBoundsAndTransformUpdate
Definition ISMPoolComponent.h:329
bool bCachedRecycle
Definition ISMPoolComponent.h:326
bool BatchUpdateInstancesTransforms(FISMPoolMeshInfo &MeshInfo, int32 StartInstanceIndex, TArrayView< const FTransform > NewInstancesTransforms, bool bWorldSpace, bool bMarkRenderStateDirty, bool bTeleport, bool bAllowPerInstanceRemoval)
Definition ISMPoolComponent.cpp:323
void UpdateAbsoluteTransforms(const FTransform &BaseTransform, EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport)
Definition ISMPoolComponent.cpp:558
FISMIndex GetOrAddISM(UISMPoolComponent *OwningComponent, const FISMPoolStaticMeshInstance &MeshInstance, bool &bOutISMCreated)
Definition ISMPoolComponent.cpp:279
void RemoveInstancesFromISM(const FISMPoolMeshInfo &MeshInfo)
Definition ISMPoolComponent.cpp:405
void RequestPreallocateMeshInstance(const FISMPoolStaticMeshInstance &MeshInstances)
Definition ISMPoolComponent.cpp:502
TMap< FISMPoolStaticMeshInstance, FISMIndex > MeshToISMIndex
Definition ISMPoolComponent.h:314
TSet< FISMPoolStaticMeshInstance > PrellocationQueue
Definition ISMPoolComponent.h:317
void ProcessPreallocationRequests(UISMPoolComponent *OwningComponent, int32 MaxPreallocations)
Definition ISMPoolComponent.cpp:533
Definition ResourceSize.h:31
Definition ObjectPtr.h:488
Definition WeakObjectPtrTemplates.h:25
FORCEINLINE bool HasSameIndexAndSerialNumber(const TWeakObjectPtr &Other) const
Definition WeakObjectPtrTemplates.h:273
static CORE_API const TVector< double > ZeroVector
Definition Vector.h:79