OSDN Git Service

スワップチェーンのフラグを、None から AllowModeSwitch に戻した。
[strokestylet/CsWin10Desktop3.git] / FDK24 / メディア / デバイスリソース.cs
index 10ef8aa..85beea7 100644 (file)
@@ -5,10 +5,10 @@ using System.Linq;
 
 namespace FDK.メディア
 {
-       public class デバイスリソース
+       public class デバイスリソース : IDisposable
        {
-               public SharpDX.Size2F 設計画面サイズdpx = new SharpDX.Size2F( 640f, 480f );
-               public SharpDX.Size2F 物理画面サイズpx = new SharpDX.Size2F( 0, 0 );     // (0, 0) は、サイズ依存リソース無効の印。
+               public SharpDX.Size2F 設計画面サイズdpx = new SharpDX.Size2F( 1920f, 1080f );    // 設計サイズ。
+               public SharpDX.Size2F 物理画面サイズpx = new SharpDX.Size2F( 0, 0 );   // (0, 0) は、サイズ依存リソース無効の印。
                public IntPtr ウィンドウハンドル = IntPtr.Zero;
                public float 視野角deg { get; set; } = 45f;
                public SharpDX.Matrix ビュー変換行列
@@ -37,283 +37,439 @@ namespace FDK.メディア
                                return mat;
                        }
                }
