public void Resize(int width, int height)
        {
            _wglInterop = new WGLInterop();
            ReleaseResources();

            _device = new DeviceEx(
                new Direct3DEx(),
                0,
                DeviceType.Hardware,
                IntPtr.Zero,
                CreateFlags.HardwareVertexProcessing |
                CreateFlags.Multithreaded |
                CreateFlags.FpuPreserve,
                new PresentParameters()
            {
                Windowed             = true,
                SwapEffect           = SwapEffect.Discard,
                DeviceWindowHandle   = IntPtr.Zero,
                PresentationInterval = PresentInterval.Default,
                BackBufferFormat     = Format.Unknown,
                BackBufferWidth      = width,
                BackBufferHeight     = height
            });

            _surface = Surface.CreateRenderTarget(
                _device,
                width,
                height,
                Format.A8R8G8B8,
                MultisampleType.None,
                0,
                false,
                ref _dxSharedHandle);

            _fbo       = gl.GenFramebuffer();
            _glTexture = gl.GenTexture();

            _glHandle = _wglInterop.WglDXOpenDeviceNV(_device.NativePointer);
            _wglInterop.WglDXSetResourceShareHandleNV(_surface.NativePointer, _dxSharedHandle);

            var genHandle = _wglInterop.WglDXRegisterObjectNV(
                _glHandle,
                _surface.NativePointer,
                (uint)_glTexture,
                (uint)TextureTarget.Texture2D,
                WGLInterop.WGL_ACCESS_READ_WRITE_NV);

            _glHandles = new IntPtr[] { genHandle };

            gl.BindFramebuffer(FramebufferTarget.Framebuffer, _fbo);
            gl.FramebufferTexture2D(FramebufferTarget.Framebuffer,
                                    FramebufferAttachment.ColorAttachment0,
                                    TextureTarget.Texture2D,
                                    _glTexture,
                                    0);



            gl.DrawBuffer((DrawBufferMode)FramebufferAttachment.ColorAttachment0);
        }
Exemplo n.º 2
0
        public void Resize(int width, int height)
        {
            mGlHandle       = IntPtr.Zero;
            mDxSharedHandle = IntPtr.Zero;

            if (mFbo > -1)
            {
                gl.DeleteFramebuffer(mFbo);
            }
            mFbo = -1;
            if (mRboDepth > -1)
            {
                gl.DeleteRenderbuffer(mRboDepth);
            }
            mRboDepth = -1;
            if (mSharedTexture > -1)
            {
                gl.DeleteTexture(mSharedTexture);
            }
            mSharedTexture = -1;
            if (mSurface != null)
            {
                mSurface.Dispose();
            }
            mSurface = null;
            if (mDevice != null)
            {
                mDevice.Dispose();
            }
            mDevice = null;

            mWglInterop = new WGLInterop();
            mDevice     = new Device(DXInterop.Direct3DCreate9(DXInterop.D3D_SDK_VERSION));


            mDevice = new DeviceEx(
                new Direct3DEx(),
                0,
                DeviceType.Hardware,
                IntPtr.Zero,
                CreateFlags.HardwareVertexProcessing |
                CreateFlags.Multithreaded |
                CreateFlags.PureDevice,
                new PresentParameters()
            {
                Windowed             = true,
                SwapEffect           = SwapEffect.Discard,
                DeviceWindowHandle   = IntPtr.Zero,
                PresentationInterval = PresentInterval.Default,
                BackBufferFormat     = Format.Unknown,
                BackBufferWidth      = width,
                BackBufferHeight     = height
            });

            mSurface = Surface.CreateRenderTarget(
                mDevice,
                width,
                height,
                Format.A8R8G8B8,
                MultisampleType.None,
                0,
                false,
                ref mDxSharedHandle);

            mFbo           = gl.GenFramebuffer();
            mSharedTexture = gl.GenTexture();

            mGlHandle = mWglInterop.WglDXOpenDeviceNV(mDevice.NativePointer);
            mWglInterop.WglDXSetResourceShareHandleNV(mSurface.NativePointer, mDxSharedHandle);

            var genHandle = mWglInterop.WglDXRegisterObjectNV(
                mGlHandle,
                mSurface.NativePointer,
                (uint)mSharedTexture,
                (uint)TextureTarget.Texture2D,
                WGLInterop.WGL_ACCESS_READ_WRITE_NV);

            mGlHandles = new IntPtr[] { genHandle };

            gl.BindFramebuffer(FramebufferTarget.Framebuffer, mFbo);
            gl.FramebufferTexture2D(
                FramebufferTarget.Framebuffer,
                FramebufferAttachment.ColorAttachment0,
                TextureTarget.Texture2D,
                mSharedTexture, 0);

            mRboDepth = gl.GenRenderbuffer();
            gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, mRboDepth);
            gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, width, height);
            gl.FramebufferRenderbuffer(
                FramebufferTarget.Framebuffer,
                FramebufferAttachment.DepthAttachment,
                RenderbufferTarget.Renderbuffer,
                mRboDepth);

            gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }