0030748: Visualization - Marker displayed in immediate layer ruins QT Quick view...
[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.
132 //! If Sampler Object is not NULL and valid resource, the parameters will be set to it (and it is not required Sampler Object being bound).
133 //! Otherwise, parameters will be applied to currently bound Texture object.
134 Standard_EXPORT static void applySamplerParams (const Handle(OpenGl_Context)& theCtx,
135 const Handle(Graphic3d_TextureParams)& theParams,
136 OpenGl_Sampler* theSampler,
137 const GLenum theTarget,
138 const bool theHasMipMaps);
139
140 //! Apply global texture state for deprecated OpenGL functionality.
141 Standard_EXPORT static void applyGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
142 const OpenGl_Texture& theTexture,
143 const Handle(Graphic3d_TextureParams)& theParams);
144
145 //! Reset global texture state for deprecated OpenGL functionality.
146 Standard_EXPORT static void resetGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
147 const OpenGl_Texture& theTexture,
148 const Handle(Graphic3d_TextureParams)& theParams);
25ef750e 149
cc8cbabe 150protected:
25ef750e 151
cc8cbabe 152 Handle(Graphic3d_TextureParams) myParams; //!< texture parameters
153 unsigned int mySamplerRevision; //!< modification counter of parameters related to sampler state
154 GLuint mySamplerID; //!< OpenGL sampler object ID
155 bool myIsImmutable; //!< immutable flag preventing further modifications of sampler parameters, FALSE by default
25ef750e 156
157};
158
cc8cbabe 159DEFINE_STANDARD_HANDLE(OpenGl_Sampler, OpenGl_Resource)
160
161#endif // _OpenGl_Sampler_Header