public DescriptorHandle Allocate(int count)
        {
            if (_currentHeap == null ||
                _remainingFreeHandles < count)
            {
                _currentHeap      = Device.RequestNewHeap(Type, DescriptorsPerHeap);
                _currentCpuHandle = _currentHeap.GetCPUDescriptorHandleForHeapStart();
                if (IsShaderVisible)
                {
                    _currentGpuHandle = _currentHeap.GetGPUDescriptorHandleForHeapStart();
                }

                _remainingFreeHandles = DescriptorsPerHeap;

                if (_descriptorSize == 0)
                {
                    _descriptorSize = Device.D3D12Device.GetDescriptorHandleIncrementSize(Type);
                }
            }

            var cpuHandle = _currentCpuHandle;

            _currentCpuHandle.Ptr += count * _descriptorSize;
            _remainingFreeHandles -= count;

            if (IsShaderVisible)
            {
                var gpuHandle = _currentGpuHandle;
                _currentGpuHandle.Ptr += (long)(count * _descriptorSize);
                return(new DescriptorHandle(_currentHeap, _descriptorSize, cpuHandle, gpuHandle));
            }

            return(new DescriptorHandle(_currentHeap, _descriptorSize, cpuHandle));
        }
        private void DrawRenderItems(GraphicsCommandList cmdList, List <RenderItem> ritems)
        {
            int objCBByteSize = D3DUtil.CalcConstantBufferByteSize <ObjectConstants>();
            int matCBByteSize = D3DUtil.CalcConstantBufferByteSize <MaterialConstants>();

            Resource objectCB = CurrFrameResource.ObjectCB.Resource;
            Resource matCB    = CurrFrameResource.MaterialCB.Resource;

            foreach (RenderItem ri in ritems)
            {
                cmdList.SetVertexBuffer(0, ri.Geo.VertexBufferView);
                cmdList.SetIndexBuffer(ri.Geo.IndexBufferView);
                cmdList.PrimitiveTopology = ri.PrimitiveType;

                GpuDescriptorHandle tex = _srvDescriptorHeap.GPUDescriptorHandleForHeapStart + ri.Mat.DiffuseSrvHeapIndex * CbvSrvUavDescriptorSize;

                long objCBAddress = objectCB.GPUVirtualAddress + ri.ObjCBIndex * objCBByteSize;
                long matCBAddress = matCB.GPUVirtualAddress + ri.Mat.MatCBIndex * matCBByteSize;

                cmdList.SetGraphicsRootDescriptorTable(0, tex);
                cmdList.SetGraphicsRootConstantBufferView(1, objCBAddress);
                cmdList.SetGraphicsRootConstantBufferView(3, matCBAddress);

                cmdList.DrawIndexedInstanced(ri.IndexCount, 1, ri.StartIndexLocation, ri.BaseVertexLocation, 0);
            }
        }
Example #3
0
 public DescriptorHandle(ID3D12DescriptorHeap heap, int sizeIncrement, CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle)
 {
     Heap          = heap;
     SizeIncrement = sizeIncrement;
     CpuHandle     = cpuHandle;
     GpuHandle     = gpuHandle;
 }
Example #4
0
        private void InitBundles()
        {
            // Create and record the bundle.
            bundle = device.CreateCommandList(0, CommandListType.Bundle, bundleAllocator, pipelineState);
            bundle.SetGraphicsRootSignature(rootSignature);
            bundle.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            bundle.SetVertexBuffer(0, vertexBufferView);
            bundle.SetIndexBuffer(indexBufferView);

            bundle.SetDescriptorHeaps(1, new DescriptorHeap[] { constantBufferViewHeap });

            //first cube
            GpuDescriptorHandle heapStart = constantBufferViewHeap.GPUDescriptorHandleForHeapStart;

            for (int i = 0; i < NumCubes; i++)
            {
                bundle.SetGraphicsRootDescriptorTable(0, heapStart);
                bundle.DrawIndexedInstanced(36, 1, 0, 0, 0);
                heapStart += constantBufferDescriptorSize;
            }

            bundle.Close();

            // Create synchronization objects.
            {
                fence      = device.CreateFence(0, FenceFlags.None);
                fenceValue = 1;

                // Create an event handle to use for frame synchronization.
                fenceEvent = new AutoResetEvent(false);
            }
        }
