Exemple #1
0
        public static DXGI.SwapChain1 CreateSwapChain(
            int width,
            int height,
            Direct3D11.Device device)
        {
            var dxgiDevice  = device.QueryInterface <DXGI.Device>();
            var dxgiFactory = dxgiDevice.Adapter.GetParent <DXGI.Factory2>();
            var dxgiDesc    = new DXGI.SwapChainDescription1
            {
                Width             = width,
                Height            = height,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Usage             = DXGI.Usage.RenderTargetOutput,
                BufferCount       = 2,
                Scaling           = DXGI.Scaling.Stretch,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
            };

            return(new DXGI.SwapChain1(
                       dxgiFactory,
                       device,
                       ref dxgiDesc));
        }
Exemple #2
0
        private void CreateSwapChain(IntPtr window, bool vsync)
        {
            using (SharpDX.DXGI.Factory4 factory = new Factory4())
            {
                //SharpDX.DXGI.Adapter adapter = factory.GetAdapterByLuid(_dev.AdapterLuid);
                //System.Console.WriteLine($"  Adapter: {adapter.Description.Description}");
                System.Console.WriteLine($"  Adapter: {factory.Adapters[0].Description.Description}");

                if (!vsync)
                {
                    using (SharpDX.DXGI.Factory5 factory5 = factory.QueryInterface <Factory5>())
                    {
                        if (factory5 != null)
                        {
                            SharpDX.Mathematics.Interop.RawBool tearing = false;
                            GCHandle pinnedInt = GCHandle.Alloc(tearing, GCHandleType.Pinned);
                            IntPtr   pointer   = pinnedInt.AddrOfPinnedObject();

                            factory5.CheckFeatureSupport(SharpDX.DXGI.Feature.PresentAllowTearing, pointer, System.Runtime.InteropServices.Marshal.SizeOf(tearing));
                            if (tearing != false)
                            {
                                _swapChainFlags = SharpDX.DXGI.SwapChainFlags.AllowTearing;
                                _presentFlags   = SharpDX.DXGI.PresentFlags.AllowTearing;
                            }

                            pinnedInt.Free();
                        }
                    }

                    _syncInterval = 0;
                }
                else
                {
                    _syncInterval = 1;
                }

                SharpDX.DXGI.SwapChainDescription1 desc = new SharpDX.DXGI.SwapChainDescription1();
                desc.Width             = 0;
                desc.Height            = 0;
                desc.Format            = SharpDX.DXGI.Format.R8G8B8A8_UNorm;
                desc.Stereo            = false;
                desc.SampleDescription = new SampleDescription {
                    Count = 1, Quality = 0
                };
                desc.Usage       = SharpDX.DXGI.Usage.RenderTargetOutput;
                desc.BufferCount = FrameCount;
                desc.Scaling     = SharpDX.DXGI.Scaling.Stretch;
                desc.SwapEffect  = SharpDX.DXGI.SwapEffect.FlipDiscard;
                desc.AlphaMode   = SharpDX.DXGI.AlphaMode.Unspecified;
                desc.Flags       = _swapChainFlags;

                using (SharpDX.DXGI.SwapChain1 swapChain = new SharpDX.DXGI.SwapChain1(factory, _queue, window, ref desc))
                {
                    factory.MakeWindowAssociation(window, WindowAssociationFlags.IgnoreAltEnter);
                    _swapChain = swapChain.QueryInterface <SharpDX.DXGI.SwapChain3>();
                }
            }
        }
Exemple #3
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            item      = i;
            device    = d;
            d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            var size = item.Size;

            if (size.Height == 0 || size.Width == 0)
            {
                size = new SizeInt32()
                {
                    Height = 1, Width = 1
                }
            }
            ;

            var dxgiFactory = new SharpDX.DXGI.Factory2();
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = size.Width,
                Height            = size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                size);
            session  = framePool.CreateCaptureSession(i);
            lastSize = size;

            framePool.FrameArrived += OnFrameArrived;
        }
Exemple #4
0
        public static DXGI.SwapChain1 CreateSwapChainForHwnd(
            Direct3D11.Device device,
            IntPtr hwnd)
        {
            var dxgiDevice  = device.QueryInterface <DXGI.Device>();
            var dxgiFactory = dxgiDevice.Adapter.GetParent <DXGI.Factory2>();
            var dxgiDesc    = new DXGI.SwapChainDescription1
            {
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Usage             = DXGI.Usage.RenderTargetOutput,
                BufferCount       = 2,
            };

            return(new DXGI.SwapChain1(
                       dxgiFactory,
                       device,
                       hwnd,
                       ref dxgiDesc));
        }
