Beispiel #1
0
        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            if (m_target == null)
            {
                return;
            }

            RenderingEventArgs args = (RenderingEventArgs)e;

            if ((args.RenderingTime - m_lastRender).TotalSeconds * 1000 < FPSTIME)
            {
                return;
            }

            if (this.m_source.IsFrontBufferAvailable && args.RenderingTime != m_lastRender)
            {
                lock (m_locker)
                {
                    this.m_source.Lock();
                    unsafe
                    {
                        //byte[] _data = new byte[4 * m_current.Width * m_current.Height];
                        //m_current.GetData<byte>(_data);
                        //m_target.SetData<byte>(_data);
                        if (!m_target.IsContentLost && !m_target.IsDisposed)
                        {
                            m_source.SetBackBuffer(D3DResourceType.IDirect3DSurface9, m_target.GetPtr());
                        }
                        m_source.AddDirtyRect(m_rect);
                    }
                    this.m_source.Unlock();
                }

                m_lastRender = args.RenderingTime;
            }
        }
        //Solution 1 Ori
        //[System.Runtime.InteropServices.DllImport("gdi32.dll")]
        //public static extern bool DeleteObject(IntPtr hObject);

        //Solution 2 Ori
        //public BitmapImage BitmapToBitmapImage(System.Drawing.Bitmap bitmap)
        //{
        //    using (MemoryStream stream = new MemoryStream())
        //    {
        //        bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        //        stream.Position = 0;
        //        BitmapImage result = new BitmapImage();
        //        result.BeginInit();
        //        result.CacheOption = BitmapCacheOption.OnLoad;
        //        result.StreamSource = stream;
        //        result.EndInit();
        //        result.Freeze();
        //        return result;
        //    }
        //}

        // D3D Solution
        private void RenderD3D(IntPtr surface, D3DImage d3dImage)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                if (d3dImage.IsFrontBufferAvailable && surface != IntPtr.Zero)
                {
                    var showRect = new Int32Rect(0, 0, d3dImage.PixelWidth, d3dImage.PixelHeight);
                    d3dImage.Lock();
                    d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface);

                    d3dImage.AddDirtyRect(showRect);
                    d3dImage.Unlock();
                }
            }));
        }
Beispiel #3
0
        private void ResetBackBufferReference()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            _logger.Information("Resetting back buffer reference.");
            if (_renderTarget != null)
            {
                _renderTarget.Dispose();
                _renderTarget = null;
            }

            if (_renderTargetD3D9 != null)
            {
                _renderTargetD3D9.Dispose();
                _renderTargetD3D9 = null;
            }

            _direct3DImage.Lock();
            _direct3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
            _direct3DImage.Unlock();
        }
        private void BeginRenderingScene()
        {
            if (_di.IsFrontBufferAvailable)
            {
                // create a custom D3D scene and get a pointer to its surface
                _scene = InitializeScene();

                // set the back buffer using the new scene pointer
                _di.Lock();
                _di.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _scene);
                _di.Unlock();

                // leverage the Rendering event of WPF's composition target to
                // update the custom D3D scene
                CompositionTarget.Rendering += OnRendering;
            }
        }
        private void windowLoaded(object sender, RoutedEventArgs e)
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;

            System.Threading.Thread updateThread = new System.Threading.Thread(delegate() {
                ctx = CreateEngine(handle, currentCallback);
                Dispatcher.Invoke(() => {
                    renderTarget      = new D3DImage();
                    drawTarget.Source = renderTarget;
                    renderTarget.Lock();
                    renderTarget.SetBackBuffer(D3DResourceType.IDirect3DSurface9, ctx.surface);
                    renderTarget.Unlock();
                });
                while (running)
                {
                    lock (ctxlock)
                    {
                        if (recordMode)
                        {
                            RecordFrame(ctx.handle);
                        }
                        else
                        {
                            if (!clientOnly)
                            {
                                DrawBackbuffer(ctx.handle);
                            }
                        }
                    }
                    try
                    {
                        Dispatcher.Invoke(() =>
                        {
                            renderTarget.Lock();
                            renderTarget.AddDirtyRect(new Int32Rect(0, 0, renderTarget.PixelWidth, renderTarget.PixelHeight));
                            renderTarget.Unlock();
                        });
                    }catch (Exception er)
                    {
                    }
                }
            });
            updateThread.Name = "EncodeThread";
            updateThread.Start();
        }
