OSDN Git Service

ひとまず、高頻度進行処理タスクは起動せず、その中の処理を進行描画タスクへ移動する。
[strokestylet/CsWin10Desktop3.git] / FDK24 / メディア / デバイスリソース.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5
6 namespace FDK.メディア
7 {
8         public class デバイスリソース : IDisposable
9         {
10                 public SharpDX.Size2F 設計画面サイズdpx = new SharpDX.Size2F( 1920f, 1080f );    // 設計サイズ。
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 ビュー変換行列
15                 {
16                         get
17                         {
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();  // 転置
23                                 return mat;
24                         }
25                 }
26                 public SharpDX.Matrix 射影変換行列
27                 {
28                         get
29                         {
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,  // アスペクト比
34                                         -dz,  // 前方投影面までの距離
35                                         dz ); // 後方投影面までの距離
36                                 mat.Transpose();  // 転置
37                                 return mat;
38                         }
39                 }
40                 public SharpDX.MediaFoundation.DXGIDeviceManager DXGIDeviceManager => ( this.bs_DXGIDeviceManager );
41                 public SharpDX.DXGI.SwapChain1 SwapChain1 => ( this.bs_SwapChain1 );
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.Direct3D11.DeviceDebug D3DDeviceDebug => ( this.bs_D3DDeviceDebug );
48                 public SharpDX.Direct2D1.Factory2 D2DFactory2 => ( this.bs_D2DFactory2 );
49                 public SharpDX.DirectWrite.Factory DWriteFactory => ( this.bs_DWriteFactory );
50                 public SharpDX.WIC.ImagingFactory2 WicImagingFactory2 => ( this.bs_WicImagingFactory2 );
51                 public SharpDX.Direct2D1.Device1 D2DDevice1 => ( this.bs_D2DDevice1 );
52                 public SharpDX.Direct2D1.DeviceContext1 D2DContext1 => ( this.bs_D2DContext1 );
53                 public SharpDX.Direct2D1.Bitmap1 D2DRenderTargetBitmap => ( this.bs_D2DRenderTargetBitmap );
54
55                 public float 拡大率DPXtoPX横方向 => ( this.物理画面サイズpx.Width / this.設計画面サイズdpx.Width );
56                 public float 拡大率DPXtoPX縦方向 => ( this.物理画面サイズpx.Height / this.設計画面サイズdpx.Height );
57                 public float 拡大率PXtoDPX横方向 => ( 1f / this.拡大率DPXtoPX横方向 );
58                 public float 拡大率PXtoDPX縦方向 => ( 1f / this.拡大率DPXtoPX縦方向 );
59                 public SharpDX.Matrix3x2 拡大行列DPXtoPX => SharpDX.Matrix3x2.Scaling( this.拡大率DPXtoPX横方向, this.拡大率DPXtoPX縦方向 );
60                 public SharpDX.Matrix3x2 拡大行列PXtoDPX => SharpDX.Matrix3x2.Scaling( this.拡大率PXtoDPX横方向, this.拡大率PXtoDPX縦方向 );
61
62                 public void すべてのリソースを作成する( System.Drawing.Size バックバッファサイズpx, IntPtr ウィンドウハンドル )
63                 {
64                         try
65                         {
66                                 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
67
68                                 this.物理画面サイズpx = new SharpDX.Size2F( バックバッファサイズpx.Width, バックバッファサイズpx.Height );
69                                 this.ウィンドウハンドル = ウィンドウハンドル;
70
71                                 this.すべてのリソースを作成する();
72                         }
73                         finally
74                         {
75                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
76                         }
77                 }
78                 protected void すべてのリソースを作成する()
79                 {
80                         try
81                         {
82                                 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
83
84                                 // これらが呼び出し前に設定されていること。
85                                 Debug.Assert( ( 0f < this.物理画面サイズpx.Width ) && ( 0f < this.物理画面サイズpx.Height ) );
86                                 Debug.Assert( IntPtr.Zero != this.ウィンドウハンドル );
87
88                                 #region " D2DFactory2 を作成する。"
89                                 //-----------------
90                                 {
91                                         var デバッグレベル = SharpDX.Direct2D1.DebugLevel.None;
92 #if DEBUG
93                                         // プロジェクトがデバッグビルドに含まれている場合は、Direct2D デバッグレイヤーを SDK レイヤーを介して有効にする。
94                                         デバッグレベル = SharpDX.Direct2D1.DebugLevel.Information;
95 #endif
96                                         this.bs_D2DFactory2 = new SharpDX.Direct2D1.Factory2( SharpDX.Direct2D1.FactoryType.MultiThreaded, デバッグレベル );
97                                 }
98                                 //-----------------
99                                 #endregion
100                                 #region " DWriteFactory を作成する。"
101                                 //-----------------
102                                 this.bs_DWriteFactory = new SharpDX.DirectWrite.Factory( SharpDX.DirectWrite.FactoryType.Shared );
103                                 //-----------------
104                                 #endregion
105                                 #region " WicImagingFactory2 を作成する。"
106                                 //-----------------
107                                 this.bs_WicImagingFactory2 = new SharpDX.WIC.ImagingFactory2();
108                                 //-----------------
109                                 #endregion
110
111                                 var d3dDevice = (SharpDX.Direct3D11.Device) null;
112
113                                 #region " DXGIDeviceManager を作成する。"
114                                 //-----------------
115                                 this.bs_DXGIDeviceManager = new SharpDX.MediaFoundation.DXGIDeviceManager();
116                                 //-----------------
117                                 #endregion
118                                 #region " D3Dデバイスを作成する。"
119                                 //----------------
120                                 // 作成フラグ
121                                 var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; // D2Dをサポートするなら BgraSupport フラグが必須。
122 #if DEBUG
123                                 // D3D11 Debugメッセージは、プロジェクトプロパティで「ネイティブコードのデバッグを有効にする」を ON にしないと表示されないので注意。
124                                 creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
125 #endif
126                                 d3dDevice = new SharpDX.Direct3D11.Device(
127                                         SharpDX.Direct3D.DriverType.Hardware,
128                                         creationFlags,
129                                         new SharpDX.Direct3D.FeatureLevel[] {
130                                                 SharpDX.Direct3D.FeatureLevel.Level_11_1,
131                                                 SharpDX.Direct3D.FeatureLevel.Level_11_0,
132                                         } );
133
134                                 FDK.Log.Info( "D3Dデバイスを生成しました。" );
135                                 FDK.Log.Info( $"機能レベル: {d3dDevice.FeatureLevel.ToString()}" );
136                                 //----------------
137                                 #endregion
138
139                                 using( d3dDevice )
140                                 {
141                                         // D3D 関連
142
143                                         #region " スワップチェーンを作成する。"
144                                         //----------------
145                                         using( var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>() )
146                                         using( var dxgiAdapter = dxgiDevice.Adapter )
147                                         using( var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>() )
148                                         {
149                                                 var swapChainDesc1 = new SharpDX.DXGI.SwapChainDescription1() {
150                                                         BufferCount = 2,
151                                                         Width = (int) this.物理画面サイズpx.Width,
152                                                         Height = (int) this.物理画面サイズpx.Height,
153                                                         Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,  // D2D をサポートするなら B8G8R8A8 を使う必要がある。
154                                                         Scaling = SharpDX.DXGI.Scaling.Stretch,
155                                                         SampleDescription = new SharpDX.DXGI.SampleDescription( 1, 0 ),
156                                                         SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard,       // Windowsストアアプリはこれが必須。
157                                                         Usage = SharpDX.DXGI.Usage.RenderTargetOutput,
158                                                         Flags = SharpDX.DXGI.SwapChainFlags.None,// SharpDX.DXGI.SwapChainFlags.AllowModeSwitch,        // ResizeTarget を使うなら必須
159                                                 };
160                                                 this.bs_SwapChain1 = new SharpDX.DXGI.SwapChain1(
161                                                         dxgiFactory2,
162                                                         d3dDevice,
163                                                         this.ウィンドウハンドル,
164                                                         ref swapChainDesc1 );
165                                         }
166                                         //----------------
167                                         #endregion
168                                         #region " デバイスからデバッグオブジェクトを取得する。"
169                                         //----------------
170                                         this.bs_D3DDeviceDebug = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D11.DeviceDebug>();
171                                         //----------------
172                                         #endregion
173                                         #region " D3DDevice が ID3D11VideoDevice を実装してないならエラー。(Win8以降のPCでは実装されているはず。) "
174                                         //-----------------
175                                         using( var videoDevice = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D11.VideoDevice>() )
176                                         {
177                                                 if( null == videoDevice )
178                                                         throw new FDKException( "Direct3D11デバイスが、ID3D11VideoDevice をサポートしていません。" );
179                                         }
180                                         //-----------------
181                                         #endregion
182                                         #region " マルチスレッドモードを ON に設定する。DXVAを使う場合は必須。"
183                                         //-----------------
184                                         using( var multithread = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D.DeviceMultithread>() )
185                                         {
186                                                 if( null == multithread )
187                                                         throw new FDKException( "Direct3D11デバイスが、ID3D10Multithread をサポートしていません。" );
188
189                                                 multithread.SetMultithreadProtected( true );
190                                         }
191                                         //-----------------
192                                         #endregion
193                                         #region " DXGIデバイスマネージャに D3Dデバイスを登録する。"
194                                         //-----------------
195                                         this.DXGIDeviceManager.ResetDevice( d3dDevice );
196                                         //-----------------
197                                         #endregion
198                                         #region " すべての Windows イベントを無視する。具体的には PrintScreen と Alt+Enter 。"
199                                         //----------------
200                                         using( var factory = this.bs_SwapChain1.GetParent<SharpDX.DXGI.Factory>() )
201                                         {
202                                                 factory.MakeWindowAssociation( ウィンドウハンドル, SharpDX.DXGI.WindowAssociationFlags.IgnoreAll );
203                                         }
204                                         //----------------
205                                         #endregion
206                                 }
207
208                                 this.サイズに依存するリソースを作成する();
209                         }
210                         finally
211                         {
212                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
213                         }
214                 }
215                 public void すべてのリソースを解放する()
216                 {
217                         try
218                         {
219                                 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
220
221                                 // D3D 関連
222                                 this.bs_SwapChain1?.SetFullscreenState( fullscreen: false, targetRef: null );    // スワップチェインをウインドウモードにする。
223                                 this.サイズに依存するリソースを解放する();
224
225                                 FDK.Utilities.解放する( ref this.bs_SwapChain1 );
226                                 FDK.Utilities.解放する( ref this.bs_DXGIDeviceManager );
227
228                                 // その他
229                                 FDK.Utilities.解放する( ref this.bs_WicImagingFactory2 );
230                                 FDK.Utilities.解放する( ref this.bs_DWriteFactory );
231                                 FDK.Utilities.解放する( ref this.bs_D2DFactory2 );
232
233                                 this.bs_D3DDeviceDebug?.ReportLiveDeviceObjects( SharpDX.Direct3D11.ReportingLevel.Detail );
234                                 FDK.Utilities.解放する( ref this.bs_D3DDeviceDebug );
235                         }
236                         finally
237                         {
238                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
239                         }
240                 }
241                 public void Dispose()
242                 {
243                         this.すべてのリソースを解放する();
244                 }
245                 public void サイズに依存するリソースを作成する()
246                 {
247                         try
248                         {
249                                 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
250
251                                 #region " スワップチェーンのサイズを変更する。"
252                                 //----------------
253                                 Debug.Assert( null != this.SwapChain1 ); // スワップチェーンは(デバイスとともに)すでに生成されていること。
254
255                                 // ResizeTarget は、全画面モードにしたとき、モニタ画面の解像度も変更する。
256                                 //var mode = new SharpDX.DXGI.ModeDescription(
257                                 //      width: (int) this.物理画面サイズpx.Width,
258                                 //      height: (int) this.物理画面サイズpx.Height,
259                                 //      refreshRate: new SharpDX.DXGI.Rational( 60000, 1001 ),  // 多くのモニタは(60,1)じゃない;一致しないとFlipじゃなくBlitになるので注意(MSDN)。
260                                 //      format: SharpDX.DXGI.Format.B8G8R8A8_UNorm );
261                                 //this.SwapChain.ResizeTarget( ref mode );
262
263                                 // ResizeBuffers は、全画面モードにしたとき、モニタの解像度を変更しない。
264                                 // ただし、swapChainFlags に AllowModeSwitch を指定すると変更される(ResizeTargetと同じ挙動になる)。
265                                 this.SwapChain1.ResizeBuffers(
266                                         bufferCount: 2,
267                                         width: (int) this.物理画面サイズpx.Width,
268                                         height: (int) this.物理画面サイズpx.Height,
269                                         newFormat: SharpDX.DXGI.Format.B8G8R8A8_UNorm,
270                                         swapChainFlags: SharpDX.DXGI.SwapChainFlags.None );
271                                 //----------------
272                                 #endregion
273
274                                 // バックバッファを使って、D3D / D2D 関連のリソースを作成する。
275                                 using( var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>( this.bs_SwapChain1, 0 ) )
276                                 {
277                                         var d3dDevice = (SharpDX.Direct3D11.Device) null;
278                                         using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
279                                         using( d3dDevice )
280                                         {
281                                                 // D3D 関連
282                                                 #region " レンダーターゲットビューを作成する。"
283                                                 //----------------
284                                                 this.bs_D3DRenderTargetView = new SharpDX.Direct3D11.RenderTargetView( d3dDevice, backBuffer );
285                                                 //----------------
286                                                 #endregion
287                                                 #region " 深度ステンシルテクスチャを作成する。"
288                                                 //----------------
289                                                 var descDepth = backBuffer.Description;
290                                                 //descDepth.Width = backBuffer.Description.Width;       → backBuffer に同じ
291                                                 //descDepth.Height = backBuffer.Description.Height;     → 同上
292                                                 descDepth.MipLevels = 1;    // ミップマップレベル数
293                                                 descDepth.ArraySize = 1;    // 配列サイズ
294                                                 descDepth.Format = SharpDX.DXGI.Format.D32_Float;   // フォーマット(深度のみ)
295                                                 descDepth.Usage = SharpDX.Direct3D11.ResourceUsage.Default; // デフォルト使用法
296                                                 descDepth.BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil;    // 深度ステンシル
297                                                 descDepth.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None;  // CPUからはアクセスしない
298                                                 descDepth.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None;    // その他の設定なし
299                                                 this.bs_D3DDepthStencil = new SharpDX.Direct3D11.Texture2D( d3dDevice, descDepth );
300                                                 //----------------
301                                                 #endregion
302                                                 #region " 深度ステンシルビューを作成する。"
303                                                 //----------------
304                                                 var descDSV = new SharpDX.Direct3D11.DepthStencilViewDescription() {
305                                                         Format = descDepth.Format,
306                                                         Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D,
307                                                         Flags = SharpDX.Direct3D11.DepthStencilViewFlags.None,
308                                                         Texture2D = new SharpDX.Direct3D11.DepthStencilViewDescription.Texture2DResource() {
309                                                                 MipSlice = 0,
310                                                         },
311                                                 };
312                                                 this.bs_D3DDepthStencilView = new SharpDX.Direct3D11.DepthStencilView( d3dDevice, this.bs_D3DDepthStencil, descDSV );
313                                                 //----------------
314                                                 #endregion
315                                                 #region " 深度ステンシルステートを作成する。"
316                                                 //----------------
317                                                 var DepthSencil = new SharpDX.Direct3D11.DepthStencilStateDescription() {
318                                                         IsDepthEnabled = true,  // 深度テストあり
319                                                         DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.All,     // 書き込む
320                                                         DepthComparison = SharpDX.Direct3D11.Comparison.Less,   // 手前の物体を描画
321                                                         IsStencilEnabled = false,   // ステンシルテストなし。
322                                                         StencilReadMask = 0,    // ステンシル読み込みマスク。
323                                                         StencilWriteMask = 0,   // ステンシル書き込みマスク。
324
325                                                         // 面が表を向いている場合のステンシル・テストの設定
326                                                         FrontFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
327                                                                 FailOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
328                                                                 DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep,  // 維持
329                                                                 PassOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
330                                                                 Comparison = SharpDX.Direct3D11.Comparison.Never,   // 常に失敗
331                                                         },
332
333                                                         // 面が裏を向いている場合のステンシル・テストの設定
334                                                         BackFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
335                                                                 FailOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
336                                                                 DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep,  // 維持
337                                                                 PassOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
338                                                                 Comparison = SharpDX.Direct3D11.Comparison.Always,  // 常に成功
339                                                         },
340                                                 };
341                                                 this.bs_D3DDepthStencilState = new SharpDX.Direct3D11.DepthStencilState( d3dDevice, DepthSencil );
342                                                 //----------------
343                                                 #endregion
344                                                 #region " ビューポートを作成する。"
345                                                 //----------------
346                                                 this.bs_D3DViewPort[ 0 ] = new SharpDX.Mathematics.Interop.RawViewportF() {
347                                                         X = 0.0f,
348                                                         Y = 0.0f,
349                                                         Width = (float) backBuffer.Description.Width,   // 物理画面単位[px]
350                                                         Height = (float) backBuffer.Description.Height, // 物理画面単位[px]
351                                                         MinDepth = 0.0f,
352                                                         MaxDepth = 1.0f,
353                                                 };
354                                                 //----------------
355                                                 #endregion
356                                                 #region " 全テクスチャで共有するリソースを作成する。"
357                                                 //----------------
358                                                 FDK.メディア.テクスチャ.全インスタンスで共有するリソースを作成する( this );
359                                                 //----------------
360                                                 #endregion
361
362                                                 // D2D 関連
363                                                 using( var backsurface = SharpDX.DXGI.Surface.FromSwapChain( this.bs_SwapChain1, 0 ) )
364                                                 {
365                                                         #region " D2DDevice を作成する。"
366                                                         //-----------------
367                                                         using( var dxgiDevice = d3dDevice.QueryInterfaceOrNull<SharpDX.DXGI.Device>() )
368                                                         {
369                                                                 if( null == dxgiDevice )
370                                                                         throw new FDKException( "Direct3D11デバイスが、IDXGIDevice3 をサポートしていません。" );
371
372                                                                 this.bs_D2DDevice1 = new SharpDX.Direct2D1.Device1( this.D2DFactory2, dxgiDevice );
373                                                         }
374                                                         //-----------------
375                                                         #endregion
376                                                         #region " D2Dの既定のデバイスコンテキストを作成する。"
377                                                         //-----------------
378                                                         this.bs_D2DContext1 = new SharpDX.Direct2D1.DeviceContext1( this.D2DDevice1, SharpDX.Direct2D1.DeviceContextOptions.None );
379
380                                                         // 現在のディスプレイDPI を取得し、D2DContext に設定する。
381                                                         //this.D2DContext1.DotsPerInch = this.D2DFactory2.DesktopDpi;
382                                                         this.D2DContext1.DotsPerInch = new SharpDX.Size2F( 96f, 96f );
383                                                         //-----------------
384                                                         #endregion
385                                                         #region " D2Dの既定のレンダーターゲットビットマップを作成する。"
386                                                         //-----------------
387                                                         var dpi = this.D2DContext1.DotsPerInch;
388
389                                                         // DXGIスワップチェーンのバックバッファとデータを共有するD2Dターゲットビットマップを作成する。ビューではなく共有リソース。
390                                                         this.bs_D2DRenderTargetBitmap = new SharpDX.Direct2D1.Bitmap1(
391                                                                 this.D2DContext1,   // このコンテキストを通じて、
392                                                                 backsurface, // バックバッファとデータを共有する。
393                                                                 new SharpDX.Direct2D1.BitmapProperties1(
394                                                                         new SharpDX.Direct2D1.PixelFormat( backsurface.Description.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied ),
395                                                                         dpi.Width,
396                                                                         dpi.Height,
397                                                                         SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw ) );
398
399                                                         // ここでもうD2Dのレンダーターゲットとして登録しておく。
400                                                         this.D2DContext1.Target = this.bs_D2DRenderTargetBitmap;
401                                                         //-----------------
402                                                         #endregion
403                                                         #region " テキストのアンチエイリアシングを設定する。"
404                                                         //-----------------
405                                                         // Grayscale が、すべての Windows ストアアプリで推奨される。らしい。
406                                                         this.D2DContext1.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
407                                                         //-----------------
408                                                         #endregion
409                                                 }
410                                         }
411                                 }
412                         }
413                         finally
414                         {
415                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
416                         }
417                 }
418                 public void サイズに依存するリソースを解放する()
419                 {
420                         try
421                         {
422                                 FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
423
424                                 // D2D 関連
425                                 if( null != this.bs_D2DContext1 )
426                                         this.bs_D2DContext1.Target = null;
427                                 FDK.Utilities.解放する( ref this.bs_D2DRenderTargetBitmap );
428                                 FDK.Utilities.解放する( ref this.bs_D2DContext1 );
429                                 FDK.Utilities.解放する( ref this.bs_D2DDevice1 );
430
431                                 // D3D 関連
432                                 var d3dDevice = (SharpDX.Direct3D11.Device) null;
433                                 using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
434                                 using( d3dDevice )
435                                 using( var d3dContext = d3dDevice.ImmediateContext )
436                                 {
437                                         d3dContext.ClearState();
438                                         d3dContext.OutputMerger.ResetTargets();
439
440                                         FDK.メディア.テクスチャ.全インスタンスで共有するリソースを解放する();
441
442                                         FDK.Utilities.解放する( ref this.bs_D3DDepthStencilState );
443                                         FDK.Utilities.解放する( ref this.bs_D3DDepthStencilView );
444                                         FDK.Utilities.解放する( ref this.bs_D3DDepthStencil );
445                                         FDK.Utilities.解放する( ref this.bs_D3DRenderTargetView );
446                                         //FDK.Utilities.解放する( ref this.bs_SwapChain ); → スワップチェーンは解放しない(これの生成・解放はデバイスとセットで行う)。
447
448                                         // (0,0)は、サイズ依存リソース無効の印。
449                                         this.物理画面サイズpx = new SharpDX.Size2F( 0, 0 );
450                                 }
451                         }
452                         finally
453                         {
454                                 FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
455                         }
456                 }
457                 public void D3Dデバイスが消失していれば再構築する( out bool 異常状態なのでアプリを終了せよ )
458                 {
459                         異常状態なのでアプリを終了せよ = false;
460                         SharpDX.Result 削除理由;
461
462                         var d3dDevice = (SharpDX.Direct3D11.Device) null;
463                         using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
464                         using( d3dDevice )
465                         {
466                                 削除理由 = d3dDevice.DeviceRemovedReason;
467                         }
468
469                         if( 削除理由.Success )
470                                 return; // デバイスは消失していない。
471
472                         var エラー詳細 = new[] {
473                                         new { Code = SharpDX.DXGI.ResultCode.DeviceHung.Code, Info = SharpDX.DXGI.ResultCode.DeviceHung.ApiCode, Rebuild = true },
474                                         new { Code = SharpDX.DXGI.ResultCode.DeviceReset.Code, Info = SharpDX.DXGI.ResultCode.DeviceReset.ApiCode, Rebuild = true },
475                                         new { Code = SharpDX.DXGI.ResultCode.DeviceRemoved.Code, Info = SharpDX.DXGI.ResultCode.DeviceRemoved.ApiCode, Rebuild = false },
476                                         new { Code = SharpDX.DXGI.ResultCode.DriverInternalError.Code, Info = SharpDX.DXGI.ResultCode.DriverInternalError.ApiCode, Rebuild = false },
477                                         new { Code = SharpDX.DXGI.ResultCode.InvalidCall.Code, Info = SharpDX.DXGI.ResultCode.InvalidCall.ApiCode, Rebuild = false },
478                                 }.First( ( エラー ) => エラー.Code == 削除理由.Code );  // 見つからないなら System.InvalidOperationException 。
479
480                         Trace.WriteLine( $"D3Dデバイスが消失しました: {エラー詳細.Info}" );
481
482                         if( エラー詳細.Rebuild )
483                         {
484                                 this.すべてのリソースを解放する();
485                                 this.すべてのリソースを作成する();
486                         }
487                         else
488                         {
489                                 異常状態なのでアプリを終了せよ = true;
490                         }
491                 }
492                 public void D2DContextの設定をリセットする( SharpDX.Direct2D1.DeviceContext1 context1 )
493                 {
494                         context1.Transform = SharpDX.Matrix3x2.Identity;
495                         context1.PrimitiveBlend = SharpDX.Direct2D1.PrimitiveBlend.SourceOver;
496                 }
497
498                 private float dz( float 高さdpx, float 視野角deg )
499                 {
500                         return (float) ( 高さdpx / ( 4.0 * Math.Tan( SharpDX.MathUtil.DegreesToRadians( 視野角deg / 2.0f ) ) ) );
501                 }
502
503                 #region " バックストア。"
504                 //----------------
505                 private SharpDX.Direct2D1.Factory2 bs_D2DFactory2 = null;
506                 private SharpDX.DirectWrite.Factory bs_DWriteFactory = null;
507                 private SharpDX.WIC.ImagingFactory2 bs_WicImagingFactory2 = null;
508                 private SharpDX.MediaFoundation.DXGIDeviceManager bs_DXGIDeviceManager = null;
509                 private SharpDX.DXGI.SwapChain1 bs_SwapChain1 = null;
510                 private SharpDX.Mathematics.Interop.RawViewportF[] bs_D3DViewPort = new SharpDX.Mathematics.Interop.RawViewportF[ 1 ];
511                 private SharpDX.Direct3D11.DepthStencilState bs_D3DDepthStencilState = null;
512                 private SharpDX.Direct3D11.RenderTargetView bs_D3DRenderTargetView = null;
513                 private SharpDX.Direct3D11.Texture2D bs_D3DDepthStencil = null;
514                 private SharpDX.Direct3D11.DepthStencilView bs_D3DDepthStencilView = null;
515                 private SharpDX.Direct3D11.DeviceDebug bs_D3DDeviceDebug = null;
516                 private SharpDX.Direct2D1.Device1 bs_D2DDevice1 = null;
517                 private SharpDX.Direct2D1.DeviceContext1 bs_D2DContext1 = null;
518                 private SharpDX.Direct2D1.Bitmap1 bs_D2DRenderTargetBitmap = null;
519                 //----------------
520                 #endregion
521         }
522 }