Advertisement
Guest User

Minimum DirectX12 - Error 0x887A0005

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