1 #include "BridgeFBO.hxx"
3 #include <OpenGl_ArbFBO.hxx>
5 #include <Graphic3d_TextureParams.hxx>
9 // list of required libraries
10 #pragma comment(lib, "TKOpenGl.lib")
11 #pragma comment(lib, "opengl32.lib")
13 // =======================================================================
16 // =======================================================================
17 Standard_Boolean BridgeFBO::Init (const Handle(OpenGl_Context)& theGlContext,
20 const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
21 if (aFuncs->wglDXOpenDeviceNV == NULL)
23 return Standard_False;
26 myGlD3DHandle = aFuncs->wglDXOpenDeviceNV (theD3DDevice);
27 if (myGlD3DHandle == NULL)
29 std::cerr << "Could not create the GL <-> DirectX Interop" << std::endl;
30 return Standard_False;
33 theGlContext->arbFBO->glGenFramebuffers (1, &myGlFBufferId);
37 // =======================================================================
40 // =======================================================================
41 void BridgeFBO::Release (OpenGl_Context* theGlContext)
43 if (myGlD3DHandle != NULL)
45 const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
46 if (myGlD3DSharedColorHandle != NULL)
48 aFuncs->wglDXUnregisterObjectNV (myGlD3DHandle, myGlD3DSharedColorHandle);
49 myGlD3DSharedColorHandle = NULL;
52 aFuncs->wglDXCloseDeviceNV (myGlD3DHandle);
56 OpenGl_FrameBuffer::Release (theGlContext);
59 // =======================================================================
60 // function : RegisterD3DColorBuffer
62 // =======================================================================
63 Standard_Boolean BridgeFBO::RegisterD3DColorBuffer (const Handle(OpenGl_Context)& theGlContext,
67 const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
68 if (myGlD3DSharedColorHandle != NULL)
70 if (!aFuncs->wglDXUnregisterObjectNV (myGlD3DHandle, myGlD3DSharedColorHandle))
72 return Standard_False;
74 myGlD3DSharedColorHandle = NULL;
77 if (!aFuncs->wglDXSetResourceShareHandleNV (theD3DBuffer, theBufferShare))
79 return Standard_False;
82 myColorTexture->Release (theGlContext.operator->());
83 myColorTexture->Create (theGlContext);
85 myGlD3DSharedColorHandle = aFuncs->wglDXRegisterObjectNV (myGlD3DHandle,
86 theD3DBuffer, myColorTexture->TextureId(), GL_TEXTURE_2D, WGL_ACCESS_WRITE_DISCARD_NV);
88 if (myGlD3DSharedColorHandle == NULL)
90 std::cerr << "Could not register color buffer" << std::endl;
91 return Standard_False;
97 // =======================================================================
100 // =======================================================================
101 void BridgeFBO::Resize (const Handle(OpenGl_Context)& theGlContext,
105 myVPSizeX = theSizeX;
106 myVPSizeY = theSizeY;
108 myDepthStencilTexture->Init (theGlContext, GL_DEPTH24_STENCIL8,
109 GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
110 myVPSizeX, myVPSizeY, Graphic3d_TOT_2D);
113 // =======================================================================
114 // function : BindBuffer
116 // =======================================================================
117 void BridgeFBO::BindBuffer (const Handle(OpenGl_Context)& theGlContext)
119 if (myGlD3DSharedColorHandle != NULL)
122 const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
123 aFuncs->wglDXLockObjectsNV (myGlD3DHandle, 1, &myGlD3DSharedColorHandle);
126 OpenGl_FrameBuffer::BindBuffer (theGlContext);
127 theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
128 GL_TEXTURE_2D, myColorTexture->TextureId(), 0);
129 theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
130 GL_TEXTURE_2D, myDepthStencilTexture->TextureId(), 0);
131 if (theGlContext->arbFBO->glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
133 std::cerr << "Invalid FBO can not be bound!\n";
134 Release (theGlContext.operator->());
138 // =======================================================================
139 // function : UnbindBuffer
141 // =======================================================================
142 void BridgeFBO::UnbindBuffer (const Handle(OpenGl_Context)& theGlContext)
144 // Unbind the frame buffer
145 OpenGl_FrameBuffer::UnbindBuffer (theGlContext);
146 if (myGlD3DSharedColorHandle != NULL)
148 // Unlock for Direct3D
149 const OpenGl_GlFunctions* aFuncs = (const OpenGl_GlFunctions* )theGlContext->core11;
150 aFuncs->wglDXUnlockObjectsNV (myGlD3DHandle, 1, &myGlD3DSharedColorHandle);