Exemple #1
0
        /// <summary>
        /// Constructs a <see cref="TemporaryRenderTarget"/> instance.
        /// </summary>
        /// <param name="renderTargetIndex">Render target index</param>
        /// <param name="targetSurface">Target surface to render on</param>
        public TemporaryRenderTarget(int renderTargetIndex, Surface targetSurface)
        {
            // Select target index (0 by default)
            _renderTargetIndex = renderTargetIndex;

            // Remember old RenderTarget
            _backBuffer = _device.GetRenderTarget(_renderTargetIndex);

            // Get information of new Texture target
            _targetSurface = targetSurface;

            // Set new target
            _device.SetRenderTarget(_renderTargetIndex, _targetSurface);
        }
    public DwmCapture(IntPtr hWnd)
    {
        _captureHwnd = hWnd;

        #region Initialise the Direct3D device

        int adapterOrdinal = 0;

        _d3dEx       = new Direct3DEx();
        _adapterLuid = _d3dEx.GetAdapterLuid(adapterOrdinal);

        var presentParams = new PresentParameters
        {
            PresentFlags     = PresentFlags.LockableBackBuffer,
            Windowed         = true,
            BackBufferFormat = Format.A8R8G8B8,
            SwapEffect       = SwapEffect.Flip
        };

        _deviceEx = new DeviceEx(_d3dEx, adapterOrdinal, DeviceType.Hardware, _captureHwnd, SlimDX.Direct3D9.CreateFlags.Multithreaded | SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, presentParams);

        #endregion

        #region Setup the shared surface (using DWM)

        uint   format        = 0;
        IntPtr pSharedHandle = IntPtr.Zero;
        int    hr            = NativeMethods.GetSharedSurface(_captureHwnd, _adapterLuid, 0, 0, ref format, out pSharedHandle, 0);
        NativeMethods.UpdateWindowShared(_captureHwnd, 0, 0, 0, IntPtr.Zero, IntPtr.Zero);

        RECT winRect;
        NativeMethods.GetWindowRect(_captureHwnd, out winRect);

        Size size = new Size(winRect.Right - winRect.Left, winRect.Bottom - winRect.Top);

        /* Hack because SlimDX does not let you specify a shared handle for creating shared resources we
         * have to create an IDirect3DDevice9 reference to the device instead */
        IDirect3DDevice9 devEx = (IDirect3DDevice9)Marshal.GetObjectForIUnknown(_deviceEx.ComPointer);
        IntPtr           pTexture;
        devEx.CreateTexture((int)size.Width, (int)size.Height, 1, 1, format, 0, out pTexture, ref pSharedHandle);
        Texture texture = Texture.FromPointer(pTexture);

        _sharedSurface = texture.GetSurfaceLevel(0);

        _renderTarget = Surface.CreateRenderTarget(_deviceEx, (int)size.Width, (int)size.Height, Format.X8R8G8B8,
                                                   MultisampleType.None, 0, false);

        _deviceEx.SetRenderTarget(0, _renderTarget);

        Surface.FromSurface(_renderTarget, _sharedSurface, Filter.None, 0);

        _systemMemorySurface = Surface.CreateOffscreenPlain(_deviceEx, (int)size.Width, (int)size.Height,
                                                            Format.X8R8G8B8, Pool.SystemMemory);

        #endregion
    }
