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 <NCollection_DataMap.hxx>
26 #include <NCollection_Map.hxx>
27 #include <NCollection_Handle.hxx>
28 #include <NCollection_List.hxx>
29 #include <Message.hxx>
30 #include <OpenGl_Caps.hxx>
31 #include <OpenGl_Resource.hxx>
32 #include <Standard_Transient.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <Handle_OpenGl_Context.hxx>
35 #include <OpenGl_Clipping.hxx>
36 #include <OpenGl_GlCore11.hxx>
38 //! Forward declarations
39 struct OpenGl_GlFunctions;
46 template<typename theBaseClass_t> struct OpenGl_TmplCore12;
47 typedef OpenGl_TmplCore12<OpenGl_GlCore11> OpenGl_GlCore12;
48 typedef OpenGl_TmplCore12<OpenGl_GlCore11Fwd> OpenGl_GlCore12Fwd;
50 struct OpenGl_GlCore13;
51 struct OpenGl_GlCore13Fwd;
53 template<typename theBaseClass_t> struct OpenGl_TmplCore14;
54 typedef OpenGl_TmplCore14<OpenGl_GlCore13> OpenGl_GlCore14;
55 typedef OpenGl_TmplCore14<OpenGl_GlCore13Fwd> OpenGl_GlCore14Fwd;
57 template<typename theBaseClass_t> struct OpenGl_TmplCore15;
58 typedef OpenGl_TmplCore15<OpenGl_GlCore14> OpenGl_GlCore15;
59 typedef OpenGl_TmplCore15<OpenGl_GlCore14Fwd> OpenGl_GlCore15Fwd;
61 template<typename theBaseClass_t> struct OpenGl_TmplCore20;
62 typedef OpenGl_TmplCore20<OpenGl_GlCore15> OpenGl_GlCore20;
63 typedef OpenGl_TmplCore20<OpenGl_GlCore15Fwd> OpenGl_GlCore20Fwd;
65 template<typename theBaseClass_t> struct OpenGl_TmplCore21;
66 typedef OpenGl_TmplCore21<OpenGl_GlCore20> OpenGl_GlCore21;
67 typedef OpenGl_TmplCore21<OpenGl_GlCore20Fwd> OpenGl_GlCore21Fwd;
69 template<typename theBaseClass_t> struct OpenGl_TmplCore30;
70 typedef OpenGl_TmplCore30<OpenGl_GlCore21> OpenGl_GlCore30;
71 typedef OpenGl_TmplCore30<OpenGl_GlCore21Fwd> OpenGl_GlCore30Fwd;
73 template<typename theBaseClass_t> struct OpenGl_TmplCore31;
74 typedef OpenGl_TmplCore31<OpenGl_GlCore30> OpenGl_GlCore31Back;
75 typedef OpenGl_TmplCore31<OpenGl_GlCore30Fwd> OpenGl_GlCore31;
77 template<typename theBaseClass_t> struct OpenGl_TmplCore32;
78 typedef OpenGl_TmplCore32<OpenGl_GlCore31Back> OpenGl_GlCore32Back;
79 typedef OpenGl_TmplCore32<OpenGl_GlCore31> OpenGl_GlCore32;
81 template<typename theBaseClass_t> struct OpenGl_TmplCore33;
82 typedef OpenGl_TmplCore33<OpenGl_GlCore32Back> OpenGl_GlCore33Back;
83 typedef OpenGl_TmplCore33<OpenGl_GlCore32> OpenGl_GlCore33;
85 template<typename theBaseClass_t> struct OpenGl_TmplCore40;
86 typedef OpenGl_TmplCore40<OpenGl_GlCore33Back> OpenGl_GlCore40Back;
87 typedef OpenGl_TmplCore40<OpenGl_GlCore33> OpenGl_GlCore40;
89 template<typename theBaseClass_t> struct OpenGl_TmplCore41;
90 typedef OpenGl_TmplCore41<OpenGl_GlCore40Back> OpenGl_GlCore41Back;
91 typedef OpenGl_TmplCore41<OpenGl_GlCore40> OpenGl_GlCore41;
93 template<typename theBaseClass_t> struct OpenGl_TmplCore42;
94 typedef OpenGl_TmplCore42<OpenGl_GlCore41Back> OpenGl_GlCore42Back;
95 typedef OpenGl_TmplCore42<OpenGl_GlCore41> OpenGl_GlCore42;
97 template<typename theBaseClass_t> struct OpenGl_TmplCore43;
98 typedef OpenGl_TmplCore43<OpenGl_GlCore42Back> OpenGl_GlCore43Back;
99 typedef OpenGl_TmplCore43<OpenGl_GlCore42> OpenGl_GlCore43;
101 template<typename theBaseClass_t> struct OpenGl_TmplCore44;
102 typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
103 typedef OpenGl_TmplCore44<OpenGl_GlCore43> OpenGl_GlCore44;
105 //! This class generalize access to the GL context and available extensions.
107 //! Functions are grouped into structures and accessed as fields.
108 //! You should check the group for NULL before usage (if group is not NULL
109 //! then all functions are available):
111 //! if (myContext->core20 != NULL)
113 //! myGlProgram = myContext->core20->glCreateProgram();
114 //! .. do more stuff ..
118 //! .. compatibility with outdated configurations ..
122 //! Current implementation provide access to OpenGL core functionality up to 2.0 version
123 //! (core12, core13, core14, core15, fields core20).
124 //! within several extensions (arbTBO, arbFBO, etc.).
126 //! Simplified extensions classification:
127 //! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
128 //! - prefixed with EXT are accepted by 2+ vendors;
129 //! - prefixed with ARB are accepted by Architecture Review Board and are candidates
130 //! for inclusion into GL core functionality.
131 //! Some functionality can be represented in several extensions simultaneously.
132 //! In this case developer should be careful because different specification may differ
133 //! in aspects (like enumeration values and error-handling).
135 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
136 //! with different capabilities. Thats why OpenGl_Context should be initialized and used
137 //! for each GL context individually.
138 class OpenGl_Context : public Standard_Transient
142 //! Function for getting power of to number larger or equal to input number.
143 //! @param theNumber number to 'power of two'
144 //! @param theThreshold upper threshold
145 //! @return power of two number
146 inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
147 const Standard_Integer theThreshold)
149 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
161 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
162 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
165 Standard_EXPORT virtual ~OpenGl_Context();
167 //! Share GL context resources.
168 //! theShareCtx - handle to context to retrieve handles to shared resources.
169 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
171 //! Initialize class from currently bound OpenGL context. Method should be called only once.
172 //! @return false if no GL context is bound to the current thread
173 Standard_EXPORT Standard_Boolean Init();
175 //! @return true if this context is valid (has been initialized)
176 inline Standard_Boolean IsValid() const
178 return myIsInitialized;
181 #if defined(HAVE_EGL)
182 //! Initialize class from specified surface and rendering context. Method should be called only once.
183 //! @return false if OpenGL context can not be bound to specified surface
184 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theEglSurface,
185 const Aspect_Display theEglDisplay,
186 const Aspect_RenderingContext theEglContext);
187 #elif defined(_WIN32)
188 //! Initialize class from specified window and rendering context. Method should be called only once.
189 //! @return false if OpenGL context can not be bound to specified window
190 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
191 const Aspect_Handle theWindowDC,
192 const Aspect_RenderingContext theGContext);
194 //! @return the window handle (HWND) currently bound to this OpenGL context
195 inline Aspect_Handle Window() const
200 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
201 //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
202 Standard_EXPORT Standard_Boolean Init (const void* theGContext);
204 //! Initialize class from specified window and rendering context. Method should be called only once.
205 //! @return false if OpenGL context can not be bound to specified window
206 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
207 const Aspect_Display theDisplay,
208 const Aspect_RenderingContext theGContext);
210 //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
211 inline Aspect_Drawable Window() const
217 //! Check if theExtName extension is supported by active GL context.
218 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
220 //! Check if theExtName extension is in extensions string.
221 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
222 const char* theExtName);
224 //! Auxiliary template to retrieve GL function pointer.
225 //! Pointer to function retrieved from library is statically casted
226 //! to requested type - there no way to check real signature of exported function.
227 //! The context should be bound before call.
228 template <typename FuncType_t>
229 Standard_Boolean FindProc (const char* theFuncName,
230 FuncType_t& theFuncPtr)
232 theFuncPtr = (FuncType_t )findProc (theFuncName);
233 return (theFuncPtr != NULL);
236 //! @return true if detected GL version is greater or equal to requested one.
237 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
238 const Standard_Integer theVerMinor) const
240 return (myGlVerMajor > theVerMajor)
241 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
244 //! Clean up errors stack for this GL context (glGetError() in loop).
245 Standard_EXPORT void ResetErrors();
247 //! This method uses system-dependent API to retrieve information
248 //! about GL context bound to the current thread.
249 //! @return true if current thread is bound to this GL context
250 Standard_EXPORT Standard_Boolean IsCurrent() const;
252 //! Activates current context.
253 //! Class should be initialized with appropriate info.
254 Standard_EXPORT Standard_Boolean MakeCurrent();
256 //! Swap front/back buffers for this GL context (should be activated before!).
257 Standard_EXPORT void SwapBuffers();
259 //! Return true if active mode is GL_RENDER (cached state)
260 Standard_Boolean IsRender() const
262 return myRenderMode == GL_RENDER;
265 //! Return true if active mode is GL_FEEDBACK (cached state)
266 Standard_Boolean IsFeedback() const
268 return myRenderMode == GL_FEEDBACK;
271 //! This function retrieves information from GL about free GPU memory that is:
272 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
273 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
274 //! - Numbers meaning may vary.
275 //! You should use this info only for diagnostics purposes.
276 //! @return free GPU dedicated memory in bytes.
277 Standard_EXPORT Standard_Size AvailableMemory() const;
279 //! This function retrieves information from GL about GPU memory
280 //! and contains more vendor-specific values than AvailableMemory().
281 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
283 //! Access shared resource by its name.
284 //! @param theKey - unique identifier;
285 //! @return handle to shared resource or NULL.
286 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
288 //! Access shared resource by its name.
289 //! @param theKey - unique identifier;
290 //! @param theValue - handle to fill;
291 //! @return true if resource was shared.
292 template<typename TheHandleType>
293 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
294 TheHandleType& theValue) const
296 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
297 if (aResource.IsNull())
299 return Standard_False;
302 theValue = TheHandleType::DownCast (aResource);
303 return !theValue.IsNull();
306 //! Register shared resource.
307 //! Notice that after registration caller shouldn't release it by himself -
308 //! it will be automatically released on context destruction.
309 //! @param theKey - unique identifier, shouldn't be empty;
310 //! @param theResource - new resource to register, shouldn't be NULL.
311 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
312 const Handle(OpenGl_Resource)& theResource);
314 //! Release shared resource.
315 //! If there are more than one reference to this resource
316 //! (also used by some other existing object) then call will be ignored.
317 //! This means that current object itself should nullify handle before this call.
318 //! Notice that this is unrecommended operation at all and should be used
319 //! only in case of fat resources to release memory for other needs.
320 //! @param theKey unique identifier
321 //! @param theToDelay postpone release until next redraw call
322 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
323 const Standard_Boolean theToDelay = Standard_False);
325 //! Append resource to queue for delayed clean up.
326 //! Resources in this queue will be released at next redraw call.
327 Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
329 //! Clean up the delayed release queue.
330 Standard_EXPORT void ReleaseDelayed();
332 //! @return tool for management of clippings within this context.
333 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
335 //! @return tool for management of clippings within this context.
336 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
338 //! @return tool for management of shader programs within this context.
339 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
343 //! @return maximum degree of anisotropy texture filter
344 Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
346 //! @return value for GL_MAX_TEXTURE_SIZE
347 Standard_EXPORT Standard_Integer MaxTextureSize() const;
349 //! Get maximum number of clip planes supported by OpenGl.
350 //! This value is implementation dependent. At least 6
351 //! planes should be supported by OpenGl (see specs).
352 //! @return value for GL_MAX_CLIP_PLANES
353 Standard_EXPORT Standard_Integer MaxClipPlanes() const;
357 //! @return messenger instance
358 inline const Handle(Message_Messenger)& Messenger() const
360 return ::Message::DefaultMessenger();
363 //! Callback for GL_ARB_debug_output extension
364 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
365 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
366 //! @param theId message ID within source
367 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
368 //! @param theMessage the message itself
369 Standard_EXPORT void PushMessage (const unsigned int theSource,
370 const unsigned int theType,
371 const unsigned int theId,
372 const unsigned int theSeverity,
373 const TCollection_ExtendedString& theMessage);
377 //! @return true if OpenGl context supports left and
378 //! right rendering buffers.
379 Standard_Boolean HasStereoBuffers() const
381 return myIsStereoBuffers;
384 //! Switch to left stereographic rendering buffer.
385 //! This method can be used to keep unchanged choise
386 //! of front/back/both buffer rendering.
387 Standard_EXPORT void SetDrawBufferLeft();
389 //! Switch to right stereographic rendering buffer.
390 //! This method can be used to keep unchanged choise
391 //! of front/back/both buffer rendering.
392 Standard_EXPORT void SetDrawBufferRight();
394 //! Switch to non-stereographic rendering buffer.
395 //! This method can be used to keep unchanged choise
396 //! of front/back/both buffer rendering.
397 Standard_EXPORT void SetDrawBufferMono();
399 //! Fetch OpenGl context state. This class tracks value of several OpenGl
400 //! state variables. Consulting the cached values is quicker than
401 //! doing the same via OpenGl API. Call this method if any of the controlled
402 //! OpenGl state variables has a possibility of being out-of-date.
403 Standard_EXPORT void FetchState();
407 //! Wrapper to system function to retrieve GL function pointer by name.
408 Standard_EXPORT void* findProc (const char* theFuncName);
410 //! Read OpenGL version information from active context.
411 Standard_EXPORT void readGlVersion();
413 //! Private initialization function that should be called only once.
414 Standard_EXPORT void init();
416 public: //! @name core profiles
418 OpenGl_GlCore11* core11; //!< OpenGL 1.1 core functionality
419 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
420 OpenGl_GlCore15* core15; //!< OpenGL 1.5 core functionality
421 OpenGl_GlCore15Fwd* core15fwd; //!< OpenGL 1.5 without deprecated entry points
422 OpenGl_GlCore20* core20; //!< OpenGL 2.0 core functionality (includes 1.5)
423 OpenGl_GlCore20Fwd* core20fwd; //!< OpenGL 2.0 without deprecated entry points
424 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
425 OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
426 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
427 OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
428 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
429 OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
430 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
431 OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
432 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
433 OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
435 Handle(OpenGl_Caps) caps; //!< context options
437 public: //! @name extensions
439 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
440 Standard_Boolean arbTexRG;//!< GL_ARB_texture_rg
441 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
442 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
443 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
444 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
445 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
446 Standard_Boolean extBgra; //!< GL_EXT_bgra
447 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
448 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
449 Standard_Boolean atiMem; //!< GL_ATI_meminfo
450 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
452 private: // system-dependent fields
454 #if defined(HAVE_EGL)
455 Aspect_Drawable myWindow; //!< EGL surface : EGLSurface
456 Aspect_Display myDisplay; //!< EGL connection to the Display : EGLDisplay
457 Aspect_RenderingContext myGContext; //!< EGL rendering context : EGLContext
458 #elif defined(_WIN32)
459 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
460 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
461 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
462 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
463 void* myGContext; //!< Rendering Context handle : NSOpenGLContext
465 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
466 Aspect_Display myDisplay; //!< connection to the X-server : Display*
467 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
470 private: // context info
472 typedef NCollection_DataMap<TCollection_AsciiString, Standard_Integer> OpenGl_DelayReleaseMap;
473 typedef NCollection_Handle<OpenGl_DelayReleaseMap> Handle(OpenGl_DelayReleaseMap);
474 typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
475 typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
476 typedef NCollection_List<Handle(OpenGl_Resource)> OpenGl_ResourcesStack;
477 typedef NCollection_Handle<OpenGl_ResourcesStack> Handle(OpenGl_ResourcesStack);
479 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
480 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
481 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
483 OpenGl_Clipping myClippingState; //!< state of clip planes
485 void* myGlLibHandle; //!< optional handle to GL library
486 NCollection_Handle<OpenGl_GlFunctions>
487 myFuncs; //!< mega structure for all GL functions
488 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
489 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
490 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
491 Standard_Integer myGlVerMajor; //!< cached GL version major number
492 Standard_Integer myGlVerMinor; //!< cached GL version minor number
493 Standard_Integer myRenderMode; //!< value for active rendering mode
494 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
495 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
496 Standard_Integer myDrawBuffer; //!< current draw buffer.
498 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
502 //! Copying allowed only within Handles
503 OpenGl_Context (const OpenGl_Context& );
504 OpenGl_Context& operator= (const OpenGl_Context& );
508 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
510 friend class OpenGl_Window;
514 #endif // _OpenGl_Context_H__