/// <summary>
        /// Конструктор
        /// </summary>
        /// <param name="BackBuffer">Буффер на который будем рисовать, наш холст</param>
        /// <param name="Width">Ширина области в которую будем рисовать</param>
        /// <param name="Height">Высота объласти в которую будем рисовать</param>
        public TextWirter(SharpDX.Direct3D11.Device2 device, SwapChain2 swapChain, Color color, float dpi = 96f, string font = "Calibri", int size = 14)
        {
#if DEBUG
            var debug = SharpDX.Direct2D1.DebugLevel.Error;
#else
            var debug = SharpDX.Direct2D1.DebugLevel.None;
#endif

            _Factory2D = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debug);
            using (var dxgiDevice = device.QueryInterface <SharpDX.DXGI.Device>())
            {
                d2dDevice = new SharpDX.Direct2D1.Device(_Factory2D, dxgiDevice);
            }
            _RenderTarget2D = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

            BitmapProperties1 properties = new BitmapProperties1(
                new PixelFormat(
                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    SharpDX.Direct2D1.AlphaMode.Premultiplied),
                dpi,
                dpi,
                BitmapOptions.Target | BitmapOptions.CannotDraw);
            Surface backBuffer = swapChain.GetBackBuffer <Surface>(0);
            d2dTarget        = new Bitmap1(_RenderTarget2D, backBuffer, properties);
            this.TextFont    = font;
            this.TextSize    = size;
            _FactoryDWrite   = new SharpDX.DirectWrite.Factory();
            _SceneColorBrush = new SolidColorBrush(_RenderTarget2D, color);
            InitTextFormat();
            _RenderTarget2D.TextAntialiasMode = TextAntialiasMode.Cleartype;
        }
Пример #2
0
        public SwapChainGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters)
            : base(device, presentationParameters)
        {
            PresentInterval = presentationParameters.PresentationInterval;

            // Initialize the swap chain
            swapChain = ToDispose(CreateSwapChain());

#if DIRECTX11_2
            swapChain2 = ToDispose(swapChain.QueryInterface <SwapChain2>());
#endif

            backBuffer = ToDispose(RenderTarget2D.New(device, swapChain.GetBackBuffer <Direct3D11.Texture2D>(0)));
        }
Пример #3
0
        /// <summary>
        /// Destroys the SwapChain and all related instances.
        /// </summary>
        void DestroySwapChain()
        {
            _swapChain2?.Dispose();
            _swapChain2 = null;

            _device3?.Dispose();
            _device3 = null;

            _swapChain?.Dispose();
            _swapChain = null;

            _device?.Dispose();
            _device = null;

            _d3D11Device?.Dispose();
            _d3D11Device = null;
        }
        /// <summary>
        /// Called when [create render target and depth buffers].
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <returns></returns>
        protected override ShaderResourceViewProxy OnCreateBackBuffer(int width, int height)
        {
            if (swapChain == null || swapChain.IsDisposed)
            {
                swapChain = CreateSwapChain();
            }
            else
            {
                swapChain.ResizeBuffers(swapChain.Description1.BufferCount, TargetWidth, TargetHeight, swapChain.Description.ModeDescription.Format, swapChain.Description.Flags);
            }

            var backBuffer = Collect(new ShaderResourceViewProxy(Device, Texture2D.FromSwapChain <Texture2D>(swapChain, 0)));

            d2dTarget = Collect(new D2DTargetProxy());
            d2dTarget.Initialize(swapChain, DeviceContext2D);
            return(backBuffer);
        }
