/// <summary>
        /// Creates device-based resources to store a constant buffer, cube
        /// geometry, and vertex and pixel shaders. In some cases this will also
        /// store a geometry shader.
        /// </summary>
        public void CreateDeviceDependentResources()
        {
            ReleaseDeviceDependentResources();

            // Create a default sampler state, which will use point sampling.
            pointSampler = ToDispose(new SamplerState(deviceResources.D3DDevice, new SamplerStateDescription()
            {
                AddressU           = TextureAddressMode.Clamp,
                AddressV           = TextureAddressMode.Clamp,
                AddressW           = TextureAddressMode.Clamp,
                BorderColor        = new RawColor4(0, 0, 0, 0),
                ComparisonFunction = Comparison.Never,
                Filter             = Filter.MinMagMipLinear,
                MaximumAnisotropy  = 16,
                MaximumLod         = float.MaxValue,
                MinimumLod         = 0,
                MipLodBias         = 0.0f
            }));

            // Create the texture that will be used as the offscreen render target.
            var textureDesc = new Texture2DDescription
            {
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Width             = textureWidth,
                Height            = textureHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource | BindFlags.RenderTarget,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                OptionFlags       = ResourceOptionFlags.None,
                Usage             = ResourceUsage.Default,
                CpuAccessFlags    = CpuAccessFlags.None
            };

            texture2D = new Texture2D(deviceResources.D3DDevice, textureDesc);

            // Create read and write views for the offscreen render target.
            shaderResourceView = new ShaderResourceView(deviceResources.D3DDevice, texture2D);
            renderTargetView   = new RenderTargetView(deviceResources.D3DDevice, texture2D);

            // In this example, we are using D2D and DirectWrite; so, we need to create a D2D render target as well.
            SharpDX.Direct2D1.RenderTargetProperties props = new SharpDX.Direct2D1.RenderTargetProperties(
                SharpDX.Direct2D1.RenderTargetType.Default,
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                96, 96,
                SharpDX.Direct2D1.RenderTargetUsage.None,
                SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT);

            // The DXGI surface is used to create the render target.
            SharpDX.DXGI.Surface dxgiSurface = texture2D.QueryInterface <SharpDX.DXGI.Surface>();
            d2dRenderTarget = new SharpDX.Direct2D1.RenderTarget(deviceResources.D2DFactory, dxgiSurface, props);

            // Create a solid color brush that will be used to render the text.
            whiteBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dRenderTarget, new RawColor4(1f, 1f, 1f, 1f));

            // This is where we format the text that will be written on the render target.
            textFormat = new SharpDX.DirectWrite.TextFormat(deviceResources.DWriteFactory, "Consolas", SharpDX.DirectWrite.FontWeight.Normal, SharpDX.DirectWrite.FontStyle.Normal, 64f);
            textFormat.TextAlignment      = SharpDX.DirectWrite.TextAlignment.Center;
            textFormat.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Center;
        }
        private void CreateRenderTarget(SharpDX.DXGI.Surface surface)
        {
            // Create a D2D render target which can draw into our offscreen D3D surface.
            // D2D uses device independant units, like WPF, at 96/inch.
            var properties = new SharpDX.Direct2D1.RenderTargetProperties();

            properties.DpiX        = 96;
            properties.DpiY        = 96;
            properties.MinLevel    = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT;
            properties.PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            properties.Usage       = SharpDX.Direct2D1.RenderTargetUsage.None;

            if (this.renderTarget != null)
            {
                this.renderTarget.Dispose();
            }

            renderTarget = new SharpDX.Direct2D1.RenderTarget(factory2D, surface, properties);
        }
Exemplo n.º 3
0
        public DUIWindowRenderTarget(IntPtr handle)
        {
            Size size = System.Windows.Forms.Control.FromHandle(handle).Size;

            this.direct2D1factory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded);
            SharpDX.Direct2D1.HwndRenderTargetProperties hwndRenderTargetProperties = new SharpDX.Direct2D1.HwndRenderTargetProperties()
            {
                Hwnd           = handle,
                PixelSize      = new SharpDX.Size2(size.Width, size.Height),
                PresentOptions = SharpDX.Direct2D1.PresentOptions.None
            };
            SharpDX.Direct2D1.PixelFormat pixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            //强制设置DPI为96,96(默认值),无意间发现有的人居然会去修改系统的dpi
            SharpDX.Direct2D1.RenderTargetProperties renderTargetProperties = new SharpDX.Direct2D1.RenderTargetProperties(SharpDX.Direct2D1.RenderTargetType.Default, pixelFormat, 96, 96, SharpDX.Direct2D1.RenderTargetUsage.None, SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT);
            //初始化,在_d2dFactory创建渲染缓冲区并与Target绑定
            //SharpDX.Direct2D1.PixelFormat pf = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            this.windowRenderTarget = new SharpDX.Direct2D1.WindowRenderTarget(direct2D1factory, renderTargetProperties, hwndRenderTargetProperties);
            this.windowRenderTarget.AntialiasMode     = SharpDX.Direct2D1.AntialiasMode.Aliased;
            this.windowRenderTarget.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased;
        }
Exemplo n.º 4
0
        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;
                }
            }
        }
        private void CreateRenderTarget(SharpDX.DXGI.Surface surface)
        {
            // Create a D2D render target which can draw into our offscreen D3D surface. 
            // D2D uses device independant units, like WPF, at 96/inch.
            var properties = new SharpDX.Direct2D1.RenderTargetProperties();
            properties.DpiX = 96;
            properties.DpiY = 96;
            properties.MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT;
            properties.PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied);
            properties.Usage = SharpDX.Direct2D1.RenderTargetUsage.None;

            if (this.renderTarget != null)
            {
                this.renderTarget.Dispose();
            }

            renderTarget = new SharpDX.Direct2D1.RenderTarget(factory2D, surface, properties);
        }