Particles/Compute.cpp
//-----------------------------------------------------------------------------
// cross-adapter copy from other compute object into this one
//-----------------------------------------------------------------------------
cross-adapter copy from other compute object into this one
cross - adapter copy from other compute object into this one
void Compute::CopyState(Compute* in_pCompute)
{
    in_pCompute->ResetFromAsyncHelper();
    ComPtr<ID3D12Heap> sharedHeap;
    ThrowIfFailed(m_device->OpenSharedHandle(in_pCompute->m_sharedHandles.m_heap, IID_PPV_ARGS(&sharedHeap)));
    const D3D12_RESOURCE_DESC crossAdapterDesc = CD3DX12_RESOURCE_DESC::Buffer(
        in_pCompute->m_sharedHandles.m_alignedDataSize,
        D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER);
    ComPtr<ID3D12Resource> srcBuffer[m_NUM_BUFFERS];
    for (UINT i = 0; i < m_NUM_BUFFERS; i++)
    {
        ThrowIfFailed(m_device->CreatePlacedResource(
            sharedHeap.Get(),
            i * in_pCompute->m_sharedHandles.m_alignedDataSize,
            &crossAdapterDesc,
            D3D12_RESOURCE_STATE_COPY_SOURCE,
            nullptr,
            IID_PPV_ARGS(&srcBuffer[i])));
    }
    {
        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++)
        {
            m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_positionBuffers[i].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST));
            m_commandList->CopyBufferRegion(
                m_positionBuffers[i].Get(), 0,
                srcBuffer[i].Get(), 0,
                m_numParticles * sizeof(Render::Particle));
            m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_positionBuffers[i].Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE));
        }
        ThrowIfFailed(m_commandList->Close());
        ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
        m_commandQueue->ExecuteCommandLists(1, ppCommandLists);
        WaitForGpu();
    }
    {
        in_pCompute->m_commandAllocators[m_bufferIndex]->Reset();
        ThrowIfFailed(in_pCompute->m_commandList->Reset(in_pCompute->m_commandAllocators[m_bufferIndex].Get(), in_pCompute->m_computeState.Get()));
        for (UINT i = 0; i < m_NUM_BUFFERS; i++)
        {
            in_pCompute->m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(in_pCompute->m_positionBuffers[i].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST));
            in_pCompute->m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(in_pCompute->m_velocityBuffers[i].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE));
            in_pCompute->m_commandList->CopyBufferRegion(
                in_pCompute->m_positionBuffers[i].Get(), 0,
                in_pCompute->m_velocityBuffers[i].Get(), 0,
                m_numParticles * sizeof(ParticleVelocity));
        }
        ThrowIfFailed(in_pCompute->m_commandList->Close());
        ID3D12CommandList* ppCommandLists[] = { in_pCompute->m_commandList.Get() };
        in_pCompute->m_commandQueue->ExecuteCommandLists(1, ppCommandLists);
        in_pCompute->WaitForGpu();
    }
    {
        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++)
        {
            m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_velocityBuffers[i].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST));
            m_commandList->CopyBufferRegion(
                m_velocityBuffers[i].Get(), 0,
                srcBuffer[i].Get(), 0,
                m_numParticles * sizeof(ParticleVelocity));
            m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_velocityBuffers[i].Get(), 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();
}
void Compute :: CopyState ( Compute * in_pCompute ) { in_pCompute -> ResetFromAsyncHelper ( ) ; ComPtr < ID3D12Heap > sharedHeap ; ThrowIfFailed ( m_device -> OpenSharedHandle ( in_pCompute -> m_sharedHandles . m_heap , IID_PPV_ARGS ( & sharedHeap ) ) ) ; const D3D12_RESOURCE_DESC crossAdapterDesc = CD3DX12_RESOURCE_DESC :: Buffer ( in_pCompute -> m_sharedHandles . m_alignedDataSize , D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER ) ; ComPtr < ID3D12Resource > srcBuffer [ m_NUM_BUFFERS ] ; for ( UINT i = 0 ; i < m_NUM_BUFFERS ; i ++ ) { ThrowIfFailed ( m_device -> CreatePlacedResource ( sharedHeap . Get ( ) , i * in_pCompute -> m_sharedHandles . m_alignedDataSize , & crossAdapterDesc , D3D12_RESOURCE_STATE_COPY_SOURCE , nullptr , IID_PPV_ARGS ( & srcBuffer [ i ] ) ) ) ; } { 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 ++ ) { m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( m_positionBuffers [ i ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) ) ; m_commandList -> CopyBufferRegion ( m_positionBuffers [ i ] . Get ( ) , 0 , srcBuffer [ i ] . Get ( ) , 0 , m_numParticles * sizeof ( Render :: Particle ) ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( m_positionBuffers [ i ] . Get ( ) , D3D12_RESOURCE_STATE_COPY_DEST , D3D12_RESOURCE_STATE_COPY_SOURCE ) ) ; } ThrowIfFailed ( m_commandList -> Close ( ) ) ; ID3D12CommandList * ppCommandLists [ ] = { m_commandList . Get ( ) } ; m_commandQueue -> ExecuteCommandLists ( 1 , ppCommandLists ) ; WaitForGpu ( ) ; } { in_pCompute -> m_commandAllocators [ m_bufferIndex ] -> Reset ( ) ; ThrowIfFailed ( in_pCompute -> m_commandList -> Reset ( in_pCompute -> m_commandAllocators [ m_bufferIndex ] . Get ( ) , in_pCompute -> m_computeState . Get ( ) ) ) ; for ( UINT i = 0 ; i < m_NUM_BUFFERS ; i ++ ) { in_pCompute -> m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( in_pCompute -> m_positionBuffers [ i ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) ) ; in_pCompute -> m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( in_pCompute -> m_velocityBuffers [ i ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_SOURCE ) ) ; in_pCompute -> m_commandList -> CopyBufferRegion ( in_pCompute -> m_positionBuffers [ i ] . Get ( ) , 0 , in_pCompute -> m_velocityBuffers [ i ] . Get ( ) , 0 , m_numParticles * sizeof ( ParticleVelocity ) ) ; } ThrowIfFailed ( in_pCompute -> m_commandList -> Close ( ) ) ; ID3D12CommandList * ppCommandLists [ ] = { in_pCompute -> m_commandList . Get ( ) } ; in_pCompute -> m_commandQueue -> ExecuteCommandLists ( 1 , ppCommandLists ) ; in_pCompute -> WaitForGpu ( ) ; } { 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 ++ ) { m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( m_velocityBuffers [ i ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) ) ; m_commandList -> CopyBufferRegion ( m_velocityBuffers [ i ] . Get ( ) , 0 , srcBuffer [ i ] . Get ( ) , 0 , m_numParticles * sizeof ( ParticleVelocity ) ) ; m_commandList -> ResourceBarrier ( 1 , & CD3DX12_RESOURCE_BARRIER :: Transition ( m_velocityBuffers [ i ] . Get ( ) , 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 ( ) ; }
cross-adapter copy from other compute object into this one
cross - adapter copy from other compute object into this one
//--------------------------------------------------------------- // open shared buffers //--------------------------------------------------------------- //--------------------------------------------------------------- // copy the position data from the other compute device //--------------------------------------------------------------- //--------------------------------------------------------------- // within the other adapter, copy the velocity buffers into the shared position buffers // WARNING: the size of the velocity data better be <= the size of the position data //--------------------------------------------------------------- //--------------------------------------------------------------- // now copy the velocity data from the other compute device //---------------------------------------------------------------