private void DoComputeWork()
        {
            // Reuse the memory associated with command recording.
            // We can only reset when the associated command lists have finished execution on the GPU.
            DirectCmdListAlloc.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(DirectCmdListAlloc, _psos["vecAdd"]);

            CommandList.SetComputeRootSignature(_rootSignature);

            CommandList.SetComputeRootShaderResourceView(0, _inputBufferA.GPUVirtualAddress);
            CommandList.SetComputeRootShaderResourceView(1, _inputBufferB.GPUVirtualAddress);
            CommandList.SetComputeRootUnorderedAccessView(2, _outputBuffer.GPUVirtualAddress);

            CommandList.Dispatch(1, 1, 1);

            // Schedule to copy the data to the default buffer to the readback buffer.
            CommandList.ResourceBarrierTransition(_outputBuffer, ResourceStates.Common, ResourceStates.CopySource);

            CommandList.CopyResource(_readBackBuffer, _outputBuffer);

            CommandList.ResourceBarrierTransition(_outputBuffer, ResourceStates.CopySource, ResourceStates.Common);

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

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

            // Wait for the work to finish.
            FlushCommandQueue();

            // Map the data so we can read it on CPU.
            var    mappedData = new Data[NumDataElements];
            IntPtr ptr        = _readBackBuffer.Map(0);

            Utilities.Read(ptr, mappedData, 0, NumDataElements);

            using (var fstream = File.OpenWrite("results.txt"))
            {
                using (var strWriter = new StreamWriter(fstream))
                {
                    foreach (Data data in mappedData)
                    {
                        strWriter.WriteLine($"({data.V1.X}, {data.V1.Y}, {data.V1.Z}, {data.V2.X}, {data.V2.Y})");
                    }
                }
            }

            _readBackBuffer.Unmap(0);
        }
Пример #2
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);
        }