-               public SharpDX.Direct3D11.Device D3DDevice => this.bs_D3DDevice;
-               public SharpDX.DXGI.SwapChain SwapChain => this.bs_SwapChain;
-               public SharpDX.Direct3D11.RenderTargetView RenderTargetView => this.bs_RenderTargetView;
-               public SharpDX.Mathematics.Interop.RawViewportF[] ViewPort => this.bs_ViewPort;
-               public SharpDX.Direct3D11.Texture2D DepthStencil => this.bs_DepthStencil;
-               public SharpDX.Direct3D11.DepthStencilView DepthStencilView => this.bs_DepthStencilView;
-               public SharpDX.Direct3D11.DepthStencilState DepthStencilState => this.bs_DepthStencilState;
-               public SharpDX.Direct3D11.BlendState BlendState => this.bs_BlendState;
-               public SharpDX.Direct3D11.RasterizerState RasterizerState => this.bs_RasterizerState;
-               public SharpDX.Direct3D11.ShaderResourceView ShaderResourceView => this.bs_ShaderResourceView;
-               public SharpDX.Direct3D11.SamplerState SamplerState => this.bs_SamplerState;
-               protected SharpDX.Size2F ShaderResourceViewSize;
-
-               public void すべてのリソースを作成する( System.Drawing.Size バックバッファサイズ, IntPtr ウィンドウハンドル )
+               public SharpDX.MediaFoundation.DXGIDeviceManager DXGIDeviceManager => ( this.bs_DXGIDeviceManager );
+               public SharpDX.DXGI.SwapChain1 SwapChain1 => ( this.bs_SwapChain1 );
+               public SharpDX.Direct3D11.RenderTargetView D3DRenderTargetView => ( this.bs_D3DRenderTargetView );
+               public SharpDX.Mathematics.Interop.RawViewportF[] D3DViewPort => ( this.bs_D3DViewPort );
+               public SharpDX.Direct3D11.Texture2D D3DDepthStencil => ( this.bs_D3DDepthStencil );
+               public SharpDX.Direct3D11.DepthStencilView D3DDepthStencilView => ( this.bs_D3DDepthStencilView );
+               public SharpDX.Direct3D11.DepthStencilState D3DDepthStencilState => ( this.bs_D3DDepthStencilState );
+               public SharpDX.Direct3D11.DeviceDebug D3DDeviceDebug => ( this.bs_D3DDeviceDebug );
+               public SharpDX.Direct2D1.Factory2 D2DFactory2 => ( this.bs_D2DFactory2 );
+               public SharpDX.DirectWrite.Factory DWriteFactory => ( this.bs_DWriteFactory );
+               public SharpDX.WIC.ImagingFactory2 WicImagingFactory2 => ( this.bs_WicImagingFactory2 );
+               public SharpDX.Direct2D1.Device1 D2DDevice1 => ( this.bs_D2DDevice1 );
+               public SharpDX.Direct2D1.DeviceContext1 D2DContext1 => ( this.bs_D2DContext1 );
+               public SharpDX.Direct2D1.Bitmap1 D2DRenderTargetBitmap => ( this.bs_D2DRenderTargetBitmap );
+
+               public float 拡大率DPXtoPX横方向 => ( this.物理画面サイズpx.Width / this.設計画面サイズdpx.Width );
+               public float 拡大率DPXtoPX縦方向 => ( this.物理画面サイズpx.Height / this.設計画面サイズdpx.Height );
+               public float 拡大率PXtoDPX横方向 => ( 1f / this.拡大率DPXtoPX横方向 );
+               public float 拡大率PXtoDPX縦方向 => ( 1f / this.拡大率DPXtoPX縦方向 );
+               public SharpDX.Matrix3x2 拡大行列DPXtoPX => SharpDX.Matrix3x2.Scaling( this.拡大率DPXtoPX横方向, this.拡大率DPXtoPX縦方向 );
+               public SharpDX.Matrix3x2 拡大行列PXtoDPX => SharpDX.Matrix3x2.Scaling( this.拡大率PXtoDPX横方向, this.拡大率PXtoDPX縦方向 );
+
+               public void すべてのリソースを作成する( System.Drawing.Size バックバッファサイズpx, IntPtr ウィンドウハンドル )
                {
-                       this.物理画面サイズpx = new SharpDX.Size2F( バックバッファサイズ.Width, バックバッファサイズ.Height );
-                       this.ウィンドウハンドル = ウィンドウハンドル;
+                       try
+                       {
+                               FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
 
-                       this.すべてのリソースを作成する();
+                               this.物理画面サイズpx = new SharpDX.Size2F( バックバッファサイズpx.Width, バックバッファサイズpx.Height );
+                               this.ウィンドウハンドル = ウィンドウハンドル;
+
+                               this.すべてのリソースを作成する();
+                       }
+                       finally
+                       {
+                               FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
+                       }
                }
                protected void すべてのリソースを作成する()
                {
-                       // これらが呼び出し前に設定されていること。
-                       Debug.Assert( ( 0f < this.物理画面サイズpx.Width ) && ( 0f < this.物理画面サイズpx.Height ) );
-                       Debug.Assert( IntPtr.Zero != this.ウィンドウハンドル );
-
-                       #region " デバイス、スワップチェーンを作成する。"
-                       //----------------
-                       // スワップチェーン desc
-                       var swapChainDesc = new SharpDX.DXGI.SwapChainDescription() {
-                               BufferCount = 3,
-                               ModeDescription = new SharpDX.DXGI.ModeDescription(
-                                       (int) this.物理画面サイズpx.Width,
-                                       (int) this.物理画面サイズpx.Height,
-                                       new SharpDX.DXGI.Rational( 60, 1 ),
-                                       SharpDX.DXGI.Format.B8G8R8A8_UNorm
-                               ),
-                               IsWindowed = true,
-                               OutputHandle = ウィンドウハンドル,
-                               SampleDescription = new SharpDX.DXGI.SampleDescription( 1, 0 ),
-                               SwapEffect = SharpDX.DXGI.SwapEffect.Discard,
-                               Usage = SharpDX.DXGI.Usage.RenderTargetOutput,
-                       };
-                       // 機能レベル
-                       var featureLevels = new SharpDX.Direct3D.FeatureLevel[] {
-                               SharpDX.Direct3D.FeatureLevel.Level_11_0,
-                               SharpDX.Direct3D.FeatureLevel.Level_10_1,
-                               SharpDX.Direct3D.FeatureLevel.Level_10_0,
-                       };
-                       // デバイスとスワップチェーンを作成する。
-                       SharpDX.Direct3D11.Device.CreateWithSwapChain(
-                               SharpDX.Direct3D.DriverType.Hardware,
-                               SharpDX.Direct3D11.DeviceCreationFlags.None,
-                               featureLevels,
-                               swapChainDesc,
-                               out this.bs_D3DDevice,
-                               out this.bs_SwapChain );
-
-                       Trace.WriteLine( "D3Dデバイスとスワップチェーンを生成しました。" );
-                       Trace.WriteLine( $"機能レベル: {this.bs_D3DDevice.FeatureLevel.ToString()}" );
-                       //----------------
-                       #endregion
-                       #region " すべての Windows イベントを無視する。具体的には PrintScreen と Alt+Enter 。"
-                       //----------------
-                       using( var factory = this.bs_SwapChain.GetParent<SharpDX.DXGI.Factory>() )
+                       try
                        {
-                               factory.MakeWindowAssociation( ウィンドウハンドル, SharpDX.DXGI.WindowAssociationFlags.IgnoreAll );
+                               FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
+
+                               // これらが呼び出し前に設定されていること。
+                               Debug.Assert( ( 0f < this.物理画面サイズpx.Width ) && ( 0f < this.物理画面サイズpx.Height ) );
+                               Debug.Assert( IntPtr.Zero != this.ウィンドウハンドル );
+
+                               #region " D2DFactory2 を作成する。"
+                               //-----------------
+                               {
+                                       var デバッグレベル = SharpDX.Direct2D1.DebugLevel.None;
+#if DEBUG
+                                       // プロジェクトがデバッグビルドに含まれている場合は、Direct2D デバッグレイヤーを SDK レイヤーを介して有効にする。
+                                       デバッグレベル = SharpDX.Direct2D1.DebugLevel.Information;
+#endif
+                                       this.bs_D2DFactory2 = new SharpDX.Direct2D1.Factory2( SharpDX.Direct2D1.FactoryType.MultiThreaded, デバッグレベル );
+                               }
+                               //-----------------
+                               #endregion
+                               #region " DWriteFactory を作成する。"
+                               //-----------------
+                               this.bs_DWriteFactory = new SharpDX.DirectWrite.Factory( SharpDX.DirectWrite.FactoryType.Shared );
+                               //-----------------
+                               #endregion
+                               #region " WicImagingFactory2 を作成する。"
+                               //-----------------
+                               this.bs_WicImagingFactory2 = new SharpDX.WIC.ImagingFactory2();
+                               //-----------------
+                               #endregion
+
+                               var d3dDevice = (SharpDX.Direct3D11.Device) null;
+
+                               #region " DXGIDeviceManager を作成する。"
+                               //-----------------
+                               this.bs_DXGIDeviceManager = new SharpDX.MediaFoundation.DXGIDeviceManager();
+                               //-----------------
+                               #endregion
+                               #region " D3Dデバイスを作成する。"
+                               //----------------
+                               // 作成フラグ
+                               var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; // D2Dをサポートするなら BgraSupport フラグが必須。
+#if DEBUG
+                               // D3D11 Debugメッセージは、プロジェクトプロパティで「ネイティブコードのデバッグを有効にする」を ON にしないと表示されないので注意。
+                               creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug;
+#endif
+                               d3dDevice = new SharpDX.Direct3D11.Device(
+                                       SharpDX.Direct3D.DriverType.Hardware,
+                                       creationFlags,
+                                       new SharpDX.Direct3D.FeatureLevel[] {
+                                               SharpDX.Direct3D.FeatureLevel.Level_11_1,
+                                               SharpDX.Direct3D.FeatureLevel.Level_11_0,
+                                       } );
+
+                               FDK.Log.Info( "D3Dデバイスを生成しました。" );
+                               FDK.Log.Info( $"機能レベル: {d3dDevice.FeatureLevel.ToString()}" );
+                               //----------------
+                               #endregion
+
+                               using( d3dDevice )
+                               {
+                                       // D3D 関連
+
+                                       #region " スワップチェーンを作成する。"
+                                       //----------------
+                                       using( var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>() )
+                                       using( var dxgiAdapter = dxgiDevice.Adapter )
+                                       using( var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>() )
+                                       {
+                                               var swapChainDesc1 = new SharpDX.DXGI.SwapChainDescription1() {
+                                                       BufferCount = 2,
+                                                       Width = (int) this.物理画面サイズpx.Width,
+                                                       Height = (int) this.物理画面サイズpx.Height,
+                                                       Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,  // D2D をサポートするなら B8G8R8A8 を使う必要がある。
+                                                       Scaling = SharpDX.DXGI.Scaling.Stretch,
+                                                       SampleDescription = new SharpDX.DXGI.SampleDescription( 1, 0 ),
+                                                       SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard,       // Windowsストアアプリはこれが必須。
+                                                       Usage = SharpDX.DXGI.Usage.RenderTargetOutput,
+                                                       //Flags = SharpDX.DXGI.SwapChainFlags.None,
+                                                       Flags = SharpDX.DXGI.SwapChainFlags.AllowModeSwitch,
+                                               };
+                                               this.bs_SwapChain1 = new SharpDX.DXGI.SwapChain1(
+                                                       dxgiFactory2,
+                                                       d3dDevice,
+                                                       this.ウィンドウハンドル,
+                                                       ref swapChainDesc1 );
+                                       }
+                                       //----------------
+                                       #endregion
+                                       #region " デバイスからデバッグオブジェクトを取得する。"
+                                       //----------------
+                                       this.bs_D3DDeviceDebug = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D11.DeviceDebug>();
+                                       //----------------
+                                       #endregion
+                                       #region " D3DDevice が ID3D11VideoDevice を実装してないならエラー。(Win8以降のPCでは実装されているはず。) "
+                                       //-----------------
+                                       using( var videoDevice = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D11.VideoDevice>() )
+                                       {
+                                               if( null == videoDevice )
+                                                       throw new FDKException( "Direct3D11デバイスが、ID3D11VideoDevice をサポートしていません。" );
+                                       }
+                                       //-----------------
+                                       #endregion
+                                       #region " マルチスレッドモードを ON に設定する。DXVAを使う場合は必須。"
+                                       //-----------------
+                                       using( var multithread = d3dDevice.QueryInterfaceOrNull<SharpDX.Direct3D.DeviceMultithread>() )
+                                       {
+                                               if( null == multithread )
+                                                       throw new FDKException( "Direct3D11デバイスが、ID3D10Multithread をサポートしていません。" );
+
+                                               multithread.SetMultithreadProtected( true );
+                                       }
+                                       //-----------------
+                                       #endregion
+                                       #region " DXGIデバイスマネージャに D3Dデバイスを登録する。"
+                                       //-----------------
+                                       this.DXGIDeviceManager.ResetDevice( d3dDevice );
+                                       //-----------------
+                                       #endregion
+                                       #region " すべての Windows イベントを無視する。具体的には PrintScreen と Alt+Enter 。"
+                                       //----------------
+                                       using( var factory = this.bs_SwapChain1.GetParent<SharpDX.DXGI.Factory>() )
+                                       {
+                                               factory.MakeWindowAssociation( ウィンドウハンドル, SharpDX.DXGI.WindowAssociationFlags.IgnoreAll );
+                                       }
+                                       //----------------
+                                       #endregion
+                               }
+
+                               this.サイズに依存するリソースを作成する();
+                       }
+                       finally
+                       {
+                               FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
                        }
-                       //----------------
-                       #endregion
-                       #region " ブレンドステート "
-                       //----------------
-                       var BlendState = new SharpDX.Direct3D11.BlendStateDescription() {
-                               AlphaToCoverageEnable = false,  // アルファマスクで透過する(するならZバッファ必須)
-                               IndependentBlendEnable = false, // 個別設定。false なら BendStateDescription.RenderTarget[0] だけが有効で、[1~7] は無視される。
-                       };
-                       BlendState.RenderTarget[ 0 ].IsBlendEnabled = true; // true ならブレンディングが有効。
-                       BlendState.RenderTarget[ 0 ].RenderTargetWriteMask = SharpDX.Direct3D11.ColorWriteMaskFlags.All;        // RGBA の書き込みマスク。
-
-                       // アルファ値のブレンディング設定 ... 特になし
-                       BlendState.RenderTarget[ 0 ].SourceAlphaBlend = SharpDX.Direct3D11.BlendOption.One;
-                       BlendState.RenderTarget[ 0 ].DestinationAlphaBlend = SharpDX.Direct3D11.BlendOption.Zero;
-                       BlendState.RenderTarget[ 0 ].AlphaBlendOperation = SharpDX.Direct3D11.BlendOperation.Add;
-
-                       // 色値のブレンディング設定 ... アルファ強度に応じた透明合成(テクスチャのアルファ値は、テクスチャのアルファ×ピクセルシェーダでの全体アルファとする(HLSL参照))
-                       BlendState.RenderTarget[ 0 ].SourceBlend = SharpDX.Direct3D11.BlendOption.SourceAlpha;
-                       BlendState.RenderTarget[ 0 ].DestinationBlend = SharpDX.Direct3D11.BlendOption.InverseSourceAlpha;
-                       BlendState.RenderTarget[ 0 ].BlendOperation = SharpDX.Direct3D11.BlendOperation.Add;
-
-                       // ブレンドステートを作成する。
-                       this.bs_BlendState = new SharpDX.Direct3D11.BlendState( this.bs_D3DDevice, BlendState );
-                       //----------------
-                       #endregion
-                       #region " ラスタライザステート "
-                       //----------------
-                       var RSDesc = new SharpDX.Direct3D11.RasterizerStateDescription() {
-                               FillMode = SharpDX.Direct3D11.FillMode.Solid,   // 普通に描画する
-                               CullMode = SharpDX.Direct3D11.CullMode.None,    // 両面を描画する
-                               IsFrontCounterClockwise = false,    // 時計回りが表面
-                               DepthBias = 0,
-                               DepthBiasClamp = 0,
-                               SlopeScaledDepthBias = 0,
-                               IsDepthClipEnabled = true,
-                               IsScissorEnabled = false,
-                               IsMultisampleEnabled = false,
-                               IsAntialiasedLineEnabled = false,
-                       };
-                       this.bs_RasterizerState = new SharpDX.Direct3D11.RasterizerState( this.bs_D3DDevice, RSDesc );
-                       //----------------
-                       #endregion
-                       #region " 深度ステンシルステート "
-                       //----------------
-                       var DepthSencil = new SharpDX.Direct3D11.DepthStencilStateDescription() {
-                               IsDepthEnabled = true,  // 深度テストあり
-                               DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.All,     // 書き込む
-                               DepthComparison = SharpDX.Direct3D11.Comparison.Less,   // 手前の物体を描画
-                               IsStencilEnabled = false,   // ステンシルテストなし。
-                               StencilReadMask = 0,    // ステンシル読み込みマスク。
-                               StencilWriteMask = 0,   // ステンシル書き込みマスク。
-
-                               // 面が表を向いている場合のステンシル・テストの設定
-                               FrontFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
-                                       FailOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
-                                       DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep,  // 維持
-                                       PassOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
-                                       Comparison = SharpDX.Direct3D11.Comparison.Never,   // 常に失敗
-                               },
-
-                               // 面が裏を向いている場合のステンシル・テストの設定
-                               BackFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
-                                       FailOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
-                                       DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep,  // 維持
-                                       PassOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
-                                       Comparison = SharpDX.Direct3D11.Comparison.Always,  // 常に成功
-                               },
-                       };
-                       this.bs_DepthStencilState = new SharpDX.Direct3D11.DepthStencilState( this.bs_D3DDevice, DepthSencil );
-                       //----------------
-                       #endregion
-                       #region " シェーダーリソースビュー "
-                       //----------------
-                       this.bs_ShaderResourceView = FDK.Utilities.CreateShaderResourceViewFromFile(
-                               this.D3DDevice, "テスト用テクスチャ画像.jpg", out this.ShaderResourceViewSize );
-                       //D3DX11CreateShaderResourceViewFromFile( g_pD3DDevice, L"texture.png", NULL, NULL, &g_pTextureSRV, &hr );
-                       //----------------
-                       #endregion
-                       #region " サンプラー "
-                       //----------------
-                       var descSampler = new SharpDX.Direct3D11.SamplerStateDescription() {
-                               Filter = SharpDX.Direct3D11.Filter.Anisotropic,
-                               AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap,
-                               AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap,
-                               AddressW = SharpDX.Direct3D11.TextureAddressMode.Wrap,
-                               MipLodBias = 0.0f,
-                               MaximumAnisotropy = 2,
-                               ComparisonFunction = SharpDX.Direct3D11.Comparison.Never,
-                               BorderColor = new SharpDX.Mathematics.Interop.RawColor4( 0f, 0f, 0f, 0f ),
-                               MinimumLod = float.MinValue,
-                               MaximumLod = float.MaxValue,
-                       };
-                       this.bs_SamplerState = new SharpDX.Direct3D11.SamplerState( this.bs_D3DDevice, descSampler );
-                       //----------------
-                       #endregion
-
-                       this.サイズに依存するリソースを作成する();
                }
                public void すべてのリソースを解放する()
                {
-                       // デバイスステートをクリアする。
-                       this.bs_D3DDevice?.ImmediateContext?.ClearState();
-
-                       // スワップチェインをウインドウモードにする。
-                       this.bs_SwapChain?.SetFullscreenState( fullscreen: false, targetRef: null );
-
-                       this.サイズに依存するリソースを解放する();
-
-                       // 取得したインターフェイスの開放
-                       this.bs_DepthStencilState?.Dispose();
-                       this.bs_SamplerState?.Dispose();
-                       this.bs_ShaderResourceView?.Dispose();
-                       this.bs_RasterizerState?.Dispose();
-                       this.bs_BlendState?.Dispose();
-                       this.bs_SwapChain?.Dispose();
-                       this.bs_D3DDevice?.Dispose();
+                       try
+                       {
+                               FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
+
+                               // D3D 関連
+                               this.bs_SwapChain1?.SetFullscreenState( fullscreen: false, targetRef: null );    // スワップチェインをウインドウモードにする。
+                               this.サイズに依存するリソースを解放する();
+
+                               FDK.Utilities.解放する( ref this.bs_SwapChain1 );
+                               FDK.Utilities.解放する( ref this.bs_DXGIDeviceManager );
+
+                               // その他
+                               FDK.Utilities.解放する( ref this.bs_WicImagingFactory2 );
+                               FDK.Utilities.解放する( ref this.bs_DWriteFactory );
+                               FDK.Utilities.解放する( ref this.bs_D2DFactory2 );
+
+                               this.bs_D3DDeviceDebug?.ReportLiveDeviceObjects( SharpDX.Direct3D11.ReportingLevel.Detail );
+                               FDK.Utilities.解放する( ref this.bs_D3DDeviceDebug );
+                       }
+                       finally
+                       {
+                               FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
+                       }
+               }
+               public void Dispose()
+               {
+                       this.すべてのリソースを解放する();
                }
                public void サイズに依存するリソースを作成する()
                {
-                       #region " スワップチェーンのサイズを変更する。"
-                       //----------------
-                       Debug.Assert( null != this.SwapChain ); // スワップチェーンは、デバイスとともに、すでに生成される。
-                       this.SwapChain.ResizeBuffers(
-                               bufferCount: 2,
-                               width: (int) this.物理画面サイズpx.Width,
-                               height: (int) this.物理画面サイズpx.Height,
-                               newFormat: SharpDX.DXGI.Format.R8G8B8A8_UNorm,
-                               swapChainFlags: SharpDX.DXGI.SwapChainFlags.AllowModeSwitch );
-                       //----------------
-                       #endregion
-
-                       // 新しいサイズになったバックバッファを取得して、リソースを作成する。
-                       using( var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>( this.bs_SwapChain, 0 ) )
+                       try
                        {
-                               #region " RenderTargetView の作成 "
-                               //----------------
-                               this.bs_RenderTargetView = new SharpDX.Direct3D11.RenderTargetView( this.bs_D3DDevice, backBuffer );
-                               //----------------
-                               #endregion
-                               #region " 深度ステンシルテクスチャの作成 "
-                               //----------------
-                               var descDepth = backBuffer.Description;
-                               //descDepth.Width = backBuffer.Description.Width;       → backBuffer に同じ
-                               //descDepth.Height = backBuffer.Description.Height;     → 同上
-                               descDepth.MipLevels = 1;    // ミップマップレベル数
-                               descDepth.ArraySize = 1;    // 配列サイズ
-                               descDepth.Format = SharpDX.DXGI.Format.D32_Float;   // フォーマット(深度のみ)
-                               descDepth.Usage = SharpDX.Direct3D11.ResourceUsage.Default; // デフォルト使用法
-                               descDepth.BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil;    // 深度ステンシル
-                               descDepth.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None;  // CPUからはアクセスしない
-                               descDepth.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None;    // その他の設定なし
-
-                               this.bs_DepthStencil = new SharpDX.Direct3D11.Texture2D( this.bs_D3DDevice, descDepth );
-                               //----------------
-                               #endregion
-                               #region " 深度ステンシルビューの作成 "
-                               //----------------
-                               var descDSV = new SharpDX.Direct3D11.DepthStencilViewDescription() {
-                                       Format = descDepth.Format,
-                                       Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D,
-                                       Flags = SharpDX.Direct3D11.DepthStencilViewFlags.None,
-                               };
-                               descDSV.Texture2D.MipSlice = 0;
-                               this.bs_DepthStencilView = new SharpDX.Direct3D11.DepthStencilView(
-                                       this.bs_D3DDevice,
-                                       this.bs_DepthStencil,
-                                       descDSV );
-                               //----------------
-                               #endregion
-                               #region " ビューポートの設定 "
-                               //----------------
-                               this.bs_ViewPort[ 0 ] = new SharpDX.Mathematics.Interop.RawViewportF() {
-                                       X = 0.0f,
-                                       Y = 0.0f,
-                                       Width = (float) backBuffer.Description.Width,
-                                       Height = (float) backBuffer.Description.Height,
-                                       MinDepth = 0.0f,
-                                       MaxDepth = 1.0f,
-                               };
-                               //----------------
-                               #endregion
-                               #region " テクスチャの共有リソースの作成 "
+                               FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
+
+                               #region " スワップチェーンのサイズを変更する。"
                                //----------------
-                               FDK.メディア.テクスチャ.共有リソースを作成する( this );
+                               Debug.Assert( null != this.SwapChain1 ); // スワップチェーンは(デバイスとともに)すでに生成されていること。
+
+                               // ResizeTarget は、全画面モードにしたとき、モニタ画面の解像度も変更する。
+                               //var mode = new SharpDX.DXGI.ModeDescription(
+                               //      width: (int) this.物理画面サイズpx.Width,
+                               //      height: (int) this.物理画面サイズpx.Height,
+                               //      refreshRate: new SharpDX.DXGI.Rational( 60000, 1001 ),  // 多くのモニタは(60,1)じゃない;一致しないとFlipじゃなくBlitになるので注意(MSDN)。
+                               //      format: SharpDX.DXGI.Format.B8G8R8A8_UNorm );
+                               //this.SwapChain.ResizeTarget( ref mode );
+
+                               // ResizeBuffers は、全画面モードにしたとき、モニタの解像度を変更しない。
+                               // ただし、swapChainFlags に AllowModeSwitch を指定すると変更される(ResizeTargetと同じ挙動になる)。
+                               this.SwapChain1.ResizeBuffers(
+                                       bufferCount: 2,
+                                       width: (int) this.物理画面サイズpx.Width,
+                                       height: (int) this.物理画面サイズpx.Height,
+                                       newFormat: SharpDX.DXGI.Format.B8G8R8A8_UNorm,
+                                       //swapChainFlags: SharpDX.DXGI.SwapChainFlags.None );
+                                       swapChainFlags: SharpDX.DXGI.SwapChainFlags.AllowModeSwitch );
                                //----------------
                                #endregion
+
+                               // バックバッファを使って、D3D / D2D 関連のリソースを作成する。
+                               using( var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>( this.bs_SwapChain1, 0 ) )
+                               {
+                                       var d3dDevice = (SharpDX.Direct3D11.Device) null;
+                                       using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
+                                       using( d3dDevice )
+                                       {
+                                               // D3D 関連
+                                               #region " レンダーターゲットビューを作成する。"
+                                               //----------------
+                                               this.bs_D3DRenderTargetView = new SharpDX.Direct3D11.RenderTargetView( d3dDevice, backBuffer );
+                                               //----------------
+                                               #endregion
+                                               #region " 深度ステンシルテクスチャを作成する。"
+                                               //----------------
+                                               var descDepth = backBuffer.Description;
+                                               //descDepth.Width = backBuffer.Description.Width;       → backBuffer に同じ
+                                               //descDepth.Height = backBuffer.Description.Height;     → 同上
+                                               descDepth.MipLevels = 1;    // ミップマップレベル数
+                                               descDepth.ArraySize = 1;    // 配列サイズ
+                                               descDepth.Format = SharpDX.DXGI.Format.D32_Float;   // フォーマット(深度のみ)
+                                               descDepth.Usage = SharpDX.Direct3D11.ResourceUsage.Default; // デフォルト使用法
+                                               descDepth.BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil;    // 深度ステンシル
+                                               descDepth.CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None;  // CPUからはアクセスしない
+                                               descDepth.OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None;    // その他の設定なし
+                                               this.bs_D3DDepthStencil = new SharpDX.Direct3D11.Texture2D( d3dDevice, descDepth );
+                                               //----------------
+                                               #endregion
+                                               #region " 深度ステンシルビューを作成する。"
+                                               //----------------
+                                               var descDSV = new SharpDX.Direct3D11.DepthStencilViewDescription() {
+                                                       Format = descDepth.Format,
+                                                       Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D,
+                                                       Flags = SharpDX.Direct3D11.DepthStencilViewFlags.None,
+                                                       Texture2D = new SharpDX.Direct3D11.DepthStencilViewDescription.Texture2DResource() {
+                                                               MipSlice = 0,
+                                                       },
+                                               };
+                                               this.bs_D3DDepthStencilView = new SharpDX.Direct3D11.DepthStencilView( d3dDevice, this.bs_D3DDepthStencil, descDSV );
+                                               //----------------
+                                               #endregion
+                                               #region " 深度ステンシルステートを作成する。"
+                                               //----------------
+                                               var DepthSencil = new SharpDX.Direct3D11.DepthStencilStateDescription() {
+                                                       IsDepthEnabled = true,  // 深度テストあり
+                                                       DepthWriteMask = SharpDX.Direct3D11.DepthWriteMask.All,     // 書き込む
+                                                       DepthComparison = SharpDX.Direct3D11.Comparison.Less,   // 手前の物体を描画
+                                                       IsStencilEnabled = false,   // ステンシルテストなし。
+                                                       StencilReadMask = 0,    // ステンシル読み込みマスク。
+                                                       StencilWriteMask = 0,   // ステンシル書き込みマスク。
+
+                                                       // 面が表を向いている場合のステンシル・テストの設定
+                                                       FrontFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
+                                                               FailOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
+                                                               DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep,  // 維持
+                                                               PassOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
+                                                               Comparison = SharpDX.Direct3D11.Comparison.Never,   // 常に失敗
+                                                       },
+
+                                                       // 面が裏を向いている場合のステンシル・テストの設定
+                                                       BackFace = new SharpDX.Direct3D11.DepthStencilOperationDescription() {
+                                                               FailOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
+                                                               DepthFailOperation = SharpDX.Direct3D11.StencilOperation.Keep,  // 維持
+                                                               PassOperation = SharpDX.Direct3D11.StencilOperation.Keep,   // 維持
+                                                               Comparison = SharpDX.Direct3D11.Comparison.Always,  // 常に成功
+                                                       },
+                                               };
+                                               this.bs_D3DDepthStencilState = new SharpDX.Direct3D11.DepthStencilState( d3dDevice, DepthSencil );
+                                               //----------------
+                                               #endregion
+                                               #region " ビューポートを作成する。"
+                                               //----------------
+                                               this.bs_D3DViewPort[ 0 ] = new SharpDX.Mathematics.Interop.RawViewportF() {
+                                                       X = 0.0f,
+                                                       Y = 0.0f,
+                                                       Width = (float) backBuffer.Description.Width,   // 物理画面単位[px]
+                                                       Height = (float) backBuffer.Description.Height, // 物理画面単位[px]
+                                                       MinDepth = 0.0f,
+                                                       MaxDepth = 1.0f,
+                                               };
+                                               //----------------
+                                               #endregion
+                                               #region " 全テクスチャで共有するリソースを作成する。"
+                                               //----------------
+                                               FDK.メディア.テクスチャ.全インスタンスで共有するリソースを作成する( this );
+                                               //----------------
+                                               #endregion
+
+                                               // D2D 関連
+                                               using( var backsurface = SharpDX.DXGI.Surface.FromSwapChain( this.bs_SwapChain1, 0 ) )
+                                               {
+                                                       #region " D2DDevice を作成する。"
+                                                       //-----------------
+                                                       using( var dxgiDevice = d3dDevice.QueryInterfaceOrNull<SharpDX.DXGI.Device>() )
+                                                       {
+                                                               if( null == dxgiDevice )
+                                                                       throw new FDKException( "Direct3D11デバイスが、IDXGIDevice3 をサポートしていません。" );
+
+                                                               this.bs_D2DDevice1 = new SharpDX.Direct2D1.Device1( this.D2DFactory2, dxgiDevice );
+                                                       }
+                                                       //-----------------
+                                                       #endregion
+                                                       #region " D2Dの既定のデバイスコンテキストを作成する。"
+                                                       //-----------------
+                                                       this.bs_D2DContext1 = new SharpDX.Direct2D1.DeviceContext1( this.D2DDevice1, SharpDX.Direct2D1.DeviceContextOptions.None );
+
+                                                       // 現在のディスプレイDPI を取得し、D2DContext に設定する。
+                                                       //this.D2DContext1.DotsPerInch = this.D2DFactory2.DesktopDpi;
+                                                       this.D2DContext1.DotsPerInch = new SharpDX.Size2F( 96f, 96f );
+                                                       //-----------------
+                                                       #endregion
+                                                       #region " D2Dの既定のレンダーターゲットビットマップを作成する。"
+                                                       //-----------------
+                                                       var dpi = this.D2DContext1.DotsPerInch;
+
+                                                       // DXGIスワップチェーンのバックバッファとデータを共有するD2Dターゲットビットマップを作成する。ビューではなく共有リソース。
+                                                       this.bs_D2DRenderTargetBitmap = new SharpDX.Direct2D1.Bitmap1(
+                                                               this.D2DContext1,   // このコンテキストを通じて、
+                                                               backsurface, // バックバッファとデータを共有する。
+                                                               new SharpDX.Direct2D1.BitmapProperties1(
+                                                                       new SharpDX.Direct2D1.PixelFormat( backsurface.Description.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied ),
+                                                                       dpi.Width,
+                                                                       dpi.Height,
+                                                                       SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw ) );
+
+                                                       // ここでもうD2Dのレンダーターゲットとして登録しておく。
+                                                       this.D2DContext1.Target = this.bs_D2DRenderTargetBitmap;
+                                                       //-----------------
+                                                       #endregion
+                                                       #region " テキストのアンチエイリアシングを設定する。"
+                                                       //-----------------
+                                                       // Grayscale が、すべての Windows ストアアプリで推奨される。らしい。
+                                                       this.D2DContext1.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
+                                                       //-----------------
+                                                       #endregion
+                                               }
+                                       }
+                               }
+                       }
+                       finally
+                       {
+                               FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
                        }
                }
                public void サイズに依存するリソースを解放する()
                {
-                       // 描画ターゲットを解除する。
-                       this.D3DDevice.ImmediateContext.OutputMerger.ResetTargets();
+                       try
+                       {
+                               FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
+
+                               // D2D 関連
+                               if( null != this.bs_D2DContext1 )
+                                       this.bs_D2DContext1.Target = null;
+                               FDK.Utilities.解放する( ref this.bs_D2DRenderTargetBitmap );
+                               FDK.Utilities.解放する( ref this.bs_D2DContext1 );
+                               FDK.Utilities.解放する( ref this.bs_D2DDevice1 );
+
+                               // D3D 関連
+                               var d3dDevice = (SharpDX.Direct3D11.Device) null;
+                               using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
+                               using( d3dDevice )
+                               using( var d3dContext = d3dDevice.ImmediateContext )
+                               {
+                                       d3dContext.ClearState();
+                                       d3dContext.OutputMerger.ResetTargets();
 
-                       FDK.メディア.テクスチャ.共有リソースを解放する();
-                       this.bs_DepthStencilView?.Dispose();
-                       this.bs_DepthStencil?.Dispose();
-                       this.bs_RenderTargetView?.Dispose();
-                       //this.bs_SwapChain?.Dispose(); → スワップチェーンは解放しない(生成・解放はデバイスとセット)。
+                                       FDK.メディア.テクスチャ.全インスタンスで共有するリソースを解放する();
 
-                       // (0,0)は、サイズ依存リソース無効の印。
-                       this.物理画面サイズpx = new SharpDX.Size2F( 0, 0 );
+                                       FDK.Utilities.解放する( ref this.bs_D3DDepthStencilState );
+                                       FDK.Utilities.解放する( ref this.bs_D3DDepthStencilView );
+                                       FDK.Utilities.解放する( ref this.bs_D3DDepthStencil );
+                                       FDK.Utilities.解放する( ref this.bs_D3DRenderTargetView );
+                                       //FDK.Utilities.解放する( ref this.bs_SwapChain ); → スワップチェーンは解放しない(これの生成・解放はデバイスとセットで行う)。
+
+                                       // (0,0)は、サイズ依存リソース無効の印。
+                                       this.物理画面サイズpx = new SharpDX.Size2F( 0, 0 );
+                               }
+                       }
+                       finally
+                       {
+                               FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
+                       }
                }
                public void D3Dデバイスが消失していれば再構築する( out bool 異常状態なのでアプリを終了せよ )
                {
                        異常状態なのでアプリを終了せよ = false;
+                       SharpDX.Result 削除理由;
+
+                       var d3dDevice = (SharpDX.Direct3D11.Device) null;
+                       using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.DXGIDeviceManager, out d3dDevice ) )
+                       using( d3dDevice )
+                       {
+                               削除理由 = d3dDevice.DeviceRemovedReason;
+                       }
 
