Exemple #1
0
        public void Initialize(UInt32 CmdQueueType)
        {
            // 1. create command queue
            CommandQueueDescription?descCmdQueue = null;

            switch (CmdQueueType)
            {
            case H1GlobalDX12Definitions.CMDQUEUE_GRAPHICS:
                descCmdQueue = new CommandQueueDescription(CommandListType.Direct);
                break;

            case H1GlobalDX12Definitions.CMDQUEUE_COMPUTE:
                descCmdQueue = new CommandQueueDescription(CommandListType.Compute);
                break;

            case H1GlobalDX12Definitions.CMDQUEUE_COPY:
                descCmdQueue = new CommandQueueDescription(CommandListType.Copy);
                break;
            }

            if (descCmdQueue != null)
            {
                m_CmdQueue = m_DeviceRef.Device.CreateCommandQueue(descCmdQueue.Value);
            }
        }
Exemple #2
0
        private ID3D12CommandQueue CreateCommandQueue(ID3D12Device device)
        {
            var desc = new CommandQueueDescription();

            desc.Type  = CommandListType.Direct;
            desc.Flags = CommandQueueFlags.None;
            return(device.CreateCommandQueue(desc));
        }
Exemple #3
0
        internal ID3D12CommandQueue CreateCommandQueueCopy()
        {
            // Describe and create the command queue.
            CommandQueueDescription QueueDesc = new CommandQueueDescription()
            {
                Type  = Vortice.Direct3D12.CommandListType.Copy,
                Flags = CommandQueueFlags.None,
            };

            return(GraphicsDevice.NativeDevice.CreateCommandQueue <ID3D12CommandQueue>(QueueDesc));
        }
Exemple #4
0
        public ID3D12CommandQueue CreateCommandQueue(ID3D12Device5 pDevice)
        {
            ID3D12CommandQueue      pQueue;
            CommandQueueDescription cpDesc = new CommandQueueDescription();

            cpDesc.Flags = CommandQueueFlags.None;
            cpDesc.Type  = CommandListType.Direct;
            pQueue       = pDevice.CreateCommandQueue(cpDesc);

            return(pQueue);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        /// <exception cref="SharpDX.SharpDXException"></exception>
        protected override void CreateImpl()
        {
            var descd3d12 = new CommandQueueDescription
            {
                Priority = (int)CommandQueuePriority.Normal,
                Flags    = CommandQueueFlags.None, // Disabling GPU timeout is not yet exposed.
                NodeMask = 0,
                Type     = InternalUtils.GetDXCommandListType(Desc.Type)
            };

            CommandQueueD3D12      = ((Device)Device).DeviceD3D12.CreateCommandQueue(descd3d12);
            CommandQueueD3D12.Name = Label;
        }
        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();
        }
Exemple #8
0
        public void Initialize()
        {
            // Lets create a present command queue
            var queueDesc = new CommandQueueDescription(CommandListType.Direct);

            NativeCommandQueue = Device.NativeDevice.CreateCommandQueue(queueDesc);

            // Descirbe and create the swap chain
            using (var factory = new Factory4())
            {
                var width  = Description.Width;
                var height = Description.Height;
                var swapChainDescription = new SwapChainDescription
                {
                    BufferCount       = BackBufferCount,
                    ModeDescription   = new ModeDescription(width, height, new Rational(60, 1), Format.B8G8R8A8_UNorm),
                    Usage             = Usage.RenderTargetOutput,
                    SwapEffect        = SwapEffect.FlipDiscard,
                    OutputHandle      = Description.WindowHandle,
                    Flags             = SwapChainFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                    IsWindowed        = true
                };
                using (var tempSwapChain = new SharpDX.DXGI.SwapChain(factory, NativeCommandQueue, swapChainDescription))
                {
                    NativeSwapChain = tempSwapChain.QueryInterface <SwapChain3>();
                    BackBufferIndex = NativeSwapChain.CurrentBackBufferIndex;
                }
            }
            // We need now to retrieve the back buffers:
            // 1) We need a heap to store the views
            BackBuffers = new Texture[BackBufferCount];
            for (int i = 0; i < BackBufferCount; i++)
            {
                BackBuffers[i] = new Texture(Device);
                BackBuffers[i].Initialize(NativeSwapChain.GetBackBuffer <Resource>(i));
            }

            BackBuffer = new Texture(Device);
            BackBuffer.Initialize(NativeSwapChain.GetBackBuffer <Resource>(BackBufferIndex));

            CreateDepthStencilBuffer();

            Device.PrintLiveObjects();
        }
        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();
        }
        void InitializePlatformDependent()
        {
            // get the device reference
            m_DeviceRef = H1Global <H1ManagedRenderer> .Instance.Device;

            // platform-dependent assignments
            m_Fence = new H1GpuFence();
            m_CommandAllocatorPool = new H1CommandAllocatorPoolDX12(m_Type, m_DeviceRef);

            // create command queue (depending on command queue type)
            CommandQueueDescription?commandQueueDesc = null;

            switch (m_Type)
            {
            case H1CommandListType.Bundle:
                commandQueueDesc = new CommandQueueDescription(CommandListType.Bundle);
                break;

            case H1CommandListType.Compute:
                commandQueueDesc = new CommandQueueDescription(CommandListType.Compute);
                break;

            case H1CommandListType.Direct:
                commandQueueDesc = new CommandQueueDescription(CommandListType.Direct);
                break;

            case H1CommandListType.Copy:
                commandQueueDesc = new CommandQueueDescription(CommandListType.Copy);
                break;
            }

            // try to create command queue
            if (commandQueueDesc != null)
            {
                m_CommandQueue = m_DeviceRef.CreateCommandQueue(commandQueueDesc.Value);
            }
        }
