public void InitializeD3D11() { DXInteropHelper.InitializeD3D(D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, out d3d11Device, D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT); DXInteropHelper.InitializeD2D(d3d11Device, out d2dDeviceContext); dxgiDevice = DXInteropHelper.GetDXGIDevice(d3d11Device); dxgiSwapChain = DXInteropHelper.CreateSwapChainForComposition(d3d11Device, (int)Window.Current.Bounds.Width, (int)Window.Current.Bounds.Height, true); }
public static void SetBackgroundColor(this IDXGISwapChain1 swapChain, _D3DCOLORVALUE value) { if (swapChain == null) { throw new ArgumentNullException(nameof(swapChain)); } swapChain.SetBackgroundColor(ref value).ThrowOnError(); }
public static DXGI_SWAP_CHAIN_DESC1 GetDesc1(this IDXGISwapChain1 obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } obj.GetDesc1(out var value); return(value); }
public static DXGI_SWAP_CHAIN_DESC1 GetDesc1(this IDXGISwapChain1 input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } input.GetDesc1(out var value).ThrowOnError(); return(value); }
public void InitializeDevice(IntPtr windowHandle) { D3Device = DirectXTools.CreateD3Device(); { RenderTarget = DirectXTools.CreateRenderTarget(Direct2DFactory, D3Device); _solidBrush = RenderTarget.CreateSolidColorBrush(Color4.Black); SwapChain = DirectXTools.CreateSwapChainForHwnd(D3Device, windowHandle); DirectXTools.CreateDeviceSwapChainBitmap(SwapChain, RenderTarget); Bitmaps.SetRenderTarget(RenderTarget); TextLayouts.SetRenderTarget(RenderTarget); } }
public void InitializeDeviceGdiCompatible(IntPtr windowHandle, int width, int height) { D3Device = DirectXTools.CreateD3Device(); { RenderTarget = DirectXTools.CreateRenderTarget(Direct2DFactory, D3Device); _solidBrush = RenderTarget.CreateSolidColorBrush(Color4.Black); SwapChain = DirectXTools.CreateSwapChainForHwnd(D3Device, windowHandle); //DirectXTools.CreateDeviceSwapChainBitmap(SwapChain, RenderTarget); DirectXTools.CreateDeviceContextCPUBitmap(RenderTarget, width, height); Bitmaps.SetRenderTarget(RenderTarget); TextLayouts.SetRenderTarget(RenderTarget); } }
public static void CreateDeviceSwapChainBitmap( IDXGISwapChain1 swapChain, ID2D1DeviceContext target) { using (IDXGISurface surface = swapChain.GetBuffer <IDXGISurface>(0)) { var props = new BitmapProperties1 { BitmapOptions = BitmapOptions.Target | BitmapOptions.CannotDraw, PixelFormat = new Vortice.DCommon.PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Ignore) }; using (var bitmap = target.CreateBitmapFromDxgiSurface(surface, props)) { target.Target = bitmap; } } }
public IDXGISwapChain3 CreateDXGISwapChain(IDXGIFactory4 pFactory, IntPtr hwnd, int width, int height, Vortice.DXGI.Format format, ID3D12CommandQueue pCommandQueue) { SwapChainDescription1 swapChainDesc = new SwapChainDescription1(); swapChainDesc.BufferCount = kDefaultSwapChainBuffers; swapChainDesc.Width = width; swapChainDesc.Height = height; swapChainDesc.Format = format; swapChainDesc.Usage = Usage.RenderTargetOutput; swapChainDesc.SwapEffect = SwapEffect.FlipDiscard; swapChainDesc.SampleDescription = new SampleDescription(1, 0); // CreateSwapChainForHwnd() doesn't accept IDXGISwapChain3 (Why MS? Why?) IDXGISwapChain1 pSwapChain = pFactory.CreateSwapChainForHwnd(pCommandQueue, hwnd, swapChainDesc); IDXGISwapChain3 pSwapChain3 = pSwapChain.QueryInterface <IDXGISwapChain3>(); return(pSwapChain3); }
// scbp user is expected to listen for the window size/orientation changes and always create 2x1 swapchain for Dflip. void Window_SizeChanged(object sender, WindowSizeChangedEventArgs e) { //DisplayProperties.CurrentOrienation takes sometime to return correct orientation value. As a workaround, delaying this by .5 sec. DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(500) }; timer.Tick += (s1, e1) => { dxgiSwapChain = DXInteropHelper.CreateSwapChainForComposition(d3d11Device, (int)e.Size.Width, (int)e.Size.Height, true); customSCBP.SetSwapChain(dxgiSwapChain); timer.Stop(); }; timer.Start(); }
public void Resize(int width, int height) { WaitForGpu(); this.width = Math.Max(width, 1); this.height = Math.Max(height, 1); if (swapChain == null) { SwapChainDescription1 swapChainDescription; swapChainDescription.Width = width; swapChainDescription.Height = height; swapChainDescription.Format = swapChainFormat; swapChainDescription.Stereo = false; swapChainDescription.SampleDescription.Count = 1; swapChainDescription.SampleDescription.Quality = 0; swapChainDescription.Usage = Usage.RenderTargetOutput; swapChainDescription.BufferCount = bufferCount; swapChainDescription.SwapEffect = SwapEffect.FlipDiscard; swapChainDescription.Flags = SwapChainFlags.AllowTearing; swapChainDescription.Scaling = Scaling.Stretch; swapChainDescription.AlphaMode = AlphaMode.Ignore; IDXGISwapChain1 swapChain1 = dxgiFactory.CreateSwapChainForHwnd(commandQueue, hwnd, swapChainDescription); swapChain = swapChain1.QueryInterface <IDXGISwapChain3>(); swapChain1.Dispose(); } else { foreach (var screenResource in screenResources) { screenResource.Dispose(); } ThrowIfFailed(swapChain.ResizeBuffers(bufferCount, width, height, swapChainFormat, SwapChainFlags.AllowTearing)); } screenResources = new List <ID3D12Resource>(); CpuDescriptorHandle handle = rtvHeap.GetCPUDescriptorHandleForHeapStart(); for (int i = 0; i < bufferCount; i++) { ThrowIfFailed(swapChain.GetBuffer(i, out ID3D12Resource res)); screenResources.Add(res); device.CreateRenderTargetView(res, null, handle); handle.Ptr += rtvHeapIncrementSize; } }
private static IDXGISwapChain3 CreateSwapChain(GraphicsDevice device, PresentationParameters presentationParameters, IntPtr windowHandle) { SwapChainDescription1 swapChainDescription = new SwapChainDescription1 { Width = presentationParameters.BackBufferWidth, Height = presentationParameters.BackBufferHeight, SampleDescription = new SampleDescription(1, 0), Stereo = presentationParameters.Stereo, Usage = Usage.RenderTargetOutput, BufferCount = BufferCount, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.FlipSequential, Format = (Format)presentationParameters.BackBufferFormat, Flags = SwapChainFlags.None, AlphaMode = AlphaMode.Unspecified }; DXGI.CreateDXGIFactory2(false, out IDXGIFactory2 factory); using IDXGISwapChain1 tempSwapChain = factory.CreateSwapChainForHwnd(device.NativeDirectCommandQueue, windowHandle, swapChainDescription); factory.Dispose(); return(tempSwapChain.QueryInterface <IDXGISwapChain3>()); }
private static IDXGISwapChain3 CreateSwapChain(GraphicsDevice device, PresentationParameters presentationParameters, SwapChainPanel swapChainPanel) { SwapChainDescription1 swapChainDescription = new SwapChainDescription1 { Width = presentationParameters.BackBufferWidth, Height = presentationParameters.BackBufferHeight, SampleDescription = new Vortice.DXGI.SampleDescription(1, 0), Stereo = presentationParameters.Stereo, Usage = Usage.RenderTargetOutput, BufferCount = BufferCount, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.FlipSequential, Format = (Format)presentationParameters.BackBufferFormat.ToNonSRgb(), Flags = SwapChainFlags.None, AlphaMode = AlphaMode.Unspecified }; swapChainDescription.AlphaMode = AlphaMode.Premultiplied; DXGI.CreateDXGIFactory2(false, out IDXGIFactory2 factory); Direct3DInterop.ISwapChainPanelNative nativePanel = (Direct3DInterop.ISwapChainPanelNative)swapChainPanel; using IDXGISwapChain1 tempSwapChain = factory.CreateSwapChainForComposition(device.DirectCommandQueue.NativeCommandQueue, swapChainDescription); factory.Dispose(); IDXGISwapChain3 swapChain = tempSwapChain.QueryInterface <IDXGISwapChain3>(); nativePanel.SetSwapChain(swapChain.NativePointer); swapChain.MatrixTransform = new Matrix3x2 { M11 = 1.0f / swapChainPanel.CompositionScaleX, M22 = 1.0f / swapChainPanel.CompositionScaleY }; return(swapChain); }
private D3D11GraphicsDevice(Window?window, Size size) { Window = window; Size = size; if (CreateDXGIFactory1(out Factory).Failure) { throw new InvalidOperationException("Cannot create IDXGIFactory1"); } using (IDXGIAdapter1? adapter = GetHardwareAdapter()) { DeviceCreationFlags creationFlags = DeviceCreationFlags.BgraSupport; #if DEBUG if (SdkLayersAvailable()) { creationFlags |= DeviceCreationFlags.Debug; } #endif if (D3D11CreateDevice( adapter !, DriverType.Unknown, creationFlags, s_featureLevels, out ID3D11Device tempDevice, out FeatureLevel, out ID3D11DeviceContext tempContext).Failure) { // If the initialization fails, fall back to the WARP device. // For more information on WARP, see: // http://go.microsoft.com/fwlink/?LinkId=286690 D3D11CreateDevice( null, DriverType.Warp, creationFlags, s_featureLevels, out tempDevice, out FeatureLevel, out tempContext).CheckError(); } Device = tempDevice.QueryInterface <ID3D11Device1>(); DeviceContext = tempContext.QueryInterface <ID3D11DeviceContext1>(); tempContext.Dispose(); tempDevice.Dispose(); } if (window != null) { IntPtr hwnd = window.Handle; SwapChainDescription1 swapChainDescription = new SwapChainDescription1() { Width = window.ClientSize.Width, Height = window.ClientSize.Height, Format = Format.R8G8B8A8_UNorm, BufferCount = FrameCount, Usage = Vortice.DXGI.Usage.RenderTargetOutput, SampleDescription = new SampleDescription(1, 0), Scaling = Scaling.Stretch, SwapEffect = SwapEffect.FlipDiscard, AlphaMode = AlphaMode.Ignore }; SwapChainFullscreenDescription fullscreenDescription = new SwapChainFullscreenDescription { Windowed = true }; SwapChain = Factory.CreateSwapChainForHwnd(Device, hwnd, swapChainDescription, fullscreenDescription); Factory.MakeWindowAssociation(hwnd, WindowAssociationFlags.IgnoreAltEnter); BackBufferTexture = SwapChain.GetBuffer <ID3D11Texture2D>(0); RenderTargetView = Device.CreateRenderTargetView(BackBufferTexture); } else { // Create offscreen texture OffscreenTexture = Device.CreateTexture2D(new Texture2DDescription(Format.R8G8B8A8_UNorm, Size.Width, Size.Height, 1, 1, BindFlags.ShaderResource | BindFlags.RenderTarget)); RenderTargetView = Device.CreateRenderTargetView(OffscreenTexture); } }
private SwapChainPanelInterop CreateSCPContent(ElementCompositeMode compositeMode, DXContentType dxContentType, float[] color, Rect?rect, int height = 200) { double scpWidth = scrollViewer.Width; double scpHeight = height; SwapChainPanel scp = new SwapChainPanel() { Width = scpWidth, Height = scpHeight, CompositeMode = compositeMode }; SwapChainPanelInterop scpInterop = scp; IDXGISwapChain1 swapChain = DXInteropHelper.CreateSwapChainForComposition(d3d11Device, (int)scpWidth, (int)scpHeight, false); scpInterop.SetSwapChain(swapChain); DrawDXContent(dxContentType, scpInterop, color, rect); scpInterop.CopyBuffers(1, 0); float compositionScaleX = 1; float compositionScaleY = 1; scp.CompositionScaleChanged += (s, e) => { compositionScaleX = scp.CompositionScaleX; compositionScaleY = scp.CompositionScaleY; try { scpInterop.ResizeBuffers((int)scp.Width, (int)scp.Height); } catch (System.Runtime.InteropServices.COMException ex) { if (ex.HResult == unchecked ((int)0x887A0005)) /* DXGI_ERROR_DEVICE_REMOVED */ { swapChain = DXInteropHelper.CreateSwapChainForComposition(d3d11Device, (int)scp.Width, (int)scp.Height, false); scpInterop.SetSwapChain(swapChain); } else { throw ex; } } DrawDXContent(dxContentType, scpInterop, color, rect); scpInterop.CopyBuffers(1, 0); }; StackPanel scpStackPanel = new StackPanel() { Orientation = Orientation.Vertical }; StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal }; TextBox scpWidthTB = new TextBox() { Height = 60, Header = "SCPWidth" }; TextBox scpHeightTB = new TextBox() { Height = 60, Header = "SCPHeight" }; Binding binding = new Binding() { Source = scp, Path = new PropertyPath("Width"), Mode = BindingMode.TwoWay }; scpWidthTB.SetBinding(TextBox.TextProperty, binding); binding = new Binding() { Source = scp, Path = new PropertyPath("Height"), Mode = BindingMode.TwoWay }; scpHeightTB.SetBinding(TextBox.TextProperty, binding); sp.Children.Add(scpWidthTB); sp.Children.Add(scpHeightTB); scpStackPanel.Children.Add(sp); StackPanel sp1 = new StackPanel() { Orientation = Orientation.Horizontal }; sp1.Children.Add(new TextBlock() { Text = "CoreInput" }); CheckBox coreinputEnabledCB = new CheckBox() { IsChecked = true, IsThreeState = false }; sp1.Children.Add(coreinputEnabledCB); sp1.Children.Add(new TextBlock() { Text = "Touch" }); CheckBox coreinputTouchCB = new CheckBox() { IsChecked = true, IsThreeState = false }; sp1.Children.Add(coreinputTouchCB); sp1.Children.Add(new TextBlock() { Text = "Mouse" }); CheckBox coreinputMouseCB = new CheckBox() { IsChecked = true, IsThreeState = false }; sp1.Children.Add(coreinputMouseCB); scpStackPanel.Children.Add(sp1); scp.Children.Add(scpStackPanel); bool enableTouchCoreInput = true; bool enableMouseCoreInput = true; CoreDispatcher coreInputDispatcher = null; CoreDispatcher xamlDispatcher = Window.Current.Dispatcher; Action coreInputThreadAction = () => { CoreInputDeviceTypes deviceTypes = CoreInputDeviceTypes.Pen; if (enableTouchCoreInput) { deviceTypes |= CoreInputDeviceTypes.Touch; } if (enableMouseCoreInput) { deviceTypes |= CoreInputDeviceTypes.Mouse; } CoreIndependentInputSource coreInput = scp.CreateCoreIndependentInputSource(deviceTypes); coreInputDispatcher = coreInput.Dispatcher; xamlDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { coreinputEnabledCB.Unchecked += (s4, e4) => { coreInputDispatcher.RunAsync(CoreDispatcherPriority.High, () => { coreInput.IsInputEnabled = false; }); }; coreinputEnabledCB.Checked += (s4, e4) => { coreInputDispatcher.RunAsync(CoreDispatcherPriority.High, () => { coreInput.IsInputEnabled = true; }); }; }); coreInput.PointerPressed += (s, e) => { scpInterop.UpdateSurfaceWithD2DEllipse(new Point(e.CurrentPoint.Position.X * compositionScaleX, e.CurrentPoint.Position.Y * compositionScaleY), 10.0f, new float[] { 1f, 0f, 1f, 1f }, true /*copyBuffers*/); }; coreInput.PointerMoved += (s1, e1) => { scpInterop.UpdateSurfaceWithD2DEllipse(new Point(e1.CurrentPoint.Position.X * compositionScaleX, e1.CurrentPoint.Position.Y * compositionScaleY), 10.0f, new float[] { 0f, 0f, 1f, 1f }, true /*copyBuffers*/); }; coreInput.PointerReleased += (s2, e2) => { scpInterop.UpdateSurfaceWithD2DEllipse(new Point(e2.CurrentPoint.Position.X * compositionScaleX, e2.CurrentPoint.Position.Y * compositionScaleY), 10.0f, new float[] { 0f, 0f, 1f, 1f }, true /*copyBuffers*/); // Marking this unhandled so that AppBar can be invoked by letting the right click go through CoreInput which then raises WM_ContextMenu // that calls handles in xaml framework which marshalles the call to the UIthread to toggle AppBar. e2.Handled = false; }; coreInputDispatcher.ProcessEvents(CoreProcessEventsOption.ProcessUntilQuit); }; Action startCoreInputThread = () => { Windows.System.Threading.ThreadPool.RunAsync((asyncAction) => { if (coreInputDispatcher != null) { coreInputDispatcher.StopProcessEvents(); coreInputDispatcher = null; } coreInputThreadAction(); }, WorkItemPriority.High); }; startCoreInputThread(); coreinputTouchCB.Checked += (x, y) => { enableTouchCoreInput = true; startCoreInputThread(); }; coreinputTouchCB.Unchecked += (x, y) => { enableTouchCoreInput = false; startCoreInputThread(); }; coreinputMouseCB.Checked += (x, y) => { enableMouseCoreInput = true; scp.CompositeMode = (ElementCompositeMode)(((int)scp.CompositeMode + 1) % 4); startCoreInputThread(); }; coreinputMouseCB.Unchecked += (x, y) => { enableMouseCoreInput = false; startCoreInputThread(); }; return(scpInterop); }