private static Device CreateDeviceWithSwapChain(DriverType driverType, FeatureLevel level, SwapChainDescription swapChainDescription, out SwapChain swapChain, out CommandQueue queue) { #if DEBUG // Enable the D3D12 debug layer. // DebugInterface.Get().EnableDebugLayer(); #endif using (var factory = new Factory4()) { var adapter = driverType == DriverType.Hardware ? null : factory.GetWarpAdapter(); var device = new Device(adapter, level); queue = device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct)); swapChain = new SwapChain(factory, queue, swapChainDescription); return(device); } }
/// <summary> /// Creates the rendering pipeline. /// </summary> void LoadPipeline() { // create swap chain descriptor var swapChainDescription1 = new SwapChainDescription1() { AlphaMode = AlphaMode.Unspecified, BufferCount = SwapBufferCount, Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipSequential, SampleDescription = new SampleDescription(1, 0), Format = Format.R8G8B8A8_UNorm, Width = width, Height = height }; // enable debug layer using (var debugInterface = DebugInterface.Get()) debugInterface.EnableDebugLayer(); // create device using (var factory = new Factory4()) { #if USE_WARP using (var warpAdapter = factory.GetWarpAdapter()) { device = Collect(new Device(warpAdapter, FeatureLevel.Level_12_0)); } #else using (var adapter = factory.Adapters[1]) { device = Collect(new Device(adapter, FeatureLevel.Level_11_0)); } #endif commandQueue = Collect(device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct))); CreateSwapChain(ref swapChainDescription1, factory); } // create command queue and allocator objects commandListAllocator = Collect(device.CreateCommandAllocator(CommandListType.Direct)); }
protected void InitDirect3D() { #if DEBUG // The Direct3D 12 debug layer may or may not be installed. It's installation can be // managed through settings page "Manage optional features" with a feature called // "Graphics Tools". // There may be a better solution to check for it instead of try/catch. If you happen // to know, please consider opening an issue or PR in the repo. try { DebugInterface.Get().EnableDebugLayer(); } catch (SharpDXException ex) when(ex.Descriptor.NativeApiCode == "DXGI_ERROR_SDK_COMPONENT_MISSING") { Debug.WriteLine("Failed to enable debug layer. Please ensure \"Graphics Tools\" feature is enabled in Windows \"Manage optional feature\" settings page"); } #endif _factory = new Factory4(); try { // Try to create hardware device. // Pass NULL to use the default adapter which is the first adapter that is enumerated by Factory.Adapters. // Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dn770336(v=vs.85).aspx Device = new Device(null, FeatureLevel.Level_11_0); } catch (SharpDXException) { // Fallback to WARP device. Adapter warpAdapter = _factory.GetWarpAdapter(); Device = new Device(warpAdapter, FeatureLevel.Level_11_0); } Fence = Device.CreateFence(0, FenceFlags.None); _fenceEvent = new AutoResetEvent(false); RtvDescriptorSize = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); DsvDescriptorSize = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.DepthStencilView); CbvSrvUavDescriptorSize = Device.GetDescriptorHandleIncrementSize( DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); // Check 4X MSAA quality support for our back buffer format. // All Direct3D 11 capable devices support 4X MSAA for all render // target formats, so we only need to check quality support. FeatureDataMultisampleQualityLevels msQualityLevels; msQualityLevels.Format = BackBufferFormat; msQualityLevels.SampleCount = 4; msQualityLevels.Flags = MultisampleQualityLevelFlags.None; msQualityLevels.QualityLevelCount = 0; Debug.Assert(Device.CheckFeatureSupport(Feature.MultisampleQualityLevels, ref msQualityLevels)); _m4xMsaaQuality = msQualityLevels.QualityLevelCount; #if DEBUG LogAdapters(); #endif CreateCommandObjects(); CreateSwapChain(); CreateRtvAndDsvDescriptorHeaps(); }
public GraphicsHost(RenderForm window, bool hidden = false) { if (window == null) { throw new ArgumentNullException(nameof(window)); } this.window = window; if (!hidden) { this.window.Visible = true; } #if DEBUG Configuration.EnableObjectTracking = true; Configuration.ThrowOnShaderCompileError = false; #endif var swapChainDescription = new SwapChainDescription() { BufferCount = SwapBufferCount, ModeDescription = new ModeDescription(Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, OutputHandle = window.Handle, SwapEffect = SwapEffect.FlipDiscard, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; #if DEBUG // Enable the D3D12 debug layer. // DebugInterface.Get().EnableDebugLayer(); #endif try { // null == DriverType.Hardware device = new Device(null, FeatureLevel.Level_11_0); commandQueue = device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct)); } catch (SharpDXException) { using (var factory = new Factory4()) { device = new Device(factory.GetWarpAdapter(), FeatureLevel.Level_11_0); commandQueue = device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct)); } } using (var factory = new Factory1()) swapChain = new SwapChain(factory, commandQueue, swapChainDescription); commandListAllocator = device.CreateCommandAllocator(CommandListType.Direct); descriptorHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { Type = DescriptorHeapType.RenderTargetView, DescriptorCount = 1 }); commandList = device.CreateCommandList(CommandListType.Direct, commandListAllocator, null); renderTarget = swapChain.GetBackBuffer <Resource>(0); device.CreateRenderTargetView(renderTarget, null, descriptorHeap.CPUDescriptorHandleForHeapStart); viewport = new ViewportF(0, 0, this.Width, this.Height); scissorRectangle = new Rectangle(0, 0, this.Width, this.Height); fence = device.CreateFence(0, FenceFlags.None); currentFence = 1; commandList.Close(); eventHandle = new AutoResetEvent(false); WaitForPrevFrame(); }