Advertisement
Guest User

Working minimal DirectX12 (horrible code)

a guest
Jun 25th, 2017
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.30 KB | None | 0 0
  1. using SharpDX.Direct3D12;
  2. using SharpDX.Mathematics.Interop;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace DirectX12BlueScreen2
  14. {
  15. class Program
  16. {
  17. private static Stack<IDisposable> disposables =
  18. new Stack<IDisposable>();
  19.  
  20. static void Main(string[] args)
  21. {
  22. try
  23. {
  24. Form form; disposables.Push(form = new Form());
  25. form.Width = 640;
  26. form.Height = 480;
  27. form.Visible = true;
  28. var watch = Stopwatch.StartNew();
  29. while (form.Handle == IntPtr.Zero &&
  30. watch.Elapsed.TotalSeconds < 2.5) ;
  31. if (form.Handle == IntPtr.Zero)
  32. throw new ArgumentNullException("form");
  33. int frameBufferCount = 3;
  34. SharpDX.DXGI.Factory factory;
  35. disposables.Push(factory = new SharpDX.DXGI.Factory1());
  36. SharpDX.DXGI.Adapter adapter;
  37. disposables.Push(adapter = factory.Adapters[0]);
  38. Console.WriteLine(adapter.Description.Description);
  39. Device device;
  40. disposables.Push(device = new Device(null,
  41. SharpDX.Direct3D.FeatureLevel.Level_11_0));
  42. SharpDX.DXGI.SwapChainDescription swapChainDescription =
  43. new SharpDX.DXGI.SwapChainDescription()
  44. {
  45. BufferCount = frameBufferCount,
  46. Flags = SharpDX.DXGI.SwapChainFlags.None,
  47. IsWindowed = true,
  48. ModeDescription = new SharpDX.DXGI.ModeDescription(
  49. SharpDX.DXGI.Format.R8G8B8A8_UNorm),
  50. OutputHandle = form.Handle,
  51. SampleDescription =
  52. new SharpDX.DXGI.SampleDescription(1, 0),
  53. SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard,
  54. Usage = SharpDX.DXGI.Usage.RenderTargetOutput
  55. };
  56. SharpDX.DXGI.SwapChain3 swapChain;
  57. CommandQueueDescription commandQueueDescription =
  58. new CommandQueueDescription()
  59. {
  60. Flags = CommandQueueFlags.None,
  61. NodeMask = 0,
  62. Priority = (int)CommandQueuePriority.Normal,
  63. Type = CommandListType.Direct
  64. };
  65. CommandQueue commandQueue;
  66. disposables.Push(commandQueue =
  67. device.CreateCommandQueue(commandQueueDescription));
  68. {
  69. SharpDX.DXGI.SwapChain tempSwapChain =
  70. new SharpDX.DXGI.SwapChain(factory,
  71. commandQueue, swapChainDescription);
  72. disposables.Push(swapChain =
  73. tempSwapChain.QueryInterface<SharpDX.DXGI.SwapChain3>());
  74. }
  75. DescriptorHeapDescription rtvDescriptorHeapDescription =
  76. new DescriptorHeapDescription()
  77. {
  78. DescriptorCount = frameBufferCount,
  79. Flags = DescriptorHeapFlags.None,
  80. NodeMask = 0,
  81. Type = DescriptorHeapType.RenderTargetView
  82. };
  83. DescriptorHeap rtvDescriptorHeap;
  84. disposables.Push(rtvDescriptorHeap =
  85. device.CreateDescriptorHeap(rtvDescriptorHeapDescription));
  86. Resource[] renderTargets = new Resource[frameBufferCount];
  87. for (int i = 0; i < frameBufferCount; ++i)
  88. disposables.Push(renderTargets[i] =
  89. swapChain.GetBackBuffer<Resource>(i));
  90. int renderTargetViewHeapSize =
  91. device.GetDescriptorHandleIncrementSize(
  92. DescriptorHeapType.RenderTargetView);
  93. CpuDescriptorHandle renderTargetView =
  94. rtvDescriptorHeap.CPUDescriptorHandleForHeapStart;
  95. device.CreateRenderTargetView(
  96. renderTargets[0], null, renderTargetView);
  97. renderTargetView += renderTargetViewHeapSize;
  98. device.CreateRenderTargetView(
  99. renderTargets[1], null, renderTargetView);
  100. renderTargetView += renderTargetViewHeapSize;
  101. device.CreateRenderTargetView(
  102. renderTargets[2], null, renderTargetView);
  103. CommandAllocator[] commandAllocator =
  104. new CommandAllocator[frameBufferCount];
  105. for (int i = 0; i < frameBufferCount; ++i)
  106. disposables.Push(commandAllocator[i] =
  107. device.CreateCommandAllocator(CommandListType.Direct));
  108. GraphicsCommandList commandList;
  109. disposables.Push(
  110. commandList = device.CreateCommandList(
  111. nodeMask: 0,
  112. type: CommandListType.Direct,
  113. commandAllocatorRef: commandAllocator[0],
  114. initialStateRef: null
  115. )
  116. );
  117. Fence[] fence = new Fence[frameBufferCount];
  118. disposables.Push(fence[0] =
  119. device.CreateFence(0, FenceFlags.None));
  120. disposables.Push(fence[1] =
  121. device.CreateFence(0, FenceFlags.None));
  122. disposables.Push(fence[2] =
  123. device.CreateFence(0, FenceFlags.None));
  124. long fenceValue = 1;
  125. AutoResetEvent fenceEvent = new AutoResetEvent(false);
  126.  
  127. RawViewportF viewport =
  128. new RawViewportF()
  129. {
  130. X = 0,
  131. Y = 0,
  132. Width = form.DisplayRectangle.Width,
  133. Height = form.DisplayRectangle.Height,
  134. MinDepth = 0,
  135. MaxDepth = float.MaxValue
  136. };
  137. RawRectangle scissorsRectangle =
  138. new RawRectangle(
  139. left: 0,
  140. top: 0,
  141. right: form.DisplayRectangle.Width,
  142. bottom: form.DisplayRectangle.Height);
  143.  
  144.  
  145.  
  146. commandList.Close();
  147. renderTargetView = rtvDescriptorHeap.CPUDescriptorHandleForHeapStart;
  148. int currentBufferIndex = swapChain.CurrentBackBufferIndex;
  149. while (form.Visible)
  150. {
  151. commandAllocator[0].Reset();
  152. commandList.Reset(commandAllocator[0], null);
  153. // commandList.SetViewport(viewport: viewport);
  154. // commandList.SetScissorRectangles(rectangle: scissorsRectangle);
  155. commandList.ResourceBarrierTransition(
  156. resource: renderTargets[currentBufferIndex],
  157. stateBefore: ResourceStates.Present,
  158. stateAfter: ResourceStates.RenderTarget);
  159. renderTargetView =
  160. rtvDescriptorHeap.CPUDescriptorHandleForHeapStart +
  161. currentBufferIndex *
  162. renderTargetViewHeapSize;
  163. commandList.ClearRenderTargetView(
  164. renderTargetView: renderTargetView,
  165. colorRGBA: new RawColor4(0f, 0f, 1f, 1f));
  166. commandList.ResourceBarrierTransition(
  167. resource: renderTargets[currentBufferIndex],
  168. stateBefore: ResourceStates.RenderTarget,
  169. stateAfter: ResourceStates.Present);
  170. commandList.Close();
  171. commandQueue.ExecuteCommandList(commandList);
  172. swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
  173. long fenceToWaitFor = fenceValue;
  174. commandQueue.Signal(fence[0], fenceValue);
  175. ++fenceValue;
  176. if (fence[0].CompletedValue < fenceValue)
  177. {
  178. fence[0].SetEventOnCompletion(fenceToWaitFor,
  179. fenceEvent.SafeWaitHandle.DangerousGetHandle());
  180. fenceEvent.WaitOne();
  181. fenceEvent.Reset();
  182. }
  183.  
  184. currentBufferIndex = swapChain.CurrentBackBufferIndex;
  185. Application.DoEvents();
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. while (disposables.Count > 0)
  191. disposables.Pop().Dispose();
  192. Console.WriteLine(ex);
  193. Console.ReadKey(true);
  194. }
  195. finally
  196. {
  197. while (disposables.Count > 0)
  198. disposables.Pop().Dispose();
  199. }
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement