Changing adapter runtime or when to clear resources?

Started by
3 comments, last by Volgogradetzzz 8 years, 10 months ago

Greetings,

In order to better understand internals I want to create a demo application where the user can change adapters, resolutions, monitors runtime. And I immediately encountered a problem. When I create a device second time (after adapter change) I can't create a swap chain. I receive an error:


E_ACCESSDENIED General access denied error.

that makes not a lot of sense. Debug log tells me:


Microsoft C++ exception: _com_error at memory location 0x002DDE84.

This looks not like a d3d error.

After some investigation I found that this error happens if I don't clear resources (vertex buffer, for example). I'm using a ComPtr for storing all d3d interfaces. The funny thing - I'm not using this resource at all! I just created it and forgot (actually, not forgot - I'm clearing it and creating it once more after the swap chain creation). So why I have this error? Who use this resource if not me and why? Why I should clear it exactly before a swap chain creation?

Advertisement

Do you dispose of the device before creating a new one?

You wouldnt be able to just CreateDevice() over the top of your existing pointer as it has existing objects associated with it, e.g. your swap chain etc.

There are ways to adjust the resolution etc at runtime without disposing of the device using DXGI. You need to look into the ResizeBuffers() method of your swap chain, for starters.

This will lead you along the right path, if you get stuck feel free to ask :)

Thank you for the answer.

Yes, I'm disposing it (actually ComPtr disposes it automatically).

Just to clarify - if I'm clearing all resources before creating a new swap chain - everything is ok. But if at the moment of creation a swap chain with there exist at least one "old" resource - the function call fails. I just wonder why - how does CreateSwapChainForHwnd() and resources related? I couldn't find any documentation about this.

I would recommend not disposing it, and instead just resizing it to suit the new window dimensions and resolution etc.

Personally i don't use CreateSwapChainForHwnd(), but according to its documentation when you Release() the pointer (e.g. by letting the CComPtr go out of scope) DX11 won't immediately free the resources and the behaviour is deferred:

Because you can associate only one flip presentation model swap chain at a time with an HWND, the Microsoft Direct3D 11 policy of deferring the destruction of objects can cause problems if you attempt to destroy a flip presentation model swap chain and replace it with another swap chain. For more info about this situation, see Deferred Destruction Issues with Flip Presentation Swap Chains.

This is likely the cause of your problem. If you clear the state of your device with ClearState(), this should fix the issue: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476425%28v=vs.85%29.aspx#Defer_Issues_with_Flip

Alternately, use IDXGIFactory2::CreateSwapChainForCoreWindow instead, which is compatible with windows store apps where CreateSwapChainForHwnd is not.

Yeah, you're right - the problem was because of flip swap effect. Thanks a lot! Didn't understand why, but clearing a context didn't help. So my only option is to clear everything manually before swap chain creation.

This topic is closed to new replies.

Advertisement