This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
/
SwapChainGraphicsPresenter.cs
299 lines (258 loc) · 11.5 KB
/
SwapChainGraphicsPresenter.cs
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2010-2013 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
#if WIN8METRO
using Windows.UI.Core;
using Windows.Graphics.Display;
using Windows.UI.Xaml.Controls;
#elif WP8
#else
using System.Windows.Forms;
#endif
using SharpDX.DXGI;
namespace SharpDX.Toolkit.Graphics
{
/// <summary>
/// Graphics presenter for SwapChain.
/// </summary>
public class SwapChainGraphicsPresenter : GraphicsPresenter
{
private RenderTarget2D backBuffer;
private SwapChain swapChain;
private int bufferCount;
public SwapChainGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters)
: base(device, presentationParameters)
{
PresentInterval = presentationParameters.PresentationInterval;
// Initialize the swap chain
swapChain = ToDispose(CreateSwapChain());
backBuffer = ToDispose(RenderTarget2D.New(device, swapChain.GetBackBuffer<Direct3D11.Texture2D>(0)));
}
public override RenderTarget2D BackBuffer
{
get
{
return backBuffer;
}
}
public override object NativePresenter
{
get
{
return swapChain;
}
}
public override bool IsFullScreen
{
get
{
#if WIN8METRO
return true;
#else
return swapChain.IsFullScreen;
#endif
}
set
{
#if !WIN8METRO
var outputIndex = PrefferedFullScreenOutputIndex;
// no outputs connected to the current graphics adapter
var output = GraphicsDevice.Adapter == null || GraphicsDevice.Adapter.OutputsCount == 0 ? null : GraphicsDevice.Adapter.GetOutputAt(outputIndex);
Output currentOutput = null;
try
{
Bool isCurrentlyFullscreen;
swapChain.GetFullscreenState(out isCurrentlyFullscreen, out currentOutput);
// check if the current fullscreen monitor is the same as new one
if (isCurrentlyFullscreen == value && output != null && currentOutput != null && currentOutput.NativePointer == ((Output)output).NativePointer)
return;
}
finally
{
if (currentOutput != null)
currentOutput.Dispose();
}
bool switchToFullScreen = value;
// If going to fullscreen mode: call 1) SwapChain.ResizeTarget 2) SwapChain.IsFullScreen
var description = new ModeDescription(backBuffer.Width, backBuffer.Height, Description.RefreshRate, Description.BackBufferFormat);
if(switchToFullScreen)
{
Description.IsFullScreen = true;
// Destroy and recreate the full swapchain in case of fullscreen switch
// It seems to be more reliable then trying to change the current swap-chain.
RemoveAndDispose(ref backBuffer);
RemoveAndDispose(ref swapChain);
swapChain = CreateSwapChain();
backBuffer = ToDispose(RenderTarget2D.New(GraphicsDevice, swapChain.GetBackBuffer<Direct3D11.Texture2D>(0)));
}
else
{
Description.IsFullScreen = false;
swapChain.IsFullScreen = false;
// call 1) SwapChain.IsFullScreen 2) SwapChain.Resize
Resize(backBuffer.Width, backBuffer.Height, backBuffer.Format);
}
// If going to window mode:
if (!switchToFullScreen)
{
// call 1) SwapChain.IsFullScreen 2) SwapChain.Resize
description.RefreshRate = new Rational(0, 0);
swapChain.ResizeTarget(ref description);
}
#endif
}
}
public override void Present()
{
swapChain.Present((int)PresentInterval, PresentFlags.None);
}
/// <summary>
/// Called when name changed for this component.
/// </summary>
protected override void OnPropertyChanged(string propertyName)
{
base.OnPropertyChanged(propertyName);
if (propertyName == "Name")
{
if (GraphicsDevice.IsDebugMode && swapChain != null)
{
swapChain.DebugName = Name;
}
}
}
public override bool Resize(int width, int height, Format format, Rational? refreshRate = null)
{
if (!base.Resize(width, height, format, refreshRate)) return false;
RemoveAndDispose(ref backBuffer);
swapChain.ResizeBuffers(bufferCount, width, height, format, Description.Flags);
// Recreate the back buffer
backBuffer = ToDispose(RenderTarget2D.New(GraphicsDevice, swapChain.GetBackBuffer<Direct3D11.Texture2D>(0)));
// Reinit the Viewport
DefaultViewport = new ViewportF(0, 0, backBuffer.Width, backBuffer.Height);
return true;
}
private SwapChain CreateSwapChain()
{
// Check for Window Handle parameter
if (Description.DeviceWindowHandle == null)
{
throw new ArgumentException("DeviceWindowHandle cannot be null");
}
#if WIN8METRO
return CreateSwapChainForWinRT();
#else
return CreateSwapChainForDesktop();
#endif
}
#if WIN8METRO
private SwapChain CreateSwapChainForWinRT()
{
var coreWindow = Description.DeviceWindowHandle as CoreWindow;
var swapChainBackgroundPanel = Description.DeviceWindowHandle as SwapChainBackgroundPanel;
bufferCount = 2;
var description = new SwapChainDescription1
{
// Automatic sizing
Width = Description.BackBufferWidth,
Height = Description.BackBufferHeight,
Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, // TODO: Check if we can use the Description.BackBufferFormat
Stereo = false,
SampleDescription = new SharpDX.DXGI.SampleDescription((int)Description.MultiSampleCount, 0),
Usage = Description.RenderTargetUsage,
// Use two buffers to enable flip effect.
BufferCount = bufferCount,
Scaling = SharpDX.DXGI.Scaling.Stretch,
SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential,
};
if (coreWindow != null)
{
// Creates a SwapChain from a CoreWindow pointer
using (var comWindow = new ComObject(coreWindow))
return new SwapChain1((DXGI.Factory2)GraphicsAdapter.Factory, (Direct3D11.Device)GraphicsDevice, comWindow, ref description);
}
else if (swapChainBackgroundPanel != null)
{
var nativePanel = ComObject.As<ISwapChainBackgroundPanelNative>(swapChainBackgroundPanel);
// Creates the swap chain for XAML composition
var swapChain = new SwapChain1((DXGI.Factory2)GraphicsAdapter.Factory, (Direct3D11.Device)GraphicsDevice, ref description);
// Associate the SwapChainBackgroundPanel with the swap chain
nativePanel.SwapChain = swapChain;
return swapChain;
}
else
{
#if DIRECTX11_2
using (var comObject = new ComObject(Description.DeviceWindowHandle))
{
var swapChainPanel = comObject.QueryInterfaceOrNull<ISwapChainPanelNative>();
if (swapChainPanel != null)
{
var swapChain = new SwapChain1((DXGI.Factory2)GraphicsAdapter.Factory, (Direct3D11.Device)GraphicsDevice, ref description);
swapChainPanel.SwapChain = swapChain;
return swapChain;
}
}
#endif
throw new NotSupportedException();
}
}
#elif WP8
private SwapChain CreateSwapChainForDesktop()
{
throw new NotImplementedException();
}
#else
private SwapChain CreateSwapChainForDesktop()
{
IntPtr? handle = null;
var control = Description.DeviceWindowHandle as Control;
if (control != null) handle = control.Handle;
else if (Description.DeviceWindowHandle is IntPtr) handle = (IntPtr)Description.DeviceWindowHandle;
if (!handle.HasValue)
{
throw new NotSupportedException(string.Format("DeviceWindowHandle of type [{0}] is not supported. Only System.Windows.Control or IntPtr are supported", Description.DeviceWindowHandle != null ? Description.DeviceWindowHandle.GetType().Name : "null"));
}
bufferCount = 1;
var description = new SwapChainDescription
{
ModeDescription = new ModeDescription(Description.BackBufferWidth, Description.BackBufferHeight, Description.RefreshRate, Description.BackBufferFormat),
BufferCount = bufferCount, // TODO: Do we really need this to be configurable by the user?
OutputHandle = handle.Value,
SampleDescription = new SampleDescription((int)Description.MultiSampleCount, 0),
SwapEffect = SwapEffect.Discard,
Usage = Description.RenderTargetUsage,
IsWindowed = true,
Flags = Description.Flags,
};
var newSwapChain = new SwapChain(GraphicsAdapter.Factory, (Direct3D11.Device)GraphicsDevice, description);
if (Description.IsFullScreen)
{
// Before fullscreen switch
newSwapChain.ResizeTarget(ref description.ModeDescription);
// Switch to full screen
newSwapChain.IsFullScreen = true;
// This is really important to call ResizeBuffers AFTER switching to IsFullScreen
newSwapChain.ResizeBuffers(bufferCount, Description.BackBufferWidth, Description.BackBufferHeight, Description.BackBufferFormat, SwapChainFlags.AllowModeSwitch);
}
return newSwapChain;
}
#endif
}
}