/// <summary>
        /// The RequestAllocator
        /// </summary>
        /// <param name="completedFenceValue">The <see cref="long"/></param>
        /// <returns>The <see cref="CommandAllocator"/></returns>
        public CommandAllocator RequestAllocator(long completedFenceValue)
        {
            lock (_Mutex)
            {
                CommandAllocator result = null;

                if (_ReadyAllocator.Count != 0)
                {
                    (var fenceValue, var allocator) = _ReadyAllocator.Peek();

                    if (fenceValue <= completedFenceValue)
                    {
                        result = allocator;
                        result.Reset();
                        _ReadyAllocator.Dequeue();
                    }
                }

                if (result == null)
                {
                    result      = _Device.CreateCommandAllocator(_Type);
                    result.Name = $"CommandAllocator {_AllocatorPool.Count}";
                    _AllocatorPool.Add(result);
                }

                return(result);
            }
        }
        protected override void Draw(GameTimer gt)
        {
            CommandAllocator cmdListAlloc = CurrFrameResource.CmdListAlloc;

            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            cmdListAlloc.Reset();

            // A command list can be reset after it has been added to the command queue via ExecuteCommandList.
            // Reusing the command list reuses memory.
            CommandList.Reset(cmdListAlloc, _psos["opaque"]);

            CommandList.SetViewport(Viewport);
            CommandList.SetScissorRectangles(ScissorRectangle);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            // Clear the back buffer and depth buffer.
            CommandList.ClearRenderTargetView(CurrentBackBufferView, new Color(_mainPassCB.FogColor));
            CommandList.ClearDepthStencilView(DepthStencilView, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(CurrentBackBufferView, DepthStencilView);

            CommandList.SetDescriptorHeaps(_descriptorHeaps.Length, _descriptorHeaps);

            CommandList.SetGraphicsRootSignature(_rootSignature);

            var passCBByteSize = D3DUtil.CalcConstantBufferByteSize <PassConstants>();

            // Draw opaque items--floors, walls, skull.
            Resource passCB = CurrFrameResource.PassCB.Resource;

            CommandList.SetGraphicsRootConstantBufferView(2, passCB.GPUVirtualAddress);

            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // Done recording commands.
            CommandList.Close();

            // Add the command list to the queue for execution.
            CommandQueue.ExecuteCommandList(CommandList);

            // Present the buffer to the screen. Presenting will automatically swap the back and front buffers.
            SwapChain.Present(0, PresentFlags.None);

            // Advance the fence value to mark commands up to this fence point.
            CurrFrameResource.Fence = ++CurrentFence;

            // Add an instruction to the command queue to set a new fence point.
            // Because we are on the GPU timeline, the new fence point won't be
            // set until the GPU finishes processing all the commands prior to this Signal().
            CommandQueue.Signal(Fence, CurrentFence);
        }
Beispiel #3
0
        protected override void Draw(GameTimer gt)
        {
            CommandAllocator cmdListAlloc = CurrFrameResource.CmdListAlloc;

            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            cmdListAlloc.Reset();

            // A command list can be reset after it has been added to the command queue via ExecuteCommandList.
            // Reusing the command list reuses memory.
            CommandList.Reset(cmdListAlloc, _isWireframe ? _psos["opaque_wireframe"] : _psos["opaque"]);

            CommandList.SetViewport(Viewport);
            CommandList.SetScissorRectangles(ScissorRectangle);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            // Clear the back buffer and depth buffer.
            CommandList.ClearRenderTargetView(CurrentBackBufferView, Color.LightSteelBlue);
            CommandList.ClearDepthStencilView(DepthStencilView, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(CurrentBackBufferView, DepthStencilView);

            CommandList.SetDescriptorHeaps(_descriptorHeaps.Length, _descriptorHeaps);

            CommandList.SetGraphicsRootSignature(_rootSignature);

            int passCbvIndex = _passCbvOffset + _currFrameResourceIndex;
            GpuDescriptorHandle passCbvHandle = _cbvHeap.GPUDescriptorHandleForHeapStart;

            passCbvHandle += passCbvIndex * CbvSrvUavDescriptorSize;
            CommandList.SetGraphicsRootDescriptorTable(1, passCbvHandle);

            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // Done recording commands.
            CommandList.Close();

            // Add the command list to the queue for execution.
            CommandQueue.ExecuteCommandList(CommandList);

            // Present the buffer to the screen. Presenting will automatically swap the back and front buffers.
            SwapChain.Present(0, PresentFlags.None);

            // Advance the fence value to mark commands up to this fence point.
            CurrFrameResource.Fence = ++CurrentFence;

            // Add an instruction to the command queue to set a new fence point.
            // Because we are on the GPU timeline, the new fence point won't be
            // set until the GPU finishes processing all the commands prior to this Signal().
            CommandQueue.Signal(Fence, CurrentFence);
        }
        public bool DrawFrame(Action <int, int> draw, [CallerMemberName] string frameName = null)
        {
            if (!this.renderLoop.NextFrame())
            {
                return(false);
            }

            CommandAllocator.Reset();
            bundleAllocator.Reset();

            commandList.Reset(CommandAllocator, null);

            BeginFrame(commandList);

            commandList.SetViewport(viewport);
            commandList.SetScissorRectangles(scissorRect);

            var commandLists = commandListPool.ToArray();

            commandQueue.ExecuteCommandLists(commandLists.Length, commandLists);
            bundlePool.Reset();

            commandList.ResourceBarrierTransition(RenderTarget, ResourceStates.Present, ResourceStates.RenderTarget);

            CpuDescriptorHandle rtvHandle = rtvHeap.CPUDescriptorHandleForHeapStart;

            rtvHandle += CurrentFrameIndex * rtvDescriptorSize;
            commandList.SetRenderTargets(1, rtvHandle, null);

            var clearColor = new RawColor4(0, 0, 0, 0);

            commandList.ClearRenderTargetView(rtvHandle, clearColor, 0, null);

            draw(window.Width, window.Height);

            foreach (var bundle in bundlePool)
            {
                commandList.ExecuteBundle(bundle);
            }

            bundlePool.Reset();

            commandList.ResourceBarrierTransition(RenderTarget, ResourceStates.RenderTarget, ResourceStates.Present);

            commandList.Close();

            commandQueue.ExecuteCommandList(commandList);

            swapChain.Present(1, PresentFlags.None);

            WaitForPrevFrame();

            return(true);
        }
        private void PopulateCommandList()
        {
            commandAllocator.Reset();
            commandList.Reset(commandAllocator, pipelineState);
            commandList.SetGraphicsRootSignature(rootSignature);

            commandList.SetViewport(viewport);
            commandList.SetScissorRectangles(scissorRect);
            commandList.ResourceBarrierTransition(renderTargets[frameIndex], ResourceStates.Present, ResourceStates.RenderTarget);
            var rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;

            rtvHandle += frameIndex * rtvDescriptorSize;


            commandList.ClearDepthStencilView(handleDSV, ClearFlags.FlagsDepth, 1.0f, 0);
            commandList.ClearRenderTargetView(rtvHandle, new Color4(0, 0.2F, 0.4f, 1), 0, null);
            commandList.SetRenderTargets(1, rtvHandle, false, handleDSV);

            DescriptorHeap[] descHeaps = new[] { samplerViewHeap, shaderRenderViewHeap }; // shaderRenderViewHeap,
            commandList.SetDescriptorHeaps(descHeaps.GetLength(0), descHeaps);
            commandList.SetGraphicsRootDescriptorTable(0, shaderRenderViewHeap.GPUDescriptorHandleForHeapStart);
            commandList.SetGraphicsRootDescriptorTable(3, samplerViewHeap.GPUDescriptorHandleForHeapStart);
            commandList.PipelineState = pipelineState;

            commandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;

            foreach (BufferView b in bufferViews)
            {
                commandList.SetGraphicsRoot32BitConstant(1, b.TexsCount, 0);
                commandList.SetGraphicsRootDescriptorTable(2, shaderRenderViewHeap.GPUDescriptorHandleForHeapStart + b.ViewStep);
                commandList.SetVertexBuffer(0, b.vertexBufferView);
                commandList.SetIndexBuffer(b.indexBufferView);
                commandList.DrawIndexedInstanced(b.IndexCount, 1, 0, 0, 0);
            }

            commandList.SetGraphicsRootSignature(terrainRootSignature);
            commandList.PipelineState = pipelineState2;
            descHeaps = new[] { samplerViewHeap, terrainHeap };
            commandList.SetDescriptorHeaps(descHeaps.GetLength(0), descHeaps);
            commandList.SetGraphicsRootDescriptorTable(0, terrainHeap.GPUDescriptorHandleForHeapStart + device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView) * 2);
            commandList.SetGraphicsRootDescriptorTable(1, terrainHeap.GPUDescriptorHandleForHeapStart);
            commandList.SetGraphicsRootDescriptorTable(2, samplerViewHeap.GPUDescriptorHandleForHeapStart);
            commandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            commandList.SetVertexBuffer(0, TerrainVertexBufferView);
            commandList.DrawInstanced(100 * 100, 1, 0, 0);

            commandList.ResourceBarrierTransition(renderTargets[frameIndex], ResourceStates.RenderTarget, ResourceStates.Present);

            commandList.Close();
        }
        public bool BeginFrame()
        {
            commandListAllocator.Reset();
            commandList.Reset(commandListAllocator, null);

            commandList.SetViewport(viewport);
            commandList.SetScissorRectangles(scissorRectangle);

            commandList.ResourceBarrierTransition(renderTarget, ResourceStates.Present, ResourceStates.RenderTarget);

            commandList.ClearRenderTargetView(descriptorHeap.CPUDescriptorHandleForHeapStart, new Color4(1.0f, 1.0f, 1.0f, 1.0f), 0, null);
            commandList.ResourceBarrierTransition(renderTarget, ResourceStates.RenderTarget, ResourceStates.Present);

            commandList.Close();

            return(true);
        }
        private void PopulateCommandList()
        {
            commandAllocator.Reset();
            commandList.Reset(commandAllocator, pipelineState);
            commandList.SetGraphicsRootSignature(rootSignature);

            //commandList.SetDescriptorHeaps(1, new DescriptorHeap[] { shaderRenderViewHeap });
            //commandList.SetGraphicsRootDescriptorTable(0, constantBufferViewHeap.GPUDescriptorHandleForHeapStart);

            commandList.SetViewport(viewport);
            commandList.SetScissorRectangles(scissorRect);
            commandList.ResourceBarrierTransition(renderTargets[frameIndex], ResourceStates.Present, ResourceStates.RenderTarget);
            var rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;

            rtvHandle += frameIndex * rtvDescriptorSize;


            commandList.ClearDepthStencilView(handleDSV, ClearFlags.FlagsDepth, 1.0f, 0);
            commandList.ClearRenderTargetView(rtvHandle, new Color4(0, 0.2F, 0.4f, 1), 0, null);
            commandList.SetRenderTargets(1, rtvHandle, false, handleDSV);

            commandList.SetGraphicsRootSignature(rootSignature);
            DescriptorHeap[] descHeaps = new[] { srvCbvHeap, samplerViewHeap };
            commandList.SetDescriptorHeaps(descHeaps.GetLength(0), descHeaps);
            commandList.SetGraphicsRootDescriptorTable(0, srvCbvHeap.GPUDescriptorHandleForHeapStart);
            commandList.SetGraphicsRootDescriptorTable(1, samplerViewHeap.GPUDescriptorHandleForHeapStart);
            commandList.PipelineState = pipelineState;

            commandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            commandList.SetVertexBuffer(0, vertexBufferView);
            commandList.SetIndexBuffer(indexBufferView);
            commandList.DrawIndexedInstanced(36, 1, 0, 0, 0);
            //commandList.DrawInstanced(3, 1, 0, 0);
            commandList.ResourceBarrierTransition(renderTargets[frameIndex], ResourceStates.RenderTarget, ResourceStates.Present);

            commandList.Close();
        }
        /// <summary>
        /// Fill the command list with commands
        /// </summary>
        private void PopulateCommandLists()
        {
            commandListAllocator.Reset();

            commandList.Reset(commandListAllocator, null);

            // setup viewport and scissors
            commandList.SetViewport(viewPort);
            commandList.SetScissorRectangles(scissorRectangle);

            // Use barrier to notify that we are using the RenderTarget to clear it
            commandList.ResourceBarrierTransition(renderTarget, ResourceStates.Present, ResourceStates.RenderTarget);

            // Clear the RenderTarget
            var time = clock.Elapsed.TotalSeconds;

            commandList.ClearRenderTargetView(descriptorHeap.CPUDescriptorHandleForHeapStart, new Color4((float)Math.Sin(time) * 0.25f + 0.5f, (float)Math.Sin(time * 0.5f) * 0.4f + 0.6f, 0.4f, 1.0f), 0, null);

            // Use barrier to notify that we are going to present the RenderTarget
            commandList.ResourceBarrierTransition(renderTarget, ResourceStates.RenderTarget, ResourceStates.Present);

            // Execute the command
            commandList.Close();
        }
        protected override void Draw(GameTimer gt)
        {
            CommandAllocator cmdListAlloc = CurrFrameResource.CmdListAlloc;

            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            cmdListAlloc.Reset();

            // A command list can be reset after it has been added to the command queue via ExecuteCommandList.
            // Reusing the command list reuses memory.
            CommandList.Reset(cmdListAlloc, _psos["opaque"]);

            CommandList.SetDescriptorHeaps(_descriptorHeaps.Length, _descriptorHeaps);

            UpdateWavesGPU(gt);

            CommandList.PipelineState = _psos["opaque"];

            CommandList.SetViewport(Viewport);
            CommandList.SetScissorRectangles(ScissorRectangle);

            // Change offscreen texture to be used as a a render target output.
            CommandList.ResourceBarrierTransition(_offscreenRT.Resource, ResourceStates.GenericRead, ResourceStates.RenderTarget);

            // Clear the back buffer and depth buffer.
            CommandList.ClearRenderTargetView(_offscreenRT.Rtv, new Color(_mainPassCB.FogColor));
            CommandList.ClearDepthStencilView(DepthStencilView, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(_offscreenRT.Rtv, DepthStencilView);

            CommandList.SetGraphicsRootSignature(_rootSignature);

            Resource passCB = CurrFrameResource.PassCB.Resource;

            CommandList.SetGraphicsRootConstantBufferView(2, passCB.GPUVirtualAddress);

            CommandList.SetGraphicsRootDescriptorTable(4, _waves.DisplacementMap);

            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            CommandList.PipelineState = _psos["alphaTested"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.AlphaTested]);

            CommandList.PipelineState = _psos["transparent"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Transparent]);

            CommandList.PipelineState = _psos["wavesRender"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.GpuWaves]);

            // Change offscreen texture to be used as an input.
            CommandList.ResourceBarrierTransition(_offscreenRT.Resource, ResourceStates.RenderTarget, ResourceStates.GenericRead);

            _sobelFilter.Execute(CommandList, _postProcessRootSignature, _psos["sobel"], _offscreenRT.Srv);

            //
            // Switching back to back buffer rendering.
            //

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(CurrentBackBufferView, DepthStencilView);

            CommandList.SetGraphicsRootSignature(_postProcessRootSignature);
            CommandList.PipelineState = _psos["composite"];
            CommandList.SetGraphicsRootDescriptorTable(0, _offscreenRT.Srv);
            CommandList.SetGraphicsRootDescriptorTable(1, _sobelFilter.OutputSrv);
            DrawFullscreenQuad(CommandList);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // Done recording commands.
            CommandList.Close();

            // Add the command list to the queue for execution.
            CommandQueue.ExecuteCommandList(CommandList);

            // Present the buffer to the screen. Presenting will automatically swap the back and front buffers.
            SwapChain.Present(0, PresentFlags.None);

            // Advance the fence value to mark commands up to this fence point.
            CurrFrameResource.Fence = ++CurrentFence;

            // Add an instruction to the command queue to set a new fence point.
            // Because we are on the GPU timeline, the new fence point won't be
            // set until the GPU finishes processing all the commands prior to this Signal().
            CommandQueue.Signal(Fence, CurrentFence);
        }
