public Direct2DCanvas() { int width = 1024; int height = 1024; SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device( SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); // Query the default device for the supported device and context interfaces. var device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1> (); // Query for the adapter and more advanced DXGI objects. SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2> (); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2> (); var swapChainDescription = new SharpDX.DXGI.SwapChainDescription1 { Width = width, Height = height, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Stereo = false, Usage = SharpDX.DXGI.Usage.RenderTargetOutput, BufferCount = 2, Scaling = SharpDX.DXGI.Scaling.Stretch, SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential, Flags = SharpDX.DXGI.SwapChainFlags.None, }; swapChainDescription.SampleDescription.Count = 1; swapChainDescription.SampleDescription.Quality = 0; swapChain = dxgiFactory2.CreateSwapChainForComposition(dxgiDevice2, ref swapChainDescription, null); swapChain.BackgroundColor = new Color4(1, 0, 0, 0); var native = SharpDX.ComObject.QueryInterface <SharpDX.DXGI.ISwapChainBackgroundPanelNative> (this); native.SwapChain = swapChain; g = new Direct2DGraphics(swapChain); _drawTimer = new WindowsDrawTimer { Continuous = true, ShouldDrawFunc = () => Content != null && ActualWidth > 0 && ActualHeight > 0, DrawFunc = Draw, }; Unloaded += HandleUnloaded; Loaded += HandleLoaded; LayoutUpdated += HandleLayoutUpdated; }
private void Initialize(IntPtr devicePointer) { FeatureLevel[] levels = new FeatureLevel[] { #if DIRECTX11_1 FeatureLevel.Level_11_1, #endif FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0, FeatureLevel.Level_9_3 }; Device dev; if (devicePointer != IntPtr.Zero) { dev = new SharpDX.Direct3D11.Device(devicePointer); } else if (adapterindex > 0) { SharpDX.DXGI.Factory f = new SharpDX.DXGI.Factory1(); SharpDX.DXGI.Adapter a = f.GetAdapter(adapterindex); dev = new Device(a, this.creationflags, levels); f.Dispose(); a.Dispose(); } else { dev = new Device(DriverType.Hardware, this.creationflags, levels); } #if DIRECTX11_1 this.Device = dev.QueryInterface <DirectXDevice>(); Marshal.Release(this.Device.NativePointer); #else this.Device = dev; #endif DXGIDevice dxgidevice = this.Device.QueryInterface <DXGIDevice>(); Marshal.Release(this.Device.NativePointer); this.Adapter = dxgidevice.Adapter.QueryInterface <DXGIAdapter>(); Marshal.Release(dxgidevice.Adapter.NativePointer); this.Factory = this.Adapter.GetParent <DXGIFactory>(); Marshal.Release(this.Adapter.NativePointer); this.OnLoad(); }
private void OnLoaded(object sender, RoutedEventArgs e) { ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia | ManipulationModes.Scale; tileBackground.ImageSource = "Assets/bgline.JPG"; onePointWait.Visibility = Visibility.Collapsed; twoPointWait.Visibility = Visibility.Collapsed; double val = -Math.PI / 2; foreach (UIElement child in colorSelect.Children) { child.SetValue(Canvas.LeftProperty, Math.Cos(val) * 70); child.SetValue(Canvas.TopProperty, Math.Sin(val) * 70); val += Math.PI / 5; } var debugLevel = SharpDX.Direct2D1.DebugLevel.None; d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel); var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; using (var defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, creationFlags)) d3dDevice = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); d3dContext = d3dDevice.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>(); using (dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>()) d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); DisplayProperties.LogicalDpiChanged += LogicalDpiChanged; }
private void OnLoaded(object sender, RoutedEventArgs e) { ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY | ManipulationModes.TranslateInertia | ManipulationModes.Scale; tileBackground.ImageSource = "Assets/bgline.JPG"; onePointWait.Visibility = Visibility.Collapsed; twoPointWait.Visibility = Visibility.Collapsed; double val = -Math.PI / 2; foreach (UIElement child in colorSelect.Children) { child.SetValue(Canvas.LeftProperty, Math.Cos(val) * 70); child.SetValue(Canvas.TopProperty, Math.Sin(val) * 70); val += Math.PI / 5; } var debugLevel = SharpDX.Direct2D1.DebugLevel.None; d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel); var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; using (var defaultDevice = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, creationFlags)) d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>(); d3dContext = d3dDevice.ImmediateContext.QueryInterface<SharpDX.Direct3D11.DeviceContext1>(); using (dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); DisplayProperties.LogicalDpiChanged += LogicalDpiChanged; }
void CreateDevice() { // d3d11 var flags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; #if DEBUG // error on Windows10(Geforce435M) //flags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug; #endif using (var device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware , flags , SharpDX.Direct3D.FeatureLevel.Level_10_0 )) { D3DDevice = device.QueryInterfaceOrNull<SharpDX.Direct3D11.Device1>(); } // DXGIDevice DXGIDevice = D3DDevice.QueryInterface<SharpDX.DXGI.Device2>(); DXGIDevice.MaximumFrameLatency = 1; }
// Protected implementation of Dispose pattern. protected virtual void Dispose(bool disposing) { if (disposed) return; if (disposing) { if (SwapChain != null) { SwapChain.Dispose(); SwapChain = null; } // Free any other managed objects here. if (DXGIDevice != null) { DXGIDevice.Dispose(); DXGIDevice = null; } if (D3DDevice != null) { D3DDevice.Dispose(); D3DDevice = null; } } // Free any unmanaged objects here. // disposed = true; }