Example #5
0
        public void BuildDescriptors(
            Resource depthStencilBuffer,
            CpuDescriptorHandle cpuSrv,
            GpuDescriptorHandle gpuSrv,
            CpuDescriptorHandle cpuRtv,
            int cbvSrvUavDescriptorSize,
            int rtvDescriptorSize)
        {
            // Save references to the descriptors. The Ssao reserves heap space
            // for 5 contiguous Srvs.

            _ambientMap0CpuSrv     = cpuSrv;
            _ambientMap1CpuSrv     = cpuSrv + cbvSrvUavDescriptorSize;
            _normalMapCpuSrv       = cpuSrv + 2 * cbvSrvUavDescriptorSize;
            _depthMapCpuSrv        = cpuSrv + 3 * cbvSrvUavDescriptorSize;
            _randomVectorMapCpuSrv = cpuSrv + 4 * cbvSrvUavDescriptorSize;

            _ambientMap0GpuSrv = gpuSrv;
            _ambientMap1GpuSrv = gpuSrv + cbvSrvUavDescriptorSize;
            _normalMapGpuSrv   = gpuSrv + 2 * cbvSrvUavDescriptorSize;
            // We skip a depth map gpu srv.
            _randomVectorMapGpuSrv = gpuSrv + 4 * cbvSrvUavDescriptorSize;

            _normalMapCpuRtv   = cpuRtv;
            _ambientMap0CpuRtv = cpuRtv + rtvDescriptorSize;
            _ambientMap1CpuRtv = cpuRtv + 2 * rtvDescriptorSize;

            // Create the descriptors
            RebuildDescriptors(depthStencilBuffer);
        }
Example #6
0
        public void BuildDescriptors(CpuDescriptorHandle cpuSrv, GpuDescriptorHandle gpuSrv, CpuDescriptorHandle cpuRtv)
        {
            // Save references to the descriptors.
            _cpuSrv = cpuSrv;
            _gpuSrv = gpuSrv;
            _cpuRtv = cpuRtv;

            BuildDescriptors();
        }
Example #7
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);
        }
Example #8
0
        private GpuDescriptorHandle CopyDescriptors(DescriptorAllocator descriptorAllocator, CpuDescriptorHandle baseDescriptor, int descriptorCount)
        {
            GpuDescriptorHandle gpuDescriptorHandle   = descriptorAllocator.CurrentGpuDescriptorHandle;
            CpuDescriptorHandle destinationDescriptor = descriptorAllocator.Allocate(descriptorCount);

            GraphicsDevice.NativeDevice.CopyDescriptorsSimple(descriptorCount, destinationDescriptor, baseDescriptor, descriptorAllocator.DescriptorHeap.Description.Type);

            return(gpuDescriptorHandle);
        }
Example #9
0
        public void BuildDescriptors(CpuDescriptorHandle cpuDescriptor, GpuDescriptorHandle gpuDescriptor, int descriptorSize)
        {
            // Save references to the descriptors.
            _cpuSrv = cpuDescriptor;
            _cpuUav = cpuDescriptor + descriptorSize;
            _gpuSrv = gpuDescriptor;
            _gpuUav = gpuDescriptor + descriptorSize;

            BuildDescriptors();
        }
Example #10
0
 public DescriptorHandle(ID3D12DescriptorHeap heap, int sizeIncrement, CpuDescriptorHandle cpuHandle)
 {
     Heap          = heap;
     SizeIncrement = sizeIncrement;
     CpuHandle     = cpuHandle;
     GpuHandle     = new GpuDescriptorHandle
     {
         Ptr = InvalidAddress
     };
 }
Example #11
0
        public void Update(GameTimer gt, GraphicsCommandList cmdList, RootSignature rootSig, PipelineState pso)
        {
            // Accumulate time.
            _t += gt.DeltaTime;

            cmdList.PipelineState = pso;
            cmdList.SetComputeRootSignature(rootSig);

            // Only update the simulation at the specified time step.
            if (_t >= _timeStep)
            {
                // Set the update constants.
                Utilities.Pin(_k, ptr => cmdList.SetComputeRoot32BitConstants(0, 3, ptr, 0));

                cmdList.SetComputeRootDescriptorTable(1, _prevSolUav);
                cmdList.SetComputeRootDescriptorTable(2, _currSolUav);
                cmdList.SetComputeRootDescriptorTable(3, _nextSolUav);

                // How many groups do we need to dispatch to cover the wave grid.
                // Note that RowCount and ColumnCount should be divisible by 16
                // so there is no remainder.
                int numGroupsX = ColumnCount / 16;
                int numGroupsY = RowCount / 16;
                cmdList.Dispatch(numGroupsX, numGroupsY, 1);

                //
                // Ping-pong buffers in preparation for the next update.
                // The previous solution is no longer needed and becomes the target of the next solution in the next update.
                // The current solution becomes the previous solution.
                // The next solution becomes the current solution.
                //

                Resource resTemp = _prevSol;
                _prevSol = _currSol;
                _currSol = _nextSol;
                _nextSol = resTemp;

                GpuDescriptorHandle srvTemp = _prevSolSrv;
                _prevSolSrv = _currSolSrv;
                _currSolSrv = _nextSolSrv;
                _nextSolSrv = srvTemp;

                GpuDescriptorHandle uavTemp = _prevSolUav;
                _prevSolUav = _currSolUav;
                _currSolUav = _nextSolUav;
                _nextSolUav = uavTemp;

                // Reset time.
                _t = 0.0f;

                // The current solution needs to be able to be read by the vertex shader, so change its state to GENERIC_READ.
                cmdList.ResourceBarrierTransition(_currSol, ResourceStates.UnorderedAccess, ResourceStates.GenericRead);
            }
        }