Beispiel #10
0
        /// <summary>
        /// Fill the command list with commands
        /// </summary>
        private void PopulateCommandLists()
        {
            var time = clock.Elapsed.TotalSeconds;

            commandListAllocator.Reset();

            commandList.Reset(commandListAllocator, pipelineState);

            // setup viewport and scissors
            commandList.SetViewport(viewPort);
            commandList.SetScissorRectangles(scissorRectangle);

            commandList.SetGraphicsRootSignature(rootSignature);

#if USE_TEXTURE
            commandList.SetDescriptorHeaps(2, descriptorsHeaps);
            commandList.SetGraphicsRootDescriptorTable(1, descriptorHeapCB.GPUDescriptorHandleForHeapStart);
            commandList.SetGraphicsRootDescriptorTable(2, descriptorHeapCB.GPUDescriptorHandleForHeapStart + descrOffsetCB);
            commandList.SetGraphicsRootDescriptorTable(3, descriptorHeapS.GPUDescriptorHandleForHeapStart);
#else
            commandList.SetDescriptorHeaps(1, descriptorsHeaps);
            commandList.SetGraphicsRootDescriptorTable(1, descriptorHeapCB.GPUDescriptorHandleForHeapStart);
#endif
            // Use barrier to notify that we are using the RenderTarget to clear it
            commandList.ResourceBarrierTransition(renderTarget, ResourceStates.Present, ResourceStates.RenderTarget);
            //commandList.ResourceBarrierTransition(depthBuffer, ResourceStates.Present, ResourceStates.DepthWrite);

            // Clear the RenderTarget
            commandList.ClearRenderTargetView(descriptorHeapRT.CPUDescriptorHandleForHeapStart, new Color4((float)Math.Sin(time) * 0.25f + 0.5f, (float)Math.Sin(time * 0.5f) * 0.4f + 0.6f, 0.4f, 1.0f), 0, null);
#if USE_DEPTH
            commandList.ClearDepthStencilView(descriptorHeapDS.CPUDescriptorHandleForHeapStart, ClearFlags.FlagsDepth, 1, 0, 0, null);
            commandList.SetRenderTargets(descriptorHeapRT.CPUDescriptorHandleForHeapStart, descriptorHeapDS.CPUDescriptorHandleForHeapStart);
#else
            commandList.SetRenderTargets(descriptorHeapRT.CPUDescriptorHandleForHeapStart, null);
#endif
            commandList.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
            commandList.SetVertexBuffers(0, vertexBufferView, 1);
#if USE_INSTANCES
            var world = Matrix.Scaling(0.3f) * Matrix.RotationY((float)time) * Matrix.RotationX((float)time / 2);
            world.Transpose();
            Utilities.Write(transWorldPtr, ref world);
            commandList.SetGraphicsRootConstantBufferView(0, transWorld.GPUVirtualAddress);

            commandList.SetVertexBuffers(1, instancesBufferView, 1);
#if USE_INDICES
            commandList.SetIndexBuffer(indexBufferView);
            commandList.DrawIndexedInstanced(34, 7, 0, 0, 0);
#else
            commandList.DrawInstanced(34, 7, 0, 0);
#endif
#else // no instances
            for (int i = 0; i < 7; i++)
            {
                var world = Matrix.Scaling(0.3f) *
                            Matrix.RotationY((float)time) *
                            Matrix.RotationX((float)time / 2) *
                            Matrix.Translation(instances[3 * i] / 3, instances[3 * i + 1] / 3, instances[3 * i + 2] / 3);
                world.Transpose();
                int offset = i * sizeOfMatrix;
                Utilities.Write(transWorldPtr + offset, ref world);
                commandList.SetGraphicsRootConstantBufferView(0, transWorld.GPUVirtualAddress + offset);

#if USE_INDICES
                commandList.SetIndexBuffer(indexBufferView);
                commandList.DrawIndexedInstanced(34, 1, 0, 0, 0);
#else
                commandList.DrawInstanced(34, 1, 0, 0);
#endif
            }
#endif

            // Use barrier to notify that we are going to present the RenderTarget
            commandList.ResourceBarrierTransition(renderTarget, ResourceStates.RenderTarget, ResourceStates.Present);
            //commandList.ResourceBarrierTransition(depthBuffer, ResourceStates.DepthWrite, ResourceStates.Present);

            // Execute the command
            commandList.Close();
        }
