void CreateSwapChain()
        {
            SwapChainDescription1 swapChainDescription = new SwapChainDescription1()
            {
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 2,
                SwapEffect        = SwapEffect.FlipSequential,
                Stereo            = false,
                SampleDescription = new SampleDescription(1, 0),
                Scaling           = Scaling.Stretch,
                Format            = Format.R8G8B8A8_UNorm,
                Height            = 1080,
                Width             = 1920,
            };

            // 建立SwapChain
            using (SharpDX.DXGI.Device3 dxgiDevice3 = D3D11Device.QueryInterface <SharpDX.DXGI.Device3>()) {
                using (Factory2 dxgiFactory2 = dxgiDevice3.Adapter.GetParent <Factory2>()) {
                    swapChain1 = new SwapChain1(dxgiFactory2, D3D11Device, ref swapChainDescription);
                    swapChain1.QueryInterface <SwapChain>();
                }
            }

            // 把Xaml的SwapChainPanel與DirectX的SwapChain連結起來
            using (ISwapChainPanelNative swapChainPanelNative = ComObject.As <ISwapChainPanelNative>(this)) {
                swapChainPanelNative.SwapChain = swapChain1;
                SetViewport();
            }
        }
Exemple #2
0
 protected override SharpDX.DXGI.SwapChain2 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
 {
     // Creates a SwapChain from a CoreWindow pointer
     using (var comWindow = new ComObject(window))
         using (var swapChain1 = new SwapChain1(factory, device, comWindow, ref desc))
             return(swapChain1.QueryInterface <SwapChain2>());
 }
Exemple #3
0
        private static SwapChain3 CreateSwapChain(
            IntPtr windowHandle,
            D3D12.CommandQueue commandQueue,
            PixelFormat backBufferFormat,
            int backBufferCount,
            int width,
            int height)
        {
            var swapChainDescription = new SwapChainDescription1
            {
                Width             = width,
                Height            = height,
                Format            = backBufferFormat.ToDxgiFormat(),
                Stereo            = false,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                Scaling           = Scaling.Stretch,
                BufferCount       = backBufferCount,
                SwapEffect        = SwapEffect.FlipSequential,
                Flags             = SwapChainFlags.None
            };

#if DEBUG
            const bool debug = true;
#else
            const bool debug = false;
#endif
            using (var dxgiFactory = new Factory2(debug))
                using (var tempSwapChain = new SwapChain1(dxgiFactory, commandQueue, windowHandle, ref swapChainDescription))
                {
                    return(tempSwapChain.QueryInterface <SwapChain3>());
                }
        }
        public void CreateDirectXSwapChain()
        {
            SwapChainDescription1 swapChainDescription = new SwapChainDescription1()
            {
                // Want Transparency.
                AlphaMode = SharpDX.DXGI.AlphaMode.Premultiplied,
                // Double buffer.
                BufferCount = 2,
                // BGRA 32bit pixel format.
                Format = Format.B8G8R8A8_UNorm,
                // Unlike in CoreWindow swap chains, the dimensions must be set.
                Height = (int)(ActualHeight),
                Width  = (int)(ActualWidth),
                // 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,
            };

            using (SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport))
            {
                this.device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device2>();
            }

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

            using (SharpDX.DXGI.Device3 dxgiDevice3 = device.QueryInterface <SharpDX.DXGI.Device3>())
            {
                using (SharpDX.DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <SharpDX.DXGI.Factory3>())
                {
                    using (SwapChain1 swapChain1 = new SwapChain1(dxgiFactory3, device, ref swapChainDescription))
                    {
                        swapChain = swapChain1.QueryInterface <SwapChain2>();
                    }
                }
            }

            using (ISwapChainPanelNative nativeObject = ComObject.As <ISwapChainPanelNative>(this))
            {
                nativeObject.SwapChain = swapChain;
            }

            backBufferTexture = SharpDX.Direct3D11.Resource.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0);

            using (var surface = backBufferTexture.QueryInterface <Surface2>())
            {
                d2d1DC = new DeviceContext(surface);
            }
        }
        protected override SharpDX.DXGI.SwapChain2 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
        {
            // Creates the swap chain for XAML composition
            using (var swapChain1 = new SwapChain1(factory, device, ref desc))
            {
                var swapChain2 = swapChain1.QueryInterface<SwapChain2>();

                // Associate the SwapChainPanel with the swap chain
                nativePanel.SwapChain = swapChain2;

                // Returns the new swap chain
                return swapChain2;
            }
        }
