0025854: Visualization, TKOpenGl - add option to request Core profile 3.2+
[occt.git] / src / OpenGl / OpenGl_TextureBufferArb.cxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-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_TextureBufferArb.hxx>
16
17 #include <OpenGl_Context.hxx>
18 #include <Standard_Assert.hxx>
19
20 IMPLEMENT_STANDARD_HANDLE (OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
21 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
22
23 // =======================================================================
24 // function : OpenGl_TextureBufferArb
25 // purpose  :
26 // =======================================================================
27 OpenGl_TextureBufferArb::OpenGl_TextureBufferArb()
28 : OpenGl_VertexBuffer(),
29   myTextureId (NO_TEXTURE),
30   myTexFormat (GL_RGBA32F)
31 {
32   //
33 }
34
35 // =======================================================================
36 // function : ~OpenGl_TextureBufferArb
37 // purpose  :
38 // =======================================================================
39 OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
40 {
41   Release (NULL);
42 }
43
44 // =======================================================================
45 // function : GetTarget
46 // purpose  :
47 // =======================================================================
48 GLenum OpenGl_TextureBufferArb::GetTarget() const
49 {
50   return GL_TEXTURE_BUFFER_ARB; // GL_TEXTURE_BUFFER for OpenGL 3.1+
51 }
52
53 // =======================================================================
54 // function : Release
55 // purpose  :
56 // =======================================================================
57 void OpenGl_TextureBufferArb::Release (OpenGl_Context* theGlCtx)
58 {
59   if (myTextureId != NO_TEXTURE)
60   {
61     // application can not handle this case by exception - this is bug in code
62     Standard_ASSERT_RETURN (theGlCtx != NULL,
63       "OpenGl_TextureBufferExt destroyed without GL context! Possible GPU memory leakage...",);
64
65     if (theGlCtx->IsValid())
66     {
67       glDeleteTextures (1, &myTextureId);
68     }
69     myTextureId = NO_TEXTURE;
70   }
71   OpenGl_VertexBuffer::Release (theGlCtx);
72 }
73
74 // =======================================================================
75 // function : Create
76 // purpose  :
77 // =======================================================================
78 bool OpenGl_TextureBufferArb::Create (const Handle(OpenGl_Context)& theGlCtx)
79 {
80   if (!OpenGl_VertexBuffer::Create (theGlCtx))
81   {
82     return false;
83   }
84
85   if (myTextureId == NO_TEXTURE)
86   {
87     glGenTextures (1, &myTextureId);
88   }
89   return myTextureId != NO_TEXTURE;
90 }
91
92 // =======================================================================
93 // function : Init
94 // purpose  :
95 // =======================================================================
96 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
97                                     const GLuint   theComponentsNb,
98                                     const GLsizei  theElemsNb,
99                                     const GLfloat* theData)
100 {
101 #if !defined(GL_ES_VERSION_2_0)
102   if (theComponentsNb < 1
103    || theComponentsNb > 4)
104   {
105     // unsupported format
106     return false;
107   }
108   else if (theComponentsNb == 3
109        && !theGlCtx->arbTboRGB32)
110   {
111     return false;
112   }
113   else if (!Create (theGlCtx)
114         || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
115   {
116     return false;
117   }
118
119   switch (theComponentsNb)
120   {
121     case 1: myTexFormat = GL_R32F;    break;
122     case 2: myTexFormat = GL_RG32F;   break;
123     case 3: myTexFormat = GL_RGB32F;  break; // GL_ARB_texture_buffer_object_rgb32
124     case 4: myTexFormat = GL_RGBA32F; break;
125   }
126
127   Bind (theGlCtx);
128   BindTexture (theGlCtx);
129   theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
130   UnbindTexture (theGlCtx);
131   Unbind (theGlCtx);
132   return true;
133 #else
134   return false;
135 #endif
136 }
137
138 // =======================================================================
139 // function : Init
140 // purpose  :
141 // =======================================================================
142 bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
143                                     const GLuint   theComponentsNb,
144                                     const GLsizei  theElemsNb,
145                                     const GLuint*  theData)
146 {
147 #if !defined(GL_ES_VERSION_2_0)
148   if (theComponentsNb < 1
149    || theComponentsNb > 4)
150   {
151     // unsupported format
152     return false;
153   }
154   else if (theComponentsNb == 3
155        && !theGlCtx->arbTboRGB32)
156   {
157     return false;
158   }
159   else if (!Create (theGlCtx)
160         || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
161   {
162     return false;
163   }
164
165   switch (theComponentsNb)
166   {
167     case 1: myTexFormat = GL_R32I;    break;
168     case 2: myTexFormat = GL_RG32I;   break;
169     case 3: myTexFormat = GL_RGB32I;  break;
170     case 4: myTexFormat = GL_RGBA32I; break;
171   }
172
173   Bind (theGlCtx);
174   BindTexture (theGlCtx);
175   theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
176   UnbindTexture (theGlCtx);
177   Unbind (theGlCtx);
178   return true;
179 #else
180   return false;
181 #endif
182 }
183
184 // =======================================================================
185 // function : BindTexture
186 // purpose  :
187 // =======================================================================
188 void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
189                                            const GLenum theTextureUnit) const
190 {
191   theGlCtx->core20fwd->glActiveTexture (theTextureUnit);
192   glBindTexture (GetTarget(), myTextureId);
193 }
194
195 // =======================================================================
196 // function : UnbindTexture
197 // purpose  :
198 // =======================================================================
199 void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
200                                              const GLenum theTextureUnit) const
201 {
202   theGlCtx->core20fwd->glActiveTexture (theTextureUnit);
203   glBindTexture (GetTarget(), NO_TEXTURE);
204 }