Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:35

0001 // Created on: 2014-10-08
0002 // Created by: Denis BOGOLEPOV
0003 // Copyright (c) 2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _OpenGl_Sampler_Header
0017 #define _OpenGl_Sampler_Header
0018 
0019 #include <Graphic3d_TextureParams.hxx>
0020 #include <OpenGl_Resource.hxx>
0021 
0022 class OpenGl_Texture;
0023 
0024 //! Class implements OpenGL sampler object resource that
0025 //! stores the sampling parameters for a texture access.
0026 class OpenGl_Sampler : public OpenGl_Resource
0027 {
0028   friend class OpenGl_Context;
0029   friend class OpenGl_Texture;
0030   friend class OpenGl_Text;
0031   DEFINE_STANDARD_RTTIEXT(OpenGl_Sampler, OpenGl_Resource)
0032 public:
0033 
0034   //! Helpful constant defining invalid sampler identifier
0035   static const unsigned int NO_SAMPLER = 0;
0036 
0037 public:
0038 
0039   //! Creates new sampler object.
0040   Standard_EXPORT OpenGl_Sampler (const Handle(Graphic3d_TextureParams)& theParams);
0041 
0042   //! Releases resources of sampler object.
0043   Standard_EXPORT virtual ~OpenGl_Sampler();
0044 
0045   //! Destroys object - will release GPU memory if any.
0046   Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
0047 
0048   //! Returns estimated GPU memory usage - not implemented.
0049   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
0050 
0051   //! Creates an uninitialized sampler object.
0052   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theContext);
0053 
0054   //! Creates and initializes sampler object.
0055   //! Existing object will be reused if possible, however if existing Sampler Object has Immutable flag
0056   //! and texture parameters should be re-initialized, then Sampler Object will be recreated.
0057   Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theContext,
0058                                          const OpenGl_Texture& theTexture);
0059 
0060   //! Returns true if current object was initialized.
0061   Standard_Boolean IsValid() const
0062   {
0063     return isValidSampler();
0064   }
0065 
0066   //! Binds sampler object to texture unit specified in parameters.
0067   void Bind (const Handle(OpenGl_Context)& theCtx)
0068   {
0069     Bind (theCtx, myParams->TextureUnit());
0070   }
0071 
0072   //! Unbinds sampler object from texture unit specified in parameters.
0073   void Unbind (const Handle(OpenGl_Context)& theCtx)
0074   {
0075     Unbind (theCtx, myParams->TextureUnit());
0076   }
0077 
0078   //! Binds sampler object to the given texture unit.
0079   Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
0080                              const Graphic3d_TextureUnit   theUnit);
0081 
0082   //! Unbinds sampler object from the given texture unit.
0083   Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
0084                                const Graphic3d_TextureUnit   theUnit);
0085 
0086   //! Sets specific sampler parameter.
0087   void SetParameter (const Handle(OpenGl_Context)& theCtx,
0088                      unsigned int theTarget,
0089                      unsigned int theParam,
0090                      Standard_Integer theValue)
0091   {
0092     setParameter (theCtx, this, theTarget, theParam, theValue);
0093   }
0094 
0095   //! Returns OpenGL sampler ID.
0096   unsigned int SamplerID() const
0097   {
0098     return mySamplerID;
0099   }
0100 
0101   //! Return immutable flag preventing further modifications of sampler parameters, FALSE by default.
0102   //! Immutable flag might be set when Sampler Object is used within Bindless Texture.
0103   bool IsImmutable() const { return myIsImmutable; }
0104 
0105   //! Setup immutable flag. It is not possible unsetting this flag without Sampler destruction.
0106   void SetImmutable() { myIsImmutable = true; }
0107 
0108   //! Returns texture parameters.
0109   const Handle(Graphic3d_TextureParams)& Parameters() { return myParams; }
0110 
0111   //! Sets texture parameters.
0112   Standard_EXPORT void SetParameters (const Handle(Graphic3d_TextureParams)& theParams);
0113 
0114   //! Returns texture parameters initialization state.
0115   bool ToUpdateParameters() const { return mySamplerRevision != myParams->SamplerRevision(); }
0116 
0117 protected:
0118 
0119   //! Checks if sampler object is valid.
0120   Standard_Boolean isValidSampler() const
0121   {
0122     return mySamplerID != NO_SAMPLER;
0123   }
0124 
0125   //! Sets specific sampler parameter.
0126   Standard_EXPORT static void setParameter (const Handle(OpenGl_Context)& theContext,
0127                                             OpenGl_Sampler* theSampler,
0128                                             unsigned int theTarget,
0129                                             unsigned int theParam,
0130                                             Standard_Integer theValue);
0131 
0132   //! Apply sampler parameters.
0133   //! @param theCtx     [in] active OpenGL context
0134   //! @param theParams  [in] texture parameters to apply
0135   //! @param theSampler [in] apply parameters to Texture object (NULL)
0136   //!                        or to specified Sampler object (non-NULL, sampler is not required to be bound)
0137   //! @param theTarget  [in] OpenGL texture target
0138   //! @param theMaxMipLevel [in] maximum mipmap level defined within the texture
0139   Standard_EXPORT static void applySamplerParams (const Handle(OpenGl_Context)& theCtx,
0140                                                   const Handle(Graphic3d_TextureParams)& theParams,
0141                                                   OpenGl_Sampler* theSampler,
0142                                                   const unsigned int theTarget,
0143                                                   const Standard_Integer theMaxMipLevel);
0144 
0145   //! Apply global texture state for deprecated OpenGL functionality.
0146   Standard_EXPORT static void applyGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
0147                                                         const OpenGl_Texture& theTexture,
0148                                                         const Handle(Graphic3d_TextureParams)& theParams);
0149 
0150   //! Reset global texture state for deprecated OpenGL functionality.
0151   Standard_EXPORT static void resetGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
0152                                                         const OpenGl_Texture& theTexture,
0153                                                         const Handle(Graphic3d_TextureParams)& theParams);
0154 
0155 protected:
0156 
0157   Handle(Graphic3d_TextureParams) myParams;          //!< texture parameters
0158   unsigned int                    mySamplerRevision; //!< modification counter of parameters related to sampler state
0159   unsigned int                    mySamplerID;       //!< OpenGL sampler object ID
0160   bool                            myIsImmutable;     //!< immutable flag preventing further modifications of sampler parameters, FALSE by default
0161 
0162 };
0163 
0164 DEFINE_STANDARD_HANDLE(OpenGl_Sampler, OpenGl_Resource)
0165 
0166 #endif // _OpenGl_Sampler_Header