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 <Aspect_TypeOfLine.hxx>
24 #include <NCollection_DataMap.hxx>
25 #include <NCollection_Map.hxx>
26 #include <NCollection_Handle.hxx>
27 #include <NCollection_List.hxx>
28 #include <Message.hxx>
29 #include <OpenGl_Caps.hxx>
30 #include <OpenGl_MatrixState.hxx>
31 #include <OpenGl_Vec.hxx>
32 #include <OpenGl_Resource.hxx>
33 #include <Standard_Transient.hxx>
34 #include <TCollection_AsciiString.hxx>
35 #include <TColStd_PackedMapOfInteger.hxx>
36 #include <OpenGl_Clipping.hxx>
37 #include <OpenGl_GlCore11.hxx>
38 #include <OpenGl_ShaderProgram.hxx>
40 #include <NCollection_Shared.hxx>
42 //! Forward declarations
43 #if defined(__APPLE__)
44 #import <TargetConditionals.h>
45 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
53 @class NSOpenGLContext;
55 struct NSOpenGLContext;
60 struct OpenGl_GlFunctions;
65 struct OpenGl_ArbFBOBlit;
67 struct OpenGl_ArbTexBindless;
69 template<typename theBaseClass_t> struct OpenGl_TmplCore12;
70 typedef OpenGl_TmplCore12<OpenGl_GlCore11> OpenGl_GlCore12;
71 typedef OpenGl_TmplCore12<OpenGl_GlCore11Fwd> OpenGl_GlCore12Fwd;
73 struct OpenGl_GlCore13;
74 struct OpenGl_GlCore13Fwd;
76 template<typename theBaseClass_t> struct OpenGl_TmplCore14;
77 typedef OpenGl_TmplCore14<OpenGl_GlCore13> OpenGl_GlCore14;
78 typedef OpenGl_TmplCore14<OpenGl_GlCore13Fwd> OpenGl_GlCore14Fwd;
80 template<typename theBaseClass_t> struct OpenGl_TmplCore15;
81 typedef OpenGl_TmplCore15<OpenGl_GlCore14> OpenGl_GlCore15;
82 typedef OpenGl_TmplCore15<OpenGl_GlCore14Fwd> OpenGl_GlCore15Fwd;
84 template<typename theBaseClass_t> struct OpenGl_TmplCore20;
85 typedef OpenGl_TmplCore20<OpenGl_GlCore15> OpenGl_GlCore20;
86 typedef OpenGl_TmplCore20<OpenGl_GlCore15Fwd> OpenGl_GlCore20Fwd;
88 template<typename theBaseClass_t> struct OpenGl_TmplCore21;
89 typedef OpenGl_TmplCore21<OpenGl_GlCore20> OpenGl_GlCore21;
90 typedef OpenGl_TmplCore21<OpenGl_GlCore20Fwd> OpenGl_GlCore21Fwd;
92 template<typename theBaseClass_t> struct OpenGl_TmplCore30;
93 typedef OpenGl_TmplCore30<OpenGl_GlCore21> OpenGl_GlCore30;
94 typedef OpenGl_TmplCore30<OpenGl_GlCore21Fwd> OpenGl_GlCore30Fwd;
96 template<typename theBaseClass_t> struct OpenGl_TmplCore31;
97 typedef OpenGl_TmplCore31<OpenGl_GlCore30> OpenGl_GlCore31Back;
98 typedef OpenGl_TmplCore31<OpenGl_GlCore30Fwd> OpenGl_GlCore31;
100 template<typename theBaseClass_t> struct OpenGl_TmplCore32;
101 typedef OpenGl_TmplCore32<OpenGl_GlCore31Back> OpenGl_GlCore32Back;
102 typedef OpenGl_TmplCore32<OpenGl_GlCore31> OpenGl_GlCore32;
104 template<typename theBaseClass_t> struct OpenGl_TmplCore33;
105 typedef OpenGl_TmplCore33<OpenGl_GlCore32Back> OpenGl_GlCore33Back;
106 typedef OpenGl_TmplCore33<OpenGl_GlCore32> OpenGl_GlCore33;
108 template<typename theBaseClass_t> struct OpenGl_TmplCore40;
109 typedef OpenGl_TmplCore40<OpenGl_GlCore33Back> OpenGl_GlCore40Back;
110 typedef OpenGl_TmplCore40<OpenGl_GlCore33> OpenGl_GlCore40;
112 template<typename theBaseClass_t> struct OpenGl_TmplCore41;
113 typedef OpenGl_TmplCore41<OpenGl_GlCore40Back> OpenGl_GlCore41Back;
114 typedef OpenGl_TmplCore41<OpenGl_GlCore40> OpenGl_GlCore41;
116 template<typename theBaseClass_t> struct OpenGl_TmplCore42;
117 typedef OpenGl_TmplCore42<OpenGl_GlCore41Back> OpenGl_GlCore42Back;
118 typedef OpenGl_TmplCore42<OpenGl_GlCore41> OpenGl_GlCore42;
120 template<typename theBaseClass_t> struct OpenGl_TmplCore43;
121 typedef OpenGl_TmplCore43<OpenGl_GlCore42Back> OpenGl_GlCore43Back;
122 typedef OpenGl_TmplCore43<OpenGl_GlCore42> OpenGl_GlCore43;
124 template<typename theBaseClass_t> struct OpenGl_TmplCore44;
125 typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
126 typedef OpenGl_TmplCore44<OpenGl_GlCore43> OpenGl_GlCore44;
128 class OpenGl_ShaderManager;
129 class OpenGl_Sampler;
130 class OpenGl_FrameBuffer;
132 class OpenGl_Context;
133 DEFINE_STANDARD_HANDLE(OpenGl_Context, Standard_Transient)
135 //! This class generalize access to the GL context and available extensions.
137 //! Functions related to specific OpenGL version or extension are grouped into structures which can be accessed as fields of this class.
138 //! The most simple way to check that required functionality is available - is NULL check for the group:
140 //! if (myContext->core20 != NULL)
142 //! myGlProgram = myContext->core20->glCreateProgram();
143 //! .. do more stuff ..
147 //! .. compatibility with outdated configurations ..
151 //! Current implementation provide access to OpenGL core functionality up to 4.4 version (core12, core13, core14, core15, fields core20)
152 //! as well as several extensions (arbTBO, arbFBO, etc.).
154 //! OpenGL context might be initialized in Core Profile. In this case deprecated functionality become unavailable.
155 //! To make code easily adaptable to wide range of OpenGL versions, function sets related to each version has two kinds of suffixes:
156 //! - "back" for version 3.2+.
157 //! Represents function set for Backward-Compatible Profile.
158 //! Function sets without this suffix represents core profile.
159 //! - "fwd" for version 3.0-.
160 //! Represents non-deprecated function set of earlier OpenGL versions, which are still available within OpenGL 3.2 Core Profile.
161 //! Function sets without this suffix represents complete list of functions related to specific OpenGL version.
163 //! To select which core** function set should be used in specific case:
164 //! - Determine the minimal OpenGL version required for implemented functionality and use it to access all functions.
165 //! For example, if algorithm requires OpenGL 2.1+, it is better to write core20fwd->glEnable() rather than core11fwd->glEnable() for uniformity.
166 //! - If functionality will work within Core Profile, use function sets with appropriate suffix.
167 //! - Validate minimal requirements at initialization/creation time and omit checks within code where algorithm should be already initialized.
168 //! Properly escape code incompatible with Core Profile. The simplest way to check Core Profile is "if (core11 == NULL)".
170 //! Simplified extensions classification:
171 //! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
172 //! - prefixed with EXT are accepted by 2+ vendors;
173 //! - prefixed with ARB are accepted by Architecture Review Board and are candidates
174 //! for inclusion into GL core functionality.
175 //! Some functionality can be represented in several extensions simultaneously.
176 //! In this case developer should be careful because different specification may differ
177 //! in aspects (like enumeration values and error-handling).
179 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts with different capabilities.
180 //! For this reason OpenGl_Context should be initialized and used for each GL context independently.
181 class OpenGl_Context : public Standard_Transient
185 //! Function for getting power of to number larger or equal to input number.
186 //! @param theNumber number to 'power of two'
187 //! @param theThreshold upper threshold
188 //! @return power of two number
189 inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
190 const Standard_Integer theThreshold)
192 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
204 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
205 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
208 Standard_EXPORT virtual ~OpenGl_Context();
210 //! Release all resources, including shared ones
211 Standard_EXPORT void forcedRelease();
213 //! Share GL context resources.
214 //! theShareCtx - handle to context to retrieve handles to shared resources.
215 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
217 //! Initialize class from currently bound OpenGL context. Method should be called only once.
218 //! @return false if no GL context is bound to the current thread
219 Standard_EXPORT Standard_Boolean Init (const Standard_Boolean theIsCoreProfile = Standard_False);
221 //! @return true if this context is valid (has been initialized)
222 inline Standard_Boolean IsValid() const
224 return myIsInitialized;
227 #if defined(HAVE_EGL)
228 //! Initialize class from specified surface and rendering context. Method should be called only once.
229 //! @return false if OpenGL context can not be bound to specified surface
230 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theEglSurface,
231 const Aspect_Display theEglDisplay,
232 const Aspect_RenderingContext theEglContext,
233 const Standard_Boolean theIsCoreProfile = Standard_False);
234 #elif defined(_WIN32)
235 //! Initialize class from specified window and rendering context. Method should be called only once.
236 //! @return false if OpenGL context can not be bound to specified window
237 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
238 const Aspect_Handle theWindowDC,
239 const Aspect_RenderingContext theGContext,
240 const Standard_Boolean theIsCoreProfile = Standard_False);
242 //! @return the window handle (HWND) currently bound to this OpenGL context
243 inline Aspect_Handle Window() const
248 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
249 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
251 //! Initialize class from specified OpenGL ES context (EAGLContext). Method should be called only once.
252 Standard_EXPORT Standard_Boolean Init (EAGLContext* theGContext,
253 const Standard_Boolean theIsCoreProfile = Standard_False);
256 //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
257 Standard_EXPORT Standard_Boolean Init (NSOpenGLContext* theGContext,
258 const Standard_Boolean theIsCoreProfile = Standard_False);
262 //! Initialize class from specified window and rendering context. Method should be called only once.
263 //! @return false if OpenGL context can not be bound to specified window
264 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
265 const Aspect_Display theDisplay,
266 const Aspect_RenderingContext theGContext,
267 const Standard_Boolean theIsCoreProfile = Standard_False);
269 //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
270 inline Aspect_Drawable Window() const
276 //! Read OpenGL version information from active context.
277 Standard_EXPORT static void ReadGlVersion (Standard_Integer& theGlVerMajor,
278 Standard_Integer& theGlVerMinor);
280 //! Check if theExtName extension is supported by active GL context.
281 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
283 //! Check if theExtName extension is in extensions string.
284 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
285 const char* theExtName);
287 //! Auxiliary template to retrieve GL function pointer.
288 //! Pointer to function retrieved from library is statically casted
289 //! to requested type - there no way to check real signature of exported function.
290 //! The context should be bound before call.
291 template <typename FuncType_t>
292 Standard_Boolean FindProc (const char* theFuncName,
293 FuncType_t& theFuncPtr)
295 theFuncPtr = (FuncType_t )findProc (theFuncName);
296 return (theFuncPtr != NULL);
299 //! @return true if detected GL version is greater or equal to requested one.
300 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
301 const Standard_Integer theVerMinor) const
303 return (myGlVerMajor > theVerMajor)
304 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
307 //! Access entire map of loaded OpenGL functions.
308 const OpenGl_GlFunctions* Functions() const { return myFuncs.operator->(); }
310 //! Clean up errors stack for this GL context (glGetError() in loop).
311 Standard_EXPORT void ResetErrors (const bool theToPrintErrors = false);
313 //! This method uses system-dependent API to retrieve information
314 //! about GL context bound to the current thread.
315 //! @return true if current thread is bound to this GL context
316 Standard_EXPORT Standard_Boolean IsCurrent() const;
318 //! Activates current context.
319 //! Class should be initialized with appropriate info.
320 Standard_EXPORT Standard_Boolean MakeCurrent();
322 //! Swap front/back buffers for this GL context (should be activated before!).
323 Standard_EXPORT void SwapBuffers();
325 //! Setup swap interval (VSync).
326 Standard_EXPORT Standard_Boolean SetSwapInterval (const Standard_Integer theInterval);
328 //! Return true if active mode is GL_RENDER (cached state)
329 Standard_Boolean IsRender() const
331 #if !defined(GL_ES_VERSION_2_0)
332 return myRenderMode == GL_RENDER;
334 return Standard_True;
338 //! Return true if active mode is GL_FEEDBACK (cached state)
339 Standard_Boolean IsFeedback() const
341 #if !defined(GL_ES_VERSION_2_0)
342 return myRenderMode == GL_FEEDBACK;
344 return Standard_False;
348 //! This function retrieves information from GL about free GPU memory that is:
349 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
350 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
351 //! - Numbers meaning may vary.
352 //! You should use this info only for diagnostics purposes.
353 //! @return free GPU dedicated memory in bytes.
354 Standard_EXPORT Standard_Size AvailableMemory() const;
356 //! This function retrieves information from GL about GPU memory
357 //! and contains more vendor-specific values than AvailableMemory().
358 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
360 //! Access shared resource by its name.
361 //! @param theKey - unique identifier;
362 //! @return handle to shared resource or NULL.
363 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
365 //! Access shared resource by its name.
366 //! @param theKey - unique identifier;
367 //! @param theValue - handle to fill;
368 //! @return true if resource was shared.
369 template<typename TheHandleType>
370 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
371 TheHandleType& theValue) const
373 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
374 if (aResource.IsNull())
376 return Standard_False;
379 theValue = TheHandleType::DownCast (aResource);
380 return !theValue.IsNull();
383 //! Register shared resource.
384 //! Notice that after registration caller shouldn't release it by himself -
385 //! it will be automatically released on context destruction.
386 //! @param theKey - unique identifier, shouldn't be empty;
387 //! @param theResource - new resource to register, shouldn't be NULL.
388 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
389 const Handle(OpenGl_Resource)& theResource);
391 //! Release shared resource.
392 //! If there are more than one reference to this resource
393 //! (also used by some other existing object) then call will be ignored.
394 //! This means that current object itself should nullify handle before this call.
395 //! Notice that this is unrecommended operation at all and should be used
396 //! only in case of fat resources to release memory for other needs.
397 //! @param theKey unique identifier
398 //! @param theToDelay postpone release until next redraw call
399 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
400 const Standard_Boolean theToDelay = Standard_False);
402 //! Append resource to queue for delayed clean up.
403 //! Resources in this queue will be released at next redraw call.
405 void DelayedRelease (Handle(T)& theResource)
407 myUnusedResources->Prepend (theResource);
408 theResource.Nullify();
411 //! Clean up the delayed release queue.
412 Standard_EXPORT void ReleaseDelayed();
414 //! @return tool for management of clippings within this context.
415 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
417 //! @return tool for management of clippings within this context.
418 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
420 //! @return tool for management of shader programs within this context.
421 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
425 //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
426 Standard_Integer TextureWrapClamp() const { return myTexClamp; }
428 //! @return maximum degree of anisotropy texture filter
429 Standard_Integer MaxDegreeOfAnisotropy() const { return myAnisoMax; }
431 //! @return value for GL_MAX_TEXTURE_SIZE
432 Standard_Integer MaxTextureSize() const { return myMaxTexDim; }
434 //! @return value for GL_MAX_SAMPLES
435 Standard_Integer MaxMsaaSamples() const { return myMaxMsaaSamples; }
437 //! Get maximum number of clip planes supported by OpenGl.
438 //! This value is implementation dependent. At least 6
439 //! planes should be supported by OpenGl (see specs).
440 //! @return value for GL_MAX_CLIP_PLANES
441 Standard_Integer MaxClipPlanes() const { return myMaxClipPlanes; }
443 //! Returns true if VBO is supported and permitted.
444 inline bool ToUseVbo() const
446 return core15fwd != NULL
447 && !caps->vboDisable;
450 //! @return cached state of GL_NORMALIZE.
451 Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
453 //! Sets GL_NORMALIZE enabled or disabled.
454 //! @return old value of the flag
455 Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
457 //! Applies matrix stored in ModelWorldState to OpenGl.
458 Standard_EXPORT void ApplyModelWorldMatrix();
460 //! Applies matrix stored in WorldViewState to OpenGl.
461 Standard_EXPORT void ApplyWorldViewMatrix();
463 //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
464 Standard_EXPORT void ApplyModelViewMatrix();
466 //! Applies matrix stored in ProjectionState to OpenGl.
467 Standard_EXPORT void ApplyProjectionMatrix();
471 //! @return messenger instance
472 inline const Handle(Message_Messenger)& Messenger() const
474 return ::Message::DefaultMessenger();
477 //! Callback for GL_ARB_debug_output extension
478 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
479 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
480 //! @param theId message ID within source
481 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
482 //! @param theMessage the message itself
483 Standard_EXPORT void PushMessage (const unsigned int theSource,
484 const unsigned int theType,
485 const unsigned int theId,
486 const unsigned int theSeverity,
487 const TCollection_ExtendedString& theMessage);
489 //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
490 Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
491 const unsigned int theId);
493 //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
494 Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
495 const unsigned int theId);
497 //! @return true if OpenGl context supports left and
498 //! right rendering buffers.
499 Standard_Boolean HasStereoBuffers() const
501 #if !defined(GL_ES_VERSION_2_0)
502 return myIsStereoBuffers;
504 return Standard_False;
508 public: //! @name methods to alter or retrieve current state
510 //! Return active read buffer.
511 Standard_Integer ReadBuffer() { return myReadBuffer; }
513 //! Switch read buffer, wrapper for ::glReadBuffer().
514 Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
516 //! Return active draw buffer.
517 Standard_Integer DrawBuffer() { return myDrawBuffer; }
519 //! Switch draw buffer, wrapper for ::glDrawBuffer().
520 Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
522 //! Switch read/draw buffers.
523 void SetReadDrawBuffer (const Standard_Integer theBuffer)
525 SetReadBuffer (theBuffer);
526 SetDrawBuffer (theBuffer);
529 //! Fetch OpenGl context state. This class tracks value of several OpenGl
530 //! state variables. Consulting the cached values is quicker than
531 //! doing the same via OpenGl API. Call this method if any of the controlled
532 //! OpenGl state variables has a possibility of being out-of-date.
533 Standard_EXPORT void FetchState();
535 //! @return active GLSL program
536 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
538 return myActiveProgram;
541 //! @return OpenGL sampler object used to override default texture parameters
542 const Handle(OpenGl_Sampler)& TextureSampler()
547 //! Bind specified program to current context,
548 //! or unbind previous one when NULL specified.
549 //! @return true if some program is bound to context
550 Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
552 //! Setup current color.
553 Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
555 //! Setup type of line.
556 Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine theType,
557 const Standard_ShortReal theFactor = 1.0f);
559 //! Setup width of line.
560 Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
562 //! Setup point size.
563 Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
565 //! Setup texture matrix to active GLSL program or to FFP global state using glMatrixMode (GL_TEXTURE).
566 Standard_EXPORT void SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams);
568 //! Bind default Vertex Array Object
569 Standard_EXPORT void BindDefaultVao();
571 //! Default Frame Buffer Object.
572 const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
577 //! Setup new Default Frame Buffer Object and return previously set.
578 //! This call doesn't change Active FBO!
579 Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
581 //! Return debug context initialization state.
582 Standard_Boolean IsDebugContext() const
584 return myIsGlDebugCtx;
587 Standard_EXPORT void EnableFeatures() const;
589 Standard_EXPORT void DisableFeatures() const;
591 //! Set resolution ratio.
592 //! Note that this method rounds @theRatio to nearest integer.
593 void SetResolutionRatio (const Standard_ShortReal theRatio)
595 myResolutionRatio = Max (1.0f, std::floor (theRatio + 0.5f));
600 //! Wrapper to system function to retrieve GL function pointer by name.
601 Standard_EXPORT void* findProc (const char* theFuncName);
603 //! Print error if not all functions have been exported by context for reported version.
604 //! Note that this will never happen when using GLX, since returned functions can not be validated.
605 //! @param theGlVerMajor the OpenGL major version with missing functions
606 //! @param theGlVerMinor the OpenGL minor version with missing functions
607 Standard_EXPORT void checkWrongVersion (const Standard_Integer theGlVerMajor,
608 const Standard_Integer theGlVerMinor);
610 //! Private initialization function that should be called only once.
611 Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
613 public: //! @name core profiles
615 OpenGl_GlCore11* core11; //!< OpenGL 1.1 core functionality
616 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
617 OpenGl_GlCore15* core15; //!< OpenGL 1.5 core functionality
618 OpenGl_GlCore15Fwd* core15fwd; //!< OpenGL 1.5 without deprecated entry points
619 OpenGl_GlCore20* core20; //!< OpenGL 2.0 core functionality (includes 1.5)
620 OpenGl_GlCore20Fwd* core20fwd; //!< OpenGL 2.0 without deprecated entry points
621 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
622 OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
623 OpenGl_GlCore33* core33; //!< OpenGL 3.3 core profile
624 OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
625 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
626 OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
627 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
628 OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
629 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
630 OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
631 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
632 OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
634 Handle(OpenGl_Caps) caps; //!< context options
636 public: //! @name extensions
638 Standard_Boolean hasHighp; //!< highp in GLSL ES fragment shader is supported
639 Standard_Boolean hasUintIndex; //!< GLuint for index buffer is supported (always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_element_index_uint)
640 Standard_Boolean hasTexRGBA8; //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
641 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
642 Standard_Boolean arbTexRG; //!< GL_ARB_texture_rg
643 Standard_Boolean arbTexFloat; //!< GL_ARB_texture_float (on desktop OpenGL - since 3.0 or as extension GL_ARB_texture_float; on OpenGL ES - since 3.0)
644 OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
645 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
646 Standard_Boolean arbTboRGB32; //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0
647 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
648 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
649 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
650 OpenGl_ArbFBOBlit* arbFBOBlit; //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
651 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
652 Standard_Boolean extBgra; //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
653 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
654 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
655 Standard_Boolean atiMem; //!< GL_ATI_meminfo
656 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
658 private: // system-dependent fields
660 #if defined(HAVE_EGL)
661 Aspect_Drawable myWindow; //!< EGL surface : EGLSurface
662 Aspect_Display myDisplay; //!< EGL connection to the Display : EGLDisplay
663 Aspect_RenderingContext myGContext; //!< EGL rendering context : EGLContext
664 #elif defined(_WIN32)
665 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
666 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
667 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
668 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
669 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
670 EAGLContext* myGContext; //!< Rendering Context handle
672 NSOpenGLContext* myGContext; //!< Rendering Context handle
675 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
676 Aspect_Display myDisplay; //!< connection to the X-server : Display*
677 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
680 private: // context info
682 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Standard_Integer> > OpenGl_DelayReleaseMap;
683 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> > OpenGl_ResourcesMap;
684 typedef NCollection_Shared< NCollection_List<Handle(OpenGl_Resource)> > OpenGl_ResourcesStack;
686 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
687 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
688 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
690 OpenGl_Clipping myClippingState; //!< state of clip planes
692 void* myGlLibHandle; //!< optional handle to GL library
693 NCollection_Handle<OpenGl_GlFunctions>
694 myFuncs; //!< mega structure for all GL functions
695 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
696 Standard_Integer myTexClamp; //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
697 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
698 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
699 Standard_Integer myMaxMsaaSamples; //!< value for GL_MAX_SAMPLES
700 Standard_Integer myGlVerMajor; //!< cached GL version major number
701 Standard_Integer myGlVerMinor; //!< cached GL version minor number
702 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
703 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
704 Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
705 //!< Used to tell OpenGl that normals should be normalized
707 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
709 private: //! @name fields tracking current state
711 Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
712 Handle(OpenGl_Sampler) myTexSampler; //!< currently active sampler object
713 Handle(OpenGl_FrameBuffer) myDefaultFbo; //!< default Frame Buffer Object
714 Standard_Integer myRenderMode; //!< value for active rendering mode
715 Standard_Integer myReadBuffer; //!< current read buffer
716 Standard_Integer myDrawBuffer; //!< current draw buffer
717 unsigned int myDefaultVao; //!< default Vertex Array Object
718 Standard_Boolean myIsGlDebugCtx; //!< debug context initialization state
719 TCollection_AsciiString myVendor; //!< Graphics Driver's vendor
720 TColStd_PackedMapOfInteger myFilters[6]; //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
721 Standard_ShortReal myResolutionRatio; //!< scaling factor for parameters like text size
722 //!< to be properly displayed on device (screen / printer)
726 OpenGl_MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
727 OpenGl_MatrixState<Standard_ShortReal> WorldViewState; //!< state of orientation matrix
728 OpenGl_MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection matrix
732 //! Copying allowed only within Handles
733 OpenGl_Context (const OpenGl_Context& );
734 OpenGl_Context& operator= (const OpenGl_Context& );
738 DEFINE_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient) // Type definition
740 friend class OpenGl_Window;
744 #endif // _OpenGl_Context_H__