Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock the d3d10 device during presentation #833

Merged
merged 2 commits into from Dec 30, 2018

Conversation

Projects
None yet
3 participants
@K0bin
Copy link
Contributor

commented Dec 28, 2018

The D3D10Device uses a mutex to avoid accessing the D3D11ImmediateContext from multiple threads at the same time. The new D3D11Swapchain needs to be locked as well because with D3D10 it's not guaranteed that the game only accesses the ImmediateContext from the render thread.

This fixes Just Cause 2 for me. I haven't done that much testing, just 5 minutes of running around an shooting things.

In theory D3D11VkInterop has the same issue because it also accesses the ImmediateContext.
But that is only used with VR and thus D3D11, right?

@doitsujin

This comment has been minimized.

Copy link
Owner

commented Dec 28, 2018

Interesting, although I'm not sure if this is really the best way to fix this since this thread safety issue is not necessarily exclusive to D3D10. Can you test if this patch works as well?

dxvk-sync-cs-thread.patch.txt

I think the whole D3D10 thread safety thing needs some work anyway since currently both the D3D11 context and D3D10 device use a mutex, which should be redundant since the D3D10 code itself doesn't really do anything.

@K0bin K0bin force-pushed the K0bin:d3d10-presentation-fix branch 3 times, most recently from 6ac8749 to 769575f Dec 30, 2018

@K0bin

This comment has been minimized.

Copy link
Contributor Author

commented Dec 30, 2018

I changed the PR so D3D10Device just enables Multithread protection on the contexts ID3D11Multithread.

m_multithread(this, !(pDevice->GetCreationFlags() & D3D10_CREATE_DEVICE_SINGLETHREADED)) {

: m_device(pDevice), m_context(pContext) {
m_context->GetMultithread()->SetMultithreadProtected(!(pDevice->GetCreationFlags() & D3D10_CREATE_DEVICE_SINGLETHREADED));

This comment has been minimized.

Copy link
@doitsujin

doitsujin Dec 30, 2018

Owner

This will enable multithreaded protection and thus cause a ~20% performance hit for all D3D11 apps as well, since the d3d10 device is always created. The correct fix would be to do this in D3D10CoreCreateDevice.

Show resolved Hide resolved src/d3d11/d3d11_device.cpp
*ppvObject = ref(m_d3d11Device.GetD3D10Multithread());
ID3D11DeviceContext* context;
m_d3d11Device.GetImmediateContext(&context);
*ppvObject = ref(static_cast<D3D11ImmediateContext*>(context)->GetMultithread());
return S_OK;

This comment has been minimized.

Copy link
@doitsujin

doitsujin Dec 30, 2018

Owner

It would be better to replace these two lines by return context->QueryInterface(riid, ppvObject);


D3D10Multithread* GetMultithread() {
return &m_multithread;
}

This comment has been minimized.

Copy link
@doitsujin

doitsujin Dec 30, 2018

Owner

This shouldn't be necessary when using QueryInterface as mentioned in the previous comment.

@K0bin K0bin force-pushed the K0bin:d3d10-presentation-fix branch from 769575f to 0bc16e7 Dec 30, 2018

Com<ID3D10Multithread> multithread;
if (FAILED(d3d11Device->QueryInterface(__uuidof(ID3D10Multithread), reinterpret_cast<void**>(&multithread)))) {
return E_FAIL;
}

This comment has been minimized.

Copy link
@doitsujin

doitsujin Dec 30, 2018

Owner

nit: for consistency, please don't use { ... } around a single line of code.

@K0bin K0bin force-pushed the K0bin:d3d10-presentation-fix branch from 0bc16e7 to 762d723 Dec 30, 2018

Show resolved Hide resolved src/d3d10/d3d10_main.cpp Outdated

K0bin added some commits Dec 30, 2018

[d3d11] Lock in SynchronizeCsThread
This prevents fixes threading issues with D3D10 games when
Present() gets called.

Fixes #567.

@K0bin K0bin force-pushed the K0bin:d3d10-presentation-fix branch from 762d723 to f30dafa Dec 30, 2018

@doitsujin doitsujin merged commit 1750b14 into doitsujin:master Dec 30, 2018

@mozo78

This comment has been minimized.

Copy link

commented Dec 30, 2018

Did you succeed and will JC2 run without problems?

@K0bin

This comment has been minimized.

Copy link
Contributor Author

commented Dec 30, 2018

See my comment in #567.

Before those changes the game would always deadlock on the splash screen for me. That's fixed now.

@K0bin K0bin deleted the K0bin:d3d10-presentation-fix branch Dec 30, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.