/// <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(); } } }