Exemple #5
0
        /// <summary>
        /// Creates the swap chain description.
        /// </summary>
        /// <returns>A swap chain description</returns>
        /// <remarks>
        /// This method can be overloaded in order to modify default parameters.
        /// </remarks>
        protected virtual SharpDX.DXGI.SwapChainDescription1 CreateSwapChainDescription()
        {
            // SwapChain description
            var desc = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width  = Width,
                Height = Height,
                // B8G8R8A8_UNorm gives us better performance
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount       = 1,
                Scaling           = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect        = SharpDX.DXGI.SwapEffect.Discard,
                Flags             = SwapChainFlags.AllowModeSwitch
            };

            return(desc);
        }
        /// <summary>
        /// Creates the swap chain description.
        /// </summary>
        /// <returns>A swap chain description</returns>
        /// <remarks>
        /// This method can be overloaded in order to modify default parameters.
        /// </remarks>
        protected virtual SharpDX.DXGI.SwapChainDescription1 CreateSwapChainDescription()
        {
            // SwapChain description
            var desc = new SharpDX.DXGI.SwapChainDescription1()
            {
                // Automatic sizing
                Width             = Width,
                Height            = Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
                // Use two buffers to enable flip effect.
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.None,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
            };

            return(desc);
        }
Exemple #7
0
        private void InitializeD3D()
        {
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, D3D11.DeviceCreationFlags.Debug))
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            this.deviceContext = this.device.ImmediateContext2;

            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                AlphaMode         = DXGI.AlphaMode.Ignore,
                BufferCount       = 2,
                Format            = DXGI.Format.R8G8B8A8_UNorm,
                Height            = (int)(this.swapChainPanel.RenderSize.Height),
                Width             = (int)(this.swapChainPanel.RenderSize.Width),
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Scaling           = SharpDX.DXGI.Scaling.Stretch,
                Stereo            = false,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
                Usage             = DXGI.Usage.RenderTargetOutput
            };

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

            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(this.swapChainPanel))
                nativeObject.SwapChain = this.swapChain;

            this.backBufferTexture = this.swapChain.GetBackBuffer <D3D11.Texture2D>(0);
            this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);

            deviceContext.Rasterizer.SetViewport(0, 0, (int)swapChainPanel.ActualWidth, (int)swapChainPanel.ActualHeight);

            CompositionTarget.Rendering    += CompositionTarget_Rendering;
            Application.Current.Suspending += Current_Suspending;

            isDXInitialized = true;
        }
Exemple #8
0
        public static DX11SwapChain FromComposition(DxDevice dxDevice, int w, int h)
        {
            DX11SwapChain swapShain = new DX11SwapChain();

            swapShain.device = dxDevice;
            var desc = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = w,
                Height            = h,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput | Usage.ShaderInput,
                BufferCount       = 2,
                Scaling           = SharpDX.DXGI.Scaling.None,
                SwapEffect        = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode         = AlphaMode.Premultiplied
            };

            swapShain.swapchain = new SwapChain1(dxDevice.Factory, dxDevice.Device, ref desc);
            swapShain.Initialize();
            return(swapShain);
        }
Exemple #9
0
        /// <summary>
        /// Creates the swap chain description.
        /// </summary>
        /// <returns>A swap chain description</returns>
        /// <remarks>
        /// This method can be overloaded in order to modify default parameters.
        /// </remarks>
        protected virtual SharpDX.DXGI.SwapChainDescription1 CreateSwapChainDescription()
        {
            bool windows8 = Environment.OSVersion.Version.Major == 8;

            // SwapChain description
            var desc = new SharpDX.DXGI.SwapChainDescription1()
            {
                // Automatic sizing
                Width             = deviceManager.Settings.ScreenWidth,
                Height            = deviceManager.Settings.ScreenHeight,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = deviceManager.Settings.IsStereo,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Usage             = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,

                // Use two buffers to enable flip effect.
                BufferCount = 2,
                Scaling     = windows8 ? SharpDX.DXGI.Scaling.None : Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                Flags       = SwapChainFlags.AllowModeSwitch
            };

            return(desc);
        }
