d3dcoder / d3d12book

Sample code for the book "Introduction to 3D Game Programming with DirectX 12"
1.47k stars 571 forks source link

GpuWaves.cpp Resource Barrier run time warning. #21

Open HuazyYang opened 4 years ago

HuazyYang commented 4 years ago

when i am debugging the GpuWaves from chapter 13: Compute Shader, there are may warnings about resource state swtiches, I think there are problems in GpuWaves::Update and GpuWaves::Disturb when setting up barrier to indicate GPU to asynchronize the resource between state: "unordered access resources" to "shader resources", please check my rectified code pieces for an example: void GpuWaves::Update( FLOAT fTime, FLOAT fElapsedTime, ID3D12GraphicsCommandList pd3dCommandList, ID3D12RootSignature pRootSignature, ID3D12PipelineState *pso ) { static FLOAT t_base;

t_base += fTime;

if (t_base >= m_fTimeStep) {

    /// Reset the time step.
    t_base = 0.0f;

    // Only update the simulation at the specified time step.

    pd3dCommandList->SetPipelineState(pso);
    pd3dCommandList->SetComputeRootSignature(pRootSignature);

    /// Synchronize the resources.

    // Set the update constants.
    pd3dCommandList->SetComputeRoot32BitConstants(0, 3, m_fK, 0);

    pd3dCommandList->SetComputeRootDescriptorTable(1, m_hPrevSolSrv);
    pd3dCommandList->SetComputeRootDescriptorTable(2, m_hCurrSolSrv);
    pd3dCommandList->SetComputeRootDescriptorTable(3, m_hNextSolUav);

    pd3dCommandList->Dispatch(m_uNumCols / 16, m_uNumRows / 16, 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.
    //
    D3D12_GPU_DESCRIPTOR_HANDLE hTemp = m_hPrevSolSrv;
    m_hPrevSolSrv = m_hCurrSolSrv;
    m_hCurrSolSrv = m_hNextSolSrv;
    m_hNextSolSrv = hTemp;

    hTemp = m_hPrevSolUav;
    m_hPrevSolUav = m_hCurrSolUav;
    m_hCurrSolUav = m_hNextSolUav;
    m_hNextSolUav = hTemp;

    ID3D12Resource *pTexTemp = m_pPrevSol;
    m_pPrevSol = m_pCurrSol;
    m_pCurrSol = m_pNextSol;
    m_pNextSol = pTexTemp;

    CD3DX12_RESOURCE_BARRIER syncBarrier[] = {
        CD3DX12_RESOURCE_BARRIER::Transition(m_pCurrSol,
            D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_GENERIC_READ),
        CD3DX12_RESOURCE_BARRIER::Transition(m_pNextSol,
            D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)
    };

    // The current solution needs to be able to be read by the vertex shader, so change its state to GENERIC_READ.
    pd3dCommandList->ResourceBarrier(2, syncBarrier);
}

}

void GpuWaves::Disturb( ID3D12GraphicsCommandList pd3dCommandList, ID3D12RootSignature pRootSignature, ID3D12PipelineState *pso, UINT i, UINT j, FLOAT fMagnitude ) {

pd3dCommandList->SetPipelineState(pso);
pd3dCommandList->SetComputeRootSignature(pRootSignature);

UINT uDisturbIndex[] = { i, j };
pd3dCommandList->SetComputeRoot32BitConstants(0, 1, &fMagnitude, 3);
pd3dCommandList->SetComputeRoot32BitConstants(0, 2, uDisturbIndex, 4);
pd3dCommandList->SetComputeRootDescriptorTable(1, m_hCurrSolUav);

pd3dCommandList->ResourceBarrier(1,
    &CD3DX12_RESOURCE_BARRIER::Transition(m_pCurrSol,
        D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_RESOURCE_STATE_UNORDERED_ACCESS));

pd3dCommandList->Dispatch(1, 1, 1);

pd3dCommandList->ResourceBarrier(1,
    &CD3DX12_RESOURCE_BARRIER::Transition(m_pCurrSol,
        D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_GENERIC_READ));

}