Exemple #1
0
        /// <summary>
        /// Render scene
        /// </summary>
        public void Render()
        {
            // record all the commands we need to render the scene into the command list
            PopulateCommandLists();

            // execute the command list
            commandQueue.ExecuteCommandList(commandList);

            // swap the back and front buffers
            swapChain.Present(1, 0);

            // wait and reset EVERYTHING
            WaitForPrevFrame();

            RemoveAndDispose(ref renderTarget);
            if (width != newWidth || height != newHeight)
            {
                width  = newWidth;
                height = newHeight;
                swapChain.ResizeBuffers(SwapBufferCount, width, height, Format.Unknown, SwapChainFlags.None);
#if USE_DEPTH
                RemoveAndDispose(ref depthBuffer);
                depthBuffer = Collect(device.CreateCommittedResource(
                                          new HeapProperties(HeapType.Default),
                                          HeapFlags.None,
                                          new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 1, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil),
                                          ResourceStates.Common,
                                          new ClearValue
                {
                    Format       = Format.D32_Float,
                    DepthStencil = new DepthStencilValue
                    {
                        Depth   = 1,
                        Stencil = 0,
                    }
                }));
                device.CreateDepthStencilView(depthBuffer, null, descriptorHeapDS.CPUDescriptorHandleForHeapStart);
#endif
                // Create the viewport
                viewPort = new ViewportF(0, 0, width, height);

                // Create the scissor
                scissorRectangle = new Rectangle(0, 0, width, height);
            }
            renderTarget = Collect(swapChain.GetBackBuffer <Resource>(swapChain.CurrentBackBufferIndex));
            device.CreateRenderTargetView(renderTarget, null, descriptorHeapRT.CPUDescriptorHandleForHeapStart);
        }
Exemple #2
0
        public void RebuildDescriptors(Resource depthStencilBuffer)
        {
            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Format    = NormalMapFormat,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip = 0,
                    MipLevels       = 1
                }
            };

            _device.CreateShaderResourceView(_normalMap, srvDesc, _normalMapCpuSrv);

            srvDesc.Format = Format.R24_UNorm_X8_Typeless;
            _device.CreateShaderResourceView(depthStencilBuffer, srvDesc, _depthMapCpuSrv);

            srvDesc.Format = Format.R8G8B8A8_UNorm;
            _device.CreateShaderResourceView(_randomVectorMap, srvDesc, _randomVectorMapCpuSrv);

            srvDesc.Format = AmbientMapFormat;
            _device.CreateShaderResourceView(_ambientMap0, srvDesc, _ambientMap0CpuSrv);
            _device.CreateShaderResourceView(_ambientMap1, srvDesc, _ambientMap1CpuSrv);

            var rtvDesc = new RenderTargetViewDescription
            {
                Dimension = RenderTargetViewDimension.Texture2D,
                Format    = NormalMapFormat,
                Texture2D = new RenderTargetViewDescription.Texture2DResource
                {
                    MipSlice   = 0,
                    PlaneSlice = 0
                }
            };

            _device.CreateRenderTargetView(_normalMap, rtvDesc, _normalMapCpuRtv);

            rtvDesc.Format = AmbientMapFormat;
            _device.CreateRenderTargetView(_ambientMap0, rtvDesc, _ambientMap0CpuRtv);
            _device.CreateRenderTargetView(_ambientMap1, rtvDesc, _ambientMap1CpuRtv);
        }
        public void EndFrame()
        {
            commandQueue.ExecuteCommandList(commandList);

            swapChain.Present(1, PresentFlags.None);
            indexLastSwapBuf = (indexLastSwapBuf + 1) % SwapBufferCount;
            Utilities.Dispose(ref renderTarget);
            renderTarget = swapChain.GetBackBuffer <Resource>(indexLastSwapBuf);
            device.CreateRenderTargetView(renderTarget, null, descriptorHeap.CPUDescriptorHandleForHeapStart);

            WaitForPrevFrame();
        }
