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

Phi*_*erg 5 c++ directx time monitor

我正在使用visual studio 2012,想知道high_resolution_clock的准确性.

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

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

编辑这是我的计时代码.这个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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Chu*_*urn 0

不幸的是,在 VS 2012 和 2013 中chrono::high_resolution_clock没有使用 RDTSC。这在 VS 的未来版本中已修复。请参阅VS 连接。同时,请改用 QPC。