0025695: Visualization, AIS_InteractiveContext - define default HilightMode
[occt.git] / src / OpenGl / OpenGl_Context.hxx
CommitLineData
6aca4d39 1// Created on: 2012-01-26
b311480e 2// Created by: Kirill GAVRILOV
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
2166f0fa
SK
16#ifndef _OpenGl_Context_H__
17#define _OpenGl_Context_H__
18
2bd4c032 19#include <Aspect_Handle.hxx>
6d0e6be5 20#include <Aspect_HatchStyle.hxx>
2bd4c032 21#include <Aspect_Drawable.hxx>
22#include <Aspect_Display.hxx>
23#include <Aspect_RenderingContext.hxx>
ac116c22 24#include <Aspect_TypeOfLine.hxx>
5e27df78 25#include <NCollection_DataMap.hxx>
26d9c835 26#include <Graphic3d_DiagnosticInfo.hxx>
4269bd1b 27#include <NCollection_Map.hxx>
5e27df78 28#include <NCollection_Handle.hxx>
3125ebb6 29#include <NCollection_List.hxx>
cbf18624 30#include <Message.hxx>
58655684 31#include <OpenGl_Caps.hxx>
6d0e6be5 32#include <OpenGl_LineAttributes.hxx>
825aa485 33#include <OpenGl_MatrixState.hxx>
8625ef7e 34#include <OpenGl_Vec.hxx>
5e27df78 35#include <OpenGl_Resource.hxx>
2166f0fa 36#include <Standard_Transient.hxx>
26d9c835 37#include <TColStd_IndexedDataMapOfStringString.hxx>
c87535af 38#include <TColStd_PackedMapOfInteger.hxx>
30f0ad28 39#include <OpenGl_Clipping.hxx>
b5ac8292 40#include <OpenGl_GlCore11.hxx>
c04c30b3 41#include <OpenGl_ShaderProgram.hxx>
42
43#include <NCollection_Shared.hxx>
2166f0fa 44
5f8b738e 45//! Forward declarations
a2e4f780 46#if defined(__APPLE__)
47 #import <TargetConditionals.h>
48 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
49 #ifdef __OBJC__
50 @class EAGLContext;
51 #else
52 struct EAGLContext;
53 #endif
54 #else
55 #ifdef __OBJC__
56 @class NSOpenGLContext;
57 #else
58 struct NSOpenGLContext;
59 #endif
60 #endif
61#endif
62
01ca42b2 63struct OpenGl_GlFunctions;
5e27df78 64struct OpenGl_ArbTBO;
65struct OpenGl_ArbIns;
58655684 66struct OpenGl_ArbDbg;
01ca42b2 67struct OpenGl_ArbFBO;
b86bb3df 68struct OpenGl_ArbFBOBlit;
37eb4787 69struct OpenGl_ExtGS;
25ef750e 70struct OpenGl_ArbTexBindless;
2166f0fa 71
01ca42b2 72template<typename theBaseClass_t> struct OpenGl_TmplCore12;
73typedef OpenGl_TmplCore12<OpenGl_GlCore11> OpenGl_GlCore12;
74typedef OpenGl_TmplCore12<OpenGl_GlCore11Fwd> OpenGl_GlCore12Fwd;
75
76struct OpenGl_GlCore13;
77struct OpenGl_GlCore13Fwd;
78
79template<typename theBaseClass_t> struct OpenGl_TmplCore14;
80typedef OpenGl_TmplCore14<OpenGl_GlCore13> OpenGl_GlCore14;
81typedef OpenGl_TmplCore14<OpenGl_GlCore13Fwd> OpenGl_GlCore14Fwd;
82
83template<typename theBaseClass_t> struct OpenGl_TmplCore15;
84typedef OpenGl_TmplCore15<OpenGl_GlCore14> OpenGl_GlCore15;
85typedef OpenGl_TmplCore15<OpenGl_GlCore14Fwd> OpenGl_GlCore15Fwd;
86
87template<typename theBaseClass_t> struct OpenGl_TmplCore20;
88typedef OpenGl_TmplCore20<OpenGl_GlCore15> OpenGl_GlCore20;
89typedef OpenGl_TmplCore20<OpenGl_GlCore15Fwd> OpenGl_GlCore20Fwd;
90
91template<typename theBaseClass_t> struct OpenGl_TmplCore21;
92typedef OpenGl_TmplCore21<OpenGl_GlCore20> OpenGl_GlCore21;
93typedef OpenGl_TmplCore21<OpenGl_GlCore20Fwd> OpenGl_GlCore21Fwd;
94
95template<typename theBaseClass_t> struct OpenGl_TmplCore30;
96typedef OpenGl_TmplCore30<OpenGl_GlCore21> OpenGl_GlCore30;
97typedef OpenGl_TmplCore30<OpenGl_GlCore21Fwd> OpenGl_GlCore30Fwd;
98
99template<typename theBaseClass_t> struct OpenGl_TmplCore31;
100typedef OpenGl_TmplCore31<OpenGl_GlCore30> OpenGl_GlCore31Back;
101typedef OpenGl_TmplCore31<OpenGl_GlCore30Fwd> OpenGl_GlCore31;
102
103template<typename theBaseClass_t> struct OpenGl_TmplCore32;
104typedef OpenGl_TmplCore32<OpenGl_GlCore31Back> OpenGl_GlCore32Back;
105typedef OpenGl_TmplCore32<OpenGl_GlCore31> OpenGl_GlCore32;
106
107template<typename theBaseClass_t> struct OpenGl_TmplCore33;
108typedef OpenGl_TmplCore33<OpenGl_GlCore32Back> OpenGl_GlCore33Back;
109typedef OpenGl_TmplCore33<OpenGl_GlCore32> OpenGl_GlCore33;
110
111template<typename theBaseClass_t> struct OpenGl_TmplCore40;
112typedef OpenGl_TmplCore40<OpenGl_GlCore33Back> OpenGl_GlCore40Back;
113typedef OpenGl_TmplCore40<OpenGl_GlCore33> OpenGl_GlCore40;
114
115template<typename theBaseClass_t> struct OpenGl_TmplCore41;
116typedef OpenGl_TmplCore41<OpenGl_GlCore40Back> OpenGl_GlCore41Back;
117typedef OpenGl_TmplCore41<OpenGl_GlCore40> OpenGl_GlCore41;
118
119template<typename theBaseClass_t> struct OpenGl_TmplCore42;
120typedef OpenGl_TmplCore42<OpenGl_GlCore41Back> OpenGl_GlCore42Back;
121typedef OpenGl_TmplCore42<OpenGl_GlCore41> OpenGl_GlCore42;
122
123template<typename theBaseClass_t> struct OpenGl_TmplCore43;
124typedef OpenGl_TmplCore43<OpenGl_GlCore42Back> OpenGl_GlCore43Back;
125typedef OpenGl_TmplCore43<OpenGl_GlCore42> OpenGl_GlCore43;
126
127template<typename theBaseClass_t> struct OpenGl_TmplCore44;
128typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
129typedef OpenGl_TmplCore44<OpenGl_GlCore43> OpenGl_GlCore44;
130
c04c30b3 131class OpenGl_ShaderManager;
132class OpenGl_Sampler;
133class OpenGl_FrameBuffer;
299e0ab9 134class OpenGl_AspectFace;
f838dac4 135class Graphic3d_PresentationAttributes;
c04c30b3 136
c04c30b3 137DEFINE_STANDARD_HANDLE(OpenGl_Context, Standard_Transient)
138
5f8b738e 139//! This class generalize access to the GL context and available extensions.
140//!
4e1523ef 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:
5f8b738e 143//! @code
144//! if (myContext->core20 != NULL)
145//! {
146//! myGlProgram = myContext->core20->glCreateProgram();
147//! .. do more stuff ..
148//! }
149//! else
150//! {
151//! .. compatibility with outdated configurations ..
152//! }
153//! @endcode
154//!
4e1523ef 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.).
157//!
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.
166//!
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)".
5f8b738e 173//!
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).
182//!
4e1523ef 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.
2166f0fa
SK
185class OpenGl_Context : public Standard_Transient
186{
187public:
188
a174a3c5 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)
195 {
196 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
197 {
198 if (theNumber <= p2)
199 {
200 return p2;
201 }
202 }
203 return theThreshold;
204 }
205
206public:
207
5f8b738e 208 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
58655684 209 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
5f8b738e 210
211 //! Destructor.
212 Standard_EXPORT virtual ~OpenGl_Context();
2166f0fa 213
05e2200b 214 //! Release all resources, including shared ones
215 Standard_EXPORT void forcedRelease();
216
5e27df78 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);
220
7e7c2f0b 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
4e1523ef 223 Standard_EXPORT Standard_Boolean Init (const Standard_Boolean theIsCoreProfile = Standard_False);
2166f0fa 224
fd4a6963 225 //! @return true if this context is valid (has been initialized)
226 inline Standard_Boolean IsValid() const
227 {
228 return myIsInitialized;
229 }
230
da8bb41d 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,
4e1523ef 236 const Aspect_RenderingContext theEglContext,
237 const Standard_Boolean theIsCoreProfile = Standard_False);
da8bb41d 238#elif defined(_WIN32)
7e7c2f0b 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
f0430952 241 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
242 const Aspect_Handle theWindowDC,
4e1523ef 243 const Aspect_RenderingContext theGContext,
244 const Standard_Boolean theIsCoreProfile = Standard_False);
7e7c2f0b 245
246 //! @return the window handle (HWND) currently bound to this OpenGL context
247 inline Aspect_Handle Window() const
248 {
249 return myWindow;
250 }
251
4fe56619 252#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
a2e4f780 253 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
254
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);
258 #else
259
7e7c2f0b 260 //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
a2e4f780 261 Standard_EXPORT Standard_Boolean Init (NSOpenGLContext* theGContext,
4e1523ef 262 const Standard_Boolean theIsCoreProfile = Standard_False);
a2e4f780 263 #endif
2bd4c032 264#else
a2e4f780 265
7e7c2f0b 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
f0430952 268 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
269 const Aspect_Display theDisplay,
4e1523ef 270 const Aspect_RenderingContext theGContext,
271 const Standard_Boolean theIsCoreProfile = Standard_False);
7e7c2f0b 272
273 //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
274 inline Aspect_Drawable Window() const
275 {
276 return myWindow;
277 }
2bd4c032 278#endif
279
4e1523ef 280 //! Read OpenGL version information from active context.
281 Standard_EXPORT static void ReadGlVersion (Standard_Integer& theGlVerMajor,
282 Standard_Integer& theGlVerMinor);
283
5f8b738e 284 //! Check if theExtName extension is supported by active GL context.
2bd4c032 285 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
2166f0fa 286
58655684 287 //! Check if theExtName extension is in extensions string.
288 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
289 const char* theExtName);
290
2166f0fa 291 //! Auxiliary template to retrieve GL function pointer.
5f8b738e 292 //! Pointer to function retrieved from library is statically casted
293 //! to requested type - there no way to check real signature of exported function.
2166f0fa
SK
294 //! The context should be bound before call.
295 template <typename FuncType_t>
5f8b738e 296 Standard_Boolean FindProc (const char* theFuncName,
297 FuncType_t& theFuncPtr)
2166f0fa 298 {
5f8b738e 299 theFuncPtr = (FuncType_t )findProc (theFuncName);
2166f0fa
SK
300 return (theFuncPtr != NULL);
301 }
302
2bd4c032 303 //! @return true if detected GL version is greater or equal to requested one.
304 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
7e7c2f0b 305 const Standard_Integer theVerMinor) const
5f8b738e 306 {
307 return (myGlVerMajor > theVerMajor)
308 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
309 }
310
26d9c835 311 //! Return cached GL version major number.
312 Standard_Integer VersionMajor() const { return myGlVerMajor; }
313
314 //! Return cached GL version minor number.
315 Standard_Integer VersionMinor() const { return myGlVerMinor; }
316
f978241f 317 //! Access entire map of loaded OpenGL functions.
318 const OpenGl_GlFunctions* Functions() const { return myFuncs.operator->(); }
319
5f8b738e 320 //! Clean up errors stack for this GL context (glGetError() in loop).
f9f740d6 321 //! @return true if some error has been cleared
322 Standard_EXPORT bool ResetErrors (const bool theToPrintErrors = false);
5f8b738e 323
86fa64d9 324 //! This method uses system-dependent API to retrieve information
325 //! about GL context bound to the current thread.
326 //! @return true if current thread is bound to this GL context
327 Standard_EXPORT Standard_Boolean IsCurrent() const;
328
2bd4c032 329 //! Activates current context.
330 //! Class should be initialized with appropriate info.
331 Standard_EXPORT Standard_Boolean MakeCurrent();
332
5e27df78 333 //! Swap front/back buffers for this GL context (should be activated before!).
334 Standard_EXPORT void SwapBuffers();
335
f978241f 336 //! Setup swap interval (VSync).
337 Standard_EXPORT Standard_Boolean SetSwapInterval (const Standard_Integer theInterval);
338
b5ac8292 339 //! Return true if active mode is GL_RENDER (cached state)
340 Standard_Boolean IsRender() const
341 {
ca3c13d1 342 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 343 return myRenderMode == GL_RENDER;
ca3c13d1 344 #else
345 return Standard_True;
346 #endif
b5ac8292 347 }
664cae74 348
b5ac8292 349 //! Return true if active mode is GL_FEEDBACK (cached state)
350 Standard_Boolean IsFeedback() const
351 {
ca3c13d1 352 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 353 return myRenderMode == GL_FEEDBACK;
ca3c13d1 354 #else
355 return Standard_False;
356 #endif
b5ac8292 357 }
664cae74 358
f0430952 359 //! This function retrieves information from GL about free GPU memory that is:
360 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
361 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
362 //! - Numbers meaning may vary.
363 //! You should use this info only for diagnostics purposes.
364 //! @return free GPU dedicated memory in bytes.
365 Standard_EXPORT Standard_Size AvailableMemory() const;
366
367 //! This function retrieves information from GL about GPU memory
368 //! and contains more vendor-specific values than AvailableMemory().
369 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
370
26d9c835 371 //! This function retrieves information from GL about GPU memory.
372 Standard_EXPORT void MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const;
373
374 //! Fill in the dictionary with OpenGL info.
375 //! Should be called with bound context.
376 Standard_EXPORT void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
377 Graphic3d_DiagnosticInfo theFlags) const;
378
5e27df78 379 //! Access shared resource by its name.
380 //! @param theKey - unique identifier;
381 //! @return handle to shared resource or NULL.
382 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
383
384 //! Access shared resource by its name.
385 //! @param theKey - unique identifier;
386 //! @param theValue - handle to fill;
387 //! @return true if resource was shared.
388 template<typename TheHandleType>
389 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
390 TheHandleType& theValue) const
391 {
392 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
393 if (aResource.IsNull())
394 {
395 return Standard_False;
396 }
397
398 theValue = TheHandleType::DownCast (aResource);
399 return !theValue.IsNull();
400 }
401
402 //! Register shared resource.
403 //! Notice that after registration caller shouldn't release it by himself -
404 //! it will be automatically released on context destruction.
405 //! @param theKey - unique identifier, shouldn't be empty;
406 //! @param theResource - new resource to register, shouldn't be NULL.
407 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
408 const Handle(OpenGl_Resource)& theResource);
409
410 //! Release shared resource.
411 //! If there are more than one reference to this resource
412 //! (also used by some other existing object) then call will be ignored.
413 //! This means that current object itself should nullify handle before this call.
414 //! Notice that this is unrecommended operation at all and should be used
415 //! only in case of fat resources to release memory for other needs.
a174a3c5 416 //! @param theKey unique identifier
417 //! @param theToDelay postpone release until next redraw call
418 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
419 const Standard_Boolean theToDelay = Standard_False);
5e27df78 420
421 //! Append resource to queue for delayed clean up.
422 //! Resources in this queue will be released at next redraw call.
aa00364d 423 template <class T>
424 void DelayedRelease (Handle(T)& theResource)
425 {
426 myUnusedResources->Prepend (theResource);
427 theResource.Nullify();
428 }
5e27df78 429
430 //! Clean up the delayed release queue.
431 Standard_EXPORT void ReleaseDelayed();
432
4269bd1b 433 //! @return tool for management of clippings within this context.
30f0ad28 434 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
4269bd1b 435
436 //! @return tool for management of clippings within this context.
30f0ad28 437 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
438
439 //! @return tool for management of shader programs within this context.
440 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
4269bd1b 441
442public:
443
ca3c13d1 444 //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
445 Standard_Integer TextureWrapClamp() const { return myTexClamp; }
446
bf75be98 447 //! @return maximum degree of anisotropy texture filter
3c4b62a4 448 Standard_Integer MaxDegreeOfAnisotropy() const { return myAnisoMax; }
bf75be98 449
450 //! @return value for GL_MAX_TEXTURE_SIZE
3c4b62a4 451 Standard_Integer MaxTextureSize() const { return myMaxTexDim; }
452
453 //! @return value for GL_MAX_SAMPLES
454 Standard_Integer MaxMsaaSamples() const { return myMaxMsaaSamples; }
bf75be98 455
4269bd1b 456 //! Get maximum number of clip planes supported by OpenGl.
7e7c2f0b 457 //! This value is implementation dependent. At least 6
4269bd1b 458 //! planes should be supported by OpenGl (see specs).
459 //! @return value for GL_MAX_CLIP_PLANES
3c4b62a4 460 Standard_Integer MaxClipPlanes() const { return myMaxClipPlanes; }
4269bd1b 461
3a9b5dc8 462 //! @return TRUE if ray tracing mode is supported
463 Standard_Boolean HasRayTracing() const { return myHasRayTracing; }
464
465 //! @return TRUE if textures in ray tracing mode are supported
466 Standard_Boolean HasRayTracingTextures() const { return myHasRayTracingTextures; }
467
468 //! @return TRUE if adaptive screen sampling in ray tracing mode is supported
469 Standard_Boolean HasRayTracingAdaptiveSampling() const { return myHasRayTracingAdaptiveSampling; }
470
7d3e64ef 471 //! Returns true if VBO is supported and permitted.
472 inline bool ToUseVbo() const
473 {
474 return core15fwd != NULL
475 && !caps->vboDisable;
476 }
477
7d9e854b 478 //! @return cached state of GL_NORMALIZE.
479 Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
480
481 //! Sets GL_NORMALIZE enabled or disabled.
482 //! @return old value of the flag
483 Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
484
6d0e6be5 485 //! @return cached state of polygon rasterization mode (glPolygonMode()).
486 Standard_Integer PolygonMode() const { return myPolygonMode; }
487
488 //! Sets polygon rasterization mode (glPolygonMode() function).
489 //! @return old value of the rasterization mode.
490 Standard_EXPORT Standard_Integer SetPolygonMode (const Standard_Integer theMode);
491
492 //! @return cached enabled state of polygon hatching rasterization.
493 bool IsPolygonHatchEnabled() const
494 {
495 return !myHatchStyles.IsNull() && myHatchStyles->TypeOfHatch() != 0;
496 }
497
498 //! Sets enabled state of polygon hatching rasterization
499 //! without affecting currently selected hatching pattern.
500 //! @return previous state of polygon hatching mode.
501 Standard_EXPORT bool SetPolygonHatchEnabled (const bool theIsEnabled);
502
503 //! @return cached state of polygon hatch type.
504 Standard_Integer PolygonHatchStyle() const
505 {
506 return myHatchStyles.IsNull() ? Aspect_HS_SOLID : myHatchStyles->TypeOfHatch();
507 }
508
509 //! Sets polygon hatch pattern.
510 //! Zero-index value is a default alias for solid filling.
511 //! @param the type of hatch supported by base implementation of
512 //! OpenGl_LineAttributes (Aspect_HatchStyle) or the type supported by custom
513 //! implementation derived from OpenGl_LineAttributes class.
514 //! @return old type of hatch.
640d5fe2 515 Standard_EXPORT Standard_Integer SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle);
6d0e6be5 516
c827ea3a 517 //! Applies matrix stored in ModelWorldState to OpenGl.
5e94009d 518 Standard_EXPORT void ApplyModelWorldMatrix();
c827ea3a 519
520 //! Applies matrix stored in WorldViewState to OpenGl.
5e94009d 521 Standard_EXPORT void ApplyWorldViewMatrix();
c827ea3a 522
523 //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
5e94009d 524 Standard_EXPORT void ApplyModelViewMatrix();
c827ea3a 525
526 //! Applies matrix stored in ProjectionState to OpenGl.
5e94009d 527 Standard_EXPORT void ApplyProjectionMatrix();
c827ea3a 528
cbf18624 529public:
530
7e7c2f0b 531 //! @return messenger instance
857ffd5e 532 inline const Handle(Message_Messenger)& Messenger() const
cbf18624 533 {
8fa64b52 534 return ::Message::DefaultMessenger();
cbf18624 535 }
536
537 //! Callback for GL_ARB_debug_output extension
538 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
539 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
540 //! @param theId message ID within source
541 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
542 //! @param theMessage the message itself
543 Standard_EXPORT void PushMessage (const unsigned int theSource,
544 const unsigned int theType,
545 const unsigned int theId,
546 const unsigned int theSeverity,
547 const TCollection_ExtendedString& theMessage);
548
c87535af 549 //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
550 Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
551 const unsigned int theId);
b5ac8292 552
c87535af 553 //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
554 Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
555 const unsigned int theId);
b5ac8292 556
557 //! @return true if OpenGl context supports left and
558 //! right rendering buffers.
559 Standard_Boolean HasStereoBuffers() const
560 {
ca3c13d1 561 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 562 return myIsStereoBuffers;
ca3c13d1 563 #else
564 return Standard_False;
565 #endif
b5ac8292 566 }
567
7d3e64ef 568public: //! @name methods to alter or retrieve current state
569
3bffef55 570 //! Return cached viewport definition (x, y, width, height).
571 const Standard_Integer* Viewport() const { return myViewport; }
572
573 //! Resize the viewport (alias for glViewport).
574 //! @param theRect viewport definition (x, y, width, height)
575 Standard_EXPORT void ResizeViewport (const Standard_Integer theRect[4]);
576
38a0206f 577 //! Return active read buffer.
578 Standard_Integer ReadBuffer() { return myReadBuffer; }
b5ac8292 579
38a0206f 580 //! Switch read buffer, wrapper for ::glReadBuffer().
581 Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
b5ac8292 582
38a0206f 583 //! Return active draw buffer.
584 Standard_Integer DrawBuffer() { return myDrawBuffer; }
585
586 //! Switch draw buffer, wrapper for ::glDrawBuffer().
587 Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
588
589 //! Switch read/draw buffers.
590 void SetReadDrawBuffer (const Standard_Integer theBuffer)
591 {
592 SetReadBuffer (theBuffer);
593 SetDrawBuffer (theBuffer);
594 }
b5ac8292 595
b6472664 596 //! Return back face culling state.
597 bool ToCullBackFaces() const { return myToCullBackFaces; }
598
599 //! Enable or disable back face culling (glEnable (GL_CULL_FACE)).
600 Standard_EXPORT void SetCullBackFaces (bool theToEnable);
601
b5ac8292 602 //! Fetch OpenGl context state. This class tracks value of several OpenGl
603 //! state variables. Consulting the cached values is quicker than
604 //! doing the same via OpenGl API. Call this method if any of the controlled
605 //! OpenGl state variables has a possibility of being out-of-date.
606 Standard_EXPORT void FetchState();
607
7d3e64ef 608 //! @return active GLSL program
609 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
610 {
611 return myActiveProgram;
612 }
613
25ef750e 614 //! @return OpenGL sampler object used to override default texture parameters
615 const Handle(OpenGl_Sampler)& TextureSampler()
616 {
617 return myTexSampler;
618 }
619
7d3e64ef 620 //! Bind specified program to current context,
621 //! or unbind previous one when NULL specified.
8625ef7e 622 //! @return true if some program is bound to context
623 Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
624
299e0ab9 625 //! Setup current shading material.
f9ba5c4d 626 Standard_EXPORT void SetShadingMaterial (const OpenGl_AspectFace* theAspect,
f838dac4 627 const Handle(Graphic3d_PresentationAttributes)& theHighlight = Handle(Graphic3d_PresentationAttributes)());
299e0ab9 628
8625ef7e 629 //! Setup current color.
630 Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
631
ac116c22 632 //! Setup type of line.
633 Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine theType,
634 const Standard_ShortReal theFactor = 1.0f);
635
636 //! Setup width of line.
637 Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
638
8625ef7e 639 //! Setup point size.
640 Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
7d3e64ef 641
fd59283a 642 //! Setup point sprite origin using GL_POINT_SPRITE_COORD_ORIGIN state:
643 //! - GL_UPPER_LEFT when GLSL program is active;
644 //! flipping should be handled in GLSL program for compatibility with OpenGL ES
645 //! - GL_LOWER_LEFT for FFP
646 Standard_EXPORT void SetPointSpriteOrigin();
647
79f4f036 648 //! Setup texture matrix to active GLSL program or to FFP global state using glMatrixMode (GL_TEXTURE).
649 Standard_EXPORT void SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams);
650
4e1523ef 651 //! Bind default Vertex Array Object
652 Standard_EXPORT void BindDefaultVao();
653
a2e4f780 654 //! Default Frame Buffer Object.
655 const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
656 {
657 return myDefaultFbo;
658 }
659
660 //! Setup new Default Frame Buffer Object and return previously set.
661 //! This call doesn't change Active FBO!
662 Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
663
4e1523ef 664 //! Return debug context initialization state.
665 Standard_Boolean IsDebugContext() const
666 {
667 return myIsGlDebugCtx;
668 }
669
c357e426 670 Standard_EXPORT void EnableFeatures() const;
671
672 Standard_EXPORT void DisableFeatures() const;
673
75c262a9 674 //! Set resolution ratio.
675 //! Note that this method rounds @theRatio to nearest integer.
676 void SetResolutionRatio (const Standard_ShortReal theRatio)
677 {
678 myResolutionRatio = Max (1.0f, std::floor (theRatio + 0.5f));
679 }
680
5f8b738e 681private:
682
683 //! Wrapper to system function to retrieve GL function pointer by name.
684 Standard_EXPORT void* findProc (const char* theFuncName);
685
ee51a9fe 686 //! Print error if not all functions have been exported by context for reported version.
687 //! Note that this will never happen when using GLX, since returned functions can not be validated.
688 //! @param theGlVerMajor the OpenGL major version with missing functions
689 //! @param theGlVerMinor the OpenGL minor version with missing functions
690 Standard_EXPORT void checkWrongVersion (const Standard_Integer theGlVerMajor,
691 const Standard_Integer theGlVerMinor);
692
5f8b738e 693 //! Private initialization function that should be called only once.
4e1523ef 694 Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
5f8b738e 695
01ca42b2 696public: //! @name core profiles
697
698 OpenGl_GlCore11* core11; //!< OpenGL 1.1 core functionality
699 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
700 OpenGl_GlCore15* core15; //!< OpenGL 1.5 core functionality
701 OpenGl_GlCore15Fwd* core15fwd; //!< OpenGL 1.5 without deprecated entry points
702 OpenGl_GlCore20* core20; //!< OpenGL 2.0 core functionality (includes 1.5)
703 OpenGl_GlCore20Fwd* core20fwd; //!< OpenGL 2.0 without deprecated entry points
704 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
705 OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
25ef750e 706 OpenGl_GlCore33* core33; //!< OpenGL 3.3 core profile
707 OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
01ca42b2 708 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
709 OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
710 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
711 OpenGl_GlCore42Back* core42back; //!< OpenGL 4.2 backward compatibility profile
712 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
713 OpenGl_GlCore43Back* core43back; //!< OpenGL 4.3 backward compatibility profile
714 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
715 OpenGl_GlCore44Back* core44back; //!< OpenGL 4.4 backward compatibility profile
5f8b738e 716
58655684 717 Handle(OpenGl_Caps) caps; //!< context options
718
01ca42b2 719public: //! @name extensions
5f8b738e 720
25ef750e 721 Standard_Boolean hasHighp; //!< highp in GLSL ES fragment shader is supported
e99a2f7c 722 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)
25ef750e 723 Standard_Boolean hasTexRGBA8; //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
724 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
725 Standard_Boolean arbTexRG; //!< GL_ARB_texture_rg
74fb257d 726 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)
25ef750e 727 OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
728 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
729 Standard_Boolean arbTboRGB32; //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0
730 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
731 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
732 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
b86bb3df 733 OpenGl_ArbFBOBlit* arbFBOBlit; //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
1ce0716b 734 Standard_Boolean extFragDepth; //!< GL_EXT_frag_depth on OpenGL ES 2.0 (gl_FragDepthEXT built-in variable, before OpenGL ES 3.0)
25ef750e 735 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
736 Standard_Boolean extBgra; //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
737 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
738 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
739 Standard_Boolean atiMem; //!< GL_ATI_meminfo
740 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
5f8b738e 741
5e27df78 742private: // system-dependent fields
2166f0fa 743
da8bb41d 744#if defined(HAVE_EGL)
745 Aspect_Drawable myWindow; //!< EGL surface : EGLSurface
746 Aspect_Display myDisplay; //!< EGL connection to the Display : EGLDisplay
747 Aspect_RenderingContext myGContext; //!< EGL rendering context : EGLContext
748#elif defined(_WIN32)
2bd4c032 749 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
750 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
751 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
4fe56619 752#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
a2e4f780 753 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
754 EAGLContext* myGContext; //!< Rendering Context handle
755 #else
756 NSOpenGLContext* myGContext; //!< Rendering Context handle
757 #endif
2bd4c032 758#else
759 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
760 Aspect_Display myDisplay; //!< connection to the X-server : Display*
761 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
762#endif
763
5e27df78 764private: // context info
765
c04c30b3 766 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Standard_Integer> > OpenGl_DelayReleaseMap;
767 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> > OpenGl_ResourcesMap;
768 typedef NCollection_Shared< NCollection_List<Handle(OpenGl_Resource)> > OpenGl_ResourcesStack;
5e27df78 769
a174a3c5 770 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
771 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
3125ebb6 772 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
5e27df78 773
30f0ad28 774 OpenGl_Clipping myClippingState; //!< state of clip planes
4269bd1b 775
7d9e854b 776 void* myGlLibHandle; //!< optional handle to GL library
01ca42b2 777 NCollection_Handle<OpenGl_GlFunctions>
7d9e854b 778 myFuncs; //!< mega structure for all GL functions
779 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
780 Standard_Integer myTexClamp; //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
781 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
782 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
3c4b62a4 783 Standard_Integer myMaxMsaaSamples; //!< value for GL_MAX_SAMPLES
7d9e854b 784 Standard_Integer myGlVerMajor; //!< cached GL version major number
785 Standard_Integer myGlVerMinor; //!< cached GL version minor number
786 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
787 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
788 Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
789 //!< Used to tell OpenGl that normals should be normalized
2166f0fa 790
3a9b5dc8 791 Standard_Boolean myHasRayTracing; //! indicates whether ray tracing mode is supported
792 Standard_Boolean myHasRayTracingTextures; //! indicates whether textures in ray tracing mode are supported
793 Standard_Boolean myHasRayTracingAdaptiveSampling; //! indicates whether adaptive screen sampling in ray tracing mode is supported
794
30f0ad28 795 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
796
7d3e64ef 797private: //! @name fields tracking current state
798
6d0e6be5 799 Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
800 Handle(OpenGl_Sampler) myTexSampler; //!< currently active sampler object
801 Handle(OpenGl_FrameBuffer) myDefaultFbo; //!< default Frame Buffer Object
802 Handle(OpenGl_LineAttributes) myHatchStyles; //!< resource holding predefined hatch styles patterns
3bffef55 803 Standard_Integer myViewport[4]; //!< current viewport
6d0e6be5 804 Standard_Integer myPointSpriteOrig; //!< GL_POINT_SPRITE_COORD_ORIGIN state (GL_UPPER_LEFT by default)
805 Standard_Integer myRenderMode; //!< value for active rendering mode
806 Standard_Integer myPolygonMode; //!< currently used polygon rasterization mode (glPolygonMode)
807 bool myToCullBackFaces; //!< back face culling mode enabled state (glIsEnabled (GL_CULL_FACE))
808 Standard_Integer myReadBuffer; //!< current read buffer
809 Standard_Integer myDrawBuffer; //!< current draw buffer
810 unsigned int myDefaultVao; //!< default Vertex Array Object
811 Standard_Boolean myIsGlDebugCtx; //!< debug context initialization state
812 TCollection_AsciiString myVendor; //!< Graphics Driver's vendor
813 TColStd_PackedMapOfInteger myFilters[6]; //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
814 Standard_ShortReal myResolutionRatio; //!< scaling factor for parameters like text size
75c262a9 815 //!< to be properly displayed on device (screen / printer)
7d3e64ef 816
c827ea3a 817public:
818
825aa485 819 OpenGl_MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
820 OpenGl_MatrixState<Standard_ShortReal> WorldViewState; //!< state of orientation matrix
821 OpenGl_MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection matrix
c827ea3a 822
5e27df78 823private:
824
825 //! Copying allowed only within Handles
826 OpenGl_Context (const OpenGl_Context& );
827 OpenGl_Context& operator= (const OpenGl_Context& );
828
2166f0fa
SK
829public:
830
92efcf78 831 DEFINE_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient) // Type definition
2166f0fa 832
5e27df78 833 friend class OpenGl_Window;
834
2166f0fa
SK
835};
836
837#endif // _OpenGl_Context_H__