3ec1e07723fa5f8eb0e9b4cb7783a5855801fc13
[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
18 #include <OpenGl_ArbSamplerObject.hxx>
19 #include <OpenGl_Texture.hxx>
20 #include <Standard_Assert.hxx>
21
22 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Sampler,OpenGl_Resource)
23
24 // =======================================================================
25 // function : OpenGl_Sampler
26 // purpose  :
27 // =======================================================================
28 OpenGl_Sampler::OpenGl_Sampler (const Handle(Graphic3d_TextureParams)& theParams)
29 : myParams (theParams),
30   mySamplerRevision (0),
31   mySamplerID (NO_SAMPLER),
32   myIsImmutable (false)
33 {
34   if (myParams.IsNull())
35   {
36     myParams = new Graphic3d_TextureParams();
37   }
38 }
39
40 // =======================================================================
41 // function : ~OpenGl_Sampler
42 // purpose  :
43 // =======================================================================
44 OpenGl_Sampler::~OpenGl_Sampler()
45 {
46   Release (NULL);
47 }
48
49 // =======================================================================
50 // function : Release
51 // purpose  :
52 // =======================================================================
53 void OpenGl_Sampler::Release (OpenGl_Context* theCtx)
54 {
55   myIsImmutable = false;
56   mySamplerRevision = myParams->SamplerRevision() - 1;
57   if (!isValidSampler())
58   {
59     return;
60   }
61
62   // application can not handle this case by exception - this is bug in code
63   Standard_ASSERT_RETURN (theCtx != NULL,
64     "OpenGl_Sampler destroyed without GL context! Possible GPU memory leakage...",);
65
66   if (theCtx->IsValid())
67   {
68     theCtx->arbSamplerObject->glDeleteSamplers (1, &mySamplerID);
69   }
70
71   mySamplerID = NO_SAMPLER;
72 }
73
74 // =======================================================================
75 // function : Create
76 // purpose  :
77 // =======================================================================
78 Standard_Boolean OpenGl_Sampler::Create (const Handle(OpenGl_Context)& theCtx)
79 {
80   if (isValidSampler())
81   {
82     return Standard_True;
83   }
84   else if (theCtx->arbSamplerObject == NULL)
85   {
86     return Standard_False;
87   }
88
89   theCtx->arbSamplerObject->glGenSamplers (1, &mySamplerID);
90   return Standard_True;
91 }
92
93 // =======================================================================
94 // function : Init
95 // purpose  :
96 // =======================================================================
97 Standard_Boolean OpenGl_Sampler::Init (const Handle(OpenGl_Context)& theCtx,
98                                        const OpenGl_Texture& theTexture)
99 {
100   if (isValidSampler())
101   {
102     if (!ToUpdateParameters())
103     {
104       return Standard_True;
105     }
106     else if (!myIsImmutable)
107     {
108       applySamplerParams (theCtx, myParams, this, theTexture.GetTarget(), theTexture.MaxMipmapLevel());
109       return Standard_True;
110     }
111     Release (theCtx.get());
112   }
113
114   if (!Create (theCtx))
115   {
116     return Standard_False;
117   }
118
119   applySamplerParams (theCtx, myParams, this, theTexture.GetTarget(), theTexture.MaxMipmapLevel());
120   return Standard_True;
121 }
122
123 // =======================================================================
124 // function : Bind
125 // purpose  : Binds sampler object to the given texture unit
126 // =======================================================================
127 void OpenGl_Sampler::Bind (const Handle(OpenGl_Context)& theCtx,
128                            const Graphic3d_TextureUnit   theUnit)
129 {
130   if (isValidSampler())
131   {
132     theCtx->arbSamplerObject->glBindSampler (theUnit, mySamplerID);
133   }
134 }
135
136 // =======================================================================
137 // function : Unbind
138 // purpose  : Unbinds sampler object from the given texture unit
139 // =======================================================================
140 void OpenGl_Sampler::Unbind (const Handle(OpenGl_Context)& theCtx,
141                              const Graphic3d_TextureUnit   theUnit)
142 {
143   if (isValidSampler())
144   {
145     theCtx->arbSamplerObject->glBindSampler (theUnit, NO_SAMPLER);
146   }
147 }
148
149 // =======================================================================
150 // function : setParameter
151 // purpose  :
152 // =======================================================================
153 void OpenGl_Sampler::setParameter (const Handle(OpenGl_Context)& theCtx,
154                                    OpenGl_Sampler* theSampler,
155                                    GLenum theTarget,
156                                    GLenum theParam,
157                                    GLint  theValue)
158 {
159   if (theSampler != NULL && theSampler->isValidSampler())
160   {
161     theCtx->arbSamplerObject->glSamplerParameteri (theSampler->mySamplerID, theParam, theValue);
162   }
163   else
164   {
165     theCtx->core11fwd->glTexParameteri (theTarget, theParam, theValue);
166   }
167 }
168
169 // =======================================================================
170 // function : SetParameters
171 // purpose  :
172 // =======================================================================
173 void OpenGl_Sampler::SetParameters (const Handle(Graphic3d_TextureParams)& theParams)
174 {
175   if (myParams != theParams)
176   {
177     myParams = theParams;
178     mySamplerRevision = myParams->SamplerRevision() - 1;
179   }
180 }
181
182 // =======================================================================
183 // function : applySamplerParams
184 // purpose  :
185 // =======================================================================
186 void OpenGl_Sampler::applySamplerParams (const Handle(OpenGl_Context)& theCtx,
187                                          const Handle(Graphic3d_TextureParams)& theParams,
188                                          OpenGl_Sampler* theSampler,
189                                          const GLenum theTarget,
190                                          const Standard_Integer theMaxMipLevels)
191 {
192   if (theSampler != NULL && theSampler->Parameters() == theParams)
193   {
194     theSampler->mySamplerRevision = theParams->SamplerRevision();
195   }
196
197   // setup texture filtering
198   const GLenum aFilter = (theParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
199   GLenum aFilterMin = aFilter;
200   if (theMaxMipLevels > 0)
201   {
202     aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
203     if (theParams->Filter() == Graphic3d_TOTF_BILINEAR)
204     {
205       aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
206     }
207     else if (theParams->Filter() == Graphic3d_TOTF_TRILINEAR)
208     {
209       aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
210     }
211   }
212
213   setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_MIN_FILTER, aFilterMin);
214   setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_MAG_FILTER, aFilter);
215
216   // setup texture wrapping
217   const GLenum aWrapMode = theParams->IsRepeat() ? GL_REPEAT : theCtx->TextureWrapClamp();
218   setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_WRAP_S, aWrapMode);
219 #if !defined(GL_ES_VERSION_2_0)
220   if (theTarget == GL_TEXTURE_1D)
221   {
222     return;
223   }
224 #endif
225   setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_WRAP_T, aWrapMode);
226   if (theTarget == GL_TEXTURE_3D
227    || theTarget == GL_TEXTURE_CUBE_MAP)
228   {
229     setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_WRAP_R, aWrapMode);
230     return;
231   }
232
233   if (theCtx->extAnis)
234   {
235     // setup degree of anisotropy filter
236     const GLint aMaxDegree = theCtx->MaxDegreeOfAnisotropy();
237     GLint aDegree;
238     switch (theParams->AnisoFilter())
239     {
240       case Graphic3d_LOTA_QUALITY:
241       {
242         aDegree = aMaxDegree;
243         break;
244       }
245       case Graphic3d_LOTA_MIDDLE:
246       {
247         aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
248         break;
249       }
250       case Graphic3d_LOTA_FAST:
251       {
252         aDegree = 2;
253         break;
254       }
255       case Graphic3d_LOTA_OFF:
256       default:
257       {
258         aDegree = 1;
259         break;
260       }
261     }
262
263     setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
264   }
265
266   if (theCtx->HasTextureBaseLevel()
267    && (theSampler == NULL || !theSampler->isValidSampler()))
268   {
269     const Standard_Integer aMaxLevel = Min (theMaxMipLevels, theParams->MaxLevel());
270     setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_BASE_LEVEL, theParams->BaseLevel());
271     setParameter (theCtx, theSampler, theTarget, GL_TEXTURE_MAX_LEVEL,  aMaxLevel);
272   }
273 }
274
275 // =======================================================================
276 // function : applyGlobalTextureParams
277 // purpose  :
278 // =======================================================================
279 void OpenGl_Sampler::applyGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
280                                                const OpenGl_Texture& theTexture,
281                                                const Handle(Graphic3d_TextureParams)& theParams)
282 {
283 #if defined(GL_ES_VERSION_2_0)
284   (void )theCtx;
285   (void )theTexture;
286   (void )theParams;
287 #else
288   if (theCtx->core11 == NULL
289    || theParams->TextureUnit() >= theCtx->MaxTextureUnitsFFP())
290   {
291     return;
292   }
293
294   GLint anEnvMode = GL_MODULATE; // lighting mode
295   if (!theParams->IsModulate())
296   {
297     anEnvMode = GL_DECAL;
298     if (theTexture.GetFormat() == GL_ALPHA
299      || theTexture.GetFormat() == GL_LUMINANCE)
300     {
301       anEnvMode = GL_REPLACE;
302     }
303   }
304
305   // setup generation of texture coordinates
306   switch (theParams->GenMode())
307   {
308     case Graphic3d_TOTM_OBJECT:
309     {
310       theCtx->core11->glTexGeni  (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
311       theCtx->core11->glTexGenfv (GL_S, GL_OBJECT_PLANE,     theParams->GenPlaneS().GetData());
312       if (theTexture.GetTarget() != GL_TEXTURE_1D)
313       {
314         theCtx->core11->glTexGeni  (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
315         theCtx->core11->glTexGenfv (GL_T, GL_OBJECT_PLANE,     theParams->GenPlaneT().GetData());
316       }
317       break;
318     }
319     case Graphic3d_TOTM_SPHERE:
320     {
321       theCtx->core11->glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
322       if (theTexture.GetTarget() != GL_TEXTURE_1D)
323       {
324         theCtx->core11->glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
325       }
326       break;
327     }
328     case Graphic3d_TOTM_EYE:
329     {
330       theCtx->WorldViewState.Push();
331       theCtx->WorldViewState.SetIdentity();
332       theCtx->ApplyWorldViewMatrix();
333
334       theCtx->core11->glTexGeni  (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
335       theCtx->core11->glTexGenfv (GL_S, GL_EYE_PLANE,        theParams->GenPlaneS().GetData());
336       if (theTexture.GetTarget() != GL_TEXTURE_1D)
337       {
338         theCtx->core11->glTexGeni  (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
339         theCtx->core11->glTexGenfv (GL_T, GL_EYE_PLANE,        theParams->GenPlaneT().GetData());
340       }
341
342       theCtx->WorldViewState.Pop();
343       break;
344     }
345     case Graphic3d_TOTM_SPRITE:
346     {
347       if (theCtx->core20fwd != NULL)
348       {
349         theCtx->core11fwd->glEnable (GL_POINT_SPRITE);
350         glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
351         anEnvMode = GL_REPLACE;
352       }
353       break;
354     }
355     case Graphic3d_TOTM_MANUAL:
356     default: break;
357   }
358
359   // setup lighting
360   glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode);
361
362   switch (theTexture.GetTarget())
363   {
364     case GL_TEXTURE_1D:
365     {
366       if (theParams->GenMode() != Graphic3d_TOTM_MANUAL)
367       {
368         theCtx->core11fwd->glEnable (GL_TEXTURE_GEN_S);
369       }
370       theCtx->core11fwd->glEnable (GL_TEXTURE_1D);
371       break;
372     }
373     case GL_TEXTURE_2D:
374     {
375       if (theParams->GenMode() != Graphic3d_TOTM_MANUAL)
376       {
377         theCtx->core11fwd->glEnable (GL_TEXTURE_GEN_S);
378         theCtx->core11fwd->glEnable (GL_TEXTURE_GEN_T);
379       }
380       theCtx->core11fwd->glEnable (GL_TEXTURE_2D);
381       break;
382     }
383     default: break;
384   }
385 #endif
386 }
387
388 // =======================================================================
389 // function : resetGlobalTextureParams
390 // purpose  :
391 // =======================================================================
392 void OpenGl_Sampler::resetGlobalTextureParams (const Handle(OpenGl_Context)& theCtx,
393                                                const OpenGl_Texture& theTexture,
394                                                const Handle(Graphic3d_TextureParams)& theParams)
395 {
396 #if defined(GL_ES_VERSION_2_0)
397   (void )theCtx;
398   (void )theTexture;
399   (void )theParams;
400 #else
401   if (theCtx->core11 == NULL)
402   {
403     return;
404   }
405
406   // reset texture matrix because some code may expect it is identity
407   GLint aMatrixMode = GL_TEXTURE;
408   theCtx->core11fwd->glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
409   theCtx->core11->glMatrixMode (GL_TEXTURE);
410   theCtx->core11->glLoadIdentity();
411   theCtx->core11->glMatrixMode (aMatrixMode);
412
413   switch (theTexture.GetTarget())
414   {
415     case GL_TEXTURE_1D:
416     {
417       if (theParams->GenMode() != GL_NONE)
418       {
419         theCtx->core11fwd->glDisable (GL_TEXTURE_GEN_S);
420       }
421       theCtx->core11fwd->glDisable (GL_TEXTURE_1D);
422       break;
423     }
424     case GL_TEXTURE_2D:
425     {
426       if (theParams->GenMode() != GL_NONE)
427       {
428         theCtx->core11fwd->glDisable (GL_TEXTURE_GEN_S);
429         theCtx->core11fwd->glDisable (GL_TEXTURE_GEN_T);
430         if (theParams->GenMode() == Graphic3d_TOTM_SPRITE
431          && theCtx->core20fwd != NULL)
432         {
433           theCtx->core11fwd->glDisable (GL_POINT_SPRITE);
434         }
435       }
436       theCtx->core11fwd->glDisable (GL_TEXTURE_2D);
437       break;
438     }
439     default: break;
440   }
441 #endif
442 }