Exemple #10
0
        protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
        {
            // Create the swap chain for XAML composition
            var swapChain = new SwapChain1(factory, device, ref desc);

            // Attach swap chain to SwapChainPanel
            nativePanel.SwapChain = swapChain;
            return(swapChain);
        }
        /// <summary>
        /// main thread
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="dpi"></param>
        internal static void CreateD3DInstance(ref SwapChainComponent com)
        {
            int width  = (int)com.panel.Width;
            int height = (int)com.panel.Height;

            dpi      = (int)com.dpi;
            com.port = new Viewport(0, 0, width, height, 0, 1);
            D2D.d3dContext.Rasterizer.SetViewport(com.port);

            var multisampleDesc = new Dxgi.SampleDescription(1, 0);
            var desc            = new Dxgi.SwapChainDescription1()
            {
                // Automatic sizing
                AlphaMode         = Dxgi.AlphaMode.Premultiplied,
                Width             = width,
                Height            = height,
                Format            = Dxgi.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = multisampleDesc,
                Usage             = Dxgi.Usage.RenderTargetOutput,
                BufferCount       = 2,
                SwapEffect        = Dxgi.SwapEffect.FlipSequential,
                Scaling           = Dxgi.Scaling.Stretch
            };

            using (var dxgiDevice2 = D2D.d3dDevice.QueryInterface <Dxgi.Device2>())
                using (var dxgiAdapter = dxgiDevice2.Adapter)
                    using (var dxgiFactory2 = dxgiAdapter.GetParent <Dxgi.Factory2>())
                        using (com.NativePanel = ComObject.As <Dxgi.ISwapChainPanelNative>(com.panel))
                        {
                            com.swapChain                   = new Dxgi.SwapChain1(dxgiFactory2, dxgiDevice2, ref desc, null);
                            com.NativePanel.SwapChain       = com.swapChain;
                            dxgiDevice2.MaximumFrameLatency = 1;
                        }
            using (com.backbuffer = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(com.swapChain, 0))
                com.TargetView = new D3D11.RenderTargetView(D2D.d3dDevice, com.backbuffer);

            using (com.depthbuffer = new D3D11.Texture2D(D2D.d3dDevice, new D3D11.Texture2DDescription()
            {
                Format = Dxgi.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = width,
                Height = height,
                SampleDescription = multisampleDesc,
                Usage = D3D11.ResourceUsage.Default,
                BindFlags = D3D11.BindFlags.DepthStencil,
            }))
                com.StencilView = new D3D11.DepthStencilView(D2D.d3dDevice, com.depthbuffer);
            D2D.d3dContext.OutputMerger.SetTargets(com.StencilView, com.TargetView);
            var bitmapProperties = new D2D1.BitmapProperties1(
                new D2D1.PixelFormat(Dxgi.Format.B8G8R8A8_UNorm, D2D1.AlphaMode.Premultiplied),
                com.dpi, com.dpi,
                D2D1.BitmapOptions.Target | D2D1.BitmapOptions.CannotDraw);

            using (var dxgiBackBuffer = com.swapChain.GetBackBuffer <Dxgi.Surface>(0))
            {
                com.Bitmap = new D2D1.Bitmap1(D2D.d2dContext, dxgiBackBuffer, bitmapProperties);
            }
            D2D.d2dContext.Target            = com.Bitmap;
            D2D.d2dContext.TextAntialiasMode = D2D1.TextAntialiasMode.Cleartype;
        }
        //public override int Width
        //{
        //    get
        //    {
        //        return 0; // use size of CoreWindow
        //    }
        //}

        //public override int Height
        //{
        //    get
        //    {
        //        return 0; // use size of CoreWindow
        //    }
        //}

        protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
        {
            // Creates the swap chain for the CoreWindow
            using (var coreWindow = new ComObject(_window))
                return(new SwapChain1(factory, device, coreWindow, ref desc));
        }
        // Allocate all memory resources that change on a window SizeChanged event.
        protected virtual void CreateWindowSizeDependentResources()
        {
            _windowBounds = _window.Bounds;

            if (_swapChain != null)
            {
                _swapChain.ResizeBuffers(2, 0, 0, DXGI.Format.B8G8R8A8_UNorm, 0);
            }
            else
            {
                var swapChainDesc = new DXGI.SwapChainDescription1()
                {
                    Width = 0,
                    Height = 0,
                    Format = DXGI.Format.B8G8R8A8_UNorm,
                    Stereo = false,
                    Usage = DXGI.Usage.RenderTargetOutput,
                    BufferCount = 2,
                    Scaling = DXGI.Scaling.None,
                    SwapEffect = DXGI.SwapEffect.FlipSequential,
                    SampleDescription = new DXGI.SampleDescription { Count = 1, Quality = 0 },
                    Flags = 0
                };

                _swapChain = _window.CreateSwapChain(_device, ref swapChainDesc);

                // gotta figure out some reasonable way of doing this
                // dxgiDevice.MaximumFrameLatency = 1;

                D3D11.Texture2D backBuffer = _swapChain.GetBackBuffer<D3D11.Texture2D>(0);

                _renderTargetView = new D3D11.RenderTargetView(_device, backBuffer);

                // Cache the rendertarget dimensions in our helper class for convenient use.
                _renderTargetSize.Width = backBuffer.Description.Width;
                _renderTargetSize.Height = backBuffer.Description.Height;

                // Create a descriptor for the depth/stencil buffer.
                var depthStencilDesc = new D3D11.Texture2DDescription
                {
                    Format = DXGI.Format.D24_UNorm_S8_UInt,
                    Width = backBuffer.Description.Width,
                    Height = backBuffer.Description.Height,
                    ArraySize = 1,
                    MipLevels = 1,
                    BindFlags = D3D11.BindFlags.DepthStencil,
                    SampleDescription = new DXGI.SampleDescription { Count = 1 }
                };

                // Allocate a 2-D surface as the depth/stencil buffer.
                var depthStencil = new D3D11.Texture2D(_device, depthStencilDesc);

                // Create a DepthStencil view on this surface to use on bind.
                _depthStencilView = new D3D11.DepthStencilView(_device, depthStencil,
                    new D3D11.DepthStencilViewDescription { Dimension = D3D11.DepthStencilViewDimension.Texture2D });

                // Create a viewport descriptor of the full window size.
                var viewPort = new D3D11.Viewport
                {
                    TopLeftX = 0.0f,
                    TopLeftY = 0.0f,
                    Width = backBuffer.Description.Width,
                    Height = backBuffer.Description.Height
                };

                // Set the current viewport using the descriptor.
                _deviceContext.Rasterizer.SetViewports(viewPort);
            }
        }
