I'm building a DirectX Windows Desktop application to present stereoscopic images via two monitors. Currently, I have one swap chain per window (monitor). When updating the windows, I use a simple loop and call Present
on the swap chains sequentially, which obviously results in them not updating simultaneously.
However, when I tried creating 2 separate threads and executing Present
on both of the swap chains concurrently, the majority of the time a deadlock occurs, which I suspect is because of 2 threads being created and joined each frame, which may be too fast to do so.
Is there any alternative means of updating the contents of the swapchains at once?
This is my rendering code, which gets called once per frame:
void Game::Render(DrawFunction func)
{
for (int i = 0; i < NUMBER_OF_WINDOWS; i++)
{
Clear(i);
m_deviceResources->PIXBeginEvent(L"Render");
auto context = m_deviceResources->GetD3DDeviceContext();
m_spriteBatch->Begin();
func(i, m_spriteBatch.get());
m_spriteBatch->End();
m_deviceResources->PIXEndEvent();
auto renderTarget = m_deviceResources->GetRenderTargetView(i);
context->OMSetRenderTargets(1, &renderTarget, nullptr);
m_toneMap[i]->Process(context);
ID3D11ShaderResourceView* nullsrv[] = { nullptr };
context->PSSetShaderResources(0, 1, nullsrv);
}
m_deviceResources->ThreadPresent();
for (int i = 0; i < NUMBER_OF_WINDOWS; i++)
{
m_deviceResources->CleanFrame(i);
}
}
void DX::DeviceResources::ThreadPresent()
{
std::thread one([&]() { m_swapChain[0]->Present(1, 0); });
std::thread two([&]() { m_swapChain[1]->Present(1, 0); });
one.join();
two.join();
}