30 {
31 try
32 {
33
34 VerticalSyncEnabled = DSystemConfiguration.VerticalSyncEnabled;
35
36
37 var factory = new Factory1();
38
39
40 var adapter = factory.GetAdapter1(0);
41
42
43 var monitor = adapter.GetOutput(0);
44
45
46 var modes = monitor.GetDisplayModeList(Format.R8G8B8A8_UNorm, DisplayModeEnumerationFlags.Interlaced);
47
48
49
50
51 var rational = new Rational(0, 1);
52 if (VerticalSyncEnabled)
53 {
54 foreach (var mode in modes)
55 {
56 if (mode.Width == configuration.Width && mode.Height == configuration.Height)
57 {
58 rational = new Rational(mode.RefreshRate.Numerator, mode.RefreshRate.Denominator);
59 break;
60 }
61 }
62 }
63
64
65 var adapterDescription = adapter.Description;
66
67
68
69
70
71 VideoCardDescription = adapterDescription.Description.Trim('\0');
72
73
74 monitor.Dispose();
75
76 adapter.Dispose();
77
78 factory.Dispose();
79
80
81 var swapChainDesc = new SwapChainDescription()
82 {
83
84 BufferCount = 1,
85
86 ModeDescription = new ModeDescription(configuration.Width, configuration.Height, rational, Format.R8G8B8A8_UNorm),
87
88 Usage = Usage.RenderTargetOutput,
89
90 OutputHandle = windowHandle,
91
92 SampleDescription = new SampleDescription(1, 0),
93
94 IsWindowed = !DSystemConfiguration.FullScreen,
95
96 Flags = SwapChainFlags.None,
97
98 SwapEffect = SwapEffect.Discard
99 };
100
101
102 SharpDX.Direct3D11.Device device;
103 SwapChain swapChain;
104 SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain);
105
106 Device = device;
107 SwapChain = swapChain;
108 DeviceContext = device.ImmediateContext;
109
110
111 var backBuffer = Texture2D.FromSwapChain<Texture2D>(SwapChain, 0);
112
113
114 RenderTargetView = new RenderTargetView(device, backBuffer);
115
116
117 backBuffer.Dispose();
118
119
120 Texture2DDescription depthBufferDesc = new Texture2DDescription()
121 {
122 Width = configuration.Width,
123 Height = configuration.Height,
124 MipLevels = 1,
125 ArraySize = 1,
126 Format = Format.D24_UNorm_S8_UInt,
127 SampleDescription = new SampleDescription(1, 0),
128 Usage = ResourceUsage.Default,
129 BindFlags = BindFlags.DepthStencil,
130 CpuAccessFlags = CpuAccessFlags.None,
131 OptionFlags = ResourceOptionFlags.None
132 };
133
134
135 DepthStencilBuffer = new Texture2D(device, depthBufferDesc);
136
137
138 DepthStencilStateDescription depthStencilDesc = new DepthStencilStateDescription()
139 {
140 IsDepthEnabled = true,
141 DepthWriteMask = DepthWriteMask.All,
142 DepthComparison = Comparison.Less,
143 IsStencilEnabled = true,
144 StencilReadMask = 0xFF,
145 StencilWriteMask = 0xFF,
146
147 FrontFace = new DepthStencilOperationDescription()
148 {
149 FailOperation = StencilOperation.Keep,
150 DepthFailOperation = StencilOperation.Increment,
151 PassOperation = StencilOperation.Keep,
152 Comparison = Comparison.Always
153 },
154
155 BackFace = new DepthStencilOperationDescription()
156 {
157 FailOperation = StencilOperation.Keep,
158 DepthFailOperation = StencilOperation.Decrement,
159 PassOperation = StencilOperation.Keep,
160 Comparison = Comparison.Always
161 }
162 };
163
164
165 DepthStencilState = new DepthStencilState(Device, depthStencilDesc);
166
167
168 DeviceContext.OutputMerger.SetDepthStencilState(DepthStencilState, 1);
169
170
171 DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
172 {
173 Format = Format.D24_UNorm_S8_UInt,
174 Dimension = DepthStencilViewDimension.Texture2D,
175 Texture2D = new DepthStencilViewDescription.Texture2DResource()
176 {
177 MipSlice = 0
178 }
179 };
180
181
182 DepthStencilView = new DepthStencilView(Device, DepthStencilBuffer, depthStencilViewDesc);
183
184
185 DeviceContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
186
187
188 RasterizerStateDescription rasterDesc = new RasterizerStateDescription()
189 {
190 IsAntialiasedLineEnabled = false,
191 CullMode = CullMode.Back,
192 DepthBias = 0,
193 DepthBiasClamp = .0f,
194 IsDepthClipEnabled = false,
195 FillMode = FillMode.Solid,
196 IsFrontCounterClockwise = false,
197 IsMultisampleEnabled = false,
198 IsScissorEnabled = false,
199 SlopeScaledDepthBias = .0f
200 };
201
202
203 RasterState = new RasterizerState(Device, rasterDesc);
204
205
206 DeviceContext.Rasterizer.State = RasterState;
207
208
209 DeviceContext.Rasterizer.SetViewport(0, 0, configuration.Width, configuration.Height, 0, 1);
210
211
212 ProjectionMatrix = Matrix.PerspectiveFovLH((float)(Math.PI / 4), ((float)configuration.Width / (float)configuration.Height), DSystemConfiguration.ScreenNear, DSystemConfiguration.ScreenDepth);
213
214
215 WorldMatrix = Matrix.Identity;
216
217 BlendStateDescription blendStateDescription = new BlendStateDescription
218 {
219 AlphaToCoverageEnable = false,
220 };
221
222 blendStateDescription.RenderTarget[0].IsBlendEnabled = true;
223 blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
224 blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
225 blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add;
226 blendStateDescription.RenderTarget[0].SourceAlphaBlend = BlendOption.Zero;
227 blendStateDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
228 blendStateDescription.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
229 blendStateDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
230
231 DeviceContext.OutputMerger.BlendState = new BlendState(device, blendStateDescription);
232
233 return true;
234 }
235 catch
236 {
237 return false;
238 }
239 }