-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSampleAppUWP.cpp
261 lines (225 loc) · 9.08 KB
/
SampleAppUWP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#include "SampleApp.hpp"
#include "ImguiUWPEventHelper.h"
#include "InputControllerEventHandlerUWP.h"
#include "RenderDeviceD3D12.h"
#include "RenderDeviceD3D11.h"
#include "SwapChainD3D12.h"
#include "SwapChainD3D11.h"
#include "EngineFactoryD3D11.h"
#include "EngineFactoryD3D12.h"
#include "ImGuiImplUWP.hpp"
namespace Diligent
{
class SampleAppUWP final : public SampleApp
{
public:
SampleAppUWP()
{
m_DeviceType = RENDER_DEVICE_TYPE_D3D12;
}
virtual void OnSetWindow(Windows::UI::Core::CoreWindow^ window)override final
{
m_ImguiEventHandler = ImguiUWPEventHelper::Create(window);
m_InputControllerEventHandlerUWP = InputControllerEventHandlerUWP::Create(window, m_TheSample->GetInputController().GetSharedState());
}
virtual void OnWindowSizeChanged()override final
{
if (m_SampleInitialized)
{
m_TheSample->PreWindowResize();
}
InitWindowSizeDependentResources();
if (m_SampleInitialized)
{
const auto& SCDesc = m_pSwapChain->GetDesc();
m_TheSample->WindowResize(SCDesc.Width, SCDesc.Height);
}
}
virtual void Render()override
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}
SampleApp::Render();
m_bFrameReady = true;
}
// Notifies the app that it is being suspended.
virtual void OnSuspending()override final
{
// TODO: Replace this with your app's suspending logic.
// Process lifetime management may terminate suspended apps at any time, so it is
// good practice to save any state that will allow the app to restart where it left off.
//m_sceneRenderer->SaveState();
// If your application uses video memory allocations that are easy to re-create,
// consider releasing that memory to make it available to other applications.
}
// Notifies the app that it is no longer suspended.
virtual void OnResuming()override final
{
// TODO: Replace this with your app's resuming logic.
}
// Notifies renderers that device resources need to be released.
virtual void OnDeviceRemoved()override final
{
// TODO: Save any necessary application or renderer state and release the renderer
// and its resources which are no longer valid.
//m_sceneRenderer->SaveState();
m_TheSample.reset();
}
virtual void Present()override
{
if (m_pSwapChain)
m_pSwapChain->Present();
//// If the device was removed either by a disconnection or a driver upgrade, we
//// must recreate all device resources.
//if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
//{
// m_deviceRemoved = true;
//}
//else
//{
// DX::ThrowIfFailed(hr);
//}
}
virtual std::shared_ptr<DX::DeviceResources> InitDeviceResources()override
{
try
{
InitializeDiligentEngine(nullptr);
ID3D12Device *pd3d12Device = nullptr;
ID3D11Device *pd3d11Device = nullptr;
if (m_DeviceType == RENDER_DEVICE_TYPE_D3D12)
{
// Store pointers to the Direct3D 11.1 API device and immediate context.
RefCntAutoPtr<IRenderDeviceD3D12> pRenderDeviceD3D12(m_pDevice, IID_RenderDeviceD3D12);
pd3d12Device = pRenderDeviceD3D12->GetD3D12Device();
}
else if (m_DeviceType == RENDER_DEVICE_TYPE_D3D11)
{
RefCntAutoPtr<IRenderDeviceD3D11> pRenderDeviceD3D11(m_pDevice, IID_RenderDeviceD3D11);
pd3d11Device = pRenderDeviceD3D11->GetD3D11Device();
}
else
{
UNEXPECTED("Unexpected device type");
}
m_DeviceResources = std::make_shared<DX::DeviceResources>(pd3d11Device, pd3d12Device);
}
catch(...)
{
LOG_ERROR("Failed to initialize Diligent Engine.");
}
return m_DeviceResources;
}
virtual void InitWindowSizeDependentResources()override
{
if (!m_DeviceResources)
return;
m_DeviceResources->UpdateRenderTargetSize();
auto backBufferWidth = m_DeviceResources->GetBackBufferWidth();
auto backBufferHeight = m_DeviceResources->GetBackBufferHeight();
if (m_swapChain != nullptr)
{
m_swapChain.Reset();
// If the swap chain already exists, resize it.
m_pSwapChain->Resize(backBufferWidth, backBufferHeight);
#if 0
if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
{
// If the device was removed for any reason, a new device and swap chain will need to be created.
m_deviceRemoved = true;
// Do not continue execution of this method. DeviceResources will be destroyed and re-created.
return;
}
else
{
DX::ThrowIfFailed(hr);
}
#endif
}
else
{
//DXGI_SCALING scaling = DisplayMetrics::SupportHighResolutions ? DXGI_SCALING_NONE : DXGI_SCALING_STRETCH;
SwapChainDesc SCDesc;
SCDesc.Width = backBufferWidth;
SCDesc.Height = backBufferHeight;
SCDesc.ColorBufferFormat = TEX_FORMAT_RGBA8_UNORM_SRGB;
SCDesc.DepthBufferFormat = TEX_FORMAT_D32_FLOAT;
auto window = m_DeviceResources->GetWindow();
UWPNativeWindow UWPWindow{reinterpret_cast<IUnknown*>(window)};
IDXGISwapChain3 *pDXGISwapChain3 = nullptr;
if (m_DeviceType == RENDER_DEVICE_TYPE_D3D12)
{
RefCntAutoPtr<IEngineFactoryD3D12> pFactoryD3D12{m_pEngineFactory, IID_EngineFactoryD3D12};
pFactoryD3D12->CreateSwapChainD3D12(m_pDevice, GetImmediateContext(), SCDesc, FullScreenModeDesc{}, UWPWindow, &m_pSwapChain);
}
else if (m_DeviceType == RENDER_DEVICE_TYPE_D3D11)
{
RefCntAutoPtr<IEngineFactoryD3D11> pFactoryD3D11{m_pEngineFactory, IID_EngineFactoryD3D11};
pFactoryD3D11->CreateSwapChainD3D11(m_pDevice, GetImmediateContext(), SCDesc, FullScreenModeDesc{}, UWPWindow, &m_pSwapChain);
}
else
UNEXPECTED("Unexpected device type");
}
IDXGISwapChain3 *pDXGISwapChain3 = nullptr;
if (m_DeviceType == RENDER_DEVICE_TYPE_D3D12)
{
RefCntAutoPtr<ISwapChainD3D12> pSwapChainD3D12(m_pSwapChain, IID_SwapChainD3D12);
pSwapChainD3D12->GetDXGISwapChain()->QueryInterface(__uuidof(pDXGISwapChain3), reinterpret_cast<void**>(&pDXGISwapChain3));
}
else if (m_DeviceType == RENDER_DEVICE_TYPE_D3D11)
{
RefCntAutoPtr<ISwapChainD3D11> pSwapChainD3D11(m_pSwapChain, IID_SwapChainD3D11);
pSwapChainD3D11->GetDXGISwapChain()->QueryInterface(__uuidof(pDXGISwapChain3), reinterpret_cast<void**>(&pDXGISwapChain3));
}
else
UNEXPECTED("Unexpected device type");
m_swapChain.Attach(pDXGISwapChain3);
m_DeviceResources->SetSwapChainRotation(m_swapChain.Get());
}
virtual void CreateRenderers()override
{
if (!m_DeviceResources)
return;
const auto& SCDesc = m_pSwapChain->GetDesc();
m_pImGui = ImGuiImplUWP::Create(ImGuiDiligentCreateInfo{m_pDevice, SCDesc});
InitializeSample();
m_SampleInitialized = true;
}
private:
ImguiUWPEventHelper^ m_ImguiEventHandler;
InputControllerEventHandlerUWP^ m_InputControllerEventHandlerUWP;
Microsoft::WRL::ComPtr<IDXGISwapChain3> m_swapChain;
bool m_SampleInitialized = false;
};
NativeAppBase* CreateApplication()
{
return new SampleAppUWP;
}
}