Beispiel #6
0
        /// <summary>
        /// Creates a cloned image of the current video frame.
        /// The image can be used thread-safe.
        /// </summary>
        /// <returns></returns>
        public Image CloneSingleFrameImage()
        {
            // create new image and it's D3D source
            Image    img       = new Image();
            D3DImage d3dSource = new D3DImage();

            // add the D3D source
            img.Source = d3dSource;

            // set default stretch
            img.Stretch          = (Stretch)StretchProperty.DefaultMetadata.DefaultValue;
            img.StretchDirection = (StretchDirection)StretchProperty.DefaultMetadata.DefaultValue;

            // store pixel width and height
            int pxWidth  = 0;
            int pxHeight = 0;

            /* We have this around a try/catch just in case we
             * lose the device and our Surface is invalid. The
             * try/catch may not be needed, but testing needs
             * to take place before it's removed */
            try
            {
                // assign surface as back buffer
                d3dSource.Lock();
                d3dSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, m_pBackBuffer);
                d3dSource.Unlock();

                // update pixel width and height
                pxWidth  = d3dSource.PixelWidth;
                pxHeight = d3dSource.PixelHeight;
            }
            catch (Exception ex)
            {
                return(null);
            }

            // UIElement Layout Update
            img.Measure(new Size(pxWidth, pxHeight));
            img.Arrange(new Rect(new Size(pxWidth, pxHeight)));
            img.UpdateLayout();

            return(img);
        }
        /// <summary> Resizes Direct3D surfaces and OpenGL FBO. </summary>
        public void Resize(int theSizeX, int theSizeY)
        {
            mySize = new WndSize(theSizeX, theSizeY);

            if (!myIsFailed && myD3DImage.IsFrontBufferAvailable)
            {
                IntPtr aColorSurfShare;

                // Initialize Direct3D device and render target
                Direct3DProxy.ResizeWindow(ref myD3DRender, ref mySize, out myColorSurf, out aColorSurfShare);

                // Set the back buffer for Direct3D WPF image
                myD3DImage.Lock();
                {
                    myD3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, myColorSurf);
                }
                myD3DImage.Unlock();

                Viewer.View.ResizeBridgeFBO(mySize.cx, mySize.cy, myColorSurf, aColorSurfShare);
            }
        }
Beispiel #8
0
        /// <summary>
        /// The render by OpenGL.
        /// </summary>
        private void RenderGLToD3dImage(D3DImage image, int w, int h, Action rendering)
        {
            if (w == 0 || h == 0)
            {
                return;
            }
            this.glControl.MakeCurrent();

            // resize D3D/OpenGL Surface if need
            this.ResizeIfNeed(w, h);

            // OnRender may be called twice in the same frame. Only render the first time.
            if (image.IsFrontBufferAvailable)
            {
                // render to sharedSurface using OpenGL
                this.wgl.WglDXLockObjectsNV(wglHandleDevice, 1, singleWglHandleSharedSurfaceArray);
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
                GL.DrawBuffer((DrawBufferMode)FramebufferAttachment.ColorAttachment0);
                rendering();
                GL.Finish();
                this.wgl.WglDXUnlockObjectsNV(wglHandleDevice, 1, singleWglHandleSharedSurfaceArray);

                try
                {
                    image.Lock();
                    image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, sharedSurface.NativePointer);
                    image.AddDirtyRect(new Int32Rect(0, 0, w, h));
                }
                catch (Exception ex)
                {
                    // ???
                    Console.WriteLine(ex.ToString());
                    this.device.ResetEx(ref this.presentparams);
                }
                finally
                {
                    image.Unlock();
                }
            }
        }
Beispiel #9
0
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            RenderingEventArgs args = (RenderingEventArgs)e;

            if (renderScene.IsFrontBufferAvailable && lastRender != args.RenderingTime)
            {
                IntPtr pSurface = IntPtr.Zero;
                HRESULT.Check(NativeMethods.GetBackBufferNoRef(out pSurface));
                if (pSurface != IntPtr.Zero)
                {
                    renderScene.Lock();
                    renderScene.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSurface);

                    if (Model != null && Model.IsLoaded)
                    {
                        render.BeginRender();

                        Model.LoadParam();
                        render.UpdateMotion();
                        render.UpdateEyeBlink();
                        Model.SaveParam();

                        render.UpdateBreath();
                        render.UpdateExpression();

                        Rendering();

                        render.UpdatePhysics();
                        render.UpdatePose();

                        render.EndRender();
                    }

                    renderScene.AddDirtyRect(new Int32Rect(0, 0, renderScene.PixelWidth, renderScene.PixelHeight));
                    renderScene.Unlock();

                    lastRender = args.RenderingTime;
                }
            }
        }