Exemple #11
0
        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.
                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 CreateDeviceResources()
        {
#if DEBUG
            Configuration.EnableObjectTracking      = true;
            Configuration.ThrowOnShaderCompileError = false;

            // Enable the D3D12 debug layer.
            DebugInterface.Get().EnableDebugLayer();
#endif

            using (var factory = new Factory4())
            {
                // Create the Direct3D 12 API device object
                device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
                if (device == null)
                {
                    // TODO: We want to be able to specify adaptor
                    var adapter = factory.Adapters[0];
                    device = new Device(adapter, SharpDX.Direct3D.FeatureLevel.Level_11_0);
                }

                // Create the command queue.
                var queueDesc = new CommandQueueDescription(CommandListType.Direct);
                commandQueue      = device.CreateCommandQueue(queueDesc);
                commandQueue.Name = $"CommandQueue";

                // Create Command Allocator buffers.
                for (int i = 0; i < FrameCount; i++)
                {
                    commandAllocators[i]      = device.CreateCommandAllocator(CommandListType.Direct);
                    commandAllocators[i].Name = $"CommandAllocator F{i}";
                }
            }

            // Create RootSignature.
            var rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout,
                                                                 new[]
            {
                new RootParameter(ShaderVisibility.Vertex,
                                  new DescriptorRange()
                {
                    RangeType       = DescriptorRangeType.ConstantBufferView,
                    DescriptorCount = 1,
                    OffsetInDescriptorsFromTableStart = int.MinValue,
                    BaseShaderRegister = 0,
                }),
                new RootParameter(ShaderVisibility.Pixel,
                                  new DescriptorRange()
                {
                    RangeType       = DescriptorRangeType.ShaderResourceView,
                    DescriptorCount = 1,
                    OffsetInDescriptorsFromTableStart = int.MinValue,
                    BaseShaderRegister = 0
                })
            },
                                                                 new[]
            {
                new StaticSamplerDescription(ShaderVisibility.Pixel, 0, 0)
                {
                    Filter     = Filter.MinimumMinMagMipPoint,
                    AddressUVW = TextureAddressMode.Border,
                }
            });

            rootSignature = device.CreateRootSignature(rootSignatureDesc.Serialize());

            // Create Constant Buffer View Heap.
            var cbvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = 1,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
            };
            cbvHeap      = device.CreateDescriptorHeap(cbvHeapDesc);
            cbvHeap.Name = "CBV Heap";

            // Create Shader Render View Heap.
            var srvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = 1,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
            };
            srvHeap      = device.CreateDescriptorHeap(srvHeapDesc);
            srvHeap.Name = "SRV Heap";

            // Create synchronization objects.
            fence      = device.CreateFence(fenceValues[currentFrame], FenceFlags.None);
            fence.Name = $"Fence";
            fenceValues[currentFrame]++;

            // Create an event handle to use for frame synchronization.
            fenceEvent = new AutoResetEvent(false);
        }
