0

In my Direct3D 10.1 application, I'm trying to mix D2D and D3D drawing code. While the drawing code works fine, the app invariably crashes (in module d3d11.dll, with code c0000005) after an indeterminate amount of time (minutes, not hours). I've posted the relevant sections of code below, please ask if you want to see any other bits and I can add them to this question.

DeviceCreationFlags

D3D10.DeviceCreationFlags deviceflags = D3D10.DeviceCreationFlags.BgraSupport; // Doesn't work without this flag

Device creation

SlimDX.Direct3D10_1.Device1.CreateWithSwapChain( null, D3D10.DriverType.Hardware, deviceflags, SlimDX.Direct3D10_1.FeatureLevel.Level_10_1, swapchaindesc, out device, out swapchain );

Direct2D initialization

        d2dFactory = new SlimDX.Direct2D.Factory(FactoryType.SingleThreaded);
        using (var backBuffer = SlimDX.Direct3D10.Resource.FromSwapChain<SlimDX.Direct3D10.Texture2D>(swapchain, 0))
        {
            d2dSurf = backBuffer.AsSurface();
            System.Drawing.SizeF dpi = d2dFactory.DesktopDpi;
            d2dRt = SlimDX.Direct2D.RenderTarget.FromDXGI(d2dFactory, d2dSurf,
                new SlimDX.Direct2D.RenderTargetProperties()
                {
                    Usage = SlimDX.Direct2D.RenderTargetUsage.None,
                    Type = SlimDX.Direct2D.RenderTargetType.Default,
                    PixelFormat = new SlimDX.Direct2D.PixelFormat(SlimDX.DXGI.Format.Unknown, SlimDX.Direct2D.AlphaMode.Premultiplied),
                });
            color = new SlimDX.Color4(Color.White);
            brush = new SlimDX.Direct2D.SolidColorBrush(d2dRt, color);
        }

Drawing code

    private void Draw2D()
    {
        d2dRt.BeginDraw();

        // Drawing code here

        var hr = d2dRt.EndDraw();

        if (hr != SlimDX.Direct2D.ResultCode.Success)
            Console.WriteLine("EndDraw result: {0}", hr);
    }

Thanks in advance!

Ani
  • 10,826
  • 3
  • 27
  • 46
  • Are you using the same D3DDevice for your Direct3D renderings ? Also, you are using one thread for your D2D and D3D renderings, right ? You may check these guidances (especially the remark section) - [Direct3D 10.1 Device Sharing with Direct2D](https://msdn.microsoft.com/en-us/library/windows/desktop/ee913554(v=vs.85).aspx#direct3d_10.1_device_sharing_with_direct2d) – Peter Kostov Mar 10 '15 at 06:44
  • Yes, I'm using the same device. I am following all the guidelines from that article. The code I posted above was written based on the snippets from that very article. Note that the code I have works and renders fine, but crashes after rendering thousands of frames. – Ani Mar 11 '15 at 05:25
  • Yes, this led me to a possible thread concurrency problem. You didn't answer are you doing your D2D and D3D renderings from one thread, because if you are using two threads (one for D2D renderings and second for D3D ones), most probably you will need to protect/lock the loops (most precise the resources they use). Anyway, try to minimize your code. Eg, turn off the D2D/D3D rendering code and see if the exception will be raised again. – Peter Kostov Mar 11 '15 at 06:16
  • Sorry, here's a more elaborate answer. Yes, all my rendering happens on a single thread. When I turn off either rendering, it all works fine. It's only when both are rendering that it crashes. – Ani Mar 12 '15 at 23:28

0 Answers0