internal static void InitializeDirect2D() { lock (s_initLock) { if (s_initialized) { return; } #if DEBUG try { Direct2D1Factory = new SharpDX.Direct2D1.Factory1( SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.Error); } catch { // } #endif if (Direct2D1Factory == null) { Direct2D1Factory = new SharpDX.Direct2D1.Factory1( SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.None); } using (var factory = new SharpDX.DirectWrite.Factory()) { DirectWriteFactory = factory.QueryInterface <SharpDX.DirectWrite.Factory1>(); } ImagingFactory = new SharpDX.WIC.ImagingFactory(); var featureLevels = new[] { SharpDX.Direct3D.FeatureLevel.Level_11_1, SharpDX.Direct3D.FeatureLevel.Level_11_0, SharpDX.Direct3D.FeatureLevel.Level_10_1, SharpDX.Direct3D.FeatureLevel.Level_10_0, SharpDX.Direct3D.FeatureLevel.Level_9_3, SharpDX.Direct3D.FeatureLevel.Level_9_2, SharpDX.Direct3D.FeatureLevel.Level_9_1, }; Direct3D11Device = new SharpDX.Direct3D11.Device( SharpDX.Direct3D.DriverType.Hardware, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport, featureLevels); DxgiDevice = Direct3D11Device.QueryInterface <SharpDX.DXGI.Device1>(); Direct2D1Device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice); s_initialized = true; } }
/// <summary> /// Create device manager objects /// </summary> /// <remarks> /// This method is called at the initialization of this instance. /// </remarks> protected virtual void CreateInstance() { // Dispose previous references and set to null RemoveAndDispose(ref d3dDevice); RemoveAndDispose(ref d3dContext); RemoveAndDispose(ref d2dDevice); RemoveAndDispose(ref d2dContext); RemoveAndDispose(ref d2dFactory); RemoveAndDispose(ref dwriteFactory); RemoveAndDispose(ref wicFactory); #region Create Direct3D 11.1 device and retrieve device context // BGRA performs better especially with Direct2D software render targets var creationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport; #if DEBUG // Enble D3D device debug layer creationFlags |= SharpDX.Direct3D11.DeviceCreationFlags.Debug; #endif // Retrieve the Direct3D 11.1 device and device context using (var device = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags, D3DFeatureLevel)) { d3dDevice = ToDispose(device.QueryInterface <Device1>()); } // Get Direct3D 11.1 immediate context d3dContext = ToDispose(d3dDevice.ImmediateContext.QueryInterface <DeviceContext1>()); #endregion #region Create Direct2D device and context #if DEBUG var debugLevel = SharpDX.Direct2D1.DebugLevel.Information; #endif // Allocate new references d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel)); dwriteFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)); wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2()); // Create Direct2D device using (var dxgiDevice = d3dDevice.QueryInterface <SharpDX.DXGI.Device>()) { d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice)); } // Create Direct2D context d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None)); #endregion }
/// <summary> /// Creates device independent resources. /// </summary> /// <remarks> /// This method is called at the initialization of this instance. /// </remarks> protected virtual void CreateDeviceIndependentResources() { #if DEBUG var debugLevel = SharpDX.Direct2D1.DebugLevel.Information; #else var debugLevel = SharpDX.Direct2D1.DebugLevel.None; #endif // Dispose previous references and set to null Utilities.Dispose(ref d2dFactory); Utilities.Dispose(ref dwriteFactory); Utilities.Dispose(ref wicFactory); // Allocate new references d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel); dwriteFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared); wicFactory = new SharpDX.WIC.ImagingFactory2(); }
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; }
private void InitDx() { logger.Debug("GDICapture::InitDx()"); SharpDX.DXGI.Factory1 dxgiFactory = null; SharpDX.DXGI.Adapter1 adapter = null; SharpDX.DXGI.Output output = null; try { dxgiFactory = new SharpDX.DXGI.Factory1(); logger.Info(MediaFoundation.DxTool.LogDxAdapters(dxgiFactory.Adapters1)); //var hMonitor = NativeAPIs.User32.GetMonitorFromRect(this.srcRect); //if (hMonitor != IntPtr.Zero) //{ // foreach (var _adapter in dxgiFactory.Adapters1) // { // foreach (var _output in _adapter.Outputs) // { // var descr = _output.Description; // if (descr.MonitorHandle == hMonitor) // { // adapter = _adapter; // output = _output; // break; // } // } // } //} if (adapter == null) {// первым идет адаптер с которому подключен primary монитор adapter = dxgiFactory.GetAdapter1(0); } AdapterId = adapter.Description.Luid; //logger.Info("Screen source info: " + adapter.Description.Description + " " + output.Description.DeviceName); var deviceCreationFlags = //DeviceCreationFlags.Debug | // DeviceCreationFlags.VideoSupport | DeviceCreationFlags.BgraSupport; //var feature = FeatureLevel.Level_11_0; device = new Device(adapter, deviceCreationFlags); using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>()) { multiThread.SetMultithreadProtected(true); } SharedTexture = new Texture2D(device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = DestSize.Width, Height = DestSize.Height, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, }); gdiTexture = new Texture2D(device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = SrcRect.Width, //DestSize.Width, Height = SrcRect.Height, //DestSize.Height, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.GdiCompatible, }); renderTexture = new Texture2D(device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = DestSize.Width, Height = DestSize.Height, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, //OptionFlags = ResourceOptionFlags.GdiCompatible//ResourceOptionFlags.None, OptionFlags = ResourceOptionFlags.Shared, }); if (DestSize.Width != SrcRect.Width || DestSize.Height != SrcRect.Height) { using (SharpDX.Direct2D1.Factory1 factory2D1 = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded)) { using (var surf = renderTexture.QueryInterface <SharpDX.DXGI.Surface>()) { //var pixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Ignore); var pixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); var renderTargetProps = new SharpDX.Direct2D1.RenderTargetProperties(pixelFormat); renderTarget = new SharpDX.Direct2D1.RenderTarget(factory2D1, surf, renderTargetProps); //var d2dContext = new SharpDX.Direct2D1.DeviceContext(surface); } } } } finally { if (adapter != null) { adapter.Dispose(); adapter = null; } if (output != null) { output.Dispose(); output = null; } if (dxgiFactory != null) { dxgiFactory.Dispose(); dxgiFactory = null; } } }
/// <summary> /// Creates device independent resources. /// </summary> /// <remarks> /// This method is called at the initialization of this instance. /// </remarks> protected virtual void CreateDeviceIndependentResources() { #if DEBUG var debugLevel = SharpDX.Direct2D1.DebugLevel.Information; #else var debugLevel = SharpDX.Direct2D1.DebugLevel.None; #endif // Dispose previous references and set to null SafeDispose(ref d2dFactory); SafeDispose(ref dwriteFactory); SafeDispose(ref wicFactory); // Allocate new references d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel)); dwriteFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)); wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2()); }
/* DEVICE MANAGER METHODS */ /// <summary> /// Initialize resources and trigger an initialization event for all registered listeners /// </summary> public void Initialize(Tesseract gameEngine) { // Release any pre-exisitng references ReleaseResources(); // Retrieve the Direct3D 11.1 device using (var device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel)) { Device3D = ToDispose(device.QueryInterface<Device1>()); } // Get the Direct3D 11.1 context Context3D = ToDispose(Device3D.ImmediateContext.QueryInterface<DeviceContext1>()); // Create the remaining references Factory2D = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded)); FactoryDW = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)); FactoryWIC = ToDispose(new SharpDX.WIC.ImagingFactory2()); // Create the Direct2D device using (var device = Device3D.QueryInterface<SharpDX.DXGI.Device>()) { Device2D = ToDispose(new SharpDX.Direct2D1.Device(Factory2D, device)); } // Create the Direct2D context Context2D = ToDispose(new SharpDX.Direct2D1.DeviceContext(Device2D, SharpDX.Direct2D1.DeviceContextOptions.None)); }
/// <summary> /// Creates device manager objects /// </summary> /// <remarks> /// This method is called at the initialization of this instance. /// </remarks> protected virtual void CreateInstances() { // Dispose previous references and set to null RemoveAndDispose(ref d3dDevice); RemoveAndDispose(ref d3dContext); RemoveAndDispose(ref d2dDevice); RemoveAndDispose(ref d2dContext); RemoveAndDispose(ref d2dFactory); RemoveAndDispose(ref dwriteFactory); RemoveAndDispose(ref wicFactory); #region Create Direct3D 11.1 device and retrieve device context // Bgra performs better especially with Direct2D software // render targets var creationFlags = DeviceCreationFlags.BgraSupport; #if DEBUG // Enable D3D device debug layer creationFlags |= DeviceCreationFlags.Debug; #endif // Retrieve the Direct3D 11.1 device and device context using (var device = new Device(DriverType.Hardware, creationFlags, Direct3DFeatureLevels)) { d3dDevice = ToDispose(device.QueryInterface<Device1>()); } // Get Direct3D 11.1 context d3dContext = ToDispose(d3dDevice.ImmediateContext.QueryInterface<DeviceContext1>()); #endregion #region Create Direct2D device and context #if DEBUG var debugLevel = SharpDX.Direct2D1.DebugLevel.Information; #else var debugLevel = SharpDX.Direct2D1.DebugLevel.None; #endif // Allocate new references d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, debugLevel)); dwriteFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)); wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2()); // Create Direct2D device using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>()) { d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice)); } // Create Direct2D context d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None)); #endregion }