1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef OPENGL_FRAME_BUFFER_H
16 #define OPENGL_FRAME_BUFFER_H
18 #include <OpenGl_Context.hxx>
19 #include <OpenGl_Resource.hxx>
20 #include <OpenGl_Texture.hxx>
22 #include <Graphic3d_BufferType.hxx>
23 #include <NCollection_Vector.hxx>
25 class OpenGl_FrameBuffer;
26 DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource)
28 //! Short declaration of useful collection types.
29 typedef NCollection_Vector<GLint> OpenGl_ColorFormats;
31 //! Class implements FrameBuffer Object (FBO) resource
32 //! intended for off-screen rendering.
33 class OpenGl_FrameBuffer : public OpenGl_Resource
39 static const GLuint NO_FRAMEBUFFER = 0;
40 static const GLuint NO_RENDERBUFFER = 0;
44 //! Dump content into image.
45 //! @param theGlCtx bound OpenGL context
46 //! @param theFbo FBO to dump (or window buffer, if NULL)
47 //! @param theImage target image
48 //! @param theBufferType buffer type (attachment) to dump
49 //! @return TRUE on success
50 Standard_EXPORT static Standard_Boolean BufferDump (const Handle(OpenGl_Context)& theGlCtx,
51 const Handle(OpenGl_FrameBuffer)& theFbo,
52 Image_PixMap& theImage,
53 Graphic3d_BufferType theBufferType);
58 Standard_EXPORT OpenGl_FrameBuffer();
61 Standard_EXPORT virtual ~OpenGl_FrameBuffer();
63 //! Destroy object - will release GPU memory if any.
64 Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE;
66 //! Number of multisampling samples.
67 GLsizei NbSamples() const
72 //! Number of color buffers.
73 GLsizei NbColorBuffers() const
75 return myColorTextures.Length();
78 //! Return true if FBO has been created with color attachment.
81 return !myColorFormats.IsEmpty();
84 //! Return true if FBO has been created with depth attachment.
87 return myDepthFormat != 0;
91 GLsizei GetSizeX() const
93 return myColorTextures (0)->SizeX();
97 GLsizei GetSizeY() const
99 return myColorTextures (0)->SizeY();
103 GLsizei GetVPSizeX() const
109 GLsizei GetVPSizeY() const
115 GLsizei GetInitVPSizeX() const
117 return myInitVPSizeX;
121 GLsizei GetInitVPSizeY() const
123 return myInitVPSizeY;
126 //! Returns true if current object was initialized
127 Standard_Boolean IsValid() const
129 return isValidFrameBuffer();
132 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
133 //! @param theGlCtx currently bound OpenGL context
134 //! @param theSizeX texture width
135 //! @param theSizeY texture height
136 //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
137 //! @param theDepthStencilTexture depth-stencil texture
138 //! @param theNbSamples MSAA number of samples (0 means normal texture)
139 //! @return true on success
140 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
141 const GLsizei theSizeX,
142 const GLsizei theSizeY,
143 const OpenGl_ColorFormats& theColorFormats,
144 const Handle(OpenGl_Texture)& theDepthStencilTexture,
145 const GLsizei theNbSamples = 0);
147 //! Initialize FBO for rendering into textures.
148 //! @param theGlCtx currently bound OpenGL context
149 //! @param theSizeX texture width
150 //! @param theSizeY texture height
151 //! @param theColorFormat color texture sized format (0 means no color attachment), e.g. GL_RGBA8
152 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
153 //! @param theNbSamples MSAA number of samples (0 means normal texture)
154 //! @return true on success
155 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
156 const GLsizei theSizeX,
157 const GLsizei theSizeY,
158 const GLint theColorFormat,
159 const GLint theDepthFormat,
160 const GLsizei theNbSamples = 0);
162 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
163 //! @param theGlCtx currently bound OpenGL context
164 //! @param theSizeX texture width
165 //! @param theSizeY texture height
166 //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8
167 //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8
168 //! @param theNbSamples MSAA number of samples (0 means normal texture)
169 //! @return true on success
170 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx,
171 const GLsizei theSizeX,
172 const GLsizei theSizeY,
173 const OpenGl_ColorFormats& theColorFormats,
174 const GLint theDepthFormat,
175 const GLsizei theNbSamples = 0);
177 //! (Re-)initialize FBO with specified dimensions.
178 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
179 const GLsizei theViewportSizeX,
180 const GLsizei theViewportSizeY,
181 const GLint theColorFormat,
182 const GLint theDepthFormat,
183 const GLsizei theNbSamples = 0);
185 //! (Re-)initialize FBO with specified dimensions.
186 Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
187 const GLsizei theViewportSizeX,
188 const GLsizei theViewportSizeY,
189 const OpenGl_ColorFormats& theColorFormats,
190 const GLint theDepthFormat,
191 const GLsizei theNbSamples = 0);
193 //! (Re-)initialize FBO with properties taken from another FBO.
194 Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx,
195 const OpenGl_FrameBuffer& theFbo)
197 return InitLazy (theGlCtx, theFbo.myVPSizeX, theFbo.myVPSizeY, theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples);
200 //! (Re-)initialize FBO with specified dimensions.
201 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
202 //! @param theGlCtx currently bound OpenGL context
203 //! @param theSizeX render buffer width
204 //! @param theSizeY render buffer height
205 //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8
206 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
207 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
208 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
209 const GLsizei theSizeX,
210 const GLsizei theSizeY,
211 const GLint theColorFormat,
212 const GLint theDepthFormat,
213 const GLuint theColorRBufferFromWindow = 0);
215 //! Initialize class from currently bound FBO.
216 //! Retrieved OpenGL objects will not be destroyed on Release.
217 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
219 //! Setup viewport to render into FBO
220 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
222 //! Override viewport settings
223 Standard_EXPORT void ChangeViewport (const GLsizei theVPSizeX,
224 const GLsizei theVPSizeY);
226 //! Bind frame buffer for drawing and reading (to render into the texture).
227 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
229 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
230 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
232 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
233 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
235 //! Unbind frame buffer.
236 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
238 //! Returns the color texture for the given color buffer index.
239 inline const Handle(OpenGl_Texture)& ColorTexture (const GLint theColorBufferIndex = 0) const
241 return myColorTextures (theColorBufferIndex);
244 //! Returns the depth-stencil texture.
245 inline const Handle(OpenGl_Texture)& DepthStencilTexture() const
247 return myDepthStencilTexture;
250 //! Returns the color Render Buffer.
251 GLuint ColorRenderBuffer() const
253 return myGlColorRBufferId;
256 //! Returns the depth Render Buffer.
257 GLuint DepthStencilRenderBuffer() const
259 return myGlDepthRBufferId;
262 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
263 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
267 Standard_Boolean isValidFrameBuffer() const
269 return myGlFBufferId != NO_FRAMEBUFFER;
274 typedef NCollection_Vector<Handle(OpenGl_Texture)> OpenGl_TextureArray;
278 GLsizei myInitVPSizeX; //!< viewport width specified during initialization (kept even on failure)
279 GLsizei myInitVPSizeY; //!< viewport height specified during initialization (kept even on failure)
280 GLsizei myVPSizeX; //!< viewport width (should be <= texture width)
281 GLsizei myVPSizeY; //!< viewport height (should be <= texture height)
282 GLsizei myNbSamples; //!< number of MSAA samples
283 OpenGl_ColorFormats myColorFormats; //!< sized format for color texture, GL_RGBA8 by default
284 GLint myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default
285 GLuint myGlFBufferId; //!< FBO object ID
286 GLuint myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
287 GLuint myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
288 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
289 bool myIsOwnDepth; //!< flag indicating that FBO should be deallocated by this class
290 OpenGl_TextureArray myColorTextures; //!< color texture objects
291 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
295 DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer,OpenGl_Resource) // Type definition
299 #endif // OPENGL_FRAME_BUFFER_H