0029353: Optimization of TPrsStd_AISPresentation::SetSelectionMode()
[occt.git] / src / OpenGl / OpenGl_Texture.hxx
CommitLineData
bf75be98 1// Created by: Kirill GAVRILOV
d5f74e42 2// Copyright (c) 2013-2014 OPEN CASCADE SAS
bf75be98 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
bf75be98 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.
bf75be98 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
bf75be98 14
15#ifndef _OpenGl_Texture_H__
16#define _OpenGl_Texture_H__
17
18#include <OpenGl_GlCore13.hxx>
cc8cbabe 19#include <OpenGl_NamedResource.hxx>
20#include <OpenGl_Sampler.hxx>
bf75be98 21#include <Graphic3d_TypeOfTexture.hxx>
cc8cbabe 22#include <Graphic3d_TextureUnit.hxx>
bf75be98 23
c04c30b3 24class Graphic3d_TextureParams;
bf75be98 25class Image_PixMap;
26
68333c8f 27//! Selects preferable texture format for specified parameters.
28template<class T>
29struct OpenGl_TextureFormatSelector
30{
31 // Not implemented
32};
33
34template<>
35struct OpenGl_TextureFormatSelector<GLubyte>
36{
37 static GLint Internal (GLuint theChannels)
38 {
39 switch (theChannels)
40 {
41 case 1:
42 return GL_R8;
43 case 2:
44 return GL_RG8;
45 case 3:
46 return GL_RGB8;
47 case 4:
48 return GL_RGBA8;
49 default:
50 return GL_NONE;
51 }
52 }
fbef84f9 53
54 static GLint DataType()
55 {
56 return GL_UNSIGNED_BYTE;
57 }
68333c8f 58};
59
60template<>
61struct OpenGl_TextureFormatSelector<GLushort>
62{
63 static GLint Internal (GLuint theChannels)
64 {
65 switch (theChannels)
66 {
67 case 1:
68 return GL_R16;
69 case 2:
70 return GL_RG16;
71 case 3:
72 return GL_RGB16;
73 case 4:
74 return GL_RGBA16;
75 default:
76 return GL_NONE;
77 }
78 }
fbef84f9 79
80 static GLint DataType()
81 {
82 return GL_UNSIGNED_SHORT;
83 }
68333c8f 84};
85
86template<>
87struct OpenGl_TextureFormatSelector<GLfloat>
88{
89 static GLint Internal (GLuint theChannels)
90 {
91 switch (theChannels)
92 {
93 case 1:
94 return GL_R32F;
95 case 2:
96 return GL_RG32F;
97 case 3:
98 return GL_RGB32F;
99 case 4:
100 return GL_RGBA32F;
101 default:
102 return GL_NONE;
103 }
104 }
fbef84f9 105
106 static GLint DataType()
107 {
108 return GL_FLOAT;
109 }
110};
111
112template<>
113struct OpenGl_TextureFormatSelector<GLuint>
114{
115 static GLint Internal (GLuint theChannels)
116 {
117 switch (theChannels)
118 {
119 case 1:
120 return GL_RED;
121 case 2:
122 return GL_RG;
123 case 3:
124 return GL_RGB;
125 case 4:
126 return GL_RGBA;
127 default:
128 return GL_NONE;
129 }
130 }
131
132 static GLint DataType()
133 {
134 return GL_UNSIGNED_INT;
135 }
136};
137
138//! Only unsigned formats are available in OpenGL ES 2.0
139#if !defined(GL_ES_VERSION_2_0)
140template<>
141struct OpenGl_TextureFormatSelector<GLbyte>
142{
143 static GLint Internal (GLuint theChannels)
144 {
145 switch (theChannels)
146 {
147 case 1:
148 return GL_R8_SNORM;
149 case 2:
150 return GL_RG8_SNORM;
151 case 3:
152 return GL_RGB8_SNORM;
153 case 4:
154 return GL_RGBA8_SNORM;
155 default:
156 return GL_NONE;
157 }
158 }
159
160 static GLint DataType()
161 {
162 return GL_BYTE;
163 }
164};
165
166template<>
167struct OpenGl_TextureFormatSelector<GLshort>
168{
169 static GLint Internal (GLuint theChannels)
170 {
171 switch (theChannels)
172 {
173 case 1:
174 return GL_R16_SNORM;
175 case 2:
176 return GL_RG16_SNORM;
177 case 3:
178 return GL_RGB16_SNORM;
179 case 4:
180 return GL_RGBA16_SNORM;
181 default:
182 return GL_NONE;
183 }
184 }
185
186 static GLint DataType()
187 {
188 return GL_SHORT;
189 }
190};
191
192template<>
193struct OpenGl_TextureFormatSelector<GLint>
194{
195 static GLint Internal (GLuint theChannels)
196 {
197 switch (theChannels)
198 {
199 case 1:
200 return GL_RED_SNORM;
201 case 2:
202 return GL_RG_SNORM;
203 case 3:
204 return GL_RGB_SNORM;
205 case 4:
206 return GL_RGBA_SNORM;
207 default:
208 return GL_NONE;
209 }
210 }
211
212 static GLint DataType()
213 {
214 return GL_INT;
215 }
68333c8f 216};
fbef84f9 217#endif
68333c8f 218
219//! Stores parameters of OpenGL texture format.
220class OpenGl_TextureFormat
221{
222 friend class OpenGl_Texture;
223
224public:
225
226 //! Returns OpenGL format of the pixel data.
227 inline GLenum Format() const
228 {
229 switch (myChannels)
230 {
231 case 1:
232 return GL_RED;
233 case 2:
234 return GL_RG;
235 case 3:
236 return GL_RGB;
237 case 4:
238 return GL_RGBA;
239 default:
240 return GL_NONE;
241 }
242 }
243
244 //! Returns OpenGL internal format of the pixel data.
245 inline GLint Internal() const
246 {
247 return myInternal;
248 }
249
fbef84f9 250 //! Returns OpenGL data type of the pixel data.
251 inline GLint DataType() const
252 {
253 return myDataType;
254 }
255
68333c8f 256 //! Returns texture format for specified type and number of channels.
257 template<class T, int N>
258 static OpenGl_TextureFormat Create()
259 {
fbef84f9 260 return OpenGl_TextureFormat (N,
261 OpenGl_TextureFormatSelector<T>::Internal(N),
262 OpenGl_TextureFormatSelector<T>::DataType());
68333c8f 263 }
264
265private:
266
267 //! Creates new texture format.
5322131b 268 OpenGl_TextureFormat (const GLint theChannels,
fbef84f9 269 const GLint theInternal,
270 const GLint theDataType)
5322131b 271 : myInternal (theInternal),
fbef84f9 272 myChannels (theChannels),
273 myDataType (theDataType) {}
68333c8f 274
275private:
276
5322131b 277 GLint myInternal; //!< OpenGL internal format of the pixel data
278 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
fbef84f9 279 GLint myDataType; //!< OpenGL data type of input pixel data
68333c8f 280
68333c8f 281};
282
bf75be98 283//! Texture resource.
cc8cbabe 284class OpenGl_Texture : public OpenGl_NamedResource
bf75be98 285{
cc8cbabe 286 DEFINE_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource)
bf75be98 287public:
288
289 //! Helpful constants
290 static const GLuint NO_TEXTURE = 0;
291
292public:
293
cc8cbabe 294 //! Create uninitialized texture.
295 Standard_EXPORT OpenGl_Texture (const TCollection_AsciiString& theResourceId = TCollection_AsciiString(),
296 const Handle(Graphic3d_TextureParams)& theParams = Handle(Graphic3d_TextureParams)());
bf75be98 297
298 //! Destroy object.
299 Standard_EXPORT virtual ~OpenGl_Texture();
300
301 //! @return true if current object was initialized
302 inline bool IsValid() const
303 {
304 return myTextureId != NO_TEXTURE;
305 }
306
307 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
308 inline GLenum GetTarget() const
309 {
310 return myTarget;
311 }
312
a174a3c5 313 //! @return texture width (0 LOD)
314 inline GLsizei SizeX() const
315 {
316 return mySizeX;
317 }
318
319 //! @return texture height (0 LOD)
320 inline GLsizei SizeY() const
321 {
322 return mySizeY;
323 }
324
325 //! @return texture ID
326 inline GLuint TextureId() const
327 {
328 return myTextureId;
329 }
330
8625ef7e 331 //! @return texture format (not sized)
332 inline GLenum GetFormat() const
a577aaab 333 {
334 return myTextFormat;
335 }
336
4e1523ef 337 //! Return true for GL_RED and GL_ALPHA formats.
338 bool IsAlpha() const
339 {
340 return myIsAlpha;
341 }
342
343 //! Setup to interprete the format as Alpha by Shader Manager
344 //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
345 void SetAlpha (const bool theValue)
346 {
347 myIsAlpha = theValue;
348 }
349
bf75be98 350 //! Creates Texture id if not yet generated.
351 //! Data should be initialized by another method.
352 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
353
354 //! Destroy object - will release GPU memory if any.
79104795 355 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
bf75be98 356
cc8cbabe 357 //! Return texture sampler.
358 const Handle(OpenGl_Sampler)& Sampler() const { return mySampler; }
359
360 //! Set texture sampler.
361 void SetSampler (const Handle(OpenGl_Sampler)& theSampler) { mySampler = theSampler; }
362
363 //! Initialize the Sampler Object (as OpenGL object).
364 //! @param theCtx currently bound OpenGL context
365 Standard_EXPORT bool InitSamplerObject (const Handle(OpenGl_Context)& theCtx);
366
367 //! Bind this Texture to the unit specified in sampler parameters.
368 //! Also binds Sampler Object if it is allocated.
369 void Bind (const Handle(OpenGl_Context)& theCtx) const
370 {
371 Bind (theCtx, mySampler->Parameters()->TextureUnit());
372 }
373
374 //! Unbind texture from the unit specified in sampler parameters.
375 //! Also unbinds Sampler Object if it is allocated.
376 void Unbind (const Handle(OpenGl_Context)& theCtx) const
377 {
378 Unbind (theCtx, mySampler->Parameters()->TextureUnit());
379 }
380
bf75be98 381 //! Bind this Texture to specified unit.
cc8cbabe 382 //! Also binds Sampler Object if it is allocated.
bf75be98 383 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
cc8cbabe 384 const Graphic3d_TextureUnit theTextureUnit) const;
bf75be98 385
386 //! Unbind texture from specified unit.
cc8cbabe 387 //! Also unbinds Sampler Object if it is allocated.
bf75be98 388 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
cc8cbabe 389 const Graphic3d_TextureUnit theTextureUnit) const;
bf75be98 390
d2edda76 391 //! Revision of associated data source.
392 Standard_Size Revision() const { return myRevision; }
393
394 //! Set revision of associated data source.
395 void SetRevision (const Standard_Size theRevision) { myRevision = theRevision; }
396
bf75be98 397 //! Notice that texture will be unbound after this call.
398 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
399 const Image_PixMap& theImage,
400 const Graphic3d_TypeOfTexture theType);
401
18f4e8e2 402 //! Initialize the texture with specified format, size and texture type.
403 //! If theImage is empty the texture data will contain trash.
404 //! Notice that texture will be unbound after this call.
405 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
406 const GLint theTextFormat,
407 const GLenum thePixelFormat,
408 const GLenum theDataType,
409 const GLsizei theSizeX,
410 const GLsizei theSizeY,
411 const Graphic3d_TypeOfTexture theType,
412 const Image_PixMap* theImage = NULL);
413
3c4b62a4 414 //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
415 Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
416 const GLsizei theNbSamples,
417 const GLint theTextFormat,
418 const GLsizei theSizeX,
419 const GLsizei theSizeY);
420
68333c8f 421 //! Allocates texture rectangle with specified format and size.
422 //! \note Texture data is not initialized (will contain trash).
423 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
424 const Standard_Integer theSizeX,
425 const Standard_Integer theSizeY,
426 const OpenGl_TextureFormat& theFormat);
427
74fb257d 428 //! Initializes 3D texture rectangle with specified format and size.
429 Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx,
430 const GLint theTextFormat,
431 const GLenum thePixelFormat,
432 const GLenum theDataType,
433 const Standard_Integer theSizeX,
434 const Standard_Integer theSizeY,
435 const Standard_Integer theSizeZ,
436 const void* thePixels);
437
bf75be98 438 //! @return true if texture was generated within mipmaps
cc8cbabe 439 Standard_Boolean HasMipmaps() const { return myHasMipmaps; }
bf75be98 440
18f4e8e2 441 //! Return texture type and format by Image_PixMap data format.
442 Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
443 const Image_PixMap& theData,
444 GLint& theTextFormat,
445 GLenum& thePixelFormat,
446 GLenum& theDataType);
447
bf75be98 448protected:
449
cc8cbabe 450 //! Apply default sampler parameters after texture creation.
451 Standard_EXPORT void applyDefaultSamplerParams (const Handle(OpenGl_Context)& theCtx);
452
453protected:
454
455 Handle(OpenGl_Sampler) mySampler; //!< texture sampler
d2edda76 456 Standard_Size myRevision; //!< revision of associated data source
bf75be98 457 GLuint myTextureId; //!< GL resource ID
74fb257d 458 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
bf75be98 459 GLsizei mySizeX; //!< texture width
460 GLsizei mySizeY; //!< texture height
74fb257d 461 GLsizei mySizeZ; //!< texture depth
8625ef7e 462 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
bf75be98 463 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
4e1523ef 464 bool myIsAlpha; //!< indicates alpha format
bf75be98 465
bf75be98 466};
467
cc8cbabe 468DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_NamedResource)
469
bf75be98 470#endif // _OpenGl_Texture_H__