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_Vec.hxx>
31 #include <OpenGl_Resource.hxx>
32 #include <Standard_Transient.hxx>
33 #include <TCollection_AsciiString.hxx>
34 #include <TColStd_PackedMapOfInteger.hxx>
35 #include <OpenGl_Clipping.hxx>
36 #include <OpenGl_GlCore11.hxx>
37 #include <OpenGl_Utils.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();
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.
404 Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
406 //! Clean up the delayed release queue.
407 Standard_EXPORT void ReleaseDelayed();
409 //! @return tool for management of clippings within this context.
410 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
412 //! @return tool for management of clippings within this context.
413 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
415 //! @return tool for management of shader programs within this context.
416 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
420 //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
421 Standard_Integer TextureWrapClamp() const { return myTexClamp; }
423 //! @return maximum degree of anisotropy texture filter
424 Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
426 //! @return value for GL_MAX_TEXTURE_SIZE
427 Standard_EXPORT Standard_Integer MaxTextureSize() const;
429 //! Get maximum number of clip planes supported by OpenGl.
430 //! This value is implementation dependent. At least 6
431 //! planes should be supported by OpenGl (see specs).
432 //! @return value for GL_MAX_CLIP_PLANES
433 Standard_EXPORT Standard_Integer MaxClipPlanes() const;
435 //! Returns true if VBO is supported and permitted.
436 inline bool ToUseVbo() const
438 return core15fwd != NULL
439 && !caps->vboDisable;
442 //! @return cached state of GL_NORMALIZE.
443 Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
445 //! Sets GL_NORMALIZE enabled or disabled.
446 //! @return old value of the flag
447 Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
449 //! Applies matrix stored in ModelWorldState to OpenGl.
450 Standard_EXPORT void ApplyModelWorldMatrix();
452 //! Applies matrix stored in WorldViewState to OpenGl.
453 Standard_EXPORT void ApplyWorldViewMatrix();
455 //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
456 Standard_EXPORT void ApplyModelViewMatrix();
458 //! Applies matrix stored in ProjectionState to OpenGl.
459 Standard_EXPORT void ApplyProjectionMatrix();
463 //! @return messenger instance
464 inline const Handle(Message_Messenger)& Messenger() const
466 return ::Message::DefaultMessenger();
469 //! Callback for GL_ARB_debug_output extension
470 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
471 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
472 //! @param theId message ID within source
473 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
474 //! @param theMessage the message itself
475 Standard_EXPORT void PushMessage (const unsigned int theSource,
476 const unsigned int theType,
477 const unsigned int theId,
478 const unsigned int theSeverity,
479 const TCollection_ExtendedString& theMessage);
481 //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
482 Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
483 const unsigned int theId);
485 //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
486 Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
487 const unsigned int theId);
489 //! @return true if OpenGl context supports left and
490 //! right rendering buffers.
491 Standard_Boolean HasStereoBuffers() const
493 #if !defined(GL_ES_VERSION_2_0)
494 return myIsStereoBuffers;
496 return Standard_False;
500 public: //! @name methods to alter or retrieve current state
502 //! Return active read buffer.
503 Standard_Integer ReadBuffer() { return myReadBuffer; }
505 //! Switch read buffer, wrapper for ::glReadBuffer().
506 Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
508 //! Return active draw buffer.
509 Standard_Integer DrawBuffer() { return myDrawBuffer; }
511 //! Switch draw buffer, wrapper for ::glDrawBuffer().
512 Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
514 //! Switch read/draw buffers.
515 void SetReadDrawBuffer (const Standard_Integer theBuffer)
517 SetReadBuffer (theBuffer);
518 SetDrawBuffer (theBuffer);
521 //! Fetch OpenGl context state. This class tracks value of several OpenGl
522 //! state variables. Consulting the cached values is quicker than
523 //! doing the same via OpenGl API. Call this method if any of the controlled
524 //! OpenGl state variables has a possibility of being out-of-date.
525 Standard_EXPORT void FetchState();
527 //! @return active GLSL program
528 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
530 return myActiveProgram;
533 //! @return OpenGL sampler object used to override default texture parameters
534 const Handle(OpenGl_Sampler)& TextureSampler()
539 //! Bind specified program to current context,
540 //! or unbind previous one when NULL specified.
541 //! @return true if some program is bound to context
542 Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
544 //! Setup current color.
545 Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
547 //! Setup type of line.
548 Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine theType,
549 const Standard_ShortReal theFactor = 1.0f);
551 //! Setup width of line.
552 Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
554 //! Setup point size.
555 Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
557 //! Bind default Vertex Array Object
558 Standard_EXPORT void BindDefaultVao();
560 //! Default Frame Buffer Object.
561 const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
566 //! Setup new Default Frame Buffer Object and return previously set.
567 //! This call doesn't change Active FBO!
568 Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
570 //! Return debug context initialization state.
571 Standard_Boolean IsDebugContext() const
573 return myIsGlDebugCtx;
578 //! Wrapper to system function to retrieve GL function pointer by name.
579 Standard_EXPORT void* findProc (const char* theFuncName);
581 //! Print error if not all functions have been exported by context for reported version.
582 //! Note that this will never happen when using GLX, since returned functions can not be validated.
583 //! @param theGlVerMajor the OpenGL major version with missing functions
584 //! @param theGlVerMinor the OpenGL minor version with missing functions
585 Standard_EXPORT void checkWrongVersion (const Standard_Integer theGlVerMajor,
586 const Standard_Integer theGlVerMinor);
588 //! Private initialization function that should be called only once.
589 Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
591 public: //! @name core profiles
593 OpenGl_GlCore11* core11; //!< OpenGL 1.1 core functionality
594 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
595 OpenGl_GlCore15* core15; //!< OpenGL 1.5 core functionality
596 OpenGl_GlCore15Fwd* core15fwd; //!< OpenGL 1.5 without deprecated entry points
597 OpenGl_GlCore20* core20; //!< OpenGL 2.0 core functionality (includes 1.5)
598 OpenGl_GlCore20Fwd* core20fwd; //!< OpenGL 2.0 without deprecated entry points
599 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
600 OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
601 OpenGl_GlCore33* core33; //!< OpenGL 3.3 core profile
602 OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
603 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
604 OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
605 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
606 OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
607 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
608 OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
609 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
610 OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
612 Handle(OpenGl_Caps) caps; //!< context options
614 public: //! @name extensions
616 Standard_Boolean hasHighp; //!< highp in GLSL ES fragment shader is supported
617 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)
618 Standard_Boolean hasTexRGBA8; //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
619 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
620 Standard_Boolean arbTexRG; //!< GL_ARB_texture_rg
621 OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
622 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
623 Standard_Boolean arbTboRGB32; //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0
624 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
625 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
626 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
627 OpenGl_ArbFBOBlit* arbFBOBlit; //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
628 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
629 Standard_Boolean extBgra; //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
630 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
631 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
632 Standard_Boolean atiMem; //!< GL_ATI_meminfo
633 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
635 private: // system-dependent fields
637 #if defined(HAVE_EGL)
638 Aspect_Drawable myWindow; //!< EGL surface : EGLSurface
639 Aspect_Display myDisplay; //!< EGL connection to the Display : EGLDisplay
640 Aspect_RenderingContext myGContext; //!< EGL rendering context : EGLContext
641 #elif defined(_WIN32)
642 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
643 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
644 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
645 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
646 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
647 EAGLContext* myGContext; //!< Rendering Context handle
649 NSOpenGLContext* myGContext; //!< Rendering Context handle
652 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
653 Aspect_Display myDisplay; //!< connection to the X-server : Display*
654 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
657 private: // context info
659 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Standard_Integer> > OpenGl_DelayReleaseMap;
660 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> > OpenGl_ResourcesMap;
661 typedef NCollection_Shared< NCollection_List<Handle(OpenGl_Resource)> > OpenGl_ResourcesStack;
663 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
664 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
665 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
667 OpenGl_Clipping myClippingState; //!< state of clip planes
669 void* myGlLibHandle; //!< optional handle to GL library
670 NCollection_Handle<OpenGl_GlFunctions>
671 myFuncs; //!< mega structure for all GL functions
672 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
673 Standard_Integer myTexClamp; //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
674 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
675 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
676 Standard_Integer myGlVerMajor; //!< cached GL version major number
677 Standard_Integer myGlVerMinor; //!< cached GL version minor number
678 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
679 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
680 Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
681 //!< Used to tell OpenGl that normals should be normalized
683 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
685 private: //! @name fields tracking current state
687 Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
688 Handle(OpenGl_Sampler) myTexSampler; //!< currently active sampler object
689 Handle(OpenGl_FrameBuffer) myDefaultFbo; //!< default Frame Buffer Object
690 Standard_Integer myRenderMode; //!< value for active rendering mode
691 Standard_Integer myReadBuffer; //!< current read buffer
692 Standard_Integer myDrawBuffer; //!< current draw buffer
693 unsigned int myDefaultVao; //!< default Vertex Array Object
694 Standard_Boolean myIsGlDebugCtx; //!< debug context initialization state
695 TCollection_AsciiString myVendor; //!< Graphics Driver's vendor
696 TColStd_PackedMapOfInteger myFilters[6]; //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
700 OpenGl_Utils::MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
701 OpenGl_Utils::MatrixState<Standard_ShortReal> WorldViewState; //!< state of orientation matrix
702 OpenGl_Utils::MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection matrix
706 //! Copying allowed only within Handles
707 OpenGl_Context (const OpenGl_Context& );
708 OpenGl_Context& operator= (const OpenGl_Context& );
712 DEFINE_STANDARD_RTTI(OpenGl_Context, Standard_Transient) // Type definition
714 friend class OpenGl_Window;
718 #endif // _OpenGl_Context_H__