2 using System.Collections.Generic;
3 using System.Diagnostics;
10 public SharpDX.Size2F 設計画面サイズdpx = new SharpDX.Size2F( 640f, 480f );
11 public SharpDX.Size2F 物理画面サイズpx = new SharpDX.Size2F( 0, 0 ); // (0, 0) は、サイズ依存リソース無効の印。
12 public IntPtr ウィンドウハンドル = IntPtr.Zero;
13 public float 視野角deg { get; set; } = 45f;
14 public SharpDX.Matrix ビュー変換行列
18 var カメラの位置 = new SharpDX.Vector3( 0f, 0f, ( -2f * this.dz( this.設計画面サイズdpx.Height, this.視野角deg ) ) );
19 var カメラの注視点 = new SharpDX.Vector3( 0f, 0f, 0f );
20 var カメラの上方向 = new SharpDX.Vector3( 0f, 1f, 0f );
21 var mat = SharpDX.Matrix.LookAtLH( カメラの位置, カメラの注視点, カメラの上方向 );
22 mat.Transpose(); // 転置
26 public SharpDX.Matrix 射影変換行列
30 float dz = this.dz( this.設計画面サイズdpx.Height, this.視野角deg );
31 var mat = SharpDX.Matrix.PerspectiveFovLH(
32 SharpDX.MathUtil.DegreesToRadians( 視野角deg ),
33 設計画面サイズdpx.Width / 設計画面サイズdpx.Height, // アスペクト比
36 mat.Transpose(); // 転置
40 public SharpDX.MediaFoundation.DXGIDeviceManager DXGIDeviceManager => this.bs_DXGIDeviceManager;
41 public SharpDX.DXGI.SwapChain SwapChain => this.bs_SwapChain;
42 public SharpDX.Direct3D11.RenderTargetView D3DRenderTargetView => this.bs_D3DRenderTargetView;
43 public SharpDX.Mathematics.Interop.RawViewportF[] D3DViewPort => this.bs_D3DViewPort;
44 public SharpDX.Direct3D11.Texture2D D3DDepthStencil => this.bs_D3DDepthStencil;
45 public SharpDX.Direct3D11.DepthStencilView D3DDepthStencilView => this.bs_D3DDepthStencilView;
46 public SharpDX.Direct3D11.DepthStencilState D3DDepthStencilState => this.bs_D3DDepthStencilState;
47 public SharpDX.Direct2D1.Factory2 D2DFactory2 => this.bs_D2DFactory2;
48 public SharpDX.DirectWrite.Factory DWriteFactory => this.bs_DWriteFactory;
49 public SharpDX.WIC.ImagingFactory2 WicImagingFactory2 => this.bs_WicImagingFactory2;
50 public SharpDX.Direct2D1.Device1 D2DDevice1 => this.bs_D2DDevice1;
51 public SharpDX.Direct2D1.DeviceContext1 D2DContext1 => this.bs_D2DContext1;
52 public SharpDX.Direct2D1.Bitmap1 D2DRenderTargetBitmap => this.bs_D2DRenderTargetBitmap;
54 public void すべてのリソースを作成する( System.Drawing.Size バックバッファサイズ, IntPtr ウィンドウハンドル )
56 this.物理画面サイズpx = new SharpDX.Size2F( バックバッファサイズ.Width, バックバッファサイズ.Height );
57 this.ウィンドウハンドル = ウィンドウハンドル;
61 protected void すべてのリソースを作成する()
63 // これらが呼び出し前に設定されていること。
64 Debug.Assert( ( 0f < this.物理画面サイズpx.Width ) && ( 0f < this.物理画面サイズpx.Height ) );
65 Debug.Assert( IntPtr.Zero != this.ウィンドウハンドル );
67 #region " D2DFactory2 を作成する。"
70 var デバッグレベル = SharpDX.Direct2D1.DebugLevel.None;
72 // プロジェクトがデバッグビルドに含まれている場合は、Direct2D デバッグレイヤーを SDK レイヤーを介して有効にする。
73 デバッグレベル = SharpDX.Direct2D1.DebugLevel.Information;
75 this.bs_D2DFactory2 = new SharpDX.Direct2D1.Factory2(
76 SharpDX.Direct2D1.FactoryType.SingleThreaded,
81 #region " DWriteFactory を作成する。"
83 this.bs_DWriteFactory = new SharpDX.DirectWrite.Factory(
84 SharpDX.DirectWrite.FactoryType.Shared );
87 #region " WicImagingFactory2 を作成する。"
89 this.bs_WicImagingFactory2 = new SharpDX.WIC.ImagingFactory2();
93 var d3dDevice = (SharpDX.Direct3D11.Device) null;
95 #region " DXGIDeviceManager を作成する。"
97 this.bs_DXGIDeviceManager = new SharpDX.MediaFoundation.DXGIDeviceManager();
100 #region " D3Dデバイス、スワップチェーンを作成する。"
103 var swapChainDesc = new SharpDX.DXGI.SwapChainDescription() {
105 ModeDescription = new SharpDX.DXGI.ModeDescription() {
106 Width = (int) this.物理画面サイズpx.Width,
107 Height = (int) this.物理画面サイズpx.Height,
108 RefreshRate = new SharpDX.DXGI.Rational( 60, 1 ),
109 Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, // D2D をサポートするなら B8G8R8A8 で。
110 Scaling = SharpDX.DXGI.DisplayModeScaling.Stretched,
111 ScanlineOrdering = SharpDX.DXGI.DisplayModeScanlineOrder.Progressive,
114 OutputHandle = ウィンドウハンドル,
115 SampleDescription = new SharpDX.DXGI.SampleDescription( 1, 0 ),
116 SwapEffect = SharpDX.DXGI.SwapEffect.Discard,
117 Usage = SharpDX.DXGI.Usage.RenderTargetOutput,
120 var featureLevels = new SharpDX.Direct3D.FeatureLevel[] {
121 SharpDX.Direct3D.FeatureLevel.Level_11_0,
122 SharpDX.Direct3D.FeatureLevel.Level_10_1,
123 SharpDX.Direct3D.FeatureLevel.Level_10_0,
125 // デバイスとスワップチェーンを作成する。
126 SharpDX.Direct3D11.Device.CreateWithSwapChain(
127 SharpDX.Direct3D.DriverType.Hardware,
128 SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, // D2Dをサポートするなら必須。
132 out this.bs_SwapChain );
134 Trace.WriteLine( "D3Dデバイスとスワップチェーンを生成しました。" );
135 Trace.WriteLine( $"機能レベル: {d3dDevice.FeatureLevel.ToString()}" );
142 #region " D3DDevice が ID3D11VideoDevice を実装してないならエラー。(Win8以降のPCでは実装されているはず。) "
144 using( var videoDevice = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D11.VideoDevice>() )
146 if( null == videoDevice )
147 throw new FDKException( "Direct3D11デバイスが、ID3D11VideoDevice をサポートしていません。" );
151 #region " マルチスレッドモードを ON に設定する。DXVAを使う場合は必須。"
153 using( var multithread = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D.DeviceMultithread>() )
155 if( null == multithread )
156 throw new FDKException( "Direct3D11デバイスが、ID3D10Multithread をサポートしていません。" );
158 multithread.SetMultithreadProtected( true );
162 #region " DXGIデバイスマネージャに D3Dデバイスを登録する。"
164 this.DXGIDeviceManager.ResetDevice( d3dDevice );
167 #region " すべての Windows イベントを無視する。具体的には PrintScreen と Alt+Enter 。"
169 using( var factory = this.bs_SwapChain.GetParent<SharpDX.DXGI.Factory>() )
171 factory.MakeWindowAssociation( ウィンドウハンドル, SharpDX.DXGI.WindowAssociationFlags.IgnoreAll );
175 #region " 深度ステンシルステートを作成する。"
177 var DepthSencil = new SharpDX.Direct3D11.DepthStencilStateDescription() {
178 IsDepthEnabled = true, // 深度テストあり
179 DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.All, // 書き込む
180 DepthComparison = SharpDX.Direct3D11.Comparison.Less, // 手前の物体を描画
181 IsStencilEnabled = false, // ステンシルテストなし。
182 StencilReadMask = 0, // ステンシル読み込みマスク。
183 StencilWriteMask = 0, // ステンシル書き込みマスク。
185 // 面が表を向いている場合のステンシル・テストの設定
186 FrontFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
187 FailOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
188 DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
189 PassOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
190 Comparison = SharpDX.Direct3D11.Comparison.Never, // 常に失敗
193 // 面が裏を向いている場合のステンシル・テストの設定
194 BackFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
195 FailOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
196 DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
197 PassOperation = SharpDX.Direct3D11.StencilOperation.Keep, // 維持
198 Comparison = SharpDX.Direct3D11.Comparison.Always, // 常に成功
201 this.bs_D3DDepthStencilState = new SharpDX.Direct3D11.DepthStencilState( d3dDevice, DepthSencil );
206 #region " D2DDevice を作成する。"
208 using( var dxgiDevice = d3dDevice.QueryInterfaceOrNull<SharpDX.DXGI.Device>() )
210 if( null == dxgiDevice )
211 throw new FDKException( "Direct3D11デバイスが、IDXGIDevice3 をサポートしていません。" );
213 this.bs_D2DDevice1 = new SharpDX.Direct2D1.Device1( this.D2DFactory2, dxgiDevice );
217 #region " D2Dの既定のコンテキスト D2DContext1 を作成する。"
219 this.bs_D2DContext1 = new SharpDX.Direct2D1.DeviceContext1( this.D2DDevice1, SharpDX.Direct2D1.DeviceContextOptions.None );
221 // 現在のディスプレイDPI を取得し、D2DContext に設定する。
222 this.D2DContext1.DotsPerInch = this.D2DFactory2.DesktopDpi;
227 this.サイズに依存するリソースを作成する();
229 public void すべてのリソースを解放する()
231 FDK.Utilities.解放する( ref this.bs_WicImagingFactory2 );
232 FDK.Utilities.解放する( ref this.bs_DWriteFactory );
233 FDK.Utilities.解放する( ref this.bs_D2DFactory2 );
236 FDK.Utilities.解放する( ref this.bs_D2DContext1 );
237 FDK.Utilities.解放する( ref this.bs_D2DDevice1 );
240 var d3dDevice = (SharpDX.Direct3D11.Device) null;
241 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
243 d3dDevice.ImmediateContext?.ClearState(); // デバイスステートをクリアする。
244 this.bs_SwapChain?.SetFullscreenState( fullscreen: false, targetRef: null ); // スワップチェインをウインドウモードにする。
246 this.サイズに依存するリソースを解放する();
248 FDK.Utilities.解放する( ref this.bs_D3DDepthStencilState );
249 FDK.Utilities.解放する( ref this.bs_SwapChain );
250 this.bs_DXGIDeviceManager?.ResetDevice( null );
251 FDK.Utilities.解放する( ref this.bs_DXGIDeviceManager );
254 public void サイズに依存するリソースを作成する()
256 #region " スワップチェーンのサイズを変更する。"
258 Debug.Assert( null != this.SwapChain ); // スワップチェーンは、デバイスとともに、すでに生成される。
259 this.SwapChain.ResizeBuffers(
261 width: (int) this.物理画面サイズpx.Width,
262 height: (int) this.物理画面サイズpx.Height,
263 newFormat: SharpDX.DXGI.Format.R8G8B8A8_UNorm,
264 swapChainFlags: SharpDX.DXGI.SwapChainFlags.AllowModeSwitch );
269 using( var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>( this.bs_SwapChain, 0 ) )
271 var d3dDevice = (SharpDX.Direct3D11.Device) null;
272 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
274 #region " RenderTargetView の作成 "
276 this.bs_D3DRenderTargetView = new SharpDX.Direct3D11.RenderTargetView( d3dDevice, backBuffer );
279 #region " 深度ステンシルテクスチャの作成 "
281 var descDepth = backBuffer.Description;
282 //descDepth.Width = backBuffer.Description.Width; → backBuffer に同じ
283 //descDepth.Height = backBuffer.Description.Height; → 同上
284 descDepth.MipLevels = 1; // ミップマップレベル数
285 descDepth.ArraySize = 1; // 配列サイズ
286 descDepth.Format = SharpDX.DXGI.Format.D32_Float; // フォーマット(深度のみ)
287 descDepth.Usage = SharpDX.Direct3D11.ResourceUsage.Default; // デフォルト使用法
288 descDepth.BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil; // 深度ステンシル
289 descDepth.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None; // CPUからはアクセスしない
290 descDepth.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None; // その他の設定なし
291 this.bs_D3DDepthStencil = new SharpDX.Direct3D11.Texture2D( d3dDevice, descDepth );
294 #region " 深度ステンシルビューの作成 "
296 var descDSV = new SharpDX.Direct3D11.DepthStencilViewDescription() {
297 Format = descDepth.Format,
298 Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D,
299 Flags = SharpDX.Direct3D11.DepthStencilViewFlags.None,
301 descDSV.Texture2D.MipSlice = 0;
302 this.bs_D3DDepthStencilView = new SharpDX.Direct3D11.DepthStencilView( d3dDevice, this.bs_D3DDepthStencil, descDSV );
305 #region " ビューポートの設定 "
307 this.bs_D3DViewPort[ 0 ] = new SharpDX.Mathematics.Interop.RawViewportF() {
310 Width = (float) backBuffer.Description.Width,
311 Height = (float) backBuffer.Description.Height,
317 #region " テクスチャの共有リソースの作成 "
319 FDK.メディア.テクスチャ.共有リソースを作成する( this );
326 using( var backbuffer =SharpDX.DXGI.Surface.FromSwapChain( this.bs_SwapChain, 0 ) )
328 #region " D2Dの既定のレンダーターゲットビットマップを作成する。"
330 var dpi = this.D2DContext1.DotsPerInch;
332 // DXGIスワップチェーンのバックバッファとデータを共有するD2Dターゲットビットマップを作成する。ビューではなく共有リソース。
333 this.bs_D2DRenderTargetBitmap = new SharpDX.Direct2D1.Bitmap1(
334 this.D2DContext1, // このコンテキストを通じて、
335 backbuffer, // バックバッファとデータを共有する。
336 new SharpDX.Direct2D1.BitmapProperties1(
337 new SharpDX.Direct2D1.PixelFormat( backbuffer.Description.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied ),
340 SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw ) );
343 #region " テキストのアンチエイリアシングを設定する。Grayscale が、すべての Windows ストアアプリで推奨される。"
345 this.D2DContext1.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
351 public void サイズに依存するリソースを解放する()
354 FDK.Utilities.解放する( ref this.bs_D2DRenderTargetBitmap );
357 var d3dDevice = (SharpDX.Direct3D11.Device) null;
358 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
361 d3dDevice.ImmediateContext.OutputMerger.ResetTargets();
363 FDK.メディア.テクスチャ.共有リソースを解放する();
364 FDK.Utilities.解放する( ref this.bs_D3DDepthStencilView );
365 FDK.Utilities.解放する( ref this.bs_D3DDepthStencil );
366 FDK.Utilities.解放する( ref this.bs_D3DRenderTargetView );
367 //FDK.Utilities.解放する( ref this.bs_SwapChain ); → スワップチェーンは解放しない(生成・解放はデバイスとセット)。
369 // (0,0)は、サイズ依存リソース無効の印。
370 this.物理画面サイズpx = new SharpDX.Size2F( 0, 0 );
373 public void D3Dデバイスが消失していれば再構築する( out bool 異常状態なのでアプリを終了せよ )
375 var d3dDevice = (SharpDX.Direct3D11.Device) null;
376 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
378 異常状態なのでアプリを終了せよ = false;
380 var 削除理由 = d3dDevice.DeviceRemovedReason;
385 new { Code = SharpDX.DXGI.ResultCode.DeviceHung.Code, Info = SharpDX.DXGI.ResultCode.DeviceHung.ApiCode, Rebuild = true },
386 new { Code = SharpDX.DXGI.ResultCode.DeviceReset.Code, Info = SharpDX.DXGI.ResultCode.DeviceReset.ApiCode, Rebuild = true },
387 new { Code = SharpDX.DXGI.ResultCode.DeviceRemoved.Code, Info = SharpDX.DXGI.ResultCode.DeviceRemoved.ApiCode, Rebuild = false },
388 new { Code = SharpDX.DXGI.ResultCode.DriverInternalError.Code, Info = SharpDX.DXGI.ResultCode.DriverInternalError.ApiCode, Rebuild = false },
389 new { Code = SharpDX.DXGI.ResultCode.InvalidCall.Code, Info = SharpDX.DXGI.ResultCode.InvalidCall.ApiCode, Rebuild = false },
390 }.First( ( エラー ) => エラー.Code == 削除理由.Code ); // 見つからないなら System.InvalidOperationException 。
392 Trace.WriteLine( $"D3Dデバイスが消失しました: {エラー詳細.Info}" );
396 this.すべてのリソースを解放する();
397 this.すべてのリソースを作成する();
401 異常状態なのでアプリを終了せよ = true;
406 private float dz( float 高さdpx, float 視野角deg )
408 return (float) ( 高さdpx / ( 4.0 * Math.Tan( SharpDX.MathUtil.DegreesToRadians( 視野角deg / 2.0f ) ) ) );
413 private SharpDX.Direct2D1.Factory2 bs_D2DFactory2 = null;
414 private SharpDX.DirectWrite.Factory bs_DWriteFactory = null;
415 private SharpDX.WIC.ImagingFactory2 bs_WicImagingFactory2 = null;
416 private SharpDX.MediaFoundation.DXGIDeviceManager bs_DXGIDeviceManager = null;
417 private SharpDX.DXGI.SwapChain bs_SwapChain = null;
418 private SharpDX.Mathematics.Interop.RawViewportF[] bs_D3DViewPort = new SharpDX.Mathematics.Interop.RawViewportF[ 1 ];
419 private SharpDX.Direct3D11.DepthStencilState bs_D3DDepthStencilState = null;
420 private SharpDX.Direct3D11.RenderTargetView bs_D3DRenderTargetView = null;
421 private SharpDX.Direct3D11.Texture2D bs_D3DDepthStencil = null;
422 private SharpDX.Direct3D11.DepthStencilView bs_D3DDepthStencilView = null;
423 private SharpDX.Direct2D1.Device1 bs_D2DDevice1 = null;
424 private SharpDX.Direct2D1.DeviceContext1 bs_D2DContext1 = null;
425 private SharpDX.Direct2D1.Bitmap1 bs_D2DRenderTargetBitmap = null;