Example #12
0
        private void CBVSRVUAVHandle(out CpuDescriptorHandle cpuHandle, out GpuDescriptorHandle gpuHandle)
        {
            CpuDescriptorHandle handle = graphicsDevice.cbvsrvuavHeap.GetCPUDescriptorHandleForHeapStart();

            handle.Ptr += graphicsDevice.cbvsrvuavAllocatedCount * graphicsDevice.cbvsrvuavHeapIncrementSize;
            GpuDescriptorHandle gpuHandle1 = graphicsDevice.cbvsrvuavHeap.GetGPUDescriptorHandleForHeapStart();

            gpuHandle1.Ptr += (ulong)(graphicsDevice.cbvsrvuavAllocatedCount * graphicsDevice.cbvsrvuavHeapIncrementSize);

            graphicsDevice.cbvsrvuavAllocatedCount = (graphicsDevice.cbvsrvuavAllocatedCount + 1) % graphicsDevice.CBVSRVUAVDescriptorCount;
            cpuHandle = handle;
            gpuHandle = gpuHandle1;
        }
Example #13
0
        public void GetTempHandle(out CpuDescriptorHandle cpuHandle, out GpuDescriptorHandle gpuHandle)
        {
            CpuDescriptorHandle cpuHandle1 = heap.GetCPUDescriptorHandleForHeapStart();

            cpuHandle1.Ptr += (nuint)(allocatedCount * IncrementSize);
            GpuDescriptorHandle gpuHandle1 = heap.GetGPUDescriptorHandleForHeapStart();

            gpuHandle1.Ptr += (ulong)(allocatedCount * IncrementSize);

            allocatedCount = (allocatedCount + 1) % descriptorCount;
            cpuHandle      = cpuHandle1;
            gpuHandle      = gpuHandle1;
        }
        public void BuildDescriptors(
            CpuDescriptorHandle cpuSrv,
            GpuDescriptorHandle gpuSrv,
            CpuDescriptorHandle cpuDsv)
        {
            // Save references to the descriptors.
            _cpuSrv = cpuSrv;
            _gpuSrv = gpuSrv;
            _cpuDsv = cpuDsv;

            //  Create the descriptors
            BuildDescriptors();
        }
        public Boolean ClearUAV(H1GpuBuffer target)
        {
            H1GpuBuffer             targetDX12 = target as H1GpuBuffer;
            H1DynamicDescriptorHeap dynamicDescriptorHeapDX12 = DynamicDescriptorHeap;
            GpuDescriptorHandle     gpuVisibleHandle          = new GpuDescriptorHandle();

            //if (dynamicDescriptorHeapDX12.UploadDirect(targetDX12.UAV, ref gpuVisibleHandle) == false)
            //{
            //    return false;
            //}

            return(true);
        }
Example #16
0
        public DescriptorTablePoolEntry(
            int heapIndex,
            int descriptorCount,
            GpuDescriptorHandle gpuDescriptorHandle,
            CpuDescriptorHandle cpuDescriptorHandle,
            int incrementSize)
        {
            HeapIndex           = heapIndex;
            DescriptorCount     = descriptorCount;
            GpuDescriptorHandle = gpuDescriptorHandle;
            CpuDescriptorHandle = cpuDescriptorHandle;

            _incrementSize = incrementSize;
        }
Example #17
0
        public void BuildDescriptors(CpuDescriptorHandle cpuDescriptor, GpuDescriptorHandle gpuDescriptor, int descriptorSize)
        {
            _blur0CpuSrv = cpuDescriptor;
            _blur0CpuUav = cpuDescriptor + descriptorSize;
            _blur1CpuSrv = cpuDescriptor + descriptorSize * 2;
            _blur1CpuUav = cpuDescriptor + descriptorSize * 3;

            _blur0GpuSrv = gpuDescriptor;
            _blur0GpuUav = gpuDescriptor + descriptorSize;
            _blur1GpuSrv = gpuDescriptor + descriptorSize * 2;
            _blur1GpuUav = gpuDescriptor + descriptorSize * 3;

            BuildDescriptors();
        }
Example #18
0
        /// <summary>
        /// Creates a new <see cref="DescriptorAllocator"/> instance with the specified parameters
        /// </summary>
        /// <param name="device">The <see cref="GraphicsDevice"/> instance to use</param>
        public DescriptorAllocator(GraphicsDevice device)
        {
            DescriptorSize = device.NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            DescriptorHeapDescription descriptorHeapDescription = new DescriptorHeapDescription
            {
                DescriptorCount = DescriptorsPerHeap,
                Flags           = DescriptorHeapFlags.ShaderVisible,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView
            };

            DescriptorHeap = device.NativeDevice.CreateDescriptorHeap(descriptorHeapDescription);

            _RemainingHandles = DescriptorsPerHeap;
            _CurrentCpuHandle = DescriptorHeap.GetCPUDescriptorHandleForHeapStart();
            _CurrentGpuHandle = DescriptorHeap.GetGPUDescriptorHandleForHeapStart();
        }
