UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MultiGPU.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 MultiGPU.h: Multi-GPU support
5=============================================================================*/
6
7#pragma once
8
9#include "Containers/Array.h"
12
13#if DO_GUARD_SLOW
14 #define GPUMASK_CONSTEXPR
15#else
16 #define GPUMASK_CONSTEXPR constexpr
17#endif
18
19#if WITH_MGPU
20 #define MAX_NUM_GPUS 8
23 #define SGPU_CONSTEXPR
24#else
25 #define MAX_NUM_GPUS 1
26 #define GNumExplicitGPUsForRendering 1
27 #define GVirtualMGPU 0
28 #define SGPU_CONSTEXPR GPUMASK_CONSTEXPR
29#endif
30
33{
34private:
35#if WITH_MGPU
36 uint32 GPUMask;
37 inline GPUMASK_CONSTEXPR uint32 GetMask() const
38 {
39 return GPUMask;
40 }
41#else
42 inline constexpr uint32 GetMask() const
43 {
44 return 1;
45 }
46#endif
47
48
49#if WITH_MGPU
51 : GPUMask(InGPUMask)
52 {
53 checkSlow(InGPUMask != 0);
54 }
55#else
57 {
58 checkSlow(InGPUMask == 1);
59 }
60#endif
61
62public:
67
68#if WITH_MGPU
69 inline uint32 ToIndex() const
70 {
72 return FMath::CountTrailingZeros(GetMask());
73 }
74
75 inline SGPU_CONSTEXPR bool HasSingleIndex() const
76 {
77 return FMath::IsPowerOfTwo(GetMask());
78 }
79
81 {
82 return FPlatformMath::CountBits(GetMask());
83 }
84
86 {
87 return FPlatformMath::FloorLog2(GetMask());
88 }
89
91 {
92 return FPlatformMath::CountTrailingZeros(GetMask());
93 }
94#else
95 inline constexpr uint32 ToIndex() const
96 {
97 return 0;
98 }
99
100 inline constexpr bool HasSingleIndex() const
101 {
102 return true;
103 }
104
105 inline constexpr uint32 GetNumActive() const
106 {
107 return 1;
108 }
109
110 inline constexpr uint32 GetLastIndex() const
111 {
112 return 0;
113 }
114
115 inline constexpr uint32 GetFirstIndex() const
116 {
117 return 0;
118 }
119#endif
120
121 inline SGPU_CONSTEXPR bool Contains(uint32 GPUIndex) const
122 {
123 return (GetMask() & (1 << GPUIndex)) != 0;
124 }
125
127 {
128 return (GetMask() & Rhs.GetMask()) == Rhs.GetMask();
129 }
130
131 inline SGPU_CONSTEXPR bool Intersects(FRHIGPUMask Rhs) const
132 {
133 return (GetMask() & Rhs.GetMask()) != 0;
134 }
135
137 {
138 return GetMask() == Rhs.GetMask();
139 }
140
142 {
143 return GetMask() != Rhs.GetMask();
144 }
145
147 {
148#if WITH_MGPU
149 GPUMask |= Rhs.GetMask();
150#endif
151 }
152
154 {
155#if WITH_MGPU
156 GPUMask &= Rhs.GetMask();
157#endif
158 }
159
161 {
162 return GVirtualMGPU ? 1 : GetMask();
163 }
164
165 // Direct use of the internal mask is discouraged, but it can be useful for debugging to display
167 {
168 return GetMask();
169 }
170
172 {
173 return FRHIGPUMask(GetMask() & Rhs.GetMask());
174 }
175
177 {
178 return FRHIGPUMask(GetMask() | Rhs.GetMask());
179 }
180
182 {
183 return FRHIGPUMask(1 << GPUIndex);
184 }
185
187 {
188 return FRHIGPUMask(1);
189 }
190
192 {
193 return FRHIGPUMask((1 << GNumExplicitGPUsForRendering) - 1);
194 }
195
197 {
198 return FRHIGPUMask(~((1u << GPUIndex) - 1)) & All();
199 }
200
201 // Inverts a GPU mask, returning true if the inverse succeeded. If it fails, OutInverse is arbitrarily set to GPU0.
202 inline bool Invert(FRHIGPUMask& OutInverse) const
203 {
204 if (*this == All())
205 {
207 return false;
208 }
209 else
210 {
211 OutInverse = FRHIGPUMask(~GetMask()) & All();
212 return true;
213 }
214 }
215
217 {
218 inline explicit FIterator(const uint32 InGPUMask)
219 : GPUMask(InGPUMask)
221 , FirstGPUIndexInMask(FPlatformMath::CountTrailingZeros(InGPUMask))
222#endif
223 {
224 }
225
227 : FIterator(InGPUMask.GetMask())
228 {
229 }
230
232 {
233#if WITH_MGPU
234 GPUMask &= ~(1 << FirstGPUIndexInMask);
235 FirstGPUIndexInMask = FPlatformMath::CountTrailingZeros(GPUMask);
236#else
237 GPUMask = 0;
238#endif
239 return *this;
240 }
241
243 {
244 FIterator Copy(*this);
245 ++*this;
246 return Copy;
247 }
248
249 inline uint32 operator*() const
250 {
251 return GetFirstIndexInMask();
252 }
253
254 inline bool operator !=(const FIterator& Rhs) const
255 {
256 return GetMask() != Rhs.GetMask();
257 }
258
259 inline explicit operator bool() const
260 {
261 return GetMask() != 0;
262 }
263
264 inline bool operator !() const
265 {
266 return !(bool)*this;
267 }
268
269 private:
270 // NOTE: we cannot remove this in single GPU mode since we need to actually iterate once.
271 uint32 GPUMask;
272
273 inline uint32 GetMask() const
274 {
275 return GPUMask;
276 }
277
278#if WITH_MGPU
280 inline uint32 GetFirstIndexInMask() const
281 {
282 return FirstGPUIndexInMask;
283 }
284#else
285 inline constexpr uint32 GetFirstIndexInMask() const
286 {
287 return 0;
288 }
289#endif
290 };
291
293 {
294 return FRHIGPUMask::FIterator(NodeMask.GetMask());
295 }
296
297 inline friend FRHIGPUMask::FIterator end(FRHIGPUMask NodeMask)
298 {
299 return FRHIGPUMask::FIterator(0);
300 }
301};
302
303#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_5
305#endif
FClangPlatformMath FPlatformMath
Definition AndroidPlatformMath.h:10
#define checkSlow(expr)
Definition AssertionMacros.h:332
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define GPUMASK_CONSTEXPR
Definition MultiGPU.h:16
#define GVirtualMGPU
Definition MultiGPU.h:27
#define SGPU_CONSTEXPR
Definition MultiGPU.h:28
#define GNumExplicitGPUsForRendering
Definition MultiGPU.h:26
const bool
Definition NetworkReplayStreaming.h:178
if(Failed) console_printf("Failed.\n")
uint32_t uint32
Definition binka_ue_file_header.h:6
static constexpr UE_FORCEINLINE_HINT bool IsPowerOfTwo(T Value)
Definition UnrealMathUtility.h:519
Definition MultiGPU.h:217
uint32 operator*() const
Definition MultiGPU.h:249
FIterator & operator++()
Definition MultiGPU.h:231
FIterator operator++(int)
Definition MultiGPU.h:242
FIterator(const uint32 InGPUMask)
Definition MultiGPU.h:218
bool operator!() const
Definition MultiGPU.h:264
bool operator!=(const FIterator &Rhs) const
Definition MultiGPU.h:254
FIterator(FRHIGPUMask InGPUMask)
Definition MultiGPU.h:226
Definition MultiGPU.h:33
GPUMASK_CONSTEXPR FRHIGPUMask()
Definition MultiGPU.h:63
SGPU_CONSTEXPR uint32 GetNative() const
Definition MultiGPU.h:160
static GPUMASK_CONSTEXPR FRHIGPUMask FromIndex(uint32 GPUIndex)
Definition MultiGPU.h:181
constexpr uint32 ToIndex() const
Definition MultiGPU.h:95
friend FRHIGPUMask::FIterator end(FRHIGPUMask NodeMask)
Definition MultiGPU.h:297
SGPU_CONSTEXPR FRHIGPUMask operator|(FRHIGPUMask Rhs) const
Definition MultiGPU.h:176
bool Invert(FRHIGPUMask &OutInverse) const
Definition MultiGPU.h:202
constexpr bool HasSingleIndex() const
Definition MultiGPU.h:100
static SGPU_CONSTEXPR FRHIGPUMask All()
Definition MultiGPU.h:191
friend FRHIGPUMask::FIterator begin(FRHIGPUMask NodeMask)
Definition MultiGPU.h:292
SGPU_CONSTEXPR bool Contains(uint32 GPUIndex) const
Definition MultiGPU.h:121
SGPU_CONSTEXPR uint32 GetForDisplay() const
Definition MultiGPU.h:166
constexpr uint32 GetLastIndex() const
Definition MultiGPU.h:110
SGPU_CONSTEXPR FRHIGPUMask operator&(FRHIGPUMask Rhs) const
Definition MultiGPU.h:171
static GPUMASK_CONSTEXPR FRHIGPUMask GPU0()
Definition MultiGPU.h:186
SGPU_CONSTEXPR bool operator==(FRHIGPUMask Rhs) const
Definition MultiGPU.h:136
static SGPU_CONSTEXPR FRHIGPUMask FilterGPUsBefore(uint32 GPUIndex)
Definition MultiGPU.h:196
SGPU_CONSTEXPR void operator&=(FRHIGPUMask Rhs)
Definition MultiGPU.h:153
constexpr uint32 GetNumActive() const
Definition MultiGPU.h:105
constexpr uint32 GetFirstIndex() const
Definition MultiGPU.h:115
SGPU_CONSTEXPR bool ContainsAll(FRHIGPUMask Rhs) const
Definition MultiGPU.h:126
SGPU_CONSTEXPR void operator|=(FRHIGPUMask Rhs)
Definition MultiGPU.h:146
SGPU_CONSTEXPR bool operator!=(FRHIGPUMask Rhs) const
Definition MultiGPU.h:141
SGPU_CONSTEXPR bool Intersects(FRHIGPUMask Rhs) const
Definition MultiGPU.h:131