Beispiel #11
0
        //填充命令列表
        private void PopulateCommandList()
        {
            //命令列表分配器只有当相关的命令列表在GPU上执行完成后才能重置,应用应当使用围栏来确定GPU的执行进度
            commandAllocator.Reset();

            //但是当在特定的命令列表上调用ExecuteCommandList()时,可以随时重置该命令列表,并且必须在此之前重新写入
            commandList.Reset(commandAllocator, pipelineState);

            //设置根签名布局
            commandList.SetGraphicsRootSignature(rootSignature);

            //设置描述符堆
            //更改与命令列表相关联的当前绑定的描述符堆
            commandList.SetDescriptorHeaps(
                1,//要绑定的描述符堆的数量
                new DescriptorHeap[]
            {
                constantBufferViewHeap
            });    //指向要在命令列表上设置的堆的对象的指针

            //为描述符表设置图形根签名
            commandList.SetGraphicsRootDescriptorTable(
                0,                                                     //用于绑定的符堆序号
                renderTargetViewHeap.GPUDescriptorHandleForHeapStart); //用于设置基本描述符的GPU_descriptor_handle对象
            //将纹理绑定至根参数0
            commandList.SetGraphicsRootDescriptorTable(
                0,
                shaderRenderViewHeap.GPUDescriptorHandleForHeapStart
                );

            //设置视口和裁剪矩形
            commandList.SetViewport(viewPort);
            commandList.SetScissorRectangles(scissorRectangle);

            //按照资源的用途指示其状态的改变
            commandList.ResourceBarrierTransition(
                renderTargets[frameIndex],
                ResourceStates.Present,
                ResourceStates.RenderTarget);

            CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;

            rtvHandle += frameIndex * rtvDescriptorSize;

            //为渲染目标和深度模板设置CPU描述符句柄
            commandList.SetRenderTargets(rtvHandle, null);

            //写入命令
            commandList.ClearRenderTargetView(rtvHandle, new Color4(0.4f, 0.0f, 0.4f, 1), 0, null);

            //执行bundle
            commandList.ExecuteBundle(bundle);

            //按照资源的用途指示其状态的改变
            commandList.ResourceBarrierTransition(
                renderTargets[frameIndex],
                ResourceStates.RenderTarget,
                ResourceStates.Present);

            commandList.Close();
        }
