UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
D3D12Viewport.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*=============================================================================
4 D3D12Viewport.h: D3D viewport RHI definitions.
5=============================================================================*/
6
7#pragma once
8
9#include "D3D12RHICommon.h"
10#include "D3D12Texture.h"
11#include "HAL/Runnable.h"
12#include "MultiGPU.h"
13#include "RHIResources.h"
15#include "DXGIUtilities.h"
16
17class FD3D12Texture;
19
20class FD3D12SyncPoint;
22
24{
25public:
26
27 // Lock viewport windows association and back buffer destruction because of possible crash inside DXGI factory during a call to MakeWindowAssociation
28 // Backbuffer release will wait on the call to MakeWindowAssociation while this will fail internally with 'The requested operation is not implemented.' in KernelBase.dll
29 // Reported & known problem in DXGI and will be fixed with future release but DXGI is not part of the Agility SDK so a code side fix is needed for now.
31
33
34 void Init();
35
37
39
46
50 bool Present(class FD3D12CommandContextBase& Context, bool bLockToVsync);
51
52 // Accessors.
54 {
55 return FIntPoint(SizeX, SizeY);
56 }
57
59 {
61#if D3D12RHI_USE_DUMMY_BACKBUFFER
63#else
64 checkSlow(CurrentBackBuffer_RenderThread);
65 return CurrentBackBuffer_RenderThread->Texture;
66#endif
67 }
68
69#if D3D12RHI_SUPPORTS_UAV_BACKBUFFER
71 {
72 checkSlow(CurrentBackBuffer_RenderThread);
73 return CurrentBackBuffer_RenderThread->UAV;
74 }
75#endif
76
78 {
79 checkSlow(CurrentBackBuffer_RHIThread);
80 return CurrentBackBuffer_RHIThread->Texture;
81 }
82
84 {
85 checkSlow(CurrentBackBuffer_RHIThread);
86
87#if D3D12RHI_USE_SDR_BACKBUFFER
88 if (PixelFormat != SDRPixelFormat)
89 {
90 return CurrentBackBuffer_RHIThread->TextureSDR;
91 }
92#endif
93
94 return CurrentBackBuffer_RHIThread->Texture;
95 }
96
97#if D3D12RHI_USE_SDR_BACKBUFFER
99 {
100 for (const FBackBufferData& CurBackBuffer : BackBuffers)
101 {
102 if ( (FRHITexture*)CurBackBuffer.Texture.GetReference() == BackBufferTex)
103 {
104 return (FRHITexture*)CurBackBuffer.TextureSDR.GetReference();
105 }
106 }
107
108 return nullptr;
109 }
110#endif
111
112
113#if WITH_MGPU
115 {
117 return BackBuffers[ExpectedBackBufferIndex_RenderThread].GPUIndex;
118 }
119#endif // WITH_MGPU
120
121 virtual void WaitForFrameEventCompletion() override;
122 virtual void IssueFrameEvent() override;
123
124#if D3D12_VIEWPORT_EXPOSES_SWAP_CHAIN
125 virtual void* GetNativeSwapChain() const override;
126#endif // #if D3D12_VIEWPORT_EXPOSES_SWAP_CHAIN
127
128 virtual void* GetNativeBackBufferTexture() const override;
129 virtual void* GetNativeBackBufferRT() const override;
130
131 virtual void SetCustomPresent(FRHICustomPresent* InCustomPresent) override;
132 virtual FRHICustomPresent* GetCustomPresent() const override;
133
134 virtual void* GetNativeWindow(void** AddParam = nullptr) const override { return (void*)WindowHandle; }
135
136 uint32 GetNumBackBuffers() const { return NumBackBuffers; }
137
138 inline const bool IsFullscreen() const { return bIsFullscreen; }
139
141 bool CurrentOutputSupportsHDR() const;
142
145
146 void OnResumeRendering();
147 void OnSuspendRendering();
148
149private:
150 bool IsPresentAllowed();
151
152 FString GetStateString();
153
154#if D3D12RHI_USE_DUMMY_BACKBUFFER
156#endif
157
162 bool PresentChecked(IRHICommandContext& RHICmdContext, int32 SyncInterval);
163
168 HRESULT PresentInternal(int32 SyncInterval);
169
170 void ResizeInternal();
171 void FinalDestroyInternal();
172 void ClearPresentQueue();
173
174 // Determine how deep the swapchain should be
175 void InitializeBackBufferArrays();
176
178 bool CheckHDRSupport();
179
181 void EnableHDR();
182
184 void ShutdownHDR();
185
186#if D3D12RHI_USE_DXGI_COLOR_SPACE
189#endif
190
191 void SetBackBufferIndex_RHIThread(uint32 Index)
192 {
193 CurrentBackBufferIndex_RHIThread = Index % NumBackBuffers;
194 CurrentBackBuffer_RHIThread = &BackBuffers[CurrentBackBufferIndex_RHIThread];
195 }
196
197 void SetBackBufferIndex_RenderThread(uint32 Index)
198 {
199 ExpectedBackBufferIndex_RenderThread = Index % NumBackBuffers;
200 CurrentBackBuffer_RenderThread = &BackBuffers[ExpectedBackBufferIndex_RenderThread];
201 }
202
203private:
204 const HWND WindowHandle;
205 uint32 SizeX = 0;
206 uint32 SizeY = 0;
207 EPixelFormat PixelFormat;
208#if D3D12RHI_USE_SDR_BACKBUFFER
209 static constexpr EPixelFormat SDRPixelFormat = PF_B8G8R8A8;
210#endif
211
212 bool bIsFullscreen = false;
213 bool bFullscreenLost = false;
214 bool bIsValid = true;
215 bool bAllowTearing = true;
216 bool bNeedSwapChain = false;
217
218 uint32 CheckedPresentFailureCounter = 0;
219
220#if D3D12_VIEWPORT_EXPOSES_SWAP_CHAIN
222#if DXGI_MAX_SWAPCHAIN_INTERFACE >= 2
224#endif
225#if DXGI_MAX_SWAPCHAIN_INTERFACE >= 3
227#endif
228#if DXGI_MAX_SWAPCHAIN_INTERFACE >= 4
230#endif
231
232#if D3D12RHI_USE_DXGI_COLOR_SPACE
234#endif
235#endif // D3D12_VIEWPORT_EXPOSES_SWAP_CHAIN
236
237 struct FBackBufferData
238 {
240#if D3D12RHI_USE_SDR_BACKBUFFER
241 // When HDR is enabled, SDR backbuffers may be required on some architectures for game DVR or broadcasting
243#endif
244#if D3D12RHI_SUPPORTS_UAV_BACKBUFFER
246#endif
247#if WITH_MGPU
248 uint32 GPUIndex = 0;
249#endif
250 };
251
252 uint32 NumBackBuffers = 0;
254
255#if D3D12RHI_USE_DUMMY_BACKBUFFER
256 // Dummy back buffer texture which always references the current back buffer on the RHI thread
258#endif
259
260 FBackBufferData* CurrentBackBuffer_RHIThread = nullptr;
261 FBackBufferData* CurrentBackBuffer_RenderThread = nullptr;
262
263 uint32 CurrentBackBufferIndex_RHIThread = 0;
264 uint32 ExpectedBackBufferIndex_RenderThread = 0;
267
269 TArray<FD3D12SyncPointRef> FrameSyncPoints;
270
271 FCustomPresentRHIRef CustomPresent;
272
273#if WITH_MGPU
274 // Where INDEX_NONE cycles through the GPU, otherwise the GPU index.
276
277 // Can very rarely be modified on the RHI thread as well if present is skipped
279#endif
280};
281
282template<>
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
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
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
EDisplayColorGamut
Definition GenericPlatformMisc.h:518
EDisplayOutputFormat
Definition GenericPlatformMisc.h:499
FInt32Point FIntPoint
Definition MathFwd.h:124
EPixelFormat
Definition PixelFormat.h:16
@ PF_B8G8R8A8
Definition PixelFormat.h:19
CORE_API bool IsInRenderingThread()
Definition ThreadingBase.cpp:273
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
void EnsureColorSpace(IDXGISwapChain *SwapChain, EDisplayColorGamut DisplayGamut, EDisplayOutputFormat OutputDevice, EPixelFormat PixelFormat)
Definition WindowsD3D11Viewport.cpp:322
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition D3D12RHICommon.h:54
Definition D3D12Adapter.h:136
Definition D3D12CommandContext.h:479
Definition D3D12Submission.h:76
Definition D3D12Texture.h:31
Definition D3D12View.h:506
Definition D3D12Viewport.h:24
void ConditionalResetSwapChain(bool bIgnoreFocus)
Definition WindowsD3D12Viewport.cpp:188
FD3D12Texture * GetSDRBackBuffer_RHIThread() const
Definition D3D12Viewport.h:83
virtual void SetCustomPresent(FRHICustomPresent *InCustomPresent) override
Definition D3D12Viewport.cpp:466
uint32 GetNumBackBuffers() const
Definition D3D12Viewport.h:136
void OnSuspendRendering()
Definition WindowsD3D12Viewport.cpp:540
virtual void IssueFrameEvent() override
Definition D3D12Viewport.cpp:666
void OnResumeRendering()
Definition WindowsD3D12Viewport.cpp:536
~FD3D12Viewport()
Definition D3D12Viewport.cpp:217
virtual void * GetNativeBackBufferTexture() const override
Definition D3D12Viewport.cpp:456
virtual FRHICustomPresent * GetCustomPresent() const override
Definition D3D12Viewport.cpp:471
bool CurrentOutputSupportsHDR() const
Definition WindowsD3D12Viewport.cpp:426
void Init()
Definition WindowsD3D12Viewport.cpp:43
FIntPoint GetSizeXY() const
Definition D3D12Viewport.h:53
void Resize(uint32 InSizeX, uint32 InSizeY, bool bInIsFullscreen, EPixelFormat PreferredPixelFormat)
Definition D3D12Viewport.cpp:292
virtual void * GetNativeSwapChain() const override
Definition D3D12Viewport.cpp:450
virtual void WaitForFrameEventCompletion() override
Definition D3D12Viewport.cpp:650
static FCriticalSection DXGIBackBufferLock
Definition D3D12Viewport.h:30
virtual void * GetNativeWindow(void **AddParam=nullptr) const override
Definition D3D12Viewport.h:134
void AdvanceExpectedBackBufferIndex_RenderThread()
Definition D3D12Viewport.cpp:477
const bool IsFullscreen() const
Definition D3D12Viewport.h:138
FD3D12Texture * GetBackBuffer_RenderThread() const
Definition D3D12Viewport.h:58
virtual void * GetNativeBackBufferRT() const override
Definition D3D12Viewport.cpp:461
FD3D12Texture * GetBackBuffer_RHIThread() const
Definition D3D12Viewport.h:77
Definition RHIResources.h:4294
Definition RHIResources.h:2153
Definition RHIResources.h:2515
virtual FRHITexture * GetOptionalSDRBackBuffer(FRHITexture *BackBuffer) const
Definition RHIResources.h:2560
Definition ScopeLock.h:141
Definition RHIContext.h:693
Definition Array.h:670
HWND__ * HWND
Definition MinimalWindowsApi.h:81
U16 Index
Definition radfft.cpp:71
FD3D12Viewport TConcreteType
Definition D3D12Viewport.h:285
Definition D3D12RHICommon.h:374
Definition IntPoint.h:25