Exemple #3
0
        public override void Reset(int w, int h)
        {
            device.GetOrThrow();

            if (w < 1)
            {
                throw new ArgumentOutOfRangeException("w");
            }
            if (h < 1)
            {
                throw new ArgumentOutOfRangeException("h");
            }

            Set(ref renderTarget, new Texture(this.device, w, h, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default));

            using (var surface = renderTarget.GetSurfaceLevel(0))
                device.SetRenderTarget(0, surface);
        }
        public void Initialize(int width, int height)
        {
            Uninitialize();

            _d3d = new Direct3DEx();
            IntPtr            windowHandle       = GetWpfWindowHandle();
            PresentParameters presentationParams = new PresentParameters(width, height);

            _d3dDevice   = new DeviceEx(_d3d, DefaultAdapterNumber, DeviceType.Hardware, windowHandle, CreateFlags.HardwareVertexProcessing, presentationParams);
            RenderTarget = Surface.CreateRenderTarget(_d3dDevice, width, height, Format.A8R8G8B8, MultisampleType.None, 0, true);
            _d3dDevice.SetRenderTarget(0, RenderTarget);

            _vertices = new VertexBuffer(_d3dDevice, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Default);
            _vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
                new Vertex()
                {
                    Color = new RawColorBGRA(0, 0, 128, 255), Position = new Vector4(32.0f, 8.0f, 4.0f, 1.0f)
                },
                new Vertex()
                {
                    Color = new RawColorBGRA(0, 128, 0, 255), Position = new Vector4(52f, 40.0f, 4.0f, 1.0f)
                },
                new Vertex()
                {
                    Color = new RawColorBGRA(128, 0, 0, 255), Position = new Vector4(12f, 40.0f, 4.0f, 1.0f)
                }
            });
            _vertices.Unlock();

            var vertexElems = new[] {
                new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
                new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
                VertexElement.VertexDeclarationEnd
            };

            _vertexDecl = new VertexDeclaration(_d3dDevice, vertexElems);
        }
    public DwmCapture(IntPtr hWnd)
    {
        _captureHwnd = hWnd;

        #region Initialise the Direct3D device

        int adapterOrdinal = 0;

        _d3dEx = new Direct3DEx();
        _adapterLuid = _d3dEx.GetAdapterLuid(adapterOrdinal);

        var presentParams = new PresentParameters
        {
            PresentFlags = PresentFlags.LockableBackBuffer,
            Windowed = true,
            BackBufferFormat = Format.A8R8G8B8,
            SwapEffect = SwapEffect.Flip
        };

        _deviceEx = new DeviceEx(_d3dEx, adapterOrdinal, DeviceType.Hardware, _captureHwnd, SlimDX.Direct3D9.CreateFlags.Multithreaded | SlimDX.Direct3D9.CreateFlags.SoftwareVertexProcessing, presentParams);

        #endregion

        #region Setup the shared surface (using DWM)

        uint format = 0;
        IntPtr pSharedHandle = IntPtr.Zero;
        int hr = NativeMethods.GetSharedSurface(_captureHwnd, _adapterLuid, 0, 0, ref format, out pSharedHandle, 0);
        NativeMethods.UpdateWindowShared(_captureHwnd, 0, 0, 0, IntPtr.Zero, IntPtr.Zero);

        RECT winRect;
        NativeMethods.GetWindowRect(_captureHwnd, out winRect);

        Size size = new Size(winRect.Right - winRect.Left, winRect.Bottom - winRect.Top);

        /* Hack because SlimDX does not let you specify a shared handle for creating shared resources we
         * have to create an IDirect3DDevice9 reference to the device instead */
        IDirect3DDevice9 devEx = (IDirect3DDevice9)Marshal.GetObjectForIUnknown(_deviceEx.ComPointer);
        IntPtr pTexture;
        devEx.CreateTexture((int)size.Width, (int)size.Height, 1, 1, format, 0, out pTexture, ref pSharedHandle);
        Texture texture = Texture.FromPointer(pTexture);

        _sharedSurface = texture.GetSurfaceLevel(0);

        _renderTarget = Surface.CreateRenderTarget(_deviceEx, (int)size.Width, (int)size.Height, Format.X8R8G8B8,
                                                    MultisampleType.None, 0, false);

        _deviceEx.SetRenderTarget(0, _renderTarget);

        Surface.FromSurface(_renderTarget, _sharedSurface, Filter.None, 0);

        _systemMemorySurface = Surface.CreateOffscreenPlain(_deviceEx, (int)size.Width, (int)size.Height,
                                                             Format.X8R8G8B8, Pool.SystemMemory);

        #endregion
    }