b311480e |
1 | // Created by: Kirill GAVRILOV |
973c2be1 |
2 | // Copyright (c) 2011-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | #ifndef OPENGL_FRAME_BUFFER_H |
16 | #define OPENGL_FRAME_BUFFER_H |
17 | |
fd4a6963 |
18 | #include <OpenGl_Resource.hxx> |
7fd59977 |
19 | |
6cde53c4 |
20 | #include <Graphic3d_BufferType.hxx> |
1220d98e |
21 | #include <Graphic3d_Vec2.hxx> |
a1073ae2 |
22 | #include <NCollection_Vector.hxx> |
1220d98e |
23 | #include <NCollection_Sequence.hxx> |
24 | |
25 | class Image_PixMap; |
26 | class OpenGl_Texture; |
a1073ae2 |
27 | |
c04c30b3 |
28 | DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_Resource) |
a2e4f780 |
29 | |
a1073ae2 |
30 | //! Short declaration of useful collection types. |
1220d98e |
31 | typedef NCollection_Vector<Standard_Integer> OpenGl_ColorFormats; |
a1073ae2 |
32 | |
fd4a6963 |
33 | //! Class implements FrameBuffer Object (FBO) resource |
34 | //! intended for off-screen rendering. |
35 | class OpenGl_FrameBuffer : public OpenGl_Resource |
7fd59977 |
36 | { |
37 | |
38 | public: |
39 | |
40 | //! Helpful constants |
1220d98e |
41 | static const unsigned int NO_FRAMEBUFFER = 0; |
42 | static const unsigned int NO_RENDERBUFFER = 0; |
7fd59977 |
43 | |
6cde53c4 |
44 | public: |
45 | |
46 | //! Dump content into image. |
47 | //! @param theGlCtx bound OpenGL context |
48 | //! @param theFbo FBO to dump (or window buffer, if NULL) |
49 | //! @param theImage target image |
50 | //! @param theBufferType buffer type (attachment) to dump |
51 | //! @return TRUE on success |
52 | Standard_EXPORT static Standard_Boolean BufferDump (const Handle(OpenGl_Context)& theGlCtx, |
53 | const Handle(OpenGl_FrameBuffer)& theFbo, |
54 | Image_PixMap& theImage, |
55 | Graphic3d_BufferType theBufferType); |
56 | |
7fd59977 |
57 | public: |
58 | |
fd4a6963 |
59 | //! Empty constructor |
3c4b62a4 |
60 | Standard_EXPORT OpenGl_FrameBuffer(); |
7fd59977 |
61 | |
fd4a6963 |
62 | //! Destructor |
63 | Standard_EXPORT virtual ~OpenGl_FrameBuffer(); |
64 | |
65 | //! Destroy object - will release GPU memory if any. |
79104795 |
66 | Standard_EXPORT virtual void Release (OpenGl_Context* theGlCtx) Standard_OVERRIDE; |
7fd59977 |
67 | |
3c4b62a4 |
68 | //! Number of multisampling samples. |
1220d98e |
69 | Standard_Integer NbSamples() const { return myNbSamples; } |
3c4b62a4 |
70 | |
a1073ae2 |
71 | //! Number of color buffers. |
1220d98e |
72 | Standard_Integer NbColorBuffers() const { return myColorTextures.Length(); } |
a1073ae2 |
73 | |
3c4b62a4 |
74 | //! Return true if FBO has been created with color attachment. |
1220d98e |
75 | bool HasColor() const { return !myColorFormats.IsEmpty(); } |
3c4b62a4 |
76 | |
77 | //! Return true if FBO has been created with depth attachment. |
1220d98e |
78 | bool HasDepth() const { return myDepthFormat != 0; } |
79 | |
80 | //! Return textures width x height. |
81 | Graphic3d_Vec2i GetSize() const { return Graphic3d_Vec2i (GetSizeX(), GetSizeY()); } |
3c4b62a4 |
82 | |
18f4e8e2 |
83 | //! Textures width. |
1220d98e |
84 | Standard_EXPORT Standard_Integer GetSizeX() const; |
7fd59977 |
85 | |
18f4e8e2 |
86 | //! Textures height. |
1220d98e |
87 | Standard_EXPORT Standard_Integer GetSizeY() const; |
88 | |
89 | //! Return viewport width x height. |
90 | Graphic3d_Vec2i GetVPSize() const { return Graphic3d_Vec2i (myVPSizeX, myVPSizeY); } |
7fd59977 |
91 | |
92 | //! Viewport width. |
1220d98e |
93 | Standard_Integer GetVPSizeX() const { return myVPSizeX; } |
7fd59977 |
94 | |
95 | //! Viewport height. |
1220d98e |
96 | Standard_Integer GetVPSizeY() const { return myVPSizeY; } |
97 | |
98 | //! Return viewport width x height. |
99 | Graphic3d_Vec2i GetInitVPSize() const { return Graphic3d_Vec2i (myInitVPSizeX, myInitVPSizeY); } |
7fd59977 |
100 | |
a0b49de4 |
101 | //! Viewport width. |
1220d98e |
102 | Standard_Integer GetInitVPSizeX() const { return myInitVPSizeX; } |
a0b49de4 |
103 | |
104 | //! Viewport height. |
1220d98e |
105 | Standard_Integer GetInitVPSizeY() const { return myInitVPSizeY; } |
a0b49de4 |
106 | |
7fd59977 |
107 | //! Returns true if current object was initialized |
108 | Standard_Boolean IsValid() const |
109 | { |
18f4e8e2 |
110 | return isValidFrameBuffer(); |
7fd59977 |
111 | } |
112 | |
a1073ae2 |
113 | //! Initialize FBO for rendering into single/multiple color buffer and depth textures. |
114 | //! @param theGlCtx currently bound OpenGL context |
1220d98e |
115 | //! @param theSize texture width x height |
a1073ae2 |
116 | //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8 |
117 | //! @param theDepthStencilTexture depth-stencil texture |
118 | //! @param theNbSamples MSAA number of samples (0 means normal texture) |
119 | //! @return true on success |
120 | Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, |
1220d98e |
121 | const Graphic3d_Vec2i& theSize, |
a1073ae2 |
122 | const OpenGl_ColorFormats& theColorFormats, |
123 | const Handle(OpenGl_Texture)& theDepthStencilTexture, |
1220d98e |
124 | const Standard_Integer theNbSamples = 0); |
a1073ae2 |
125 | |
3c4b62a4 |
126 | //! Initialize FBO for rendering into textures. |
127 | //! @param theGlCtx currently bound OpenGL context |
1220d98e |
128 | //! @param theSize texture width x height |
3c4b62a4 |
129 | //! @param theColorFormat color texture sized format (0 means no color attachment), e.g. GL_RGBA8 |
130 | //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8 |
131 | //! @param theNbSamples MSAA number of samples (0 means normal texture) |
132 | //! @return true on success |
fd4a6963 |
133 | Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, |
1220d98e |
134 | const Graphic3d_Vec2i& theSize, |
135 | const Standard_Integer theColorFormat, |
136 | const Standard_Integer theDepthFormat, |
137 | const Standard_Integer theNbSamples = 0); |
7fd59977 |
138 | |
a1073ae2 |
139 | //! Initialize FBO for rendering into single/multiple color buffer and depth textures. |
140 | //! @param theGlCtx currently bound OpenGL context |
1220d98e |
141 | //! @param theSize texture width x height |
a1073ae2 |
142 | //! @param theColorFormats list of color texture sized format (0 means no color attachment), e.g. GL_RGBA8 |
143 | //! @param theDepthFormat depth-stencil texture sized format (0 means no depth attachment), e.g. GL_DEPTH24_STENCIL8 |
144 | //! @param theNbSamples MSAA number of samples (0 means normal texture) |
145 | //! @return true on success |
146 | Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theGlCtx, |
1220d98e |
147 | const Graphic3d_Vec2i& theSize, |
a1073ae2 |
148 | const OpenGl_ColorFormats& theColorFormats, |
1220d98e |
149 | const Standard_Integer theDepthFormat, |
150 | const Standard_Integer theNbSamples = 0); |
a1073ae2 |
151 | |
38a0206f |
152 | //! (Re-)initialize FBO with specified dimensions. |
153 | Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
1220d98e |
154 | const Graphic3d_Vec2i& theViewportSize, |
155 | const Standard_Integer theColorFormat, |
156 | const Standard_Integer theDepthFormat, |
157 | const Standard_Integer theNbSamples = 0); |
3c4b62a4 |
158 | |
a1073ae2 |
159 | //! (Re-)initialize FBO with specified dimensions. |
160 | Standard_EXPORT Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
1220d98e |
161 | const Graphic3d_Vec2i& theViewportSize, |
a1073ae2 |
162 | const OpenGl_ColorFormats& theColorFormats, |
1220d98e |
163 | const Standard_Integer theDepthFormat, |
164 | const Standard_Integer theNbSamples = 0); |
a1073ae2 |
165 | |
3c4b62a4 |
166 | //! (Re-)initialize FBO with properties taken from another FBO. |
167 | Standard_Boolean InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
168 | const OpenGl_FrameBuffer& theFbo) |
169 | { |
1220d98e |
170 | return InitLazy (theGlCtx, Graphic3d_Vec2i (theFbo.myVPSizeX, theFbo.myVPSizeY), theFbo.myColorFormats, theFbo.myDepthFormat, theFbo.myNbSamples); |
3c4b62a4 |
171 | } |
38a0206f |
172 | |
a2e4f780 |
173 | //! (Re-)initialize FBO with specified dimensions. |
174 | //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures). |
3c4b62a4 |
175 | //! @param theGlCtx currently bound OpenGL context |
1220d98e |
176 | //! @param theSize render buffer width x height |
3c4b62a4 |
177 | //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8 |
178 | //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8 |
a2e4f780 |
179 | //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class |
180 | Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx, |
1220d98e |
181 | const Graphic3d_Vec2i& theSize, |
182 | const Standard_Integer theColorFormat, |
183 | const Standard_Integer theDepthFormat, |
184 | const unsigned int theColorRBufferFromWindow = 0); |
a2e4f780 |
185 | |
186 | //! Initialize class from currently bound FBO. |
187 | //! Retrieved OpenGL objects will not be destroyed on Release. |
188 | Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx); |
189 | |
78c4e836 |
190 | //! Wrap existing color textures. |
191 | Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlContext, |
192 | const NCollection_Sequence<Handle(OpenGl_Texture)>& theColorTextures, |
193 | const Handle(OpenGl_Texture)& theDepthTexture = Handle(OpenGl_Texture)()); |
194 | |
7fd59977 |
195 | //! Setup viewport to render into FBO |
fd4a6963 |
196 | Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
197 | |
198 | //! Override viewport settings |
1220d98e |
199 | Standard_EXPORT void ChangeViewport (const Standard_Integer theVPSizeX, |
200 | const Standard_Integer theVPSizeY); |
7fd59977 |
201 | |
b86bb3df |
202 | //! Bind frame buffer for drawing and reading (to render into the texture). |
18f4e8e2 |
203 | Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
204 | |
b86bb3df |
205 | //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture). |
206 | Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx); |
207 | |
208 | //! Bind frame buffer for reading GL_READ_FRAMEBUFFER |
209 | Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx); |
210 | |
7fd59977 |
211 | //! Unbind frame buffer. |
18f4e8e2 |
212 | Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx); |
7fd59977 |
213 | |
a1073ae2 |
214 | //! Returns the color texture for the given color buffer index. |
1220d98e |
215 | const Handle(OpenGl_Texture)& ColorTexture (const Standard_Integer theColorBufferIndex = 0) const |
7fd59977 |
216 | { |
1220d98e |
217 | return myColorTextures.Value (theColorBufferIndex); |
7fd59977 |
218 | } |
219 | |
18f4e8e2 |
220 | //! Returns the depth-stencil texture. |
1220d98e |
221 | const Handle(OpenGl_Texture)& DepthStencilTexture() const { return myDepthStencilTexture; } |
222 | |
223 | //! Returns the color Render Buffer. |
224 | unsigned int ColorRenderBuffer() const { return myGlColorRBufferId; } |
225 | |
226 | //! Returns the depth Render Buffer. |
227 | unsigned int DepthStencilRenderBuffer() const { return myGlDepthRBufferId; } |
228 | |
229 | //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules. |
230 | Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE; |
231 | |
232 | public: |
233 | |
234 | //! Initialize FBO for rendering into single/multiple color buffer and depth textures. |
235 | Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i") |
236 | bool Init (const Handle(OpenGl_Context)& theGlCtx, |
237 | const Standard_Integer theSizeX, |
238 | const Standard_Integer theSizeY, |
239 | const OpenGl_ColorFormats& theColorFormats, |
240 | const Handle(OpenGl_Texture)& theDepthStencilTexture, |
241 | const Standard_Integer theNbSamples = 0) |
7fd59977 |
242 | { |
1220d98e |
243 | return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormats, theDepthStencilTexture, theNbSamples); |
7fd59977 |
244 | } |
245 | |
1220d98e |
246 | //! Initialize FBO for rendering into textures. |
247 | Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i") |
248 | bool Init (const Handle(OpenGl_Context)& theGlCtx, |
249 | const Standard_Integer theSizeX, |
250 | const Standard_Integer theSizeY, |
251 | const Standard_Integer theColorFormat, |
252 | const Standard_Integer theDepthFormat, |
253 | const Standard_Integer theNbSamples = 0) |
a2e4f780 |
254 | { |
1220d98e |
255 | return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormat, theDepthFormat, theNbSamples); |
a2e4f780 |
256 | } |
257 | |
1220d98e |
258 | //! Initialize FBO for rendering into single/multiple color buffer and depth textures. |
259 | Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i") |
260 | bool Init (const Handle(OpenGl_Context)& theGlCtx, |
261 | const Standard_Integer theSizeX, |
262 | const Standard_Integer theSizeY, |
263 | const OpenGl_ColorFormats& theColorFormats, |
264 | const Standard_Integer theDepthFormat, |
265 | const Standard_Integer theNbSamples = 0) |
a2e4f780 |
266 | { |
1220d98e |
267 | return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormats, theDepthFormat, theNbSamples); |
a2e4f780 |
268 | } |
269 | |
1220d98e |
270 | //! (Re-)initialize FBO with specified dimensions. |
271 | Standard_DEPRECATED("Obsolete method, use InitLazy() taking Graphic3d_Vec2i") |
272 | bool InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
273 | const Standard_Integer theViewportSizeX, |
274 | const Standard_Integer theViewportSizeY, |
275 | const Standard_Integer theColorFormat, |
276 | const Standard_Integer theDepthFormat, |
277 | const Standard_Integer theNbSamples = 0) |
278 | { |
279 | return InitLazy (theGlCtx, Graphic3d_Vec2i (theViewportSizeX, theViewportSizeY), theColorFormat, theDepthFormat, theNbSamples); |
280 | } |
281 | |
282 | //! (Re-)initialize FBO with specified dimensions. |
283 | Standard_DEPRECATED("Obsolete method, use InitLazy() taking Graphic3d_Vec2i") |
284 | bool InitLazy (const Handle(OpenGl_Context)& theGlCtx, |
285 | const Standard_Integer theViewportSizeX, |
286 | const Standard_Integer theViewportSizeY, |
287 | const OpenGl_ColorFormats& theColorFormats, |
288 | const Standard_Integer theDepthFormat, |
289 | const Standard_Integer theNbSamples = 0) |
290 | { |
291 | return InitLazy (theGlCtx, Graphic3d_Vec2i (theViewportSizeX, theViewportSizeY), theColorFormats, theDepthFormat, theNbSamples); |
292 | } |
293 | |
294 | //! (Re-)initialize FBO with specified dimensions. |
295 | //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures). |
296 | Standard_DEPRECATED("Obsolete method, use InitWithRB() taking Graphic3d_Vec2i") |
297 | bool InitWithRB (const Handle(OpenGl_Context)& theGlCtx, |
298 | const Standard_Integer theSizeX, |
299 | const Standard_Integer theSizeY, |
300 | const Standard_Integer theColorFormat, |
301 | const Standard_Integer theDepthFormat, |
302 | const unsigned int theColorRBufferFromWindow = 0) |
303 | { |
304 | return InitWithRB (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormat, theDepthFormat, theColorRBufferFromWindow); |
305 | } |
15669413 |
306 | |
18f4e8e2 |
307 | protected: |
308 | |
18f4e8e2 |
309 | Standard_Boolean isValidFrameBuffer() const |
b859a34d |
310 | { |
18f4e8e2 |
311 | return myGlFBufferId != NO_FRAMEBUFFER; |
b859a34d |
312 | } |
313 | |
a1073ae2 |
314 | protected: |
315 | |
316 | typedef NCollection_Vector<Handle(OpenGl_Texture)> OpenGl_TextureArray; |
317 | |
18f4e8e2 |
318 | protected: |
319 | |
1220d98e |
320 | Standard_Integer myInitVPSizeX; //!< viewport width specified during initialization (kept even on failure) |
321 | Standard_Integer myInitVPSizeY; //!< viewport height specified during initialization (kept even on failure) |
322 | Standard_Integer myVPSizeX; //!< viewport width (should be <= texture width) |
323 | Standard_Integer myVPSizeY; //!< viewport height (should be <= texture height) |
324 | Standard_Integer myNbSamples; //!< number of MSAA samples |
a1073ae2 |
325 | OpenGl_ColorFormats myColorFormats; //!< sized format for color texture, GL_RGBA8 by default |
1220d98e |
326 | Standard_Integer myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default |
327 | unsigned int myGlFBufferId; //!< FBO object ID |
328 | unsigned int myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture) |
329 | unsigned int myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture) |
a2e4f780 |
330 | bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class |
78c4e836 |
331 | bool myIsOwnColor; //!< flag indicating that color textures should be deallocated by this class |
332 | bool myIsOwnDepth; //!< flag indicating that depth texture should be deallocated by this class |
a1073ae2 |
333 | OpenGl_TextureArray myColorTextures; //!< color texture objects |
18f4e8e2 |
334 | Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object |
fd4a6963 |
335 | |
336 | public: |
337 | |
92efcf78 |
338 | DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer,OpenGl_Resource) // Type definition |
7fd59977 |
339 | |
7fd59977 |
340 | }; |
341 | |
fd4a6963 |
342 | #endif // OPENGL_FRAME_BUFFER_H |