Example #1
0
        private void Application_SlideShowBegin(SlideShowWindow Wn)
        {
            if (!PresentationToNDIAddIn.Properties.Settings.Default.NDIDynamic)
            {
                return;
            }

            _window = Wn;
            _wasFullscreenBefore = IsFullscreen;
            _xOrig = Wn.Presentation.SlideMaster.Width;
            _yOrig = Wn.Presentation.SlideMaster.Height;

            _device    = Direct3D11Helper.CreateDevice(!PresentationToNDIAddIn.Properties.Settings.Default.UseHw);
            _d3dDevice = Direct3D11Helper.CreateSharpDXDevice(_device);
            _factory   = new Factory2();

            _item = GetItem();
            CreateCaptureItemDependendStuff();

            _ndiSender = new Thread(SendNdi)
            {
                Priority = ThreadPriority.Normal, Name = "DynamicNdiSenderThread", IsBackground = true
            };
            _ndiSender.Start();
        }
Example #2
0
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.

        #endregion

        #region Initialization

        private void InitializeDevices()
        {
            var createdDevice = Direct3D11Helper.CreateDevice();

            device = createdDevice ?? throw new Exception("Failed to create Direct3D device.");

            var createdSharpDXDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            sharpDXDevice = createdSharpDXDevice ?? throw new Exception("Failed to create SharpDX device.");

            multithread = sharpDXDevice.QueryInterface <Multithread>();
            multithread.SetMultithreadProtected(true);
        }
Example #3
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            item      = i;
            device    = d;
            d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            var size = item.Size;

            if (size.Height == 0 || size.Width == 0)
            {
                size = new SizeInt32()
                {
                    Height = 1, Width = 1
                }
            }
            ;

            var dxgiFactory = new SharpDX.DXGI.Factory2();
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = size.Width,
                Height            = size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                size);
            session  = framePool.CreateCaptureSession(i);
            lastSize = size;

            framePool.FrameArrived += OnFrameArrived;
        }
Example #4
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            item       = i;
            device     = d;
            d3dDevice  = Direct3D11Helper.CreateSharpDXDevice(device);
            wicFactory = new ImagingFactory();

            // 建立DirectX图形基础设施工厂(DirectX Graphics Infrastructure,DXGI)
            var dxgiFactory = new SharpDX.DXGI.Factory2();

            // 建立交换链
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = item.Size.Width,
                Height            = item.Size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false, // 立体声
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,   // 连续翻转
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,     // 预乘
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                i.Size);

            // 创建捕获会话
            session = framePool.CreateCaptureSession(i);
            // 记录最后帧尺寸
            lastSize = i.Size;

            // 帧已到达事件添加
            framePool.FrameArrived += OnFrameArrived;
        }
Example #5
0
        public ScreenCapture(GraphicsCaptureItem i)
        {
            item = i;

            device    = Direct3D11Helper.CreateDevice();
            d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device);

            SizeInt32 correctedSize = item.Size;

            correctedSize.Width  = correctedSize.Width / 8 * 8;
            correctedSize.Height = correctedSize.Height / 8 * 8;

            var dxgiFactory = new Factory2();
            var description = new SwapChainDescription1()
            {
                Width             = correctedSize.Width,
                Height            = correctedSize.Height,
                Format            = Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = Scaling.Stretch,
                SwapEffect  = SwapEffect.FlipSequential,
                AlphaMode   = AlphaMode.Premultiplied,
                Flags       = SwapChainFlags.None
            };

            swapChain = new SwapChain1(dxgiFactory, d3dDevice, ref description);

            framePool = Direct3D11CaptureFramePool.Create(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                correctedSize);
            session  = framePool.CreateCaptureSession(i);
            lastSize = correctedSize;

            framePool.FrameArrived += OnFrameArrived;
        }
Example #6
0
        public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i)
        {
            __item      = i;
            __device    = d;
            __d3dDevice = Direct3D11Helper.CreateSharpDXDevice(__device);

            var dxgiFactory = new SharpDX.DXGI.Factory2();
            var description = new SharpDX.DXGI.SwapChainDescription1()
            {
                Width             = __item.Size.Width,
                Height            = __item.Size.Height,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage       = SharpDX.DXGI.Usage.RenderTargetOutput,
                BufferCount = 2,
                Scaling     = SharpDX.DXGI.Scaling.Stretch,
                SwapEffect  = SharpDX.DXGI.SwapEffect.FlipSequential,
                AlphaMode   = SharpDX.DXGI.AlphaMode.Premultiplied,
                Flags       = SharpDX.DXGI.SwapChainFlags.None
            };

            __swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, __d3dDevice, ref description);

            __framePool = Direct3D11CaptureFramePool.Create(
                __device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                2,
                i.Size);
            __session  = __framePool.CreateCaptureSession(i);
            __lastSize = i.Size;

            // Disable mouse cursor to be visible
            __session.IsCursorCaptureEnabled = false;

            __framePool.FrameArrived += OnFrameArrived;
        }
