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_HatchStyle.hxx>
21 #include <Aspect_Drawable.hxx>
22 #include <Aspect_Display.hxx>
23 #include <Aspect_RenderingContext.hxx>
24 #include <Aspect_TypeOfLine.hxx>
25 #include <NCollection_DataMap.hxx>
26 #include <Graphic3d_DiagnosticInfo.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_LineAttributes.hxx>
33 #include <OpenGl_MatrixState.hxx>
34 #include <OpenGl_Vec.hxx>
35 #include <OpenGl_Resource.hxx>
36 #include <Standard_Transient.hxx>
37 #include <TColStd_IndexedDataMapOfStringString.hxx>
38 #include <TColStd_PackedMapOfInteger.hxx>
39 #include <OpenGl_Clipping.hxx>
40 #include <OpenGl_GlCore11.hxx>
41 #include <OpenGl_ShaderProgram.hxx>
43 #include <NCollection_Shared.hxx>
45 //! Forward declarations
46 #if defined(__APPLE__)
47 #import <TargetConditionals.h>
48 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
56 @class NSOpenGLContext;
58 struct NSOpenGLContext;
63 struct OpenGl_GlFunctions;
68 struct OpenGl_ArbFBOBlit;
70 struct OpenGl_ArbTexBindless;
72 template<typename theBaseClass_t> struct OpenGl_TmplCore12;
73 typedef OpenGl_TmplCore12<OpenGl_GlCore11> OpenGl_GlCore12;
74 typedef OpenGl_TmplCore12<OpenGl_GlCore11Fwd> OpenGl_GlCore12Fwd;
76 struct OpenGl_GlCore13;
77 struct OpenGl_GlCore13Fwd;
79 template<typename theBaseClass_t> struct OpenGl_TmplCore14;
80 typedef OpenGl_TmplCore14<OpenGl_GlCore13> OpenGl_GlCore14;
81 typedef OpenGl_TmplCore14<OpenGl_GlCore13Fwd> OpenGl_GlCore14Fwd;
83 template<typename theBaseClass_t> struct OpenGl_TmplCore15;
84 typedef OpenGl_TmplCore15<OpenGl_GlCore14> OpenGl_GlCore15;
85 typedef OpenGl_TmplCore15<OpenGl_GlCore14Fwd> OpenGl_GlCore15Fwd;
87 template<typename theBaseClass_t> struct OpenGl_TmplCore20;
88 typedef OpenGl_TmplCore20<OpenGl_GlCore15> OpenGl_GlCore20;
89 typedef OpenGl_TmplCore20<OpenGl_GlCore15Fwd> OpenGl_GlCore20Fwd;
91 template<typename theBaseClass_t> struct OpenGl_TmplCore21;
92 typedef OpenGl_TmplCore21<OpenGl_GlCore20> OpenGl_GlCore21;
93 typedef OpenGl_TmplCore21<OpenGl_GlCore20Fwd> OpenGl_GlCore21Fwd;
95 template<typename theBaseClass_t> struct OpenGl_TmplCore30;
96 typedef OpenGl_TmplCore30<OpenGl_GlCore21> OpenGl_GlCore30;
97 typedef OpenGl_TmplCore30<OpenGl_GlCore21Fwd> OpenGl_GlCore30Fwd;
99 template<typename theBaseClass_t> struct OpenGl_TmplCore31;
100 typedef OpenGl_TmplCore31<OpenGl_GlCore30> OpenGl_GlCore31Back;
101 typedef OpenGl_TmplCore31<OpenGl_GlCore30Fwd> OpenGl_GlCore31;
103 template<typename theBaseClass_t> struct OpenGl_TmplCore32;
104 typedef OpenGl_TmplCore32<OpenGl_GlCore31Back> OpenGl_GlCore32Back;
105 typedef OpenGl_TmplCore32<OpenGl_GlCore31> OpenGl_GlCore32;
107 template<typename theBaseClass_t> struct OpenGl_TmplCore33;
108 typedef OpenGl_TmplCore33<OpenGl_GlCore32Back> OpenGl_GlCore33Back;
109 typedef OpenGl_TmplCore33<OpenGl_GlCore32> OpenGl_GlCore33;
111 template<typename theBaseClass_t> struct OpenGl_TmplCore40;
112 typedef OpenGl_TmplCore40<OpenGl_GlCore33Back> OpenGl_GlCore40Back;
113 typedef OpenGl_TmplCore40<OpenGl_GlCore33> OpenGl_GlCore40;
115 template<typename theBaseClass_t> struct OpenGl_TmplCore41;
116 typedef OpenGl_TmplCore41<OpenGl_GlCore40Back> OpenGl_GlCore41Back;
117 typedef OpenGl_TmplCore41<OpenGl_GlCore40> OpenGl_GlCore41;
119 template<typename theBaseClass_t> struct OpenGl_TmplCore42;
120 typedef OpenGl_TmplCore42<OpenGl_GlCore41Back> OpenGl_GlCore42Back;
121 typedef OpenGl_TmplCore42<OpenGl_GlCore41> OpenGl_GlCore42;
123 template<typename theBaseClass_t> struct OpenGl_TmplCore43;
124 typedef OpenGl_TmplCore43<OpenGl_GlCore42Back> OpenGl_GlCore43Back;
125 typedef OpenGl_TmplCore43<OpenGl_GlCore42> OpenGl_GlCore43;
127 template<typename theBaseClass_t> struct OpenGl_TmplCore44;
128 typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
129 typedef OpenGl_TmplCore44<OpenGl_GlCore43> OpenGl_GlCore44;
131 class OpenGl_ShaderManager;
132 class OpenGl_Sampler;
133 class OpenGl_FrameBuffer;
134 class OpenGl_AspectFace;
136 class OpenGl_Context;
137 DEFINE_STANDARD_HANDLE(OpenGl_Context, Standard_Transient)
139 //! This class generalize access to the GL context and available extensions.
141 //! Functions related to specific OpenGL version or extension are grouped into structures which can be accessed as fields of this class.
142 //! The most simple way to check that required functionality is available - is NULL check for the group:
144 //! if (myContext->core20 != NULL)
146 //! myGlProgram = myContext->core20->glCreateProgram();
147 //! .. do more stuff ..
151 //! .. compatibility with outdated configurations ..
155 //! Current implementation provide access to OpenGL core functionality up to 4.4 version (core12, core13, core14, core15, fields core20)
156 //! as well as several extensions (arbTBO, arbFBO, etc.).
158 //! OpenGL context might be initialized in Core Profile. In this case deprecated functionality become unavailable.
159 //! To make code easily adaptable to wide range of OpenGL versions, function sets related to each version has two kinds of suffixes:
160 //! - "back" for version 3.2+.
161 //! Represents function set for Backward-Compatible Profile.
162 //! Function sets without this suffix represents core profile.
163 //! - "fwd" for version 3.0-.
164 //! Represents non-deprecated function set of earlier OpenGL versions, which are still available within OpenGL 3.2 Core Profile.
165 //! Function sets without this suffix represents complete list of functions related to specific OpenGL version.
167 //! To select which core** function set should be used in specific case:
168 //! - Determine the minimal OpenGL version required for implemented functionality and use it to access all functions.
169 //! For example, if algorithm requires OpenGL 2.1+, it is better to write core20fwd->glEnable() rather than core11fwd->glEnable() for uniformity.
170 //! - If functionality will work within Core Profile, use function sets with appropriate suffix.
171 //! - Validate minimal requirements at initialization/creation time and omit checks within code where algorithm should be already initialized.
172 //! Properly escape code incompatible with Core Profile. The simplest way to check Core Profile is "if (core11 == NULL)".
174 //! Simplified extensions classification:
175 //! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
176 //! - prefixed with EXT are accepted by 2+ vendors;
177 //! - prefixed with ARB are accepted by Architecture Review Board and are candidates
178 //! for inclusion into GL core functionality.
179 //! Some functionality can be represented in several extensions simultaneously.
180 //! In this case developer should be careful because different specification may differ
181 //! in aspects (like enumeration values and error-handling).
183 //! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts with different capabilities.
184 //! For this reason OpenGl_Context should be initialized and used for each GL context independently.
185 class OpenGl_Context : public Standard_Transient
189 //! Function for getting power of to number larger or equal to input number.
190 //! @param theNumber number to 'power of two'
191 //! @param theThreshold upper threshold
192 //! @return power of two number
193 inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
194 const Standard_Integer theThreshold)
196 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
208 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
209 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
212 Standard_EXPORT virtual ~OpenGl_Context();
214 //! Release all resources, including shared ones
215 Standard_EXPORT void forcedRelease();
217 //! Share GL context resources.
218 //! theShareCtx - handle to context to retrieve handles to shared resources.
219 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
221 //! Initialize class from currently bound OpenGL context. Method should be called only once.
222 //! @return false if no GL context is bound to the current thread
223 Standard_EXPORT Standard_Boolean Init (const Standard_Boolean theIsCoreProfile = Standard_False);
225 //! @return true if this context is valid (has been initialized)
226 inline Standard_Boolean IsValid() const
228 return myIsInitialized;
231 #if defined(HAVE_EGL)
232 //! Initialize class from specified surface and rendering context. Method should be called only once.
233 //! @return false if OpenGL context can not be bound to specified surface
234 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theEglSurface,
235 const Aspect_Display theEglDisplay,
236 const Aspect_RenderingContext theEglContext,
237 const Standard_Boolean theIsCoreProfile = Standard_False);
238 #elif defined(_WIN32)
239 //! Initialize class from specified window and rendering context. Method should be called only once.
240 //! @return false if OpenGL context can not be bound to specified window
241 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
242 const Aspect_Handle theWindowDC,
243 const Aspect_RenderingContext theGContext,
244 const Standard_Boolean theIsCoreProfile = Standard_False);
246 //! @return the window handle (HWND) currently bound to this OpenGL context
247 inline Aspect_Handle Window() const
252 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
253 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
255 //! Initialize class from specified OpenGL ES context (EAGLContext). Method should be called only once.
256 Standard_EXPORT Standard_Boolean Init (EAGLContext* theGContext,
257 const Standard_Boolean theIsCoreProfile = Standard_False);
260 //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
261 Standard_EXPORT Standard_Boolean Init (NSOpenGLContext* theGContext,
262 const Standard_Boolean theIsCoreProfile = Standard_False);
266 //! Initialize class from specified window and rendering context. Method should be called only once.
267 //! @return false if OpenGL context can not be bound to specified window
268 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
269 const Aspect_Display theDisplay,
270 const Aspect_RenderingContext theGContext,
271 const Standard_Boolean theIsCoreProfile = Standard_False);
273 //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
274 inline Aspect_Drawable Window() const
280 //! Read OpenGL version information from active context.
281 Standard_EXPORT static void ReadGlVersion (Standard_Integer& theGlVerMajor,
282 Standard_Integer& theGlVerMinor);
284 //! Check if theExtName extension is supported by active GL context.
285 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
287 //! Check if theExtName extension is in extensions string.
288 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
289 const char* theExtName);
291 //! Auxiliary template to retrieve GL function pointer.
292 //! Pointer to function retrieved from library is statically casted
293 //! to requested type - there no way to check real signature of exported function.
294 //! The context should be bound before call.
295 template <typename FuncType_t>
296 Standard_Boolean FindProc (const char* theFuncName,
297 FuncType_t& theFuncPtr)
299 theFuncPtr = (FuncType_t )findProc (theFuncName);
300 return (theFuncPtr != NULL);
303 //! @return true if detected GL version is greater or equal to requested one.
304 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
305 const Standard_Integer theVerMinor) const
307 return (myGlVerMajor > theVerMajor)
308 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
311 //! Return cached GL version major number.
312 Standard_Integer VersionMajor() const { return myGlVerMajor; }
314 //! Return cached GL version minor number.
315 Standard_Integer VersionMinor() const { return myGlVerMinor; }
317 //! Access entire map of loaded OpenGL functions.
318 const OpenGl_GlFunctions* Functions() const { return myFuncs.operator->(); }
320 //! Clean up errors stack for this GL context (glGetError() in loop).
321 Standard_EXPORT void ResetErrors (const bool theToPrintErrors = false);
323 //! This method uses system-dependent API to retrieve information
324 //! about GL context bound to the current thread.
325 //! @return true if current thread is bound to this GL context
326 Standard_EXPORT Standard_Boolean IsCurrent() const;
328 //! Activates current context.
329 //! Class should be initialized with appropriate info.
330 Standard_EXPORT Standard_Boolean MakeCurrent();
332 //! Swap front/back buffers for this GL context (should be activated before!).
333 Standard_EXPORT void SwapBuffers();
335 //! Setup swap interval (VSync).
336 Standard_EXPORT Standard_Boolean SetSwapInterval (const Standard_Integer theInterval);
338 //! Return true if active mode is GL_RENDER (cached state)
339 Standard_Boolean IsRender() const
341 #if !defined(GL_ES_VERSION_2_0)
342 return myRenderMode == GL_RENDER;
344 return Standard_True;
348 //! Return true if active mode is GL_FEEDBACK (cached state)
349 Standard_Boolean IsFeedback() const
351 #if !defined(GL_ES_VERSION_2_0)
352 return myRenderMode == GL_FEEDBACK;
354 return Standard_False;
358 //! This function retrieves information from GL about free GPU memory that is:
359 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
360 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
361 //! - Numbers meaning may vary.
362 //! You should use this info only for diagnostics purposes.
363 //! @return free GPU dedicated memory in bytes.
364 Standard_EXPORT Standard_Size AvailableMemory() const;
366 //! This function retrieves information from GL about GPU memory
367 //! and contains more vendor-specific values than AvailableMemory().
368 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
370 //! This function retrieves information from GL about GPU memory.
371 Standard_EXPORT void MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const;
373 //! Fill in the dictionary with OpenGL info.
374 //! Should be called with bound context.
375 Standard_EXPORT void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
376 Graphic3d_DiagnosticInfo theFlags) const;
378 //! Access shared resource by its name.
379 //! @param theKey - unique identifier;
380 //! @return handle to shared resource or NULL.
381 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
383 //! Access shared resource by its name.
384 //! @param theKey - unique identifier;
385 //! @param theValue - handle to fill;
386 //! @return true if resource was shared.
387 template<typename TheHandleType>
388 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
389 TheHandleType& theValue) const
391 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
392 if (aResource.IsNull())
394 return Standard_False;
397 theValue = TheHandleType::DownCast (aResource);
398 return !theValue.IsNull();
401 //! Register shared resource.
402 //! Notice that after registration caller shouldn't release it by himself -
403 //! it will be automatically released on context destruction.
404 //! @param theKey - unique identifier, shouldn't be empty;
405 //! @param theResource - new resource to register, shouldn't be NULL.
406 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
407 const Handle(OpenGl_Resource)& theResource);
409 //! Release shared resource.
410 //! If there are more than one reference to this resource
411 //! (also used by some other existing object) then call will be ignored.
412 //! This means that current object itself should nullify handle before this call.
413 //! Notice that this is unrecommended operation at all and should be used
414 //! only in case of fat resources to release memory for other needs.
415 //! @param theKey unique identifier
416 //! @param theToDelay postpone release until next redraw call
417 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
418 const Standard_Boolean theToDelay = Standard_False);
420 //! Append resource to queue for delayed clean up.
421 //! Resources in this queue will be released at next redraw call.
423 void DelayedRelease (Handle(T)& theResource)
425 myUnusedResources->Prepend (theResource);
426 theResource.Nullify();
429 //! Clean up the delayed release queue.
430 Standard_EXPORT void ReleaseDelayed();
432 //! @return tool for management of clippings within this context.
433 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
435 //! @return tool for management of clippings within this context.
436 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
438 //! @return tool for management of shader programs within this context.
439 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
443 //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
444 Standard_Integer TextureWrapClamp() const { return myTexClamp; }
446 //! @return maximum degree of anisotropy texture filter
447 Standard_Integer MaxDegreeOfAnisotropy() const { return myAnisoMax; }
449 //! @return value for GL_MAX_TEXTURE_SIZE
450 Standard_Integer MaxTextureSize() const { return myMaxTexDim; }
452 //! @return value for GL_MAX_SAMPLES
453 Standard_Integer MaxMsaaSamples() const { return myMaxMsaaSamples; }
455 //! Get maximum number of clip planes supported by OpenGl.
456 //! This value is implementation dependent. At least 6
457 //! planes should be supported by OpenGl (see specs).
458 //! @return value for GL_MAX_CLIP_PLANES
459 Standard_Integer MaxClipPlanes() const { return myMaxClipPlanes; }
461 //! Returns true if VBO is supported and permitted.
462 inline bool ToUseVbo() const
464 return core15fwd != NULL
465 && !caps->vboDisable;
468 //! @return cached state of GL_NORMALIZE.
469 Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
471 //! Sets GL_NORMALIZE enabled or disabled.
472 //! @return old value of the flag
473 Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
475 //! @return cached state of polygon rasterization mode (glPolygonMode()).
476 Standard_Integer PolygonMode() const { return myPolygonMode; }
478 //! Sets polygon rasterization mode (glPolygonMode() function).
479 //! @return old value of the rasterization mode.
480 Standard_EXPORT Standard_Integer SetPolygonMode (const Standard_Integer theMode);
482 //! @return cached enabled state of polygon hatching rasterization.
483 bool IsPolygonHatchEnabled() const
485 return !myHatchStyles.IsNull() && myHatchStyles->TypeOfHatch() != 0;
488 //! Sets enabled state of polygon hatching rasterization
489 //! without affecting currently selected hatching pattern.
490 //! @return previous state of polygon hatching mode.
491 Standard_EXPORT bool SetPolygonHatchEnabled (const bool theIsEnabled);
493 //! @return cached state of polygon hatch type.
494 Standard_Integer PolygonHatchStyle() const
496 return myHatchStyles.IsNull() ? Aspect_HS_SOLID : myHatchStyles->TypeOfHatch();
499 //! Sets polygon hatch pattern.
500 //! Zero-index value is a default alias for solid filling.
501 //! @param the type of hatch supported by base implementation of
502 //! OpenGl_LineAttributes (Aspect_HatchStyle) or the type supported by custom
503 //! implementation derived from OpenGl_LineAttributes class.
504 //! @return old type of hatch.
505 Standard_EXPORT Standard_Integer SetPolygonHatchStyle (const Standard_Integer theStyle);
507 //! Applies matrix stored in ModelWorldState to OpenGl.
508 Standard_EXPORT void ApplyModelWorldMatrix();
510 //! Applies matrix stored in WorldViewState to OpenGl.
511 Standard_EXPORT void ApplyWorldViewMatrix();
513 //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
514 Standard_EXPORT void ApplyModelViewMatrix();
516 //! Applies matrix stored in ProjectionState to OpenGl.
517 Standard_EXPORT void ApplyProjectionMatrix();
521 //! @return messenger instance
522 inline const Handle(Message_Messenger)& Messenger() const
524 return ::Message::DefaultMessenger();
527 //! Callback for GL_ARB_debug_output extension
528 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
529 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
530 //! @param theId message ID within source
531 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
532 //! @param theMessage the message itself
533 Standard_EXPORT void PushMessage (const unsigned int theSource,
534 const unsigned int theType,
535 const unsigned int theId,
536 const unsigned int theSeverity,
537 const TCollection_ExtendedString& theMessage);
539 //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
540 Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
541 const unsigned int theId);
543 //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
544 Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
545 const unsigned int theId);
547 //! @return true if OpenGl context supports left and
548 //! right rendering buffers.
549 Standard_Boolean HasStereoBuffers() const
551 #if !defined(GL_ES_VERSION_2_0)
552 return myIsStereoBuffers;
554 return Standard_False;
558 public: //! @name methods to alter or retrieve current state
560 //! Return cached viewport definition (x, y, width, height).
561 const Standard_Integer* Viewport() const { return myViewport; }
563 //! Resize the viewport (alias for glViewport).
564 //! @param theRect viewport definition (x, y, width, height)
565 Standard_EXPORT void ResizeViewport (const Standard_Integer theRect[4]);
567 //! Return active read buffer.
568 Standard_Integer ReadBuffer() { return myReadBuffer; }
570 //! Switch read buffer, wrapper for ::glReadBuffer().
571 Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
573 //! Return active draw buffer.
574 Standard_Integer DrawBuffer() { return myDrawBuffer; }
576 //! Switch draw buffer, wrapper for ::glDrawBuffer().
577 Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
579 //! Switch read/draw buffers.
580 void SetReadDrawBuffer (const Standard_Integer theBuffer)
582 SetReadBuffer (theBuffer);
583 SetDrawBuffer (theBuffer);
586 //! Return back face culling state.
587 bool ToCullBackFaces() const { return myToCullBackFaces; }
589 //! Enable or disable back face culling (glEnable (GL_CULL_FACE)).
590 Standard_EXPORT void SetCullBackFaces (bool theToEnable);
592 //! Fetch OpenGl context state. This class tracks value of several OpenGl
593 //! state variables. Consulting the cached values is quicker than
594 //! doing the same via OpenGl API. Call this method if any of the controlled
595 //! OpenGl state variables has a possibility of being out-of-date.
596 Standard_EXPORT void FetchState();
598 //! @return active GLSL program
599 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
601 return myActiveProgram;
604 //! @return OpenGL sampler object used to override default texture parameters
605 const Handle(OpenGl_Sampler)& TextureSampler()
610 //! Bind specified program to current context,
611 //! or unbind previous one when NULL specified.
612 //! @return true if some program is bound to context
613 Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
615 //! Setup current shading material.
616 Standard_EXPORT void SetShadingMaterial (const OpenGl_AspectFace* theAspect,
617 const OpenGl_Vec4* theHighlightColor = NULL);
619 //! Setup current color.
620 Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
622 //! Setup type of line.
623 Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine theType,
624 const Standard_ShortReal theFactor = 1.0f);
626 //! Setup width of line.
627 Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
629 //! Setup point size.
630 Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
632 //! Setup point sprite origin using GL_POINT_SPRITE_COORD_ORIGIN state:
633 //! - GL_UPPER_LEFT when GLSL program is active;
634 //! flipping should be handled in GLSL program for compatibility with OpenGL ES
635 //! - GL_LOWER_LEFT for FFP
636 Standard_EXPORT void SetPointSpriteOrigin();
638 //! Setup texture matrix to active GLSL program or to FFP global state using glMatrixMode (GL_TEXTURE).
639 Standard_EXPORT void SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams);
641 //! Bind default Vertex Array Object
642 Standard_EXPORT void BindDefaultVao();
644 //! Default Frame Buffer Object.
645 const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
650 //! Setup new Default Frame Buffer Object and return previously set.
651 //! This call doesn't change Active FBO!
652 Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
654 //! Return debug context initialization state.
655 Standard_Boolean IsDebugContext() const
657 return myIsGlDebugCtx;
660 Standard_EXPORT void EnableFeatures() const;
662 Standard_EXPORT void DisableFeatures() const;
664 //! Set resolution ratio.
665 //! Note that this method rounds @theRatio to nearest integer.
666 void SetResolutionRatio (const Standard_ShortReal theRatio)
668 myResolutionRatio = Max (1.0f, std::floor (theRatio + 0.5f));
673 //! Wrapper to system function to retrieve GL function pointer by name.
674 Standard_EXPORT void* findProc (const char* theFuncName);
676 //! Print error if not all functions have been exported by context for reported version.
677 //! Note that this will never happen when using GLX, since returned functions can not be validated.
678 //! @param theGlVerMajor the OpenGL major version with missing functions
679 //! @param theGlVerMinor the OpenGL minor version with missing functions
680 Standard_EXPORT void checkWrongVersion (const Standard_Integer theGlVerMajor,
681 const Standard_Integer theGlVerMinor);
683 //! Private initialization function that should be called only once.
684 Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
686 public: //! @name core profiles
688 OpenGl_GlCore11* core11; //!< OpenGL 1.1 core functionality
689 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
690 OpenGl_GlCore15* core15; //!< OpenGL 1.5 core functionality
691 OpenGl_GlCore15Fwd* core15fwd; //!< OpenGL 1.5 without deprecated entry points
692 OpenGl_GlCore20* core20; //!< OpenGL 2.0 core functionality (includes 1.5)
693 OpenGl_GlCore20Fwd* core20fwd; //!< OpenGL 2.0 without deprecated entry points
694 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
695 OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
696 OpenGl_GlCore33* core33; //!< OpenGL 3.3 core profile
697 OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
698 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
699 OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
700 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
701 OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
702 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
703 OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
704 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
705 OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
707 Handle(OpenGl_Caps) caps; //!< context options
709 public: //! @name extensions
711 Standard_Boolean hasHighp; //!< highp in GLSL ES fragment shader is supported
712 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)
713 Standard_Boolean hasTexRGBA8; //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
714 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
715 Standard_Boolean arbTexRG; //!< GL_ARB_texture_rg
716 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)
717 OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
718 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
719 Standard_Boolean arbTboRGB32; //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0
720 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
721 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
722 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
723 OpenGl_ArbFBOBlit* arbFBOBlit; //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
724 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
725 Standard_Boolean extBgra; //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
726 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
727 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
728 Standard_Boolean atiMem; //!< GL_ATI_meminfo
729 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
731 private: // system-dependent fields
733 #if defined(HAVE_EGL)
734 Aspect_Drawable myWindow; //!< EGL surface : EGLSurface
735 Aspect_Display myDisplay; //!< EGL connection to the Display : EGLDisplay
736 Aspect_RenderingContext myGContext; //!< EGL rendering context : EGLContext
737 #elif defined(_WIN32)
738 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
739 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
740 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
741 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
742 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
743 EAGLContext* myGContext; //!< Rendering Context handle
745 NSOpenGLContext* myGContext; //!< Rendering Context handle
748 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
749 Aspect_Display myDisplay; //!< connection to the X-server : Display*
750 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
753 private: // context info
755 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Standard_Integer> > OpenGl_DelayReleaseMap;
756 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> > OpenGl_ResourcesMap;
757 typedef NCollection_Shared< NCollection_List<Handle(OpenGl_Resource)> > OpenGl_ResourcesStack;
759 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
760 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
761 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
763 OpenGl_Clipping myClippingState; //!< state of clip planes
765 void* myGlLibHandle; //!< optional handle to GL library
766 NCollection_Handle<OpenGl_GlFunctions>
767 myFuncs; //!< mega structure for all GL functions
768 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
769 Standard_Integer myTexClamp; //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
770 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
771 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
772 Standard_Integer myMaxMsaaSamples; //!< value for GL_MAX_SAMPLES
773 Standard_Integer myGlVerMajor; //!< cached GL version major number
774 Standard_Integer myGlVerMinor; //!< cached GL version minor number
775 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
776 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
777 Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
778 //!< Used to tell OpenGl that normals should be normalized
780 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
782 private: //! @name fields tracking current state
784 Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
785 Handle(OpenGl_Sampler) myTexSampler; //!< currently active sampler object
786 Handle(OpenGl_FrameBuffer) myDefaultFbo; //!< default Frame Buffer Object
787 Handle(OpenGl_LineAttributes) myHatchStyles; //!< resource holding predefined hatch styles patterns
788 Standard_Integer myViewport[4]; //!< current viewport
789 Standard_Integer myPointSpriteOrig; //!< GL_POINT_SPRITE_COORD_ORIGIN state (GL_UPPER_LEFT by default)
790 Standard_Integer myRenderMode; //!< value for active rendering mode
791 Standard_Integer myPolygonMode; //!< currently used polygon rasterization mode (glPolygonMode)
792 bool myToCullBackFaces; //!< back face culling mode enabled state (glIsEnabled (GL_CULL_FACE))
793 Standard_Integer myReadBuffer; //!< current read buffer
794 Standard_Integer myDrawBuffer; //!< current draw buffer
795 unsigned int myDefaultVao; //!< default Vertex Array Object
796 Standard_Boolean myIsGlDebugCtx; //!< debug context initialization state
797 TCollection_AsciiString myVendor; //!< Graphics Driver's vendor
798 TColStd_PackedMapOfInteger myFilters[6]; //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
799 Standard_ShortReal myResolutionRatio; //!< scaling factor for parameters like text size
800 //!< to be properly displayed on device (screen / printer)
804 OpenGl_MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
805 OpenGl_MatrixState<Standard_ShortReal> WorldViewState; //!< state of orientation matrix
806 OpenGl_MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection matrix
810 //! Copying allowed only within Handles
811 OpenGl_Context (const OpenGl_Context& );
812 OpenGl_Context& operator= (const OpenGl_Context& );
816 DEFINE_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient) // Type definition
818 friend class OpenGl_Window;
822 #endif // _OpenGl_Context_H__