Particles/Compute.cpp
//-----------------------------------------------------------------------------
// when we create a compute device for async compute, we compute directly into
// the buffers used for rendering and abandon our reference to the shared resources.
// if a new compute device is created that is not async compute, we need to
// copy the current particle positions into the old shared buffers before we
// copy the data into the new compute object.
//-----------------------------------------------------------------------------
when we create a compute device for async compute, we compute directly into
the buffers used for rendering and abandon our reference to the shared resources.
if a new compute device is created that is not async compute, we need to
copy the current particle positions into the old shared buffers before we
copy the data into the new compute object.
when we create a compute device for async compute we compute directly into the buffers used for rendering and abandon our reference to the shared resources . if a new compute device is created that is not async compute we need to copy the current particle positions into the old shared buffers before we copy the data into the new compute object .
void Compute::ResetFromAsyncHelper()
{
    if (m_positionBuffers[0] == m_sharedComputeBuffersReference[0])
    {
        return;
    }
    ThrowIfFailed(m_commandAllocators[m_bufferIndex]->Reset());
    ThrowIfFailed(m_commandList->Reset(m_commandAllocators[m_bufferIndex].Get(), m_computeState.Get()));
    for (UINT i = 0; i < m_NUM_BUFFERS; i++)
    {
        auto src = m_positionBuffers[i].Get();
        auto dst = m_sharedComputeBuffersReference[i].Get();
        m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(src, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE));
        m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(dst, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST));
        m_commandList->CopyBufferRegion(
            dst, 0,
            src, 0,
            m_numParticles * sizeof(Render::Particle));
        m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(src, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS));
        m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(dst, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS));
    }
    ThrowIfFailed(m_commandList->Close());
    ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
    m_commandQueue->ExecuteCommandLists(1, ppCommandLists);
    WaitForGpu();
    SetAsync(m_sharedRenderFence, m_sharedComputeBuffersReference, m_bufferIndex);
}
void Compute :: ResetFromAsyncHelper ( ) { if ( m_positionBuffers [ 0 ] == m_sharedComputeBuffersReference [ 0 ] ) { return ; } ThrowIfFailed ( m_commandAllocators [ m_bufferIndex ] -> Reset ( ) ) ; ThrowIfFailed ( m_commandList -> Reset ( m_commandAllocators [ m_bufferIndex ] . Get ( ) , m_computeState . Get ( ) ) ) ; for ( UINT i = 0 ; i < m_NUM_BUFFERS ; i ++ ) { auto src = m_positionBuffers [ i ] . Get ( ) ; auto dst = m_sharedComputeBuffersReference [ i ] . Get ( ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( src , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_SOURCE ) ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( dst , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) ) ; m_commandList -> CopyBufferRegion ( dst , 0 , src , 0 , m_numParticles * sizeof ( Render :: Particle ) ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( src , D3D12_RESOURCE_STATE_COPY_SOURCE , D3D12_RESOURCE_STATE_UNORDERED_ACCESS ) ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( dst , D3D12_RESOURCE_STATE_COPY_DEST , D3D12_RESOURCE_STATE_UNORDERED_ACCESS ) ) ; } ThrowIfFailed ( m_commandList -> Close ( ) ) ; ID3D12CommandList * ppCommandLists [ ] = { m_commandList . Get ( ) } ; m_commandQueue -> ExecuteCommandLists ( 1 , ppCommandLists ) ; WaitForGpu ( ) ; SetAsync ( m_sharedRenderFence , m_sharedComputeBuffersReference , m_bufferIndex ) ; }
when we create a compute device for async compute, we compute directly into
the buffers used for rendering and abandon our reference to the shared resources.
when we create a compute device for async compute we compute directly into the buffers used for rendering and abandon our reference to the shared resources .
// if the reference copy made for async matches my current reference, // then we are not running in async mode and no copy is necessary. // reset the old references