0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / OpenGl / OpenGl_Sampler.hxx
1 // Created on: 2014-10-08
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_Sampler_Header
17 #define _OpenGl_Sampler_Header
18
19 #include <OpenGl_Context.hxx>
20 #include <OpenGl_Resource.hxx>
21
22 class OpenGl_Texture;
23
24 //! Class implements OpenGL sampler object resource that
25 //! stores the sampling parameters for a texture access.
26 class OpenGl_Sampler : public OpenGl_Resource
27 {
28   friend class OpenGl_Context;
29   friend class OpenGl_Texture;
30   DEFINE_STANDARD_RTTIEXT(OpenGl_Sampler, OpenGl_Resource)
31 public:
32
33   //! Helpful constant defining invalid sampler identifier
34   static const GLuint NO_SAMPLER = 0;
35
36 public:
37
38   //! Creates new sampler object.
39   Standard_EXPORT OpenGl_Sampler (const Handle(Graphic3d_TextureParams)& theParams);
40
41   //! Releases resources of sampler object.
42   Standard_EXPORT virtual ~OpenGl_Sampler();
43
44   //! Destroys object - will release GPU memory if any.
45   Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
46
47   //! Returns estimated GPU memory usage - not implemented.
48   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
49
50   //! Creates an uninitialized sampler object.
51   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theContext);
52
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);
58
59   //! Returns true if current object was initialized.
60   Standard_Boolean IsValid() const
61   {
62     return isValidSampler();
63   }
64
65   //! Binds sampler object to texture unit specified in parameters.
66   void Bind (const Handle(OpenGl_Context)& theCtx)
67   {
68     Bind (theCtx, myParams->TextureUnit());
69   }
70
71   //! Unbinds sampler object from texture unit specified in parameters.
72   void Unbind (const Handle(OpenGl_Context)& theCtx)
73   {
74     Unbind (theCtx, myParams->TextureUnit());
75   }
76
77   //! Binds sampler object to the given texture unit.
78   Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
79                              const Graphic3d_TextureUnit   theUnit);
80
81   //! Unbinds sampler object from the given texture unit.
82   Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
83                                const Graphic3d_TextureUnit   theUnit);
84
85   //! Sets specific sampler parameter.
86   void SetParameter (const Handle(OpenGl_Context)& theCtx,
87                      GLenum theTarget,
88                      GLenum theParam,
89                      GLint  theValue)
90   {
91     setParameter (theCtx, this, theTarget, theParam, theValue);
92   }
93
94   //! Returns OpenGL sampler ID.
95   GLuint SamplerID() const
96   {
97     return mySamplerID;
98   }
99
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; }
103
104   //! Setup immutable flag. It is not possible unsetting this flag without Sampler destruction.
105   void SetImmutable() { myIsImmutable = true; }
106
107   //! Returns texture parameters.
108   const Handle(Graphic3d_TextureParams)& Parameters() { return myParams; }
109
110   //! Sets texture parameters.
111   Standard_EXPORT void SetParameters (const Handle(Graphic3d_TextureParams)& theParams);
112
113   //! Returns texture parameters initialization state.
114   bool ToUpdateParameters() const { return mySamplerRevision != myParams->SamplerRevision(); }
115
116 protected:
117
118   //! Checks if sampler object is valid.
119   Standard_Boolean isValidSampler() const
120   {
121     return mySamplerID != NO_SAMPLER;
122   }
123
124   //! Sets specific sampler parameter.
125   Standard_EXPORT static void setParameter (const Handle(OpenGl_Context)& theContext,
126                                             OpenGl_Sampler* theSampler,
127                                             GLenum theTarget,
128                                             GLenum theParam,
129                                             GLint  theValue);
130
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);
143
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);
148
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);
153
154 protected:
155
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
160
161 };
162
163 DEFINE_STANDARD_HANDLE(OpenGl_Sampler, OpenGl_Resource)
164
165 #endif // _OpenGl_Sampler_Header