Пример #5
0
        /// <summary>
        /// Initializes the SwapChain for use with LibVLC
        /// </summary>
        void CreateSwapChain()
        {
            SharpDX.DXGI.Factory2 dxgiFactory = null;
            try
            {
                var deviceCreationFlags =
                    DeviceCreationFlags.BgraSupport | DeviceCreationFlags.VideoSupport;

#if DEBUG
                if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily != Mobile)
                {
                    deviceCreationFlags |= DeviceCreationFlags.Debug;
                }

                try
                {
                    dxgiFactory = new SharpDX.DXGI.Factory2(true);
                }
                catch (SharpDXException)
                {
                    dxgiFactory = new SharpDX.DXGI.Factory2(false);
                }
#else
                dxgiFactory = new SharpDX.DXGI.Factory2(false);
#endif
                _d3D11Device = null;
                foreach (var adapter in dxgiFactory.Adapters)
                {
                    try
                    {
                        _d3D11Device = new SharpDX.Direct3D11.Device(adapter, deviceCreationFlags);
                        break;
                    }
                    catch (SharpDXException)
                    {
                    }
                }

                if (_d3D11Device is null)
                {
                    throw new VLCException("Could not create Direct3D11 device : No compatible adapter found.");
                }

                _device = _d3D11Device.QueryInterface <SharpDX.DXGI.Device1>();

                //Create the swapchain
                var swapChainDescription = new SharpDX.DXGI.SwapChainDescription1
                {
                    Width             = (int)(_panel.ActualWidth * _panel.CompositionScaleX),
                    Height            = (int)(_panel.ActualHeight * _panel.CompositionScaleY),
                    Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    Stereo            = false,
                    SampleDescription =
                    {
                        Count   = 1,
                        Quality = 0
                    },
                    Usage       = Usage.RenderTargetOutput,
                    BufferCount = 2,
                    SwapEffect  = SwapEffect.FlipSequential,
                    Flags       = SwapChainFlags.None,
                    AlphaMode   = AlphaMode.Unspecified
                };

                _swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, _d3D11Device, ref swapChainDescription);

                _device.MaximumFrameLatency = 1;

                using (var panelNative = ComObject.As <ISwapChainPanelNative>(_panel))
                {
                    panelNative.SwapChain = _swapChain;
                }

                // This is necessary so we can call Trim() on suspend
                _device3 = _device.QueryInterface <SharpDX.DXGI.Device3>();
                if (_device3 == null)
                {
                    throw new VLCException("Failed to query interface \"Device3\"");
                }

                _swapChain2 = _swapChain.QueryInterface <SharpDX.DXGI.SwapChain2>();
                if (_swapChain2 == null)
                {
                    throw new VLCException("Failed to query interface \"SwapChain2\"");
                }

                UpdateScale();
                UpdateSize();
                _loaded = true;
                Initialized?.Invoke(this, new InitializedEventArgs(SwapChainOptions));
            }
            catch (Exception ex)
            {
                DestroySwapChain();
                if (ex is SharpDXException)
                {
                    throw new VLCException("SharpDX operation failed, see InnerException for details", ex);
                }

                throw;
            }
            finally
            {
                dxgiFactory?.Dispose();
            }
        }
        /// <summary>
        /// Called when [create render target and depth buffers].
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="createDepthStencilBuffer"></param>
        /// <returns></returns>
        protected override Texture2D OnCreateRenderTargetAndDepthBuffers(int width, int height, bool createDepthStencilBuffer)
        {
            if (swapChain == null || swapChain.IsDisposed)
            {
                swapChain = CreateSwapChain();
            }
            else
            {
                swapChain.ResizeBuffers(swapChain.Description1.BufferCount, TargetWidth, TargetHeight, swapChain.Description.ModeDescription.Format, swapChain.Description.Flags);
            }

#if MSAA
            int sampleCount   = 1;
            int sampleQuality = 0;
            if (MSAA != MSAALevel.Disable)
            {
                do
                {
                    var newSampleCount   = sampleCount * 2;
                    var newSampleQuality = Device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, newSampleCount) - 1;

                    if (newSampleQuality < 0)
                    {
                        break;
                    }

                    sampleCount   = newSampleCount;
                    sampleQuality = newSampleQuality;
                    if (sampleCount == (int)MSAA)
                    {
                        break;
                    }
                } while (sampleCount < 32);
            }
            renderTargetNMS = Collect(Texture2D.FromSwapChain <Texture2D>(swapChain, 0));
            var colordesc = renderTargetNMS.Description;
            colordesc.SampleDescription = new SampleDescription(sampleCount, sampleQuality);
            colorBuffer     = Collect(new Texture2D(Device, colordesc));
            colorBufferView = Collect(new RenderTargetView(Device, colorBuffer));
            var sampleDesc = colordesc.SampleDescription;
