您的位置:首页 > 其它

4.4.4 Frame Statistics

2015-10-28 21:24 113 查看
游戏和图形应用程序测量每秒被渲染帧数(FPS)是常见的。要做到这一点,我们简单地在一些指定的时间t计算帧的数量n。然后,平均FPS:fpsavg = n / t。如果我们设置t = 1,那么fpsavg = n / 1 = n。在我们的代码中,我们使用t = 1(第二个),因为它避免了除法,此外,一秒钟给了一个很好的平均-它不太长也不太短。计算FPS的代码是D3DApp:CalculateFrameStats函数:

void D3DApp::CalculateFrameStats()
{
// Code computes the average frames per second, and also the
// average time it takes to render one frame.  These stats
// are appended to the window caption bar.

static int frameCnt = 0;
static float timeElapsed = 0.0f;

frameCnt++;

// Compute averages over one second period.
if( (mTimer.TotalTime() - timeElapsed) >= 1.0f )
{
float fps = (float)frameCnt; // fps = frameCnt / 1
float mspf = 1000.0f / fps;

std::wostringstream outs;
outs.precision(6);
outs << mMainWndCaption << L"    "
<< L"FPS: " << fps << L"    "
<< L"Frame Time: " << mspf << L" (ms)";
SetWindowText(mhMainWnd, outs.str().c_str());

// Reset for next average.
frameCnt = 0;
timeElapsed += 1.0f;
}
}

另外,为了计算FPS,前面的代码也计算需要的毫秒数,平均地:

float mspf = 1000.0f / fps;

秒每帧只是FPS的倒数,我们可以乘以1000 ms / 1s实现从秒到毫秒的转换。

 

 

本文固定链接:http://www.oxox.work/web/directx11/frame-statistics/ | 虚幻大学
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: