0025219: Visualization, TKOpenGl - disable code paths unavailable on OpenGL ES 2.0
[occt.git] / src / OpenGl / OpenGl_TextureBufferArb.cxx
CommitLineData
5e27df78 1// Created by: Kirill GAVRILOV
d5f74e42 2// Copyright (c) 2013-2014 OPEN CASCADE SAS
5e27df78 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
5e27df78 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.
5e27df78 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
5e27df78 14
15#include <OpenGl_TextureBufferArb.hxx>
16
17#include <OpenGl_Context.hxx>
18#include <Standard_Assert.hxx>
19
20IMPLEMENT_STANDARD_HANDLE (OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
21IMPLEMENT_STANDARD_RTTIEXT(OpenGl_TextureBufferArb, OpenGl_VertexBuffer)
22
23// =======================================================================
24// function : OpenGl_TextureBufferArb
25// purpose :
26// =======================================================================
27OpenGl_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// =======================================================================
39OpenGl_TextureBufferArb::~OpenGl_TextureBufferArb()
40{
41 Release (NULL);
42}
43
44// =======================================================================
45// function : GetTarget
46// purpose :
47// =======================================================================
48GLenum 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// =======================================================================
10b9c7df 57void OpenGl_TextureBufferArb::Release (OpenGl_Context* theGlCtx)
5e27df78 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
fd4a6963 65 if (theGlCtx->IsValid())
66 {
67 glDeleteTextures (1, &myTextureId);
68 }
5e27df78 69 myTextureId = NO_TEXTURE;
70 }
71 OpenGl_VertexBuffer::Release (theGlCtx);
72}
73
74// =======================================================================
75// function : Create
76// purpose :
77// =======================================================================
78bool 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// =======================================================================
96bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
97 const GLuint theComponentsNb,
98 const GLsizei theElemsNb,
99 const GLfloat* theData)
100{
ca3c13d1 101#if !defined(GL_ES_VERSION_2_0)
5e27df78 102 if (theComponentsNb != 1
103 && theComponentsNb != 2
104 && theComponentsNb != 4)
105 {
106 // unsupported format
107 return false;
108 }
109 else if (!Create (theGlCtx)
110 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
111 {
112 return false;
113 }
114
115 switch (theComponentsNb)
116 {
117 case 1: myTexFormat = GL_R32F; break;
118 case 2: myTexFormat = GL_RG32F; break;
119 //case 3: myTexFormat = GL_RGB32F; break; // GL_ARB_texture_buffer_object_rgb32
120 case 4: myTexFormat = GL_RGBA32F; break;
121 }
122
123 Bind (theGlCtx);
124 BindTexture (theGlCtx);
01ca42b2 125 theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
fc73a202 126 UnbindTexture (theGlCtx);
127 Unbind (theGlCtx);
128 return true;
ca3c13d1 129#else
130 return false;
131#endif
fc73a202 132}
133
134// =======================================================================
135// function : Init
136// purpose :
137// =======================================================================
138bool OpenGl_TextureBufferArb::Init (const Handle(OpenGl_Context)& theGlCtx,
139 const GLuint theComponentsNb,
140 const GLsizei theElemsNb,
141 const GLuint* theData)
142{
ca3c13d1 143#if !defined(GL_ES_VERSION_2_0)
fc73a202 144 if (theComponentsNb != 1
145 && theComponentsNb != 2
146 && theComponentsNb != 3
147 && theComponentsNb != 4)
148 {
149 // unsupported format
150 return false;
151 }
152 else if (!Create (theGlCtx)
153 || !OpenGl_VertexBuffer::Init (theGlCtx, theComponentsNb, theElemsNb, theData))
154 {
155 return false;
156 }
157
158 switch (theComponentsNb)
159 {
160 case 1: myTexFormat = GL_R32I; break;
161 case 2: myTexFormat = GL_RG32I; break;
162 case 3: myTexFormat = GL_RGB32I; break;
163 case 4: myTexFormat = GL_RGBA32I; break;
164 }
165
166 Bind (theGlCtx);
167 BindTexture (theGlCtx);
168 theGlCtx->arbTBO->glTexBuffer (GetTarget(), myTexFormat, myBufferId);
5e27df78 169 UnbindTexture (theGlCtx);
170 Unbind (theGlCtx);
171 return true;
ca3c13d1 172#else
173 return false;
174#endif
5e27df78 175}
176
177// =======================================================================
178// function : BindTexture
179// purpose :
180// =======================================================================
181void OpenGl_TextureBufferArb::BindTexture (const Handle(OpenGl_Context)& theGlCtx,
182 const GLenum theTextureUnit) const
183{
184 theGlCtx->core20->glActiveTexture (theTextureUnit);
185 glBindTexture (GetTarget(), myTextureId);
186}
187
188// =======================================================================
189// function : UnbindTexture
190// purpose :
191// =======================================================================
192void OpenGl_TextureBufferArb::UnbindTexture (const Handle(OpenGl_Context)& theGlCtx,
193 const GLenum theTextureUnit) const
194{
195 theGlCtx->core20->glActiveTexture (theTextureUnit);
196 glBindTexture (GetTarget(), NO_TEXTURE);
197}