Exemplo n.º 1
0
        private static void Overlay()
        {
            if (OverlayEngine != null)
            {
                OverlayEngine.Dispose();
            }

            OverlayEngine = new DX11.DXOverlayEngine();
            OverlayEngine.Overlays.Add(new Capture.Hook.Common.Overlay
            {
                Elements =
                {
                    new Capture.Hook.Common.FramesPerSecond(new System.Drawing.Font("Arial",                                     20))
                    {
                        Location = new System.Drawing.Point(5,                                                             5), Color = benchmarking.benchColour,    AntiAliased = true
                    },
                    new Capture.Hook.Common.TextElement(new System.Drawing.Font("Times New Roman",                                                          22))
                    {
                        Text = "Testing",                                                          Location = new System.Drawing.Point(5,200), Color = System.Drawing.Color.Yellow, AntiAliased = false
                    },
                }
            });
            OverlayEngine.Initialise(benchChain);

            foreach (var overlay in OverlayEngine.Overlays)
            {
                overlay.Frame();
            }
            OverlayEngine.Draw();
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Hooked to allow resizing a texture/surface that is reused. Currently not in use as we create the texture for each
        ///     request
        ///     to support different sizes each time (as we use DirectX to copy only the region we are after rather than the entire
        ///     backbuffer)
        /// </summary>
        /// <param name="swapChainPtr"></param>
        /// <param name="newTargetParameters"></param>
        /// <returns></returns>
        private int ResizeTargetHook(IntPtr swapChainPtr, ref ModeDescription newTargetParameters)
        {
            // Dispose of overlay engine (so it will be recreated with correct renderTarget view size)
            if (_overlayEngine != null)
            {
                _overlayEngine.Dispose();
                _overlayEngine = null;
            }

            return(DXGISwapChain_ResizeTargetHook.Original(swapChainPtr, ref newTargetParameters));
        }
Exemplo n.º 3
0
 public override void Cleanup()
 {
     try
     {
         if (_overlayEngine != null)
         {
             _overlayEngine.Dispose();
             _overlayEngine = null;
         }
     }
     catch
     {
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Implementation of capturing from the render target of the Direct3D9 Device (or DeviceEx)
        /// </summary>
        /// <param name="device"></param>
        void DoCaptureRenderTarget(Device device, string hook)
        {
            this.Frame();

            try
            {
                #region Screenshot Request

                // If we have issued the command to copy data to our render target, check if it is complete
                bool qryResult;
                if (_queryIssued && _requestCopy != null && _query.GetData(out qryResult, false))
                {
                    // The GPU has finished copying data to _renderTargetCopy, we can now lock
                    // the data and access it on another thread.

                    _queryIssued = false;

                    // Lock the render target
                    global::SharpDX.Rectangle     rect;
                    global::SharpDX.DataRectangle lockedRect = LockRenderTarget(_renderTargetCopy, out rect);
                    _renderTargetCopyLocked = true;

                    // Copy the data from the render target
                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        lock (_lockRenderTarget)
                        {
                            ProcessCapture(rect.Width, rect.Height, lockedRect.Pitch, _renderTargetCopy.Description.Format.ToPixelFormat(), lockedRect.DataPointer, _requestCopy);
                        }
                    });
                }

                // Single frame capture request
                if (this.Request != null)
                {
                    DateTime start = DateTime.Now;
                    try
                    {
                        using (Surface renderTarget = device.GetRenderTarget(0))
                        {
                            int width, height;

                            // If resizing of the captured image, determine correct dimensions
                            if (Request.Resize != null && (renderTarget.Description.Width > Request.Resize.Value.Width || renderTarget.Description.Height > Request.Resize.Value.Height))
                            {
                                if (renderTarget.Description.Width > Request.Resize.Value.Width)
                                {
                                    width  = Request.Resize.Value.Width;
                                    height = (int)Math.Round((renderTarget.Description.Height * ((double)Request.Resize.Value.Width / (double)renderTarget.Description.Width)));
                                }
                                else
                                {
                                    height = Request.Resize.Value.Height;
                                    width  = (int)Math.Round((renderTarget.Description.Width * ((double)Request.Resize.Value.Height / (double)renderTarget.Description.Height)));
                                }
                            }
                            else
                            {
                                width  = renderTarget.Description.Width;
                                height = renderTarget.Description.Height;
                            }

                            // If existing _renderTargetCopy, ensure that it is the correct size and format
                            if (_renderTargetCopy != null && (_renderTargetCopy.Description.Width != width || _renderTargetCopy.Description.Height != height || _renderTargetCopy.Description.Format != renderTarget.Description.Format))
                            {
                                // Cleanup resources
                                Cleanup();
                            }

                            // Ensure that we have something to put the render target data into
                            if (!_resourcesInitialised || _renderTargetCopy == null)
                            {
                                CreateResources(device, width, height, renderTarget.Description.Format);
                            }

                            // Resize from render target Surface to resolvedSurface (also deals with resolving multi-sampling)
                            device.StretchRectangle(renderTarget, _resolvedTarget, TextureFilter.None);
                        }

                        // If the render target is locked from a previous request unlock it
                        if (_renderTargetCopyLocked)
                        {
                            // Wait for the the ProcessCapture thread to finish with it
                            lock (_lockRenderTarget)
                            {
                                if (_renderTargetCopyLocked)
                                {
                                    _renderTargetCopy.UnlockRectangle();
                                    _renderTargetCopyLocked = false;
                                }
                            }
                        }

                        // Copy data from resolved target to our render target copy
                        device.GetRenderTargetData(_resolvedTarget, _renderTargetCopy);

                        _requestCopy = Request.Clone();
                        _query.Issue(Issue.End);
                        _queryIssued = true;
                    }
                    finally
                    {
                        // We have completed the request - mark it as null so we do not continue to try to capture the same request
                        // Note: If you are after high frame rates, consider implementing buffers here to capture more frequently
                        //         and send back to the host application as needed. The IPC overhead significantly slows down
                        //         the whole process if sending frame by frame.
                        Request = null;
                    }
                    DateTime end = DateTime.Now;
                    this.DebugMessage(hook + ": Capture time: " + (end - start).ToString());
                }

                #endregion

                var displayOverlays = Overlays;
                if (this.Config.ShowOverlay && displayOverlays != null)
                {
                    #region Draw Overlay

                    // Check if overlay needs to be initialised
                    if (_overlayEngine == null || _overlayEngine.Device.NativePointer != device.NativePointer ||
                        IsOverlayUpdatePending)
                    {
                        // Cleanup if necessary
                        if (_overlayEngine != null)
                        {
                            RemoveAndDispose(ref _overlayEngine);
                        }

                        _overlayEngine = ToDispose(new DXOverlayEngine());
                        _overlayEngine.Overlays.AddRange((IEnumerable <IOverlay>)displayOverlays);
                        _overlayEngine.Initialise(device);
                        IsOverlayUpdatePending = false;
                    }
                    // Draw Overlay(s)
                    if (_overlayEngine != null)
                    {
                        foreach (var overlay in _overlayEngine.Overlays)
                        {
                            overlay.Frame();
                        }
                        _overlayEngine.Draw();
                    }

                    #endregion
                }
            }
            catch (Exception e)
            {
                DebugMessage(e.ToString());
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Our present hook that will grab a copy of the backbuffer when requested. Note: this supports multi-sampling
        ///     (anti-aliasing)
        /// </summary>
        /// <param name="swapChainPtr"></param>
        /// <param name="syncInterval"></param>
        /// <param name="flags"></param>
        /// <returns>The HRESULT of the original method</returns>
        private int PresentHook(IntPtr swapChainPtr, int syncInterval, PresentFlags flags)
        {
            Frame();
            var swapChain = (SwapChain)swapChainPtr;

            try
            {
                #region Screenshot Request

                if (Request != null)
                {
                    DebugMessage("PresentHook: Request Start");
                    var startTime = DateTime.Now;
                    using (var currentRT = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0))
                    {
                        #region Determine region to capture

                        var captureRegion = new Rectangle(0, 0, currentRT.Description.Width,
                                                          currentRT.Description.Height);

                        if (Request.RegionToCapture.Width > 0)
                        {
                            captureRegion = new Rectangle(Request.RegionToCapture.Left, Request.RegionToCapture.Top,
                                                          Request.RegionToCapture.Right, Request.RegionToCapture.Bottom);
                        }
                        else if (Request.Resize.HasValue)
                        {
                            captureRegion = new Rectangle(0, 0, Request.Resize.Value.Width, Request.Resize.Value.Height);
                        }

                        #endregion

                        // Create / Recreate resources as necessary
                        EnsureResources(currentRT.Device, currentRT.Description, captureRegion, Request);

                        Texture2D sourceTexture = null;

                        // If texture is multisampled, then we can use ResolveSubresource to copy it into a non-multisampled texture
                        if (currentRT.Description.SampleDescription.Count > 1 || Request.Resize.HasValue)
                        {
                            if (Request.Resize.HasValue)
                            {
                                DebugMessage("PresentHook: resizing texture");
                            }
                            else
                            {
                                DebugMessage("PresentHook: resolving multi-sampled texture");
                            }

                            // Resolve into _resolvedRT
                            if (_resolvedRTKeyedMutex != null)
                            {
                                _resolvedRTKeyedMutex.Acquire(0, int.MaxValue);
                            }
                            currentRT.Device.ImmediateContext.ResolveSubresource(currentRT, 0, _resolvedRT, 0,
                                                                                 _resolvedRT.Description.Format);
                            if (_resolvedRTKeyedMutex != null)
                            {
                                _resolvedRTKeyedMutex.Release(1);
                            }

                            if (Request.Resize.HasValue)
                            {
                                lock (_lock)
                                {
                                    if (_resolvedRTKeyedMutex_Dev2 != null)
                                    {
                                        _resolvedRTKeyedMutex_Dev2.Acquire(1, int.MaxValue);
                                    }
                                    _saQuad.ShaderResource   = _resolvedSharedSRV;
                                    _saQuad.RenderTargetView = _resizedRTV;
                                    _saQuad.RenderTarget     = _resizedRT;
                                    _saQuad.Render();
                                    if (_resolvedRTKeyedMutex_Dev2 != null)
                                    {
                                        _resolvedRTKeyedMutex_Dev2.Release(0);
                                    }
                                }

                                // set sourceTexture to the resized RT
                                sourceTexture = _resizedRT;
                            }
                            else
                            {
                                // Make sourceTexture be the resolved texture
                                sourceTexture = _resolvedRTShared;
                            }
                        }
                        else
                        {
                            // Copy the resource into the shared texture
                            if (_resolvedRTKeyedMutex != null)
                            {
                                _resolvedRTKeyedMutex.Acquire(0, int.MaxValue);
                            }
                            currentRT.Device.ImmediateContext.CopySubresourceRegion(currentRT, 0, null, _resolvedRT, 0);
                            if (_resolvedRTKeyedMutex != null)
                            {
                                _resolvedRTKeyedMutex.Release(1);
                            }
                            sourceTexture = _resolvedRTShared;
                        }

                        // Copy to memory and send back to host process on a background thread so that we do not cause any delay in the rendering pipeline
                        _requestCopy = Request.Clone();
                        // this.Request gets set to null, so copy the Request for use in the thread

                        // Prevent the request from being processed a second time
                        Request = null;

                        var acquireLock = sourceTexture == _resolvedRTShared;

                        ThreadPool.QueueUserWorkItem(o =>
                        {
                            // Acquire lock on second device
                            if (acquireLock && _resolvedRTKeyedMutex_Dev2 != null)
                            {
                                _resolvedRTKeyedMutex_Dev2.Acquire(1, int.MaxValue);
                            }

                            lock (_lock)
                            {
                                // Copy the subresource region, we are dealing with a flat 2D texture with no MipMapping, so 0 is the subresource index
                                sourceTexture.Device.ImmediateContext.CopySubresourceRegion(sourceTexture, 0,
                                                                                            new ResourceRegion
                                {
                                    Top    = captureRegion.Top,
                                    Bottom = captureRegion.Bottom,
                                    Left   = captureRegion.Left,
                                    Right  = captureRegion.Right,
                                    Front  = 0,
                                    Back   = 1   // Must be 1 or only black will be copied
                                }, _finalRT, 0, 0, 0, 0);

                                // Release lock upon shared surface on second device
                                if (acquireLock && _resolvedRTKeyedMutex_Dev2 != null)
                                {
                                    _resolvedRTKeyedMutex_Dev2.Release(0);
                                }

                                _finalRT.Device.ImmediateContext.End(_query);
                                _queryIssued = true;
                                while (!_finalRT.Device.ImmediateContext.GetData(_query).ReadBoolean())
                                {
                                    // Spin (usually no spin takes place)
                                }

                                var startCopyToSystemMemory = DateTime.Now;
                                try
                                {
                                    var db = default(DataBox);
                                    if (_requestCopy.Format == ImageFormat.PixelData)
                                    {
                                        db = _finalRT.Device.ImmediateContext.MapSubresource(_finalRT, 0, MapMode.Read,
                                                                                             MapFlags.DoNotWait);
                                        _finalRTMapped = true;
                                    }
                                    _queryIssued = false;

                                    try
                                    {
                                        using (var ms = new MemoryStream())
                                        {
                                            switch (_requestCopy.Format)
                                            {
                                            case ImageFormat.Bitmap:
                                                SharpDX.Direct3D11.Resource.ToStream(
                                                    _finalRT.Device.ImmediateContext, _finalRT, ImageFileFormat.Bmp,
                                                    ms);
                                                break;

                                            case ImageFormat.Jpeg:
                                                SharpDX.Direct3D11.Resource.ToStream(
                                                    _finalRT.Device.ImmediateContext, _finalRT, ImageFileFormat.Jpg,
                                                    ms);
                                                break;

                                            case ImageFormat.Png:
                                                SharpDX.Direct3D11.Resource.ToStream(
                                                    _finalRT.Device.ImmediateContext, _finalRT, ImageFileFormat.Png,
                                                    ms);
                                                break;

                                            case ImageFormat.PixelData:
                                                if (db.DataPointer != IntPtr.Zero)
                                                {
                                                    ProcessCapture(_finalRT.Description.Width,
                                                                   _finalRT.Description.Height, db.RowPitch,
                                                                   PixelFormat.Format32bppArgb, db.DataPointer, _requestCopy);
                                                }
                                                return;
                                            }
                                            ms.Position = 0;
                                            ProcessCapture(ms, _requestCopy);
                                        }
                                    }
                                    finally
                                    {
                                        DebugMessage("PresentHook: Copy to System Memory time: " +
                                                     (DateTime.Now - startCopyToSystemMemory).ToString());
                                    }

                                    if (_finalRTMapped)
                                    {
                                        lock (_lock)
                                        {
                                            _finalRT.Device.ImmediateContext.UnmapSubresource(_finalRT, 0);
                                            _finalRTMapped = false;
                                        }
                                    }
                                }
                                catch (SharpDXException exc)
                                {
                                    // Catch DXGI_ERROR_WAS_STILL_DRAWING and ignore - the data isn't available yet
                                }
                            }
                        });


                        // Note: it would be possible to capture multiple frames and process them in a background thread
                    }
                    DebugMessage("PresentHook: Copy BackBuffer time: " + (DateTime.Now - startTime));
                    DebugMessage("PresentHook: Request End");
                }

                #endregion

                #region Draw overlay (after screenshot so we don't capture overlay as well)

                if (Config.ShowOverlay)
                {
                    // Initialise Overlay Engine
                    if (_swapChainPointer != swapChain.NativePointer ||
                        _overlayEngine == null ||
                        IsOverlayUpdatePending)
                    {
                        if (_overlayEngine != null)
                        {
                            _overlayEngine.Dispose();
                        }

                        _overlayEngine = new DXOverlayEngine();
                        _overlayEngine.Overlays.Add(new Overlay
                        {
                            Elements = OverlayElements
                        });
                        _overlayEngine.Initialise(swapChain);

                        _swapChainPointer = swapChain.NativePointer;
                    }
                    // Draw Overlay(s)
                    else if (_overlayEngine != null)
                    {
                        foreach (var overlay in _overlayEngine.Overlays)
                        {
                            overlay.Frame();
                        }
                        _overlayEngine.Draw();
                    }
                }

                #endregion
            }
            catch (Exception e)
            {
                // If there is an error we do not want to crash the hooked application, so swallow the exception
                DebugMessage("PresentHook: Exeception: " + e.GetType().FullName + ": " + e);
                //return unchecked((int)0x8000FFFF); //E_UNEXPECTED
            }

            // As always we need to call the original method, note that EasyHook will automatically skip the hook and call the original method
            // i.e. calling it here will not cause a stack overflow into this function
            return(DXGISwapChain_PresentHook.Original(swapChainPtr, syncInterval, flags));
        }