ea2423bbc1cad199280176db3be680d4b1cb211d
[occt.git] / src / OpenGl / OpenGl_Sampler.cxx
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 #include <OpenGl_Sampler.hxx>
17 #include <OpenGl_GlCore33.hxx>
18 #include <Standard_Assert.hxx>
19
20
21 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Sampler,OpenGl_Resource)
22
23 // =======================================================================
24 // function : OpenGl_Sampler
25 // purpose  :
26 // =======================================================================
27 OpenGl_Sampler::OpenGl_Sampler()
28 : mySamplerID (NO_SAMPLER)
29 {
30   //
31 }
32
33 // =======================================================================
34 // function : ~OpenGl_Sampler
35 // purpose  :
36 // =======================================================================
37 OpenGl_Sampler::~OpenGl_Sampler()
38 {
39   Release (NULL);
40 }
41
42 // =======================================================================
43 // function : Release
44 // purpose  :
45 // =======================================================================
46 void OpenGl_Sampler::Release (OpenGl_Context* theContext)
47 {
48   if (isValidSampler())
49   {
50     // application can not handle this case by exception - this is bug in code
51     Standard_ASSERT_RETURN (theContext != NULL,
52       "OpenGl_Sampler destroyed without GL context! Possible GPU memory leakage...",);
53
54     if (theContext->IsValid())
55     {
56     #if !defined(GL_ES_VERSION_2_0) || defined(GL_ES_VERSION_3_0)
57       theContext->core33->glDeleteSamplers (1, &mySamplerID);
58     #endif
59     }
60
61     mySamplerID = NO_SAMPLER;
62   }
63 }
64
65 // =======================================================================
66 // function : Init
67 // purpose  : Initializes sampler object
68 // =======================================================================
69 Standard_Boolean OpenGl_Sampler::Init (OpenGl_Context& theContext)
70 {
71   if (theContext.core33 == NULL)
72   {
73     return Standard_False;
74   }
75
76   if (isValidSampler())
77   {
78     Release (&theContext);
79   }
80
81 #if !defined(GL_ES_VERSION_2_0) || defined(GL_ES_VERSION_3_0)
82   theContext.core33->glGenSamplers (1, &mySamplerID);
83   return Standard_True;
84 #else
85   return Standard_False;
86 #endif
87 }
88
89 // =======================================================================
90 // function : Bind
91 // purpose  : Binds sampler object to the given texture unit
92 // =======================================================================
93 void OpenGl_Sampler::Bind (OpenGl_Context& theContext,
94                            const GLuint    theUnit)
95 {
96   if (isValidSampler())
97   {
98   #if !defined(GL_ES_VERSION_2_0) || defined(GL_ES_VERSION_3_0)
99     theContext.core33->glBindSampler (theUnit, mySamplerID);
100   #endif
101   }
102 }
103
104 // =======================================================================
105 // function : Unbind
106 // purpose  : Unbinds sampler object from the given texture unit
107 // =======================================================================
108 void OpenGl_Sampler::Unbind (OpenGl_Context& theContext,
109                              const GLuint    theUnit)
110 {
111   if (isValidSampler())
112   {
113   #if !defined(GL_ES_VERSION_2_0) || defined(GL_ES_VERSION_3_0)
114     theContext.core33->glBindSampler (theUnit, NO_SAMPLER);
115   #endif
116   }
117 }
118
119 // =======================================================================
120 // function : SetParameter
121 // purpose  : Sets sampler parameters
122 // =======================================================================
123 void OpenGl_Sampler::SetParameter (OpenGl_Context& theContext,
124                                    const GLenum    theParam,
125                                    const GLint     theValue)
126 {
127   if (isValidSampler())
128   {
129   #if !defined(GL_ES_VERSION_2_0) || defined(GL_ES_VERSION_3_0)
130     theContext.core33->glSamplerParameteri (mySamplerID, theParam, theValue);
131   #endif
132   }
133 }