Beispiel #10
0
        public void Run(ID3DImageProvider provider)
        {
            logger.Debug("D3DImageRendererEx::Run(...)");

            if (initialized)
            {
                logger.Warn("D3DImageRendererEx::Run(...) invalid state");
                return;
            }

            try
            {
                d3dProvider = provider;
                d3dProvider.NewDataAvailable += D3dProvider_NewDataAvailable;
                VideoSource = new D3DImage();

                var pSurface = d3dProvider.GetSurfacePointer();

                if (pSurface != IntPtr.Zero)
                {
                    VideoSource.Lock();
                    VideoSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSurface);
                    VideoSource.Unlock();
                }

                // VideoSource.IsFrontBufferAvailableChanged += VideoSource_IsFrontBufferAvailableChanged;
                System.Windows.Media.CompositionTarget.Rendering += CompositionTarget_Rendering;

                initialized = true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Shutdown();

                // throw;
            }
        }
Beispiel #11
0
        private void d3dImage_IsFrontBufferAvailableChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (D3DImage.IsFrontBufferAvailable)
            {
                // (Re)initialization:
                var surface = NativeMethods.Initialize(new WindowInteropHelper(this).Handle, (int)Width, (int)Height);

                if (surface != IntPtr.Zero)
                {
                    D3DImage.Lock();
                    D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface);
                    D3DImage.Unlock();

                    CompositionTarget.Rendering += CompositionTarget_Rendering;
                }
            }
            else
            {
                // Cleanup:
                CompositionTarget.Rendering -= CompositionTarget_Rendering;
                NativeMethods.Cleanup();
            }
        }
Beispiel #12
0
        private void PreInitialize()
        {
            _graphicsDeviceService = _graphicsDeviceManager = XnaHelper.CreateDeviceManager(this);
            _content = new ContentManager(_services, ResourceManager.WorkingDirectory);

            HookDeviceEvents();

            _graphicsDeviceManager.CreateDevice();

            _d3DImage = new D3DImage();
            _d3DImage.Lock();

            try
            {
                _d3DImage.SetBackBuffer(
                    D3DResourceType.IDirect3DSurface9,
                    _frontBufferPointer);
            }
            finally
            {
                _d3DImage.Unlock();
            }
        }
Beispiel #13
0
        private void SetRenderTarget(D3D11.Texture2D target)
        {
            var format = TranslateFormat(target);
            var handle = GetSharedHandle(target);

            var presentParams = GetPresentParameters();
            var createFlags   = D3D9.CreateFlags.HardwareVertexProcessing | D3D9.CreateFlags.Multithreaded |
                                D3D9.CreateFlags.FpuPreserve;

            var d3DContext = new D3D9.Direct3DEx();
            var d3DDevice  = new D3D9.DeviceEx(d3DContext, 0, D3D9.DeviceType.Hardware, IntPtr.Zero, createFlags,
                                               presentParams);

            _renderTarget = new D3D9.Texture(d3DDevice, target.Description.Width, target.Description.Height, 1,
                                             D3D9.Usage.RenderTarget, format, D3D9.Pool.Default, ref handle);

            using (var surface = _renderTarget.GetSurfaceLevel(0))
            {
                _d3D.Lock();
                _d3D.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer);
                _d3D.Unlock();
            }
        }
        /// <summary>
        /// Configures D3DImage with a new surface.  This happens immediately
        /// </summary>
        private void SetBackBufferInternal(IntPtr backBuffer)
        {
            /* Do nothing if we don't have a new surface available */
            if (!m_newSurfaceAvailable)
            {
                return;
            }

            if (!D3DImage.Dispatcher.CheckAccess())
            {
                D3DImage.Dispatcher.BeginInvoke((Action)(() => SetBackBufferInternal(backBuffer)));
                return;
            }

            /* We have this around a try/catch just in case we
             * lose the device and our Surface is invalid. The
             * try/catch may not be needed, but testing needs
             * to take place before it's removed */
            try
            {
                D3DImage.Lock();
                //When front buffer is unavailable, use software render to keep rendering.
                D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, backBuffer, true);
            }
            catch
            { }
            finally
            {
                D3DImage.Unlock();
            }

            SetNaturalWidthHeight();

            /* Clear our flag, so this won't be ran again
             * until a new surface is sent */
            m_newSurfaceAvailable = false;
        }
        /// <summary>
        /// 描画を更新する
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateRendering(object sender, EventArgs e)
        {
            var args = (RenderingEventArgs)e;

            if (D3DImage.IsFrontBufferAvailable && _lastRender.TotalSeconds + 0.02 < args.RenderingTime.TotalSeconds)
            {
                // バックバッファの取得
                var backBuffer = _soundEffectToolVM.GetBackBuffer(_windowName);
                if (backBuffer != IntPtr.Zero)
                {
                    D3DImage.Lock();

                    // バックバッファの設定
                    D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, backBuffer);
                    // 描画
                    _soundEffectToolVM.Draw(_windowName, _waveOffset, PlayRatio);
                    D3DImage.AddDirtyRect(new Int32Rect(0, 0, D3DImage.PixelWidth, D3DImage.PixelHeight));

                    D3DImage.Unlock();

                    _lastRender = args.RenderingTime;
                }
            }
        }
