More Related Content Similar to WindowsストアーアプリでSharpDXを動かしてみる (20) More from ShinichiAoyagi (10) WindowsストアーアプリでSharpDXを動かしてみる2. http://sharpdx.org/
Direct3D DirectSound
DirectDraw(DirectX 7 で終了) Direct2D (Windows 7 以降)
DirectInput などなど
8. 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;
9. // 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>();
11. // 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>();
}
12. // 描画コールバックを開始
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);
}
...続く
13. ...続き
// ビューポート作成
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();
}
}
14. 頂点シェーダー GPU で頂点座標を計算
ピクセルシェーダー GPU で各ピクセルの色を計算
頂点レイアウト GPU に頂点の配置順を教える
頂点バッファー 頂点の集まり
頂点インデックス 頂点の並び順
定数バッファー GPU の変数を渡す
レンダーターゲットビュー ポリゴンの色を描画
深度バッファー ポリゴンの奥行き値を描画
16. // 頂点シェーダー
private VertexShader vertexShader;
// ピクセルシェーダー
private PixelShader pixelShader;
// 頂点レイアウト
private InputLayout layout;
// 頂点バッファー
private VertexBufferBinding vertexBufferBinding;
// 定数バッファー
private SharpDX.Direct3D11.Buffer constantBuffer;
17. // 頂点シェーダー、ピクセルシェーダーの読み込み、作成
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)
});
...続く
18. ...続き
// 頂点バッファー作成
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);
19. // レンダリングパイプライン構築
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);