Advertisement

Crash in SwapChain.Present when running video recording tool

Started by July 26, 2018 07:30 PM
3 comments, last by GalacticCrew 6 years, 6 months ago

Some people reported they can't play my game, while a video recording tool is running at the same time. So, I bought a popular one to test it and as it turns out they are right. ? I get a System.AccessViolationException with HResult=0x80004003 when calling SwapChain.Present. I am developing with DirectX 11 using C# and SharpDX. This problem only happens when a video recording tool is running. After I close it, the runs perfectly fine!

I searched online for this problem, but I did not find a solution. I also read the MSDN page of SwapChain.Present to search for possible error sources. However, according to this page the SwapChain.Present does not throw System.AccessViolationException error messages. So, I assume the problem comes from somewhere else. I tested this problem also with my second game that I am currently developing which is a 2D game that only uses basis DirectX 11 3D stuff and I get the same problem.

I tried to search all the parameters of all the render targets, viewport, swap chain, depth stencil views, etc. to search for something that might conflict with other applications (e.g. forcing my game to use some resource exclusively that might be also used by the video recording tool).

To locate the exact problem, I removed all code from the render loop, except for the SwapChain.Present call. So it was just this single line of code and it still crashed...

Does anyone of you have had a similar problem?

Firstly, 0x80004003  is E_POINTER which might just mean there's a null pointer been passed to a COM function somewhere, and something has translated it to an exception.

Finding out what's gone wrong could be tricky though. The way most video recording tools work is that they hook the D3D API so a call to Present() with them loaded will not just call the standard function - it will also do some extra stuff to capture a video frame. It sounds like your program exposes some sort of bug in the video recording program - they should be completely transparent to the game programmer.

Also note that inside D3D a lot of the things you ask the API to do are buffered up, and delayed until the Present() call. This means that Present() can report problems which are actually caused by earlier API calls.

You're probably doing something which most other games don't do. Off the top of my head I'd suggest:

- Enable D3D11_CREATE_DEVICE_DEBUG (or rather the SharpDX equivalent of that) and make sure the debug runtimes aren't reporting any issues with the way you're using the API. Note that if you're using Windows 10 you'll need to install the optional "Graphics Tools"  component for this to work.

- Try some different options for recording the game output. Windows 10 has built in support to record games - just press Win+G. I'm sure you can also find some other programs that are free, or have a free trial (OBS Studio, and FRAPS spring to mind). You may well find that some work and some don't. If that's the case then submitting a bug report for the software that doesn't work might be be easier than trying to work round their bugs.

- Simplify things as much as possible. Try taking the most basic SharpDX example program that you can find, and record the output of that. Gradually add stuff from your code to the example until you find out what breaks it. I'd start with the device creation and setup code.

- The last resort is to do some native code debugging to try to work out what's gone wrong. At the very least the debugger should be able to catch that exception when it's thrown and show you a call stack.

Advertisement

Thanks for your response! I will what you mentioned except for the second option. I do not need this specific tool to record videos, but some YouTuber wanted to create a video about my game and failed. This task is customer support for me ?

So far, I found out that it's the selected video adapter that causes troubles. If I select the crappy Intel HD Graphics chip, it works. But when selecting my GeForce GTX 1070 it crashes. Now, I have to figure out why!

Search for the problem

I found one issue after enabling D3D11_CREATE_DEVICE_DEBUG. When resizing the form, I set the wrong depth stencil view. However, this property is set in every render loop execution. Therefore, this problem did not have any effect on the game. I still solved it.

Then, I created a new project in my Visual Studio solution trying the different layers of my game engine. The most basic one, which creates a form, DirectX 11 device, swap chain, back buffer, etc. worked fine. There was no exception during the render loop. Then, I tried the next layer which adds input device management, basic application state management, asset management and basic user interface. It immediately crashed even with an empty render loop (except for the SwapChain.Present of course!). So I slowly built this layer again step by step in my new solution until I got the exception.

I found the problem here

I finally got th exception after coding this line:


RenderTarget2D = new SharpDX.Direct2D1.RenderTarget(Factory2D, _surface,
                 new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown,                 
                                                              SharpDX.Direct2D1.AlphaMode.Premultiplied)));

My games are based on DirectX11 3D, but I use a Direct 2D.1 Render Target for rendering user interface, custom cursor, etc. As soon as I add this single to my code, I get the exception at SwapChain.Present. Please note, I do not have any kind of content, the render loop is empty except for the SwapChain.Present call. I get the _surface and Factory2D using this code:


Factory2D = new SharpDX.Direct2D1.Factory();

_surface = backBuffer.QueryInterface<SharpDX.DXGI.Surface>();

Do you have any idea what causes this problem?

This topic is closed to new replies.

Advertisement