0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / OpenGl / OpenGl_Sampler.hxx
CommitLineData
25ef750e 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
cc8cbabe 16#ifndef _OpenGl_Sampler_Header
17#define _OpenGl_Sampler_Header
25ef750e 18
19#include <OpenGl_Context.hxx>
cc8cbabe 20#include <OpenGl_Resource.hxx>
25ef750e 21
cc8cbabe 22class OpenGl_Texture;
c04c30b3 23
25ef750e 24//! Class implements OpenGL sampler object resource that
25//! stores the sampling parameters for a texture access.
26class OpenGl_Sampler : public OpenGl_Resource
27{
cc8cbabe 28 friend class OpenGl_Context;
29 friend class OpenGl_Texture;
30 DEFINE_STANDARD_RTTIEXT(OpenGl_Sampler, OpenGl_Resource)
25ef750e 31public:
32
33 //! Helpful constant defining invalid sampler identifier
34 static const GLuint NO_SAMPLER = 0;
35
36public:
37
38 //! Creates new sampler object.
cc8cbabe 39 Standard_EXPORT OpenGl_Sampler (const Handle(Graphic3d_TextureParams)& theParams);
25ef750e 40
41 //! Releases resources of sampler object.
42 Standard_EXPORT virtual ~OpenGl_Sampler();
43
44 //! Destroys object - will release GPU memory if any.
79104795 45 Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
25ef750e 46
15669413 47 //! Returns estimated GPU memory usage - not implemented.
48 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
49
cc8cbabe 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);
25ef750e 58
59 //! Returns true if current object was initialized.
60 Standard_Boolean IsValid() const
61 {
62 return isValidSampler();
63 }
64
cc8cbabe 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
25ef750e 77 //! Binds sampler object to the given texture unit.
cc8cbabe 78 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx,
79 const Graphic3d_TextureUnit theUnit);
25ef750e 80
81 //! Unbinds sampler object from the given texture unit.
cc8cbabe 82 Standard_EXPORT void Unbind (const Handle(OpenGl_Context)& theCtx,
83 const Graphic3d_TextureUnit theUnit);
25ef750e 84
85 //! Sets specific sampler parameter.
cc8cbabe 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 }
25ef750e 93
94 //! Returns OpenGL sampler ID.
95 GLuint SamplerID() const
96 {
97 return mySamplerID;
98 }
99
cc8cbabe 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
25ef750e 116protected:
117
118 //! Checks if sampler object is valid.
119 Standard_Boolean isValidSampler() const
120 {
121 return mySamplerID != NO_SAMPLER;
122 }
123
cc8cbabe 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.
faff3767 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
cc8cbabe 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,
faff3767 142 const Standard_Integer theMaxMipLevel);
cc8cbabe 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);
25ef750e 153
cc8cbabe 154protected:
25ef750e 155
cc8cbabe 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
25ef750e 160
161};
162
cc8cbabe 163DEFINE_STANDARD_HANDLE(OpenGl_Sampler, OpenGl_Resource)
164
165#endif // _OpenGl_Sampler_Header