Beispiel #12
0
        public static float[] AddGpu(float[] left, float[] right)
        {
            if (left.Length != right.Length)
            {
                throw new InvalidOperationException();
            }
            long size = (sizeof(float) * left.Length);

            float[] output = new float[left.Length];

            Resource leftInputBuffer  = DirectXHelpers.CreateBuffer(device, size, HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead);
            Resource rightInputBuffer = DirectXHelpers.CreateBuffer(device, size, HeapType.Upload, ResourceFlags.None, ResourceStates.GenericRead);
            Resource outputBuffer     = DirectXHelpers.CreateBuffer(device, size, HeapType.Readback, ResourceFlags.None, ResourceStates.CopyDestination);

            fixed(float *pLeft = &left[0])
            {
                var mappedMemory = leftInputBuffer.Map(0);

                Buffer.MemoryCopy(pLeft, mappedMemory.ToPointer(), size, size);
                leftInputBuffer.Unmap(0);
            }

            fixed(float *pRight = &right[0])
            {
                var mappedMemory = rightInputBuffer.Map(0);

                Buffer.MemoryCopy(pRight, mappedMemory.ToPointer(), size, size);
                rightInputBuffer.Unmap(0);
            }

            var rootParameters = new RootParameter[3]
            {
                new RootParameter(ShaderVisibility.All, new RootDescriptor(0, 0), RootParameterType.ShaderResourceView),
                new RootParameter(ShaderVisibility.All, new RootDescriptor(1, 0), RootParameterType.ShaderResourceView),
                new RootParameter(ShaderVisibility.All, new RootDescriptor(0, 0), RootParameterType.UnorderedAccessView)
            };
            var rootSignatureDesc = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout, rootParameters);

            RootSignature computeRootSignature = DirectXHelpers.CreateRootSignature(device, rootParameters);
            PipelineState computePipelineState = DirectXHelpers.CreateComputePipelineState(device, computeRootSignature, AddArrayShaderCode);

            //sw.Restart();
            commandList.Close();
            commandAllocator.Reset();
            {
                commandList.Reset(commandAllocator, computePipelineState);
                commandList.SetComputeRootSignature(computeRootSignature);
                commandList.SetComputeRootShaderResourceView(0, leftInputBuffer.GPUVirtualAddress);
                commandList.SetComputeRootShaderResourceView(1, rightInputBuffer.GPUVirtualAddress);
                commandList.SetComputeRootShaderResourceView(2, outputBuffer.GPUVirtualAddress);
                commandList.Dispatch(left.Length / ShaderThreadCount, 1, 1);
                commandList.Close();
            }
            commandQueue.ExecuteCommandList(commandList);
            FlushCommandQueue();
            commandList.Reset(commandAllocator, null);
            //sw.Stop();

            fixed(float *pOutput = &output[0])
            {
                var mappedMemory = outputBuffer.Map(0);

                Buffer.MemoryCopy(mappedMemory.ToPointer(), pOutput, size, size);
                outputBuffer.Unmap(0);
            }

            return(output);
        }
        protected override void Draw(GameTimer gt)
        {
            CommandAllocator cmdListAlloc = CurrFrameResource.CmdListAlloc;

            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            cmdListAlloc.Reset();

            // A command list can be reset after it has been added to the command queue via ExecuteCommandList.
            // Reusing the command list reuses memory.
            CommandList.Reset(cmdListAlloc, _psos["opaque"]);

            CommandList.SetViewport(Viewport);
            CommandList.SetScissorRectangles(ScissorRectangle);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            // Clear the back buffer and depth buffer.
            CommandList.ClearRenderTargetView(CurrentBackBufferView, Color.LightSteelBlue);
            CommandList.ClearDepthStencilView(DepthStencilView, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(CurrentBackBufferView, DepthStencilView);

            CommandList.SetDescriptorHeaps(_descriptorHeaps.Length, _descriptorHeaps);

            CommandList.SetGraphicsRootSignature(_rootSignature);

            Resource passCB = CurrFrameResource.PassCB.Resource;

            CommandList.SetGraphicsRootConstantBufferView(1, passCB.GPUVirtualAddress);

            // Bind all the materials used in this scene. For structured buffers, we can bypass the heap and
            // set as a root descriptor.
            Resource matBuffer = CurrFrameResource.MaterialBuffer.Resource;

            CommandList.SetGraphicsRootShaderResourceView(2, matBuffer.GPUVirtualAddress);

            // Bind the sky cube map. For our demos, we just use one "world" cube map representing the environment
            // from far away, so all objects will use the same cube map and we only need to set it once per-frame.
            // If we wanted to use "local" cube maps, we would have to change them per-object, or dynamically
            // index into an array of cube maps.
            GpuDescriptorHandle skyTexDescriptor = _srvDescriptorHeap.GPUDescriptorHandleForHeapStart;

            skyTexDescriptor += _skyTexHeapIndex * CbvSrvUavDescriptorSize;
            CommandList.SetGraphicsRootDescriptorTable(3, skyTexDescriptor);

            // Bind all the textures used in this scene. Observe
            // that we only have to specify the first descriptor in the table.
            // The root signature knows how many descriptors are expected in the table.
            CommandList.SetGraphicsRootDescriptorTable(4, _srvDescriptorHeap.GPUDescriptorHandleForHeapStart);

            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            CommandList.PipelineState = _psos["sky"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Sky]);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // Done recording commands.
            CommandList.Close();

            // Add the command list to the queue for execution.
            CommandQueue.ExecuteCommandList(CommandList);

            // Present the buffer to the screen. Presenting will automatically swap the back and front buffers.
            SwapChain.Present(0, PresentFlags.None);

            // Advance the fence value to mark commands up to this fence point.
            CurrFrameResource.Fence = ++CurrentFence;

            // Add an instruction to the command queue to set a new fence point.
            // Because we are on the GPU timeline, the new fence point won't be
            // set until the GPU finishes processing all the commands prior to this Signal().
            CommandQueue.Signal(Fence, CurrentFence);
        }
