0027819: Visualization - provide a possibility to redefine SelectMgr_SelectableObject...
[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;
c04c30b3 135
136class OpenGl_Context;
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{
a174a3c5 187public:
188
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
2166f0fa
SK
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).
3c4b62a4 321 Standard_EXPORT void ResetErrors (const bool theToPrintErrors = false);
5f8b738e 322
86fa64d9 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;
327
2bd4c032 328 //! Activates current context.
329 //! Class should be initialized with appropriate info.
330 Standard_EXPORT Standard_Boolean MakeCurrent();
331
5e27df78 332 //! Swap front/back buffers for this GL context (should be activated before!).
333 Standard_EXPORT void SwapBuffers();
334
f978241f 335 //! Setup swap interval (VSync).
336 Standard_EXPORT Standard_Boolean SetSwapInterval (const Standard_Integer theInterval);
337
b5ac8292 338 //! Return true if active mode is GL_RENDER (cached state)
339 Standard_Boolean IsRender() const
340 {
ca3c13d1 341 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 342 return myRenderMode == GL_RENDER;
ca3c13d1 343 #else
344 return Standard_True;
345 #endif
b5ac8292 346 }
664cae74 347
b5ac8292 348 //! Return true if active mode is GL_FEEDBACK (cached state)
349 Standard_Boolean IsFeedback() const
350 {
ca3c13d1 351 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 352 return myRenderMode == GL_FEEDBACK;
ca3c13d1 353 #else
354 return Standard_False;
355 #endif
b5ac8292 356 }
664cae74 357
f0430952 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;
365
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;
369
26d9c835 370 //! This function retrieves information from GL about GPU memory.
371 Standard_EXPORT void MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const;
372
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;
377
5e27df78 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;
382
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
390 {
391 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
392 if (aResource.IsNull())
393 {
394 return Standard_False;
395 }
396
397 theValue = TheHandleType::DownCast (aResource);
398 return !theValue.IsNull();
399 }
400
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);
408
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.
a174a3c5 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);
5e27df78 419
420 //! Append resource to queue for delayed clean up.
421 //! Resources in this queue will be released at next redraw call.
aa00364d 422 template <class T>
423 void DelayedRelease (Handle(T)& theResource)
424 {
425 myUnusedResources->Prepend (theResource);
426 theResource.Nullify();
427 }
5e27df78 428
429 //! Clean up the delayed release queue.
430 Standard_EXPORT void ReleaseDelayed();
431
4269bd1b 432 //! @return tool for management of clippings within this context.
30f0ad28 433 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
4269bd1b 434
435 //! @return tool for management of clippings within this context.
30f0ad28 436 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
437
438 //! @return tool for management of shader programs within this context.
439 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
4269bd1b 440
441public:
442
ca3c13d1 443 //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
444 Standard_Integer TextureWrapClamp() const { return myTexClamp; }
445
bf75be98 446 //! @return maximum degree of anisotropy texture filter
3c4b62a4 447 Standard_Integer MaxDegreeOfAnisotropy() const { return myAnisoMax; }
bf75be98 448
449 //! @return value for GL_MAX_TEXTURE_SIZE
3c4b62a4 450 Standard_Integer MaxTextureSize() const { return myMaxTexDim; }
451
452 //! @return value for GL_MAX_SAMPLES
453 Standard_Integer MaxMsaaSamples() const { return myMaxMsaaSamples; }
bf75be98 454
4269bd1b 455 //! Get maximum number of clip planes supported by OpenGl.
7e7c2f0b 456 //! This value is implementation dependent. At least 6
4269bd1b 457 //! planes should be supported by OpenGl (see specs).
458 //! @return value for GL_MAX_CLIP_PLANES
3c4b62a4 459 Standard_Integer MaxClipPlanes() const { return myMaxClipPlanes; }
4269bd1b 460
7d3e64ef 461 //! Returns true if VBO is supported and permitted.
462 inline bool ToUseVbo() const
463 {
464 return core15fwd != NULL
465 && !caps->vboDisable;
466 }
467
7d9e854b 468 //! @return cached state of GL_NORMALIZE.
469 Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
470
471 //! Sets GL_NORMALIZE enabled or disabled.
472 //! @return old value of the flag
473 Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
474
6d0e6be5 475 //! @return cached state of polygon rasterization mode (glPolygonMode()).
476 Standard_Integer PolygonMode() const { return myPolygonMode; }
477
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);
481
482 //! @return cached enabled state of polygon hatching rasterization.
483 bool IsPolygonHatchEnabled() const
484 {
485 return !myHatchStyles.IsNull() && myHatchStyles->TypeOfHatch() != 0;
486 }
487
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);
492
493 //! @return cached state of polygon hatch type.
494 Standard_Integer PolygonHatchStyle() const
495 {
496 return myHatchStyles.IsNull() ? Aspect_HS_SOLID : myHatchStyles->TypeOfHatch();
497 }
498
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);
506
c827ea3a 507 //! Applies matrix stored in ModelWorldState to OpenGl.
5e94009d 508 Standard_EXPORT void ApplyModelWorldMatrix();
c827ea3a 509
510 //! Applies matrix stored in WorldViewState to OpenGl.
5e94009d 511 Standard_EXPORT void ApplyWorldViewMatrix();
c827ea3a 512
513 //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
5e94009d 514 Standard_EXPORT void ApplyModelViewMatrix();
c827ea3a 515
516 //! Applies matrix stored in ProjectionState to OpenGl.
5e94009d 517 Standard_EXPORT void ApplyProjectionMatrix();
c827ea3a 518
cbf18624 519public:
520
7e7c2f0b 521 //! @return messenger instance
857ffd5e 522 inline const Handle(Message_Messenger)& Messenger() const
cbf18624 523 {
8fa64b52 524 return ::Message::DefaultMessenger();
cbf18624 525 }
526
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);
538
c87535af 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);
b5ac8292 542
c87535af 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);
b5ac8292 546
547 //! @return true if OpenGl context supports left and
548 //! right rendering buffers.
549 Standard_Boolean HasStereoBuffers() const
550 {
ca3c13d1 551 #if !defined(GL_ES_VERSION_2_0)
b5ac8292 552 return myIsStereoBuffers;
ca3c13d1 553 #else
554 return Standard_False;
555 #endif
b5ac8292 556 }
557
7d3e64ef 558public: //! @name methods to alter or retrieve current state
559
3bffef55 560 //! Return cached viewport definition (x, y, width, height).
561 const Standard_Integer* Viewport() const { return myViewport; }
562
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]);
566
38a0206f 567 //! Return active read buffer.
568 Standard_Integer ReadBuffer() { return myReadBuffer; }
b5ac8292 569
38a0206f 570 //! Switch read buffer, wrapper for ::glReadBuffer().
571 Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
b5ac8292 572
38a0206f 573 //! Return active draw buffer.
574 Standard_Integer DrawBuffer() { return myDrawBuffer; }
575
576 //! Switch draw buffer, wrapper for ::glDrawBuffer().
577 Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
578
579 //! Switch read/draw buffers.
580 void SetReadDrawBuffer (const Standard_Integer theBuffer)
581 {
582 SetReadBuffer (theBuffer);
583 SetDrawBuffer (theBuffer);
584 }
b5ac8292 585
b6472664 586 //! Return back face culling state.
587 bool ToCullBackFaces() const { return myToCullBackFaces; }
588
589 //! Enable or disable back face culling (glEnable (GL_CULL_FACE)).
590 Standard_EXPORT void SetCullBackFaces (bool theToEnable);
591
b5ac8292 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();
597
7d3e64ef 598 //! @return active GLSL program
599 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
600 {
601 return myActiveProgram;
602 }
603
25ef750e 604 //! @return OpenGL sampler object used to override default texture parameters
605 const Handle(OpenGl_Sampler)& TextureSampler()
606 {
607 return myTexSampler;
608 }
609
7d3e64ef 610 //! Bind specified program to current context,
611 //! or unbind previous one when NULL specified.
8625ef7e 612 //! @return true if some program is bound to context
613 Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
614
299e0ab9 615 //! Setup current shading material.
f9ba5c4d 616 Standard_EXPORT void SetShadingMaterial (const OpenGl_AspectFace* theAspect,
617 const OpenGl_Vec4* theHighlightColor = NULL);
299e0ab9 618
8625ef7e 619 //! Setup current color.
620 Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
621
ac116c22 622 //! Setup type of line.
623 Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine theType,
624 const Standard_ShortReal theFactor = 1.0f);
625
626 //! Setup width of line.
627 Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
628
8625ef7e 629 //! Setup point size.
630 Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
7d3e64ef 631
fd59283a 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();
637
79f4f036 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);
640
4e1523ef 641 //! Bind default Vertex Array Object
642 Standard_EXPORT void BindDefaultVao();
643
a2e4f780 644 //! Default Frame Buffer Object.
645 const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
646 {
647 return myDefaultFbo;
648 }
649
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);
653
4e1523ef 654 //! Return debug context initialization state.
655 Standard_Boolean IsDebugContext() const
656 {
657 return myIsGlDebugCtx;
658 }
659
c357e426 660 Standard_EXPORT void EnableFeatures() const;
661
662 Standard_EXPORT void DisableFeatures() const;
663
75c262a9 664 //! Set resolution ratio.
665 //! Note that this method rounds @theRatio to nearest integer.
666 void SetResolutionRatio (const Standard_ShortReal theRatio)
667 {
668 myResolutionRatio = Max (1.0f, std::floor (theRatio + 0.5f));
669 }
670
5f8b738e 671private:
672
673 //! Wrapper to system function to retrieve GL function pointer by name.
674 Standard_EXPORT void* findProc (const char* theFuncName);
675
ee51a9fe 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);
682
5f8b738e 683 //! Private initialization function that should be called only once.
4e1523ef 684 Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
5f8b738e 685
01ca42b2 686public: //! @name core profiles
687
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
25ef750e 696 OpenGl_GlCore33* core33; //!< OpenGL 3.3 core profile
697 OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
01ca42b2 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
5f8b738e 706
58655684 707 Handle(OpenGl_Caps) caps; //!< context options
708
01ca42b2 709public: //! @name extensions
5f8b738e 710
25ef750e 711 Standard_Boolean hasHighp; //!< highp in GLSL ES fragment shader is supported
e99a2f7c 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)
25ef750e 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
74fb257d 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)
25ef750e 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
b86bb3df 723 OpenGl_ArbFBOBlit* arbFBOBlit; //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
25ef750e 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
5f8b738e 730
5e27df78 731private: // system-dependent fields
2166f0fa 732
da8bb41d 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)
2bd4c032 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
4fe56619 741#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
a2e4f780 742 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
743 EAGLContext* myGContext; //!< Rendering Context handle
744 #else
745 NSOpenGLContext* myGContext; //!< Rendering Context handle
746 #endif
2bd4c032 747#else
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
751#endif
752
5e27df78 753private: // context info
754
c04c30b3 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;
5e27df78 758
a174a3c5 759 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
760 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
3125ebb6 761 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
5e27df78 762
30f0ad28 763 OpenGl_Clipping myClippingState; //!< state of clip planes
4269bd1b 764
7d9e854b 765 void* myGlLibHandle; //!< optional handle to GL library
01ca42b2 766 NCollection_Handle<OpenGl_GlFunctions>
7d9e854b 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
3c4b62a4 772 Standard_Integer myMaxMsaaSamples; //!< value for GL_MAX_SAMPLES
7d9e854b 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
2166f0fa 779
30f0ad28 780 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
781
7d3e64ef 782private: //! @name fields tracking current state
783
6d0e6be5 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
3bffef55 788 Standard_Integer myViewport[4]; //!< current viewport
6d0e6be5 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
75c262a9 800 //!< to be properly displayed on device (screen / printer)
7d3e64ef 801
c827ea3a 802public:
803
825aa485 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
c827ea3a 807
5e27df78 808private:
809
810 //! Copying allowed only within Handles
811 OpenGl_Context (const OpenGl_Context& );
812 OpenGl_Context& operator= (const OpenGl_Context& );
813
2166f0fa
SK
814public:
815
92efcf78 816 DEFINE_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient) // Type definition
2166f0fa 817
5e27df78 818 friend class OpenGl_Window;
819
2166f0fa
SK
820};
821
822#endif // _OpenGl_Context_H__