1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _OpenGl_Texture_H__
16 #define _OpenGl_Texture_H__
18 #include <OpenGl_GlCore13.hxx>
19 #include <OpenGl_NamedResource.hxx>
20 #include <OpenGl_Sampler.hxx>
21 #include <Graphic3d_TypeOfTexture.hxx>
22 #include <Graphic3d_TextureUnit.hxx>
24 class Graphic3d_TextureParams;
27 //! Selects preferable texture format for specified parameters.
29 struct OpenGl_TextureFormatSelector
35 struct OpenGl_TextureFormatSelector<GLubyte>
37 static GLint Internal (GLuint theChannels)
54 static GLint DataType()
56 return GL_UNSIGNED_BYTE;
61 struct OpenGl_TextureFormatSelector<GLushort>
63 static GLint Internal (GLuint theChannels)
80 static GLint DataType()
82 return GL_UNSIGNED_SHORT;
87 struct OpenGl_TextureFormatSelector<GLfloat>
89 static GLint Internal (GLuint theChannels)
106 static GLint DataType()
113 struct OpenGl_TextureFormatSelector<GLuint>
115 static GLint Internal (GLuint theChannels)
132 static GLint DataType()
134 return GL_UNSIGNED_INT;
138 //! Only unsigned formats are available in OpenGL ES 2.0
139 #if !defined(GL_ES_VERSION_2_0)
141 struct OpenGl_TextureFormatSelector<GLbyte>
143 static GLint Internal (GLuint theChannels)
152 return GL_RGB8_SNORM;
154 return GL_RGBA8_SNORM;
160 static GLint DataType()
167 struct OpenGl_TextureFormatSelector<GLshort>
169 static GLint Internal (GLuint theChannels)
176 return GL_RG16_SNORM;
178 return GL_RGB16_SNORM;
180 return GL_RGBA16_SNORM;
186 static GLint DataType()
193 struct OpenGl_TextureFormatSelector<GLint>
195 static GLint Internal (GLuint theChannels)
206 return GL_RGBA_SNORM;
212 static GLint DataType()
219 //! Stores parameters of OpenGL texture format.
220 class OpenGl_TextureFormat
222 friend class OpenGl_Texture;
226 //! Returns OpenGL format of the pixel data.
227 inline GLenum Format() const
244 //! Returns OpenGL internal format of the pixel data.
245 inline GLint Internal() const
250 //! Returns OpenGL data type of the pixel data.
251 inline GLint DataType() const
256 //! Returns texture format for specified type and number of channels.
257 template<class T, int N>
258 static OpenGl_TextureFormat Create()
260 return OpenGl_TextureFormat (N,
261 OpenGl_TextureFormatSelector<T>::Internal(N),
262 OpenGl_TextureFormatSelector<T>::DataType());
267 //! Creates new texture format.
268 OpenGl_TextureFormat (const GLint theChannels,
269 const GLint theInternal,
270 const GLint theDataType)
271 : myInternal (theInternal),
272 myChannels (theChannels),
273 myDataType (theDataType) {}
277 GLint myInternal; //!< OpenGL internal format of the pixel data
278 GLint myChannels; //!< Number of channels for each pixel (from 1 to 4)
279 GLint myDataType; //!< OpenGL data type of input pixel data
283 //! Texture resource.
284 class OpenGl_Texture : public OpenGl_NamedResource
286 DEFINE_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource)
289 //! Helpful constants
290 static const GLuint NO_TEXTURE = 0;
292 //! Return pixel size of pixel format in bytes.
293 //! Note that this method considers that OpenGL natively supports this pixel format,
294 //! which might be not the case - in the latter case, actual pixel size might differ!
295 Standard_EXPORT static Standard_Size PixelSizeOfPixelFormat (Standard_Integer theInternalFormat);
299 //! Create uninitialized texture.
300 Standard_EXPORT OpenGl_Texture (const TCollection_AsciiString& theResourceId = TCollection_AsciiString(),
301 const Handle(Graphic3d_TextureParams)& theParams = Handle(Graphic3d_TextureParams)());
304 Standard_EXPORT virtual ~OpenGl_Texture();
306 //! @return true if current object was initialized
307 inline bool IsValid() const
309 return myTextureId != NO_TEXTURE;
312 //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
313 inline GLenum GetTarget() const
318 //! @return texture width (0 LOD)
319 inline GLsizei SizeX() const
324 //! @return texture height (0 LOD)
325 inline GLsizei SizeY() const
330 //! @return texture ID
331 inline GLuint TextureId() const
336 //! @return texture format (not sized)
337 inline GLenum GetFormat() const
342 //! Return true for GL_RED and GL_ALPHA formats.
348 //! Setup to interprete the format as Alpha by Shader Manager
349 //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
350 void SetAlpha (const bool theValue)
352 myIsAlpha = theValue;
355 //! Creates Texture id if not yet generated.
356 //! Data should be initialized by another method.
357 Standard_EXPORT bool Create (const Handle(OpenGl_Context)& theCtx);
359 //! Destroy object - will release GPU memory if any.
360 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
362 //! Return texture sampler.
363 const Handle(OpenGl_Sampler)& Sampler() const { return mySampler; }
365 //! Set texture sampler.
366 void SetSampler (const Handle(OpenGl_Sampler)& theSampler) { mySampler = theSampler; }
368 //! Initialize the Sampler Object (as OpenGL object).
369 //! @param theCtx currently bound OpenGL context
370 Standard_EXPORT bool InitSamplerObject (const Handle(OpenGl_Context)& theCtx);
372 //! Bind this Texture to the unit specified in sampler parameters.
373 //! Also binds Sampler Object if it is allocated.
374 void Bind (const Handle(OpenGl_Context)& theCtx) const
376 Bind (theCtx, mySampler->Parameters()->TextureUnit());
379 //! Unbind texture from the unit specified in sampler parameters.
380 //! Also unbinds Sampler Object if it is allocated.
381 void Unbind (const Handle(OpenGl_Context)& theCtx) const
383 Unbind (theCtx, mySampler->Parameters()->TextureUnit());
386 //! Bind this Texture to specified unit.
387 //! Also binds Sampler Object if it is allocated.
388 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
389 const Graphic3d_TextureUnit theTextureUnit) const;
391 //! Unbind texture from specified unit.
392 //! Also unbinds Sampler Object if it is allocated.
393 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
394 const Graphic3d_TextureUnit theTextureUnit) const;
396 //! Revision of associated data source.
397 Standard_Size Revision() const { return myRevision; }
399 //! Set revision of associated data source.
400 void SetRevision (const Standard_Size theRevision) { myRevision = theRevision; }
402 //! Notice that texture will be unbound after this call.
403 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
404 const Image_PixMap& theImage,
405 const Graphic3d_TypeOfTexture theType);
407 //! Initialize the texture with specified format, size and texture type.
408 //! If theImage is empty the texture data will contain trash.
409 //! Notice that texture will be unbound after this call.
410 Standard_EXPORT bool Init (const Handle(OpenGl_Context)& theCtx,
411 const GLint theTextFormat,
412 const GLenum thePixelFormat,
413 const GLenum theDataType,
414 const GLsizei theSizeX,
415 const GLsizei theSizeY,
416 const Graphic3d_TypeOfTexture theType,
417 const Image_PixMap* theImage = NULL);
419 //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
420 Standard_EXPORT bool Init2DMultisample (const Handle(OpenGl_Context)& theCtx,
421 const GLsizei theNbSamples,
422 const GLint theTextFormat,
423 const GLsizei theSizeX,
424 const GLsizei theSizeY);
426 //! Allocates texture rectangle with specified format and size.
427 //! \note Texture data is not initialized (will contain trash).
428 Standard_EXPORT bool InitRectangle (const Handle(OpenGl_Context)& theCtx,
429 const Standard_Integer theSizeX,
430 const Standard_Integer theSizeY,
431 const OpenGl_TextureFormat& theFormat);
433 //! Initializes 3D texture rectangle with specified format and size.
434 Standard_EXPORT bool Init3D (const Handle(OpenGl_Context)& theCtx,
435 const GLint theTextFormat,
436 const GLenum thePixelFormat,
437 const GLenum theDataType,
438 const Standard_Integer theSizeX,
439 const Standard_Integer theSizeY,
440 const Standard_Integer theSizeZ,
441 const void* thePixels);
443 //! @return true if texture was generated within mipmaps
444 Standard_Boolean HasMipmaps() const { return myHasMipmaps; }
446 //! Return texture type and format by Image_PixMap data format.
447 Standard_EXPORT static bool GetDataFormat (const Handle(OpenGl_Context)& theCtx,
448 const Image_PixMap& theData,
449 GLint& theTextFormat,
450 GLenum& thePixelFormat,
451 GLenum& theDataType);
453 //! Returns estimated GPU memory usage for holding data without considering overheads and allocation alignment rules.
454 Standard_EXPORT virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE;
458 //! Apply default sampler parameters after texture creation.
459 Standard_EXPORT void applyDefaultSamplerParams (const Handle(OpenGl_Context)& theCtx);
463 Handle(OpenGl_Sampler) mySampler; //!< texture sampler
464 Standard_Size myRevision; //!< revision of associated data source
465 GLuint myTextureId; //!< GL resource ID
466 GLenum myTarget; //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
467 GLsizei mySizeX; //!< texture width
468 GLsizei mySizeY; //!< texture height
469 GLsizei mySizeZ; //!< texture depth
470 GLenum myTextFormat; //!< texture format - GL_RGB, GL_RGBA,...
471 GLint mySizedFormat;//!< internal (sized) texture format
472 Standard_Integer myNbSamples; //!< number of MSAA samples
473 Standard_Boolean myHasMipmaps; //!< flag indicates that texture was uploaded with mipmaps
474 bool myIsAlpha; //!< indicates alpha format
478 DEFINE_STANDARD_HANDLE(OpenGl_Texture, OpenGl_NamedResource)
480 #endif // _OpenGl_Texture_H__