/// <summary>
 ///
 /// </summary>
 /// <param name="swapChain"></param>
 /// <param name="deviceContext"></param>
 public void Initialize(SwapChain1 swapChain, DeviceContext2D deviceContext)
 {
     RemoveAndDispose(ref d2DTarget);
     using (var surf = swapChain.GetBackBuffer <Surface>(0))
     {
         d2DTarget = Collect(BitmapProxy.Create("SwapChainTarget", deviceContext, surf));
     }
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="texture"></param>
 /// <param name="deviceContext"></param>
 public void Initialize(Texture2D texture, DeviceContext2D deviceContext)
 {
     RemoveAndDispose(ref d2DTarget);
     using (var surface = texture.QueryInterface <global::SharpDX.DXGI.Surface>())
     {
         d2DTarget = Collect(BitmapProxy.Create("TextureTarget", deviceContext, surface));
     }
 }
Exemple #3
0
        /// <summary>
        /// Creates device resources.
        /// </summary>
        /// <remarks>
        /// This method is called at the initialization of this instance.
        /// </remarks>
        protected virtual void CreateDeviceResources()
        {
            // Dispose previous references and set to null
            if (d3dDevice != null)
            {
                RemoveAndDispose(ref d3dDevice);
            }

            if (d3dContext != null)
            {
                RemoveAndDispose(ref d3dContext);
            }

            if (d2dDevice != null)
            {
                RemoveAndDispose(ref d2dDevice);
            }

            if (d2dContext != null)
            {
                RemoveAndDispose(ref d2dContext);
            }

            // Allocate new references
            // Enable compatibility with Direct2D
            // Retrieve the Direct3D 11.1 device amd device context
            var creationFlags = global::SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | global::SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;

            // Decomment this line to have Debug. Unfortunately, debug is sometimes crashing applications, so it is disable by default
            try
            {
                // Try to create it with Video Support
                // If it is not working, we just use BGRA
                // Force to FeatureLevel.Level_9_1
                using (var defaultDevice = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                    d3dDevice = defaultDevice.QueryInterface <global::SharpDX.Direct3D11.Device1>();
            } catch (Exception)
            {
                creationFlags = global::SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
                using (var defaultDevice = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags))
                    d3dDevice = defaultDevice.QueryInterface <global::SharpDX.Direct3D11.Device1>();
            }
            featureLevel = d3dDevice.FeatureLevel;

            // Get Direct3D 11.1 context
            d3dContext = Collect(d3dDevice.ImmediateContext.QueryInterface <global::SharpDX.Direct3D11.DeviceContext1>());

            // Create Direct2D device
            using (var dxgiDevice = d3dDevice.QueryInterface <global::SharpDX.DXGI.Device>())
                d2dDevice = Collect(new global::SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice));

            // Create Direct2D context
            d2dContext = Collect(new global::SharpDX.Direct2D1.DeviceContext(d2dDevice, global::SharpDX.Direct2D1.DeviceContextOptions.None));
        }