Exemple #13
0
        //创建设备
        private void LoadPipeline(SharpDX.Windows.RenderForm form)
        {
            width  = form.ClientSize.Width;
            height = form.ClientSize.Height;

            //创建视口
            viewPort = new ViewportF(0, 0, width, height);

            //创建裁剪矩形
            scissorRectangle = new Rectangle(0, 0, width, height);

#if DEBUG
            //启用调试层
            {
                DebugInterface.Get().EnableDebugLayer();
            }
#endif
            device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
            //工厂化
            using (var factory = new Factory4())
            {
                //创建命令队列
                CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
                commandQueue = device.CreateCommandQueue(queueDesc);

                //创建交换链
                SwapChainDescription swapChainDesc = new SwapChainDescription()
                {
                    BufferCount     = FrameCount,
                    ModeDescription = new ModeDescription(
                        width, height,                               //缓存大小,一般与窗口大小相同
                        new Rational(60, 1),                         //刷新率,60hz
                        Format.R8G8B8A8_UNorm),                      //像素格式,8位RGBA格式
                    Usage             = Usage.RenderTargetOutput,    //CPU访问缓冲权限
                    SwapEffect        = SwapEffect.FlipDiscard,      //描述处理曲面后的缓冲区内容
                    OutputHandle      = form.Handle,                 //获取渲染窗口句柄
                    Flags             = SwapChainFlags.None,         //描述交换链的行为
                    SampleDescription = new SampleDescription(1, 0), //一重采样
                    IsWindowed        = true                         //true为窗口显示,false为全屏显示
                };

                //创建交换链
                SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
                swapChain = tempSwapChain.QueryInterface <SwapChain3>();
                tempSwapChain.Dispose();
                frameIndex = swapChain.CurrentBackBufferIndex;//获取交换链的当前缓冲区的索引
            }

            //创建描述符堆
            //创建一个渲染目标视图(RTV)的描述符堆
            DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = FrameCount,                         //堆中的描述符数
                Flags           = DescriptorHeapFlags.None,           //结果值指定符堆,None表示堆的默认用法
                Type            = DescriptorHeapType.RenderTargetView //堆中的描述符类型
            };
            renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);

            //获取给定类型的描述符堆的句柄增量的大小,将句柄按正确的数量递增到描述符数组中
            rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);

            //创建一个CBV的描述符堆
            var cbvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = 1,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView
            };
            constantBufferViewHeap = device.CreateDescriptorHeap(cbvHeapDesc);
            //创建一个SRV的描述符堆
            var srvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = 1,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView
            };
            shaderRenderViewHeap = device.CreateDescriptorHeap(srvHeapDesc);
            srvDescriptorSize    = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            //构建资源描述符来填充描述符堆
            //获取指向描述符堆起始处的指针
            CpuDescriptorHandle srvHandle = shaderRenderViewHeap.CPUDescriptorHandleForHeapStart;

            //创建渲染目标视图
            //获取堆中起始的CPU描述符句柄,for循环为交换链中的每一个缓冲区都创建了一个RTV(渲染目标视图)
            CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            for (int n = 0; n < FrameCount; n++)
            {
                //获得交换链的第n个缓冲区
                renderTargets[n] = swapChain.GetBackBuffer <Resource>(n);
                device.CreateRenderTargetView(
                    renderTargets[n], //指向渲染目标对象的指针
                    null,             //指向描述渲染目标视图结构的指针
                    rtvHandle);       //CPU描述符句柄,表示渲染目标视图的堆的开始
                rtvHandle += rtvDescriptorSize;
            }

            //创建命令分配器对象
            commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
            bundleAllocator  = device.CreateCommandAllocator(CommandListType.Bundle);
        }
Exemple #14
0
        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);
        }
Exemple #15
0
        private void LoadPipeline()
        {
            viewport.Width    = this.width;
            viewport.Height   = this.height;
            viewport.MaxDepth = 1.0f;

            scissorRect.Right  = this.width;
            scissorRect.Bottom = this.height;

#if DEBUG
            // Enable the D3D12 debug layer.
            {
                DebugInterface.Get().EnableDebugLayer();
            }
#endif
            var fact = new SharpDX.DXGI.Factory1();
            SharpDX.DXGI.Adapter adapter = fact.GetAdapter(1);

            // create device
            using (var defaultDevice = new Device(adapter, SharpDX.Direct3D.FeatureLevel.Level_12_1))
                device = defaultDevice.QueryInterface <SharpDX.Direct3D12.Device2>();

            using (var factory = new SharpDX.DXGI.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 SharpDX.DXGI.SwapChainDescription1()
                {
                    BufferCount       = FrameCount,
                    Format            = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    Height            = height,
                    Width             = width,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Scaling           = SharpDX.DXGI.Scaling.Stretch,
                    Stereo            = false,
                    SwapEffect        = SharpDX.DXGI.SwapEffect.FlipDiscard,
                    Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                };

                var tempSwapChain = new SharpDX.DXGI.SwapChain1(factory, commandQueue, ref swapChainDesc);
                swapChain = tempSwapChain.QueryInterface <SharpDX.DXGI.SwapChain3>();
                tempSwapChain.Dispose();
                frameIndex = swapChain.CurrentBackBufferIndex;

                using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = ComObject.As <SharpDX.DXGI.ISwapChainPanelNative>(swapChainPanel))
                    nativeObject.SwapChain = swapChain;
            }

            // 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;

            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 srvCbvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = 1,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView
            };

            srvCbvHeap = device.CreateDescriptorHeap(srvCbvHeapDesc);

            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 = 10,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                NodeMask        = 0
            };

            samplerViewHeap = device.CreateDescriptorHeap(svHeapDesc);

            commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
        }