Example #19
0
        private void DrawRenderItems(GraphicsCommandList cmdList, List <RenderItem> ritems)
        {
            // For each render item...
            foreach (RenderItem ri in ritems)
            {
                cmdList.SetVertexBuffer(0, ri.Geo.VertexBufferView);
                cmdList.SetIndexBuffer(ri.Geo.IndexBufferView);
                cmdList.PrimitiveTopology = ri.PrimitiveType;

                // Offset to the CBV in the descriptor heap for this object and for this frame resource.
                int cbvIndex = _currFrameResourceIndex * _allRitems.Count + ri.ObjCBIndex;
                GpuDescriptorHandle cbvHandle = _cbvHeap.GPUDescriptorHandleForHeapStart;
                cbvHandle += cbvIndex * CbvSrvUavDescriptorSize;

                cmdList.SetGraphicsRootDescriptorTable(0, cbvHandle);

                cmdList.DrawIndexedInstanced(ri.IndexCount, 1, ri.StartIndexLocation, ri.BaseVertexLocation, 0);
            }
        }
        private void InitBundles()
        {
            // Create and record the bundle.
            bundleAllocator.Reset();
            bundle = device.CreateCommandList(0, CommandListType.Bundle, bundleAllocator, pipelineState);

            //Set Heap
            bundle.SetDescriptorHeaps(2, new DescriptorHeap[] { resourceViewHeap, samplerViewHeap });

            bundle.SetGraphicsRootSignature(rootSignature);
            bundle.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            bundle.SetVertexBuffer(0, vertexBufferView);
            bundle.SetIndexBuffer(indexBufferView);


            //model
            GpuDescriptorHandle heapStart = resourceViewHeap.GPUDescriptorHandleForHeapStart;

            //constant buffer
            bundle.SetGraphicsRootDescriptorTable(0, heapStart + constantBufferViewPosition * device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView));

            //sampler
            bundle.SetGraphicsRootDescriptorTable(2, samplerViewHeap.GPUDescriptorHandleForHeapStart);

            //set materials and draw
            int offset = 0;
            int k      = 0;

            foreach (var count in faceCounts)
            {
                //Texture coordinates start after constant buffer
                bundle.SetGraphicsRootDescriptorTable(1, heapStart + (meshTexturePosition + k) * device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView));
                bundle.DrawIndexedInstanced(count, 1, offset, 0, 0);
                offset += count;
                k++;
            }



            bundle.Close();
        }
Example #21
0
        private void InitBundles()
        {
            // Create and record the bundle.
            bundleAllocator.Reset();
            bundle = device.CreateCommandList(0, CommandListType.Bundle, bundleAllocator, pipelineState);

            //Set Heap
            bundle.SetDescriptorHeaps(2, new DescriptorHeap[] { constantBufferViewHeap, samplerViewHeap });

            bundle.SetGraphicsRootSignature(rootSignature);
            bundle.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            bundle.SetVertexBuffer(0, vertexBufferView);
            bundle.SetIndexBuffer(indexBufferView);


            //model
            GpuDescriptorHandle heapStart = constantBufferViewHeap.GPUDescriptorHandleForHeapStart;

            //constant buffer
            bundle.SetGraphicsRootDescriptorTable(0, heapStart);
            heapStart += constantBufferDescriptorSize;

            //sampler
            bundle.SetGraphicsRootDescriptorTable(2, samplerViewHeap.GPUDescriptorHandleForHeapStart);


            int offset = 0;

            foreach (var count in faceCounts)
            {
                //Texture coordinates start after constant buffer
                bundle.SetGraphicsRootDescriptorTable(1, heapStart);
                bundle.DrawIndexedInstanced(count, 1, offset, 0, 0);
                offset    += count;
                heapStart += constantBufferDescriptorSize;
            }

            heapStart += constantBufferDescriptorSize;

            bundle.Close();
        }
Example #22
0
        /// <summary>
        /// Allocates a new CPU and GPU handle pair to use in a memory buffer
        /// </summary>
        /// <returns>A pair of <see cref="CpuDescriptorHandle"/> and <see cref="GpuDescriptorHandle"/> to use</returns>
        public (CpuDescriptorHandle, GpuDescriptorHandle) Allocate()
        {
            lock (Lock)
            {
                if (_RemainingHandles == 0)
                {
                    _RemainingHandles = DescriptorHeap.Description.DescriptorCount;
                    _CurrentCpuHandle = DescriptorHeap.GetCPUDescriptorHandleForHeapStart();
                    _CurrentGpuHandle = DescriptorHeap.GetGPUDescriptorHandleForHeapStart();
                }

                CpuDescriptorHandle cpuDescriptorHandle = _CurrentCpuHandle;
                GpuDescriptorHandle gpuDescriptorHandle = _CurrentGpuHandle;

                _CurrentCpuHandle += DescriptorSize;
                _CurrentGpuHandle += DescriptorSize;
                _RemainingHandles -= 1;

                return(cpuDescriptorHandle, gpuDescriptorHandle);
            }
        }