#else
            colorBuffer = Collect(Texture2D.FromSwapChain <Texture2D>(swapChain, 0));
            var sampleDesc = swapChain.Description1.SampleDescription;
            colorBufferView = Collect(new RenderTargetView(Device, colorBuffer));
#endif

            if (createDepthStencilBuffer)
            {
                var depthdesc = new Texture2DDescription
                {
                    BindFlags = BindFlags.DepthStencil,
                    //Format = Format.D24_UNorm_S8_UInt,
                    Format            = Format.D32_Float_S8X24_UInt,
                    Width             = width,
                    Height            = height,
                    MipLevels         = 1,
                    SampleDescription = sampleDesc,
                    Usage             = ResourceUsage.Default,
                    OptionFlags       = ResourceOptionFlags.None,
                    CpuAccessFlags    = CpuAccessFlags.None,
                    ArraySize         = 1,
                };
                depthStencilBuffer     = Collect(new Texture2D(Device, depthdesc));
                depthStencilBufferView = Collect(new DepthStencilView(Device, depthStencilBuffer));
            }
            d2dTarget = Collect(new D2DTargetProxy());
            d2dTarget.Initialize(swapChain, DeviceContext2D);
            return(colorBuffer);
        }
Пример #7
0
        public Window(SwapChainPanel panel, Aiv.Fast2D.UWP.IGame game)
        {
            this.context = panel;
            this.game    = game;

            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug))
            {
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            }

            // Save the context instance
            this.deviceContext = this.device.ImmediateContext2;

            // Properties of the swap chain
            SwapChainDescription1 swapChainDescription = new SwapChainDescription1()
            {
                // No transparency.
                AlphaMode = AlphaMode.Ignore,
                // Double buffer.
                BufferCount = 2,
                // BGRA 32bit pixel format.
                Format = Format.R8G8B8A8_UNorm,
                // Unlike in CoreWindow swap chains, the dimensions must be set.
                Height = (int)(this.context.RenderSize.Height),
                Width  = (int)(this.context.RenderSize.Width),
                // Default multisampling.
                SampleDescription = new SampleDescription(1, 0),
                // In case the control is resized, stretch the swap chain accordingly.
                Scaling = Scaling.Stretch,
                // No support for stereo display.
                Stereo = false,
                // Sequential displaying for double buffering.
                SwapEffect = SwapEffect.FlipSequential,
                // This swapchain is going to be used as the back buffer.
                Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
            };

            // Retrive the DXGI device associated to the Direct3D device.
            using (Device3 dxgiDevice3 = this.device.QueryInterface <Device3>())
            {
                // Get the DXGI factory automatically created when initializing the Direct3D device.
                using (Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <Factory5>())
                {
                    // Create the swap chain and get the highest version available.
                    using (SwapChain1 swapChain1 = new SwapChain1(dxgiFactory3, this.device, ref swapChainDescription))
                    {
                        this.swapChain = swapChain1.QueryInterface <SwapChain2>();
                    }
                }
            }

            // Obtain a reference to the native COM object of the SwapChainPanel.
            using (ISwapChainPanelNative nativeObject = ComObject.As <ISwapChainPanelNative>(this.context))
            {
                // Set its swap chain.
                nativeObject.SwapChain = this.swapChain;
            }

            // Create a Texture2D from the existing swap chain to use as
            D3D11.Texture2D backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
            this.renderTargetView = new D3D11.RenderTargetView(this.device, backBufferTexture);



            FinalizeSetup();

            width  = (int)this.context.RenderSize.Width;
            height = (int)this.context.RenderSize.Height;

            scaleX = 1;
            scaleY = 1;



            this.SetViewport(0, 0, width, height);

            // for now disable only backface culling
            D3D11.RasterizerStateDescription rasterizerDescription = D3D11.RasterizerStateDescription.Default();
            rasterizerDescription.CullMode      = D3D11.CullMode.None;
            this.deviceContext.Rasterizer.State = new SharpDX.Direct3D11.RasterizerState(this.device, rasterizerDescription);

            vsync = 1;

            CompositionTarget.Rendering += this.Update;
            this.game.GameSetup(this);

            watch = new Stopwatch();
        }