Beispiel #16
0
        private void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            renderNext = !renderNext;
            if (renderNext)
            {
                return;
            }
            if (_image == null)
            {
                return;
            }

            try
            {
                // Lock the image while we are resetting as the rendering system freezes when we get a clash
                if (_sizeChanged && _image.TryLock(_timeOutDuration))
                {
                    try
                    {
                        HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "D3", IntPtr.Zero);
                        _pp.DeviceWindowHandle = hwnd.Handle;
                        _pp.BackBufferWidth    = Math.Max(100, (int)ActualWidth);
                        _pp.BackBufferHeight   = Math.Max(100, (int)ActualHeight);
                        Device.Reset(_pp);
                        _sizeChanged = false;
                    }
                    finally
                    {
                        _image.Unlock();
                    }
                    return;
                }

                if (_image.IsFrontBufferAvailable)
                {
                    if (!_image.TryLock(_timeOutDuration))
                    {
                        return;
                    }
                    Result result = Device.TestCooperativeLevel();
                    if (result.Failure)
                    {
                        throw new SharpDXException();
                    }
                    try
                    {
                        Device.SetRenderState(global::SharpDX.Direct3D9.RenderState.CullMode, Cull.None);
                        Device.SetRenderState(global::SharpDX.Direct3D9.RenderState.ZEnable, true);
                        Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Transparent, float.MaxValue, 0);
                        Device.BeginScene();
                        Render.Raise(this, new RenderEventArgs(_pp.BackBufferWidth, _pp.BackBufferHeight));
                        Device.EndScene();
                        Device.Present();

                        _image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, Device.GetBackBuffer(0, 0).NativePointer);
                        _image.AddDirtyRect(new Int32Rect(0, 0, _image.PixelWidth, _image.PixelHeight));
                    }
                    catch (Exception exc)
                    {
                        Debug.WriteLine("Error in rendering in DirectXHost: " + exc.Message);
                    }
                    finally
                    {
                        _image.Unlock();
                    }
                }
            }
            catch (SharpDXException exc)
            {
                Device.Reset(_pp);
                Debug.WriteLine("Exception in main render loop: " + exc.Message);
            }
        }
Beispiel #17
0
        public void Setup()
        {
            logger.Debug("D3DImageRenderer::Setup()");

            if (state != RendererState.Closed)
            {
                throw new InvalidOperationException("Invalid capture state " + State);
            }

            var DestSize = new Size(1920, 1088);

            try
            {
                //System.Windows.Media.CompositionTarget.Rendering += CompositionTarget_Rendering;



                direct3D = new Direct3DEx();

                var hWnd = MediaToolkit.NativeAPIs.User32.GetDesktopWindow();

                var presentParams = new PresentParameters
                {
                    //Windowed = true,
                    //SwapEffect = SharpDX.Direct3D9.SwapEffect.Discard,
                    //DeviceWindowHandle = IntPtr.Zero,
                    //PresentationInterval = SharpDX.Direct3D9.PresentInterval.Default
                    //BackBufferCount = 1,

                    Windowed        = true,
                    MultiSampleType = MultisampleType.None,
                    SwapEffect      = SwapEffect.Discard,
                    PresentFlags    = PresentFlags.None,
                };

                var flags = CreateFlags.HardwareVertexProcessing |
                            CreateFlags.Multithreaded |
                            CreateFlags.FpuPreserve;

                int adapterIndex = 0;

                device = new DeviceEx(direct3D, adapterIndex, SharpDX.Direct3D9.DeviceType.Hardware, hWnd, flags, presentParams);

                D3DDeviceManager = new Direct3DDeviceManager();
                var token = D3DDeviceManager.CreationToken;

                D3DDeviceManager.ResetDevice(device, token);

                //using (var resource = sharedTexture.QueryInterface<SharpDX.DXGI.Resource>())
                //{
                //	var handle = resource.SharedHandle;

                //	if (handle == IntPtr.Zero)
                //	{
                //		throw new ArgumentNullException(nameof(handle));
                //	}

                //	// D3DFMT_A8R8G8B8 или D3DFMT_X8R8G8B8
                //	// D3DUSAGE_RENDERTARGET
                //	// D3DPOOL_DEFAULT
                //	using (var texture3d9 = new SharpDX.Direct3D9.Texture(device,
                //			descr.Width, descr.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default,
                //			ref handle))
                //	{
                //		surface = texture3d9.GetSurfaceLevel(0);
                //	};
                //}

                surface = Surface.CreateRenderTarget(device, DestSize.Width, DestSize.Height, Format.A8R8G8B8, MultisampleType.None, 0, true);

                waitEvent = new AutoResetEvent(false);

                dispatcher.Invoke(() =>
                {
                    VideoSource = new D3DImage();

                    VideoSource.Lock();
                    VideoSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer);
                    VideoSource.Unlock();
                }, DispatcherPriority.Send);

                state = RendererState.Initialized;

                //timer.Enabled = true;
                //timer.Interval = 1000;
                //timer.Elapsed += Timer_Elapsed;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                CleanUp();

                throw;
            }
        }