Exemple #14
0
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a new Direct3D hardware device and ask for Direct3D 11.2 support
            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;

            Size2 sizeInPixels = RenderSizeToPixelSize(this.SwapChainPanel.RenderSize);

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

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

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

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

            // This event is fired when the application requests a new frame from the DirectX interop controls.
            CompositionTarget.Rendering += CompositionTarget_Rendering;

            // Subscribe to the suspending event
            Application.Current.Suspending += Application_Suspending;

            // Mark our resources as initialized
            isDXInitialized = true;
        }
Exemple #15
0
        public VideoRenderer(VideoTrack videoTrack, RendererOptions options)
        {
            VideoTrack = videoTrack;

            VideoFrameWidth     = options.VideoFrameWidth;
            VideoFrameHeight    = options.VideoFrameHeight;
            VideoFrameQueueSize = options.VideoFrameQueueSize;

            videoTrack.LocalVideoFrameProcessed += OnLocalVideoFrameProcessed;

            // _onMissedFrame = options.OnMissedFrame ?? OnMissedFrame;

            bool debug = options.CreationFlags.HasFlag(D3D11.DeviceCreationFlags.Debug);

            FactoryDXGI = new DXGI.Factory2(debug);

            // Find the requested adapter.
            using (var adapters = FactoryDXGI.Adapters.ToDisposableList())
            {
                var adapter = adapters.First(a => a.Description.VendorId == options.AdapterVendorId);

                Device3D = new D3D11.Device(adapter, options.CreationFlags, options.FeatureLevels);

                DeviceDXGI = Device3D.QueryInterface <DXGI.Device>();

                // We need to access D3D11 on multiple threads, so enable multi-threading
                ThreadLock3D = Device3D.ImmediateContext.QueryInterface <D3D11.Multithread>();
                ThreadLock3D.SetMultithreadProtected(true);

                if (options.PreviewWindowOptions != null)
                {
                    var width  = options.PreviewWindowOptions.Width ?? VideoFrameWidth;
                    var height = options.PreviewWindowOptions.Height ?? width * VideoFrameHeight / VideoFrameWidth;

                    _sdlWindow = new SdlWindow("WebRTC server preview", width, height);


                    // SwapChain description
                    var desc = new DXGI.SwapChainDescription1()
                    {
                        BufferCount       = 2,
                        AlphaMode         = DXGI.AlphaMode.Unspecified,
                        Format            = DXGI.Format.B8G8R8A8_UNorm,
                        Width             = VideoFrameWidth,
                        Height            = VideoFrameHeight,
                        Scaling           = DXGI.Scaling.Stretch,
                        Stereo            = false,
                        Flags             = DXGI.SwapChainFlags.AllowTearing | DXGI.SwapChainFlags.FrameLatencyWaitAbleObject,
                        Usage             = DXGI.Usage.RenderTargetOutput,
                        SampleDescription = new DXGI.SampleDescription(1, 0),
                        SwapEffect        = DXGI.SwapEffect.FlipDiscard,
                    };

                    SwapChain = new DXGI.SwapChain1(FactoryDXGI, Device3D, _sdlWindow.NativeHandle, ref desc);

                    using (var swapChain2 = SwapChain.QueryInterface <DXGI.SwapChain2>())
                    {
                        var value = swapChain2.MaximumFrameLatency;
                        swapChain2.MaximumFrameLatency = 1;
                    }
                }
            }
        }
