Search code examples
c#uwpdirectxsharpdx

SharpDX UWP Swapchain null reference upon Present


I'm trying to set up a SwapChain in Visual Studio, I've created a SwapChainPanel UI element that I want the swapchain to draw to.

Currently when I run this code, I get a null reference exception when presenting the swapchain. I think the issue is somewhere here with the casting? I'm not very familiar with DirectX however, any help is greatly appreciated!

var swapChainPanel = this._swapChainPanel;
var swapChainPanelNative = ComObject.As<SharpDX.DXGI.ISwapChainPanelNative>(swapChainPanel);
swapChainPanelNative.SwapChain = swapChain;

Relevant code:

Member variables and constructor

        private SwapChainPanel _swapChainPanel;
        private SharpDX.Direct3D11.Device device;
        private SharpDX.Direct3D11.DeviceContext deviceContext;
        private SwapChain swapChain;
        private IDirect3DSurface surface;

        private RenderTargetView renderTargetView;
        private Texture2D renderTargetTexture;
        private Texture2D backBuffer;

        // Media player members
        private MediaPlayer mediaPlayer;
        private MediaPlaybackItem mediaPlaybackItem;
        private FFmpegMediaSource ffmpegMediaSource;
        private SoftwareBitmap frameServerDest = new SoftwareBitmap(BitmapPixelFormat.Rgba8, 512, 512, BitmapAlphaMode.Premultiplied);
        private IDirect3DSurface direct3DSurface;


        public StreamService(SwapChainPanel swapChainPanel)
        {
            _swapChainPanel = swapChainPanel;

            swapChainPanel.Loaded += (sender, args) =>
            {
                // Only create the swap chain after the SwapChainPanel is loaded
                InitializeSwapChain();
                InitializeMediaPlayer();
            };
        }

Swapchain Initialization

        private void InitializeSwapChain()
        {
            // Create Direct3D device and context
            var creationFlags = DeviceCreationFlags.BgraSupport;
            creationFlags |= DeviceCreationFlags.Debug;
            device = new Device(SharpDX.Direct3D.DriverType.Hardware, creationFlags);

            // Descriptions
            SwapChainDescription1 swapChainDesc = new SwapChainDescription1()
            {
                Width = (int)_swapChainPanel.ActualWidth,
                Height = (int)_swapChainPanel.ActualHeight,
                Format = Format.B8G8R8A8_UNorm,
                Stereo = false,
                SampleDescription = new SampleDescription(1, 0),
                Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
                BufferCount = 2,
                SwapEffect = SwapEffect.FlipDiscard,
                Scaling = Scaling.Stretch,
                AlphaMode = AlphaMode.Premultiplied,
                Flags = SwapChainFlags.AllowModeSwitch | SwapChainFlags.AllowTearing
            };

            var textureDesc = new Texture2DDescription
            {
                Width = width,
                Height = height,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.B8G8R8A8_UNorm,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None,
            };

            using (var dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>())
            {
                using (var adapter = dxgiDevice.Adapter)
                {
                    using (var factory = adapter.GetParent<Factory2>())
                    {
                        using (swapChain = new SwapChain1(factory, dxgiDevice, ref swapChainDesc))
                        {
                            var swapChainPanel = this._swapChainPanel;
                            var swapChainPanelNative = ComObject.As<SharpDX.DXGI.ISwapChainPanelNative>(swapChainPanel);
                            swapChainPanelNative.SwapChain = swapChain;

                            // Set render view
                            backBuffer = swapChain.GetBackBuffer<Texture2D>(0);
                            renderTargetView = new RenderTargetView(device, backBuffer);
                            device.ImmediateContext.OutputMerger.SetRenderTargets(renderTargetView);

                            // Set render target texture
                            renderTargetTexture = new Texture2D(device, textureDesc);
                            direct3DSurface = Direct3D11Helper.CreateDirect3DSurfaceFromSharpDXTexture(renderTargetTexture);

                            backBuffer.Dispose();
                        }
                    }
                }
            }
        }

Presenting the swapchain

        public void RenderFrame()
        {
                // Clear the render target
                device.ImmediateContext.ClearRenderTargetView(renderTargetView, new RawColor4(1, 1, 1, 1));

                // Set the render target
                device.ImmediateContext.OutputMerger.SetRenderTargets(renderTargetView);

                // Bind the texture that was updated by MediaPlayer to the pixel shader
                using (var shaderResourceView = new ShaderResourceView(device, renderTargetTexture))
                {
                    device.ImmediateContext.PixelShader.SetShaderResource(0, shaderResourceView);

                    // Render quad
                    device.ImmediateContext.Draw(6, 0);
                }

                // Present the frame
                swapChain.Present(1, PresentFlags.None);
                System.Diagnostics.Debug.WriteLine("Frame presented successfully.");
        }

Solution

  • In your code you create the swapchain like this:

    using swapChain = new SwapChain1(factory, dxgiDevice, ref swapChainDesc);
    {
       // do something
    }
    

    Which means it will get disposed as soon as you get out of the scope. When you call RenderFrame, the swapChain object itself is not null, because it's a .NET wrapper over the native thing, but it's underlying NativePointer is, and you get a NullReferenceException in SharpDX code "below".

    Just keep the DirectX objects you need alive, like:

    swapChain = new SwapChain1(factory, dxgiDevice, ref swapChainDesc);
    

    And you should event store most of these "living" objects (device, swapchain, back buffer, etc.) stored in a member variable (dispose them only when closing the service code). On the contrary, you can generally release intermediate objects like factories as soon as you've finished using them (like what's done in today in your code).