//-----------------------------------------------------------------------------
/// Verify, align, and store the new profiler results.
/// \param pQueue The Queue used to collect the results to verify.
/// \param results The vector of profiler results to verify.
/// \param pTimestampPair A pair of calibration timestamps used to align CPU and GPU timestamps.
/// \param threadID The ThreadId that the results are collected with.
/// \param frameStartTime The start time of the frame as collected on the CPU.
//-----------------------------------------------------------------------------
void VktFrameProfilerLayer::VerifyAlignAndStoreResults(
    VktWrappedQueue*             pQueue,
    std::vector<ProfilerResult>& results,
    CalibrationTimestampPair*    pTimestampPair,
    UINT32                       threadID,
    GPS_TIMESTAMP                frameStartTime)
{
#if MANUAL_TIMESTAMP_CALIBRATION == 0
    UNREFERENCED_PARAMETER(pTimestampPair);
    UNREFERENCED_PARAMETER(frameStartTime);
#endif

    SampleIdToProfilerResultMap* pResultMap = FindOrCreateProfilerResultsMap(pQueue, threadID);
    PsAssert(pResultMap != nullptr);

    if (pResultMap != nullptr)
    {
        for (size_t resultIndex = 0; resultIndex < results.size(); ++resultIndex)
        {
            ProfilerResult& currentResult = results[resultIndex];

            const UINT64 sampleId = currentResult.measurementInfo.idInfo.sampleId;

            // Verify that the timestamps retrieved from the profiler appear to be valid.
            if (ValidateProfilerResult(currentResult) == true)
            {
#if MANUAL_TIMESTAMP_CALIBRATION
                // Now attempt to align the profiled GPU timestamps with the traced API calls on the CPU.
                bool bAlignedSuccessfully = AlignProfilerResultWithCPUTimeline(currentResult, pTimestampPair, frameStartTime);
#else
                bool bAlignedSuccessfully = true;
#endif

                // @TODO - determine if this is what we want to do
                // Make zero-duration case equal to 1 clock cycle
                if (currentResult.timestampResult.rawClocks.start == currentResult.timestampResult.rawClocks.end)
                {
                    currentResult.timestampResult.rawClocks.end++;
                }

                if (bAlignedSuccessfully)
                {
                    // Store the final adjusted profiler results if they're valid.
                    ProfilerResult* pNewResult = new ProfilerResult;
                    CopyProfilerResult(pNewResult, &currentResult);
                    (*pResultMap)[sampleId] = pNewResult;
                }
                else
                {
                    Log(logERROR, "Command with SampleId %d failed to align with CPU timeline.\n", sampleId);
                }
            }
        }
    }
}
//-----------------------------------------------------------------------------
/// Verify, align, and store the new profiler results.
/// \param pQueue The Queue used to collect the results to verify.
/// \param results The vector of profiler results to verify.
/// \param pTimestampPair A pair of calibration timestamps used to align CPU and GPU timestamps.
/// \param threadID The ThreadId that the results are collected with.
/// \param frameStartTime The start time of the frame as collected on the CPU.
//-----------------------------------------------------------------------------
void DX12FrameProfilerLayer::VerifyAlignAndStoreResults(
    Wrapped_ID3D12CommandQueue*  pQueue,
    std::vector<ProfilerResult>& results,
    CalibrationTimestampPair*    pTimestampPair,
    UINT32                       threadID,
    GPS_TIMESTAMP                frameStartTime)
{
    SampleIdToProfilerResultMap* pResultMap = FindOrCreateProfilerResultsMap(pQueue, threadID);
    PsAssert(pResultMap != nullptr);

    if (pResultMap != nullptr)
    {
        for (size_t resultIndex = 0; resultIndex < results.size(); ++resultIndex)
        {
            ProfilerResult& currentResult = results[resultIndex];

            const UINT64 sampleId = currentResult.measurementInfo.idInfo.mSampleId;

            // Verify that the timestamps retrieved from the profiler appear to be valid.
            if (ValidateProfilerResult(currentResult) == true)
            {
                // Now attempt to align the profiled GPU timestamps with the traced API calls on the CPU.
                bool bAlignedSuccessfully = AlignProfilerResultWithCPUTimeline(currentResult, pTimestampPair, frameStartTime);

                if (bAlignedSuccessfully)
                {
                    // Store the final adjusted profiler results if they're valid.
                    ProfilerResult* pNewResult = new ProfilerResult;
                    CopyProfilerResult(pNewResult, &currentResult);
                    (*pResultMap)[sampleId] = pNewResult;
                }
                else
                {
                    Log(logERROR, "Command with SampleId %d failed to align with CPU timeline.\n", sampleId);
                }
            }
        }
    }
}