How to include MetalResources.h from MetalRHI

  • UE 5.3
  • XCode

I want to include /Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h into my C++ based project. Hence I added MetalRHI in my Build.cs file as below.

PrivateDependencyModuleNames.AddRange(new string[] { "MetalRHI"});

Now this header file can be found, but it internally depends on mtlpp.hpp which is a third-party C++ wrapper of MetalAPI

#include "MetalResources.h"

Hence I added mtlpp into PrivateDependencyModuleNames as well.

PrivateDependencyModuleNames.AddRange(new string[] { "MetalRHI", "mtlpp" });

Now there are a lot of compliation errors. how to include MetalResources.h from MetalRHI?

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:268:10 conversion from 'const FMetalTexture' to 'void *' is ambiguous

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:327:5 'METAL_RHI_RAYTRACING' is not defined, evaluates to 0 [-Werror,-Wundef]

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:424:5 'METAL_RHI_RAYTRACING' is not defined, evaluates to 0 [-Werror,-Wundef]

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:435:5 'METAL_RHI_RAYTRACING' is not defined, evaluates to 0 [-Werror,-Wundef]

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:469:5 'METAL_RHI_RAYTRACING' is not defined, evaluates to 0 [-Werror,-Wundef]

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:551:5 'METAL_RHI_RAYTRACING' is not defined, evaluates to 0 [-Werror,-Wundef]

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:619:10 unknown type name 'FMetalSamplerState'; did you mean 'FRHISamplerState'?

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:624:10 unknown type name 'FMetalRasterizerState'; did you mean 'FRHIRasterizerState'?

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:629:10 unknown type name 'FMetalDepthStencilState'; did you mean 'FRHIDepthStencilState'?

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:634:10 unknown type name 'FMetalBlendState'; did you mean 'FRHIBlendState'?

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:656:5 'METAL_RHI_RAYTRACING' is not defined, evaluates to 0 [-Werror,-Wundef]

Then I changed the include part of code to

#define METAL_RHI_RAYTRACING 0
#include "MetalState.h"
#include "MetalResources.h"

Now only one error is left

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:268:10 conversion from 'const FMetalTexture' to 'void *' is ambiguous

It is from UE’s header file of this [line].
(https://github.com/EpicGames/UnrealEngine/blob/072300df18a94f18077ca20a14224b5d99fee872/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h#L268).

Thanks in advance

Try it this way:

PublicDependencyModuleNames.AddRange(new string[]
{
	...
	"RHI", 
	"MetalRHI"
});

AddEngineThirdPartyPrivateStaticDependencies(Target, "MetalCPP");	// Third party folder.
PublicWeakFrameworks.Add("Metal");	// Apple's Metal library

Thanks for replying

I receive the following error by trying your suggestion.

Could not find definition for module 'MetalCPP', (referenced via Target -> MacArDemo.Build.cs)

If I replace MetalCPP with mtlpp, it can find mtlpp.hpp but failed with the errors in my original post above.

AddEngineThirdPartyPrivateStaticDependencies(Target, "mtlpp");

And most errors can be solved by adding two extra lines. But still there is a compliation error left

Still this error is left from this line.

/Users/Shared/Epic Games/UE_5.3/Engine/Source/Runtime/Apple/MetalRHI/Public/MetalResources.h:268:10 conversion from 'const FMetalTexture' to 'void *' is ambiguous

I don’t know why this happened

I overcame the conversion error by modifying the header file MetalResources.h from UE

	virtual void* GetNativeResource() const override final
	{
		const FMetalTexture* ptr = &(this->Texture);
		return (void*)ptr;
	}

I was looking at Unreal 5.4 as a reference, for 5.3 it is being used like this:

PublicDependencyModuleNames.AddRange(new string[]
{
	...
	"RHI"
});

if (Target.Platform == UnrealTargetPlatform.Mac) 
{
    PublicDependencyModuleNames.Add("MetalRHI");
    AddEngineThirdPartyPrivateStaticDependencies(Target, "MTLPP");
}


/*
other platforms
Target.Platform == UnrealTargetPlatform.Mac
Target.Platform == UnrealTargetPlatform.IOS
Target.Platform == UnrealTargetPlatform.TVOS
Target.IsInPlatformGroup(UnrealPlatformGroup.Apple
*/
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.