Particles/Compute.cpp
//-----------------------------------------------------------------------------
// Run the particle simulation using the compute shader.
//-----------------------------------------------------------------------------
Run the particle simulation using the compute shader.
Run the particle simulation using the compute shader .
void Compute::Simulate(int in_numActiveParticles, UINT64 in_sharedFenceValue)
{
    ThrowIfFailed(m_commandQueue->Wait(m_sharedRenderFence.Get(), in_sharedFenceValue-1));
    const UINT oldIndex = m_bufferIndex; 
    const UINT newIndex = 1 - oldIndex;  
    ThrowIfFailed(m_commandAllocators[m_bufferIndex]->Reset());
    ThrowIfFailed(m_commandList->Reset(m_commandAllocators[m_bufferIndex].Get(), m_computeState.Get()));
    m_pTimer->BeginTimer(m_commandList.Get(), static_cast<std::uint32_t>(GpuTimers::Simulate));
    const UINT srcHeapIndex = UavParticlePos0 + oldIndex; 
    ID3D12DescriptorHeap* ppHeaps[] = { m_srvHeap.Get() };
    m_commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
    m_commandList->SetPipelineState(m_computeState.Get());
    m_commandList->SetComputeRootSignature(m_rootSignature.Get());
    m_commandList->SetComputeRootConstantBufferView(ComputeRootCBV, m_constantBufferCS->GetGPUVirtualAddress());
    const CD3DX12_GPU_DESCRIPTOR_HANDLE srcHeapHandle(m_srvHeap->GetGPUDescriptorHandleForHeapStart(), srcHeapIndex, m_srvUavDescriptorSize);
    m_commandList->SetComputeRootDescriptorTable(ComputeRootUAVTable, srcHeapHandle);
    ID3D12Resource* pSharedResource = m_positionBuffers[newIndex].Get();
    m_commandList->Dispatch(static_cast<UINT>(ceil(in_numActiveParticles / float(BLOCK_SIZE))), 1, 1);
    m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::UAV(pSharedResource));
    m_pTimer->EndTimer(m_commandList.Get(), static_cast<std::uint32_t>(GpuTimers::Simulate));
    m_pTimer->ResolveAllTimers(m_commandList.Get());
    ThrowIfFailed(m_commandList->Close());
    ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
    m_commandQueue->ExecuteCommandLists(1, ppCommandLists);
    MoveToNextFrame();
}
void Compute :: Simulate ( int in_numActiveParticles , UINT64 in_sharedFenceValue ) { ThrowIfFailed ( m_commandQueue -> Wait ( m_sharedRenderFence . Get ( ) , in_sharedFenceValue - 1 ) ) ; const UINT oldIndex = m_bufferIndex ; const UINT newIndex = 1 - oldIndex ; ThrowIfFailed ( m_commandAllocators [ m_bufferIndex ] -> Reset ( ) ) ; ThrowIfFailed ( m_commandList -> Reset ( m_commandAllocators [ m_bufferIndex ] . Get ( ) , m_computeState . Get ( ) ) ) ; m_pTimer -> BeginTimer ( m_commandList . Get ( ) , static_cast < std :: uint32_t > ( GpuTimers :: Simulate ) ) ; const UINT srcHeapIndex = UavParticlePos0 + oldIndex ; ID3D12DescriptorHeap * ppHeaps [ ] = { m_srvHeap . Get ( ) } ; m_commandList -> SetDescriptorHeaps ( _countof ( ppHeaps ) , ppHeaps ) ; m_commandList -> SetPipelineState ( m_computeState . Get ( ) ) ; m_commandList -> SetComputeRootSignature ( m_rootSignature . Get ( ) ) ; m_commandList -> SetComputeRootConstantBufferView ( ComputeRootCBV , m_constantBufferCS -> GetGPUVirtualAddress ( ) ) ; const CD3DX12_GPU_DESCRIPTOR_HANDLE srcHeapHandle ( m_srvHeap -> GetGPUDescriptorHandleForHeapStart ( ) , srcHeapIndex , m_srvUavDescriptorSize ) ; m_commandList -> SetComputeRootDescriptorTable ( ComputeRootUAVTable , srcHeapHandle ) ; ID3D12Resource * pSharedResource = m_positionBuffers [ newIndex ] . Get ( ) ; m_commandList -> Dispatch ( static_cast < UINT > ( ceil ( in_numActiveParticles / float ( BLOCK_SIZE ) ) ) , 1 , 1 ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: UAV ( pSharedResource ) ) ; m_pTimer -> EndTimer ( m_commandList . Get ( ) , static_cast < std :: uint32_t > ( GpuTimers :: Simulate ) ) ; m_pTimer -> ResolveAllTimers ( m_commandList . Get ( ) ) ; ThrowIfFailed ( m_commandList -> Close ( ) ) ; ID3D12CommandList * ppCommandLists [ ] = { m_commandList . Get ( ) } ; m_commandQueue -> ExecuteCommandLists ( 1 , ppCommandLists ) ; MoveToNextFrame ( ) ; }
Run the particle simulation using the compute shader.
Run the particle simulation using the compute shader .
// /previous/ copy must complete before overwriting the old state // 0 or 1. Old corresponds to the surface the render device is currently using // 1 or 0. New corresponds to the surface the render device is NOT using // 0 or 1 //------------------------------------------------- // set heap base to point at previous simulation results // note that descriptor heap[2] is a copy of heap[0], so when the base is heap[1] the dest is heap[2]==heap[0] //------------------------------------------------- //------------------------------------------------- // dispatch reads from src and writes to dest //------------------------------------------------- // a resource barrier gives maximum information to the runtime that may help other adapters with cache sync