public void Initialize() { Viewport = new ViewportF() { Width = Form.ClientSize.Width, Height = Form.ClientSize.Height, MaxDepth = 1.0f }; ScissorRect = new Rectangle() { Right = Form.ClientSize.Width, Bottom = Form.ClientSize.Height }; EnableDebug(); Device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); GraphicCommandQueue = Device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct)); SwapChain = DxHelper.CreateSwapchain(Form, GraphicCommandQueue, Config); FrameIndex = SwapChain.CurrentBackBufferIndex; Resource[] tempRenderTargets; RenderTargetViewHeap = DxHelper.CreateRenderTargetViewHeap(Config, SwapChain, out tempRenderTargets); RenderTargets = tempRenderTargets; RootSignature = DxHelper.CreateRootSignature(); Fence = Device.CreateFence(0, FenceFlags.None); FenceValue = 1; FenceEvent = new AutoResetEvent(false); }
private void CreateCommandQueue() { SharpDX.Direct3D12.CommandQueueDescription desc = new CommandQueueDescription { Type = CommandListType.Direct, Priority = (int)CommandQueuePriority.Normal, Flags = CommandQueueFlags.None, NodeMask = 0 }; _queue = _dev.CreateCommandQueue(desc); }
public CommandObjects(Device device) { var queueDesc = new CommandQueueDescription(CommandListType.Direct); this.GetCommandQueue = device.CreateCommandQueue(queueDesc); this.GetCommandAllocator = device.CreateCommandAllocator(CommandListType.Direct); this.GetGraphicsCommandList = device.CreateCommandList( 0, CommandListType.Direct, this.GetCommandAllocator, null); this.GetGraphicsCommandList.Close(); }
private void CreateCommandObjects() { var queueDesc = new CommandQueueDescription(CommandListType.Direct); CommandQueue = Device.CreateCommandQueue(queueDesc); DirectCmdListAlloc = Device.CreateCommandAllocator(CommandListType.Direct); CommandList = Device.CreateCommandList( 0, CommandListType.Direct, DirectCmdListAlloc, // Associated command allocator. null); // Initial PipelineStateObject. // Start off in a closed state. This is because the first time we refer // to the command list we will Reset it, and it needs to be closed before // calling Reset. CommandList.Close(); }
/// <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)); }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; viewport.Width = width; viewport.Height = height; viewport.MaxDepth = 1.0f; scissorRect.Right = width; scissorRect.Bottom = height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, //Flags = SwapChainFlags.None, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); DescriptorHeapDescription srvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; shaderRenderViewHeap = device.CreateDescriptorHeap(srvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; } commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); }
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> /// Initializes the specified device. /// </summary> /// <param name="graphicsProfiles">The graphics profiles.</param> /// <param name="deviceCreationFlags">The device creation flags.</param> /// <param name="windowHandle">The window handle.</param> private void InitializePlatformDevice(GraphicsProfile[] graphicsProfiles, DeviceCreationFlags deviceCreationFlags, object windowHandle) { if (nativeDevice != null) { // Destroy previous device ReleaseDevice(); } rendererName = Adapter.NativeAdapter.Description.Description; // Profiling is supported through pix markers IsProfilingSupported = true; // Command lists are thread-safe and execute deferred IsDeferred = true; bool isDebug = (deviceCreationFlags & DeviceCreationFlags.Debug) != 0; if (isDebug) { SharpDX.Direct3D12.DebugInterface.Get().EnableDebugLayer(); } // Create Device D3D12 with feature Level based on profile for (int index = 0; index < graphicsProfiles.Length; index++) { var graphicsProfile = graphicsProfiles[index]; try { // D3D12 supports only feature level 11+ var level = graphicsProfile.ToFeatureLevel(); if (level < SharpDX.Direct3D.FeatureLevel.Level_11_0) { level = SharpDX.Direct3D.FeatureLevel.Level_11_0; } nativeDevice = new SharpDX.Direct3D12.Device(Adapter.NativeAdapter, level); RequestedProfile = graphicsProfile; CurrentFeatureLevel = level; break; } catch (Exception) { if (index == graphicsProfiles.Length - 1) { throw; } } } // Describe and create the command queue. var queueDesc = new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct); NativeCommandQueue = nativeDevice.CreateCommandQueue(queueDesc); //queueDesc.Type = CommandListType.Copy; NativeCopyCommandQueue = nativeDevice.CreateCommandQueue(queueDesc); TimestampFrequency = NativeCommandQueue.TimestampFrequency; SrvHandleIncrementSize = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); SamplerHandleIncrementSize = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.Sampler); if (isDebug) { var debugDevice = nativeDevice.QueryInterfaceOrNull <DebugDevice>(); if (debugDevice != null) { var infoQueue = debugDevice.QueryInterfaceOrNull <InfoQueue>(); if (infoQueue != null) { MessageId[] disabledMessages = { // This happens when render target or depth stencil clear value is diffrent // than provided during resource allocation. MessageId.CleardepthstencilviewMismatchingclearvalue, MessageId.ClearrendertargetviewMismatchingclearvalue, // This occurs when there are uninitialized descriptors in a descriptor table, // even when a shader does not access the missing descriptors. MessageId.InvalidDescriptorHandle, // These happen when capturing with VS diagnostics MessageId.MapInvalidNullRange, MessageId.UnmapInvalidNullRange, }; // Disable irrelevant debug layer warnings InfoQueueFilter filter = new InfoQueueFilter { DenyList = new InfoQueueFilterDescription { Ids = disabledMessages } }; infoQueue.AddStorageFilterEntries(filter); //infoQueue.SetBreakOnSeverity(MessageSeverity.Error, true); //infoQueue.SetBreakOnSeverity(MessageSeverity.Warning, true); infoQueue.Dispose(); } debugDevice.Dispose(); } } // Prepare pools CommandAllocators = new CommandAllocatorPool(this); SrvHeaps = new HeapPool(this, SrvHeapSize, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); SamplerHeaps = new HeapPool(this, SamplerHeapSize, DescriptorHeapType.Sampler); // Prepare descriptor allocators SamplerAllocator = new DescriptorAllocator(this, DescriptorHeapType.Sampler); ShaderResourceViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); DepthStencilViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.DepthStencilView); RenderTargetViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.RenderTargetView); // Prepare copy command list (start it closed, so that every new use start with a Reset) NativeCopyCommandAllocator = NativeDevice.CreateCommandAllocator(CommandListType.Direct); NativeCopyCommandList = NativeDevice.CreateCommandList(CommandListType.Direct, NativeCopyCommandAllocator, null); NativeCopyCommandList.Close(); // Fence for next frame and resource cleaning nativeFence = NativeDevice.CreateFence(0, FenceFlags.None); nativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None); }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; viewport.Width = width; viewport.Height = height; viewport.MaxDepth = 1.0f; scissorRect.Right = width; scissorRect.Bottom = height; device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { var queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); var swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; var tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); frameIndex = swapChain.CurrentBackBufferIndex; } var rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); var srvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = 200, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; shaderRenderViewHeap = device.CreateDescriptorHeap(srvHeapDesc); var terrainHeapDesc = new DescriptorHeapDescription() { DescriptorCount = 2, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; terrainHeap = device.CreateDescriptorHeap(terrainHeapDesc); var meshCtrCbvDesc = new DescriptorHeapDescription() { DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; meshCtrBufferViewHeap = device.CreateDescriptorHeap(meshCtrCbvDesc); var rtcHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtcHandle); rtcHandle += rtvDescriptorSize; } var svHeapDesc = new DescriptorHeapDescription() { Type = DescriptorHeapType.Sampler, DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, NodeMask = 0 }; samplerViewHeap = device.CreateDescriptorHeap(svHeapDesc); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); }
/// <summary> /// Initializes the specified device. /// </summary> /// <param name="graphicsProfiles">The graphics profiles.</param> /// <param name="deviceCreationFlags">The device creation flags.</param> /// <param name="windowHandle">The window handle.</param> private void InitializePlatformDevice(GraphicsProfile[] graphicsProfiles, DeviceCreationFlags deviceCreationFlags, object windowHandle) { if (nativeDevice != null) { // Destroy previous device ReleaseDevice(); } rendererName = Adapter.NativeAdapter.Description.Description; // Profiling is supported through pix markers IsProfilingSupported = true; if ((deviceCreationFlags & DeviceCreationFlags.Debug) != 0) { SharpDX.Direct3D12.DebugInterface.Get().EnableDebugLayer(); } // Default fallback if (graphicsProfiles.Length == 0) { graphicsProfiles = new[] { GraphicsProfile.Level_11_0 } } ; // Create Device D3D12 with feature Level based on profile for (int index = 0; index < graphicsProfiles.Length; index++) { var graphicsProfile = graphicsProfiles[index]; try { // D3D12 supports only feature level 11+ var level = graphicsProfile.ToFeatureLevel(); if (level < SharpDX.Direct3D.FeatureLevel.Level_11_0) { level = SharpDX.Direct3D.FeatureLevel.Level_11_0; } nativeDevice = new SharpDX.Direct3D12.Device(Adapter.NativeAdapter, level); RequestedProfile = graphicsProfile; CurrentFeatureLevel = level; break; } catch (Exception) { if (index == graphicsProfiles.Length - 1) { throw; } } } // Describe and create the command queue. var queueDesc = new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct); NativeCommandQueue = nativeDevice.CreateCommandQueue(queueDesc); //queueDesc.Type = CommandListType.Copy; NativeCopyCommandQueue = nativeDevice.CreateCommandQueue(queueDesc); SrvHandleIncrementSize = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); SamplerHandleIncrementSize = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.Sampler); // Prepare pools CommandAllocators = new CommandAllocatorPool(this); SrvHeaps = new HeapPool(this, SrvHeapSize, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); SamplerHeaps = new HeapPool(this, SamplerHeapSize, DescriptorHeapType.Sampler); // Prepare descriptor allocators SamplerAllocator = new DescriptorAllocator(this, DescriptorHeapType.Sampler); ShaderResourceViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); DepthStencilViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.DepthStencilView); RenderTargetViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.RenderTargetView); // Prepare copy command list (start it closed, so that every new use start with a Reset) NativeCopyCommandAllocator = NativeDevice.CreateCommandAllocator(CommandListType.Direct); NativeCopyCommandList = NativeDevice.CreateCommandList(CommandListType.Direct, NativeCopyCommandAllocator, null); NativeCopyCommandList.Close(); // Fence for next frame and resource cleaning nativeFence = NativeDevice.CreateFence(0, FenceFlags.None); nativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None); }
private void LoadPipeline(RenderForm form) { SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericMouse, DeviceFlags.None); SharpDX.RawInput.Device.MouseInput += MouseEvent; SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, DeviceFlags.None); SharpDX.RawInput.Device.KeyboardInput += KeyboardEvent; label = new Label() { Text = "Text", Location = new System.Drawing.Point(16, 16) }; form.Controls.Add(label); int width = form.ClientSize.Width; int height = form.ClientSize.Height; viewport.Width = width; viewport.Height = height; viewport.MaxDepth = 1.0f; scissorRect.Right = width; scissorRect.Bottom = height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new SharpDX.Direct3D12.Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. var queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. var swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, //Flags = SwapChainFlags.None, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; var tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface <SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. var rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. var rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer <Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; } commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; viewport.Width = width; viewport.Height = height; viewport.MaxDepth = 1.0f; scissorRect.Right = width; scissorRect.Bottom = height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, //Flags = SwapChainFlags.None, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; } //create depth buffer; DescriptorHeapDescription dsvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.DepthStencilView }; depthStencilViewHeap = device.CreateDescriptorHeap(dsvHeapDesc); CpuDescriptorHandle dsvHandle = depthStencilViewHeap.CPUDescriptorHandleForHeapStart; ClearValue depthOptimizedClearValue = new ClearValue() { Format = Format.D32_Float, DepthStencil = new DepthStencilValue() { Depth = 1.0F, Stencil = 0 }, }; depthTarget = device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 0, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil), ResourceStates.DepthWrite, depthOptimizedClearValue); var depthView = new DepthStencilViewDescription() { Format = Format.D32_Float, Dimension = DepthStencilViewDimension.Texture2D, Flags = DepthStencilViewFlags.None, }; //bind depth buffer device.CreateDepthStencilView(depthTarget, null, dsvHandle); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle); }
private List<IntPtr> GetProcAddress() { var address = new List<IntPtr>(); _device12 = new SharpDX.Direct3D12.Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var renderForm = new Form()) { using (var factory = new SharpDX.DXGI.Factory4()) { _commandQueue = _device12.CreateCommandQueue(new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct)); _commandAllocator = _device12.CreateCommandAllocator(CommandListType.Direct); _commandList = _device12.CreateCommandList(CommandListType.Direct, _commandAllocator, null); var swapChainDesc = new SharpDX.DXGI.SwapChainDescription() { BufferCount = 2, ModeDescription = new SharpDX.DXGI.ModeDescription(100, 100, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm), Usage = SharpDX.DXGI.Usage.RenderTargetOutput, SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard, OutputHandle = renderForm.Handle, Flags = SwapChainFlags.AllowModeSwitch, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), IsWindowed = true }; var tempSwapChain = new SharpDX.DXGI.SwapChain(factory, _commandQueue, swapChainDesc); _swapChain = tempSwapChain.QueryInterface<SharpDX.DXGI.SwapChain3>(); tempSwapChain.Dispose(); } if (_device12 != null && _swapChain != null) { address.AddRange(GetVTblAddresses(_device12.NativePointer, 44)); address.AddRange(GetVTblAddresses(_commandQueue.NativePointer, 19)); address.AddRange(GetVTblAddresses(_commandAllocator.NativePointer, 9)); address.AddRange(GetVTblAddresses(_commandList.NativePointer, 60)); address.AddRange(GetVTblAddresses(_swapChain.NativePointer, 18)); _device12.Dispose(); _device12 = null; _commandQueue.Dispose(); _commandQueue = null; _commandAllocator.Dispose(); _commandAllocator = null; _commandList.Dispose(); _commandList = null; _swapChain.Dispose(); _swapChain = null; } } return address; }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, //Flags = SwapChainFlags.None, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. renderTargetViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); //create depth buffer; depthStencilViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.DepthStencilView }); //constant buffer view heap constantBufferViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = 100, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, Flags = DescriptorHeapFlags.ShaderVisible }); //Create targets CreateTargets(width, height); //sampler buffer view heap samplerViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = 10, Type = DescriptorHeapType.Sampler, Flags = DescriptorHeapFlags.ShaderVisible }); //bind sampler device.CreateSampler(new SamplerStateDescription() { Filter = Filter.ComparisonMinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, MinimumLod = float.MinValue, MaximumLod = float.MaxValue, MipLodBias = 0, MaximumAnisotropy = 0, ComparisonFunction = Comparison.Never }, samplerViewHeap.CPUDescriptorHandleForHeapStart); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle); form.UserResized += (sender, e) => { isResizing = true; }; }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; viewport.Width = width; viewport.Height = height; viewport.MaxDepth = 1.0f; scissorRect.Right = width; scissorRect.Bottom = height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_12_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = form.Handle, //Flags = SwapChainFlags.None, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); tempSwapChain.Dispose(); frameIndex = swapChain.CurrentBackBufferIndex; } // Create descriptor heaps. // Describe and create a render target view (RTV) descriptor heap. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); //Init Direct3D11 device from Direct3D12 device device11 = SharpDX.Direct3D11.Device.CreateFromDirect3D12(device, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, null, null, commandQueue); deviceContext11 = device11.ImmediateContext; device11on12 = device11.QueryInterface<SharpDX.Direct3D11.ID3D11On12Device>(); var d2dFactory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded); // Create frame resources. CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; //init Direct2D surfaces SharpDX.Direct3D11.D3D11ResourceFlags format = new SharpDX.Direct3D11.D3D11ResourceFlags() { BindFlags = (int)SharpDX.Direct3D11.BindFlags.RenderTarget, CPUAccessFlags = (int)SharpDX.Direct3D11.CpuAccessFlags.None }; device11on12.CreateWrappedResource( renderTargets[n], format, (int)ResourceStates.Present, (int)ResourceStates.RenderTarget, typeof(SharpDX.Direct3D11.Resource).GUID, out wrappedBackBuffers[n]); //Init direct2D surface var d2dSurface = wrappedBackBuffers[n].QueryInterface<Surface>(); direct2DRenderTarget[n] = new SharpDX.Direct2D1.RenderTarget(d2dFactory, d2dSurface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied))); d2dSurface.Dispose(); } commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); d2dFactory.Dispose(); //Init font var directWriteFactory = new SharpDX.DirectWrite.Factory(); textFormat = new SharpDX.DirectWrite.TextFormat(directWriteFactory, "Arial", SharpDX.DirectWrite.FontWeight.Bold, SharpDX.DirectWrite.FontStyle.Normal, 48) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near }; textBrush = new SharpDX.Direct2D1.SolidColorBrush(direct2DRenderTarget[0], Color.White); directWriteFactory.Dispose(); }
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(); }