Example #7
0
        public GraphicsItemD3DImage()
        {
            if (App.Current.MainWindow != null)
            {
                IntPtr window        = new WindowInteropHelper(App.Current.MainWindow).Handle;
                var    presentparams = new SharpDX.Direct3D9.PresentParameters
                {
                    Windowed             = true,
                    SwapEffect           = SharpDX.Direct3D9.SwapEffect.Discard,
                    DeviceWindowHandle   = window,
                    PresentationInterval = PresentInterval.Immediate,
                };

                const CreateFlags deviceFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve;

                this.direct3d  = new Direct3DEx();
                this.device    = Direct3D11Helper.CreateDevice();
                this.d3dDevice = Direct3D11Helper.CreateSharpDXDevice(this.device);
                this.d9device  = new DeviceEx(this.direct3d, 0, DeviceType.Hardware, IntPtr.Zero, deviceFlags, presentparams);
                this.IsFrontBufferAvailableChanged += this.GraphicItemD3DImageIsFrontBufferAvailableChanged;
            }
        }
Example #8
0
        static void Main(string[] args)
        {
            // Get a capture item that represents the primary monitor
            var monitor = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
            var item    = CaptureHelper.CreateItemForMonitor(monitor);
            var size    = item.Size;

            // Setup D3D
            var device     = Direct3D11Helper.CreateDevice();
            var d3dDevice  = Direct3D11Helper.CreateSharpDXDevice(device);
            var d3dContext = d3dDevice.ImmediateContext;

            // Create our staging texture
            var description = new SharpDX.Direct3D11.Texture2DDescription
            {
                Width             = size.Width,
                Height            = size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                SampleDescription = new SharpDX.DXGI.SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                Usage          = SharpDX.Direct3D11.ResourceUsage.Staging,
                BindFlags      = SharpDX.Direct3D11.BindFlags.None,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.Read,
                OptionFlags    = SharpDX.Direct3D11.ResourceOptionFlags.None
            };
            var stagingTexture = new SharpDX.Direct3D11.Texture2D(d3dDevice, description);

            // Setup capture
            var framePool = Direct3D11CaptureFramePool.CreateFreeThreaded(
                device,
                DirectXPixelFormat.B8G8R8A8UIntNormalized,
                1,
                size);
            var session = framePool.CreateCaptureSession(item);

            var imageNum = 0;
            var endEvent = new ManualResetEvent(false);

            framePool.FrameArrived += (sender, a) =>
            {
                using (var frame = sender.TryGetNextFrame())
                    using (var bitmap = Direct3D11Helper.CreateSharpDXTexture2D(frame.Surface))
                    {
                        // Copy to our staging texture
                        d3dContext.CopyResource(bitmap, stagingTexture);

                        // Map our texture and get the bits
                        var mapped       = d3dContext.MapSubresource(stagingTexture, 0, SharpDX.Direct3D11.MapMode.Read, SharpDX.Direct3D11.MapFlags.None);
                        var source       = mapped.DataPointer;
                        var sourceStride = mapped.RowPitch;

                        // Allocate some memory to hold our copy
                        var bytes = new byte[size.Width * size.Height * 4]; // 4 bytes per pixel
                        unsafe
                        {
                            fixed(byte *bytesPointer = bytes)
                            {
                                var dest       = (IntPtr)bytesPointer;
                                var destStride = size.Width * 4;

                                for (int i = 0; i < size.Height; i++)
                                {
                                    SharpDX.Utilities.CopyMemory(dest, source, destStride);

                                    source = IntPtr.Add(source, sourceStride);
                                    dest   = IntPtr.Add(dest, destStride);
                                }
                            }
                        }

                        // Don't forget to unmap when you're done!
                        d3dContext.UnmapSubresource(stagingTexture, 0);

                        // Encode it
                        // NOTE: Waiting here will stall the capture
                        EncodeBytesAsync($"image{imageNum}.png", size.Width, size.Height, bytes).Wait();

                        imageNum++;
                        if (imageNum > 10)
                        {
                            framePool.Dispose();
                            session.Dispose();
                            endEvent.Set();
                        }
                    }
            };

            // Start the capture and wait
            session.StartCapture();
            endEvent.WaitOne();
        }