Search code examples
c++directx-11hlsl

Does ID3D11DeviceContext::DrawIndexed method wait for vertex and pixel shader opertions to be completed?


I am using vertex and pixel shaders to perform basic operations such as matrix multiplication in vertex shaders and Texture sampling using pixel shader. I was wondering in the below statements , does the last statement wait till the vertex and pixel shader operations are performed.

deviceContext->VSSetShader(VS, NULL, 0);
deviceContext->PSSetShader(PS, NULL, 0);
deviceContext->PSSetSamplers(0, 1, &Sampler);
deviceContext->DrawIndexed(IndexCount, 0, 0); // Does it wait for pixel operations to be performed on all pixels?

Solution

  • In D3D, work is executed on the GPU in the order it is submitted, but in general it is not synchronized with the CPU. In your code for example, by the time DrawIndexed returns on the CPU, the GPU will very likely not have even begun rendering yet. Often tens of thousands of commands are "in flight" at once. In D3D11, you usually don't need to be concerned with this behavior though, and in fact it's intentionally difficult to introduce race conditions in the API. For example, when you call Present, the GPU will automatically flush all rendering, wait for all calls affecting the swap chain to complete, and present the correct image to the window.

    In summary - no, D3D does not wait for the vertex/pixel shader operations to complete before returning from Draw calls, but usually you can ignore this fact.