Particles/Compute.cpp
//-----------------------------------------------------------------------------
// initialize particle positions.
// Only need to do this once.
// on subsequent compute destroy/create, can copy old state from render object
//-----------------------------------------------------------------------------
initialize particle positions.
Only need to do this once.
on subsequent compute destroy/create, can copy old state from render object
initialize particle positions . Only need to do this once . on subsequent compute destroy / create can copy old state from render object
void Compute::InitializeParticles()
{
    assert(m_numParticles != 0);
    std::vector<Render::Particle> positions;
    positions.resize(m_numParticles);
    std::vector<ParticleVelocity> velocities;
    velocities.resize(m_numParticles);
    const float centerSpread = ParticleSpread * 0.750f;
    LoadParticles(
        &positions[0], &velocities[0],
        XMFLOAT3(centerSpread, 0, 0),
        INITIAL_PARTICLE_SPEED,
        ParticleSpread,
        m_numParticles / 2);
    LoadParticles(
        &positions[m_numParticles / 2], &velocities[m_numParticles / 2],
        XMFLOAT3(-centerSpread, 0, 0),
        INITIAL_PARTICLE_SPEED,
        ParticleSpread,
        m_numParticles / 2);
    const UINT dataSize = m_numParticles * sizeof(Render::Particle);
    const D3D12_HEAP_PROPERTIES uploadHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
    const D3D12_RESOURCE_DESC uploadBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(dataSize);
    ComPtr<ID3D12Resource> particleBufferUpload;
    ThrowIfFailed(m_device->CreateCommittedResource(
        &uploadHeapProperties,
        D3D12_HEAP_FLAG_NONE,
        &uploadBufferDesc,
        D3D12_RESOURCE_STATE_GENERIC_READ,
        nullptr,
        IID_PPV_ARGS(&particleBufferUpload)));
    D3D12_SUBRESOURCE_DATA particleData = {};
    particleData.pData = reinterpret_cast<UINT8*>(&positions[0]);
    particleData.RowPitch = dataSize;
    particleData.SlicePitch = particleData.RowPitch;
    ThrowIfFailed(m_commandAllocators[m_bufferIndex]->Reset());
    ThrowIfFailed(m_commandList->Reset(m_commandAllocators[m_bufferIndex].Get(), m_computeState.Get()));
    {
        const CD3DX12_RESOURCE_BARRIER barriers[] =
        {
            CD3DX12_RESOURCE_BARRIER::Transition(m_positionBuffers[0].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST),
            CD3DX12_RESOURCE_BARRIER::Transition(m_positionBuffers[1].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST),
            CD3DX12_RESOURCE_BARRIER::Transition(m_velocityBuffers[0].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST),
            CD3DX12_RESOURCE_BARRIER::Transition(m_velocityBuffers[1].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_DEST)
        };
        m_commandList->ResourceBarrier(_countof(barriers), barriers);
    }
    UpdateSubresources<1>(m_commandList.Get(), m_positionBuffers[0].Get(), particleBufferUpload.Get(), 0, 0, 1, &particleData);
    UpdateSubresources<1>(m_commandList.Get(), m_positionBuffers[1].Get(), particleBufferUpload.Get(), 0, 0, 1, &particleData);
    const UINT64 velocityBufferSize = m_numParticles * sizeof(ParticleVelocity);
    const D3D12_RESOURCE_DESC velocityBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(velocityBufferSize);
    ComPtr<ID3D12Resource> velocityBufferUpload;
    ThrowIfFailed(m_device->CreateCommittedResource(
        &uploadHeapProperties,
        D3D12_HEAP_FLAG_NONE,
        &velocityBufferDesc,
        D3D12_RESOURCE_STATE_GENERIC_READ,
        nullptr,
        IID_PPV_ARGS(&velocityBufferUpload)));
    particleData.pData = reinterpret_cast<UINT8*>(&velocities[0]);
    particleData.RowPitch = velocityBufferSize;
    particleData.SlicePitch = particleData.RowPitch;
    UpdateSubresources<1>(m_commandList.Get(), m_velocityBuffers[0].Get(), velocityBufferUpload.Get(), 0, 0, 1, &particleData);
    UpdateSubresources<1>(m_commandList.Get(), m_velocityBuffers[1].Get(), velocityBufferUpload.Get(), 0, 0, 1, &particleData);
    {
        const CD3DX12_RESOURCE_BARRIER barriers[] =
        {
            CD3DX12_RESOURCE_BARRIER::Transition(m_positionBuffers[0].Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE),
            CD3DX12_RESOURCE_BARRIER::Transition(m_positionBuffers[1].Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE),
            CD3DX12_RESOURCE_BARRIER::Transition(m_velocityBuffers[0].Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS),
            CD3DX12_RESOURCE_BARRIER::Transition(m_velocityBuffers[1].Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)
        };
        m_commandList->ResourceBarrier(_countof(barriers), barriers);
    }
    ThrowIfFailed(m_commandList->Close());
    ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
    m_commandQueue->ExecuteCommandLists(1, ppCommandLists);
    WaitForGpu();
}
void Compute :: InitializeParticles ( ) { assert ( m_numParticles != 0 ) ; std :: vector < Render :: Particle > positions ; positions . resize ( m_numParticles ) ; std :: vector < ParticleVelocity > velocities ; velocities . resize ( m_numParticles ) ; const float centerSpread = ParticleSpread * 0.750f ; LoadParticles ( & positions [ 0 ] , & velocities [ 0 ] , XMFLOAT3 ( centerSpread , 0 , 0 ) , INITIAL_PARTICLE_SPEED , ParticleSpread , m_numParticles / 2 ) ; LoadParticles ( & positions [ m_numParticles / 2 ] , & velocities [ m_numParticles / 2 ] , XMFLOAT3 ( - centerSpread , 0 , 0 ) , INITIAL_PARTICLE_SPEED , ParticleSpread , m_numParticles / 2 ) ; const UINT dataSize = m_numParticles * sizeof ( Render :: Particle ) ; const D3D12_HEAP_PROPERTIES uploadHeapProperties = CD3DX12_HEAP_PROPERTIES ( D3D12_HEAP_TYPE_UPLOAD ) ; const D3D12_RESOURCE_DESC uploadBufferDesc = CD3DX12_RESOURCE_DESC :: Buffer ( dataSize ) ; ComPtr < ID3D12Resource > particleBufferUpload ; ThrowIfFailed ( m_device -> CreateCommittedResource ( & uploadHeapProperties , D3D12_HEAP_FLAG_NONE , & uploadBufferDesc , D3D12_RESOURCE_STATE_GENERIC_READ , nullptr , IID_PPV_ARGS ( & particleBufferUpload ) ) ) ; D3D12_SUBRESOURCE_DATA particleData = { } ; particleData . pData = reinterpret_cast < UINT8 * > ( & positions [ 0 ] ) ; particleData . RowPitch = dataSize ; particleData . SlicePitch = particleData . RowPitch ; ThrowIfFailed ( m_commandAllocators [ m_bufferIndex ] -> Reset ( ) ) ; ThrowIfFailed ( m_commandList -> Reset ( m_commandAllocators [ m_bufferIndex ] . Get ( ) , m_computeState . Get ( ) ) ) ; { const CD3DX12_RESOURCE_BARRIER barriers [ ] = { CD3DX12_RESOURCE_BARRIER :: Transition ( m_positionBuffers [ 0 ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) , CD3DX12_RESOURCE_BARRIER :: Transition ( m_positionBuffers [ 1 ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) , CD3DX12_RESOURCE_BARRIER :: Transition ( m_velocityBuffers [ 0 ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) , CD3DX12_RESOURCE_BARRIER :: Transition ( m_velocityBuffers [ 1 ] . Get ( ) , D3D12_RESOURCE_STATE_UNORDERED_ACCESS , D3D12_RESOURCE_STATE_COPY_DEST ) } ; m_commandList -> ResourceBarrier ( _countof ( barriers ) , barriers ) ; } UpdateSubresources < 1 > ( m_commandList . Get ( ) , m_positionBuffers [ 0 ] . Get ( ) , particleBufferUpload . Get ( ) , 0 , 0 , 1 , & particleData ) ; UpdateSubresources < 1 > ( m_commandList . Get ( ) , m_positionBuffers [ 1 ] . Get ( ) , particleBufferUpload . Get ( ) , 0 , 0 , 1 , & particleData ) ; const UINT64 velocityBufferSize = m_numParticles * sizeof ( ParticleVelocity ) ; const D3D12_RESOURCE_DESC velocityBufferDesc = CD3DX12_RESOURCE_DESC :: Buffer ( velocityBufferSize ) ; ComPtr < ID3D12Resource > velocityBufferUpload ; ThrowIfFailed ( m_device -> CreateCommittedResource ( & uploadHeapProperties , D3D12_HEAP_FLAG_NONE , & velocityBufferDesc , D3D12_RESOURCE_STATE_GENERIC_READ , nullptr , IID_PPV_ARGS ( & velocityBufferUpload ) ) ) ; particleData . pData = reinterpret_cast < UINT8 * > ( & velocities [ 0 ] ) ; particleData . RowPitch = velocityBufferSize ; particleData . SlicePitch = particleData . RowPitch ; UpdateSubresources < 1 > ( m_commandList . Get ( ) , m_velocityBuffers [ 0 ] . Get ( ) , velocityBufferUpload . Get ( ) , 0 , 0 , 1 , & particleData ) ; UpdateSubresources < 1 > ( m_commandList . Get ( ) , m_velocityBuffers [ 1 ] . Get ( ) , velocityBufferUpload . Get ( ) , 0 , 0 , 1 , & particleData ) ; { const CD3DX12_RESOURCE_BARRIER barriers [ ] = { CD3DX12_RESOURCE_BARRIER :: Transition ( m_positionBuffers [ 0 ] . Get ( ) , D3D12_RESOURCE_STATE_COPY_DEST , D3D12_RESOURCE_STATE_COPY_SOURCE ) , CD3DX12_RESOURCE_BARRIER :: Transition ( m_positionBuffers [ 1 ] . Get ( ) , D3D12_RESOURCE_STATE_COPY_DEST , D3D12_RESOURCE_STATE_COPY_SOURCE ) , CD3DX12_RESOURCE_BARRIER :: Transition ( m_velocityBuffers [ 0 ] . Get ( ) , D3D12_RESOURCE_STATE_COPY_DEST , D3D12_RESOURCE_STATE_UNORDERED_ACCESS ) , CD3DX12_RESOURCE_BARRIER :: Transition ( m_velocityBuffers [ 1 ] . Get ( ) , D3D12_RESOURCE_STATE_COPY_DEST , D3D12_RESOURCE_STATE_UNORDERED_ACCESS ) } ; m_commandList -> ResourceBarrier ( _countof ( barriers ) , barriers ) ; } ThrowIfFailed ( m_commandList -> Close ( ) ) ; ID3D12CommandList * ppCommandLists [ ] = { m_commandList . Get ( ) } ; m_commandQueue -> ExecuteCommandLists ( 1 , ppCommandLists ) ; WaitForGpu ( ) ; }
initialize particle positions.
initialize particle positions .
// Initialize the data in the buffers. // Split the particles into two groups. //------------------------------------------------------------------------- // upload positions //------------------------------------------------------------------------- //------------------------------------------------------------------------- // upload velocities //-------------------------------------------------------------------------