private void SetSharedSurfaceToD3DImage() { _image.Lock(); _image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _d3d_surface); _image.AddDirtyRect(new Int32Rect(0, 0, _image.PixelWidth, _image.PixelHeight)); _image.Unlock(); }
private void OnRender(object sender, EventArgs e) { _gameTime.ElapsedGameTime = _stopwatch.Elapsed; _gameTime.TotalGameTime += _gameTime.ElapsedGameTime; _stopwatch.Restart(); if (CanBeginDraw()) { try { _direct3DImage.Lock(); if (_renderTarget == null) { _renderTarget = CreateRenderTarget(); } if (_renderTarget != null) { GraphicsDevice.SetRenderTarget(_renderTarget); SetViewport(); _viewModel?.Update(_gameTime); _viewModel?.Draw(_gameTime); GraphicsDevice.Flush(); _direct3DImage.AddDirtyRect(new Int32Rect(0, 0, (int)ActualWidth, (int)ActualHeight)); } } finally { _direct3DImage.Unlock(); GraphicsDevice.SetRenderTarget(null); } } }
void CompositionTarget_Rendering(object sender, EventArgs e) { if (Draw != null && contentNeedsRefresh && BeginDraw()) { contentNeedsRefresh = false; D3dImage.Lock(); D3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, backbuffer = Interop.GetBackBuffer(GraphicsDeviceService.GraphicsDevice)); ResetGraphicsDeviceState(); SetViewport(); eventArgs.GraphicsDevice = GraphicsDeviceService.GraphicsDevice; eventArgs.DrawingSurface = this; eventArgs.TotalTime = stopWatch.Elapsed; eventArgs.DeltaTime = eventArgs.TotalTime - lastDrawTimestamp; lastDrawTimestamp = eventArgs.TotalTime; Draw(this, eventArgs); EndDraw(); D3dImage.AddDirtyRect(new Int32Rect(0, 0, (int)ActualWidth, (int)ActualHeight)); D3dImage.Unlock(); Marshal.Release(backbuffer); } }
/// <summary> /// Invalidates the entire Direct3D image, notifying WPF to redraw /// </summary> protected void InternalInvalidateVideoImage() { /* Ensure we run on the correct Dispatcher */ if (!D3DImage.Dispatcher.CheckAccess()) { D3DImage.Dispatcher.Invoke((Action)(() => InvalidateVideoImage())); return; } /* If there is a new Surface to set, * this method will do the trick */ SetBackBufferInternal(m_pBackBuffer); /* Only render the video image if possible, or if IsRenderingEnabled is true */ if (D3DImage.IsFrontBufferAvailable && IsRenderingEnabled && m_pBackBuffer != IntPtr.Zero) { try { /* Invalidate the entire image */ D3DImage.Lock(); D3DImage.AddDirtyRect(new Int32Rect(0, /* Left */ 0, /* Top */ D3DImage.PixelWidth, /* Width */ D3DImage.PixelHeight /* Height */)); D3DImage.Unlock(); } catch (Exception) { } } /* Invalidate all of our cloned D3DRenderers */ InvalidateClonedVideoImages(); }
private void D3DImage_OnRendering(object sender, EventArgs e) { if (renderTexture != null && !needCreateRenderTarget) { if (currentTextureSize != GetDemandTextureSize()) { needCreateRenderTarget = true; } } //render scene if (d3dImage.IsFrontBufferAvailable && renderTexture != null && !needCreateRenderTarget) { // lock the D3DImage d3dImage.Lock(); // when WPF's composition target is about to render, we update our // custom render target so that it can be blended with the WPF target RenderScene(null); // invalidate the updated region of the D3DImage (in this case, the whole image) if (texture != null && d3dImage.Height != 0) { d3dImage.AddDirtyRect(new Int32Rect(0, 0, texture.Size.X, texture.Size.Y)); } // unlock the D3DImage d3dImage.Unlock(); } }
public int OnNewSurfaceArrived() { _d3dImage.Dispatcher.Invoke(new Action(() => { if (_d3dImage.IsFrontBufferAvailable) { _d3dImage.Lock(); IntPtr pSurface; _pvpPresenter.GetBackBufferNoRef(out pSurface); if (pSurface != null) { // Repeatedly calling SetBackBuffer with the same IntPtr is // a no-op. There is no performance penalty. _d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSurface); _d3dImage.AddDirtyRect(new Int32Rect(0, 0, _d3dImage.PixelWidth, _d3dImage.PixelHeight)); } _d3dImage.Unlock(); } }), System.Windows.Threading.DispatcherPriority.Send); return(0); }
private void Compose() { if (_d3DImage == null) { return; } if (_d3DImage.IsFrontBufferAvailable && Graphics.GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Normal) { _d3DImage.Lock(); if (_compositionTargetSetBackBufferRequired) { _d3DImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _frontBufferPointer); _compositionTargetSetBackBufferRequired = false; } if (_frontBufferPointer != IntPtr.Zero) { _d3DImage.AddDirtyRect( new Int32Rect( 0, 0, _d3DImage.PixelWidth, _d3DImage.PixelHeight)); } _d3DImage.Unlock(); } }
private void DisplayAsTexture(DirectXResource res) { if (res.Texture2D == null) { Core.LogWarning("DisplayAsTexture failed as Texture2D == null"); return; } res.GetDx().RunOnContext(ctx => { ctx.CopyResource(res.Texture2D, _sharedResource); ctx.Flush(); }, "CopyToUI"); if (_d3dimage.TryLock(TimeSpan.FromMilliseconds(1500))) { _d3dimage.AddDirtyRect(new Int32Rect(0, 0, res.Texture2D.Description.Width, res.Texture2D.Description.Height)); _d3dimage.Unlock(); } else { Core.LogWarning("Failed to Lock DirectXPresenter/d3dimage"); } if (Source != _d3dimage) { Core.LogInfo("Assigning new D3DImage"); Source = _d3dimage; } }
public void InvalidateImageSource() { _d3dImage.Lock(); _d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _surface.NativePointer); _d3dImage.AddDirtyRect(new Int32Rect(0, 0, _d3dImage.PixelWidth, _d3dImage.PixelHeight)); _d3dImage.Unlock(); }
private void OnCompositionTargetRendering(object sender, EventArgs eventArgs) { if (_d3dImage == null) { return; } this.Present(); if (_d3dImage.IsFrontBufferAvailable && _deviceManager.GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Normal) { _d3dImage.Lock(); if (_compositionTargetSetBackBufferRequired) { _d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _frontBufferPointer); _compositionTargetSetBackBufferRequired = false; } if (_frontBufferPointer != IntPtr.Zero) { _d3dImage.AddDirtyRect( new Int32Rect( 0, 0, _d3dImage.PixelWidth, _d3dImage.PixelHeight)); } _d3dImage.Unlock(); } }
private void UpdateScene() { if (RenderImage.IsFrontBufferAvailable) { int w = System.Convert.ToInt32(RenderTarget.ActualWidth); int h = System.Convert.ToInt32(RenderTarget.ActualHeight); if (w == 0 || h == 0) { return; } RenderImage.Lock(); if (RenderImage.PixelWidth != w || RenderImage.PixelHeight != h) { RenderImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); Rush.RushResize(w, h); RenderSurface = Rush.RushGetBackBuffer(); RenderImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, RenderSurface); } Rush.RushUpdate(); if (RenderSurface != IntPtr.Zero) { Int32Rect rect = new Int32Rect(0, 0, w, h); RenderImage.AddDirtyRect(rect); } RenderImage.Unlock(); } }
private void CompositionTarget_Rendering(object sender, EventArgs e) { RenderingEventArgs args = (RenderingEventArgs)e; // It's possible for Rendering to call back twice in the same frame // so only render when we haven't already rendered in this frame. if (_d3dImage.IsFrontBufferAvailable && _lastRender != args.RenderingTime) { bool newSurfaceArrived; _pvpPresenter.HasNewSurfaceArrived(out newSurfaceArrived); if (newSurfaceArrived) { _d3dImage.Lock(); IntPtr pSurface; _pvpPresenter.GetBackBufferNoRef(out pSurface); if (pSurface != null) { // Repeatedly calling SetBackBuffer with the same IntPtr is // a no-op. There is no performance penalty. _d3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSurface); _d3dImage.AddDirtyRect(new Int32Rect(0, 0, _d3dImage.PixelWidth, _d3dImage.PixelHeight)); } _d3dImage.Unlock(); } _lastRender = args.RenderingTime; //System.Diagnostics.Debug.WriteLine("{0} : Repaint: {1}", args.RenderingTime, newSurfaceArrived); } }
public void UpdateSurface(D3DImage previewSurface, string bsChannelID, string bsEventName, string bsEventParam, object pEventObject) { if (bsEventName == "wpf_nextframe") { IntPtr pSurfaceIUnk = Marshal.GetIUnknownForObject(pEventObject); if (pSurfaceIUnk != SavedSurfaceIUnk) { if (SavedSurfaceIUnk != IntPtr.Zero) { Marshal.Release(SavedSurfaceIUnk); } SavedSurfaceIUnk = pSurfaceIUnk; Marshal.AddRef(SavedSurfaceIUnk); previewSurface.Lock(); previewSurface.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); previewSurface.SetBackBuffer(D3DResourceType.IDirect3DSurface9, SavedSurfaceIUnk); previewSurface.Unlock(); } if (pSurfaceIUnk != IntPtr.Zero) { Marshal.Release(pSurfaceIUnk); } previewSurface.Lock(); previewSurface.AddDirtyRect(new Int32Rect(0, 0, previewSurface.PixelWidth, previewSurface.PixelHeight)); previewSurface.Unlock(); } Marshal.ReleaseComObject(pEventObject); }
/// <summary> /// Marks the surface of element as invalid and requests its presentation on screen. /// </summary> internal void InvalidateRendering() { DisposedGuard(); image.Lock(); image.AddDirtyRect(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight)); image.Unlock(); }
void Swap() { _backBuffer.Flush(); _image.Lock(); _image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, _backBuffer?.Texture?.NativePointer ?? IntPtr.Zero, true); _image.AddDirtyRect(new Int32Rect(0, 0, _image.PixelWidth, _image.PixelHeight)); _image.Unlock(); }
void onPacket(long timestamp, IntPtr data, int len) { Dispatcher.Invoke(delegate() { img.Lock(); img.AddDirtyRect(new Int32Rect(0, 0, img.PixelWidth, img.PixelHeight)); img.Unlock(); }); }
private void CompositionTarget_Rendering(object sender, EventArgs e) { if (counter++ % 2 == 0) { return; } if (image == null) { return; } try { if (sizeChanged) { pp.BackBufferWidth = (int)ActualWidth; pp.BackBufferHeight = (int)ActualHeight; device.Reset(pp); sizeChanged = false; } if (image.IsFrontBufferAvailable) { Result result = Device.TestCooperativeLevel(); if (result.IsFailure) { throw new Direct3D9Exception(); } image.Lock(); device.SetRenderState(SlimDX.Direct3D9.RenderState.CullMode, Cull.None); device.SetRenderState(SlimDX.Direct3D9.RenderState.ZEnable, true); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(1, 1, 1, 1), 1.0f, 0); device.BeginScene(); try { Render.Raise(this); } catch (Exception exc) { Debug.WriteLine("Error in rendering in DirectXHost: " + exc.Message); } device.EndScene(); device.Present(); image.SetBackBuffer(D3DResourceType.IDirect3DSurface9, Device.GetBackBuffer(0, 0).ComPointer); image.AddDirtyRect(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight)); image.Unlock(); } } catch (Direct3D9Exception exc) { Device.Reset(pp); Debug.WriteLine("Exception in main render loop: " + exc.Message); } }
private void OnRendering(object sender, EventArgs e) { Result result; try { if (Device == null) { Initialize(StartThread); } if (sizeChanged) { pp.BackBufferWidth = (int)ActualWidth; pp.BackBufferHeight = (int)ActualHeight; Device.Reset(pp); OnDeviceReset(EventArgs.Empty); } if (d3dimage.IsFrontBufferAvailable) { result = Device.TestCooperativeLevel(); if (result.IsFailure) { throw new Direct3D9Exception(); } d3dimage.Lock(); Device.SetRenderState(RenderState.CullMode, Cull.None); Device.SetRenderState(RenderState.ZEnable, true); Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(0, 0, 0, 0), 1.0f, 0); Device.BeginScene(); components.Draw(timeManager.Current); OnMainLoop(EventArgs.Empty); Device.EndScene(); Device.Present(); d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, Device.GetBackBuffer(0, 0).ComPointer); d3dimage.AddDirtyRect(new Int32Rect(0, 0, d3dimage.PixelWidth, d3dimage.PixelHeight)); d3dimage.Unlock(); } } catch (Direct3D9Exception ex) { string msg = ex.Message; Initialize(StartThread); } sizeChanged = false; }
/// <summary> /// Marks the surface of element as invalid and requests its presentation on screen. /// </summary> internal void InvalidateRendering() { if (!isLoaded || texture == null) { return; } image.Lock(); image.AddDirtyRect(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight)); image.Unlock(); }
// Render the DirectX scene onto the D3DImage when WPF itself is ready to render private void CompositionTarget_Rendering(object sender, EventArgs e) { if (D3DImage.IsFrontBufferAvailable) { D3DImage.Lock(); PlatformMethods.Render(); // Invalidate the whole area: D3DImage.AddDirtyRect(new Int32Rect(0, 0, D3DImage.PixelWidth, D3DImage.PixelHeight)); D3DImage.Unlock(); } }
public void Compute() { if (mD3dImage == null) { return; } mD3dImage.Lock(); mD3dImage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, mSurface.NativePointer); mD3dImage.AddDirtyRect(new Int32Rect(0, 0, mD3dImage.PixelWidth, mD3dImage.PixelHeight)); mD3dImage.Unlock(); }
internal void NewFrame() { DispatchToUI(() => { if (_d3dImage.IsFrontBufferAvailable && _surface != IntPtr.Zero) { _d3dImage.Lock(); _d3dImage.AddDirtyRect(new Int32Rect(0, 0, _d3dImage.PixelWidth, _d3dImage.PixelHeight)); _d3dImage.Unlock(); } }); }
private void M_objPreview_OnEventSafe(string bsChannelID, string bsEventName, string bsEventParam, object pEventObject) { // specific name for event is "wpf_nextframe" if (bsEventName == "wpf_nextframe") { // it is necessary to keep a pointer in memory cause in case of format or source changes the pointer can be changed too IntPtr pSurfaceIUnk = Marshal.GetIUnknownForObject(pEventObject); if (pSurfaceIUnk != IntPtr.Zero) { if (pSurfaceIUnk != pSavedSurfaceIUnk) { // Release prev object if (pSavedSurfaceIUnk != IntPtr.Zero) { Marshal.Release(pSavedSurfaceIUnk); pSavedSurfaceIUnk = IntPtr.Zero; } // here we change back buffer of the surface (only in case of the pointer is changed) pSavedSurfaceIUnk = pSurfaceIUnk; Marshal.AddRef(pSavedSurfaceIUnk); // Lock and Unlock are required for update of the surface previewSource.Lock(); previewSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero); previewSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSavedSurfaceIUnk); previewSource.Unlock(); // use this 3D surface as source for preview control preview.Source = previewSource; //GC.Collect(); } Marshal.Release(pSurfaceIUnk); } Marshal.ReleaseComObject(pEventObject); // update of preview rectangle previewSource.Lock(); try { previewSource.AddDirtyRect(new Int32Rect(0, 0, previewSource.PixelWidth, previewSource.PixelHeight)); } catch (Exception) { previewSource.SetBackBuffer(D3DResourceType.IDirect3DSurface9, pSavedSurfaceIUnk); } previewSource.Unlock(); } }
private void CompositionTarget_Rendering(object?sender, EventArgs e) { _d2DRenderTarget.BeginDraw(); OnRender(_d2DRenderTarget); _d2DRenderTarget.EndDraw(); D3DImage.Lock(); D3DImage.AddDirtyRect(new Int32Rect(0, 0, D3DImage.PixelWidth, D3DImage.PixelHeight)); D3DImage.Unlock(); }
/// <summary> Initializes Direct3D-OCCT rendering. </summary> private void UpdateScene() { if (!myIsFailed && myD3DImage.IsFrontBufferAvailable && myColorSurf != IntPtr.Zero) { myD3DImage.Lock(); { // Update the scene (via a call into our custom library) Viewer.View.RedrawView(); // Invalidate the updated region of the D3DImage myD3DImage.AddDirtyRect(new Int32Rect(0, 0, mySize.cx, mySize.cy)); } myD3DImage.Unlock(); } }
//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(); } })); }
/// <summary> /// Invalidates the entire Direct3D image, notifying WPF to redraw /// </summary> protected void InternalInvalidateVideoImage() { /* Ensure we run on the correct Dispatcher */ if (!D3DImage.Dispatcher.CheckAccess()) { D3DImage.Dispatcher.BeginInvoke((Action)(() => InternalInvalidateVideoImage())); return; } /* If there is a new Surface to set, * this method will do the trick */ SetBackBufferInternal(m_pBackBuffer); // may save a few AddDirtyRect calls when the rendering thread is too busy // or RenderOnCompositionTargetRendering is set but the video is not playing bool invalid = GetSetVideoImageInvalid(false); if (!invalid) { return; } /* Only render the video image if possible, or if IsRenderingEnabled is true */ if (IsRenderingEnabled && m_pBackBuffer != IntPtr.Zero) { try { if (!D3DImage.TryLock(InvalidateVideoImageLockDuration)) { return; } /* Invalidate the entire image */ D3DImage.AddDirtyRect(new Int32Rect(0, /* Left */ 0, /* Top */ D3DImage.PixelWidth, /* Width */ D3DImage.PixelHeight /* Height */)); } catch (Exception) { } finally { D3DImage.Unlock(); } } /* Invalidate all of our cloned D3DRenderers */ InvalidateClonedVideoImages(); }
private void UpdateScene(double elasped) { if (_di.IsFrontBufferAvailable && _scene != IntPtr.Zero) { _emote.Update((float)elasped); // lock the D3DImage _di.Lock(); // update the scene (via a call into our custom library) _emote.D3DBeginScene(); _emote.Draw(); _emote.D3DEndScene(); // invalidate the updated region of the D3DImage (in this case, the whole image) _di.AddDirtyRect(new Int32Rect(0, 0, _emote.SurfaceWidth, _emote.SurfaceHeight)); // unlock the D3DImage _di.Unlock(); } }
private void UpdateScene() { if (_di.IsFrontBufferAvailable && _scene != IntPtr.Zero) { // lock the D3DImage _di.Lock(); // update the scene (via a call into our custom library) SIZE size = new SIZE(); RenderScene(size); // invalidate the updated region of the D3DImage (in this case, the whole image) _di.AddDirtyRect(new Int32Rect(0, 0, size.Width, size.Height)); // unlock the D3DImage _di.Unlock(); } }
public void RenderScene() { if (d3dImage == null) { return; } d3dImage.Lock(); if (BeginDraw()) { // Draw is overriden in derived classes Draw(); EndDraw(); } if (d3dImage.IsFrontBufferAvailable) { d3dImage.AddDirtyRect(new Int32Rect(0, 0, viewportWidth, viewportHeight)); d3dImage.Unlock(); } }