Exemple #17
0
        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;
            };
        }
Exemple #18
0
        void loadDevice()
        {
            _resources = new GraphicsResource[0];

            _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

            using (var factory = new Factory4())
            {
                _device = new Device(factory.GetAdapter(_adapterIndex), SharpDX.Direct3D.FeatureLevel.Level_12_1).QueryInterface <Device3>();
                // Describe and create the command queue.
                CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
                _graphicsQueue = _device.CreateCommandQueue(queueDesc);


                // Describe and create the swap chain.
                SwapChainDescription swapChainDesc = new SwapChainDescription()
                {
                    BufferCount       = FRAME_COUNT,
                    ModeDescription   = new ModeDescription(WIDTH, HEIGHT, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                    Usage             = Usage.RenderTargetOutput,
                    SwapEffect        = SwapEffect.FlipDiscard,
                    OutputHandle      = _window.Handle,
                    Flags             = SwapChainFlags.AllowModeSwitch,
                    SampleDescription = new SampleDescription(1, 0),
                    IsWindowed        = true
                };

                SwapChain tempSwapChain = new SwapChain(factory, _graphicsQueue, 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 = FRAME_COUNT,
                Flags           = DescriptorHeapFlags.None,
                Type            = DescriptorHeapType.RenderTargetView
            };

            _renderTargetViewHeap = _device.CreateDescriptorHeap(rtvHeapDesc);

            DescriptorHeapDescription _dsvHeapDescription = new DescriptorHeapDescription()
            {
                DescriptorCount = 1,
                Flags           = DescriptorHeapFlags.None,
                NodeMask        = 0,
                Type            = DescriptorHeapType.DepthStencilView
            };
            _depthStencilView = _device.CreateDescriptorHeap(_dsvHeapDescription);

            _rtvDescriptorSize = _device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);

            // Create frame resources.
            CpuDescriptorHandle rtvHandle = _renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            for (int n = 0; n < FRAME_COUNT; n++)
            {
                _renderTargets[n] = _swapChain.GetBackBuffer <Resource>(n);
                _device.CreateRenderTargetView(_renderTargets[n], null, rtvHandle);
                rtvHandle += _rtvDescriptorSize;
            }

            //Initialize Depth/Stencil Buffer
            _depthStencilDesc = new ResourceDescription(ResourceDimension.Texture2D, 0,
                                                        _window.Width, _window.Height, 1, 1, Format.D24_UNorm_S8_UInt, 1, 0,
                                                        TextureLayout.Unknown, ResourceFlags.AllowDepthStencil);
            _depthStencilClear = new ClearValue()
            {
                DepthStencil = new DepthStencilValue()
                {
                    Depth   = 1.0f,
                    Stencil = 0
                },
                Format = Format.D24_UNorm_S8_UInt
            };
            _depthStencilBuffer = _device.CreateCommittedResource(new HeapProperties(HeapType.Default),
                                                                  HeapFlags.None, _depthStencilDesc, ResourceStates.Common, _depthStencilClear);

            //Create Descriptor to mip level 0 of the entire resource using format of the resouce
            _device.CreateDepthStencilView(_depthStencilBuffer, null, DepthStencilHandle);

            _commandAllocator       = _device.CreateCommandAllocator(CommandListType.Direct);
            _bundleCommandAllocator = _device.CreateCommandAllocator(CommandListType.Bundle);

            // Create the command list.
            _commandList       = _device.CreateCommandList(CommandListType.Direct, _commandAllocator, null);
            _bundleCommandList = _device.CreateCommandList(CommandListType.Bundle, _bundleCommandAllocator, null);

            _commandList.ResourceBarrier(new ResourceBarrier(new ResourceTransitionBarrier(_depthStencilBuffer,
                                                                                           ResourceStates.Common, ResourceStates.DepthWrite)));

            // Command lists are created in the recording state, but there is nothing
            // to record yet. The main loop expects it to be closed, so close it now.
            _bundleCommandList.Close();
        }
Exemple #19
0
        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();
        }