-                       var 削除理由 = this.bs_D3DDevice.DeviceRemovedReason;
                        if( 削除理由.Success )
-                               return;
+                               return; // デバイスは消失していない。
 
                        var エラー詳細 = new[] {
                                        new { Code = SharpDX.DXGI.ResultCode.DeviceHung.Code, Info = SharpDX.DXGI.ResultCode.DeviceHung.ApiCode, Rebuild = true },
@@ -335,6 +491,11 @@ namespace FDK.メディア
                                異常状態なのでアプリを終了せよ = true;
                        }
                }
+               public void D2DContextの設定をリセットする( SharpDX.Direct2D1.DeviceContext1 context1 )
+               {
+                       context1.Transform = SharpDX.Matrix3x2.Identity;
+                       context1.PrimitiveBlend = SharpDX.Direct2D1.PrimitiveBlend.SourceOver;
+               }
 
                private float dz( float 高さdpx, float 視野角deg )
                {
@@ -343,17 +504,20 @@ namespace FDK.メディア
 
                #region " バックストア。"
                //----------------
-               private SharpDX.Direct3D11.Device bs_D3DDevice = null;
-               private SharpDX.DXGI.SwapChain bs_SwapChain = null;
-               private SharpDX.Mathematics.Interop.RawViewportF[] bs_ViewPort = new SharpDX.Mathematics.Interop.RawViewportF[ 1 ];
-               private SharpDX.Direct3D11.DepthStencilState bs_DepthStencilState = null;
-               private SharpDX.Direct3D11.BlendState bs_BlendState = null;
-               private SharpDX.Direct3D11.RasterizerState bs_RasterizerState = null;
-               private SharpDX.Direct3D11.ShaderResourceView bs_ShaderResourceView = null;
-               private SharpDX.Direct3D11.SamplerState bs_SamplerState = null;
-               private SharpDX.Direct3D11.RenderTargetView bs_RenderTargetView = null;
-               private SharpDX.Direct3D11.Texture2D bs_DepthStencil = null;
-               private SharpDX.Direct3D11.DepthStencilView bs_DepthStencilView = null;
+               private SharpDX.Direct2D1.Factory2 bs_D2DFactory2 = null;
+               private SharpDX.DirectWrite.Factory bs_DWriteFactory = null;
+               private SharpDX.WIC.ImagingFactory2 bs_WicImagingFactory2 = null;
+               private SharpDX.MediaFoundation.DXGIDeviceManager bs_DXGIDeviceManager = null;
+               private SharpDX.DXGI.SwapChain1 bs_SwapChain1 = null;
+               private SharpDX.Mathematics.Interop.RawViewportF[] bs_D3DViewPort = new SharpDX.Mathematics.Interop.RawViewportF[ 1 ];
+               private SharpDX.Direct3D11.DepthStencilState bs_D3DDepthStencilState = null;
+               private SharpDX.Direct3D11.RenderTargetView bs_D3DRenderTargetView = null;
+               private SharpDX.Direct3D11.Texture2D bs_D3DDepthStencil = null;
+               private SharpDX.Direct3D11.DepthStencilView bs_D3DDepthStencilView = null;
+               private SharpDX.Direct3D11.DeviceDebug bs_D3DDeviceDebug = null;
+               private SharpDX.Direct2D1.Device1 bs_D2DDevice1 = null;
+               private SharpDX.Direct2D1.DeviceContext1 bs_D2DContext1 = null;
+               private SharpDX.Direct2D1.Bitmap1 bs_D2DRenderTargetBitmap = null;
                //----------------
                #endregion
        }