Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 09:21:43

0001 // Created by: Kirill GAVRILOV
0002 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef OpenGl_Texture_HeaderFile
0016 #define OpenGl_Texture_HeaderFile
0017 
0018 #include <Graphic3d_CubeMap.hxx>
0019 #include <NCollection_Vec3.hxx>
0020 #include <Standard_TypeDef.hxx>
0021 #include <OpenGl_TextureFormat.hxx>
0022 #include <OpenGl_NamedResource.hxx>
0023 #include <OpenGl_Sampler.hxx>
0024 #include <Graphic3d_TextureUnit.hxx>
0025 #include <Graphic3d_TypeOfTexture.hxx>
0026 #include <TCollection_AsciiString.hxx>
0027 
0028 //! Texture resource.
0029 class OpenGl_Texture : public OpenGl_NamedResource
0030 {
0031   DEFINE_STANDARD_RTTIEXT(OpenGl_Texture, OpenGl_NamedResource)
0032 public:
0033   //! Helpful constants
0034   static const unsigned int NO_TEXTURE = 0;
0035 
0036   //! Return pixel size of pixel format in bytes.
0037   //! Note that this method considers that OpenGL natively supports this pixel format,
0038   //! which might be not the case - in the latter case, actual pixel size might differ!
0039   Standard_EXPORT static size_t PixelSizeOfPixelFormat(int theInternalFormat);
0040 
0041 public:
0042   //! Create uninitialized texture.
0043   Standard_EXPORT OpenGl_Texture(
0044     const TCollection_AsciiString& theResourceId          = TCollection_AsciiString::EmptyString(),
0045     const occ::handle<Graphic3d_TextureParams>& theParams = occ::handle<Graphic3d_TextureParams>());
0046 
0047   //! Destroy object.
0048   Standard_EXPORT ~OpenGl_Texture() override;
0049 
0050   //! @return true if current object was initialized
0051   virtual bool IsValid() const { return myTextureId != NO_TEXTURE; }
0052 
0053   //! @return target to which the texture is bound (GL_TEXTURE_1D, GL_TEXTURE_2D)
0054   unsigned int GetTarget() const { return myTarget; }
0055 
0056   //! Return texture dimensions (0 LOD)
0057   const NCollection_Vec3<int>& Size() const { return mySize; }
0058 
0059   //! Return texture width (0 LOD)
0060   int SizeX() const { return mySize.x(); }
0061 
0062   //! Return texture height (0 LOD)
0063   int SizeY() const { return mySize.y(); }
0064 
0065   //! Return texture depth (0 LOD)
0066   int SizeZ() const { return mySize.z(); }
0067 
0068   //! @return texture ID
0069   unsigned int TextureId() const { return myTextureId; }
0070 
0071   //! @return texture format (not sized)
0072   unsigned int GetFormat() const { return myTextFormat; }
0073 
0074   //! @return texture format (sized)
0075   int SizedFormat() const { return mySizedFormat; }
0076 
0077   //! Return true for GL_RED and GL_ALPHA formats.
0078   bool IsAlpha() const { return myIsAlpha; }
0079 
0080   //! Setup to interpret the format as Alpha by Shader Manager
0081   //! (should be GL_ALPHA within compatible context or GL_RED otherwise).
0082   void SetAlpha(const bool theValue) { myIsAlpha = theValue; }
0083 
0084   //! Return if 2D surface is defined top-down (TRUE) or bottom-up (FALSE).
0085   //! Normally set from Image_PixMap::IsTopDown() within texture initialization.
0086   bool IsTopDown() const { return myIsTopDown; }
0087 
0088   //! Set if 2D surface is defined top-down (TRUE) or bottom-up (FALSE).
0089   void SetTopDown(bool theIsTopDown) { myIsTopDown = theIsTopDown; }
0090 
0091   //! Creates Texture id if not yet generated.
0092   //! Data should be initialized by another method.
0093   Standard_EXPORT bool Create(const occ::handle<OpenGl_Context>& theCtx);
0094 
0095   //! Destroy object - will release GPU memory if any.
0096   Standard_EXPORT void Release(OpenGl_Context* theCtx) override;
0097 
0098   //! Return texture sampler.
0099   const occ::handle<OpenGl_Sampler>& Sampler() const { return mySampler; }
0100 
0101   //! Set texture sampler.
0102   void SetSampler(const occ::handle<OpenGl_Sampler>& theSampler) { mySampler = theSampler; }
0103 
0104   //! Initialize the Sampler Object (as OpenGL object).
0105   //! @param theCtx currently bound OpenGL context
0106   Standard_EXPORT bool InitSamplerObject(const occ::handle<OpenGl_Context>& theCtx);
0107 
0108   //! Bind this Texture to the unit specified in sampler parameters.
0109   //! Also binds Sampler Object if it is allocated.
0110   void Bind(const occ::handle<OpenGl_Context>& theCtx) const
0111   {
0112     Bind(theCtx, mySampler->Parameters()->TextureUnit());
0113   }
0114 
0115   //! Unbind texture from the unit specified in sampler parameters.
0116   //! Also unbinds Sampler Object if it is allocated.
0117   void Unbind(const occ::handle<OpenGl_Context>& theCtx) const
0118   {
0119     Unbind(theCtx, mySampler->Parameters()->TextureUnit());
0120   }
0121 
0122   //! Bind this Texture to specified unit.
0123   //! Also binds Sampler Object if it is allocated.
0124   Standard_EXPORT void Bind(const occ::handle<OpenGl_Context>& theCtx,
0125                             const Graphic3d_TextureUnit        theTextureUnit) const;
0126 
0127   //! Unbind texture from specified unit.
0128   //! Also unbinds Sampler Object if it is allocated.
0129   Standard_EXPORT void Unbind(const occ::handle<OpenGl_Context>& theCtx,
0130                               const Graphic3d_TextureUnit        theTextureUnit) const;
0131 
0132   //! Revision of associated data source.
0133   size_t Revision() const { return myRevision; }
0134 
0135   //! Set revision of associated data source.
0136   void SetRevision(const size_t theRevision) { myRevision = theRevision; }
0137 
0138   //! Notice that texture will be unbound after this call.
0139   Standard_EXPORT bool Init(const occ::handle<OpenGl_Context>& theCtx,
0140                             const Image_PixMap&                theImage,
0141                             const Graphic3d_TypeOfTexture      theType,
0142                             const bool                         theIsColorMap);
0143 
0144   //! Initialize the texture with specified format, size and texture type.
0145   //! If theImage is empty the texture data will contain trash.
0146   //! Notice that texture will be unbound after this call.
0147   Standard_EXPORT bool Init(const occ::handle<OpenGl_Context>& theCtx,
0148                             const OpenGl_TextureFormat&        theFormat,
0149                             const NCollection_Vec3<int>&       theSizeXYZ,
0150                             const Graphic3d_TypeOfTexture      theType,
0151                             const Image_PixMap*                theImage = nullptr);
0152 
0153   //! Initialize the 2D texture with specified format, size and texture type.
0154   //! If theImage is empty the texture data will contain trash.
0155   //! Notice that texture will be unbound after this call.
0156   bool Init(const occ::handle<OpenGl_Context>& theCtx,
0157             const OpenGl_TextureFormat&        theFormat,
0158             const NCollection_Vec2<int>&       theSizeXY,
0159             const Graphic3d_TypeOfTexture      theType,
0160             const Image_PixMap*                theImage = nullptr)
0161   {
0162     return Init(theCtx, theFormat, NCollection_Vec3<int>(theSizeXY, 1), theType, theImage);
0163   }
0164 
0165   //! Initialize the texture with Graphic3d_TextureMap.
0166   //! It is an universal way to initialize.
0167   //! Suitable initialization method will be chosen.
0168   Standard_EXPORT bool Init(const occ::handle<OpenGl_Context>&        theCtx,
0169                             const occ::handle<Graphic3d_TextureRoot>& theTextureMap);
0170 
0171   //! Generate mipmaps.
0172   Standard_EXPORT bool GenerateMipmaps(const occ::handle<OpenGl_Context>& theCtx);
0173 
0174   //! Initialize the texture with Image_CompressedPixMap.
0175   Standard_EXPORT bool InitCompressed(const occ::handle<OpenGl_Context>& theCtx,
0176                                       const Image_CompressedPixMap&      theImage,
0177                                       const bool                         theIsColorMap);
0178 
0179   //! Initialize the 2D multisampling texture using glTexImage2DMultisample().
0180   Standard_EXPORT bool Init2DMultisample(const occ::handle<OpenGl_Context>& theCtx,
0181                                          const int                          theNbSamples,
0182                                          const int                          theTextFormat,
0183                                          const int                          theSizeX,
0184                                          const int                          theSizeY);
0185 
0186   //! Allocates texture rectangle with specified format and size.
0187   //! \note Texture data is not initialized (will contain trash).
0188   Standard_EXPORT bool InitRectangle(const occ::handle<OpenGl_Context>& theCtx,
0189                                      const int                          theSizeX,
0190                                      const int                          theSizeY,
0191                                      const OpenGl_TextureFormat&        theFormat);
0192 
0193   //! Initializes 3D texture rectangle with specified format and size.
0194   Standard_EXPORT bool Init3D(const occ::handle<OpenGl_Context>& theCtx,
0195                               const OpenGl_TextureFormat&        theFormat,
0196                               const NCollection_Vec3<int>&       theSizeXYZ,
0197                               const void*                        thePixels);
0198 
0199   //! @return true if texture was generated within mipmaps
0200   bool HasMipmaps() const { return myMaxMipLevel > 0; }
0201 
0202   //! Return upper mipmap level index (0 means no mipmaps).
0203   int MaxMipmapLevel() const { return myMaxMipLevel; }
0204 
0205   //! Return number of MSAA samples.
0206   int NbSamples() const { return myNbSamples; }
0207 
0208   //! Returns estimated GPU memory usage for holding data without considering overheads and
0209   //! allocation alignment rules.
0210   Standard_EXPORT size_t EstimatedDataSize() const override;
0211 
0212   //! Returns TRUE for point sprite texture.
0213   virtual bool IsPointSprite() const { return false; }
0214 
0215   //! Auxiliary method for making an image dump from texture data.
0216   //! @param[out] theImage    result image data (will be overridden)
0217   //! @param[in] theCtx       active GL context
0218   //! @param[in] theTexUnit   texture slot to use
0219   //! @param[in] theLevel     mipmap level to dump
0220   //! @param[in] theCubeSide  cubemap side to dump within [0, 5] range
0221   //! @return FALSE on error
0222   Standard_EXPORT bool ImageDump(Image_PixMap&                      theImage,
0223                                  const occ::handle<OpenGl_Context>& theCtx,
0224                                  Graphic3d_TextureUnit              theTexUnit,
0225                                  int                                theLevel    = 0,
0226                                  int                                theCubeSide = 0) const;
0227 
0228 public:
0229   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used "
0230                       "instead")
0231   static bool GetDataFormat(const occ::handle<OpenGl_Context>& theCtx,
0232                             const Image_Format                 theFormat,
0233                             int&                               theTextFormat,
0234                             unsigned int&                      thePixelFormat,
0235                             unsigned int&                      theDataType)
0236   {
0237     OpenGl_TextureFormat aFormat = OpenGl_TextureFormat::FindFormat(theCtx, theFormat, false);
0238     theTextFormat                = aFormat.InternalFormat();
0239     thePixelFormat               = aFormat.PixelFormat();
0240     theDataType                  = aFormat.DataType();
0241     return aFormat.IsValid();
0242   }
0243 
0244   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat::FindFormat() should be used "
0245                       "instead")
0246   static bool GetDataFormat(const occ::handle<OpenGl_Context>& theCtx,
0247                             const Image_PixMap&                theData,
0248                             int&                               theTextFormat,
0249                             unsigned int&                      thePixelFormat,
0250                             unsigned int&                      theDataType)
0251   {
0252     OpenGl_TextureFormat aFormat =
0253       OpenGl_TextureFormat::FindFormat(theCtx, theData.Format(), false);
0254     theTextFormat  = aFormat.InternalFormat();
0255     thePixelFormat = aFormat.PixelFormat();
0256     theDataType    = aFormat.DataType();
0257     return aFormat.IsValid();
0258   }
0259 
0260   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of "
0261                       "separate parameters")
0262   bool Init(const occ::handle<OpenGl_Context>& theCtx,
0263             const int                          theTextFormat,
0264             const unsigned int                 thePixelFormat,
0265             const unsigned int                 theDataType,
0266             const int                          theSizeX,
0267             const int                          theSizeY,
0268             const Graphic3d_TypeOfTexture      theType,
0269             const Image_PixMap*                theImage = nullptr)
0270   {
0271     OpenGl_TextureFormat aFormat;
0272     aFormat.SetInternalFormat(theTextFormat);
0273     aFormat.SetPixelFormat(thePixelFormat);
0274     aFormat.SetDataType(theDataType);
0275     return Init(theCtx, aFormat, NCollection_Vec2<int>(theSizeX, theSizeY), theType, theImage);
0276   }
0277 
0278   Standard_DEPRECATED("Deprecated method, theIsColorMap parameter should be explicitly "
0279                       "specified")
0280   bool Init(const occ::handle<OpenGl_Context>& theCtx,
0281             const Image_PixMap&                theImage,
0282             const Graphic3d_TypeOfTexture      theType)
0283   {
0284     return Init(theCtx, theImage, theType, true);
0285   }
0286 
0287   Standard_DEPRECATED("Deprecated method, OpenGl_TextureFormat should be passed instead of "
0288                       "separate parameters")
0289   bool Init3D(const occ::handle<OpenGl_Context>& theCtx,
0290               const int                          theTextFormat,
0291               const unsigned int                 thePixelFormat,
0292               const unsigned int                 theDataType,
0293               const int                          theSizeX,
0294               const int                          theSizeY,
0295               const int                          theSizeZ,
0296               const void*                        thePixels)
0297   {
0298     OpenGl_TextureFormat aFormat;
0299     aFormat.SetInternalFormat(theTextFormat);
0300     aFormat.SetPixelFormat(thePixelFormat);
0301     aFormat.SetDataType(theDataType);
0302     return Init3D(theCtx, aFormat, NCollection_Vec3<int>(theSizeX, theSizeY, theSizeZ), thePixels);
0303   }
0304 
0305   //! Initializes 6 sides of cubemap.
0306   //! If theCubeMap is not NULL then size and format will be taken from it and corresponding
0307   //! arguments will be ignored. Otherwise this parameters will be taken from arguments.
0308   //! @param[in] theCtx          active OpenGL context
0309   //! @param[in] theCubeMap      cubemap definition, can be NULL
0310   //! @param[in] theSize         cubemap dimensions
0311   //! @param[in] theFormat       image format
0312   //! @param[in] theToGenMipmap  flag to generate mipmaped cubemap
0313   //! @param[in] theIsColorMap   flag indicating cubemap storing color values
0314   Standard_EXPORT bool InitCubeMap(const occ::handle<OpenGl_Context>&    theCtx,
0315                                    const occ::handle<Graphic3d_CubeMap>& theCubeMap,
0316                                    size_t                                theSize,
0317                                    Image_Format                          theFormat,
0318                                    bool                                  theToGenMipmap,
0319                                    bool                                  theIsColorMap);
0320 
0321 protected:
0322   //! Apply default sampler parameters after texture creation.
0323   Standard_EXPORT void applyDefaultSamplerParams(const occ::handle<OpenGl_Context>& theCtx);
0324 
0325 protected:
0326   occ::handle<OpenGl_Sampler> mySampler;     //!< texture sampler
0327   size_t                      myRevision;    //!< revision of associated data source
0328   unsigned int                myTextureId;   //!< GL resource ID
0329   unsigned int                myTarget;      //!< GL_TEXTURE_1D/GL_TEXTURE_2D/GL_TEXTURE_3D
0330   NCollection_Vec3<int>       mySize;        //!< texture width x height x depth
0331   unsigned int                myTextFormat;  //!< texture format - GL_RGB, GL_RGBA,...
0332   int                         mySizedFormat; //!< internal (sized) texture format
0333   int                         myNbSamples;   //!< number of MSAA samples
0334   int                         myMaxMipLevel; //!< upper mipmap level index (0 means no mipmaps)
0335   bool                        myIsAlpha;     //!< indicates alpha format
0336   // clang-format off
0337   bool             myIsTopDown;  //!< indicates if 2D surface is defined top-down (TRUE) or bottom-up (FALSE)
0338   // clang-format on
0339 };
0340 
0341 #endif // _OpenGl_Texture_H__