Beispiel #18
0
        private void SetRenderTarget(D3D11.ID3D11Texture2D *target)
        {
            DXGI.IDXGIResource *pDXGIResource;
            var dxgiResourceGuid = DXGI.IDXGIResource.Guid;

            target->QueryInterface(ref dxgiResourceGuid, (void **)&pDXGIResource);

            D3D11.Texture2DDesc texture2DDescription = default;
            target->GetDesc(ref texture2DDescription);

            void *sharedHandle;
            var   hr = pDXGIResource->GetSharedHandle(&sharedHandle);

            SilkMarshal.ThrowHResult(hr);

            var d3d9 = D3D9.D3D9.GetApi();

            D3D9.IDirect3D9Ex *pDirect3D9Ex;
            hr = d3d9.Direct3DCreate9Ex(SDKVersion: 32, &pDirect3D9Ex);
            SilkMarshal.ThrowHResult(hr);
            var d3DContext = pDirect3D9Ex;

            _pDirect3D9Ex = pDirect3D9Ex;

            var presentParameters = new D3D9.PresentParameters()
            {
                Windowed             = 1,// true
                SwapEffect           = D3D9.Swapeffect.SwapeffectDiscard,
                HDeviceWindow        = GetDesktopWindow(),
                PresentationInterval = D3D9.D3D9.PresentIntervalDefault,
            };

            // 设置使用多线程方式,这样的性能才足够
            uint createFlags = D3D9.D3D9.CreateHardwareVertexprocessing | D3D9.D3D9.CreateMultithreaded | D3D9.D3D9.CreateFpuPreserve;

            D3D9.IDirect3DDevice9Ex *pDirect3DDevice9Ex;
            hr = d3DContext->CreateDeviceEx(Adapter: 0,
                                            DeviceType: D3D9.Devtype.DevtypeHal,// 使用硬件渲染
                                            hFocusWindow: IntPtr.Zero,
                                            createFlags,
                                            ref presentParameters,
                                            pFullscreenDisplayMode: (D3D9.Displaymodeex *)IntPtr.Zero,
                                            &pDirect3DDevice9Ex);
            SilkMarshal.ThrowHResult(hr);

            var d3DDevice = pDirect3DDevice9Ex;

            D3D9.IDirect3DTexture9 *pDirect3DTexture9;
            hr = d3DDevice->CreateTexture(texture2DDescription.Width, texture2DDescription.Height, Levels: 1,
                                          D3D9.D3D9.UsageRendertarget,
                                          D3D9.Format.FmtA8R8G8B8, // 这是必须要求的颜色,不能使用其他颜色
                                          D3D9.Pool.PoolDefault,
                                          &pDirect3DTexture9,
                                          &sharedHandle);
            SilkMarshal.ThrowHResult(hr);
            _renderTarget = pDirect3DTexture9;

            D3D9.IDirect3DSurface9 *pDirect3DSurface9;
            _renderTarget->GetSurfaceLevel(0, &pDirect3DSurface9);
            _pDirect3DSurface9 = pDirect3DSurface9;

            D3DImage.Lock();
            D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, new IntPtr(pDirect3DSurface9));
            D3DImage.Unlock();
        }
        private bool CreateRenderTarget()
        {
            DestroyRenderTarget();

            if (RendererWorld.Instance == null)
            {
                return(false);
            }

            Vec2I textureSize = GetDemandTextureSize();

            if (textureSize.X < 1 || textureSize.Y < 1)
            {
                return(false);
            }

            string textureName = TextureManager.Instance.GetUniqueName("WPFRenderTexture");

            int hardwareFSAA = 0;

            if (!RendererWorld.InitializationOptions.AllowSceneMRTRendering)
            {
                if (!int.TryParse(RendererWorld.InitializationOptions.FullSceneAntialiasing, out hardwareFSAA))
                {
                    hardwareFSAA = 0;
                }
            }

            texture = TextureManager.Instance.Create(textureName, Texture.Type.Type2D, textureSize,
                                                     1, 0, Engine.Renderer.PixelFormat.R8G8B8, Texture.Usage.RenderTarget, false, hardwareFSAA);

            if (texture == null)
            {
                return(false);
            }

            currentTextureSize = textureSize;

            renderTexture                     = texture.GetBuffer().GetRenderTarget();
            renderTexture.AutoUpdate          = false;
            renderTexture.AllowAdditionalMRTs = true;

            camera = SceneManager.Instance.CreateCamera(
                SceneManager.Instance.GetUniqueCameraName("UserControl"));
            camera.Purpose = Camera.Purposes.MainCamera;

            //update camera settings
            camera.NearClipDistance = cameraNearFarClipDistance.Minimum;
            camera.FarClipDistance  = cameraNearFarClipDistance.Maximum;
            camera.AspectRatio      = (float)texture.Size.X / (float)texture.Size.Y;
            camera.FixedUp          = cameraFixedUp;
            camera.Position         = cameraPosition;
            camera.Direction        = cameraDirection;
            camera.Fov               = cameraFov;
            camera.ProjectionType    = cameraProjectionType;
            camera.OrthoWindowHeight = cameraOrthoWindowHeight;

            viewport = renderTexture.AddViewport(camera);

            //Initialize HDR compositor for HDR render technique
            if (EngineApp.RenderTechnique == "HDR")
            {
                viewport.AddCompositor("HDR", 0);
                viewport.SetCompositorEnabled("HDR", true);
            }

            //Initialize Fast Approximate Antialiasing (FXAA)
            {
                bool   useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering;
                string fsaa   = RendererWorld.InitializationOptions.FullSceneAntialiasing;
                if ((useMRT && (fsaa == "" || fsaa == "RecommendedSetting") && IsActivateFXAAByDefault()) ||
                    fsaa == "FXAA")
                {
                    if (RenderSystem.Instance.HasShaderModel3())
                    {
                        InitializeFXAACompositor();
                    }
                }
            }

            //add listener
            renderTargetListener = new ViewRenderTargetListener(this);
            renderTexture.AddListener(renderTargetListener);

            if (guiRenderer == null)
            {
                guiRenderer = new GuiRenderer(viewport);
            }
            else
            {
                guiRenderer.ChangeViewport(viewport);
            }

            if (controlManager == null)
            {
                controlManager = new ScreenControlManager(guiRenderer);
            }

            //initialize D3DImage output
            if (d3dImageIsSupported && allowUsingD3DImage)
            {
                // create a D3DImage to host the scene and monitor it for changes in front buffer availability
                if (d3dImage == null)
                {
                    d3dImage = new D3DImage();
                    d3dImage.IsFrontBufferAvailableChanged += D3DImage_IsFrontBufferAvailableChanged;
                    CompositionTarget.Rendering            += D3DImage_OnRendering;
                }

                // set output to background image
                Background = new ImageBrush(d3dImage);

                // set the back buffer using the new scene pointer
                HardwarePixelBuffer            buffer = texture.GetBuffer(0, 0);
                GetD3D9HardwarePixelBufferData data   = new GetD3D9HardwarePixelBufferData();
                data.hardwareBuffer = buffer._GetRealObject();
                data.outPointer     = IntPtr.Zero;
                unsafe
                {
                    GetD3D9HardwarePixelBufferData *pData = &data;
                    if (!RenderSystem.Instance.CallCustomMethod("Get D3D9HardwarePixelBuffer getSurface", (IntPtr)pData))
                    {
                        Log.Fatal("Get D3D9HardwarePixelBuffer getSurface failed.");
                    }
                }
                d3dImage.Lock();
                d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, data.outPointer);
                d3dImage.Unlock();
            }

            return(true);
        }
        private void OpenSharedResourceIfNeeded(DirectXResource res)
        {
            try
            {
                bool reinit = false;
                _occludedCounter++;

                if (_occludedCounter % 1 == 0) // every 200 ms
                {
                    var  state         = _device?.CheckDeviceState(_windowHandle) ?? DeviceState.Ok;
                    bool occludedState = state == DeviceState.PresentOccluded; // happens when windows is locked; then we could get black screen in preview d3dImage

                    reinit = _occludedState && !occludedState;                 // reinit if change from bad -> good
                    if (reinit)
                    {
                        Core.LogInfo("occluded -> normal");

                        if (_windowStateManager.IsMinimized())
                        {
                            _windowStateManager.IsMinimizedChanged += OnwindowStateManagerIsMinimizedChanged;
                        }
                        else
                        {
                            _ = ReinitSurfacesWithDelay();
                        }
                    }

                    if (!_occludedState && occludedState)
                    {
                        Core.LogInfo("normal -> occluded");
                    }

                    _occludedState = occludedState;
                }

                if (_sharedResource == null ||
                    _sharedResource.Description.Width != res.Texture2D.Description.Width ||
                    _sharedResource.Description.Height != res.Texture2D.Description.Height ||
                    _sharedResourceOwner != res.GetDx() ||
                    _reinitSurfaces)
                {
                    Core.LogInfo("Initing DX Presenter surface");
                    Source          = null;
                    _reinitSurfaces = false;

                    _surfaceD9?.Dispose();
                    _sharedResource?.Dispose();

                    _surfaceD9           = null;
                    _sharedResource      = null;
                    _sharedResourceOwner = null;


                    IntPtr handle = IntPtr.Zero;
                    _surfaceD9 = Surface.CreateRenderTarget(_device, res.Texture2D.Description.Width, res.Texture2D.Description.Height, Format.A8R8G8B8, MultisampleType.None, 0, true, ref handle);
                    if (handle == IntPtr.Zero)
                    {
                        Core.LogWarning("DirectX 9 Device failed to create Surface. Reinit Devices");
                        _device?.Dispose();
                        _direct3d?.Dispose();
                        _device   = null;
                        _direct3d = null;
                        InitDx9();
                        _surfaceD9 = Surface.CreateRenderTarget(_device, res.Texture2D.Description.Width, res.Texture2D.Description.Height, Format.A8R8G8B8, MultisampleType.None, 0, true, ref handle);

                        if (handle == IntPtr.Zero)
                        {
                            Core.LogWarning("DirectX 9 Device failed to create Surface after recreation.");
                        }
                    }
                    _sharedResource      = res.GetDx().Device.OpenSharedResource <Texture2D>(handle);
                    _sharedResourceOwner = res.GetDx();

                    _d3dimage = new D3DImage();
                    _d3dimage.Lock();
                    _d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _surfaceD9.NativePointer);
                    _d3dimage.Unlock();

                    Core.LogInfo("Inited DX Presenter surface");
                }
            }
            catch (Exception e)
            {
                Core.LogError(e, "Failed to open shared resource");
            }
        }
        void StartVideo()
        {
            context     = MainWindow.CreateEngine(new WindowInteropHelper(this).Handle, cb);
            cimg.Source = img;
            img.Lock();
            img.SetBackBuffer(D3DResourceType.IDirect3DSurface9, context.surface);
            img.Unlock();
            System.Threading.Thread mthread = new System.Threading.Thread(delegate() {
                BinaryReader mreader = new BinaryReader(str);
                while (true)
                {
                    try
                    {
                        switch (mreader.ReadByte())
                        {
                        case 0:
                            //Video frame
                            long timestamp = mreader.ReadInt64();
                            byte[] packet  = mreader.ReadBytes(mreader.ReadInt32());
                            unsafe
                            {
                                fixed(byte *pptr = packet)
                                {
                                    MainWindow.SendPacket(context.handle, new IntPtr(pptr), packet.Length);
                                }
                            }
                            break;

                        case 12:
                            Dispatcher.Invoke(() => {
                                if (trustedComputer.IsChecked.Value)
                                {
                                    System.Windows.Forms.Cursor.Hide();
                                }
                            });
                            break;

                        case 13:
                            Dispatcher.Invoke(() => {
                                if (trustedComputer.IsChecked.Value)
                                {
                                    System.Windows.Forms.Cursor.Show();
                                }
                            });
                            break;

                        case 14:
                            ulong forme = mreader.ReadUInt64();
                            Dispatcher.Invoke(() => {
                                if (trustedComputer.IsChecked.Value)
                                {
                                    if (ulongforcursors.ContainsKey(forme))
                                    {
                                        LoadCursor(forme);
                                    }
                                    else
                                    {
                                        BinaryWriter mwriter = new BinaryWriter(str);
                                        mwriter.Write((byte)16);
                                        mwriter.Write(forme);
                                    }
                                }
                            });
                            break;

                        case 15:
                            {
                                ulong foranothercursor = mreader.ReadUInt64();
                                MemoryStream cstr      = new MemoryStream(mreader.ReadBytes(mreader.ReadInt32()));
                                Dispatcher.Invoke(() => {
                                    Cursor curses = new Cursor(cstr);
                                    ulongforcursors[foranothercursor] = curses;
                                    LoadCursor(foranothercursor);
                                });
                            }
                            break;

                        case 16:
                            break;     //RESERVED. DO NOT USE.

                        default:
                            throw new Exception("Unsupported operation");
                            break;
                        }
                    }
                    catch (Exception er)
                    {
                        Dispatcher.Invoke(Close);
                        break;
                    }
                }
            });
            mthread.Start();
        }