Exemple #16
0
        void CreateBuffers()
        {
            SharpDX.Direct3D12.CpuDescriptorHandle rtv   = _heapRTV.CPUDescriptorHandleForHeapStart;
            SharpDX.Direct3D12.HeapFlags           flags = SharpDX.Direct3D12.HeapFlags.None;

            _frameIndex = _swapChain.CurrentBackBufferIndex;
            SharpDX.DXGI.SwapChainDescription1 swapChainDesc = _swapChain.Description1;

            // TODO: Check ID3D12Device8 to enable D3D12_HEAP_FLAG_CREATE_NOT_ZEROED

            SharpDX.Direct3D12.ResourceDescription desc = new SharpDX.Direct3D12.ResourceDescription();
            desc.Dimension         = SharpDX.Direct3D12.ResourceDimension.Texture2D;
            desc.Alignment         = 0;
            desc.Width             = swapChainDesc.Width;
            desc.Height            = swapChainDesc.Height;
            desc.DepthOrArraySize  = 1;
            desc.MipLevels         = 1;
            desc.SampleDescription = _sampleDesc;
            desc.Layout            = SharpDX.Direct3D12.TextureLayout.Unknown;

            SharpDX.Direct3D12.HeapProperties heap = new SharpDX.Direct3D12.HeapProperties();
            heap.Type                 = SharpDX.Direct3D12.HeapType.Default;
            heap.CPUPageProperty      = SharpDX.Direct3D12.CpuPageProperty.Unknown;
            heap.MemoryPoolPreference = SharpDX.Direct3D12.MemoryPool.Unknown;
            heap.CreationNodeMask     = 0;
            heap.VisibleNodeMask      = 0;

            SharpDX.Direct3D12.ClearValue clearValue = new SharpDX.Direct3D12.ClearValue();
            clearValue.Format = _format;
            clearValue.Color  = new SharpDX.Mathematics.Interop.RawVector4(0.0f, 0.0f, 0.0f, 0.0f);

            for (int i = 0; i < FrameCount; i++)
            {
                desc.Format = _format;
                desc.Flags  = SharpDX.Direct3D12.ResourceFlags.AllowRenderTarget;

                _backBuffers[i] = _swapChain.GetBackBuffer <SharpDX.Direct3D12.Resource>(i);

                if (_sampleDesc.Count != 1)
                {
                    _backBuffersAA[i] = _dev.CreateCommittedResource(heap, flags, desc, SharpDX.Direct3D12.ResourceStates.ResolveSource, clearValue);
                    _dev.CreateRenderTargetView(_backBuffersAA[i], null, rtv);
                }
                else
                {
                    SharpDX.Direct3D12.RenderTargetViewDescription view = new SharpDX.Direct3D12.RenderTargetViewDescription();
                    view.Format               = _format;
                    view.Dimension            = SharpDX.Direct3D12.RenderTargetViewDimension.Texture2D;
                    view.Texture2D.MipSlice   = 0;
                    view.Texture2D.PlaneSlice = 0;

                    _dev.CreateRenderTargetView(_backBuffers[i], view, rtv);
                }

                rtv.Ptr += _sizeRTV;
            }

            // Stencil buffer
            clearValue.Format               = SharpDX.DXGI.Format.D24_UNorm_S8_UInt;
            clearValue.DepthStencil.Depth   = 0.0f;
            clearValue.DepthStencil.Stencil = 0;

            desc.Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt;
            desc.Flags  = SharpDX.Direct3D12.ResourceFlags.AllowDepthStencil;

            _stencilBuffer = _dev.CreateCommittedResource(heap, flags, desc, SharpDX.Direct3D12.ResourceStates.DepthWrite, clearValue);

            SharpDX.Direct3D12.CpuDescriptorHandle dsv = _heapDSV.CPUDescriptorHandleForHeapStart;
            _dev.CreateDepthStencilView(_stencilBuffer, null, dsv);

            // Viewport
            _viewport[0].TopLeftX = 0.0f;
            _viewport[0].TopLeftY = 0.0f;
            _viewport[0].Width    = desc.Width;
            _viewport[0].Height   = desc.Height;
            _viewport[0].MinDepth = 0.0f;
            _viewport[0].MaxDepth = 1.0f;
        }
 protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
 {
     // Creates the swap chain for the Window's Hwnd
     return(new SwapChain1(factory, device, Window.Handle, ref desc, CreateFullScreenDescription(), null));
 }
Exemple #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FormRenderer"/> class.
        /// </summary>
        /// <param name="form">The form.</param>
        public FormRenderer(Form1 form)
        {
            this.form = form;
#if DEBUG
            var creationFlags = D3D11.DeviceCreationFlags.BgraSupport | D3D11.DeviceCreationFlags.Debug;
            var debugFactory  = true;
#else
            var creationFlags = D3D11.DeviceCreationFlags.BgraSupport;
            var debugFactory  = false;
#endif

            this.device = new D3D11.Device(D3D.DriverType.Hardware, creationFlags);

#if DEBUG
            this.deviceDebug = new D3D11.DeviceDebug(this.device);
#endif

            using (var dxgiDevice = this.device.QueryInterface <DXGI.Device>())
            {
                using (var dxgiFactory = new DXGI.Factory2(debugFactory))
                {
                    var desc = new DXGI.SwapChainDescription1()
                    {
                        BufferCount       = 2,
                        AlphaMode         = DXGI.AlphaMode.Premultiplied,
                        SampleDescription = new DXGI.SampleDescription(1, 0),
                        Usage             = DXGI.Usage.RenderTargetOutput,
                        SwapEffect        = DXGI.SwapEffect.FlipDiscard,
                        Format            = DXGI.Format.B8G8R8A8_UNorm,
                        Width             = form.Width,
                        Height            = form.Height,
                    };

                    this.swapChain = new DXGI.SwapChain1(dxgiFactory, dxgiDevice, ref desc, null);

                    this.deviceComp        = new DComp.Device(dxgiDevice);
                    this.compositionTarget = DComp.Target.FromHwnd(this.deviceComp, form.Handle, true);

                    using (var visual = new DComp.Visual(this.deviceComp))
                    {
                        visual.Content = this.swapChain;
                        this.compositionTarget.Root = visual;
                    }

                    this.deviceComp.Commit();
                }
            }

            using (var device = this.device.QueryInterface <DXGI.Device>())
            {
                this.device2d = new D2D.Device(this.factory2d, device);
            }

            this.deviceContext2D = new D2D.DeviceContext(this.device2d, D2D.DeviceContextOptions.None)
            {
                DotsPerInch   = this.factory2d.DesktopDpi,
                AntialiasMode = D2D.AntialiasMode.PerPrimitive,
            };

            this.CreateResources();
        }
