Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

WindowsストアーアプリでSharpDXを動かしてみる

22,806 views

Published on

MetroStyleDeveloper (プチ) #06 大阪 ~メトロスタイルアプリ エキスパート塾++2~で使用したセッション資料です。

  • Be the first to comment

WindowsストアーアプリでSharpDXを動かしてみる

  1. 1. http://sharpdx.org/ Direct3D DirectSound DirectDraw(DirectX 7 で終了) Direct2D (Windows 7 以降) DirectInput などなど
  2. 2. <Rectangle x:Name=“Rectangle1” ...以下略.../>
  3. 3. using SharpDX;using SharpDX.Direct3D;using SharpDX.Direct3D11;using SharpDX.DXGI;using SharpDX.IO;// Direct3D デバイスprivate SharpDX.Direct3D11.Device1 d3dDevice;// Direct3D コンテキストprivate SharpDX.Direct3D11.DeviceContext1 d3dContext;
  4. 4. // Direct3D デバイスの取得var creationFlags = DeviceCreationFlags.VideoSupport | DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug;using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags)){ this.d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();}// Direct3D コンテキストを取得this.d3dContext = d3dDevice.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>();
  5. 5. using Windows.Graphics.Display;using Windows.UI.Xaml.Media.Imaging;// SurfaceImageSourceprivate SurfaceImageSource surfaceImageSource;
  6. 6. // SurfaceImageSource の作成int pixelWidth = (int)(this.Rectangle1.Width * DisplayProperties.LogicalDpi / 96.0);int pixelHeight = (int)(this.Rectangle1.Height * DisplayProperties.LogicalDpi / 96.0);this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight);var brush = new ImageBrush();brush.ImageSource = this.surfaceImageSource;this.Rectangle1.Fill = brush;using (var surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(this.surfaceImageSource)){ surfaceImageSourceNative.Device = this.d3dDevice.QueryInterface<SharpDX.DXGI.Device>();}
  7. 7. // 描画コールバックを開始CompositionTarget.Rendering += CompositionTarget_Rendering;private void CompositionTarget_Rendering(object sender, object e){ int pixelWidth = (int)(this.Rectangle1.Width * DisplayProperties.LogicalDpi / 96.0); int pixelHeight = (int)(this.Rectangle1.Height * DisplayProperties.LogicalDpi / 96.0); var updateRect = new SharpDX.Rectangle(0, 0, pixelWidth, pixelHeight); DrawingPoint offset; using (var surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(this.surfaceImageSource)) using (var surface = surfaceImageSourceNative.BeginDraw(updateRect, out offset)) { // レンダーターゲット作成 Size renderTargetSize; RenderTargetView renderTargetView; using (var backBuffer = surface.QueryInterface<SharpDX.Direct3D11.Texture2D>()) { renderTargetSize = new Size(backBuffer.Description.Width, backBuffer.Description.Height); renderTargetView = new RenderTargetView(this.d3dDevice, backBuffer); } ...続く
  8. 8. ...続き // ビューポート作成 Viewport viewport = new Viewport(0, 0, (int)renderTargetSize.Width, (int)renderTargetSize.Height, 0.0f, 1.0f); // レンダーターゲット、ビューポートをセット this.d3dContext.OutputMerger.SetTargets(renderTargetView); this.d3dContext.Rasterizer.SetViewports(viewport); // 背景クリア this.d3dContext.ClearRenderTargetView(renderTargetView, Colors.Red); // レンダリング this.d3dContext.Draw(36, 0); surfaceImageSourceNative.EndDraw(); }}
  9. 9. 頂点シェーダー GPU で頂点座標を計算ピクセルシェーダー GPU で各ピクセルの色を計算頂点レイアウト GPU に頂点の配置順を教える頂点バッファー 頂点の集まり頂点インデックス 頂点の並び順定数バッファー GPU の変数を渡すレンダーターゲットビュー ポリゴンの色を描画深度バッファー ポリゴンの奥行き値を描画
  10. 10. // 頂点シェーダーprivate VertexShader vertexShader;// ピクセルシェーダーprivate PixelShader pixelShader;// 頂点レイアウトprivate InputLayout layout;// 頂点バッファーprivate VertexBufferBinding vertexBufferBinding;// 定数バッファーprivate SharpDX.Direct3D11.Buffer constantBuffer;
  11. 11. // 頂点シェーダー、ピクセルシェーダーの読み込み、作成var installPath = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;var vertexShaderByteCode = NativeFile.ReadAllBytes(Path.Combine(installPath, "SimpleVertexShader.cso"));this.vertexShader = new VertexShader(this.d3dDevice, vertexShaderByteCode);this.pixelShader = new PixelShader(this.d3dDevice, NativeFile.ReadAllBytes(Path.Combine(installPath, "SimplePixelShader.cso")));// 頂点レイアウト作成this.layout = new InputLayout(this.d3dDevice, vertexShaderByteCode, new[]{ new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)}); ...続く
  12. 12. ...続き// 頂点バッファー作成var vertices = SharpDX.Direct3D11.Buffer.Create(this.d3dDevice, BindFlags.VertexBuffer, new[]{ new Vector4( 0.0f, 1.3f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front new Vector4( 1.1f, -0.6f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector4(-1.1f, -0.6f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),});this.vertexBufferBinding = new VertexBufferBinding(vertices, Utilities.SizeOf<Vector4>() * 2, 0);// 定数バッファー作成this.constantBuffer = new SharpDX.Direct3D11.Buffer(d3dDevice, Utilities.SizeOf<SharpDX.Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
  13. 13. // レンダリングパイプライン構築this.d3dContext.InputAssembler.SetVertexBuffers(0, this.vertexBufferBinding);this.d3dContext.InputAssembler.InputLayout = this.layout;this.d3dContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;this.d3dContext.VertexShader.SetConstantBuffer(0, this.constantBuffer);this.d3dContext.VertexShader.Set(this.vertexShader);this.d3dContext.PixelShader.Set(this.pixelShader);// 定数バッファーに変換行列をセットvar projection = SharpDX.Matrix.OrthoLH(4.0f, 4.0f * (float)renderTargetSize.Height / (float)renderTargetSize.Width, 0f, 2f);var worldViewProjection = SharpDX.Matrix.RotationZ(2f * (float)Math.PI * (float)DateTime.Now.Millisecond / 1000f) * projection;worldViewProjection.Transpose();this.d3dContext.UpdateSubresource(ref worldViewProjection, this.constantBuffer, 0);
  14. 14. http://code.msdn.microsoft.com/windowsapps/site/search?f%5B0%5D.Type=Technology&f%5B0%5D.Value=DirectX

×