Beispiel #14
0
        protected override void Draw(GameTimer gt)
        {
            CommandAllocator cmdListAlloc = CurrFrameResource.CmdListAlloc;

            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            cmdListAlloc.Reset();

            // A command list can be reset after it has been added to the command queue via ExecuteCommandList.
            // Reusing the command list reuses memory.
            CommandList.Reset(cmdListAlloc, _psos["opaque"]);

            CommandList.SetViewport(Viewport);
            CommandList.SetScissorRectangles(ScissorRectangle);

            // Indicate a state transition on the resource usage.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            // Clear the back buffer and depth buffer.
            CommandList.ClearRenderTargetView(CurrentBackBufferView, new Color(_mainPassCB.FogColor));
            CommandList.ClearDepthStencilView(DepthStencilView, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1.0f, 0);

            // Specify the buffers we are going to render to.
            CommandList.SetRenderTargets(CurrentBackBufferView, DepthStencilView);

            CommandList.SetDescriptorHeaps(_descriptorHeaps.Length, _descriptorHeaps);

            CommandList.SetGraphicsRootSignature(_rootSignature);

            // Bind per-pass constant buffer. We only need to do this once per-pass.
            Resource passCB = CurrFrameResource.PassCB.Resource;

            CommandList.SetGraphicsRootConstantBufferView(2, passCB.GPUVirtualAddress);

            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Opaque]);

            CommandList.PipelineState = _psos["alphaTested"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.AlphaTested]);

            CommandList.PipelineState = _psos["transparent"];
            DrawRenderItems(CommandList, _ritemLayers[RenderLayer.Transparent]);

            _blurFilter.Execute(CommandList, _postProcessRootSignature, _psos["horzBlur"], _psos["vertBlur"], CurrentBackBuffer, 4);

            // Prepare to copy blurred output to the back buffer.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.CopySource, ResourceStates.CopyDestination);

            CommandList.CopyResource(CurrentBackBuffer, _blurFilter.Output);

            // Transition to PRESENT state.
            CommandList.ResourceBarrierTransition(CurrentBackBuffer, ResourceStates.CopyDestination, ResourceStates.Present);

            // Done recording commands.
            CommandList.Close();

            // Add the command list to the queue for execution.
            CommandQueue.ExecuteCommandList(CommandList);

            // Present the buffer to the screen. Presenting will automatically swap the back and front buffers.
            SwapChain.Present(0, PresentFlags.None);

            // Advance the fence value to mark commands up to this fence point.
            CurrFrameResource.Fence = ++CurrentFence;

            // Add an instruction to the command queue to set a new fence point.
            // Because we are on the GPU timeline, the new fence point won't be
            // set until the GPU finishes processing all the commands prior to this Signal().
            CommandQueue.Signal(Fence, CurrentFence);
        }