Exemple #19
0
        protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
        {
            // Creates a SwapChain from a CoreWindow pointer
            SwapChainFullScreenDescription scFullScreenDesc = new SwapChainFullScreenDescription()
            {
                Windowed    = deviceManager.Settings.IsWindowed,
                RefreshRate = new Rational(120, 1)
            };

#if DIRECTX11_1
            return(new SwapChain1(factory, device, form.Handle, ref desc));
#else
            return(factory.CreateSwapChainForHwnd(device, form.Handle, ref desc, scFullScreenDesc, null));
#endif
        }
        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;
            }
        }
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a new Direct3D hardware device and ask for Direct3D 11.2 support
            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;

            // We have to take into account pixel scaling; Windows Phone 8.1 uses virtual resolutions smaller than the physical screen size.
            float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f;

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

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

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

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

            // This event is fired when the application requests a new frame from the DirectX interop controls.
            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }
Exemple #22
0
        private void SwapChainPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var v = ApplicationView.GetForCurrentView();

            v.FullScreenSystemOverlayMode = FullScreenSystemOverlayMode.Minimal;
            v.TryEnterFullScreenMode();
#if DEBUG
            var debugLevel = D3D11.DeviceCreationFlags.Debug | D3D11.DeviceCreationFlags.BgraSupport;
#else
            var debugLevel = D3D11.DeviceCreationFlags.None | D3D11.DeviceCreationFlags.BgraSupport;
#endif
            using (D3D11.Device defaultDevice = new D3D11.Device(D3D.DriverType.Hardware, debugLevel))
            {
                this.device = defaultDevice.QueryInterface <D3D11.Device2>();
            }
            this.deviceContext = this.device.ImmediateContext2;
            float pixelScale = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f;

            DXGI.SwapChainDescription1 swapChainDescription = new DXGI.SwapChainDescription1()
            {
                AlphaMode         = DXGI.AlphaMode.Premultiplied,
                BufferCount       = 2,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Height            = (int)(this.SwapChainPanel.RenderSize.Height * pixelScale),
                Width             = (int)(this.SwapChainPanel.RenderSize.Width * pixelScale),
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Scaling           = DXGI.Scaling.Stretch,
                Stereo            = false,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
                Usage             = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput,
            };
            using (DXGI.Device3 dxgiDevice3 = this.device.QueryInterface <DXGI.Device3>())
            {
                using (DXGI.Factory3 dxgiFactory3 = dxgiDevice3.Adapter.GetParent <DXGI.Factory3>())
                {
                    using (DXGI.SwapChain1 swapChain1 = new DXGI.SwapChain1(dxgiFactory3, this.device, ref swapChainDescription))
                    {
                        this.swapChain = swapChain1.QueryInterface <DXGI.SwapChain2>();
                    }
                }
            }
            using (DXGI.ISwapChainPanelNative nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(this.SwapChainPanel))
            {
                nativeObject.SwapChain = this.swapChain;
            }
            this.backBufferTexture = D3D11.Texture2D.FromSwapChain <D3D11.Texture2D>(this.swapChain, 0);
            this.backBufferView    = new D3D11.RenderTargetView(this.device, this.backBufferTexture);
            isDXInitialized        = true;
            tw = new TextWirter(device, swapChain, Color.Black, DisplayInformation.GetForCurrentView().LogicalDpi);
//            #region D2D

//#if DEBUG
//            var debug = SharpDX.Direct2D1.DebugLevel.Error;
//#else
//            var debug = SharpDX.Direct2D1.DebugLevel.None;
//#endif

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

//            BitmapProperties1 properties = new BitmapProperties1(
//                new PixelFormat(
//                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
//                    SharpDX.Direct2D1.AlphaMode.Premultiplied),
//                DisplayInformation.GetForCurrentView().LogicalDpi,
//                DisplayInformation.GetForCurrentView().LogicalDpi,
//                BitmapOptions.Target | BitmapOptions.CannotDraw);
//            DXGI.Surface backBuffer = swapChain.GetBackBuffer<DXGI.Surface>(0);
//            d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);
//            SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory();
//            textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);
//            textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
//            textLayout2 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with no snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
//            layoutY = 0.0f;
//            backgroundBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.White);
//            textBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.Black);
//            #endregion

            CompositionTarget.Rendering    += CompositionTarget_Rendering;
            Application.Current.Suspending += Application_Suspending;
        }
