UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MetalRHIPrivate.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 MetalRHIPrivate.h: Private Metal RHI definitions.
5=============================================================================*/
6
7#pragma once
8
9#include "MetalRHI.h"
10#include "MetalThirdParty.h"
11#include "PixelFormat.h"
12#include "RHICommandList.h"
13#include "RHIGlobals.h"
14
15class FMetalDevice;
16class FMetalSurface;
17
19
21
22// Whether the Metal RHI is initialized sufficiently to handle resources
23extern bool GIsMetalInitialized;
24
25// Requirement for vertex buffer offset field
26#if PLATFORM_MAC
29#else
32#endif
33
34// The maximum buffer page size that can be uploaded in a set*Bytes call
36
37// The buffer size that is more efficiently uploaded in a set*Bytes call - defined in terms of BufferOffsetAlignment
38#if PLATFORM_MAC
40#else
42#endif
43
44
45#define METAL_USE_METAL_SHADER_CONVERTER PLATFORM_SUPPORTS_BINDLESS_RENDERING
46#define METAL_RHI_RAYTRACING (METAL_USE_METAL_SHADER_CONVERTER)
47
48// Metal Shader Converter
49#if METAL_USE_METAL_SHADER_CONVERTER
51 #include "metal_irconverter.h"
52 #define IR_RUNTIME_METALCPP 1
53 #define IR_PRIVATE_IMPLEMENTATION 1
54 #include "metal_irconverter_runtime.h"
56
58
59#endif
60
61#define BUFFER_CACHE_MODE MTL::ResourceCPUCacheModeDefaultCache
62
63#if PLATFORM_MAC
64#define BUFFER_MANAGED_MEM MTL::ResourceStorageModeManaged
65#define BUFFER_STORAGE_MODE MTL::StorageModeShared
66#define BUFFER_RESOURCE_STORAGE_MANAGED MTL::ResourceStorageModeManaged
67#define BUFFER_DYNAMIC_REALLOC BUF_AnyDynamic
68// How many possible vertex streams are allowed
69const uint32 MaxMetalStreams = 31;
70#else
71#define BUFFER_MANAGED_MEM 0
72
73#if WITH_IOS_SIMULATOR
74#define BUFFER_STORAGE_MODE MTL::StorageModePrivate
75#define BUFFER_RESOURCE_STORAGE_MANAGED MTL::ResourceStorageModePrivate
76#else
77#define BUFFER_STORAGE_MODE MTL::StorageModeShared
78#define BUFFER_RESOURCE_STORAGE_MANAGED MTL::ResourceStorageModeShared
79#endif
80
81#define BUFFER_DYNAMIC_REALLOC BUF_AnyDynamic
82// How many possible vertex streams are allowed
84#endif
85
86// Unavailable on iOS, but dealing with this clutters the code.
91
92// This is the right VERSION check, see Availability.h in the SDK
93#define METAL_SUPPORTS_INDIRECT_ARGUMENT_BUFFERS 1
94#define METAL_SUPPORTS_CAPTURE_MANAGER 1
95#define METAL_SUPPORTS_TILE_SHADERS 1
96
97// In addition to compile-time SDK checks we also need a way to check if these are available on runtime
99
101{
102 // Valid linear texture pixel formats - potentially different than the actual texture formats
103 MTL::PixelFormat LinearTextureFormat;
104 // Metal buffer data types for manual ALU format conversions
106};
107
109
110#define METAL_DEBUG_OPTIONS !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
111#if METAL_DEBUG_OPTIONS
112 #define METAL_DEBUG_OPTION(Code) Code
113#else
114 #define METAL_DEBUG_OPTION(Code)
115#endif
116
118
120#ifndef ENABLE_METAL_GPUEVENTS_IN_TEST
121 #define ENABLE_METAL_GPUEVENTS_IN_TEST 0
122#endif
123#define ENABLE_METAL_GPUEVENTS (UE_BUILD_DEBUG || UE_BUILD_DEVELOPMENT || (UE_BUILD_TEST && ENABLE_METAL_GPUEVENTS_IN_TEST))
124#define ENABLE_METAL_GPUPROFILE (ENABLE_METAL_GPUEVENTS && 1)
125
126#if ENABLE_METAL_GPUPROFILE && RHI_NEW_GPU_PROFILER == 0
127 #define METAL_GPUPROFILE(Code) Code
128#else
129 #define METAL_GPUPROFILE(Code)
130#endif
131
132#define UNREAL_TO_METAL_BUFFER_INDEX(Index) ((MaxMetalStreams - 1) - Index)
133#define METAL_TO_UNREAL_BUFFER_INDEX(Index) ((MaxMetalStreams - 1) - Index)
134
135#define METAL_NEW_NONNULL_DECL (__clang_major__ >= 9)
136
137#if PLATFORM_IOS
138#define METAL_FATAL_ERROR(Format, ...) { UE_LOG(LogMetal, Warning, Format, __VA_ARGS__); FIOSPlatformMisc::MetalAssert(); }
139#else
140#define METAL_FATAL_ERROR(Format, ...) UE_LOG(LogMetal, Fatal, Format, __VA_ARGS__)
141#endif
142#define METAL_FATAL_ASSERT(Condition, Format, ...) if (!(Condition)) { METAL_FATAL_ERROR(Format, __VA_ARGS__); }
143
144#if !defined(METAL_IGNORED)
145 #define METAL_IGNORED(Func)
146#endif
147
152
153// Access the underlying surface object from any kind of texture
155
156#define NOT_SUPPORTED(Func) UE_LOG(LogMetal, Fatal, TEXT("'%s' is not supported"), TEXT(Func));
157
159{
160 // we can use RHI thread resources if we are on the RHIThread or on RenderingThread when there's no RHI thread, or the RHI thread is stalled or inactive
162 || IsInRHIThread()
164}
165
167{
168 // According to Metal docs these should match now: https://developer.apple.com/library/prerelease/ios/documentation/Metal/Reference/MTLTexture_Ref/index.html#//apple_ref/c/tdef/MTLTextureType
169 switch (Face)
170 {
171 case CubeFace_PosX:;
172 default: return 0;
173 case CubeFace_NegX: return 1;
174 case CubeFace_PosY: return 2;
175 case CubeFace_NegY: return 3;
176 case CubeFace_PosZ: return 4;
177 case CubeFace_NegZ: return 5;
178 }
179}
180
182{
183 switch(LoadAction)
184 {
185 case ERenderTargetLoadAction::ENoAction: return MTL::LoadActionDontCare;
186 case ERenderTargetLoadAction::ELoad: return MTL::LoadActionLoad;
187 case ERenderTargetLoadAction::EClear: return MTL::LoadActionClear;
188 default: return MTL::LoadActionDontCare;
189 }
190}
191
192MTL::PrimitiveType TranslatePrimitiveType(uint32 PrimitiveType);
193
194#if PLATFORM_MAC
195MTL::PrimitiveTopologyClass TranslatePrimitiveTopology(uint32 PrimitiveType);
196#endif
197
198MTL::PixelFormat UEToMetalFormat(FMetalDevice& Device, EPixelFormat UEFormat, bool bSRGB);
199
200uint8 GetMetalPixelFormatKey(MTL::PixelFormat Format);
201
202template<class T>
204{
205};
206
207template<typename TRHIType>
208static FORCEINLINE typename TMetalResourceTraits<TRHIType>::TConcreteType* ResourceCast(TRHIType* Resource)
209{
210 return static_cast<typename TMetalResourceTraits<TRHIType>::TConcreteType*>(Resource);
211}
212
213static FORCEINLINE FMetalSurface* ResourceCast(FRHITexture* Texture)
214{
216}
217
218MTL::LanguageVersion ValidateVersion(uint32 Version);
219
220// Needs to be the same as EShaderFrequency when all stages are supported, but unlike EShaderFrequency you can compile out stages.
222{
225#if PLATFORM_SUPPORTS_GEOMETRY_SHADERS
226 Geometry,
227#endif
228#if PLATFORM_SUPPORTS_MESH_SHADERS
229 Mesh,
230 Amplification,
231#endif
233
235};
236
238{
239 switch (Stage)
240 {
242 return SF_Vertex;
244 return SF_Pixel;
245#if PLATFORM_SUPPORTS_GEOMETRY_SHADERS
246 case EMetalShaderStages::Geometry:
247 return SF_Geometry;
248#endif
249#if PLATFORM_SUPPORTS_MESH_SHADERS
250 case EMetalShaderStages::Mesh:
251 return SF_Mesh;
252 case EMetalShaderStages::Amplification:
253 return SF_Amplification;
254#endif
256 return SF_Compute;
257 default:
258 return SF_NumFrequencies;
259 }
260}
261
263{
264 switch (Stage)
265 {
266 case SF_Vertex:
268 case SF_Pixel:
270#if PLATFORM_SUPPORTS_GEOMETRY_SHADERS
271 case SF_Geometry:
272 return EMetalShaderStages::Geometry;
273#endif
274#if PLATFORM_SUPPORTS_MESH_SHADERS
275 case SF_Mesh:
276 return EMetalShaderStages::Mesh;
277 case SF_Amplification:
278 return EMetalShaderStages::Amplification;
279#endif
280 case SF_Compute:
282 default:
284 }
285}
286
287FORCEINLINE FString NSStringToFString(NS::String* InputString)
288{
289 return FString((__bridge CFStringRef)InputString);
290}
291
292FORCEINLINE NS::String* FStringToNSString(const FString& InputString)
293{
294 return ((NS::String*)InputString.GetCFString())->autorelease();
295}
296
297// helper functions to reduce copy and pasted checks all around
299bool AllowMSAA();
#define FORCEINLINE
Definition AndroidPlatform.h:140
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
#define DECLARE_DELEGATE_OneParam(DelegateName, Param1Type)
Definition DelegateCombinations.h:48
DIRECTLINK_API Display
Definition DirectLinkLog.h:8
#define THIRD_PARTY_INCLUDES_START
Definition GenericPlatformCompilerPreSetup.h:63
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
Definition LogMacros.h:361
bool GIsMetalInitialized
Definition MetalRHI.cpp:39
const uint32 BufferBackedLinearTextureOffsetAlignment
Definition MetalRHIPrivate.h:31
MTL::PixelFormat UEToMetalFormat(FMetalDevice &Device, EPixelFormat UEFormat, bool bSRGB)
Definition MetalTexture.cpp:187
FORCEINLINE EMetalShaderStages GetMetalShaderFrequency(EShaderFrequency Stage)
Definition MetalRHIPrivate.h:262
FORCEINLINE MTL::LoadAction GetMetalRTLoadAction(ERenderTargetLoadAction LoadAction)
Definition MetalRHIPrivate.h:181
bool AllowMSAA()
Definition MetalTexture.cpp:59
const uint32 MaxMetalStreams
Definition MetalRHIPrivate.h:83
bool GMetalCommandBufferDebuggingEnabled
Definition MetalCommandQueue.cpp:24
FORCEINLINE NS::String * FStringToNSString(const FString &InputString)
Definition MetalRHIPrivate.h:292
FORCEINLINE bool IsMetalBindlessEnabled()
Definition MetalRHIPrivate.h:148
const uint32 MetalBufferPageSize
Definition MetalRHIPrivate.h:35
const uint32 MetalBufferBytesSize
Definition MetalRHIPrivate.h:41
FORCEINLINE FString NSStringToFString(NS::String *InputString)
Definition MetalRHIPrivate.h:287
FORCEINLINE bool MetalIsSafeToUseRHIThreadResources()
Definition MetalRHIPrivate.h:158
bool ShouldUseMemoryless(ETextureCreateFlags Flags)
Definition MetalTexture.cpp:46
EMTLTextureType
Definition MetalRHIPrivate.h:88
@ EMTLTextureTypeCubeArray
Definition MetalRHIPrivate.h:89
EMetalShaderStages
Definition MetalRHIPrivate.h:222
@ Num
Definition MetalRHIPrivate.h:234
@ Pixel
Definition MetalRHIPrivate.h:224
@ Vertex
Definition MetalRHIPrivate.h:223
@ Compute
Definition MetalRHIPrivate.h:232
FORCEINLINE int32 GetMetalCubeFace(ECubeFace Face)
Definition MetalRHIPrivate.h:166
MTL::PrimitiveType TranslatePrimitiveType(uint32 PrimitiveType)
Definition MetalCommands.cpp:50
FMetalSurface * GetMetalSurfaceFromRHITexture(FRHITexture *Texture)
Definition MetalTexture.cpp:66
const uint32 BufferOffsetAlignment
Definition MetalRHIPrivate.h:30
FMetalBufferFormat GMetalBufferFormats[PF_MAX]
Definition MetalRHI.cpp:41
bool GMetalSupportsCaptureManager
Definition MetalCaptureManager.cpp:7
uint8 GetMetalPixelFormatKey(MTL::PixelFormat Format)
Definition MetalTexture.cpp:294
FORCEINLINE EShaderFrequency GetRHIShaderFrequency(EMetalShaderStages Stage)
Definition MetalRHIPrivate.h:237
MTL::LanguageVersion ValidateVersion(uint32 Version)
Definition MetalShaders.cpp:49
EPixelFormat
Definition PixelFormat.h:16
@ PF_MAX
Definition PixelFormat.h:111
bool IsRunningRHIInSeparateThread()
Definition RHICommandList.h:159
EShaderFrequency
Definition RHIDefinitions.h:202
@ SF_Compute
Definition RHIDefinitions.h:208
@ SF_NumFrequencies
Definition RHIDefinitions.h:216
@ SF_Amplification
Definition RHIDefinitions.h:205
@ SF_Vertex
Definition RHIDefinitions.h:203
@ SF_Mesh
Definition RHIDefinitions.h:204
@ SF_Geometry
Definition RHIDefinitions.h:207
@ SF_Pixel
Definition RHIDefinitions.h:206
ECubeFace
Definition RHIDefinitions.h:525
@ CubeFace_NegX
Definition RHIDefinitions.h:527
@ CubeFace_PosX
Definition RHIDefinitions.h:526
@ CubeFace_NegY
Definition RHIDefinitions.h:529
@ CubeFace_NegZ
Definition RHIDefinitions.h:531
@ CubeFace_PosZ
Definition RHIDefinitions.h:530
@ CubeFace_PosY
Definition RHIDefinitions.h:528
ETextureCreateFlags
Definition RHIDefinitions.h:1091
ERenderTargetLoadAction
Definition RHIDefinitions.h:1253
FRHIGlobals GRHIGlobals
Definition RHIGlobals.cpp:6
#define GIsRHIInitialized
Definition RHIGlobals.h:756
CORE_API bool IsInRHIThread()
Definition ThreadingBase.cpp:339
CORE_API bool IsInRenderingThread()
Definition ThreadingBase.cpp:273
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition MetalDevice.h:102
Definition MetalResources.h:206
static RHI_API bool AreRHITasksActive()
Definition RHICommandList.cpp:1791
static RHI_API bool IsStalled()
Definition RHICommandList.cpp:1850
Definition RHIResources.h:2153
Definition Voronoi.cpp:10
Definition MetalRHIPrivate.h:101
uint8 DataFormat
Definition MetalRHIPrivate.h:105
MTL::PixelFormat LinearTextureFormat
Definition MetalRHIPrivate.h:103
PRAGMA_ENABLE_DEPRECATION_WARNINGS bool bSupportsBindless
Definition RHIGlobals.h:669
Definition MetalRHIPrivate.h:204