1 // Created on: 2014-10-08
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_Sampler_Header
17 #define _OpenGl_Sampler_Header
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_Resource.hxx>
24 //! Class implements OpenGL sampler object resource that
25 //! stores the sampling parameters for a texture access.
26 class OpenGl_Sampler : public OpenGl_Resource
28 friend class OpenGl_Context;
29 friend class OpenGl_Texture;
30 DEFINE_STANDARD_RTTIEXT(OpenGl_Sampler, OpenGl_Resource)
33 //! Helpful constant defining invalid sampler identifier
34 static const GLuint NO_SAMPLER = 0;
38 //! Creates new sampler object.
39 Standard_EXPORT OpenGl_Sampler (const Handle(Graphic3d_TextureParams)& theParams);
41 //! Releases resources of sampler object.
42 Standard_EXPORT virtual ~OpenGl_Sampler();
44 //! Destroys object - will release GPU memory if any.
45 Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
47 //! Returns estimated GPU memory usage - not implemented.
48 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
50 //! Creates an uninitialized sampler object.
51 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theContext);
53 //! Creates and initializes sampler object.
54 //! Existing object will be reused if possible, however if existing Sampler Object has Immutable flag
55 //! and texture parameters should be re-initialized, then Sampler Object will be recreated.
56 Standard_EXPORT Standard_Boolean Init (const Handle(OpenGl_Context)& theContext,
57 const OpenGl_Texture& theTexture);
59 //! Returns true if current object was initialized.
60 Standard_Boolean IsValid() const
62 return isValidSampler();
65 //! Binds sampler object to texture unit specified in parameters.
66 void Bind (const Handle(OpenGl_Context)& theCtx)
68 Bind (theCtx, myParams->TextureUnit());
71 //! Unbinds sampler object from texture unit specified in parameters.
72 void Unbind (const Handle(OpenGl_Context)& theCtx)
74 Unbind (theCtx, myParams->TextureUnit());
77 //! Binds sampler object to the given texture unit.
78 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
79 const Graphic3d_TextureUnit theUnit);
81 //! Unbinds sampler object from the given texture unit.
82 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
83 const Graphic3d_TextureUnit theUnit);
85 //! Sets specific sampler parameter.
86 void SetParameter (const Handle(OpenGl_Context)& theCtx,
91 setParameter (theCtx, this, theTarget, theParam, theValue);
94 //! Returns OpenGL sampler ID.
95 GLuint SamplerID() const
100 //! Return immutable flag preventing further modifications of sampler parameters, FALSE by default.
101 //! Immutable flag might be set when Sampler Object is used within Bindless Texture.
102 bool IsImmutable() const { return myIsImmutable; }
104 //! Setup immutable flag. It is not possible unsetting this flag without Sampler destruction.
105 void SetImmutable() { myIsImmutable = true; }
107 //! Returns texture parameters.
108 const Handle(Graphic3d_TextureParams)& Parameters() { return myParams; }
110 //! Sets texture parameters.
111 Standard_EXPORT void SetParameters (const Handle(Graphic3d_TextureParams)& theParams);
113 //! Returns texture parameters initialization state.
114 bool ToUpdateParameters() const { return mySamplerRevision != myParams->SamplerRevision(); }
118 //! Checks if sampler object is valid.
119 Standard_Boolean isValidSampler() const
121 return mySamplerID != NO_SAMPLER;
124 //! Sets specific sampler parameter.
125 Standard_EXPORT static void setParameter (const Handle(OpenGl_Context)& theContext,
126 OpenGl_Sampler* theSampler,
131 //! Apply sampler parameters.
132 //! @param theCtx [in] active OpenGL context
133 //! @param theParams [in] texture parameters to apply
134 //! @param theSampler [in] apply parameters to Texture object (NULL)
135 //! or to specified Sampler object (non-NULL, sampler is not required to be bound)
136 //! @param theTarget [in] OpenGL texture target
137 //! @param theMaxMipLevel [in] maximum mipmap level defined within the texture
138 Standard_EXPORT static void applySamplerParams (const Handle(OpenGl_Context)& theCtx,
139 const Handle(Graphic3d_TextureParams)& theParams,
140 OpenGl_Sampler* theSampler,
141 const GLenum theTarget,
142 const Standard_Integer theMaxMipLevel);
144 //! Apply global texture state for deprecated OpenGL functionality.
145 Standard_EXPORT static void applyGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
146 const OpenGl_Texture& theTexture,
147 const Handle(Graphic3d_TextureParams)& theParams);
149 //! Reset global texture state for deprecated OpenGL functionality.
150 Standard_EXPORT static void resetGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
151 const OpenGl_Texture& theTexture,
152 const Handle(Graphic3d_TextureParams)& theParams);
156 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
157 unsigned int mySamplerRevision; //!< modification counter of parameters related to sampler state
158 GLuint mySamplerID; //!< OpenGL sampler object ID
159 bool myIsImmutable; //!< immutable flag preventing further modifications of sampler parameters, FALSE by default
163 DEFINE_STANDARD_HANDLE(OpenGl_Sampler, OpenGl_Resource)
165 #endif // _OpenGl_Sampler_Header