Example #23
0
        public void BuildDescriptors(CpuDescriptorHandle cpuDescriptor, GpuDescriptorHandle gpuDescriptor, int descriptorSize)
        {
            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Format    = Format.R32_Float,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip = 0,
                    MipLevels       = 1
                }
            };

            var uavDesc = new UnorderedAccessViewDescription
            {
                Format    = Format.R32_Float,
                Dimension = UnorderedAccessViewDimension.Texture2D,
                Texture2D = new UnorderedAccessViewDescription.Texture2DResource
                {
                    MipSlice = 0
                }
            };

            _device.CreateShaderResourceView(_prevSol, srvDesc, cpuDescriptor);
            _device.CreateShaderResourceView(_currSol, srvDesc, cpuDescriptor + descriptorSize);
            _device.CreateShaderResourceView(_nextSol, srvDesc, cpuDescriptor + descriptorSize * 2);

            _device.CreateUnorderedAccessView(_prevSol, null, uavDesc, cpuDescriptor + descriptorSize * 3);
            _device.CreateUnorderedAccessView(_currSol, null, uavDesc, cpuDescriptor + descriptorSize * 4);
            _device.CreateUnorderedAccessView(_nextSol, null, uavDesc, cpuDescriptor + descriptorSize * 5);

            // Save references to the GPU descriptors.
            _prevSolSrv = gpuDescriptor;
            _currSolSrv = gpuDescriptor + descriptorSize;
            _nextSolSrv = gpuDescriptor + descriptorSize * 2;
            _prevSolUav = gpuDescriptor + descriptorSize * 3;
            _currSolUav = gpuDescriptor + descriptorSize * 4;
            _nextSolUav = gpuDescriptor + descriptorSize * 5;
        }
        public Boolean UploadDirect(CpuDescriptorHandle handle, ref GpuDescriptorHandle result)
        {
            if (!HasSpace(1))
            {
                // @TODO - currently just return and make it false
                return(false);
            }

            H1CommandContext commandContextDX12 = m_CommandContextRef;

            commandContextDX12.SetDescriptorHeap(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, GetHeap());

            H1DescriptorHandle destHandle = m_FirstDescriptor + m_CurrOffset * GetDescriptorSize();

            m_CurrOffset += 1;

            H1Global <H1ManagedRenderer> .Instance.Device.CopyDescriptorsSimple(1, destHandle.CpuHandle, handle, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            result = destHandle.GpuHandle;

            return(true);
        }
        private void BuildDescriptorHeaps()
        {
            //
            // Create the SRV heap.
            //
            var srvHeapDesc = new DescriptorHeapDescription
            {
                DescriptorCount = 14,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
                Flags           = DescriptorHeapFlags.ShaderVisible
            };

            _srvDescriptorHeap = Device.CreateDescriptorHeap(srvHeapDesc);
            _descriptorHeaps   = new[] { _srvDescriptorHeap };

            //
            // Fill out the heap with actual descriptors.
            //
            CpuDescriptorHandle hDescriptor = _srvDescriptorHeap.CPUDescriptorHandleForHeapStart;

            Resource[] tex2DList =
            {
                _textures["bricksDiffuseMap"].Resource,
                _textures["bricksNormalMap"].Resource,
                _textures["tileDiffuseMap"].Resource,
                _textures["tileNormalMap"].Resource,
                _textures["defaultDiffuseMap"].Resource,
                _textures["defaultNormalMap"].Resource,
            };
            Resource skyCubeMap = _textures["skyCubeMap"].Resource;

            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip     = 0,
                    ResourceMinLODClamp = 0.0f
                }
            };

            foreach (Resource tex2D in tex2DList)
            {
                srvDesc.Format = tex2D.Description.Format;
                srvDesc.Texture2D.MipLevels = tex2D.Description.MipLevels;

                Device.CreateShaderResourceView(tex2D, srvDesc, hDescriptor);

                // Next descriptor.
                hDescriptor += CbvSrvUavDescriptorSize;
            }

            srvDesc.Dimension   = ShaderResourceViewDimension.TextureCube;
            srvDesc.TextureCube = new ShaderResourceViewDescription.TextureCubeResource
            {
                MostDetailedMip     = 0,
                MipLevels           = skyCubeMap.Description.MipLevels,
                ResourceMinLODClamp = 0.0f
            };
            srvDesc.Format = skyCubeMap.Description.Format;
            Device.CreateShaderResourceView(skyCubeMap, srvDesc, hDescriptor);

            _skyTexHeapIndex    = tex2DList.Length;
            _shadowMapHeapIndex = _skyTexHeapIndex + 1;

            _nullCubeSrvIndex = _shadowMapHeapIndex + 1;

            CpuDescriptorHandle srvCpuStart = _srvDescriptorHeap.CPUDescriptorHandleForHeapStart;
            GpuDescriptorHandle srvGpuStart = _srvDescriptorHeap.GPUDescriptorHandleForHeapStart;
            CpuDescriptorHandle dsvCpuStart = DsvHeap.CPUDescriptorHandleForHeapStart;

            CpuDescriptorHandle nullSrv = srvCpuStart + _nullCubeSrvIndex * CbvSrvUavDescriptorSize;

            _nullSrv = srvGpuStart + _nullCubeSrvIndex * CbvSrvUavDescriptorSize;

            Device.CreateShaderResourceView(null, srvDesc, nullSrv);
            nullSrv += CbvSrvUavDescriptorSize;

            srvDesc.Dimension = ShaderResourceViewDimension.Texture2D;
            srvDesc.Format    = Format.R8G8B8A8_UNorm;
            srvDesc.Texture2D = new ShaderResourceViewDescription.Texture2DResource
            {
                MostDetailedMip     = 0,
                MipLevels           = 1,
                ResourceMinLODClamp = 0.0f
            };
            Device.CreateShaderResourceView(null, srvDesc, nullSrv);

            _shadowMap.BuildDescriptors(
                srvCpuStart + _shadowMapHeapIndex * CbvSrvUavDescriptorSize,
                srvGpuStart + _shadowMapHeapIndex * CbvSrvUavDescriptorSize,
                dsvCpuStart + DsvDescriptorSize);
        }
        private void BuildDescriptorHeaps()
        {
            // Offscreen RTV goes after the swap chain descriptors.
            const int rtvOffset = SwapChainBufferCount;

            const int srvCount = 3;

            int waveSrvOffset      = srvCount;
            int sobelSrvOffset     = waveSrvOffset + _waves.DescriptorCount;
            int offscreenSrvOffset = sobelSrvOffset + _sobelFilter.DescriptorCount;

            //
            // Create the SRV heap.
            //
            var srvHeapDesc = new DescriptorHeapDescription
            {
                DescriptorCount = srvCount + _waves.DescriptorCount + _sobelFilter.DescriptorCount + 1, // Extra offscreen render target.
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
                Flags           = DescriptorHeapFlags.ShaderVisible
            };

            _srvDescriptorHeap = Device.CreateDescriptorHeap(srvHeapDesc);
            _descriptorHeaps   = new[] { _srvDescriptorHeap };

            //
            // Fill out the heap with actual descriptors.
            //
            CpuDescriptorHandle hDescriptor = _srvDescriptorHeap.CPUDescriptorHandleForHeapStart;

            Resource[] tex2DList =
            {
                _textures["grassTex"].Resource,
                _textures["waterTex"].Resource,
                _textures["fenceTex"].Resource
            };

            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip = 0,
                    MipLevels       = -1,
                }
            };

            foreach (Resource tex2D in tex2DList)
            {
                srvDesc.Format = tex2D.Description.Format;
                Device.CreateShaderResourceView(tex2D, srvDesc, hDescriptor);

                // Next descriptor.
                hDescriptor += CbvSrvUavDescriptorSize;
            }

            CpuDescriptorHandle srvCpuStart = _srvDescriptorHeap.CPUDescriptorHandleForHeapStart;
            GpuDescriptorHandle srvGpuStart = _srvDescriptorHeap.GPUDescriptorHandleForHeapStart;

            CpuDescriptorHandle rtvCpuStart = RtvHeap.CPUDescriptorHandleForHeapStart;

            _waves.BuildDescriptors(
                _srvDescriptorHeap.CPUDescriptorHandleForHeapStart + waveSrvOffset * CbvSrvUavDescriptorSize,
                _srvDescriptorHeap.GPUDescriptorHandleForHeapStart + waveSrvOffset * CbvSrvUavDescriptorSize,
                CbvSrvUavDescriptorSize);

            _sobelFilter.BuildDescriptors(
                srvCpuStart + sobelSrvOffset * CbvSrvUavDescriptorSize,
                srvGpuStart + sobelSrvOffset * CbvSrvUavDescriptorSize,
                CbvSrvUavDescriptorSize);

            _offscreenRT.BuildDescriptors(
                srvCpuStart + offscreenSrvOffset * CbvSrvUavDescriptorSize,
                srvGpuStart + offscreenSrvOffset * CbvSrvUavDescriptorSize,
                rtvCpuStart + rtvOffset * RtvDescriptorSize);
        }