Exemple #23
0
        public SharpRender(RenderForm form)
        {
            this.form = form;
            var adapters = new DXGI.Factory1().Adapters;

            DXGI.Adapter myadapter = null;
            for (int i = 0; i < adapters.Length; ++i)
            {
                Logger.Log(string.Format("Adapter Found: [{0}] " +
                                         "{1}\tDeviceId={5}" +
                                         "{1}\tLuid={6}" +
                                         "{1}\tVendorId={10}" +
                                         "{1}\tSubsystemId={9}" +
                                         "{1}\tDescription={4}" +
                                         "{1}\tRevision={7}" +
                                         "{1}\tDedicatedSystemMemory={2}" +
                                         "{1}\tDedicatedVideoMemory={3}" +
                                         "{1}\tSharedSystemMemory={8}" +
                                         "",
                                         i, Environment.NewLine,
                                         adapters[i].Description.DedicatedSystemMemory,
                                         adapters[i].Description.DedicatedVideoMemory, adapters[i].Description.Description,
                                         adapters[i].Description.DeviceId, adapters[i].Description.Luid,
                                         adapters[i].Description.Revision, adapters[i].Description.SharedSystemMemory,
                                         adapters[i].Description.SubsystemId, adapters[i].Description.VendorId));
                var outputs = adapters[i].Outputs;
                for (int j = 0; j < outputs.Length; ++j)
                {
                    Logger.Log(string.Format("Output Found: [{0},{1}]" +
                                             "{2}\tDeviceName={4}" +
                                             "{2}\tIsAttachedToDesktop={5}" +
                                             "{2}\tMonitorHandle={6}" +
                                             "{2}\tDesktopBounds={3}" +
                                             "{2}\tRotation={7}" +
                                             "",
                                             i, j, Environment.NewLine,
                                             (Rectangle)outputs[j].Description.DesktopBounds,
                                             outputs[j].Description.DeviceName,
                                             outputs[j].Description.IsAttachedToDesktop,
                                             outputs[j].Description.MonitorHandle,
                                             outputs[j].Description.Rotation));
                }
                if (outputs.Length > 0 && myadapter == null)
                {
                    myadapter = adapters[i];
                }
            }
            d3device = new Direct3D11.Device(
                myadapter,
                Direct3D11.DeviceCreationFlags.BgraSupport);
            //SharpDX.Direct3D.DriverType.Hardware,
            //Direct3D11.DeviceCreationFlags.BgraSupport |
            //Direct3D11.DeviceCreationFlags.Debug);
            defDevice    = d3device.QueryInterface <Direct3D11.Device1>();
            dxgiDevice2  = defDevice.QueryInterface <DXGI.Device2>();
            dxgiAdapter  = dxgiDevice2.Adapter;
            dxgiFactory2 = dxgiAdapter.GetParent <DXGI.Factory2>();
            var scDescription = new DXGI.SwapChainDescription1()
            {
                Width             = 0,
                Height            = 0,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Usage             = DXGI.Usage.RenderTargetOutput,
                BufferCount       = 2,
                Scaling           = DXGI.Scaling.None,
                SwapEffect        = DXGI.SwapEffect.FlipSequential
            };

            swapChain = new DXGI.SwapChain1(dxgiFactory2, defDevice, form.Handle,
                                            ref scDescription, null, null);
            d2dDevice  = new Direct2D1.Device(dxgiDevice2);
            d2dContext = new Direct2D1.DeviceContext(d2dDevice,
                                                     Direct2D1.DeviceContextOptions.None);
            fac = new Direct2D1.Factory(Direct2D1.FactoryType.SingleThreaded);
            var dpi          = fac.DesktopDpi;
            var bMProperties = new Direct2D1.BitmapProperties1(
                new Direct2D1.PixelFormat(DXGI.Format.B8G8R8A8_UNorm,
                                          Direct2D1.AlphaMode.Premultiplied),
                dpi.Width, dpi.Height,
                Direct2D1.BitmapOptions.CannotDraw | Direct2D1.BitmapOptions.Target);

            bb                = swapChain.GetBackBuffer <DXGI.Surface>(0);
            target            = new Direct2D1.Bitmap1(d2dContext, bb, bMProperties);
            d2dContext.Target = target;
            wrFactory         = new DirectWrite.Factory();

            brush = new Direct2D1.SolidColorBrush(d2dContext, c(Color.White));
        }
        protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc)
        {
            // Creates the swap chain for XAML composition
            var swapChain = new SwapChain1(factory, device, ref desc);

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

            // Returns the new swap chain
            return(swapChain);
        }
