0023101: TKOpenGl possible glext header conflicts
[occt.git] / src / OpenGl / OpenGl_Context.hxx
1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #ifndef _OpenGl_Context_H__
22 #define _OpenGl_Context_H__
23
24 #include <Aspect_Handle.hxx>
25 #include <Aspect_Drawable.hxx>
26 #include <Aspect_Display.hxx>
27 #include <Aspect_RenderingContext.hxx>
28 #include <Standard_Transient.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <Handle_OpenGl_Context.hxx>
31
32 //! Forward declarations
33 struct OpenGl_GlCore12;
34 struct OpenGl_GlCore13;
35 struct OpenGl_GlCore14;
36 struct OpenGl_GlCore15;
37 struct OpenGl_GlCore20;
38 struct OpenGl_ArbVBO;
39 struct OpenGl_ExtFBO;
40
41 //! This class generalize access to the GL context and available extensions.
42 //!
43 //! Functions are grouped into structures and accessed as fields.
44 //! You should check the group for NULL before usage (if group is not NULL
45 //! then all functions are available):
46 //! @code
47 //!   if (myContext->core20 != NULL)
48 //!   {
49 //!     myGlProgram = myContext->core20->glCreateProgram();
50 //!     .. do more stuff ..
51 //!   }
52 //!   else
53 //!   {
54 //!     .. compatibility with outdated configurations ..
55 //!   }
56 //! @endcode
57 //!
58 //! Current implementation provide access to OpenGL core functionality up to 2.0 version
59 //! (core12, core13, core14, core15, fields core20).
60 //! within several extensions (arbVBO, extFBO, etc.).
61 //!
62 //! Simplified extensions classification:
63 //!  - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
64 //!  - prefixed with EXT are accepted by 2+ vendors;
65 //!  - prefixed with ARB are accepted by Architecture Review Board and are candidates
66 //!    for inclusion into GL core functionality.
67 //! Some functionality can be represented in several extensions simultaneously.
68 //! In this case developer should be careful because different specification may differ
69 //! in aspects (like enumeration values and error-handling).
70 //!
71 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
72 //! with different capabilities. Thats why OpenGl_Context should be initialized and used
73 //! for each GL context individually.
74 class OpenGl_Context : public Standard_Transient
75 {
76 public:
77
78   //! Empty constructor. You should call Init() to perform initialization with bound GL context.
79   Standard_EXPORT OpenGl_Context();
80
81   //! Destructor.
82   Standard_EXPORT virtual ~OpenGl_Context();
83
84   //! Initialize available extensions.
85   //! GL context should be active!
86   Standard_EXPORT Standard_Boolean Init();
87
88 #if (defined(_WIN32) || defined(__WIN32__))
89   Standard_EXPORT Standard_Boolean Init (const Aspect_Handle           theWindow,
90                                          const Aspect_Handle           theWindowDC,
91                                          const Aspect_RenderingContext theGContext);
92 #else
93   Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable         theWindow,
94                                          const Aspect_Display          theDisplay,
95                                          const Aspect_RenderingContext theGContext);
96 #endif
97
98   //! Check if theExtName extension is supported by active GL context.
99   Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
100
101   //! Auxiliary template to retrieve GL function pointer.
102   //! Pointer to function retrieved from library is statically casted
103   //! to requested type - there no way to check real signature of exported function.
104   //! The context should be bound before call.
105   template <typename FuncType_t>
106   Standard_Boolean FindProc (const char* theFuncName,
107                              FuncType_t& theFuncPtr)
108   {
109     theFuncPtr = (FuncType_t )findProc (theFuncName);
110     return (theFuncPtr != NULL);
111   }
112
113   //! @return true if detected GL version is greater or equal to requested one.
114   inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
115                                             const Standard_Integer theVerMinor)
116   {
117     return (myGlVerMajor >  theVerMajor)
118         || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
119   }
120
121   //! Clean up errors stack for this GL context (glGetError() in loop).
122   Standard_EXPORT void ResetErrors();
123
124   //! Activates current context.
125   //! Class should be initialized with appropriate info.
126   Standard_EXPORT Standard_Boolean MakeCurrent();
127
128   //! Return true if active mode is GL_FEEDBACK (cached state)
129   Standard_EXPORT Standard_Boolean IsFeedback() const;
130
131   //! Setup feedback mode cached state
132   Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
133
134   //! This function retrieves information from GL about free GPU memory that is:
135   //!  - OS-dependent. On some OS it is per-process and on others - for entire system.
136   //!  - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
137   //!  - Numbers meaning may vary.
138   //! You should use this info only for diagnostics purposes.
139   //! @return free GPU dedicated memory in bytes.
140   Standard_EXPORT Standard_Size AvailableMemory() const;
141
142   //! This function retrieves information from GL about GPU memory
143   //! and contains more vendor-specific values than AvailableMemory().
144   Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
145
146 private:
147
148   //! Wrapper to system function to retrieve GL function pointer by name.
149   Standard_EXPORT void* findProc (const char* theFuncName);
150
151   //! Read OpenGL version information from active context.
152   Standard_EXPORT void readGlVersion();
153
154   //! Private initialization function that should be called only once.
155   Standard_EXPORT void init();
156
157 public: // core profiles
158
159   OpenGl_GlCore12* core12;
160   OpenGl_GlCore13* core13;
161   OpenGl_GlCore14* core14;
162   OpenGl_GlCore15* core15;
163   OpenGl_GlCore20* core20;
164
165 public: // extensions
166
167   OpenGl_ArbVBO*   arbVBO; //!< GL_ARB_vertex_buffer_object
168   OpenGl_ExtFBO*   extFBO; //!< GL_EXT_framebuffer_object
169   Standard_Boolean atiMem; //!< GL_ATI_meminfo
170   Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
171
172 private:
173
174 #if (defined(_WIN32) || defined(__WIN32__))
175   Aspect_Handle           myWindow;   //!< window handle (owner of GL context) : HWND
176   Aspect_Handle           myWindowDC; //!< Device Descriptor handle : HDC
177   Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
178 #else
179   Aspect_Drawable         myWindow;   //!< window handle (owner of GL context) : GLXDrawable
180   Aspect_Display          myDisplay;  //!< connection to the X-server : Display*
181   Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
182 #endif
183
184   void*            myGlLibHandle;   //!< optional handle to GL library
185   OpenGl_GlCore20* myGlCore20;      //!< common structure for GL core functions upto 2.0
186   Standard_Integer myGlVerMajor;    //!< cached GL version major number
187   Standard_Integer myGlVerMinor;    //!< cached GL version minor number
188   Standard_Boolean myIsFeedback;    //!< flag indicates GL_FEEDBACK mode
189   Standard_Boolean myIsInitialized; //!< flag indicates initialization state
190
191 public:
192
193   DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
194
195 };
196
197 #endif // _OpenGl_Context_H__