Example #27
0
        private void PopulateCommandListsForVisualDebugDrawing()
        {
            m_CommandList.CommandAllocator.Reset();

            m_CommandList.CommandList.Reset(m_CommandList.CommandAllocator, m_PipelineState);

            // set viewport and scissors
            m_CommandList.CommandList.SetGraphicsRootSignature(m_RootSignature);

            // @TODO - redesign this section
            m_TransformationCBPointer = m_ConstantBuffer.Map(0);
            //Utilities.Write(m_TransformationCBPointer, ref m_TransformationCB);
            List <Matrix> dataToCopy = new List <Matrix>();

            dataToCopy.Add(m_TransformationCB.viewProjectionMatrix);
            foreach (Matrix mtx in m_TransformationCB.BoneMatrices)
            {
                dataToCopy.Add(mtx);
            }
            Utilities.Write(m_TransformationCBPointer, dataToCopy.ToArray(), 0, dataToCopy.Count);
            m_ConstantBuffer.Unmap(0);

            m_CommandList.CommandList.SetDescriptorHeaps(1, new DescriptorHeap[] { m_ConstantBufferViewHeap });
            //m_CommandList.SetDescriptorHeaps(2, new DescriptorHeap[] { m_ConstantBufferViewHeap, m_srvDescriptorHeap });

            GpuDescriptorHandle hDescriptor = m_ConstantBufferViewHeap.GPUDescriptorHandleForHeapStart;

            m_CommandList.CommandList.SetGraphicsRootDescriptorTable(0, hDescriptor);

            hDescriptor += m_ConstantBufferDescriptorSize;
            //Int32 sizeInBytes = (Utilities.SizeOf<TransformationCB>() + 255) & ~255;
            //hDescriptor += sizeInBytes;

            m_CommandList.CommandList.SetGraphicsRootDescriptorTable(1, hDescriptor);

            m_CommandList.CommandList.SetViewport(m_Viewport);
            m_CommandList.CommandList.SetScissorRectangles(m_SissorRect);

            // use barrier to notify we are using the m_RenderTarget
            Resource rtvBackBuffer = m_SwapChainDX12.GetBackBuffer(m_FrameIndex);

            m_CommandList.CommandList.ResourceBarrierTransition(rtvBackBuffer, ResourceStates.Present, ResourceStates.RenderTarget);

            //CpuDescriptorHandle rtvHandle = m_RenderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            //rtvHandle += m_FrameIndex * m_RTVDescriptorSize;
            //CpuDescriptorHandle dsvHandle = m_DepthStencilViewHeap.CPUDescriptorHandleForHeapStart;

            CpuDescriptorHandle rtvHandle = m_DeviceContext.Dx12Device.RenderTargetDescriptorCache.GetCpuAddressByOffset(m_FrameIndex);
            // @TODO - need to set the depth value consistent with frame counts!
            CpuDescriptorHandle dsvHandle = m_DeviceContext.Dx12Device.DepthStencilDescriptorCache.GetCpuAddressByOffset(0);

            m_CommandList.CommandList.SetRenderTargets(rtvHandle, dsvHandle);

            // clear the render target & depth stencil
            m_CommandList.CommandList.ClearRenderTargetView(rtvHandle, new Color4(0, 0.2f, 0.4f, 1), 0, null);
            m_CommandList.CommandList.ClearDepthStencilView(dsvHandle, ClearFlags.FlagsDepth, 1.0f, 0, 0, null);

            // record commands
            //m_CommandList.CommandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            //m_CommandList.SetVertexBuffer(0, m_VertexBufferView);
            // @TODO - I need to wrap command list separately!
            //H1StaticMeshLODResource resource = m_TempStaticMesh.StaticMeshData.GetLODResource(0);
            //H1StaticMeshLODResource resource = H1Global<H1World>.Instance.PersistentLevel.GetActor(0).GetActorComponent<H1StaticMeshComponent>().StaticMesh.StaticMeshData.GetLODResource(1);
            //resource.LocalVertexFactory.setVertexBuffers(m_CommandList.CommandList);

            //m_CommandList.DrawInstanced(3, 1, 0, 0);
            //m_CommandList.SetIndexBuffer(m_IndexBufferView);
            //m_CommandList.CommandList.SetIndexBuffer(resource.IndexBuffer.View);

            //int instanceCount = Convert.ToInt32(resource.IndexBuffer.Count);
            //m_CommandList.CommandList.DrawIndexedInstanced(instanceCount, 1, 0, 0, 0);

            //H1RenderUtils.DrawBox(m_CommandList.CommandList, new H1Transform());
            //H1RenderUtils.DrawPlane10x10(m_CommandList.CommandList, new Vector2(0, 0), new Vector2(1, 1));
            //H1RenderUtils.DrawSphere(m_CommandList.CommandList, new Vector3(0, 0, 0), new Vector3(1), 8, 6);
            //AngleSingle angle1 = new AngleSingle(10.0f, AngleType.Degree);
            //H1RenderUtils.DrawCone(m_CommandList.CommandList, angle1.Radians, angle1.Radians, 24, false, new Vector4(1));
            //H1RenderUtils.DrawCylinder(m_CommandList.CommandList, new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1, 3, 10);
            //H1RenderUtils.DrawCapsule(m_CommandList.CommandList, new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1, 3, 10);
            H1RenderUtils.DrawDisc(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 1, 10);
            //H1RenderUtils.DrawFlatArrow(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), 10, 1, 0.1f);
            //H1RenderUtils.DrawWireBox(m_CommandList.CommandList, Matrix.Identity, new Vector3(-1, -1, -1), new Vector3(1), 1, 0, 0);
            //H1RenderUtils.DrawWireSphere(m_CommandList.CommandList, new Vector3(0), 2.0f, 24);
            //H1RenderUtils.DrawWireCylinder(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1.0f, 3, 10);
            //H1RenderUtils.DrawWireCapsule(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), 1.0f, 3, 10);
            //H1RenderUtils.DrawWireCone(m_CommandList.CommandList, Matrix.Identity, 1.0f, 15.0f, 10);
            //H1RenderUtils.DrawWireSphereCappedCone(m_CommandList.CommandList, Matrix.Identity, 1.0f, 15.0f, 16, 4, 10);
            //H1RenderUtils.DrawOrientedWireBox(m_CommandList.CommandList, new Vector3(0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new Vector3(2));
            //H1RenderUtils.DrawCoordinateSystem(m_CommandList.CommandList, new Vector3(0), Matrix.Identity, 2.0f);
            //H1RenderUtils.DrawDirectionalArrow(m_CommandList.CommandList, Matrix.Identity, 2.0f, 0.2f);
            //H1RenderUtils.DrawWireStar(m_CommandList.CommandList, new Vector3(0), 1.0f);
            //H1RenderUtils.DrawDashedLine(m_CommandList.CommandList, new Vector3(-1), new Vector3(1), 0.2f);
            //H1RenderUtils.DrawWireDiamond(m_CommandList.CommandList, Matrix.Identity, 0.2f);

            //H1SkeletalMeshComponent skeletalMeshComponent = H1Global<H1World>.Instance.PersistentLevel.GetActor(0).GetActorComponent<H1SkeletalMeshComponent>();
            //skeletalMeshComponent.SkeletalMesh.SkeletonDebugDrawing(m_CommandList.CommandList);

            // execute commands
            H1Global <Commands.H1CommandManager> .Instance.ExecuteCommands();

            // use barrier to notify that we are going to present the render target
            m_CommandList.CommandList.ResourceBarrierTransition(rtvBackBuffer, ResourceStates.RenderTarget, ResourceStates.Present);

            // execute the command
            m_CommandList.CommandList.Close();
        }