Beispiel #15
0
        private void PopulateCommandList()
        {
            // Command list allocators can only be reset when the associated
            // command lists have finished execution on the GPU; apps should use
            // fences to determine GPU execution progress.
            _commandAllocator.Reset();
            _bundleCommandAllocator.Reset();

            // However, when ExecuteCommandList() is called on a particular command
            // list, that command list can then be reset at any time and must be before
            // re-recording.
            _commandList.Reset(_commandAllocator, null);
            _bundleCommandList.Reset(_bundleCommandAllocator, _pipelineState);


            // Set necessary state.
            _commandList.SetGraphicsRootSignature(_rootSignature);

            List <DescriptorHeap> heaps = new List <DescriptorHeap>();

            foreach (GraphicsResource resource in _resources)
            {
                if (resource.type == ResourceType.DescriptorTable)
                {
                    heaps.Add(resource.Heap);
                }
            }

            if (heaps.Count > 0)
            {
                _commandList.SetDescriptorHeaps(heaps.ToArray());
            }
            foreach (GraphicsResource resource in _resources)
            {
                if (resource.type == ResourceType.ConstantBufferView)
                {
                    _commandList.SetGraphicsRootConstantBufferView(resource.Register, resource.Resource.GPUVirtualAddress);
                }
                else
                {
                    _commandList.SetGraphicsRootDescriptorTable(resource.Register, resource.Heap.GPUDescriptorHandleForHeapStart);
                }
            }
            _commandList.SetViewport(_viewport);
            _commandList.SetScissorRectangles(_scissorRect);

            // Indicate that the back buffer will be used as a render target.
            _commandList.ResourceBarrierTransition(_renderTargets[_frameIndex], ResourceStates.Present, ResourceStates.RenderTarget);


            CpuDescriptorHandle rtvHandle = _renderTargetViewHeap.CPUDescriptorHandleForHeapStart;

            rtvHandle += _frameIndex * _rtvDescriptorSize;
            _commandList.SetRenderTargets(1, rtvHandle, DepthStencilHandle);

            // Record commands.
            _commandList.ClearRenderTargetView(rtvHandle, Color.CornflowerBlue, 0, null);
            _commandList.ClearDepthStencilView(DepthStencilHandle, ClearFlags.FlagsDepth | ClearFlags.FlagsStencil, 1, 0);

            BundleDraw();

            _commandList.ExecuteBundle(_bundleCommandList);

            // Indicate that the back buffer will now be used to present.
            _commandList.ResourceBarrierTransition(_renderTargets[_frameIndex], ResourceStates.RenderTarget, ResourceStates.Present);

            _commandList.Close();
        }