//--------------------------------------------------------------------------------------
void RenderAOFromMesh(ID3D11Device* pd3dDevice, 
                      ID3D11DeviceContext* pd3dImmediateContext,
                      ID3D11RenderTargetView* pBackBufferRTV,
                      SceneMesh *pMesh)
{
    UINT SampleCount = g_MSAADesc[g_MSAACurrentSettings].SampleCount;
    SceneViewInfo ViewInfo;

    pd3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    pd3dImmediateContext->RSSetViewports(1, &g_FullViewport);

    //--------------------------------------------------------------------------------------
    // Clear render target and depth buffer
    //--------------------------------------------------------------------------------------
    float BgColor[4] = { 1.0f, 1.0f, 1.0f };
    pd3dImmediateContext->ClearRenderTargetView(g_ColorRTV, BgColor);
    pd3dImmediateContext->ClearDepthStencilView(g_DepthStencilDSV, D3D11_CLEAR_DEPTH, 1.0, 0);

    //--------------------------------------------------------------------------------------
    // Render color and depth with the Scene3D class
    //--------------------------------------------------------------------------------------
    pd3dImmediateContext->OMSetRenderTargets(1, &g_ColorRTV, g_DepthStencilDSV);

    if (g_UseOrbitalCamera)
    {
        ViewInfo.WorldViewMatrix = (*g_OrbitalCamera.GetWorldMatrix()) * (*g_OrbitalCamera.GetViewMatrix());
        ViewInfo.ProjectionMatrix = *g_OrbitalCamera.GetProjMatrix();
    }
    else
    {
        D3DXMATRIX WorldMatrix;
        D3DXMatrixRotationX(&WorldMatrix, -D3DX_PI * 0.5f);
        ViewInfo.WorldViewMatrix = WorldMatrix * (*g_FirstPersonCamera.GetViewMatrix());
        ViewInfo.ProjectionMatrix = *g_FirstPersonCamera.GetProjMatrix();
    }

    g_pSceneRenderer.OnFrameRender(&ViewInfo, pMesh);

    //--------------------------------------------------------------------------------------
    // Render the SSAO
    //--------------------------------------------------------------------------------------

    GFSDK_SSAO_InputDepthData InputDepthData;
    InputDepthData.pFullResDepthTextureSRV = g_DepthStencilSRV;
    InputDepthData.pProjectionMatrix = (CONST FLOAT*)ViewInfo.ProjectionMatrix;
    InputDepthData.ProjectionMatrixLayout = GFSDK_SSAO_ROW_MAJOR_ORDER;
    InputDepthData.MetersToViewSpaceUnits = pMesh->GetSceneScale();

    GFSDK_SSAO_Status status;
    status = g_AORenderer.RenderAO(pd3dImmediateContext, &InputDepthData, &g_AOParams, g_ColorRTV);
    assert(status == GFSDK_SSAO_OK);

    //--------------------------------------------------------------------------------------
    // Copy/resolve colors to the 1xAA backbuffer
    //--------------------------------------------------------------------------------------

    pd3dImmediateContext->OMSetRenderTargets(1, &pBackBufferRTV, NULL);

    ID3D11Texture2D* pBackBufferTexture;
    pBackBufferRTV->GetResource((ID3D11Resource**)&pBackBufferTexture);

    if (SampleCount > 1)
    {
        pd3dImmediateContext->ResolveSubresource(pBackBufferTexture, 0, g_ColorTexture, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
    }
    else
    {
        pd3dImmediateContext->CopyResource(pBackBufferTexture, g_ColorTexture);
    }

    SAFE_RELEASE(pBackBufferTexture);
}