Exemple #4
0
        private void BuildDescriptors()
        {
            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Format    = _format,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip = 0,
                    MipLevels       = 1
                }
            };

            _device.CreateShaderResourceView(_offscreenTex, srvDesc, _cpuSrv);
            _device.CreateRenderTargetView(_offscreenTex, null, _cpuRtv);
        }
        private void BuildDescriptors()
        {
            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Format      = _format,
                Dimension   = ShaderResourceViewDimension.TextureCube,
                TextureCube = new ShaderResourceViewDescription.TextureCubeResource
                {
                    MostDetailedMip     = 0,
                    MipLevels           = 1,
                    ResourceMinLODClamp = 0.0f
                }
            };

            // Create SRV to the entire cubemap resource.
            _device.CreateShaderResourceView(Resource, srvDesc, _cpuSrv);

            // Create RTV to each cube face.
            for (int i = 0; i < 6; i++)
            {
                var rtvDesc = new RenderTargetViewDescription
                {
                    Dimension      = RenderTargetViewDimension.Texture2DArray,
                    Format         = _format,
                    Texture2DArray = new RenderTargetViewDescription.Texture2DArrayResource
                    {
                        MipSlice   = 0,
                        PlaneSlice = 0,

                        // Render target to ith element.
                        FirstArraySlice = i,

                        // Only view one element of the array.
                        ArraySize = 1
                    }
                };

                // Create RTV to ith cubemap face.
                _device.CreateRenderTargetView(Resource, rtvDesc, Rtvs[i]);
            }
        }
        protected virtual void OnResize()
        {
            Debug.Assert(Device != null);
            Debug.Assert(SwapChain != null);
            Debug.Assert(DirectCmdListAlloc != null);

            // Flush before changing any resources.
            FlushCommandQueue();

            CommandList.Reset(DirectCmdListAlloc, null);

            // Release the previous resources we will be recreating.
            foreach (Resource buffer in _swapChainBuffers)
            {
                buffer?.Dispose();
            }
            DepthStencilBuffer?.Dispose();

            // Resize the swap chain.
            SwapChain.ResizeBuffers(
                SwapChainBufferCount,
                ClientWidth, ClientHeight,
                BackBufferFormat,
                SwapChainFlags.AllowModeSwitch);

            CpuDescriptorHandle rtvHeapHandle = RtvHeap.CPUDescriptorHandleForHeapStart;

            for (int i = 0; i < SwapChainBufferCount; i++)
            {
                Resource backBuffer = SwapChain.GetBackBuffer <Resource>(i);
                _swapChainBuffers[i] = backBuffer;
                Device.CreateRenderTargetView(backBuffer, null, rtvHeapHandle);
                rtvHeapHandle += RtvDescriptorSize;
            }

            // Create the depth/stencil buffer and view.
            var depthStencilDesc = new ResourceDescription
            {
                Dimension         = ResourceDimension.Texture2D,
                Alignment         = 0,
                Width             = ClientWidth,
                Height            = ClientHeight,
                DepthOrArraySize  = 1,
                MipLevels         = 1,
                Format            = Format.R24G8_Typeless,
                SampleDescription = new SampleDescription
                {
                    Count   = MsaaCount,
                    Quality = MsaaQuality
                },
                Layout = TextureLayout.Unknown,
                Flags  = ResourceFlags.AllowDepthStencil
            };
            var optClear = new ClearValue
            {
                Format       = DepthStencilFormat,
                DepthStencil = new DepthStencilValue
                {
                    Depth   = 1.0f,
                    Stencil = 0
                }
            };

            DepthStencilBuffer = Device.CreateCommittedResource(
                new HeapProperties(HeapType.Default),
                HeapFlags.None,
                depthStencilDesc,
                ResourceStates.Common,
                optClear);

            var depthStencilViewDesc = new DepthStencilViewDescription
            {
                Dimension = DepthStencilViewDimension.Texture2D,
                Format    = DepthStencilFormat
            };
            // Create descriptor to mip level 0 of entire resource using a depth stencil format.
            CpuDescriptorHandle dsvHeapHandle = DsvHeap.CPUDescriptorHandleForHeapStart;

            Device.CreateDepthStencilView(DepthStencilBuffer, depthStencilViewDesc, dsvHeapHandle);

            // Transition the resource from its initial state to be used as a depth buffer.
            CommandList.ResourceBarrierTransition(DepthStencilBuffer, ResourceStates.Common, ResourceStates.DepthWrite);

            // Execute the resize commands.
            CommandList.Close();
            CommandQueue.ExecuteCommandList(CommandList);

            // Wait until resize is complete.
            FlushCommandQueue();

            Viewport         = new ViewportF(0, 0, ClientWidth, ClientHeight, 0.0f, 1.0f);
            ScissorRectangle = new RectangleF(0, 0, ClientWidth, ClientHeight);
        }
Exemple #7
0
        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);
        }
Exemple #8
0
        void CreateBuffers()
        {
            SharpDX.Direct3D12.CpuDescriptorHandle rtv   = _heapRTV.CPUDescriptorHandleForHeapStart;
            SharpDX.Direct3D12.HeapFlags           flags = SharpDX.Direct3D12.HeapFlags.None;

            _frameIndex = _swapChain.CurrentBackBufferIndex;
            SharpDX.DXGI.SwapChainDescription1 swapChainDesc = _swapChain.Description1;

            // TODO: Check ID3D12Device8 to enable D3D12_HEAP_FLAG_CREATE_NOT_ZEROED

            SharpDX.Direct3D12.ResourceDescription desc = new SharpDX.Direct3D12.ResourceDescription();
            desc.Dimension         = SharpDX.Direct3D12.ResourceDimension.Texture2D;
            desc.Alignment         = 0;
            desc.Width             = swapChainDesc.Width;
            desc.Height            = swapChainDesc.Height;
            desc.DepthOrArraySize  = 1;
            desc.MipLevels         = 1;
            desc.SampleDescription = _sampleDesc;
            desc.Layout            = SharpDX.Direct3D12.TextureLayout.Unknown;

            SharpDX.Direct3D12.HeapProperties heap = new SharpDX.Direct3D12.HeapProperties();
            heap.Type                 = SharpDX.Direct3D12.HeapType.Default;
            heap.CPUPageProperty      = SharpDX.Direct3D12.CpuPageProperty.Unknown;
            heap.MemoryPoolPreference = SharpDX.Direct3D12.MemoryPool.Unknown;
            heap.CreationNodeMask     = 0;
            heap.VisibleNodeMask      = 0;

            SharpDX.Direct3D12.ClearValue clearValue = new SharpDX.Direct3D12.ClearValue();
            clearValue.Format = _format;
            clearValue.Color  = new SharpDX.Mathematics.Interop.RawVector4(0.0f, 0.0f, 0.0f, 0.0f);

            for (int i = 0; i < FrameCount; i++)
            {
                desc.Format = _format;
                desc.Flags  = SharpDX.Direct3D12.ResourceFlags.AllowRenderTarget;

                _backBuffers[i] = _swapChain.GetBackBuffer <SharpDX.Direct3D12.Resource>(i);

                if (_sampleDesc.Count != 1)
                {
                    _backBuffersAA[i] = _dev.CreateCommittedResource(heap, flags, desc, SharpDX.Direct3D12.ResourceStates.ResolveSource, clearValue);
                    _dev.CreateRenderTargetView(_backBuffersAA[i], null, rtv);
                }
                else
                {
                    SharpDX.Direct3D12.RenderTargetViewDescription view = new SharpDX.Direct3D12.RenderTargetViewDescription();
                    view.Format               = _format;
                    view.Dimension            = SharpDX.Direct3D12.RenderTargetViewDimension.Texture2D;
                    view.Texture2D.MipSlice   = 0;
                    view.Texture2D.PlaneSlice = 0;

                    _dev.CreateRenderTargetView(_backBuffers[i], view, rtv);
                }

                rtv.Ptr += _sizeRTV;
            }

            // Stencil buffer
            clearValue.Format               = SharpDX.DXGI.Format.D24_UNorm_S8_UInt;
            clearValue.DepthStencil.Depth   = 0.0f;
            clearValue.DepthStencil.Stencil = 0;

            desc.Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt;
            desc.Flags  = SharpDX.Direct3D12.ResourceFlags.AllowDepthStencil;

            _stencilBuffer = _dev.CreateCommittedResource(heap, flags, desc, SharpDX.Direct3D12.ResourceStates.DepthWrite, clearValue);

            SharpDX.Direct3D12.CpuDescriptorHandle dsv = _heapDSV.CPUDescriptorHandleForHeapStart;
            _dev.CreateDepthStencilView(_stencilBuffer, null, dsv);

            // Viewport
            _viewport[0].TopLeftX = 0.0f;
            _viewport[0].TopLeftY = 0.0f;
            _viewport[0].Width    = desc.Width;
            _viewport[0].Height   = desc.Height;
            _viewport[0].MinDepth = 0.0f;
            _viewport[0].MaxDepth = 1.0f;
        }
        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();
        }