Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SharpDX.Direct3D12;
- using SharpDX.Mathematics.Interop;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DirectX12BlueScreen2
- {
- class Program
- {
- private static Stack<IDisposable> disposables =
- new Stack<IDisposable>();
- static void Main(string[] args)
- {
- try
- {
- Form form; disposables.Push(form = new Form());
- form.Width = 640;
- form.Height = 480;
- form.Visible = true;
- var watch = Stopwatch.StartNew();
- while (form.Handle == IntPtr.Zero &&
- watch.Elapsed.TotalSeconds < 2.5) ;
- if (form.Handle == IntPtr.Zero)
- throw new ArgumentNullException("form");
- int frameBufferCount = 3;
- SharpDX.DXGI.Factory factory;
- disposables.Push(factory = new SharpDX.DXGI.Factory1());
- SharpDX.DXGI.Adapter adapter;
- disposables.Push(adapter = factory.Adapters[0]);
- Console.WriteLine(adapter.Description.Description);
- Device device;
- disposables.Push(device = new Device(null,
- SharpDX.Direct3D.FeatureLevel.Level_11_0));
- SharpDX.DXGI.SwapChainDescription swapChainDescription =
- new SharpDX.DXGI.SwapChainDescription()
- {
- BufferCount = frameBufferCount,
- Flags = SharpDX.DXGI.SwapChainFlags.None,
- IsWindowed = true,
- ModeDescription = new SharpDX.DXGI.ModeDescription(
- SharpDX.DXGI.Format.R8G8B8A8_UNorm),
- OutputHandle = form.Handle,
- SampleDescription =
- new SharpDX.DXGI.SampleDescription(1, 0),
- SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard,
- Usage = SharpDX.DXGI.Usage.RenderTargetOutput
- };
- SharpDX.DXGI.SwapChain3 swapChain;
- CommandQueueDescription commandQueueDescription =
- new CommandQueueDescription()
- {
- Flags = CommandQueueFlags.None,
- NodeMask = 0,
- Priority = (int)CommandQueuePriority.Normal,
- Type = CommandListType.Direct
- };
- CommandQueue commandQueue;
- disposables.Push(commandQueue =
- device.CreateCommandQueue(commandQueueDescription));
- {
- SharpDX.DXGI.SwapChain tempSwapChain =
- new SharpDX.DXGI.SwapChain(factory,
- commandQueue, swapChainDescription);
- disposables.Push(swapChain =
- tempSwapChain.QueryInterface<SharpDX.DXGI.SwapChain3>());
- }
- DescriptorHeapDescription rtvDescriptorHeapDescription =
- new DescriptorHeapDescription()
- {
- DescriptorCount = frameBufferCount,
- Flags = DescriptorHeapFlags.None,
- NodeMask = 0,
- Type = DescriptorHeapType.RenderTargetView
- };
- DescriptorHeap rtvDescriptorHeap;
- disposables.Push(rtvDescriptorHeap =
- device.CreateDescriptorHeap(rtvDescriptorHeapDescription));
- Resource[] renderTargets = new Resource[frameBufferCount];
- for (int i = 0; i < frameBufferCount; ++i)
- disposables.Push(renderTargets[i] =
- swapChain.GetBackBuffer<Resource>(i));
- int renderTargetViewHeapSize =
- device.GetDescriptorHandleIncrementSize(
- DescriptorHeapType.RenderTargetView);
- CpuDescriptorHandle renderTargetView =
- rtvDescriptorHeap.CPUDescriptorHandleForHeapStart;
- device.CreateRenderTargetView(
- renderTargets[0], null, renderTargetView);
- renderTargetView += renderTargetViewHeapSize;
- device.CreateRenderTargetView(
- renderTargets[1], null, renderTargetView);
- renderTargetView += renderTargetViewHeapSize;
- device.CreateRenderTargetView(
- renderTargets[2], null, renderTargetView);
- CommandAllocator[] commandAllocator =
- new CommandAllocator[frameBufferCount];
- for (int i = 0; i < frameBufferCount; ++i)
- disposables.Push(commandAllocator[i] =
- device.CreateCommandAllocator(CommandListType.Direct));
- GraphicsCommandList commandList;
- disposables.Push(
- commandList = device.CreateCommandList(
- nodeMask: 0,
- type: CommandListType.Direct,
- commandAllocatorRef: commandAllocator[0],
- initialStateRef: null
- )
- );
- Fence[] fence = new Fence[frameBufferCount];
- disposables.Push(fence[0] =
- device.CreateFence(0, FenceFlags.None));
- disposables.Push(fence[1] =
- device.CreateFence(0, FenceFlags.None));
- disposables.Push(fence[2] =
- device.CreateFence(0, FenceFlags.None));
- long fenceValue = 1;
- AutoResetEvent fenceEvent = new AutoResetEvent(false);
- RawViewportF viewport =
- new RawViewportF()
- {
- X = 0,
- Y = 0,
- Width = form.DisplayRectangle.Width,
- Height = form.DisplayRectangle.Height,
- MinDepth = 0,
- MaxDepth = float.MaxValue
- };
- RawRectangle scissorsRectangle =
- new RawRectangle(
- left: 0,
- top: 0,
- right: form.DisplayRectangle.Width,
- bottom: form.DisplayRectangle.Height);
- commandList.Close();
- renderTargetView = rtvDescriptorHeap.CPUDescriptorHandleForHeapStart;
- int currentBufferIndex = swapChain.CurrentBackBufferIndex;
- while (form.Visible)
- {
- commandAllocator[0].Reset();
- commandList.Reset(commandAllocator[0], null);
- // commandList.SetViewport(viewport: viewport);
- // commandList.SetScissorRectangles(rectangle: scissorsRectangle);
- commandList.ResourceBarrierTransition(
- resource: renderTargets[currentBufferIndex],
- stateBefore: ResourceStates.Present,
- stateAfter: ResourceStates.RenderTarget);
- renderTargetView =
- rtvDescriptorHeap.CPUDescriptorHandleForHeapStart +
- currentBufferIndex *
- renderTargetViewHeapSize;
- commandList.ClearRenderTargetView(
- renderTargetView: renderTargetView,
- colorRGBA: new RawColor4(0f, 0f, 1f, 1f));
- commandList.ResourceBarrierTransition(
- resource: renderTargets[currentBufferIndex],
- stateBefore: ResourceStates.RenderTarget,
- stateAfter: ResourceStates.Present);
- commandList.Close();
- commandQueue.ExecuteCommandList(commandList);
- swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
- long fenceToWaitFor = fenceValue;
- commandQueue.Signal(fence[0], fenceValue);
- ++fenceValue;
- if (fence[0].CompletedValue < fenceValue)
- {
- fence[0].SetEventOnCompletion(fenceToWaitFor,
- fenceEvent.SafeWaitHandle.DangerousGetHandle());
- fenceEvent.WaitOne();
- fenceEvent.Reset();
- }
- currentBufferIndex = swapChain.CurrentBackBufferIndex;
- Application.DoEvents();
- }
- }
- catch (Exception ex)
- {
- while (disposables.Count > 0)
- disposables.Pop().Dispose();
- Console.WriteLine(ex);
- Console.ReadKey(true);
- }
- finally
- {
- while (disposables.Count > 0)
- disposables.Pop().Dispose();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement