// Copyright Epic Games, Inc. All Rights Reserved. /*============================================================================= MetalViewport.h: Metal viewport RHI definitions. =============================================================================*/ #pragma once #include "MetalRHIPrivate.h" #include "PixelFormat.h" #include "RHIResources.h" #include "MetalResources.h" #if PLATFORM_MAC #include "Mac/CocoaTextView.h" @interface FMetalView : FCocoaTextView @end #endif #include "HAL/PlatformFramePacer.h" #if PLATFORM_VISIONOS #import #endif class FMetalSurface; class FMetalDevice; enum EMetalViewportAccessFlag { EMetalViewportAccessRHI, EMetalViewportAccessRenderer, EMetalViewportAccessGame, EMetalViewportAccessDisplayLink }; class FMetalCommandQueue; #if PLATFORM_VISIONOS namespace MetalRHIVisionOS { struct BeginRenderingImmersiveParams; struct PresentImmersiveParams; } #endif typedef void (^FMetalViewportPresentHandler)(uint32 CGDirectDisplayID, double OutputSeconds, double OutputDuration); class FMetalViewport : public FRHIViewport { public: FMetalViewport(FMetalDevice& InDevice, void* WindowHandle, uint32 InSizeX, uint32 InSizeY, bool bInIsFullscreen,EPixelFormat InFormat); ~FMetalViewport(); void Resize(uint32 InSizeX, uint32 InSizeY, bool bInIsFullscreen,EPixelFormat Format); TRefCountPtr GetBackBuffer(EMetalViewportAccessFlag Accessor) const; CA::MetalDrawable* GetDrawable(EMetalViewportAccessFlag Accessor); MTL::Texture* GetDrawableTexture(EMetalViewportAccessFlag Accessor); MTL::Texture* GetCurrentTexture(EMetalViewportAccessFlag Accessor); void ReleaseDrawable(void); // supports pulling the raw MTLTexture virtual void* GetNativeBackBufferTexture() const override { return GetBackBuffer(EMetalViewportAccessRenderer).GetReference(); } virtual void* GetNativeBackBufferRT() const override { return (const_cast(this))->GetDrawableTexture(EMetalViewportAccessRenderer); } #if PLATFORM_MAC NSWindow* GetWindow() const; #endif #if PLATFORM_MAC || PLATFORM_VISIONOS virtual void SetCustomPresent(FRHICustomPresent* InCustomPresent) override { CustomPresent = InCustomPresent; } virtual FRHICustomPresent* GetCustomPresent() const override { return CustomPresent; } #endif void Present(FMetalCommandQueue& CommandQueue, bool bLockToVsync); void Swap(); #if PLATFORM_VISIONOS void GetDrawableImmersiveTextures(EMetalViewportAccessFlag Accessor, cp_drawable_t SwiftDrawable, MTL::Texture*& OutColorTexture, MTL::Texture*& OutDepthTexture ); void PresentImmersive(const MetalRHIVisionOS::PresentImmersiveParams* Params); #endif private: uint32 GetViewportIndex(EMetalViewportAccessFlag Accessor) const; private: #if PLATFORM_VISIONOS CP_OBJECT_cp_layer_renderer* SwiftLayer = nullptr; #endif FMetalDevice& Device; CA::MetalDrawable* Drawable; TRefCountPtr BackBuffer[2]; mutable FCriticalSection Mutex; MTL::Texture* DrawableTextures[2]; uint32 DisplayID; FMetalViewportPresentHandler Block; volatile int32 FrameAvailable; TRefCountPtr LastCompleteFrame; bool bIsFullScreen; #if PLATFORM_IOS && !PLATFORM_TVOS && !PLATFORM_VISIONOS uint32 SizeX; uint32 SizeY; EPixelFormat Format; UIInterfaceOrientationMask OrientationMask; UIInterfaceOrientationMask OldOrientationMask; FDelegateHandle OrientationChangedHandle; #endif #if PLATFORM_MAC FMetalView* View; #endif #if PLATFORM_MAC || PLATFORM_VISIONOS FCustomPresentRHIRef CustomPresent; #endif }; template<> struct TMetalResourceTraits { typedef FMetalViewport TConcreteType; };