Beispiel #22
0
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var hr = Direct3DCreate9Ex(D3D_SDK_VERSION, out var direct3D9Ex);

            m_d3dEx = direct3D9Ex;

            var adapterMonitor = direct3D9Ex.GetAdapterMonitor(0);

            m_hWnd = GetDesktopWindow();

            var param = new D3DPRESENT_PARAMETERS
            {
                Windowed = 1,
                Flags    = ((short)D3DPRESENTFLAG.D3DPRESENTFLAG_VIDEO),

                /*
                 * D3DFMT_R8G8B8:表示一个24位像素,从左开始,8位分配给红色,8位分配给绿色,8位分配给蓝色。
                 *
                 * D3DFMT_X8R8G8B8:表示一个32位像素,从左开始,8位不用,8位分配给红色,8位分配给绿色,8位分配给蓝色。
                 *
                 * D3DFMT_A8R8G8B8:表示一个32位像素,从左开始,8位为ALPHA通道,8位分配给红色,8位分配给绿色,8位分配给蓝色。
                 *
                 * D3DFMT_A16B16G16R16F:表示一个64位浮点像素,从左开始,16位为ALPHA通道,16位分配给蓝色,16位分配给绿色,16位分配给红色。
                 *
                 * D3DFMT_A32B32G32R32F:表示一个128位浮点像素,从左开始,32位为ALPHA通道,32位分配给蓝色,32位分配给绿色,32位分配给红色。
                 */
                //BackBufferFormat = D3DFORMAT.D3DFMT_X8R8G8B8,

                //SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_COPY
                SwapEffect = D3DSWAPEFFECT.D3DSWAPEFFECT_DISCARD,

                hDeviceWindow        = GetDesktopWindow(), // 添加
                PresentationInterval = (int)D3D9.PresentInterval.Default,
            };

            /* The COM pointer to our D3D Device */
            IntPtr dev;

            m_d3dEx.CreateDeviceEx(0, D3DDEVTYPE.D3DDEVTYPE_HAL, m_hWnd,
                                   Direct3D.CreateFlags.D3DCREATE_HARDWARE_VERTEXPROCESSING | Direct3D.CreateFlags.D3DCREATE_MULTITHREADED
                                   | Direct3D.CreateFlags.D3DCREATE_FPU_PRESERVE,
                                   ref param, IntPtr.Zero, out dev);

            m_device = (IDirect3DDevice9)Marshal.GetObjectForIUnknown(dev);
            // 只是减少引用计数而已,现在换成 m_device 了
            Marshal.Release(dev);

            hr = m_device.TestCooperativeLevel();
            var pDevice = dev;

            D3D11.Texture2D d3d11Texture2D = CreateRenderTarget();
            //SetRenderTarget(d3d11Texture2D);

            var format = TranslateFormat(TranslateFormat(d3d11Texture2D));

            var dxgiResource  = d3d11Texture2D.QueryInterface <DXGI.Resource>();
            var pSharedHandle = dxgiResource.SharedHandle;

            hr = m_device.CreateTexture(ImageWidth,
                                        ImageHeight,
                                        1,
                                        1,
                                        format,
                                        0,
                                        out m_privateTexture,
                                        ref pSharedHandle);

            hr = m_privateTexture.GetSurfaceLevel(0, out m_privateSurface);

            var backBuffer = Marshal.GetIUnknownForObject(m_privateSurface);

            var surface        = new D3D9.Surface(backBuffer);
            var queryInterface = surface.QueryInterface <D3D9.Surface>();
            //// 只是减少引用计数而已
            //Marshal.Release(backBuffer);

            //hr = m_device.SetTexture(0, m_privateTexture);

            var texturePtr = Marshal.GetIUnknownForObject(m_privateTexture);

            //Marshal.Release(texturePtr);

            //var byteList = new byte[32 * 10];
            //for (int i = 0; i < byteList.Length; i++)
            //{
            //    byteList[i] = (byte)i;
            //}

            //unsafe
            //{
            //    fixed (void* p = byteList)
            //    {
            //        Buffer.MemoryCopy(p, (void*) texturePtr,0,320);
            //    }
            //}

            //var d2dFactory = new SharpDX.Direct2D1.Factory();

            //Texture2D backBufferTexture2D = new Texture2D(texturePtr);
            //var d2dRenderTarget = new RenderTarget(d2dFactory, new SharpDX.DXGI.Surface(backBuffer),
            //    new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown,AlphaMode.Premultiplied)));

            //d2dRenderTarget.BeginDraw();
            //d2dRenderTarget.Clear(new RawColor4(1,0,0.5f,1));
            //d2dRenderTarget.EndDraw();

            D3DImage.Lock();
            D3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, backBuffer, true);
            D3DImage.Unlock();

            Render();

            string s             = "123";
            var    stringBuilder = new StringBuilder(s);

            stringBuilder.Replace("%", "%25").Replace("#", "%23");
            stringBuilder.Insert(0, "123");
            stringBuilder.Insert("123".Length, "#");
        }