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