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