MainWindow.cs 13 KB

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