MOGRE 1.8.1 + WPF (C#) - Back Buffer is not valid when user changes resolution or computer goes to sleep

536 Views Asked by At

I'm working with MOGRE 1.8.1 to embed 3D models within a WPF application. I've run into an issue where the application crashes when the user changes resolution or their computer goes to sleep. I believe this is because the render system is trying to draw to a surface that it doesn't have access to anymore.

I'm not exactly sure what to do; I've tried using the dispose method to kill MOGRE and reboot it later (by catching the windows event), but have run into a memory leak. The pause render method included within the MOGRE library does not seem to work either. Does anyone have any ideas on how to circumvent this issue?

Notes

Thank you for your help.

1

There are 1 best solutions below

0
Lucas Moreira On

This error happen when the device is lost, so you have to add your control in the function RenderFrame()

//WallPaper, CTRL + ALT + DEL, etc
        if (this.isDeviceLost)
        {
            //Recreate the texture render
            ReInitRenderTarget();

            //Restore device lost
            _renderWindow._beginUpdate();
            _renderWindow._endUpdate();

            _reloadRenderTargetTime = -1;
            this.isDeviceLost = false;
        }

And this is my ReInitRenderTarget() function

protected void ReInitRenderTarget()
    {
        DetachRenderTarget(true, false);
        DisposeRenderTarget();

        _texture = TextureManager.Singleton.CreateManual(
            "OgreImageSource RenderTarget",
            ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME,
            TextureType.TEX_TYPE_2D,
            (uint)ViewportSize.Width, (uint)ViewportSize.Height,
            0, Mogre.PixelFormat.PF_R8G8B8A8,
            (int)TextureUsage.TU_RENDERTARGET);//, null, false, 8);

        _renTarget = _texture.GetBuffer().GetRenderTarget();

        _reloadRenderTargetTime = 0;

        int viewportCount = ViewportDefinitions.Length;
        viewports = new Viewport[viewportCount];

        for (int i = 0; i < viewportCount; i++)
        {
            Viewport viewport;
            ViewportDefinition vd = ViewportDefinitions[i];
            viewport = _renTarget.AddViewport(vd.Camera, zIndexCounter++, vd.Left, vd.Top, vd.Width, vd.Height);
            viewport.BackgroundColour = vd.BackgroundColour;
            viewports[i] = viewport;
        }

        var ev = ViewportsChanged;
        if (ev != null) ev();

        viewportDefinitionsChanged = false;
    }