1 // Created on: 2015-06-10
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2015 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef _D3DHost_FrameBuffer_HeaderFile
17 #define _D3DHost_FrameBuffer_HeaderFile
18 
19 #include <OpenGl_FrameBuffer.hxx>
20 
21 struct IDirect3DDevice9;
22 struct IDirect3DSurface9;
23 
24 //! Implements bridge FBO for direct rendering to Direct3D surfaces.
25 class D3DHost_FrameBuffer : public OpenGl_FrameBuffer
26 {
27 public:
28 
29   //! Empty constructor.
30   Standard_EXPORT D3DHost_FrameBuffer();
31 
32   //! Destructor, should be called after Release().
33   Standard_EXPORT ~D3DHost_FrameBuffer();
34 
35   //! Releases D3D and OpenGL resources.
36   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
37 
38   //! Initializes OpenGL FBO for Direct3D interoperability or in fallback mode.
39   //! Color pixel format is always GL_RGBA8/D3DFMT_X8R8G8B8, no MSAA; depth-stencil pixel format is GL_DEPTH24_STENCIL8.
40   //! @param theGlCtx       currently bound OpenGL context
41   //! @param theD3DDevice   d3d9 device
42   //! @param theIsD3dEx     d3d9 extended flag (for creating shared texture resource)
43   //! @param theSizeX       texture width
44   //! @param theSizeY       texture height
45   //! @return true on success
46   Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theCtx,
47                                          IDirect3DDevice9*             theD3DDevice,
48                                          const Standard_Boolean        theIsD3dEx,
49                                          const Standard_Integer        theSizeX,
50                                          const Standard_Integer        theSizeY);
51 
52   //! Initializes OpenGL FBO for Direct3D interoperability.
53   //! Color pixel format is always GL_RGBA8/D3DFMT_X8R8G8B8, no MSAA.
54   //! @param theGlCtx       currently bound OpenGL context
55   //! @param theD3DDevice   d3d9 device
56   //! @param theIsD3dEx     d3d9 extended flag (for creating shared texture resource)
57   //! @param theSizeX       texture width
58   //! @param theSizeY       texture height
59   //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
60   //! @return true on success
61   Standard_EXPORT Standard_Boolean InitD3dInterop (const Handle(OpenGl_Context)& theCtx,
62                                                    IDirect3DDevice9*             theD3DDevice,
63                                                    const Standard_Boolean        theIsD3dEx,
64                                                    const Standard_Integer        theSizeX,
65                                                    const Standard_Integer        theSizeY,
66                                                    const Standard_Integer        theDepthFormat);
67 
68   //! Initializes OpenGL FBO + Direct3D surface for copying memory using fallback.
69   //! Color pixel format is always GL_RGBA8/D3DFMT_X8R8G8B8, no MSAA.
70   //! @param theGlCtx       currently bound OpenGL context
71   //! @param theD3DDevice   d3d9 device
72   //! @param theIsD3dEx     d3d9 extended flag (for creating shared texture resource)
73   //! @param theSizeX       texture width
74   //! @param theSizeY       texture height
75   //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
76   //! @return true on success
77   Standard_EXPORT Standard_Boolean InitD3dFallback (const Handle(OpenGl_Context)& theCtx,
78                                                     IDirect3DDevice9*             theD3DDevice,
79                                                     const Standard_Boolean        theIsD3dEx,
80                                                     const Standard_Integer        theSizeX,
81                                                     const Standard_Integer        theSizeY,
82                                                     const Standard_Integer        theDepthFormat);
83 
84   //! Binds Direct3D color buffer to OpenGL texture.
85   Standard_EXPORT Standard_Boolean registerD3dBuffer (const Handle(OpenGl_Context)& theCtx);
86 
87   //! Binds Direct3D objects for OpenGL drawing.
88   //! Should be called before LockSurface() and followed by UnlockSurface();
89   Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theCtx) Standard_OVERRIDE;
90 
91   //! Acquires D3D resource for OpenGL usage.
92   Standard_EXPORT virtual void LockSurface   (const Handle(OpenGl_Context)& theCtx);
93 
94   //! Releases D3D resource.
95   Standard_EXPORT virtual void UnlockSurface (const Handle(OpenGl_Context)& theCtx);
96 
97   //! Returns D3D surface used as color buffer.
D3dColorSurface()98   IDirect3DSurface9* D3dColorSurface()      { return myD3dSurf; }
99 
100   //! Returns WDDM handle for D3D color surface.
D3dColorSurfaceShare()101   void*              D3dColorSurfaceShare() { return myD3dSurfShare; }
102 
103   //! Returns TRUE if FBO has been initialized without WGL/D3D interop.
D3dFallback() const104   Standard_Boolean   D3dFallback() const { return myD3dFallback; }
105 
106   //! Returns TRUE if color buffer is sRGB ready; FALSE by default.
107   //! Requires D3DSAMP_SRGBTEXTURE sampler parameter being set on D3D level for rendering D3D surface.
IsSRGBReady() const108   Standard_Boolean IsSRGBReady() const { return myIsSRGBReady; }
109 
110   //! Set if color buffer is sRGB ready.
SetSRGBReady(Standard_Boolean theIsReady)111   void SetSRGBReady (Standard_Boolean theIsReady) { myIsSRGBReady = theIsReady; }
112 
113 protected:
114 
115   using OpenGl_FrameBuffer::Init;
116 
117 protected:
118 
119   IDirect3DSurface9* myD3dSurf;      //!< D3D surface
120   void*              myD3dSurfShare; //!< D3D surface share handle in WDDM
121   void*              myGlD3dDevice;  //!< WGL/D3D device  handle
122   void*              myGlD3dSurf;    //!< WGL/D3D surface handle
123   Standard_Integer   myLockCount;    //!< locking counter
124   Standard_Boolean   myD3dFallback;  //!< indicates that FBO has been initialized without WGL/D3D interop
125   Standard_Boolean   myIsSRGBReady;  //!< indicates that color buffer is sRGB ready
126 
127 public:
128 
129   DEFINE_STANDARD_RTTIEXT(D3DHost_FrameBuffer,OpenGl_FrameBuffer)
130 
131 };
132 
133 DEFINE_STANDARD_HANDLE(D3DHost_FrameBuffer, OpenGl_FrameBuffer)
134 
135 #endif // _D3DHost_FrameBuffer_HeaderFile
136