a35b36ee86cf7dcef11d0eda5423905b197c05cc
[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 #ifndef _OpenGl_Context_H__
21 #define _OpenGl_Context_H__
22
23 #include <Aspect_Handle.hxx>
24 #include <Aspect_Drawable.hxx>
25 #include <Aspect_Display.hxx>
26 #include <Aspect_RenderingContext.hxx>
27 #include <Handle_OpenGl_Context.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <NCollection_Handle.hxx>
30 #include <NCollection_Queue.hxx>
31 #include <OpenGl_Resource.hxx>
32 #include <Standard_Transient.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <Handle_OpenGl_Context.hxx>
35
36 //! Forward declarations
37 struct OpenGl_GlCore12;
38 struct OpenGl_GlCore13;
39 struct OpenGl_GlCore14;
40 struct OpenGl_GlCore15;
41 struct OpenGl_GlCore20;
42 struct OpenGl_ArbVBO;
43 struct OpenGl_ArbTBO;
44 struct OpenGl_ArbIns;
45 struct OpenGl_ExtFBO;
46 struct OpenGl_ExtGS;
47
48 //! This class generalize access to the GL context and available extensions.
49 //!
50 //! Functions are grouped into structures and accessed as fields.
51 //! You should check the group for NULL before usage (if group is not NULL
52 //! then all functions are available):
53 //! @code
54 //!   if (myContext->core20 != NULL)
55 //!   {
56 //!     myGlProgram = myContext->core20->glCreateProgram();
57 //!     .. do more stuff ..
58 //!   }
59 //!   else
60 //!   {
61 //!     .. compatibility with outdated configurations ..
62 //!   }
63 //! @endcode
64 //!
65 //! Current implementation provide access to OpenGL core functionality up to 2.0 version
66 //! (core12, core13, core14, core15, fields core20).
67 //! within several extensions (arbVBO, extFBO, etc.).
68 //!
69 //! Simplified extensions classification:
70 //!  - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
71 //!  - prefixed with EXT are accepted by 2+ vendors;
72 //!  - prefixed with ARB are accepted by Architecture Review Board and are candidates
73 //!    for inclusion into GL core functionality.
74 //! Some functionality can be represented in several extensions simultaneously.
75 //! In this case developer should be careful because different specification may differ
76 //! in aspects (like enumeration values and error-handling).
77 //!
78 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
79 //! with different capabilities. Thats why OpenGl_Context should be initialized and used
80 //! for each GL context individually.
81 class OpenGl_Context : public Standard_Transient
82 {
83 public:
84
85   //! Function for getting power of to number larger or equal to input number.
86   //! @param theNumber    number to 'power of two'
87   //! @param theThreshold upper threshold
88   //! @return power of two number
89   inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
90                                                 const Standard_Integer theThreshold)
91   {
92     for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
93     {
94       if (theNumber <= p2)
95       {
96         return p2;
97       }
98     }
99     return theThreshold;
100   }
101
102 public:
103
104   //! Empty constructor. You should call Init() to perform initialization with bound GL context.
105   Standard_EXPORT OpenGl_Context();
106
107   //! Destructor.
108   Standard_EXPORT virtual ~OpenGl_Context();
109
110   //! Share GL context resources.
111   //! theShareCtx - handle to context to retrieve handles to shared resources.
112   Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
113
114   //! Initialize available extensions.
115   //! GL context should be active!
116   Standard_EXPORT Standard_Boolean Init();
117
118 #if (defined(_WIN32) || defined(__WIN32__))
119   Standard_EXPORT Standard_Boolean Init (const Aspect_Handle           theWindow,
120                                          const Aspect_Handle           theWindowDC,
121                                          const Aspect_RenderingContext theGContext);
122 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
123   Standard_EXPORT Standard_Boolean Init (const void*                   theGContext);
124 #else
125   Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable         theWindow,
126                                          const Aspect_Display          theDisplay,
127                                          const Aspect_RenderingContext theGContext);
128 #endif
129
130   //! Check if theExtName extension is supported by active GL context.
131   Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
132
133   //! Auxiliary template to retrieve GL function pointer.
134   //! Pointer to function retrieved from library is statically casted
135   //! to requested type - there no way to check real signature of exported function.
136   //! The context should be bound before call.
137   template <typename FuncType_t>
138   Standard_Boolean FindProc (const char* theFuncName,
139                              FuncType_t& theFuncPtr)
140   {
141     theFuncPtr = (FuncType_t )findProc (theFuncName);
142     return (theFuncPtr != NULL);
143   }
144
145   //! @return true if detected GL version is greater or equal to requested one.
146   inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
147                                             const Standard_Integer theVerMinor)
148   {
149     return (myGlVerMajor >  theVerMajor)
150         || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
151   }
152
153   //! Clean up errors stack for this GL context (glGetError() in loop).
154   Standard_EXPORT void ResetErrors();
155
156   //! This method uses system-dependent API to retrieve information
157   //! about GL context bound to the current thread.
158   //! @return true if current thread is bound to this GL context
159   Standard_EXPORT Standard_Boolean IsCurrent() const;
160
161   //! Activates current context.
162   //! Class should be initialized with appropriate info.
163   Standard_EXPORT Standard_Boolean MakeCurrent();
164
165   //! Swap front/back buffers for this GL context (should be activated before!).
166   Standard_EXPORT void SwapBuffers();
167
168   //! Return true if active mode is GL_FEEDBACK (cached state)
169   Standard_EXPORT Standard_Boolean IsFeedback() const;
170
171   //! Setup feedback mode cached state
172   Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
173
174   //! This function retrieves information from GL about free GPU memory that is:
175   //!  - OS-dependent. On some OS it is per-process and on others - for entire system.
176   //!  - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
177   //!  - Numbers meaning may vary.
178   //! You should use this info only for diagnostics purposes.
179   //! @return free GPU dedicated memory in bytes.
180   Standard_EXPORT Standard_Size AvailableMemory() const;
181
182   //! This function retrieves information from GL about GPU memory
183   //! and contains more vendor-specific values than AvailableMemory().
184   Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
185
186   //! Access shared resource by its name.
187   //! @param  theKey - unique identifier;
188   //! @return handle to shared resource or NULL.
189   Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
190
191   //! Access shared resource by its name.
192   //! @param  theKey   - unique identifier;
193   //! @param  theValue - handle to fill;
194   //! @return true if resource was shared.
195   template<typename TheHandleType>
196   Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
197                                 TheHandleType&                 theValue) const
198   {
199     const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
200     if (aResource.IsNull())
201     {
202       return Standard_False;
203     }
204
205     theValue = TheHandleType::DownCast (aResource);
206     return !theValue.IsNull();
207   }
208
209   //! Register shared resource.
210   //! Notice that after registration caller shouldn't release it by himself -
211   //! it will be automatically released on context destruction.
212   //! @param theKey      - unique identifier, shouldn't be empty;
213   //! @param theResource - new resource to register, shouldn't be NULL.
214   Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
215                                                   const Handle(OpenGl_Resource)& theResource);
216
217   //! Release shared resource.
218   //! If there are more than one reference to this resource
219   //! (also used by some other existing object) then call will be ignored.
220   //! This means that current object itself should nullify handle before this call.
221   //! Notice that this is unrecommended operation at all and should be used
222   //! only in case of fat resources to release memory for other needs.
223   //! @param theKey     unique identifier
224   //! @param theToDelay postpone release until next redraw call
225   Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
226                                         const Standard_Boolean         theToDelay = Standard_False);
227
228   //! Append resource to queue for delayed clean up.
229   //! Resources in this queue will be released at next redraw call.
230   Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
231
232   //! Clean up the delayed release queue.
233   Standard_EXPORT void ReleaseDelayed();
234
235   //! @return maximum degree of anisotropy texture filter
236   Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
237
238   //! @return value for GL_MAX_TEXTURE_SIZE
239   Standard_EXPORT Standard_Integer MaxTextureSize() const;
240
241 private:
242
243   //! Wrapper to system function to retrieve GL function pointer by name.
244   Standard_EXPORT void* findProc (const char* theFuncName);
245
246   //! Read OpenGL version information from active context.
247   Standard_EXPORT void readGlVersion();
248
249   //! Private initialization function that should be called only once.
250   Standard_EXPORT void init();
251
252 public: // core profiles
253
254   OpenGl_GlCore12* core12;
255   OpenGl_GlCore13* core13;
256   OpenGl_GlCore14* core14;
257   OpenGl_GlCore15* core15;
258   OpenGl_GlCore20* core20;
259
260 public: // extensions
261
262   Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
263   OpenGl_ArbVBO*   arbVBO;  //!< GL_ARB_vertex_buffer_object
264   OpenGl_ArbTBO*   arbTBO;  //!< GL_ARB_texture_buffer_object
265   OpenGl_ArbIns*   arbIns;  //!< GL_ARB_draw_instanced
266   OpenGl_ExtFBO*   extFBO;  //!< GL_EXT_framebuffer_object
267   OpenGl_ExtGS*    extGS;   //!< GL_EXT_geometry_shader4
268   Standard_Boolean extBgra; //!< GL_EXT_bgra
269   Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
270   Standard_Boolean atiMem;  //!< GL_ATI_meminfo
271   Standard_Boolean nvxMem;  //!< GL_NVX_gpu_memory_info
272
273 private: // system-dependent fields
274
275 #if (defined(_WIN32) || defined(__WIN32__))
276   Aspect_Handle           myWindow;   //!< window handle (owner of GL context) : HWND
277   Aspect_Handle           myWindowDC; //!< Device Descriptor handle : HDC
278   Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
279 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
280   void*                   myGContext; //!< Rendering Context handle : NSOpenGLContext
281 #else
282   Aspect_Drawable         myWindow;   //!< window handle (owner of GL context) : GLXDrawable
283   Aspect_Display          myDisplay;  //!< connection to the X-server : Display*
284   Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
285 #endif
286
287 private: // context info
288
289   typedef NCollection_DataMap<TCollection_AsciiString, Standard_Integer> OpenGl_DelayReleaseMap;
290   typedef NCollection_Handle<OpenGl_DelayReleaseMap> Handle(OpenGl_DelayReleaseMap);
291   typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
292   typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
293   typedef NCollection_Queue<Handle(OpenGl_Resource)> OpenGl_ResourcesQueue;
294   typedef NCollection_Handle<OpenGl_ResourcesQueue> Handle(OpenGl_ResourcesQueue);
295
296   Handle(OpenGl_ResourcesMap)    mySharedResources; //!< shared resources with unique identification key
297   Handle(OpenGl_DelayReleaseMap) myDelayed;         //!< shared resources for delayed release
298   Handle(OpenGl_ResourcesQueue)  myReleaseQueue;    //!< queue of resources for delayed clean up
299
300   void*            myGlLibHandle;   //!< optional handle to GL library
301   OpenGl_GlCore20* myGlCore20;      //!< common structure for GL core functions upto 2.0
302   Standard_Integer myAnisoMax;      //!< maximum level of anisotropy texture filter
303   Standard_Integer myMaxTexDim;     //!< value for GL_MAX_TEXTURE_SIZE
304   Standard_Integer myGlVerMajor;    //!< cached GL version major number
305   Standard_Integer myGlVerMinor;    //!< cached GL version minor number
306   Standard_Boolean myIsFeedback;    //!< flag indicates GL_FEEDBACK mode
307   Standard_Boolean myIsInitialized; //!< flag indicates initialization state
308
309 private:
310
311   //! Copying allowed only within Handles
312   OpenGl_Context            (const OpenGl_Context& );
313   OpenGl_Context& operator= (const OpenGl_Context& );
314
315 public:
316
317   DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
318
319   friend class OpenGl_Window;
320
321 };
322
323 #endif // _OpenGl_Context_H__