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); } }
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; }
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; }
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; }