gpt4 book ai didi

c - 防止 OpenGL 缓冲帧

转载 作者:IT王子 更新时间:2023-10-29 00:37:07 28 4
gpt4 key购买 nike

我正在编写一个程序,它需要极低的延迟纹理到屏幕流(低于 10 毫秒),我已经使用 GL_ARB_buffer_storage 实现了这一点,它非常适合流式传输,而 vsync 可以防止撕裂。

但是我发现 NVidia 管道在阻塞之前调用交换缓冲区时会缓冲 2 到 8 帧,我需要防止这种情况发生。

我所做的如下:

uint64_t detectPresentTime()
{
// warm up first as the GPU driver may have multiple buffers
for(int i = 0; i < 10; ++i)
glxSwapBuffers(state.renderer);

// time 10 iterations and compute the average
const uint64_t start = microtime();
for(int i = 0; i < 10; ++i)
glxSwapBuffers(state.renderer);
const uint64_t t = (microtime() - start) / 10;

// ensure all buffers are flushed
glFinish();

DEBUG_INFO("detected: %lu (%f Hz)", t, 1000000.0f / t);
return t;
}

然后在绘图线程中执行以下操作:

uint64_t presentTime = detectPresentTime();
if (presentTime > 1000)
presentTime -= 1000;

while(running)
{
const uint64_t start = microtime();
glClear();

// copy the texture to the screen

glxSwapBuffers();

const uint64_t delta = microtime() - start;
if (delta < presentTime)
{
glFlush();
usleep(delta);
glFinish();
}
}

此解决方案在 NVidia 硬件上运行良好,但据报道无法在 AMD GPU 上计算正确的当前时间。

有更好的方法吗?我知道 glFinish 通常不应在应用程序中使用,除了分析,但我找不到另一种方法来确保 GPU 管道不缓冲帧。

编辑:对于那些感兴趣的人,这有效地模拟了 Linux 下的 FastSync,但没有禁用 vsync。

Edit2:也许当前时间函数的实现应该有点不同:

uint64_t detectPresentTime()
{
glFinish();

// time 10 iterations and compute the average
const uint64_t start = microtime();
for(int i = 0; i < 10; ++i)
{
glxSwapBuffers(state.renderer);
glFinish();
}
const uint64_t t = (microtime() - start) / 10;

DEBUG_INFO("detected: %lu (%f Hz)", t, 1000000.0f / t);
return t;
}

最佳答案

我找到了答案,有一个鲜为人知的 OpenGL 扩展名为 SGI_video_sync,使用它可以等待下一帧。

即:

glFlush();
uint remainder;
glXWaitVideoSyncSGI(1, 0, &remainder);

关于c - 防止 OpenGL 缓冲帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47686196/

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