std :: chrono :: high_resolution_clock和屏幕刷新率的准确性(不精确)

时间:2014-06-24 11:58:56

标签: c++ time directx monitor

我正在使用visual studio 2012,想了解high_resolution_clock的准确性。

基本上我会编写一些代码来显示声音和图像,但是我需要它们才能很好地同步,并且图像必须是无泪的。我使用directX来提供无泪图像,并使用high_resolution_clock进行计时屏幕刷新。显示器声称为60 fps,然而,使用high_resolution_clock的时序提供60.035 fps的刷新率,平均超过10000次屏幕刷新。根据哪个是正确的,我的音频将在一秒钟后以0.5毫秒结束,一小时后约为2秒。我希望任何时钟都比这更准确 - 更像是一年内漂移,而不是一小时。

以前有没有人看过这种东西。我应该期待我的声卡时钟再次不同吗?

编辑 这是我的计时代码。这个while循环在我的渲染线程中运行。 m_renderData是包含渲染场景所需数据的结构数组,每个屏幕有一个元素。对于测试我只在一个屏幕上运行,因此它只有一个元素

while(!TestDestroy())
{
    for(size_t i=0; i<m_renderData.size(); ++i)
    {
        //work out where in the vsync cycle we are and increment the render cycle
        //as needed until we need to actually render
        D3DRASTER_STATUS rStatus;  
        m_renderData[i].deviceD3D9->GetRasterStatus(0, &rStatus);
        if(m_renderData[i].renderStage==inVBlankRenderingComplete)
        {
            if(!rStatus.InVBlank)
                m_renderData[i].renderStage=notInVBlank;
        }
        else if(m_renderData[i].renderStage==notInVBlank)
        {
            if(rStatus.InVBlank)
                m_renderData[i].renderStage=inVBlankReadyToRender;
        }

        //check for missing the vsync for rendering
        bool timeOut=false;
        if(m_renderData[i].durations.size()>0)
        {
            double timeSinceLastRender=std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-m_renderData[i].durations.back()).count();
            if (timeSinceLastRender>expectedUpdatePeriod*1.2)
                timeOut=true;

        }

        if(m_renderData[i].renderStage==inVBlankReadyToRender || timeOut)
        {
            //We have reached the time to render

            //record the time and increment the number of renders that have been performed
            m_renderData[i].durations.push_back(std::chrono::high_resolution_clock::now());
            ++m_renderData[i].nRenders;

            //we calculate the fps using 10001 times - i.e. an interval of 10000 frames
            size_t fpsUpdatePeriod=10001;
            if(m_renderData[i].nRenders<fpsUpdatePeriod)
            {
                //if we don't have enough times then display a message
                m_renderData[i].fpsString = "FPS: Calculating";
            }
            else
            {
                //we have enough timing info, calculate the fps
                double meanFrameTime = std::chrono::duration_cast<std::chrono::microseconds>(m_renderData[i].durations.back()-*(m_renderData[i].durations.end()-fpsUpdatePeriod)).count()/double(fpsUpdatePeriod-1);
                double fps = 1000000.0/meanFrameTime;
                saveFps(fps);
            }

            //render to the back buffer for this screen
            renderToBackBuffer(i);

            //display the back buffer
            if(!TestDestroy())
                m_renderData[i].deviceD3D9->Present(NULL, NULL, NULL, NULL);
            //make sure we render to the correct back buffer next time
            m_renderData[i].bufferToRender--;
            //update the render cycle
            m_renderData[i].renderStage=inVBlankRenderingComplete;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不幸的是,在VS 2012和2013中,chrono::high_resolution_clock未使用RDTSC。这是针对VS的未来版本修复的。见VS Connect。在此期间,请改用QPC。