0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.hxx
CommitLineData
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
5bd54bef 15#ifndef OpenGl_FrameBuffer_HeaderFile
16#define OpenGl_FrameBuffer_HeaderFile
7fd59977 17
ddb9ed48 18#include <OpenGl_NamedResource.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
25class Image_PixMap;
26class OpenGl_Texture;
a1073ae2 27
ddb9ed48 28DEFINE_STANDARD_HANDLE(OpenGl_FrameBuffer, OpenGl_NamedResource)
a2e4f780 29
a1073ae2 30//! Short declaration of useful collection types.
1220d98e 31typedef NCollection_Vector<Standard_Integer> OpenGl_ColorFormats;
a1073ae2 32
fd4a6963 33//! Class implements FrameBuffer Object (FBO) resource
34//! intended for off-screen rendering.
ddb9ed48 35class OpenGl_FrameBuffer : public OpenGl_NamedResource
7fd59977 36{
ddb9ed48 37 DEFINE_STANDARD_RTTIEXT(OpenGl_FrameBuffer, OpenGl_NamedResource)
7fd59977 38public:
39
40 //! Helpful constants
1220d98e 41 static const unsigned int NO_FRAMEBUFFER = 0;
42 static const unsigned int NO_RENDERBUFFER = 0;
7fd59977 43
6cde53c4 44public:
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 57public:
58
fd4a6963 59 //! Empty constructor
ddb9ed48 60 Standard_EXPORT OpenGl_FrameBuffer (const TCollection_AsciiString& theResourceId = TCollection_AsciiString());
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,
c8365a1c 168 const OpenGl_FrameBuffer& theFbo,
169 const Standard_Boolean theToKeepMsaa = true)
3c4b62a4 170 {
c8365a1c 171 return InitLazy (theGlCtx, Graphic3d_Vec2i (theFbo.myVPSizeX, theFbo.myVPSizeY), theFbo.myColorFormats, theFbo.myDepthFormat, theToKeepMsaa ? theFbo.myNbSamples : 0);
172 }
173
174 //! (Re-)initialize FBO with specified dimensions.
175 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
176 //! @param theGlCtx currently bound OpenGL context
177 //! @param theSize render buffer width x height
178 //! @param theColorFormats list of color render buffer sized format, e.g. GL_RGBA8; list should define only one element
179 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
180 //! @param theNbSamples MSAA number of samples (0 means normal render buffer)
181 Standard_Boolean InitRenderBuffer (const Handle(OpenGl_Context)& theGlCtx,
182 const Graphic3d_Vec2i& theSize,
183 const OpenGl_ColorFormats& theColorFormats,
184 const Standard_Integer theDepthFormat,
185 const Standard_Integer theNbSamples = 0)
186 {
187 return initRenderBuffer (theGlCtx, theSize, theColorFormats, theDepthFormat, theNbSamples, 0);
3c4b62a4 188 }
38a0206f 189
a2e4f780 190 //! (Re-)initialize FBO with specified dimensions.
191 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
3c4b62a4 192 //! @param theGlCtx currently bound OpenGL context
1220d98e 193 //! @param theSize render buffer width x height
3c4b62a4 194 //! @param theColorFormat color render buffer sized format, e.g. GL_RGBA8
195 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
c8365a1c 196 //! @param theColorRBufferFromWindow should be ID of already initialized RB object, which will be released within this class
a2e4f780 197 Standard_EXPORT Standard_Boolean InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
1220d98e 198 const Graphic3d_Vec2i& theSize,
199 const Standard_Integer theColorFormat,
200 const Standard_Integer theDepthFormat,
c8365a1c 201 const unsigned int theColorRBufferFromWindow);
a2e4f780 202
203 //! Initialize class from currently bound FBO.
204 //! Retrieved OpenGL objects will not be destroyed on Release.
205 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlCtx);
206
78c4e836 207 //! Wrap existing color textures.
208 Standard_EXPORT Standard_Boolean InitWrapper (const Handle(OpenGl_Context)& theGlContext,
209 const NCollection_Sequence<Handle(OpenGl_Texture)>& theColorTextures,
210 const Handle(OpenGl_Texture)& theDepthTexture = Handle(OpenGl_Texture)());
211
7fd59977 212 //! Setup viewport to render into FBO
fd4a6963 213 Standard_EXPORT void SetupViewport (const Handle(OpenGl_Context)& theGlCtx);
7fd59977 214
215 //! Override viewport settings
1220d98e 216 Standard_EXPORT void ChangeViewport (const Standard_Integer theVPSizeX,
217 const Standard_Integer theVPSizeY);
7fd59977 218
b86bb3df 219 //! Bind frame buffer for drawing and reading (to render into the texture).
18f4e8e2 220 Standard_EXPORT virtual void BindBuffer (const Handle(OpenGl_Context)& theGlCtx);
7fd59977 221
b86bb3df 222 //! Bind frame buffer for drawing GL_DRAW_FRAMEBUFFER (to render into the texture).
223 Standard_EXPORT virtual void BindDrawBuffer (const Handle(OpenGl_Context)& theGlCtx);
224
225 //! Bind frame buffer for reading GL_READ_FRAMEBUFFER
226 Standard_EXPORT virtual void BindReadBuffer (const Handle(OpenGl_Context)& theGlCtx);
227
7fd59977 228 //! Unbind frame buffer.
18f4e8e2 229 Standard_EXPORT virtual void UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx);
7fd59977 230
a1073ae2 231 //! Returns the color texture for the given color buffer index.
1220d98e 232 const Handle(OpenGl_Texture)& ColorTexture (const Standard_Integer theColorBufferIndex = 0) const
7fd59977 233 {
1220d98e 234 return myColorTextures.Value (theColorBufferIndex);
7fd59977 235 }
236
18f4e8e2 237 //! Returns the depth-stencil texture.
1220d98e 238 const Handle(OpenGl_Texture)& DepthStencilTexture() const { return myDepthStencilTexture; }
239
c8365a1c 240 //! Returns TRUE if color Render Buffer is defined.
241 bool IsColorRenderBuffer() const { return myGlColorRBufferId != NO_RENDERBUFFER; }
242
1220d98e 243 //! Returns the color Render Buffer.
244 unsigned int ColorRenderBuffer() const { return myGlColorRBufferId; }
245
c8365a1c 246 //! Returns TRUE if depth Render Buffer is defined.
247 bool IsDepthStencilRenderBuffer() const { return myGlDepthRBufferId != NO_RENDERBUFFER; }
248
1220d98e 249 //! Returns the depth Render Buffer.
250 unsigned int DepthStencilRenderBuffer() const { return myGlDepthRBufferId; }
251
252 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
253 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
254
255public:
256
c8365a1c 257 //! (Re-)initialize FBO with specified dimensions.
258 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
259 //! @param theGlCtx currently bound OpenGL context
260 //! @param theSize render buffer width x height
261 //! @param theColorFormats list of color render buffer sized format, e.g. GL_RGBA8
262 //! @param theDepthFormat depth-stencil render buffer sized format, e.g. GL_DEPTH24_STENCIL8
263 //! @param theNbSamples MSAA number of samples (0 means normal render buffer)
264 //! @param theColorRBufferFromWindow when specified - should be ID of already initialized RB object, which will be released within this class
265 Standard_EXPORT Standard_Boolean initRenderBuffer (const Handle(OpenGl_Context)& theGlCtx,
266 const Graphic3d_Vec2i& theSize,
267 const OpenGl_ColorFormats& theColorFormats,
268 const Standard_Integer theDepthFormat,
269 const Standard_Integer theNbSamples,
270 const unsigned int theColorRBufferFromWindow);
271
1220d98e 272 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
273 Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i")
274 bool Init (const Handle(OpenGl_Context)& theGlCtx,
275 const Standard_Integer theSizeX,
276 const Standard_Integer theSizeY,
277 const OpenGl_ColorFormats& theColorFormats,
278 const Handle(OpenGl_Texture)& theDepthStencilTexture,
279 const Standard_Integer theNbSamples = 0)
7fd59977 280 {
1220d98e 281 return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormats, theDepthStencilTexture, theNbSamples);
7fd59977 282 }
283
1220d98e 284 //! Initialize FBO for rendering into textures.
285 Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i")
286 bool Init (const Handle(OpenGl_Context)& theGlCtx,
287 const Standard_Integer theSizeX,
288 const Standard_Integer theSizeY,
289 const Standard_Integer theColorFormat,
290 const Standard_Integer theDepthFormat,
291 const Standard_Integer theNbSamples = 0)
a2e4f780 292 {
1220d98e 293 return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormat, theDepthFormat, theNbSamples);
a2e4f780 294 }
295
1220d98e 296 //! Initialize FBO for rendering into single/multiple color buffer and depth textures.
297 Standard_DEPRECATED("Obsolete method, use Init() taking Graphic3d_Vec2i")
298 bool Init (const Handle(OpenGl_Context)& theGlCtx,
299 const Standard_Integer theSizeX,
300 const Standard_Integer theSizeY,
301 const OpenGl_ColorFormats& theColorFormats,
302 const Standard_Integer theDepthFormat,
303 const Standard_Integer theNbSamples = 0)
a2e4f780 304 {
1220d98e 305 return Init (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormats, theDepthFormat, theNbSamples);
a2e4f780 306 }
307
1220d98e 308 //! (Re-)initialize FBO with specified dimensions.
309 Standard_DEPRECATED("Obsolete method, use InitLazy() taking Graphic3d_Vec2i")
310 bool InitLazy (const Handle(OpenGl_Context)& theGlCtx,
311 const Standard_Integer theViewportSizeX,
312 const Standard_Integer theViewportSizeY,
313 const Standard_Integer theColorFormat,
314 const Standard_Integer theDepthFormat,
315 const Standard_Integer theNbSamples = 0)
316 {
317 return InitLazy (theGlCtx, Graphic3d_Vec2i (theViewportSizeX, theViewportSizeY), theColorFormat, theDepthFormat, theNbSamples);
318 }
319
320 //! (Re-)initialize FBO with specified dimensions.
321 Standard_DEPRECATED("Obsolete method, use InitLazy() taking Graphic3d_Vec2i")
322 bool InitLazy (const Handle(OpenGl_Context)& theGlCtx,
323 const Standard_Integer theViewportSizeX,
324 const Standard_Integer theViewportSizeY,
325 const OpenGl_ColorFormats& theColorFormats,
326 const Standard_Integer theDepthFormat,
327 const Standard_Integer theNbSamples = 0)
328 {
329 return InitLazy (theGlCtx, Graphic3d_Vec2i (theViewportSizeX, theViewportSizeY), theColorFormats, theDepthFormat, theNbSamples);
330 }
331
332 //! (Re-)initialize FBO with specified dimensions.
333 //! The Render Buffer Objects will be used for Color, Depth and Stencil attachments (as opposite to textures).
334 Standard_DEPRECATED("Obsolete method, use InitWithRB() taking Graphic3d_Vec2i")
335 bool InitWithRB (const Handle(OpenGl_Context)& theGlCtx,
336 const Standard_Integer theSizeX,
337 const Standard_Integer theSizeY,
338 const Standard_Integer theColorFormat,
339 const Standard_Integer theDepthFormat,
340 const unsigned int theColorRBufferFromWindow = 0)
341 {
342 return InitWithRB (theGlCtx, Graphic3d_Vec2i (theSizeX, theSizeY), theColorFormat, theDepthFormat, theColorRBufferFromWindow);
343 }
15669413 344
18f4e8e2 345protected:
346
18f4e8e2 347 Standard_Boolean isValidFrameBuffer() const
b859a34d 348 {
18f4e8e2 349 return myGlFBufferId != NO_FRAMEBUFFER;
b859a34d 350 }
351
a1073ae2 352protected:
353
354 typedef NCollection_Vector<Handle(OpenGl_Texture)> OpenGl_TextureArray;
355
18f4e8e2 356protected:
357
1220d98e 358 Standard_Integer myInitVPSizeX; //!< viewport width specified during initialization (kept even on failure)
359 Standard_Integer myInitVPSizeY; //!< viewport height specified during initialization (kept even on failure)
360 Standard_Integer myVPSizeX; //!< viewport width (should be <= texture width)
361 Standard_Integer myVPSizeY; //!< viewport height (should be <= texture height)
362 Standard_Integer myNbSamples; //!< number of MSAA samples
a1073ae2 363 OpenGl_ColorFormats myColorFormats; //!< sized format for color texture, GL_RGBA8 by default
1220d98e 364 Standard_Integer myDepthFormat; //!< sized format for depth-stencil texture, GL_DEPTH24_STENCIL8 by default
365 unsigned int myGlFBufferId; //!< FBO object ID
366 unsigned int myGlColorRBufferId; //!< color Render Buffer object (alternative to myColorTexture)
367 unsigned int myGlDepthRBufferId; //!< depth-stencil Render Buffer object (alternative to myDepthStencilTexture)
a2e4f780 368 bool myIsOwnBuffer; //!< flag indicating that FBO should be deallocated by this class
78c4e836 369 bool myIsOwnColor; //!< flag indicating that color textures should be deallocated by this class
370 bool myIsOwnDepth; //!< flag indicating that depth texture should be deallocated by this class
a1073ae2 371 OpenGl_TextureArray myColorTextures; //!< color texture objects
18f4e8e2 372 Handle(OpenGl_Texture) myDepthStencilTexture; //!< depth-stencil texture object
fd4a6963 373
7fd59977 374};
375
fd4a6963 376#endif // OPENGL_FRAME_BUFFER_H