/// <summary> /// Create D3D9Ex context and OpenGL framebuffer /// </summary> /// <param name="handle"></param> /// <param name="hasDepthBuffer"></param> /// <param name="hasStencilBuffer"></param> private void CreateD3D9ExContext(IntPtr handle, bool hasDepthBuffer, bool hasStencilBuffer) { var d3d = new Direct3DEx(); int initW = Size.Width; int initH = Size.Height; this.presentparams = new PresentParameters(); this.presentparams.Windowed = true; this.presentparams.SwapEffect = SwapEffect.Discard; this.presentparams.DeviceWindowHandle = handle; this.presentparams.PresentationInterval = PresentInterval.Default; // FpuPreserve for WPF // Multithreaded so that resources are actually sharable between DX and GL this.device = new DeviceEx( d3d, 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded | CreateFlags.FpuPreserve, this.presentparams); // create shared surface this.sharedSurface = Surface.CreateRenderTarget( this.device, initW, initH, Format.A8R8G8B8, MultisampleType.None, 0, false); #region OpenGL Interop this.wgl = new WGL_NV_DX_interop(); this.wglHandleDevice = wgl.WglDXOpenDeviceNV(device.NativePointer); this.glSharedSurface = GL.GenTexture(); this.fbo = GL.GenFramebuffer(); // refer D3D9 surface and OpenGL Texture2D this.wglHandleSharedSurface = wgl.WglDXRegisterObjectNV( this.wglHandleDevice, this.sharedSurface.NativePointer, (uint)this.glSharedSurface, (uint)TextureTarget.Texture2D, WGL_NV_DX_interop.WGL_ACCESS_READ_WRITE_NV); this.singleWglHandleSharedSurfaceArray = new IntPtr[] { this.wglHandleSharedSurface }; // setup framebuffer Console.WriteLine(GL.GetError()); GL.BindFramebuffer(FramebufferTarget.Framebuffer, this.fbo); Console.WriteLine(GL.GetError()); GL.FramebufferTexture2D( FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, this.glSharedSurface, 0); Console.WriteLine(GL.GetError()); if (hasDepthBuffer) { var depth = GL.GenRenderbuffer(); GL.FramebufferRenderbuffer( FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, depth); GL.DeleteRenderbuffer(depth); } if (hasStencilBuffer) { var stencil = GL.GenRenderbuffer(); GL.FramebufferRenderbuffer( FramebufferTarget.Framebuffer, FramebufferAttachment.StencilAttachment, RenderbufferTarget.Renderbuffer, stencil); GL.DeleteRenderbuffer(stencil); } GL.DrawBuffer((DrawBufferMode)FramebufferAttachment.ColorAttachment0); #endregion }
internal static extern Boolean wglDXObjectAccessNV(IntPtr hObject, WGL_NV_DX_interop access);
internal static extern IntPtr wglDXRegisterObjectNV(IntPtr hDevice, IntPtr dxObject, uint name, uint type, WGL_NV_DX_interop access);
public static bool DXObjectAccessNV(IntPtr hObject, WGL_NV_DX_interop access) { throw new BindingsNotRewrittenException(); }
public static IntPtr DXRegisterObjectNV(IntPtr hDevice, IntPtr dxObject, uint name, uint type, WGL_NV_DX_interop access) { throw new BindingsNotRewrittenException(); }