Example #28
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DescriptorHandle"/> class.
 /// </summary>
 /// <param name="cpuHandle">The <see cref="CpuDescriptorHandle"/></param>
 /// <param name="gpuHandle">The <see cref="GpuDescriptorHandle"/></param>
 public DescriptorHandle(CpuDescriptorHandle cpuHandle, GpuDescriptorHandle gpuHandle)
 {
     _CPUHandle = cpuHandle;
     _GPUHandle = gpuHandle;
 }
        private void BuildDescriptorHeaps()
        {
            //
            // Create the SRV heap.
            //
            var srvHeapDesc = new DescriptorHeapDescription
            {
                DescriptorCount = 6,
                Type            = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
                Flags           = DescriptorHeapFlags.ShaderVisible
            };

            _srvDescriptorHeap = Device.CreateDescriptorHeap(srvHeapDesc);
            _descriptorHeaps   = new[] { _srvDescriptorHeap };

            //
            // Fill out the heap with actual descriptors.
            //
            CpuDescriptorHandle hDescriptor = _srvDescriptorHeap.CPUDescriptorHandleForHeapStart;

            Resource[] tex2DList =
            {
                _textures["bricksDiffuseMap"].Resource,
                _textures["tileDiffuseMap"].Resource,
                _textures["defaultDiffuseMap"].Resource
            };
            Resource skyTex = _textures["skyCubeMap"].Resource;

            var srvDesc = new ShaderResourceViewDescription
            {
                Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource
                {
                    MostDetailedMip     = 0,
                    ResourceMinLODClamp = 0.0f
                }
            };

            foreach (Resource tex2D in tex2DList)
            {
                srvDesc.Format = tex2D.Description.Format;
                srvDesc.Texture2D.MipLevels = tex2D.Description.MipLevels;
                Device.CreateShaderResourceView(tex2D, srvDesc, hDescriptor);

                // Next descriptor.
                hDescriptor += CbvSrvUavDescriptorSize;
            }

            srvDesc.Dimension   = ShaderResourceViewDimension.TextureCube;
            srvDesc.TextureCube = new ShaderResourceViewDescription.TextureCubeResource
            {
                MostDetailedMip     = 0,
                MipLevels           = skyTex.Description.MipLevels,
                ResourceMinLODClamp = 0.0f
            };
            srvDesc.Format = skyTex.Description.Format;
            Device.CreateShaderResourceView(skyTex, srvDesc, hDescriptor);

            _skyTexHeapIndex     = 3;
            _dynamicTexHeapIndex = _skyTexHeapIndex + 1;

            CpuDescriptorHandle srvCpuStart = _srvDescriptorHeap.CPUDescriptorHandleForHeapStart;
            GpuDescriptorHandle srvGpuStart = _srvDescriptorHeap.GPUDescriptorHandleForHeapStart;
            CpuDescriptorHandle rtvCpuStart = RtvHeap.CPUDescriptorHandleForHeapStart;

            // Cubemap RTV goes after the swap chain descriptors.
            const int rtvOffset = SwapChainBufferCount;

            var cubeRtvHandles = new CpuDescriptorHandle[6];

            for (int i = 0; i < 6; i++)
            {
                cubeRtvHandles[i] = rtvCpuStart + (rtvOffset + i) * RtvDescriptorSize;
            }

            // Dynamic cubemap SRV is after the sky SRV.
            _dynamicCubeMap.BuildDescriptors(
                srvCpuStart + _dynamicTexHeapIndex * CbvSrvUavDescriptorSize,
                srvGpuStart + _dynamicTexHeapIndex * CbvSrvUavDescriptorSize,
                cubeRtvHandles);
        }
        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);
        }