Esempio n. 1
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));
        }
Esempio n. 2
0
        protected override DeviceDescriptor CreateDevicesAndFactories()
        {
            DeviceDescriptor desc = new DeviceDescriptor();

            var creationFlags = global::SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
            var featureLevels = global::SharpDX.Direct3D.FeatureLevel.Level_10_0;

            using (var defaultDevice = new global::SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags, featureLevels))
                desc.D3DDevice10 = defaultDevice.QueryInterface <global::SharpDX.Direct3D11.Device1>();

            desc.WriteFactory = new global::SharpDX.DirectWrite.Factory(global::SharpDX.DirectWrite.FactoryType.Shared);
            desc.D2DFactory   = new global::SharpDX.Direct2D1.Factory(global::SharpDX.Direct2D1.FactoryType.SingleThreaded);
            return(desc);
        }
                public bool Initialize()
                {
                    if (IsInitialized)
                    {
                        return(true);
                    }
                    var list = new List <DuplicationInfo>();

                    using (var dxgDevice = device.QueryInterface <Device>())
                    {
                        using (var adapter = dxgDevice.GetParent <Adapter>())
                        {
                            var outputCount = adapter.GetOutputCount();
                            for (var i = 0; i < outputCount; ++i)
                            {
                                using (var output = adapter.GetOutput(i))
                                {
                                    using (var output1 = output.QueryInterface <Output1>())
                                    {
                                        try
                                        {
                                            var duplication = output1.DuplicateOutput(device);
                                            list.Add(new DuplicationInfo(output.Description, duplication));
                                        }
                                        catch (SharpDXException ex)
                                        {
                                            if (ex.ResultCode.Code == global::SharpDX.Result.AccessDenied.Code)
                                            {
                                                return(false);
                                            }
                                            else
                                            {
                                                throw new SharpDXException(ex.ResultCode);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    var index = 0;

                    foreach (var dup in list.OrderBy(x => x.OutputDesc.DesktopBounds.Left))
                    {
                        duplicationDict.Add(index++, dup);
                    }
                    IsInitialized = true;
                    return(true);
                }