7a8a58c24c3e6b78e7db4556b5cbcaaf11e429cc
[occt.git] / src / OpenGl / OpenGl_Context.hxx
1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-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_Context_H__
17 #define _OpenGl_Context_H__
18
19 #include <Aspect_Handle.hxx>
20 #include <Aspect_Drawable.hxx>
21 #include <Aspect_Display.hxx>
22 #include <Aspect_RenderingContext.hxx>
23 #include <Handle_OpenGl_Context.hxx>
24 #include <Handle_OpenGl_FrameBuffer.hxx>
25 #include <Handle_OpenGl_Sampler.hxx>
26 #include <Handle_OpenGl_ShaderManager.hxx>
27 #include <Handle_OpenGl_ShaderProgram.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <NCollection_Map.hxx>
30 #include <NCollection_Handle.hxx>
31 #include <NCollection_List.hxx>
32 #include <Message.hxx>
33 #include <OpenGl_Caps.hxx>
34 #include <OpenGl_Vec.hxx>
35 #include <OpenGl_Resource.hxx>
36 #include <Standard_Transient.hxx>
37 #include <TCollection_AsciiString.hxx>
38 #include <TColStd_PackedMapOfInteger.hxx>
39 #include <OpenGl_Clipping.hxx>
40 #include <OpenGl_GlCore11.hxx>
41 #include <OpenGl_Utils.hxx>
42
43 //! Forward declarations
44 #if defined(__APPLE__)
45   #import <TargetConditionals.h>
46   #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
47     #ifdef __OBJC__
48       @class EAGLContext;
49     #else
50       struct EAGLContext;
51     #endif
52   #else
53     #ifdef __OBJC__
54       @class NSOpenGLContext;
55     #else
56       struct NSOpenGLContext;
57     #endif
58   #endif
59 #endif
60
61 struct OpenGl_GlFunctions;
62 struct OpenGl_ArbTBO;
63 struct OpenGl_ArbIns;
64 struct OpenGl_ArbDbg;
65 struct OpenGl_ArbFBO;
66 struct OpenGl_ArbFBOBlit;
67 struct OpenGl_ExtGS;
68 struct OpenGl_ArbTexBindless;
69
70 template<typename theBaseClass_t> struct OpenGl_TmplCore12;
71 typedef OpenGl_TmplCore12<OpenGl_GlCore11>     OpenGl_GlCore12;
72 typedef OpenGl_TmplCore12<OpenGl_GlCore11Fwd>  OpenGl_GlCore12Fwd;
73
74 struct OpenGl_GlCore13;
75 struct OpenGl_GlCore13Fwd;
76
77 template<typename theBaseClass_t> struct OpenGl_TmplCore14;
78 typedef OpenGl_TmplCore14<OpenGl_GlCore13>     OpenGl_GlCore14;
79 typedef OpenGl_TmplCore14<OpenGl_GlCore13Fwd>  OpenGl_GlCore14Fwd;
80
81 template<typename theBaseClass_t> struct OpenGl_TmplCore15;
82 typedef OpenGl_TmplCore15<OpenGl_GlCore14>     OpenGl_GlCore15;
83 typedef OpenGl_TmplCore15<OpenGl_GlCore14Fwd>  OpenGl_GlCore15Fwd;
84
85 template<typename theBaseClass_t> struct OpenGl_TmplCore20;
86 typedef OpenGl_TmplCore20<OpenGl_GlCore15>     OpenGl_GlCore20;
87 typedef OpenGl_TmplCore20<OpenGl_GlCore15Fwd>  OpenGl_GlCore20Fwd;
88
89 template<typename theBaseClass_t> struct OpenGl_TmplCore21;
90 typedef OpenGl_TmplCore21<OpenGl_GlCore20>     OpenGl_GlCore21;
91 typedef OpenGl_TmplCore21<OpenGl_GlCore20Fwd>  OpenGl_GlCore21Fwd;
92
93 template<typename theBaseClass_t> struct OpenGl_TmplCore30;
94 typedef OpenGl_TmplCore30<OpenGl_GlCore21>     OpenGl_GlCore30;
95 typedef OpenGl_TmplCore30<OpenGl_GlCore21Fwd>  OpenGl_GlCore30Fwd;
96
97 template<typename theBaseClass_t> struct OpenGl_TmplCore31;
98 typedef OpenGl_TmplCore31<OpenGl_GlCore30>     OpenGl_GlCore31Back;
99 typedef OpenGl_TmplCore31<OpenGl_GlCore30Fwd>  OpenGl_GlCore31;
100
101 template<typename theBaseClass_t> struct OpenGl_TmplCore32;
102 typedef OpenGl_TmplCore32<OpenGl_GlCore31Back> OpenGl_GlCore32Back;
103 typedef OpenGl_TmplCore32<OpenGl_GlCore31>     OpenGl_GlCore32;
104
105 template<typename theBaseClass_t> struct OpenGl_TmplCore33;
106 typedef OpenGl_TmplCore33<OpenGl_GlCore32Back> OpenGl_GlCore33Back;
107 typedef OpenGl_TmplCore33<OpenGl_GlCore32>     OpenGl_GlCore33;
108
109 template<typename theBaseClass_t> struct OpenGl_TmplCore40;
110 typedef OpenGl_TmplCore40<OpenGl_GlCore33Back> OpenGl_GlCore40Back;
111 typedef OpenGl_TmplCore40<OpenGl_GlCore33>     OpenGl_GlCore40;
112
113 template<typename theBaseClass_t> struct OpenGl_TmplCore41;
114 typedef OpenGl_TmplCore41<OpenGl_GlCore40Back> OpenGl_GlCore41Back;
115 typedef OpenGl_TmplCore41<OpenGl_GlCore40>     OpenGl_GlCore41;
116
117 template<typename theBaseClass_t> struct OpenGl_TmplCore42;
118 typedef OpenGl_TmplCore42<OpenGl_GlCore41Back> OpenGl_GlCore42Back;
119 typedef OpenGl_TmplCore42<OpenGl_GlCore41>     OpenGl_GlCore42;
120
121 template<typename theBaseClass_t> struct OpenGl_TmplCore43;
122 typedef OpenGl_TmplCore43<OpenGl_GlCore42Back> OpenGl_GlCore43Back;
123 typedef OpenGl_TmplCore43<OpenGl_GlCore42>     OpenGl_GlCore43;
124
125 template<typename theBaseClass_t> struct OpenGl_TmplCore44;
126 typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
127 typedef OpenGl_TmplCore44<OpenGl_GlCore43>     OpenGl_GlCore44;
128
129 //! This class generalize access to the GL context and available extensions.
130 //!
131 //! Functions related to specific OpenGL version or extension are grouped into structures which can be accessed as fields of this class.
132 //! The most simple way to check that required functionality is available - is NULL check for the group:
133 //! @code
134 //!   if (myContext->core20 != NULL)
135 //!   {
136 //!     myGlProgram = myContext->core20->glCreateProgram();
137 //!     .. do more stuff ..
138 //!   }
139 //!   else
140 //!   {
141 //!     .. compatibility with outdated configurations ..
142 //!   }
143 //! @endcode
144 //!
145 //! Current implementation provide access to OpenGL core functionality up to 4.4 version (core12, core13, core14, core15, fields core20)
146 //! as well as several extensions (arbTBO, arbFBO, etc.).
147 //!
148 //! OpenGL context might be initialized in Core Profile. In this case deprecated functionality become unavailable.
149 //! To make code easily adaptable to wide range of OpenGL versions, function sets related to each version has two kinds of suffixes:
150 //!  - "back" for version 3.2+.
151 //!     Represents function set for Backward-Compatible Profile.
152 //!     Function sets without this suffix represents core profile.
153 //!  - "fwd"  for version 3.0-.
154 //!     Represents non-deprecated function set of earlier OpenGL versions, which are still available within OpenGL 3.2 Core Profile.
155 //!     Function sets without this suffix represents complete list of functions related to specific OpenGL version.
156 //!
157 //! To select which core** function set should be used in specific case:
158 //!  - Determine the minimal OpenGL version required for implemented functionality and use it to access all functions.
159 //!    For example, if algorithm requires OpenGL 2.1+, it is better to write core20fwd->glEnable() rather than core11fwd->glEnable() for uniformity.
160 //!  - If functionality will work within Core Profile, use function sets with appropriate suffix.
161 //!  - Validate minimal requirements at initialization/creation time and omit checks within code where algorithm should be already initialized.
162 //!    Properly escape code incompatible with Core Profile. The simplest way to check Core Profile is "if (core11 == NULL)".
163 //!
164 //! Simplified extensions classification:
165 //!  - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
166 //!  - prefixed with EXT are accepted by 2+ vendors;
167 //!  - prefixed with ARB are accepted by Architecture Review Board and are candidates
168 //!    for inclusion into GL core functionality.
169 //! Some functionality can be represented in several extensions simultaneously.
170 //! In this case developer should be careful because different specification may differ
171 //! in aspects (like enumeration values and error-handling).
172 //!
173 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts with different capabilities.
174 //! For this reason OpenGl_Context should be initialized and used for each GL context independently.
175 class OpenGl_Context : public Standard_Transient
176 {
177 public:
178
179   //! Function for getting power of to number larger or equal to input number.
180   //! @param theNumber    number to 'power of two'
181   //! @param theThreshold upper threshold
182   //! @return power of two number
183   inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
184                                                 const Standard_Integer theThreshold)
185   {
186     for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
187     {
188       if (theNumber <= p2)
189       {
190         return p2;
191       }
192     }
193     return theThreshold;
194   }
195
196 public:
197
198   //! Empty constructor. You should call Init() to perform initialization with bound GL context.
199   Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
200
201   //! Destructor.
202   Standard_EXPORT virtual ~OpenGl_Context();
203
204   //! Release all resources, including shared ones
205   Standard_EXPORT void forcedRelease();
206
207   //! Share GL context resources.
208   //! theShareCtx - handle to context to retrieve handles to shared resources.
209   Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
210
211   //! Initialize class from currently bound OpenGL context. Method should be called only once.
212   //! @return false if no GL context is bound to the current thread
213   Standard_EXPORT Standard_Boolean Init (const Standard_Boolean theIsCoreProfile = Standard_False);
214
215   //! @return true if this context is valid (has been initialized)
216   inline Standard_Boolean IsValid() const
217   {
218     return myIsInitialized;
219   }
220
221 #if defined(HAVE_EGL)
222   //! Initialize class from specified surface and rendering context. Method should be called only once.
223   //! @return false if OpenGL context can not be bound to specified surface
224   Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable         theEglSurface,
225                                          const Aspect_Display          theEglDisplay,
226                                          const Aspect_RenderingContext theEglContext,
227                                          const Standard_Boolean        theIsCoreProfile = Standard_False);
228 #elif defined(_WIN32)
229   //! Initialize class from specified window and rendering context. Method should be called only once.
230   //! @return false if OpenGL context can not be bound to specified window
231   Standard_EXPORT Standard_Boolean Init (const Aspect_Handle           theWindow,
232                                          const Aspect_Handle           theWindowDC,
233                                          const Aspect_RenderingContext theGContext,
234                                          const Standard_Boolean        theIsCoreProfile = Standard_False);
235
236   //! @return the window handle (HWND) currently bound to this OpenGL context
237   inline Aspect_Handle Window() const
238   {
239     return myWindow;
240   }
241
242 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
243   #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
244
245   //! Initialize class from specified OpenGL ES context (EAGLContext). Method should be called only once.
246   Standard_EXPORT Standard_Boolean Init (EAGLContext*                  theGContext,
247                                          const Standard_Boolean        theIsCoreProfile = Standard_False);
248   #else
249
250   //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
251   Standard_EXPORT Standard_Boolean Init (NSOpenGLContext*              theGContext,
252                                          const Standard_Boolean        theIsCoreProfile = Standard_False);
253   #endif
254 #else
255
256   //! Initialize class from specified window and rendering context. Method should be called only once.
257   //! @return false if OpenGL context can not be bound to specified window
258   Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable         theWindow,
259                                          const Aspect_Display          theDisplay,
260                                          const Aspect_RenderingContext theGContext,
261                                          const Standard_Boolean        theIsCoreProfile = Standard_False);
262
263   //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
264   inline Aspect_Drawable Window() const
265   {
266     return myWindow;
267   }
268 #endif
269
270   //! Read OpenGL version information from active context.
271   Standard_EXPORT static void ReadGlVersion (Standard_Integer& theGlVerMajor,
272                                              Standard_Integer& theGlVerMinor);
273
274   //! Check if theExtName extension is supported by active GL context.
275   Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
276
277   //! Check if theExtName extension is in extensions string.
278   Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
279                                                           const char* theExtName);
280
281   //! Auxiliary template to retrieve GL function pointer.
282   //! Pointer to function retrieved from library is statically casted
283   //! to requested type - there no way to check real signature of exported function.
284   //! The context should be bound before call.
285   template <typename FuncType_t>
286   Standard_Boolean FindProc (const char* theFuncName,
287                              FuncType_t& theFuncPtr)
288   {
289     theFuncPtr = (FuncType_t )findProc (theFuncName);
290     return (theFuncPtr != NULL);
291   }
292
293   //! @return true if detected GL version is greater or equal to requested one.
294   inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
295                                             const Standard_Integer theVerMinor) const
296   {
297     return (myGlVerMajor >  theVerMajor)
298         || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
299   }
300
301   //! Clean up errors stack for this GL context (glGetError() in loop).
302   Standard_EXPORT void ResetErrors();
303
304   //! This method uses system-dependent API to retrieve information
305   //! about GL context bound to the current thread.
306   //! @return true if current thread is bound to this GL context
307   Standard_EXPORT Standard_Boolean IsCurrent() const;
308
309   //! Activates current context.
310   //! Class should be initialized with appropriate info.
311   Standard_EXPORT Standard_Boolean MakeCurrent();
312
313   //! Swap front/back buffers for this GL context (should be activated before!).
314   Standard_EXPORT void SwapBuffers();
315
316   //! Return true if active mode is GL_RENDER (cached state)
317   Standard_Boolean IsRender() const
318   {
319   #if !defined(GL_ES_VERSION_2_0)
320     return myRenderMode == GL_RENDER;
321   #else
322     return Standard_True;
323   #endif
324   }
325
326   //! Return true if active mode is GL_FEEDBACK (cached state)
327   Standard_Boolean IsFeedback() const
328   {
329   #if !defined(GL_ES_VERSION_2_0)
330     return myRenderMode == GL_FEEDBACK;
331   #else
332     return Standard_False;
333   #endif
334   }
335
336   //! This function retrieves information from GL about free GPU memory that is:
337   //!  - OS-dependent. On some OS it is per-process and on others - for entire system.
338   //!  - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
339   //!  - Numbers meaning may vary.
340   //! You should use this info only for diagnostics purposes.
341   //! @return free GPU dedicated memory in bytes.
342   Standard_EXPORT Standard_Size AvailableMemory() const;
343
344   //! This function retrieves information from GL about GPU memory
345   //! and contains more vendor-specific values than AvailableMemory().
346   Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
347
348   //! Access shared resource by its name.
349   //! @param  theKey - unique identifier;
350   //! @return handle to shared resource or NULL.
351   Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
352
353   //! Access shared resource by its name.
354   //! @param  theKey   - unique identifier;
355   //! @param  theValue - handle to fill;
356   //! @return true if resource was shared.
357   template<typename TheHandleType>
358   Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
359                                 TheHandleType&                 theValue) const
360   {
361     const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
362     if (aResource.IsNull())
363     {
364       return Standard_False;
365     }
366
367     theValue = TheHandleType::DownCast (aResource);
368     return !theValue.IsNull();
369   }
370
371   //! Register shared resource.
372   //! Notice that after registration caller shouldn't release it by himself -
373   //! it will be automatically released on context destruction.
374   //! @param theKey      - unique identifier, shouldn't be empty;
375   //! @param theResource - new resource to register, shouldn't be NULL.
376   Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
377                                                   const Handle(OpenGl_Resource)& theResource);
378
379   //! Release shared resource.
380   //! If there are more than one reference to this resource
381   //! (also used by some other existing object) then call will be ignored.
382   //! This means that current object itself should nullify handle before this call.
383   //! Notice that this is unrecommended operation at all and should be used
384   //! only in case of fat resources to release memory for other needs.
385   //! @param theKey     unique identifier
386   //! @param theToDelay postpone release until next redraw call
387   Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
388                                         const Standard_Boolean         theToDelay = Standard_False);
389
390   //! Append resource to queue for delayed clean up.
391   //! Resources in this queue will be released at next redraw call.
392   Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
393
394   //! Clean up the delayed release queue.
395   Standard_EXPORT void ReleaseDelayed();
396
397   //! @return tool for management of clippings within this context.
398   inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
399
400   //! @return tool for management of clippings within this context.
401   inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
402
403   //! @return tool for management of shader programs within this context.
404   inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
405
406 public:
407
408   //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
409   Standard_Integer TextureWrapClamp() const { return myTexClamp; }
410
411   //! @return maximum degree of anisotropy texture filter
412   Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
413
414   //! @return value for GL_MAX_TEXTURE_SIZE
415   Standard_EXPORT Standard_Integer MaxTextureSize() const;
416
417   //! Get maximum number of clip planes supported by OpenGl.
418   //! This value is implementation dependent. At least 6
419   //! planes should be supported by OpenGl (see specs).
420   //! @return value for GL_MAX_CLIP_PLANES
421   Standard_EXPORT Standard_Integer MaxClipPlanes() const;
422
423   //! Returns true if VBO is supported and permitted.
424   inline bool ToUseVbo() const
425   {
426     return core15fwd != NULL
427        && !caps->vboDisable;
428   }
429
430   //! @return cached state of GL_NORMALIZE.
431   Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
432
433   //! Sets GL_NORMALIZE enabled or disabled.
434   //! @return old value of the flag
435   Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
436
437   //! Applies matrix stored in ModelWorldState to OpenGl.
438   void ApplyModelWorldMatrix();
439
440   //! Applies matrix stored in WorldViewState to OpenGl.
441   void ApplyWorldViewMatrix();
442
443   //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
444   void ApplyModelViewMatrix();
445
446   //! Applies matrix stored in ProjectionState to OpenGl.
447   void ApplyProjectionMatrix();
448
449 public:
450
451   //! @return messenger instance
452   inline const Handle(Message_Messenger)& Messenger() const
453   {
454     return ::Message::DefaultMessenger();
455   }
456
457   //! Callback for GL_ARB_debug_output extension
458   //! @param theSource   message source   within GL_DEBUG_SOURCE_   enumeration
459   //! @param theType     message type     within GL_DEBUG_TYPE_     enumeration
460   //! @param theId       message ID       within source
461   //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
462   //! @param theMessage  the message itself
463   Standard_EXPORT void PushMessage (const unsigned int theSource,
464                                     const unsigned int theType,
465                                     const unsigned int theId,
466                                     const unsigned int theSeverity,
467                                     const TCollection_ExtendedString& theMessage);
468
469   //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
470   Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
471                                                    const unsigned int theId);
472
473   //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
474   Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
475                                                    const unsigned int theId);
476
477   //! @return true if OpenGl context supports left and
478   //! right rendering buffers.
479   Standard_Boolean HasStereoBuffers() const
480   {
481   #if !defined(GL_ES_VERSION_2_0)
482     return myIsStereoBuffers;
483   #else
484     return Standard_False;
485   #endif
486   }
487
488 public: //! @name methods to alter or retrieve current state
489
490   //! Switch to left stereographic rendering buffer.
491   //! This method can be used to keep unchanged choise
492   //! of front/back/both buffer rendering.
493   Standard_EXPORT void SetDrawBufferLeft();
494
495   //! Switch to right stereographic rendering buffer.
496   //! This method can be used to keep unchanged choise
497   //! of front/back/both buffer rendering.
498   Standard_EXPORT void SetDrawBufferRight();
499
500   //! Switch to non-stereographic rendering buffer.
501   //! This method can be used to keep unchanged choise
502   //! of front/back/both buffer rendering.
503   Standard_EXPORT void SetDrawBufferMono();
504
505   //! Fetch OpenGl context state. This class tracks value of several OpenGl
506   //! state variables. Consulting the cached values is quicker than
507   //! doing the same via OpenGl API. Call this method if any of the controlled
508   //! OpenGl state variables has a possibility of being out-of-date.
509   Standard_EXPORT void FetchState();
510
511   //! @return active GLSL program
512   const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
513   {
514     return myActiveProgram;
515   }
516
517   //! @return OpenGL sampler object used to override default texture parameters
518   const Handle(OpenGl_Sampler)& TextureSampler()
519   {
520     return myTexSampler;
521   }
522
523   //! Bind specified program to current context,
524   //! or unbind previous one when NULL specified.
525   //! @return true if some program is bound to context
526   Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
527
528   //! Setup current color.
529   Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
530
531   //! Setup point size.
532   Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
533
534   //! Bind default Vertex Array Object
535   Standard_EXPORT void BindDefaultVao();
536
537   //! Default Frame Buffer Object.
538   const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
539   {
540     return myDefaultFbo;
541   }
542
543   //! Setup new Default Frame Buffer Object and return previously set.
544   //! This call doesn't change Active FBO!
545   Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
546
547   //! Return debug context initialization state.
548   Standard_Boolean IsDebugContext() const
549   {
550     return myIsGlDebugCtx;
551   }
552
553 private:
554
555   //! Wrapper to system function to retrieve GL function pointer by name.
556   Standard_EXPORT void* findProc (const char* theFuncName);
557
558   //! Print error if not all functions have been exported by context for reported version.
559   //! Note that this will never happen when using GLX, since returned functions can not be validated.
560   //! @param theGlVerMajor the OpenGL major version with missing functions
561   //! @param theGlVerMinor the OpenGL minor version with missing functions
562   Standard_EXPORT void checkWrongVersion (const Standard_Integer theGlVerMajor,
563                                           const Standard_Integer theGlVerMinor);
564
565   //! Private initialization function that should be called only once.
566   Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
567
568 public: //! @name core profiles
569
570   OpenGl_GlCore11*     core11;     //!< OpenGL 1.1 core functionality
571   OpenGl_GlCore11Fwd*  core11fwd;  //!< OpenGL 1.1 without deprecated entry points
572   OpenGl_GlCore15*     core15;     //!< OpenGL 1.5 core functionality
573   OpenGl_GlCore15Fwd*  core15fwd;  //!< OpenGL 1.5 without deprecated entry points
574   OpenGl_GlCore20*     core20;     //!< OpenGL 2.0 core functionality (includes 1.5)
575   OpenGl_GlCore20Fwd*  core20fwd;  //!< OpenGL 2.0 without deprecated entry points
576   OpenGl_GlCore32*     core32;     //!< OpenGL 3.2 core profile
577   OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
578   OpenGl_GlCore33*     core33;     //!< OpenGL 3.3 core profile
579   OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
580   OpenGl_GlCore41*     core41;     //!< OpenGL 4.1 core profile
581   OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
582   OpenGl_GlCore42*     core42;     //!< OpenGL 4.2 core profile
583   OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
584   OpenGl_GlCore43*     core43;     //!< OpenGL 4.3 core profile
585   OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
586   OpenGl_GlCore44*     core44;     //!< OpenGL 4.4 core profile
587   OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
588
589   Handle(OpenGl_Caps) caps; //!< context options
590
591 public: //! @name extensions
592
593   Standard_Boolean       hasHighp;       //!< highp in GLSL ES fragment shader is supported
594   Standard_Boolean       hasTexRGBA8;    //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
595   Standard_Boolean       arbNPTW;        //!< GL_ARB_texture_non_power_of_two
596   Standard_Boolean       arbTexRG;       //!< GL_ARB_texture_rg
597   OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
598   OpenGl_ArbTBO*         arbTBO;         //!< GL_ARB_texture_buffer_object
599   Standard_Boolean       arbTboRGB32;    //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0
600   OpenGl_ArbIns*         arbIns;         //!< GL_ARB_draw_instanced
601   OpenGl_ArbDbg*         arbDbg;         //!< GL_ARB_debug_output
602   OpenGl_ArbFBO*         arbFBO;         //!< GL_ARB_framebuffer_object
603   OpenGl_ArbFBOBlit*     arbFBOBlit;     //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
604   OpenGl_ExtGS*          extGS;          //!< GL_EXT_geometry_shader4
605   Standard_Boolean       extBgra;        //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
606   Standard_Boolean       extAnis;        //!< GL_EXT_texture_filter_anisotropic
607   Standard_Boolean       extPDS;         //!< GL_EXT_packed_depth_stencil
608   Standard_Boolean       atiMem;         //!< GL_ATI_meminfo
609   Standard_Boolean       nvxMem;         //!< GL_NVX_gpu_memory_info
610
611 private: // system-dependent fields
612
613 #if defined(HAVE_EGL)
614   Aspect_Drawable         myWindow;   //!< EGL surface                   : EGLSurface
615   Aspect_Display          myDisplay;  //!< EGL connection to the Display : EGLDisplay
616   Aspect_RenderingContext myGContext; //!< EGL rendering context         : EGLContext
617 #elif defined(_WIN32)
618   Aspect_Handle           myWindow;   //!< window handle (owner of GL context) : HWND
619   Aspect_Handle           myWindowDC; //!< Device Descriptor handle : HDC
620   Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
621 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
622   #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
623   EAGLContext*            myGContext;    //!< Rendering Context handle
624   #else
625   NSOpenGLContext*        myGContext; //!< Rendering Context handle
626   #endif
627 #else
628   Aspect_Drawable         myWindow;   //!< window handle (owner of GL context) : GLXDrawable
629   Aspect_Display          myDisplay;  //!< connection to the X-server : Display*
630   Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
631 #endif
632
633 private: // context info
634
635   typedef NCollection_DataMap<TCollection_AsciiString, Standard_Integer> OpenGl_DelayReleaseMap;
636   typedef NCollection_Handle<OpenGl_DelayReleaseMap> Handle(OpenGl_DelayReleaseMap);
637   typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
638   typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
639   typedef NCollection_List<Handle(OpenGl_Resource)> OpenGl_ResourcesStack;
640   typedef NCollection_Handle<OpenGl_ResourcesStack> Handle(OpenGl_ResourcesStack);
641
642   Handle(OpenGl_ResourcesMap)    mySharedResources; //!< shared resources with unique identification key
643   Handle(OpenGl_DelayReleaseMap) myDelayed;         //!< shared resources for delayed release
644   Handle(OpenGl_ResourcesStack)  myUnusedResources; //!< stack of resources for delayed clean up
645
646   OpenGl_Clipping myClippingState; //!< state of clip planes
647
648   void*            myGlLibHandle;          //!< optional handle to GL library
649   NCollection_Handle<OpenGl_GlFunctions>
650                    myFuncs;                //!< mega structure for all GL functions
651   Standard_Integer myAnisoMax;             //!< maximum level of anisotropy texture filter
652   Standard_Integer myTexClamp;             //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
653   Standard_Integer myMaxTexDim;            //!< value for GL_MAX_TEXTURE_SIZE
654   Standard_Integer myMaxClipPlanes;        //!< value for GL_MAX_CLIP_PLANES
655   Standard_Integer myGlVerMajor;           //!< cached GL version major number
656   Standard_Integer myGlVerMinor;           //!< cached GL version minor number
657   Standard_Boolean myIsInitialized;        //!< flag indicates initialization state
658   Standard_Boolean myIsStereoBuffers;      //!< context supports stereo buffering
659   Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
660                                            //!< Used to tell OpenGl that normals should be normalized
661
662   Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
663
664 private: //! @name fields tracking current state
665
666   Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
667   Handle(OpenGl_Sampler)       myTexSampler;    //!< currently active sampler object
668   Handle(OpenGl_FrameBuffer)   myDefaultFbo;    //!< default Frame Buffer Object
669   Standard_Integer             myRenderMode;    //!< value for active rendering mode
670   Standard_Integer             myDrawBuffer;    //!< current draw buffer
671   unsigned int                 myDefaultVao;    //!< default Vertex Array Object
672   Standard_Boolean             myIsGlDebugCtx;  //!< debug context initialization state
673   TCollection_AsciiString      myVendor;        //!< Graphics Driver's vendor
674   TColStd_PackedMapOfInteger   myFilters[6];    //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
675
676 public:
677
678   OpenGl_Utils::MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
679   OpenGl_Utils::MatrixState<Standard_ShortReal> WorldViewState;  //!< state of orientation matrix
680   OpenGl_Utils::MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection  matrix
681
682 private:
683
684   //! Copying allowed only within Handles
685   OpenGl_Context            (const OpenGl_Context& );
686   OpenGl_Context& operator= (const OpenGl_Context& );
687
688 public:
689
690   DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
691
692   friend class OpenGl_Window;
693
694 };
695
696 #endif // _OpenGl_Context_H__