OSDN Git Service

デバイスリソースの実装を完了。
[strokestylet/CsWin10Desktop3.git] / FDK24 / Utilities.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.Linq;
5
6 namespace FDK
7 {
8         public static class Utilities
9         {
10                 /// <summary>
11                 /// Dispose して、null を代入する。
12                 /// </summary>
13                 /// <param name="obj">IDisposable を実装するオブジェクト。</param>
14                 public static void 解放する<T>( ref T obj ) where T : IDisposable
15                 {
16                         ( obj as IDisposable )?.Dispose();
17                         obj = default( T );
18                 }
19
20                 /// <summary>
21                 /// 画像ファイルからシェーダリソースビューを作成して返す。
22                 /// D3DX11CreateShaderResourceViewFromFile() の代わり。
23                 /// </summary>
24                 public static SharpDX.Direct3D11.ShaderResourceView CreateShaderResourceViewFromFile(
25                         SharpDX.Direct3D11.Device d3dDevice, string 画像ファイルパス, out SharpDX.Size2F ビューのサイズ )
26                 {
27                         ビューのサイズ = new SharpDX.Size2F( 0, 0 );
28                         var srv = (SharpDX.Direct3D11.ShaderResourceView) null;
29                         using( var image = new System.Drawing.Bitmap( 画像ファイルパス ) )
30                         {
31                                 var 画像の矩形 = new System.Drawing.Rectangle( 0, 0, image.Width, image.Height );
32                                 using( var bitmap = image.Clone( 画像の矩形, System.Drawing.Imaging.PixelFormat.Format32bppArgb ) )
33                                 {
34                                         var ロック領域 = bitmap.LockBits( 画像の矩形, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat );
35                                         var dataBox = new[] { new SharpDX.DataBox( ロック領域.Scan0, bitmap.Width * 4, bitmap.Height ) };
36                                         var textureDesc = new SharpDX.Direct3D11.Texture2DDescription() {
37                                                 ArraySize = 1,
38                                                 BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
39                                                 CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
40                                                 Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
41                                                 Height = bitmap.Height,
42                                                 Width = bitmap.Width,
43                                                 MipLevels = 1,
44                                                 OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
45                                                 SampleDescription = new SharpDX.DXGI.SampleDescription( 1, 0 ),
46                                                 Usage = SharpDX.Direct3D11.ResourceUsage.Default
47                                         };
48                                         using( var texture = new SharpDX.Direct3D11.Texture2D( d3dDevice, textureDesc, dataBox ) )
49                                         {
50                                                 bitmap.UnlockBits( ロック領域 );
51                                                 srv = new SharpDX.Direct3D11.ShaderResourceView( d3dDevice, texture );
52                                         }
53                                 }
54                                 // 戻り値1:ビューのサイズ
55                                 ビューのサイズ = new SharpDX.Size2F( 画像の矩形.Width, 画像の矩形.Height );
56                         }
57                         // 戻り値2:シェーダリソースビュー
58                         return srv;
59                 }
60         }
61 }