Exemple #25
0
 protected override SharpDX.DXGI.SwapChain1 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))
         return(new SwapChain1(factory, device, comWindow, ref desc));
 }
 /// <summary>
 /// Creates the swap chain.
 /// </summary>
 /// <param name="factory">The DXGI factory</param>
 /// <param name="device">The D3D11 device</param>
 /// <param name="desc">The swap chain description</param>
 /// <returns>An instance of swap chain</returns>
 protected abstract SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc);
 /// <summary>
 /// Creates the swap chain description.
 /// </summary>
 /// <returns>A swap chain description</returns>
 /// <remarks>
 /// This method can be overloaded in order to modify default parameters.
 /// </remarks>
 protected virtual SharpDX.DXGI.SwapChainDescription1 CreateSwapChainDescription()
 {
     // SwapChain description
     var desc = new SharpDX.DXGI.SwapChainDescription1()
     {
         Width = Width,
         Height = Height,
         // B8G8R8A8_UNorm gives us better performance
         Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
         Stereo = false,
         SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
         Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
         BufferCount = 1,
         Scaling = SharpDX.DXGI.Scaling.Stretch,
         SwapEffect = SharpDX.DXGI.SwapEffect.Discard,
         Flags = SwapChainFlags.AllowModeSwitch
     };
     return desc;
 }
        private void SwapChainPanel_OnLoaded(object sender, RoutedEventArgs e)
        {
            using (var defDevice = new D3D.Device(DriverType.Hardware, D3D.DeviceCreationFlags.Debug))
            {
                _device = defDevice.QueryInterface <D3D.Device3>();
            }
            _context = _device.ImmediateContext3;

            var pixelScale    = DisplayInformation.GetForCurrentView().LogicalDpi / 96.0f;
            var swapChainDesc = new DXGI.SwapChainDescription1()
            {
                AlphaMode         = DXGI.AlphaMode.Premultiplied,
                BufferCount       = 2,
                Flags             = DXGI.SwapChainFlags.None,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Width             = (int)(panel.RenderSize.Width * pixelScale),
                Height            = (int)(panel.RenderSize.Height * pixelScale),
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Scaling           = DXGI.Scaling.Stretch,
                Stereo            = false,
                SwapEffect        = DXGI.SwapEffect.FlipSequential,
                Usage             = DXGI.Usage.BackBuffer | DXGI.Usage.RenderTargetOutput
            };

            using (var dxgiDevice = _device.QueryInterface <DXGI.Device3>())
            {
                var factory = dxgiDevice.Adapter.GetParent <DXGI.Factory4>();
                using (var tmpSwapChain = new DXGI.SwapChain1(factory, _device, ref swapChainDesc))
                {
                    _swapChain = tmpSwapChain.QueryInterface <DXGI.SwapChain3>();
                }
            }

            using (var nativeObject = ComObject.As <DXGI.ISwapChainPanelNative>(panel))
            {
                nativeObject.SwapChain = _swapChain;
            }

            using (var depthBuffer = new D3D.Texture2D(_device, new D3D.Texture2DDescription()
            {
                Format = DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = swapChainDesc.Width,
                Height = swapChainDesc.Height,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                BindFlags = D3D.BindFlags.DepthStencil,
            }))
            {
                _depthStencilView = new D3D.DepthStencilView(_device, depthBuffer, new D3D.DepthStencilViewDescription()
                {
                    Dimension = D3D.DepthStencilViewDimension.Texture2D
                });
            }

            _backBuffer = D3D.Resource.FromSwapChain <D3D.Texture2D>(_swapChain, 0);
            _renderView = new D3D.RenderTargetView1(_device, _backBuffer);

            var viewport = new ViewportF(0, 0, (float)panel.RenderSize.Width, (float)panel.RenderSize.Height, 0.0f, 1.0f);

            _context.Rasterizer.SetViewport(viewport);

            ShaderBytecode shaderBytecode;

            using (shaderBytecode = ShaderBytecode.CompileFromFile("shaders.hlsl", "vs", "vs_5_0", ShaderFlags.Debug))
            {
                _vertexShader = new D3D.VertexShader(_device, shaderBytecode);
            }

            using (var byteCode = ShaderBytecode.CompileFromFile(@"shaders.hlsl", "ps", "ps_5_0", ShaderFlags.Debug))
            {
                _pixelShader = new D3D.PixelShader(_device, byteCode);
            }

            D3D.InputElement[] inputElements =
            {
                new D3D.InputElement("POSITION", 0, DXGI.Format.R32G32B32A32_Float, 0, 0),
            };
            _inputLayout = new D3D.InputLayout(_device, shaderBytecode, inputElements);

            _vertices = new[]
            {
                new Vector4(-0.5f, 0.0f, 0.5f, 1.0f),
                new Vector4(0.0f, 0.5f, 0.5f, 1.0f),
                new Vector4(0.5f, 0.0f, 0.5f, 1.0f),
            };
            _vertexBuffer  = D3D.Buffer.Create(_device, D3D.BindFlags.VertexBuffer, _vertices);
            _vertexBinding = new D3D.VertexBufferBinding(_vertexBuffer, Utilities.SizeOf <Vector4>(), 0);

            _constantBuffer = new SharpDX.Direct3D11.Buffer(
                _device,
                Utilities.SizeOf <SharpDX.Matrix>(),
                D3D.ResourceUsage.Default,
                D3D.BindFlags.ConstantBuffer,
                D3D.CpuAccessFlags.None,
                D3D.ResourceOptionFlags.None,
                0);

            _timer = new Stopwatch();
            _timer.Start();

            CompositionTarget.Rendering += CompositionTarget_Rendering;
        }
Exemple #29
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();
            }
        }
Exemple #30
-1
 /// <summary>
 /// Creates the swap chain description.
 /// </summary>
 /// <returns>A swap chain description</returns>
 /// <remarks>
 /// This method can be overloaded in order to modify default parameters.
 /// </remarks>
 protected virtual SharpDX.DXGI.SwapChainDescription1 CreateSwapChainDescription()
 {
     // SwapChain description
     var desc = new SharpDX.DXGI.SwapChainDescription1()
     {
         // Automatic sizing
         Width = Width,
         Height = Height,
         Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
         Stereo = false,
         SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
         Usage = SharpDX.DXGI.Usage.BackBuffer | SharpDX.DXGI.Usage.RenderTargetOutput,
         // Use two buffers to enable flip effect.
         BufferCount = 2,
         Scaling = SharpDX.DXGI.Scaling.None,
         SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential,
     };
     return desc;
 }