0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / OpenGl / OpenGl_FrameBuffer.cxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2011-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <OpenGl_FrameBuffer.hxx>
16 #include <OpenGl_ArbFBO.hxx>
17
18 #include <Standard_Assert.hxx>
19
20 IMPLEMENT_STANDARD_HANDLE (OpenGl_FrameBuffer, OpenGl_Resource)
21 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameBuffer, OpenGl_Resource)
22
23 static inline bool isOddNumber (const GLsizei theNumber)
24 {
25   return theNumber & 0x01;
26 }
27
28 static inline GLsizei getEvenNumber (const GLsizei theNumber)
29 {
30   return isOddNumber (theNumber) ? (theNumber + 1) : theNumber;
31 }
32
33 //! Notice - 0 is not power of two here
34 static inline bool isPowerOfTwo (const GLsizei theNumber)
35 {
36   return !(theNumber & (theNumber - 1));
37 }
38
39 // =======================================================================
40 // function : OpenGl_FrameBuffer
41 // purpose  :
42 // =======================================================================
43 OpenGl_FrameBuffer::OpenGl_FrameBuffer (GLint theTextureFormat)
44 : myVPSizeX (0),
45   myVPSizeY (0),
46   myTextFormat (theTextureFormat),
47   myGlFBufferId (NO_FRAMEBUFFER),
48   myColorTexture (new OpenGl_Texture()),
49   myDepthStencilTexture (new OpenGl_Texture())
50 {
51   //
52 }
53
54 // =======================================================================
55 // function : ~OpenGl_FrameBuffer
56 // purpose  :
57 // =======================================================================
58 OpenGl_FrameBuffer::~OpenGl_FrameBuffer()
59 {
60   Release (NULL);
61 }
62
63 // =======================================================================
64 // function : Init
65 // purpose  :
66 // =======================================================================
67 Standard_Boolean OpenGl_FrameBuffer::Init (const Handle(OpenGl_Context)& theGlContext,
68                                            const GLsizei   theViewportSizeX,
69                                            const GLsizei   theViewportSizeY)
70 {
71   if (theGlContext->arbFBO == NULL)
72   {
73     return Standard_False;
74   }
75
76   // clean up previous state
77   Release (theGlContext.operator->());
78
79   // setup viewport sizes as is
80   myVPSizeX = theViewportSizeX;
81   myVPSizeY = theViewportSizeY;
82
83   // Create the textures (will be used as color buffer and depth-stencil buffer)
84   if (!initTrashTextures (theGlContext))
85   {
86     Release (theGlContext.operator->());
87     return Standard_False;
88   }
89
90   // Build FBO and setup it as texture
91   theGlContext->arbFBO->glGenFramebuffers (1, &myGlFBufferId);
92   theGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, myGlFBufferId);
93   theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
94                                                 GL_TEXTURE_2D, myColorTexture->TextureId(), 0);
95   theGlContext->arbFBO->glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
96                                                 GL_TEXTURE_2D, myDepthStencilTexture->TextureId(), 0);
97   if (theGlContext->arbFBO->glCheckFramebufferStatus (GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
98   {
99     Release (theGlContext.operator->());
100     return Standard_False;
101   }
102
103   UnbindBuffer (theGlContext);
104   return Standard_True;
105 }
106
107 // =======================================================================
108 // function : Release
109 // purpose  :
110 // =======================================================================
111 void OpenGl_FrameBuffer::Release (OpenGl_Context* theGlCtx)
112 {
113   if (isValidFrameBuffer())
114   {
115     // application can not handle this case by exception - this is bug in code
116     Standard_ASSERT_RETURN (theGlCtx != NULL,
117       "OpenGl_FrameBuffer destroyed without GL context! Possible GPU memory leakage...",);
118     if (theGlCtx->IsValid())
119     {
120       theGlCtx->arbFBO->glDeleteFramebuffers (1, &myGlFBufferId);
121     }
122     myGlFBufferId = NO_FRAMEBUFFER;
123   }
124
125   myColorTexture->Release (theGlCtx);
126   myDepthStencilTexture->Release (theGlCtx);
127 }
128
129 // =======================================================================
130 // function : initTrashTexture
131 // purpose  :
132 // =======================================================================
133 Standard_Boolean OpenGl_FrameBuffer::initTrashTextures (const Handle(OpenGl_Context)& theGlContext)
134 {
135   return    myColorTexture->Init (theGlContext, myTextFormat,
136                                   GL_RGBA, GL_UNSIGNED_BYTE,
137                                   myVPSizeX, myVPSizeY, Graphic3d_TOT_2D)
138   && myDepthStencilTexture->Init (theGlContext, GL_DEPTH24_STENCIL8,
139                                   GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8,
140                                   myVPSizeX, myVPSizeY, Graphic3d_TOT_2D);
141 }
142
143 // =======================================================================
144 // function : SetupViewport
145 // purpose  :
146 // =======================================================================
147 void OpenGl_FrameBuffer::SetupViewport (const Handle(OpenGl_Context)& /*theGlCtx*/)
148 {
149   glViewport (0, 0, myVPSizeX, myVPSizeY);
150 }
151
152 // =======================================================================
153 // function : ChangeViewport
154 // purpose  :
155 // =======================================================================
156 void OpenGl_FrameBuffer::ChangeViewport (const GLsizei theVPSizeX,
157                                          const GLsizei theVPSizeY)
158 {
159   myVPSizeX = theVPSizeX;
160   myVPSizeY = theVPSizeY;
161 }
162
163 // =======================================================================
164 // function : BindBuffer
165 // purpose  :
166 // =======================================================================
167 void OpenGl_FrameBuffer::BindBuffer (const Handle(OpenGl_Context)& theGlCtx)
168 {
169   theGlCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, myGlFBufferId);
170 }
171
172 // =======================================================================
173 // function : UnbindBuffer
174 // purpose  :
175 // =======================================================================
176 void OpenGl_FrameBuffer::UnbindBuffer (const Handle(OpenGl_Context)& theGlCtx)
177 {
178   theGlCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, NO_FRAMEBUFFER);
179 }