MainWindow.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Direct2D1;
  5. using Avalonia.Direct2D1.Media;
  6. using Avalonia.Markup.Xaml;
  7. using Avalonia.Platform;
  8. using Avalonia.Rendering;
  9. using SharpDX;
  10. using SharpDX.D3DCompiler;
  11. using SharpDX.Direct2D1;
  12. using SharpDX.Direct3D;
  13. using SharpDX.Direct3D11;
  14. using SharpDX.DXGI;
  15. using AlphaMode = SharpDX.Direct2D1.AlphaMode;
  16. using Buffer = SharpDX.Direct3D11.Buffer;
  17. using DeviceContext = SharpDX.Direct2D1.DeviceContext;
  18. using Factory2 = SharpDX.DXGI.Factory2;
  19. using InputElement = SharpDX.Direct3D11.InputElement;
  20. using Matrix = SharpDX.Matrix;
  21. using PixelFormat = SharpDX.Direct2D1.PixelFormat;
  22. using Resource = SharpDX.Direct3D11.Resource;
  23. namespace Direct3DInteropSample
  24. {
  25. public class MainWindow : Window
  26. {
  27. Texture2D _backBuffer;
  28. RenderTargetView _renderView;
  29. Texture2D _depthBuffer;
  30. DepthStencilView _depthView;
  31. private readonly SwapChain _swapChain;
  32. private SwapChainDescription1 _desc;
  33. private Matrix _proj = Matrix.Identity;
  34. private readonly Matrix _view;
  35. private Buffer _contantBuffer;
  36. private DeviceContext _deviceContext;
  37. private readonly MainWindowViewModel _model;
  38. public MainWindow()
  39. {
  40. DataContext = _model = new MainWindowViewModel();
  41. _desc = new SwapChainDescription1()
  42. {
  43. BufferCount = 1,
  44. Width = (int)ClientSize.Width,
  45. Height = (int)ClientSize.Height,
  46. Format = Format.R8G8B8A8_UNorm,
  47. SampleDescription = new SampleDescription(1, 0),
  48. SwapEffect = SwapEffect.Discard,
  49. Usage = Usage.RenderTargetOutput
  50. };
  51. using (var factory = Direct2D1Platform.DxgiDevice.Adapter.GetParent<Factory2>())
  52. {
  53. _swapChain = new SwapChain1(factory, Direct2D1Platform.DxgiDevice, PlatformImpl?.Handle.Handle ?? IntPtr.Zero, ref _desc);
  54. }
  55. _deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None)
  56. {
  57. DotsPerInch = new Size2F(96, 96)
  58. };
  59. CreateMesh();
  60. _view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
  61. this.GetObservable(ClientSizeProperty).Subscribe(Resize);
  62. Resize(ClientSize);
  63. AvaloniaXamlLoader.Load(this);
  64. Background = Avalonia.Media.Brushes.Transparent;
  65. }
  66. protected override void HandlePaint(Rect rect)
  67. {
  68. var viewProj = Matrix.Multiply(_view, _proj);
  69. var context = Direct2D1Platform.Direct3D11Device.ImmediateContext;
  70. // Clear views
  71. context.ClearDepthStencilView(_depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
  72. context.ClearRenderTargetView(_renderView, Color.White);
  73. // Update WorldViewProj Matrix
  74. var worldViewProj = Matrix.RotationX((float)_model.RotationX) * Matrix.RotationY((float)_model.RotationY)
  75. * Matrix.RotationZ((float)_model.RotationZ)
  76. * Matrix.Scaling((float)_model.Zoom)
  77. * viewProj;
  78. worldViewProj.Transpose();
  79. context.UpdateSubresource(ref worldViewProj, _contantBuffer);
  80. // Draw the cube
  81. context.Draw(36, 0);
  82. base.HandlePaint(rect);
  83. // Present!
  84. _swapChain.Present(0, PresentFlags.None);
  85. }
  86. private void CreateMesh()
  87. {
  88. var device = Direct2D1Platform.Direct3D11Device;
  89. // Compile Vertex and Pixel shaders
  90. var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "VS", "vs_4_0");
  91. var vertexShader = new VertexShader(device, vertexShaderByteCode);
  92. var pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "PS", "ps_4_0");
  93. var pixelShader = new PixelShader(device, pixelShaderByteCode);
  94. var signature = ShaderSignature.GetInputSignature(vertexShaderByteCode);
  95. var inputElements = new[]
  96. {
  97. new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
  98. new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
  99. };
  100. // Layout from VertexShader input signature
  101. var layout = new InputLayout(
  102. device,
  103. signature,
  104. inputElements);
  105. // Instantiate Vertex buffer from vertex data
  106. var vertices = Buffer.Create(
  107. device,
  108. BindFlags.VertexBuffer,
  109. new[]
  110. {
  111. new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front
  112. new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
  113. new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
  114. new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
  115. new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
  116. new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
  117. new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // BACK
  118. new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
  119. new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
  120. new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
  121. new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
  122. new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
  123. new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Top
  124. new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
  125. new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
  126. new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
  127. new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
  128. new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
  129. new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), // Bottom
  130. new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
  131. new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
  132. new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
  133. new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
  134. new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
  135. new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), // Left
  136. new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
  137. new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
  138. new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
  139. new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
  140. new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
  141. new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), // Right
  142. new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
  143. new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
  144. new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
  145. new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
  146. new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
  147. });
  148. // Create Constant Buffer
  149. _contantBuffer = new Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
  150. var context = Direct2D1Platform.Direct3D11Device.ImmediateContext;
  151. // Prepare All the stages
  152. context.InputAssembler.InputLayout = layout;
  153. context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
  154. context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Utilities.SizeOf<Vector4>() * 2, 0));
  155. context.VertexShader.SetConstantBuffer(0, _contantBuffer);
  156. context.VertexShader.Set(vertexShader);
  157. context.PixelShader.Set(pixelShader);
  158. }
  159. private void Resize(Size size)
  160. {
  161. Utilities.Dispose(ref _deviceContext);
  162. Utilities.Dispose(ref _backBuffer);
  163. Utilities.Dispose(ref _renderView);
  164. Utilities.Dispose(ref _depthBuffer);
  165. Utilities.Dispose(ref _depthView);
  166. var context = Direct2D1Platform.Direct3D11Device.ImmediateContext;
  167. // Resize the backbuffer
  168. _swapChain.ResizeBuffers(0, 0, 0, Format.Unknown, SwapChainFlags.None);
  169. // Get the backbuffer from the swapchain
  170. _backBuffer = Resource.FromSwapChain<Texture2D>(_swapChain, 0);
  171. // Renderview on the backbuffer
  172. _renderView = new RenderTargetView(Direct2D1Platform.Direct3D11Device, _backBuffer);
  173. // Create the depth buffer
  174. _depthBuffer = new Texture2D(
  175. Direct2D1Platform.Direct3D11Device,
  176. new Texture2DDescription()
  177. {
  178. Format = Format.D32_Float_S8X24_UInt,
  179. ArraySize = 1,
  180. MipLevels = 1,
  181. Width = (int)size.Width,
  182. Height = (int)size.Height,
  183. SampleDescription = new SampleDescription(1, 0),
  184. Usage = ResourceUsage.Default,
  185. BindFlags = BindFlags.DepthStencil,
  186. CpuAccessFlags = CpuAccessFlags.None,
  187. OptionFlags = ResourceOptionFlags.None
  188. });
  189. // Create the depth buffer view
  190. _depthView = new DepthStencilView(Direct2D1Platform.Direct3D11Device, _depthBuffer);
  191. // Setup targets and viewport for rendering
  192. context.Rasterizer.SetViewport(new Viewport(0, 0, (int)size.Width, (int)size.Height, 0.0f, 1.0f));
  193. context.OutputMerger.SetTargets(_depthView, _renderView);
  194. // Setup new projection matrix with correct aspect ratio
  195. _proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)(size.Width / size.Height), 0.1f, 100.0f);
  196. using (var dxgiBackBuffer = _swapChain.GetBackBuffer<Surface>(0))
  197. {
  198. var renderTarget = new SharpDX.Direct2D1.RenderTarget(
  199. Direct2D1Platform.Direct2D1Factory,
  200. dxgiBackBuffer,
  201. new RenderTargetProperties
  202. {
  203. DpiX = 96,
  204. DpiY = 96,
  205. Type = RenderTargetType.Default,
  206. PixelFormat = new PixelFormat(
  207. Format.Unknown,
  208. AlphaMode.Premultiplied)
  209. });
  210. _deviceContext = renderTarget.QueryInterface<DeviceContext>();
  211. renderTarget.Dispose();
  212. }
  213. }
  214. private class D3DRenderTarget : IRenderTarget
  215. {
  216. private readonly MainWindow _window;
  217. public D3DRenderTarget(MainWindow window)
  218. {
  219. _window = window;
  220. }
  221. public void Dispose()
  222. {
  223. }
  224. public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer)
  225. {
  226. return new DrawingContextImpl(visualBrushRenderer, null, _window._deviceContext);
  227. }
  228. }
  229. protected override IRenderTarget CreateRenderTarget() => new D3DRenderTarget(this);
  230. }
  231. }