Exemple #6
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();
            }
        }
        public void CreateDirectXSwapChain(Adapter adapter)
        {
            var desc = adapter.Description;

            Debug.WriteLine(desc.Description);
            Debug.WriteLine($"vender = {desc.VendorId:X4}");
            Debug.WriteLine($"Shared Memory: {desc.SharedSystemMemory} bytes");
            Debug.WriteLine($"Video Memory: {desc.DedicatedVideoMemory} bytes");
            Debug.WriteLine($"device: {desc.DeviceId}");

            FeatureLevel[] featureLevels = new FeatureLevel[] {
                FeatureLevel.Level_11_1,
                FeatureLevel.Level_11_0,
                FeatureLevel.Level_10_1,
            };

            D3D11Device = new SharpDX.Direct3D11.Device(adapter, DeviceCreationFlags.BgraSupport, featureLevels);

            SwapChainDescription1 swapChainDescription = new SwapChainDescription1()
            {
                // Double buffer.
                BufferCount = 2,
                // BGRA 32bit pixel format.
                Format = Format.B8G8R8A8_UNorm,
                // Unlike in CoreWindow swap chains, the dimensions must be set.
                Height = (int)(ActualHeight),
                Width  = (int)(ActualWidth),
                // 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,
            };

            // 建立SwapChain
            using (SharpDX.DXGI.Device3 dxgiDevice3 = D3D11Device.QueryInterface <SharpDX.DXGI.Device3>()) {
                using (Factory2 dxgiFactory2 = dxgiDevice3.Adapter.GetParent <Factory2>()) {
                    using (SwapChain1 swapChain1 = new SwapChain1(dxgiFactory2, D3D11Device, ref swapChainDescription)) {
                        swapChain = swapChain1.QueryInterface <SwapChain>();
                    }
                }
            }

            // 把Xaml的SwapChainPanel與DirectX的SwapChain連結起來
            using (ISwapChainPanelNative swapChainPanelNative = ComObject.As <ISwapChainPanelNative>(this)) {
                swapChainPanelNative.SwapChain = swapChain;
            }

            backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0);

            var renderTarget = new RenderTargetView(D3D11Device, backBuffer);

            context = D3D11Device.ImmediateContext;
            // Clear Screen to Teel Blue.
            context.ClearRenderTargetView(renderTarget, new Color(0xFF887536));
            swapChain.Present(1, PresentFlags.None);
        }
        protected virtual void CreateSizeDependentResources()
        {
            // Ensure dependent objects have been released.
            d2dContext.Target = null;
            if (d2dTargetBitmap != null)
            {
                RemoveAndDispose(ref d2dTargetBitmap);
            }
            d3dContext.OutputMerger.SetRenderTargets((RenderTargetView)null);
            d3dContext.Flush();

            /*d3dContext.FinishCommandList(false);
             * d3dContext.li*/

            // Set render target size to the rendered size of the panel including the composition scale,
            // defaulting to the minimum of 1px if no size was specified.
            var scale              = Dpi / 96;
            var renderTargetWidth  = (int)(panel.ActualWidth * scale);
            var renderTargetHeight = (int)(panel.ActualHeight * scale);

            if (renderTargetWidth == 0 || renderTargetHeight == 0)
            {
                return;
            }

            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, renderTargetWidth, renderTargetHeight, Format.B8G8R8A8_UNorm, SwapChainFlags.None);
            }
            else
            {
                var swapChainDesc = new SwapChainDescription1();
                swapChainDesc.Width  = renderTargetWidth;
                swapChainDesc.Height = renderTargetHeight;
                swapChainDesc.Format = Format.B8G8R8A8_UNorm;
                swapChainDesc.Stereo = new RawBool(false);
                swapChainDesc.SampleDescription.Count   = 1;
                swapChainDesc.SampleDescription.Quality = 0;
                swapChainDesc.Usage       = Usage.RenderTargetOutput;
                swapChainDesc.BufferCount = 2;
                swapChainDesc.SwapEffect  = SwapEffect.FlipSequential;
                swapChainDesc.Flags       = SwapChainFlags.None;
                swapChainDesc.AlphaMode   = AlphaMode.Unspecified; // Todo... May need to change
                //swapChainDesc.Scaling = Scaling.None;

                using (var dxgiDevice = d3dDevice.QueryInterface <global::SharpDX.DXGI.Device1>())
                {
                    var dxgiAdapter = dxgiDevice.Adapter;
                    var dxgiFactory = dxgiAdapter.GetParent <global::SharpDX.DXGI.Factory2>();

                    swapChain = Collect(new SwapChain1(dxgiFactory, d3dDevice, ref swapChainDesc, null));

                    // Counter act the composition scale of the render target as
                    // we already handle this in the platform window code.
                    using (var swapChain2 = swapChain.QueryInterface <SwapChain2>())
                    {
                        var inverseScale = new RawMatrix3x2();
                        inverseScale.M11           = 1.0f / scale;
                        inverseScale.M22           = 1.0f / scale;
                        swapChain2.MatrixTransform = inverseScale;
                    }

                    dxgiDevice.MaximumFrameLatency = 1;
                }

                // Associate the SwapChainBackgroundPanel with the swap chain
                using (var panelNative = ComObject.As <ISwapChainPanelNative>(panel))
                {
                    panelNative.SwapChain = swapChain;
                }
            }

            var bitmapProperties = new BitmapProperties1(
                new PixelFormat(Format.B8G8R8A8_UNorm, global::SharpDX.Direct2D1.AlphaMode.Premultiplied),
                Dpi,
                Dpi,
                BitmapOptions.Target | BitmapOptions.CannotDraw);

            using (var dxgiBackBuffer = swapChain.GetBackBuffer <Surface>(0))
            {
                d2dTargetBitmap   = Collect(new Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));
                d2dContext.Target = d2dTargetBitmap;
            }
        }
Exemple #9
0
 private void CreateSwapChain(ref SwapChainDescription1 swapChainDescription1, Factory4 factory)
 {
     using (var sc1 = new SwapChain1(factory, commandQueue, form.Handle, ref swapChainDescription1))
         swapChain = Collect(sc1.QueryInterface <SwapChain3>());
 }
        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();
        }