CPU什么时候等待GPU?

5 hardware graphics

在受 GPU 限制的应用程序中,我想知道 CPU 将在什么时候等待 GPU 完成渲染。DirectX 和 OpenGL 有区别吗?

运行类似于下面的示例,显然 CPU 不会失控 - 并且在任务管理器中查看,CPU 使用率(如果是单核机器)将低于 100%。

While (running){

    Clear ();

    SetBuffers (); // Vertex / Index

    SetTexture ();

    DrawPrimitives ();

    Present ();
}
Run Code Online (Sandbox Code Playgroud)

gog*_*ger 5

The quick summary is that you will probably see the wait in Present(), but it really depends on what it is the Present() call.

Generally, unless you specifically say you want notice of when the GPU is finished, you might end up waiting at the (random to you) point the driver's input buffer fills up. Think of the GPU driver & card as a very long pipeline. You can put in work at one end and eventually after a while it comes out to the display. You might be able to put in several frames worth of commands into the pipeline before it fills up. The card could be taking a lot of time drawing primitives, but you might see the CPU waiting at a point several frames later.

If your Present() call contains the equivalent of glFinish(), that entire pipeline must drain before that call can return. So, the CPU will wait there.

I hope the following can be helpful:

Clear ();
Run Code Online (Sandbox Code Playgroud)

Causes all the pixels in the current buffer to change color, so the GPU is doing work. Lookup your GPU's clear pix/sec rate to see what time this should be taking.

SetBuffers (); 
SetTexture ();
Run Code Online (Sandbox Code Playgroud)

The driver may do some work here, but generally it wants to wait until you actually do drawing to use this new data. In any event, the GPU doesn't do much here.

DrawPrimitives ();
Run Code Online (Sandbox Code Playgroud)

现在GPU 应该在这里完成大部分工作。根据图元大小,您将受到顶点/秒或像素/秒的限制。也许您有一个昂贵的着色器,您将受到每秒着色器指令的限制。

但是,您可能不会将此视为 CPU 等待的地方。驱动程序可能会为您缓冲命令,并且 CPU 可能能够继续运行。

Present ();
Run Code Online (Sandbox Code Playgroud)

此时,GPU 的工作量很小。它只是更改指针以从不同的缓冲区开始显示。

然而,这可能是 CPU等待GPU 的时间点。根据您的 API,“Present()”可能包含类似 glFlush() 或 glFinish() 的内容。如果是这样,那么您可能会在这里等待。