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.");
        }

2

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).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật