gpt4 book ai didi

c++ - SDL + OpenGL,具有周期性尖峰的不规则帧时间

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:08:26 25 4
gpt4 key购买 nike

我当时正在使用 SDL 开发应用程序并且很高兴。我注意到动画中有一些小的停顿。我已经用最少的功能编写了一个单独的项目,我发现即使是非常基本的设置也会出现这个问题。

这是代码:

#include <iostream>
#include <vector>
#include <fstream>
#include <SDL.h>
#include <glad/glad.h>

using namespace std;

// the application will close after this amount of time
const float MILISECONDS_TO_CLOSE = 10 * 1000;

int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

SDL_Window* window =
SDL_CreateWindow(
"tracer",
100, 100,
800, 600,
SDL_WINDOW_OPENGL
);

SDL_GLContext context = SDL_GL_CreateContext(window);

// SDL_GL_SetSwapInterval(1); // this line mitigates the problem but just slightly

if (!gladLoadGL())
{
cout << "gladLoadGL failed" << endl;
}

const GLubyte *oglVersion = glGetString(GL_VERSION);
std::cout << "This system supports OpenGL Version: " << oglVersion << std::endl;
const GLubyte *gpuVendor = glGetString(GL_VENDOR);
std::cout << "Graphics card: " << gpuVendor << std::endl;

glClearColor(0.15f, 0.15f, 0.15f, 1.0f);

vector<unsigned> deltas;
deltas.reserve(10 * MILISECONDS_TO_CLOSE);

static unsigned firstTime, prevTime, curTime;
firstTime = prevTime = curTime = SDL_GetTicks();
while (true)
{
// compute delta time
curTime = SDL_GetTicks();
unsigned dt = curTime - prevTime;
prevTime = curTime;
deltas.push_back(dt);

// close the application after some time
if (curTime - firstTime > MILISECONDS_TO_CLOSE) break;

// handle closing events
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE || event.type == SDL_QUIT) break;
}

glClear(GL_COLOR_BUFFER_BIT);

SDL_GL_SwapWindow(window);
}

// save recorded deltas to a file
fstream f("out.txt", ios::out | ios::trunc);
for (unsigned dt : deltas) f << dt << endl;
f << flush;
f.close();

return 0;
}

程序记录 10 秒内帧之间的时间并将结果保存在文本文件中。

我用 python 绘制了数据我得到了这个:

enter image description here

水平轴以毫秒为单位表示帧时间。垂直轴表示自上一帧以来耗时。同样以毫秒为单位。

如您所见,帧之间的时间非常不规则,并且存在周期性尖峰(尖峰之间大约间隔 1 秒)。

我已将 CMake 项目上传到 github repository , 因此您可以根据需要进行测试。

我已经使用我的集成 GPU 和专用 GPU(Intel 530HD 和 NVIDIA GTX 960M)进行了测试。

SDL 版本为 2.0.5。

我在 Windows 10 和 Ubuntu 16.04 LTS 上测试过它。

编辑:我已将应用程序移植到 GLFW,同样的事情发生了。因此,SDL 中存在错误的可能性很小。我相应地更新了 git 仓库,现在有两个 CMake 项目。

编辑 2:我已经在另一台电脑上测试过它并且工作正常。 enter image description here我不知道发生了什么。会不会是硬件问题?那为什么当我运行其他应用程序时它不会发生?

驱动问题?不太可能,因为它同时使用 Intel 和 NVIDIA GPU。此外,它发生在 Ubuntu 和 Windows 上。

最佳答案

由于 CPU 过热,我有过类似的行为(虽然有更长和更低的稳定期,而不是尖峰),因此决定暂时降低时钟频率。如果您的 PC 出现这种情况,您可以尝试升级内部冷却器或添加外部冷却器。或者只是降低房间空调的温度设置。

当然,这在夏天总是更成为一个问题。

Here's where I've posted about it .

关于c++ - SDL + OpenGL,具有周期性尖峰的不规则帧时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45885534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com