1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
5 #include "RendererResources.h"
10 #if CRY_PLATFORM_DURANGO
14 //////////////////////////////////////////////////////////////////////////
15 //! Display Context represent a target rendering context
16 //////////////////////////////////////////////////////////////////////////
17 class CRenderDisplayContext
19 friend class CD3D9Renderer
;
20 friend class CRenderOutput
;
21 friend class CRenderView
;
24 using TexSmartPtr
= _smart_ptr
<CTexture
>;
25 using OutputSmartPtr
= std::shared_ptr
<CRenderOutput
>;
28 //! Debug name for this Render Output
30 // Unique id to identify each context
33 // Denotes if context refers to main viewport
34 bool m_bMainViewport
= true;
35 // Description of the DisplayContext
36 IRenderer::SDisplayContextDescription m_desc
;
38 // Number of samples per output (real offscreen) pixel used in X/Y
39 int m_nSSSamplesX
= 1;
40 int m_nSSSamplesY
= 1;
42 // Dimensions of viewport on output to render content into
43 uint32_t m_DisplayWidth
= 0;
44 uint32_t m_DisplayHeight
= 0;
46 // Render output for this display context;
47 OutputSmartPtr m_pRenderOutput
= nullptr;
48 TexSmartPtr m_pColorTarget
= nullptr;
49 TexSmartPtr m_pDepthTarget
= nullptr;
50 float m_aspectRatio
= 1.0f
;
52 SRenderViewport m_viewport
;
55 CRenderDisplayContext(IRenderer::SDisplayContextDescription desc
, std::string name
, uint32 uniqueId
) : m_name(name
), m_uniqueId(uniqueId
), m_desc(desc
)
57 m_pRenderOutput
= std::make_shared
<CRenderOutput
>(this);
60 CRenderDisplayContext(CRenderDisplayContext
&&) = default;
61 CRenderDisplayContext
&operator=(CRenderDisplayContext
&&) = default;
63 virtual void ReleaseResources();
65 void SetDisplayResolutionAndRecreateTargets(uint32_t displayWidth
, uint32_t displayHeight
, const SRenderViewport
& vp
);
67 virtual void ChangeDisplayResolution(uint32_t displayWidth
, uint32_t displayHeight
, const SRenderViewport
& vp
);
68 void ChangeDisplayResolution(uint32_t displayWidth
, uint32_t displayHeight
)
70 ChangeDisplayResolution(displayWidth
, displayHeight
, SRenderViewport(0, 0, displayWidth
, displayHeight
));
74 virtual ~CRenderDisplayContext() noexcept
{}
76 virtual bool IsSwapChainBacked() const { return false; }
78 void BeginRendering();
81 virtual void PrePresent() {}
82 virtual void PostPresent() {}
83 virtual CTexture
* GetCurrentBackBuffer() const = 0;
84 virtual CTexture
* GetPresentedBackBuffer() const = 0;
86 std::string
GetName() const { return m_name
; }
87 uint32_t GetID() const { return m_uniqueId
; }
89 OutputSmartPtr
GetRenderOutput() const { return m_pRenderOutput
; };
90 Vec2_tpl
<uint32_t> GetDisplayResolution() const { return Vec2_tpl
<uint32_t>(m_DisplayWidth
, m_DisplayHeight
); }
91 const SRenderViewport
& GetViewport() const { return m_viewport
; }
92 float GetAspectRatio() const { return m_aspectRatio
; }
94 bool IsMainViewport() const { return m_bMainViewport
; }
95 bool IsMainContext() const { return IsMainViewport(); }
96 bool IsEditorDisplay() const;
97 bool IsScalable() const { return IsMainViewport() && !IsEditorDisplay(); }
98 bool IsDeferredShadeable() const { return IsMainViewport(); }
99 bool IsSuperSamplingEnabled() const { return m_nSSSamplesX
* m_nSSSamplesY
> 1; }
100 bool IsNativeScalingEnabled() const;
101 bool IsHighDynamicRangeDisplay() const { return false; /* CRendererCVars::CV_r_HDRSwapChain */ }
103 bool NeedsTempColor() const { return false; /* TODO: compare formats as well */ }
104 bool NeedsDepthStencil() const { return (m_desc
.renderFlags
& (FRT_OVERLAY_DEPTH
| FRT_OVERLAY_STENCIL
)) != 0; }
106 virtual ETEX_Format
GetColorFormat() const { return CRendererResources::GetDisplayFormat(); }
107 ETEX_Format
GetDepthFormat() const { return CRendererResources::GetDepthFormat(); }
109 // Get a temporary texture pointer (only valid for some short-lived scope
110 CTexture
* GetCurrentColorOutput() const
112 if (NeedsTempColor())
114 CRY_ASSERT(m_pColorTarget
);
115 return m_pColorTarget
.get();
118 return GetCurrentBackBuffer();
121 CTexture
* GetCurrentDepthOutput() const
123 CRY_ASSERT(m_pDepthTarget
|| !NeedsDepthStencil());
124 return m_pDepthTarget
.get();
127 // Get persistent texture pointers which are dirtied when changed
128 virtual CTexture
* GetStorableColorOutput() = 0;
129 inline CTexture
* GetStorableDepthOutput() const { return GetCurrentDepthOutput(); }
132 void AllocateColorTarget();
133 void AllocateDepthTarget();
136 using CRenderDisplayContextPtr
= std::shared_ptr
<CRenderDisplayContext
>;
138 //////////////////////////////////////////////////////////////////////////
139 //! Display Context implemented by a swap-chain
140 //////////////////////////////////////////////////////////////////////////
141 class CSwapChainBackedRenderDisplayContext
: public CRenderDisplayContext
143 friend class CD3D9Renderer
;
146 // Handle and a pointer to WIN32 window
149 CSwapChain m_swapChain
;
150 DXGIOutput
* m_pOutput
= nullptr;
152 std::vector
<TexSmartPtr
> m_backBuffersArray
;
153 CTexture
* m_pBackBufferPresented
= nullptr;
154 TexSmartPtr m_pBackBufferProxy
= nullptr;
155 bool m_bSwapProxy
= true;
156 bool m_fullscreen
= false;
157 bool m_bVSync
= false;
160 void CreateSwapChain(HWND hWnd
, bool vsync
);
163 void ReleaseResources() override
;
164 #if CRY_PLATFORM_WINDOWS
165 void EnforceFullscreenPreemption();
167 void ChangeOutputIfNecessary(bool isFullscreen
, bool vsync
= true);
168 void ChangeDisplayResolution(uint32_t displayWidth
, uint32_t displayHeight
, const SRenderViewport
& vp
) override
;
169 void ChangeDisplayResolution(uint32_t displayWidth
, uint32_t displayHeight
)
171 ChangeDisplayResolution(displayWidth
, displayHeight
, SRenderViewport(0, 0, displayWidth
, displayHeight
));
174 CSwapChain
& GetSwapChain() { return m_swapChain
; }
175 const CSwapChain
& GetSwapChain() const { return m_swapChain
; }
177 template <typename
... Args
>
178 void CreateSwapChain(Args
&&... args
) {
180 m_swapChain
= CSwapChain::CreateSwapChain(std::forward
<Args
>(args
)...);
184 CSwapChainBackedRenderDisplayContext(IRenderer::SDisplayContextDescription desc
, std::string name
, uint32 uniqueId
) : CRenderDisplayContext(desc
, name
, uniqueId
) {}
185 ~CSwapChainBackedRenderDisplayContext() { ShutDown(); }
187 CSwapChainBackedRenderDisplayContext(CSwapChainBackedRenderDisplayContext
&&) = default;
188 CSwapChainBackedRenderDisplayContext
&operator=(CSwapChainBackedRenderDisplayContext
&&) = default;
190 bool IsSwapChainBacked() const override final
{ return true; }
192 void PrePresent() override
;
193 void PostPresent() override
;
195 ETEX_Format
GetColorFormat() const override
{ return GetBackBufferFormat(); };
196 ETEX_Format
GetBackBufferFormat() const;
198 CTexture
* GetCurrentBackBuffer() const override
;
199 CTexture
* GetPresentedBackBuffer() const override
{ return m_pBackBufferPresented
; }
200 CTexture
* GetStorableColorOutput() override
;
202 HWND
GetWindowHandle() const { return m_hWnd
; }
203 //! Gets the resolution of the monitor that this context's window is currently present on
204 RectI
GetCurrentMonitorBounds() const;
206 void SetFullscreenState(bool isFullscreen
);
207 bool IsFullscreen() const { return m_fullscreen
; }
208 bool GetVSyncState() const { return m_bVSync
; }
210 Vec2_tpl
<uint32_t> FindClosestMatchingScreenResolution(const Vec2_tpl
<uint32_t> &resolution
) const;
212 #if defined(SUPPORT_DEVICE_INFO)
213 uint32
GetRefreshRateNumerator() const { return m_swapChain
.GetRefreshRateNumerator(); }
214 uint32
GetRefreshRateDemoninator() const { return m_swapChain
.GetRefreshRateDemoninator(); }
218 void ReleaseBackBuffers();
219 void AllocateBackBuffers();
222 //////////////////////////////////////////////////////////////////////////
223 //! Display Context implemented by custom buffers
224 //////////////////////////////////////////////////////////////////////////
225 class CCustomRenderDisplayContext
: public CRenderDisplayContext
228 uint32_t m_swapChainIndex
= 0;
229 std::vector
<TexSmartPtr
> m_backBuffersArray
;
230 CTexture
* m_pBackBufferPresented
= nullptr;
231 TexSmartPtr m_pBackBufferProxy
= nullptr;
232 bool m_bSwapProxy
= true;
235 void ReleaseResources() override
;
238 // Creates a display context with manual control of the swapchain
239 CCustomRenderDisplayContext(IRenderer::SDisplayContextDescription desc
, std::string name
, uint32 uniqueId
, std::vector
<TexSmartPtr
> &&backBuffersArray
, uint32_t initialSwapChainIndex
= 0);
240 ~CCustomRenderDisplayContext() { ShutDown(); }
242 CCustomRenderDisplayContext(CCustomRenderDisplayContext
&&) = default;
243 CCustomRenderDisplayContext
&operator=(CCustomRenderDisplayContext
&&) = default;
245 void SetSwapChainIndex(uint32_t index
);
247 void PrePresent() override
;
248 void PostPresent() override
;
250 CTexture
* GetCurrentBackBuffer() const override
{ return this->m_backBuffersArray
[m_swapChainIndex
]; }
251 CTexture
* GetPresentedBackBuffer() const override
{ return m_pBackBufferPresented
; }
252 CTexture
* GetStorableColorOutput() override
;