This repository was archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
151 lines (119 loc) · 5.98 KB
/
MainPage.xaml.cs
File metadata and controls
151 lines (119 loc) · 5.98 KB
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
using SharpDX.DXGI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using SharpDX;
using SharpDX.Direct3D11;
using Windows.ApplicationModel;
using System.Text;
using System.Diagnostics;
namespace UWPSwapchain
{
public sealed partial class MainPage : Page
{
SharpDX.Direct3D11.Device device;
SharpDX.Direct3D11.DeviceContext deviceContext;
SharpDX.DXGI.SwapChain1 swapchain;
Texture2D backBufferTexture;
RenderTargetView backBufferView;
Stopwatch sw = Stopwatch.StartNew();
public MainPage()
{
this.InitializeComponent();
bool enableDebug = false;
DeviceCreationFlags flags = DeviceCreationFlags.BgraSupport;
if (enableDebug)
flags |= DeviceCreationFlags.Debug;
this.device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, flags);
this.deviceContext = this.device.ImmediateContext;
}
private void CompositionTarget_Rendering(object sender, object e)
{
SharpDX.Color4 c = Color.Red;
c.Red = (float)Math.Abs(Math.Sin(sw.Elapsed.TotalSeconds));
//This is your render call, perform all render tasks and then call present on the swap chain
this.deviceContext.ClearRenderTargetView(this.backBufferView, c);
//Attach render target and set viewport
this.deviceContext.OutputMerger.SetRenderTargets(this.backBufferView);
this.deviceContext.Rasterizer.SetViewport(new ViewportF(0.0f, 0.0f, this.backBufferTexture.Description.Width, this.backBufferTexture.Description.Height, 0.0f, 1.0f));
//perform draw calls here
this.swapchain.Present(0, PresentFlags.None);
}
private void panel_Loaded(object sender, RoutedEventArgs e)
{
//This is trigerred when panel is loaded, you can now create your swap chain.
//Please note : Do not use this.panel.Width, make sure to use render size as width property returns NaN on create
SwapChainDescription1 swapChainDescription = new SwapChainDescription1()
{
AlphaMode = AlphaMode.Ignore,
BufferCount = 2,
Format = Format.R8G8B8A8_UNorm,
Height = (int)(this.panel.RenderSize.Height),
Width = (int)(this.panel.RenderSize.Width),
SampleDescription = new SampleDescription(1, 0),
Scaling = SharpDX.DXGI.Scaling.Stretch,
Stereo = false,
SwapEffect = SwapEffect.FlipSequential,
Usage = Usage.RenderTargetOutput
};
using (SharpDX.DXGI.Device3 dxgiDevice3 = this.device.QueryInterface<SharpDX.DXGI.Device3>())
{
using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent<SharpDX.DXGI.Factory3>())
{
SharpDX.DXGI.SwapChain1 swapChain1 = new SharpDX.DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription);
this.swapchain = swapChain1;
}
}
using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = SharpDX.ComObject.As<SharpDX.DXGI.ISwapChainPanelNative>(this.panel))
{
nativeObject.SwapChain = this.swapchain;
}
this.backBufferTexture = this.swapchain.GetBackBuffer<Texture2D>(0);
this.backBufferView = new RenderTargetView(this.device, this.backBufferTexture);
CompositionTarget.Rendering += CompositionTarget_Rendering;
Application.Current.Suspending += Current_Suspending;
}
private void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
//This is the only part where you will receive a "close event", you should serialize any state so app can resume correctly.
//Please note that this method has a time out (e.SuspendingOperation.Deadline), if you do not return prior to this time, application will terminate instead of being suspended.
if (this.swapchain != null)
{
this.deviceContext.ClearState();
using (SharpDX.DXGI.Device3 dxgiDevice3 = this.device.QueryInterface<SharpDX.DXGI.Device3>())
dxgiDevice3.Trim();
}
}
private void panel_Unloaded(object sender, RoutedEventArgs e)
{
//On uwp windows 10 , this is never called, quitting the app does not do anything on it
}
private void panel_SizeChanged(object sender, SizeChangedEventArgs e)
{
/* Resize swap chain, to do it cleanly, we clear state (to ensure resources are not bound to pipeline, and
* dispose associated resources (eg : render view and texture object)
* Once done, we can call resizebuffers, and create direct3d11 objects again */
if (this.swapchain != null)
{
this.deviceContext.ClearState();
Size2 newSize = new Size2((int)e.NewSize.Width, (int)e.NewSize.Height);
Utilities.Dispose(ref this.backBufferView);
Utilities.Dispose(ref this.backBufferTexture);
this.swapchain.ResizeBuffers(this.swapchain.Description.BufferCount, (int)e.NewSize.Width, (int)e.NewSize.Height, swapchain.Description1.Format, swapchain.Description1.Flags);
this.backBufferTexture = this.swapchain.GetBackBuffer<Texture2D>(0);
this.backBufferView = new RenderTargetView(this.device, this.backBufferTexture);
}
}
}
}