Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-11 09:18:49

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   //! Helpful constant defining invalid sampler identifier
0034   static const unsigned int NO_SAMPLER = 0;
0035 
0036 public:
0037   //! Creates new sampler object.
0038   Standard_EXPORT OpenGl_Sampler(const occ::handle<Graphic3d_TextureParams>& theParams);
0039 
0040   //! Releases resources of sampler object.
0041   Standard_EXPORT ~OpenGl_Sampler() override;
0042 
0043   //! Destroys object - will release GPU memory if any.
0044   Standard_EXPORT void Release(OpenGl_Context* theContext) override;
0045 
0046   //! Returns estimated GPU memory usage - not implemented.
0047   size_t EstimatedDataSize() const override { return 0; }
0048 
0049   //! Creates an uninitialized sampler object.
0050   Standard_EXPORT bool Create(const occ::handle<OpenGl_Context>& theContext);
0051 
0052   //! Creates and initializes sampler object.
0053   //! Existing object will be reused if possible, however if existing Sampler Object has Immutable
0054   //! flag and texture parameters should be re-initialized, then Sampler Object will be recreated.
0055   Standard_EXPORT bool Init(const occ::handle<OpenGl_Context>& theContext,
0056                             const OpenGl_Texture&              theTexture);
0057 
0058   //! Returns true if current object was initialized.
0059   bool IsValid() const { return isValidSampler(); }
0060 
0061   //! Binds sampler object to texture unit specified in parameters.
0062   void Bind(const occ::handle<OpenGl_Context>& theCtx) { Bind(theCtx, myParams->TextureUnit()); }
0063 
0064   //! Unbinds sampler object from texture unit specified in parameters.
0065   void Unbind(const occ::handle<OpenGl_Context>& theCtx)
0066   {
0067     Unbind(theCtx, myParams->TextureUnit());
0068   }
0069 
0070   //! Binds sampler object to the given texture unit.
0071   Standard_EXPORT void Bind(const occ::handle<OpenGl_Context>& theCtx,
0072                             const Graphic3d_TextureUnit        theUnit);
0073 
0074   //! Unbinds sampler object from the given texture unit.
0075   Standard_EXPORT void Unbind(const occ::handle<OpenGl_Context>& theCtx,
0076                               const Graphic3d_TextureUnit        theUnit);
0077 
0078   //! Sets specific sampler parameter.
0079   void SetParameter(const occ::handle<OpenGl_Context>& theCtx,
0080                     unsigned int                       theTarget,
0081                     unsigned int                       theParam,
0082                     int                                theValue)
0083   {
0084     setParameter(theCtx, this, theTarget, theParam, theValue);
0085   }
0086 
0087   //! Returns OpenGL sampler ID.
0088   unsigned int SamplerID() const { return mySamplerID; }
0089 
0090   //! Return immutable flag preventing further modifications of sampler parameters, FALSE by
0091   //! default. Immutable flag might be set when Sampler Object is used within Bindless Texture.
0092   bool IsImmutable() const { return myIsImmutable; }
0093 
0094   //! Setup immutable flag. It is not possible unsetting this flag without Sampler destruction.
0095   void SetImmutable() { myIsImmutable = true; }
0096 
0097   //! Returns texture parameters.
0098   const occ::handle<Graphic3d_TextureParams>& Parameters() { return myParams; }
0099 
0100   //! Sets texture parameters.
0101   Standard_EXPORT void SetParameters(const occ::handle<Graphic3d_TextureParams>& theParams);
0102 
0103   //! Returns texture parameters initialization state.
0104   bool ToUpdateParameters() const { return mySamplerRevision != myParams->SamplerRevision(); }
0105 
0106 protected:
0107   //! Checks if sampler object is valid.
0108   bool isValidSampler() const { return mySamplerID != NO_SAMPLER; }
0109 
0110   //! Sets specific sampler parameter.
0111   Standard_EXPORT static void setParameter(const occ::handle<OpenGl_Context>& theContext,
0112                                            OpenGl_Sampler*                    theSampler,
0113                                            unsigned int                       theTarget,
0114                                            unsigned int                       theParam,
0115                                            int                                theValue);
0116 
0117   //! Apply sampler parameters.
0118   //! @param[in] theCtx      active OpenGL context
0119   //! @param[in] theParams   texture parameters to apply
0120   //! @param[in] theSampler  apply parameters to Texture object (NULL)
0121   //!                        or to specified Sampler object (non-NULL, sampler is not required to be
0122   //!                        bound)
0123   //! @param[in] theTarget   OpenGL texture target
0124   //! @param[in] theMaxMipLevel  maximum mipmap level defined within the texture
0125   Standard_EXPORT static void applySamplerParams(
0126     const occ::handle<OpenGl_Context>&          theCtx,
0127     const occ::handle<Graphic3d_TextureParams>& theParams,
0128     OpenGl_Sampler*                             theSampler,
0129     const unsigned int                          theTarget,
0130     const int                                   theMaxMipLevel);
0131 
0132   //! Apply global texture state for deprecated OpenGL functionality.
0133   Standard_EXPORT static void applyGlobalTextureParams(
0134     const occ::handle<OpenGl_Context>&          theCtx,
0135     const OpenGl_Texture&                       theTexture,
0136     const occ::handle<Graphic3d_TextureParams>& theParams);
0137 
0138   //! Reset global texture state for deprecated OpenGL functionality.
0139   Standard_EXPORT static void resetGlobalTextureParams(
0140     const occ::handle<OpenGl_Context>&          theCtx,
0141     const OpenGl_Texture&                       theTexture,
0142     const occ::handle<Graphic3d_TextureParams>& theParams);
0143 
0144 protected:
0145   occ::handle<Graphic3d_TextureParams> myParams; //!< texture parameters
0146   // clang-format off
0147   unsigned int                    mySamplerRevision; //!< modification counter of parameters related to sampler state
0148   unsigned int                    mySamplerID;       //!< OpenGL sampler object ID
0149   bool                            myIsImmutable;     //!< immutable flag preventing further modifications of sampler parameters, FALSE by default
0150   // clang-format on
0151 };
0152 
0153 #endif // _OpenGl_Sampler_Header