Esempio n. 1
0
        /// <summary>
        /// Release the unmanaged resources used by the <see cref="Direct3DSurface"/> and it's child controls and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">True to release managed resources and unmanaged resources. False to only release unmanaged resources.</param>

        protected override void Dispose(bool disposing)
        {
            if (!disposing)
            {
                UnloadContent();

                Factory2D.Dispose();
                FactoryDirectWrite.Dispose();
                FactoryDxgi.Dispose();

                RenderTargetView.Dispose();
                RenderTarget2D.Dispose();

                _backBuffer.Dispose();
                _swapChain.Dispose();

                Context.Dispose();
                _device.Dispose();
                ProfileStats.Clear();
            }

            base.Dispose(disposing);
        }
Esempio n. 2
0
        /// <summary>
        /// Initialize DirectX factories, swap chain and render targets.
        /// </summary>
        protected void InitGraphics()
        {
            var width  = Size.Width;
            var height = Size.Height;

            Factory2D          = new Factory();
            FactoryDirectWrite = new SharpDX.DirectWrite.Factory();

            var desc = new SwapChainDescription
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput | Usage.BackBuffer,
            };

            // use software driver if RDP or VirtualMachine
            _driverType = MM_System_Profiling.IsTerminalOrVirtual() ? DriverType.Warp : DriverType.Hardware;
            bool swapChainCreated = false;

            while (!swapChainCreated)
            {
                try
                {
                    Device.CreateWithSwapChain(_driverType, DeviceCreationFlags.BgraSupport, desc, out _device, out _swapChain);
                    MM_System_Interfaces.LogError("Initialized DirectX Driver: {0}", _driverType);
                    swapChainCreated = true;
                }
                catch (Exception ex)
                {
                    // downgrade driver and try again using outer while loop
                    if (_driverType == DriverType.Hardware)
                    {
                        _driverType = DriverType.Warp;
                    }
                    else if (_driverType == DriverType.Warp)
                    {
                        _driverType = DriverType.Software;
                    }
                    else
                    {
                        MM_System_Interfaces.LogError(ex);
                        throw new NotSupportedException("DirectX Rendering could not be initialized using hardware of software.", ex);
                    }
                }
            }

            Context = _device.ImmediateContext;

            // Ignore all windows events
            FactoryDxgi = _swapChain.GetParent <SharpDX.DXGI.Factory>();
            FactoryDxgi.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll);

            // New RenderTargetView from the backbuffer
            _backBuffer                  = Resource.FromSwapChain <Texture2D>(_swapChain, 0);
            RenderTargetView             = new RenderTargetView(_device, _backBuffer);
            DxgiSurface                  = _backBuffer.QueryInterface <Surface>();
            RenderTarget2D               = new RenderTarget(Factory2D, DxgiSurface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            RenderTarget2D.AntialiasMode = AntialiasMode;

            PresenterReady = true;
        }