1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_Context_H__
17 #define _OpenGl_Context_H__
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_ShaderManager.hxx>
25 #include <Handle_OpenGl_ShaderProgram.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <NCollection_Map.hxx>
28 #include <NCollection_Handle.hxx>
29 #include <NCollection_List.hxx>
30 #include <Message.hxx>
31 #include <OpenGl_Caps.hxx>
32 #include <OpenGl_Resource.hxx>
33 #include <Standard_Transient.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <Handle_OpenGl_Context.hxx>
36 #include <OpenGl_Clipping.hxx>
37 #include <OpenGl_GlCore11.hxx>
39 //! Forward declarations
40 struct OpenGl_GlFunctions;
47 template<typename theBaseClass_t> struct OpenGl_TmplCore12;
48 typedef OpenGl_TmplCore12<OpenGl_GlCore11> OpenGl_GlCore12;
49 typedef OpenGl_TmplCore12<OpenGl_GlCore11Fwd> OpenGl_GlCore12Fwd;
51 struct OpenGl_GlCore13;
52 struct OpenGl_GlCore13Fwd;
54 template<typename theBaseClass_t> struct OpenGl_TmplCore14;
55 typedef OpenGl_TmplCore14<OpenGl_GlCore13> OpenGl_GlCore14;
56 typedef OpenGl_TmplCore14<OpenGl_GlCore13Fwd> OpenGl_GlCore14Fwd;
58 template<typename theBaseClass_t> struct OpenGl_TmplCore15;
59 typedef OpenGl_TmplCore15<OpenGl_GlCore14> OpenGl_GlCore15;
60 typedef OpenGl_TmplCore15<OpenGl_GlCore14Fwd> OpenGl_GlCore15Fwd;
62 template<typename theBaseClass_t> struct OpenGl_TmplCore20;
63 typedef OpenGl_TmplCore20<OpenGl_GlCore15> OpenGl_GlCore20;
64 typedef OpenGl_TmplCore20<OpenGl_GlCore15Fwd> OpenGl_GlCore20Fwd;
66 template<typename theBaseClass_t> struct OpenGl_TmplCore21;
67 typedef OpenGl_TmplCore21<OpenGl_GlCore20> OpenGl_GlCore21;
68 typedef OpenGl_TmplCore21<OpenGl_GlCore20Fwd> OpenGl_GlCore21Fwd;
70 template<typename theBaseClass_t> struct OpenGl_TmplCore30;
71 typedef OpenGl_TmplCore30<OpenGl_GlCore21> OpenGl_GlCore30;
72 typedef OpenGl_TmplCore30<OpenGl_GlCore21Fwd> OpenGl_GlCore30Fwd;
74 template<typename theBaseClass_t> struct OpenGl_TmplCore31;
75 typedef OpenGl_TmplCore31<OpenGl_GlCore30> OpenGl_GlCore31Back;
76 typedef OpenGl_TmplCore31<OpenGl_GlCore30Fwd> OpenGl_GlCore31;
78 template<typename theBaseClass_t> struct OpenGl_TmplCore32;
79 typedef OpenGl_TmplCore32<OpenGl_GlCore31Back> OpenGl_GlCore32Back;
80 typedef OpenGl_TmplCore32<OpenGl_GlCore31> OpenGl_GlCore32;
82 template<typename theBaseClass_t> struct OpenGl_TmplCore33;
83 typedef OpenGl_TmplCore33<OpenGl_GlCore32Back> OpenGl_GlCore33Back;
84 typedef OpenGl_TmplCore33<OpenGl_GlCore32> OpenGl_GlCore33;
86 template<typename theBaseClass_t> struct OpenGl_TmplCore40;
87 typedef OpenGl_TmplCore40<OpenGl_GlCore33Back> OpenGl_GlCore40Back;
88 typedef OpenGl_TmplCore40<OpenGl_GlCore33> OpenGl_GlCore40;
90 template<typename theBaseClass_t> struct OpenGl_TmplCore41;
91 typedef OpenGl_TmplCore41<OpenGl_GlCore40Back> OpenGl_GlCore41Back;
92 typedef OpenGl_TmplCore41<OpenGl_GlCore40> OpenGl_GlCore41;
94 template<typename theBaseClass_t> struct OpenGl_TmplCore42;
95 typedef OpenGl_TmplCore42<OpenGl_GlCore41Back> OpenGl_GlCore42Back;
96 typedef OpenGl_TmplCore42<OpenGl_GlCore41> OpenGl_GlCore42;
98 template<typename theBaseClass_t> struct OpenGl_TmplCore43;
99 typedef OpenGl_TmplCore43<OpenGl_GlCore42Back> OpenGl_GlCore43Back;
100 typedef OpenGl_TmplCore43<OpenGl_GlCore42> OpenGl_GlCore43;
102 template<typename theBaseClass_t> struct OpenGl_TmplCore44;
103 typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
104 typedef OpenGl_TmplCore44<OpenGl_GlCore43> OpenGl_GlCore44;
106 //! This class generalize access to the GL context and available extensions.
108 //! Functions are grouped into structures and accessed as fields.
109 //! You should check the group for NULL before usage (if group is not NULL
110 //! then all functions are available):
112 //! if (myContext->core20 != NULL)
114 //! myGlProgram = myContext->core20->glCreateProgram();
115 //! .. do more stuff ..
119 //! .. compatibility with outdated configurations ..
123 //! Current implementation provide access to OpenGL core functionality up to 2.0 version
124 //! (core12, core13, core14, core15, fields core20).
125 //! within several extensions (arbTBO, arbFBO, etc.).
127 //! Simplified extensions classification:
128 //! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
129 //! - prefixed with EXT are accepted by 2+ vendors;
130 //! - prefixed with ARB are accepted by Architecture Review Board and are candidates
131 //! for inclusion into GL core functionality.
132 //! Some functionality can be represented in several extensions simultaneously.
133 //! In this case developer should be careful because different specification may differ
134 //! in aspects (like enumeration values and error-handling).
136 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
137 //! with different capabilities. Thats why OpenGl_Context should be initialized and used
138 //! for each GL context individually.
139 class OpenGl_Context : public Standard_Transient
143 //! Function for getting power of to number larger or equal to input number.
144 //! @param theNumber number to 'power of two'
145 //! @param theThreshold upper threshold
146 //! @return power of two number
147 inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
148 const Standard_Integer theThreshold)
150 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
162 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
163 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
166 Standard_EXPORT virtual ~OpenGl_Context();
168 //! Share GL context resources.
169 //! theShareCtx - handle to context to retrieve handles to shared resources.
170 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
172 //! Initialize class from currently bound OpenGL context. Method should be called only once.
173 //! @return false if no GL context is bound to the current thread
174 Standard_EXPORT Standard_Boolean Init();
176 //! @return true if this context is valid (has been initialized)
177 inline Standard_Boolean IsValid() const
179 return myIsInitialized;
182 #if defined(HAVE_EGL)
183 //! Initialize class from specified surface and rendering context. Method should be called only once.
184 //! @return false if OpenGL context can not be bound to specified surface
185 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theEglSurface,
186 const Aspect_Display theEglDisplay,
187 const Aspect_RenderingContext theEglContext);
188 #elif defined(_WIN32)
189 //! Initialize class from specified window and rendering context. Method should be called only once.
190 //! @return false if OpenGL context can not be bound to specified window
191 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
192 const Aspect_Handle theWindowDC,
193 const Aspect_RenderingContext theGContext);
195 //! @return the window handle (HWND) currently bound to this OpenGL context
196 inline Aspect_Handle Window() const
201 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
202 //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
203 Standard_EXPORT Standard_Boolean Init (const void* theGContext);
205 //! Initialize class from specified window and rendering context. Method should be called only once.
206 //! @return false if OpenGL context can not be bound to specified window
207 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
208 const Aspect_Display theDisplay,
209 const Aspect_RenderingContext theGContext);
211 //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
212 inline Aspect_Drawable Window() const
218 //! Check if theExtName extension is supported by active GL context.
219 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
221 //! Check if theExtName extension is in extensions string.
222 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
223 const char* theExtName);
225 //! Auxiliary template to retrieve GL function pointer.
226 //! Pointer to function retrieved from library is statically casted
227 //! to requested type - there no way to check real signature of exported function.
228 //! The context should be bound before call.
229 template <typename FuncType_t>
230 Standard_Boolean FindProc (const char* theFuncName,
231 FuncType_t& theFuncPtr)
233 theFuncPtr = (FuncType_t )findProc (theFuncName);
234 return (theFuncPtr != NULL);
237 //! @return true if detected GL version is greater or equal to requested one.
238 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
239 const Standard_Integer theVerMinor) const
241 return (myGlVerMajor > theVerMajor)
242 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
245 //! Clean up errors stack for this GL context (glGetError() in loop).
246 Standard_EXPORT void ResetErrors();
248 //! This method uses system-dependent API to retrieve information
249 //! about GL context bound to the current thread.
250 //! @return true if current thread is bound to this GL context
251 Standard_EXPORT Standard_Boolean IsCurrent() const;
253 //! Activates current context.
254 //! Class should be initialized with appropriate info.
255 Standard_EXPORT Standard_Boolean MakeCurrent();
257 //! Swap front/back buffers for this GL context (should be activated before!).
258 Standard_EXPORT void SwapBuffers();
260 //! Return true if active mode is GL_RENDER (cached state)
261 Standard_Boolean IsRender() const
263 return myRenderMode == GL_RENDER;
266 //! Return true if active mode is GL_FEEDBACK (cached state)
267 Standard_Boolean IsFeedback() const
269 return myRenderMode == GL_FEEDBACK;
272 //! This function retrieves information from GL about free GPU memory that is:
273 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
274 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
275 //! - Numbers meaning may vary.
276 //! You should use this info only for diagnostics purposes.
277 //! @return free GPU dedicated memory in bytes.
278 Standard_EXPORT Standard_Size AvailableMemory() const;
280 //! This function retrieves information from GL about GPU memory
281 //! and contains more vendor-specific values than AvailableMemory().
282 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
284 //! Access shared resource by its name.
285 //! @param theKey - unique identifier;
286 //! @return handle to shared resource or NULL.
287 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
289 //! Access shared resource by its name.
290 //! @param theKey - unique identifier;
291 //! @param theValue - handle to fill;
292 //! @return true if resource was shared.
293 template<typename TheHandleType>
294 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
295 TheHandleType& theValue) const
297 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
298 if (aResource.IsNull())
300 return Standard_False;
303 theValue = TheHandleType::DownCast (aResource);
304 return !theValue.IsNull();
307 //! Register shared resource.
308 //! Notice that after registration caller shouldn't release it by himself -
309 //! it will be automatically released on context destruction.
310 //! @param theKey - unique identifier, shouldn't be empty;
311 //! @param theResource - new resource to register, shouldn't be NULL.
312 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
313 const Handle(OpenGl_Resource)& theResource);
315 //! Release shared resource.
316 //! If there are more than one reference to this resource
317 //! (also used by some other existing object) then call will be ignored.
318 //! This means that current object itself should nullify handle before this call.
319 //! Notice that this is unrecommended operation at all and should be used
320 //! only in case of fat resources to release memory for other needs.
321 //! @param theKey unique identifier
322 //! @param theToDelay postpone release until next redraw call
323 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
324 const Standard_Boolean theToDelay = Standard_False);
326 //! Append resource to queue for delayed clean up.
327 //! Resources in this queue will be released at next redraw call.
328 Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
330 //! Clean up the delayed release queue.
331 Standard_EXPORT void ReleaseDelayed();
333 //! @return tool for management of clippings within this context.
334 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
336 //! @return tool for management of clippings within this context.
337 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
339 //! @return tool for management of shader programs within this context.
340 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
344 //! @return maximum degree of anisotropy texture filter
345 Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
347 //! @return value for GL_MAX_TEXTURE_SIZE
348 Standard_EXPORT Standard_Integer MaxTextureSize() const;
350 //! Get maximum number of clip planes supported by OpenGl.
351 //! This value is implementation dependent. At least 6
352 //! planes should be supported by OpenGl (see specs).
353 //! @return value for GL_MAX_CLIP_PLANES
354 Standard_EXPORT Standard_Integer MaxClipPlanes() const;
356 //! Returns true if VBO is supported and permitted.
357 inline bool ToUseVbo() const
359 return core15fwd != NULL
360 && !caps->vboDisable;
365 //! @return messenger instance
366 inline const Handle(Message_Messenger)& Messenger() const
368 return ::Message::DefaultMessenger();
371 //! Callback for GL_ARB_debug_output extension
372 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
373 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
374 //! @param theId message ID within source
375 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
376 //! @param theMessage the message itself
377 Standard_EXPORT void PushMessage (const unsigned int theSource,
378 const unsigned int theType,
379 const unsigned int theId,
380 const unsigned int theSeverity,
381 const TCollection_ExtendedString& theMessage);
385 //! @return true if OpenGl context supports left and
386 //! right rendering buffers.
387 Standard_Boolean HasStereoBuffers() const
389 return myIsStereoBuffers;
392 public: //! @name methods to alter or retrieve current state
394 //! Switch to left stereographic rendering buffer.
395 //! This method can be used to keep unchanged choise
396 //! of front/back/both buffer rendering.
397 Standard_EXPORT void SetDrawBufferLeft();
399 //! Switch to right stereographic rendering buffer.
400 //! This method can be used to keep unchanged choise
401 //! of front/back/both buffer rendering.
402 Standard_EXPORT void SetDrawBufferRight();
404 //! Switch to non-stereographic rendering buffer.
405 //! This method can be used to keep unchanged choise
406 //! of front/back/both buffer rendering.
407 Standard_EXPORT void SetDrawBufferMono();
409 //! Fetch OpenGl context state. This class tracks value of several OpenGl
410 //! state variables. Consulting the cached values is quicker than
411 //! doing the same via OpenGl API. Call this method if any of the controlled
412 //! OpenGl state variables has a possibility of being out-of-date.
413 Standard_EXPORT void FetchState();
415 //! @return active GLSL program
416 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
418 return myActiveProgram;
421 //! Bind specified program to current context,
422 //! or unbind previous one when NULL specified.
423 Standard_EXPORT void BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
427 //! Wrapper to system function to retrieve GL function pointer by name.
428 Standard_EXPORT void* findProc (const char* theFuncName);
430 //! Read OpenGL version information from active context.
431 Standard_EXPORT void readGlVersion();
433 //! Private initialization function that should be called only once.
434 Standard_EXPORT void init();
436 public: //! @name core profiles
438 OpenGl_GlCore11* core11; //!< OpenGL 1.1 core functionality
439 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
440 OpenGl_GlCore15* core15; //!< OpenGL 1.5 core functionality
441 OpenGl_GlCore15Fwd* core15fwd; //!< OpenGL 1.5 without deprecated entry points
442 OpenGl_GlCore20* core20; //!< OpenGL 2.0 core functionality (includes 1.5)
443 OpenGl_GlCore20Fwd* core20fwd; //!< OpenGL 2.0 without deprecated entry points
444 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
445 OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
446 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
447 OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
448 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
449 OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
450 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
451 OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
452 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
453 OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
455 Handle(OpenGl_Caps) caps; //!< context options
457 public: //! @name extensions
459 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
460 Standard_Boolean arbTexRG;//!< GL_ARB_texture_rg
461 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
462 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
463 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
464 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
465 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
466 Standard_Boolean extBgra; //!< GL_EXT_bgra
467 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
468 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
469 Standard_Boolean atiMem; //!< GL_ATI_meminfo
470 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
472 private: // system-dependent fields
474 #if defined(HAVE_EGL)
475 Aspect_Drawable myWindow; //!< EGL surface : EGLSurface
476 Aspect_Display myDisplay; //!< EGL connection to the Display : EGLDisplay
477 Aspect_RenderingContext myGContext; //!< EGL rendering context : EGLContext
478 #elif defined(_WIN32)
479 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
480 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
481 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
482 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
483 void* myGContext; //!< Rendering Context handle : NSOpenGLContext
485 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
486 Aspect_Display myDisplay; //!< connection to the X-server : Display*
487 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
490 private: // context info
492 typedef NCollection_DataMap<TCollection_AsciiString, Standard_Integer> OpenGl_DelayReleaseMap;
493 typedef NCollection_Handle<OpenGl_DelayReleaseMap> Handle(OpenGl_DelayReleaseMap);
494 typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
495 typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
496 typedef NCollection_List<Handle(OpenGl_Resource)> OpenGl_ResourcesStack;
497 typedef NCollection_Handle<OpenGl_ResourcesStack> Handle(OpenGl_ResourcesStack);
499 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
500 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
501 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
503 OpenGl_Clipping myClippingState; //!< state of clip planes
505 void* myGlLibHandle; //!< optional handle to GL library
506 NCollection_Handle<OpenGl_GlFunctions>
507 myFuncs; //!< mega structure for all GL functions
508 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
509 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
510 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
511 Standard_Integer myGlVerMajor; //!< cached GL version major number
512 Standard_Integer myGlVerMinor; //!< cached GL version minor number
513 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
514 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
516 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
518 private: //! @name fields tracking current state
520 Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
521 Standard_Integer myRenderMode; //!< value for active rendering mode
522 Standard_Integer myDrawBuffer; //!< current draw buffer
526 //! Copying allowed only within Handles
527 OpenGl_Context (const OpenGl_Context& );
528 OpenGl_Context& operator= (const OpenGl_Context& );
532 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
534 friend class OpenGl_Window;
538 #endif // _OpenGl_Context_H__