0032733: Coding Rules - fix misprints in Doxygen tags
[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
5bd54bef 16#ifndef OpenGl_Context_HeaderFile
17#define OpenGl_Context_HeaderFile
2166f0fa 18
2bd4c032 19#include <Aspect_Handle.hxx>
6d0e6be5 20#include <Aspect_HatchStyle.hxx>
2bd4c032 21#include <Aspect_Drawable.hxx>
22#include <Aspect_Display.hxx>
c8365a1c 23#include <Aspect_GraphicsLibrary.hxx>
2bd4c032 24#include <Aspect_RenderingContext.hxx>
ac116c22 25#include <Aspect_TypeOfLine.hxx>
5e27df78 26#include <NCollection_DataMap.hxx>
26d9c835 27#include <Graphic3d_DiagnosticInfo.hxx>
cc8cbabe 28#include <Graphic3d_TextureUnit.hxx>
4269bd1b 29#include <NCollection_Map.hxx>
5e27df78 30#include <NCollection_Handle.hxx>
3125ebb6 31#include <NCollection_List.hxx>
a1073ae2 32#include <NCollection_SparseArray.hxx>
cbf18624 33#include <Message.hxx>
58655684 34#include <OpenGl_Caps.hxx>
6d0e6be5 35#include <OpenGl_LineAttributes.hxx>
8613985b 36#include <OpenGl_Material.hxx>
825aa485 37#include <OpenGl_MatrixState.hxx>
8625ef7e 38#include <OpenGl_Vec.hxx>
5e27df78 39#include <OpenGl_Resource.hxx>
cc8cbabe 40#include <OpenGl_TextureSet.hxx>
2166f0fa 41#include <Standard_Transient.hxx>
26d9c835 42#include <TColStd_IndexedDataMapOfStringString.hxx>
c87535af 43#include <TColStd_PackedMapOfInteger.hxx>
30f0ad28 44#include <OpenGl_Clipping.hxx>
c04c30b3 45
46#include <NCollection_Shared.hxx>
2166f0fa 47
7ce8fe05 48#include <memory>
49
5f8b738e 50//! Forward declarations
a2e4f780 51#if defined(__APPLE__)
52 #import <TargetConditionals.h>
53 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
54 #ifdef __OBJC__
55 @class EAGLContext;
56 #else
57 struct EAGLContext;
58 #endif
59 #else
60 #ifdef __OBJC__
61 @class NSOpenGLContext;
62 #else
63 struct NSOpenGLContext;
64 #endif
65 #endif
66#endif
67
01ca42b2 68struct OpenGl_GlFunctions;
5e27df78 69struct OpenGl_ArbTBO;
70struct OpenGl_ArbIns;
58655684 71struct OpenGl_ArbDbg;
01ca42b2 72struct OpenGl_ArbFBO;
b86bb3df 73struct OpenGl_ArbFBOBlit;
cc8cbabe 74struct OpenGl_ArbSamplerObject;
25ef750e 75struct OpenGl_ArbTexBindless;
cc8cbabe 76struct OpenGl_ExtGS;
2166f0fa 77
7ce8fe05 78struct OpenGl_GlCore11Fwd;
79struct OpenGl_GlCore11;
43eddb47 80struct OpenGl_GlCore12;
01ca42b2 81struct OpenGl_GlCore13;
43eddb47 82struct OpenGl_GlCore14;
83struct OpenGl_GlCore15;
84struct OpenGl_GlCore20;
85struct OpenGl_GlCore21;
86struct OpenGl_GlCore30;
87struct OpenGl_GlCore31;
88struct OpenGl_GlCore32;
89struct OpenGl_GlCore33;
90struct OpenGl_GlCore40;
91struct OpenGl_GlCore41;
92struct OpenGl_GlCore42;
93struct OpenGl_GlCore43;
94struct OpenGl_GlCore44;
95struct OpenGl_GlCore45;
96struct OpenGl_GlCore46;
952a0565 97
b40cdc2b 98class Graphic3d_Camera;
f838dac4 99class Graphic3d_PresentationAttributes;
bf5f0ca2 100class OpenGl_Aspects;
cc8cbabe 101class OpenGl_FrameBuffer;
cc8cbabe 102class OpenGl_ShaderProgram;
103class OpenGl_ShaderManager;
15669413 104class OpenGl_FrameStats;
c04c30b3 105
a1073ae2 106enum OpenGl_FeatureFlag
107{
108 OpenGl_FeatureNotAvailable = 0, //!< Feature is not supported by OpenGl implementation.
109 OpenGl_FeatureInExtensions = 1, //!< Feature is supported as extension.
110 OpenGl_FeatureInCore = 2 //!< Feature is supported as part of core profile.
111};
112
c04c30b3 113DEFINE_STANDARD_HANDLE(OpenGl_Context, Standard_Transient)
114
5f8b738e 115//! This class generalize access to the GL context and available extensions.
116//!
4e1523ef 117//! Functions related to specific OpenGL version or extension are grouped into structures which can be accessed as fields of this class.
118//! The most simple way to check that required functionality is available - is NULL check for the group:
5f8b738e 119//! @code
120//! if (myContext->core20 != NULL)
121//! {
122//! myGlProgram = myContext->core20->glCreateProgram();
123//! .. do more stuff ..
124//! }
125//! else
126//! {
127//! .. compatibility with outdated configurations ..
128//! }
129//! @endcode
130//!
43eddb47 131//! Current implementation provide access to OpenGL core functionality up to 4.6 version (core12, core13, core14, etc.)
4e1523ef 132//! as well as several extensions (arbTBO, arbFBO, etc.).
133//!
134//! OpenGL context might be initialized in Core Profile. In this case deprecated functionality become unavailable.
4e1523ef 135//! To select which core** function set should be used in specific case:
136//! - Determine the minimal OpenGL version required for implemented functionality and use it to access all functions.
137//! For example, if algorithm requires OpenGL 2.1+, it is better to write core20fwd->glEnable() rather than core11fwd->glEnable() for uniformity.
4e1523ef 138//! - Validate minimal requirements at initialization/creation time and omit checks within code where algorithm should be already initialized.
43eddb47 139//! Properly escape code incompatible with Core Profile. The simplest way to check Core Profile is "if (core11ffp == NULL)".
5f8b738e 140//!
141//! Simplified extensions classification:
142//! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
143//! - prefixed with EXT are accepted by 2+ vendors;
144//! - prefixed with ARB are accepted by Architecture Review Board and are candidates
145//! for inclusion into GL core functionality.
146//! Some functionality can be represented in several extensions simultaneously.
147//! In this case developer should be careful because different specification may differ
148//! in aspects (like enumeration values and error-handling).
149//!
4e1523ef 150//! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts with different capabilities.
151//! For this reason OpenGl_Context should be initialized and used for each GL context independently.
bc73b006 152//!
153//! Matrices of OpenGl transformations:
154//! model -> world -> view -> projection
155//! These matrices might be changed for local transformation, transform persistent using direct access to
156//! current matrix of ModelWorldState, WorldViewState and ProjectionState
43eddb47 157//! After, these matrices should be applied using ApplyModelWorldMatrix, ApplyWorldViewMatrix,
bc73b006 158//! ApplyModelViewMatrix or ApplyProjectionMatrix.
2166f0fa
SK
159class OpenGl_Context : public Standard_Transient
160{
8613985b 161 DEFINE_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient)
162 friend class OpenGl_Window;
e44b849d 163 friend struct OpenGl_GlFunctions;
a174a3c5 164public:
165
15669413 166 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> > OpenGl_ResourcesMap;
167
a174a3c5 168 //! Function for getting power of to number larger or equal to input number.
169 //! @param theNumber number to 'power of two'
170 //! @param theThreshold upper threshold
171 //! @return power of two number
172 inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
173 const Standard_Integer theThreshold)
174 {
175 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
176 {
177 if (theNumber <= p2)
178 {
179 return p2;
180 }
181 }
182 return theThreshold;
183 }
184
a46ab511 185 //! Format GL constant as hex value 0xABCD.
186 Standard_EXPORT static TCollection_AsciiString FormatGlEnumHex (int theGlEnum);
187
188 //! Format pointer as hex value 0xABCD.
189 Standard_EXPORT static TCollection_AsciiString FormatPointer (const void* thePtr);
190
191 //! Format size value.
192 Standard_EXPORT static TCollection_AsciiString FormatSize (Standard_Size theSize);
193
194 //! Return text description of GL error.
195 Standard_EXPORT static TCollection_AsciiString FormatGlError (int theGlError);
196
2166f0fa
SK
197public:
198
5f8b738e 199 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
58655684 200 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
5f8b738e 201
202 //! Destructor.
203 Standard_EXPORT virtual ~OpenGl_Context();
2166f0fa 204
05e2200b 205 //! Release all resources, including shared ones
206 Standard_EXPORT void forcedRelease();
207
5e27df78 208 //! Share GL context resources.
209 //! theShareCtx - handle to context to retrieve handles to shared resources.
210 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
211
7e7c2f0b 212 //! Initialize class from currently bound OpenGL context. Method should be called only once.
213 //! @return false if no GL context is bound to the current thread
4e1523ef 214 Standard_EXPORT Standard_Boolean Init (const Standard_Boolean theIsCoreProfile = Standard_False);
2166f0fa 215
fd4a6963 216 //! @return true if this context is valid (has been initialized)
217 inline Standard_Boolean IsValid() const
218 {
219 return myIsInitialized;
220 }
221
da8bb41d 222 //! Initialize class from specified surface and rendering context. Method should be called only once.
b69e576a 223 //! The meaning of parameters is platform-specific.
224 //!
225 //! EGL:
226 //! @code
227 //! Handle(Aspect_Window) theAspWin;
228 //! EGLSurface theEglSurf = eglCreateWindowSurface (theEglDisp, anEglConfig, (EGLNativeWindowType )theAspWin->NativeHandle(), NULL);
229 //! EGLDisplay theEglDisp = eglGetDisplay (EGL_DEFAULT_DISPLAY);
230 //! EGLContext theEglCtx = eglCreateContext ((EGLDisplay )theEglDisp, anEglConfig, EGL_NO_CONTEXT, anEglCtxAttribs);
231 //! Handle(OpenGl_Context) aGlCtx = new OpenGl_Context();
232 //! aGlCtx->Init ((Aspect_Drawable )theEglSurf, (Aspect_Display )theEglDisp, (Aspect_RenderingContext )theEglCtx);
233 //! @endcode
234 //!
235 //! Windows (Win32):
236 //! @code
237 //! Handle(WNT_Window) theAspWin;
238 //! HWND theWindow = (HWND )theAspWin->NativeHandle();
239 //! HDC theDevCtx = GetDC(theWindow);
240 //! HGLRC theGContext = wglCreateContext (theDevCtx);
241 //! Handle(OpenGl_Context) aGlCtx = new OpenGl_Context();
242 //! aGlCtx->Init ((Aspect_Drawable )theWindow, (Aspect_Display )theDevCtx, (Aspect_RenderingContext )theGContext);
243 //! @endcode
244 //!
245 //! Linux (Xlib):
246 //! @code
247 //! Handle(Xw_Window) theAspWin;
248 //! Window theXWindow = (Window )theAspWin->NativeHandle();
249 //! Display* theXDisp = (Display* )theAspWin->DisplayConnection()->GetDisplayAspect();
250 //! GLXContext theGlxCtx = glXCreateContext (theXDisp, aVis.get(), NULL, GL_TRUE);
251 //! Handle(OpenGl_Context) aGlCtx = new OpenGl_Context();
252 //! aGlCtx->Init ((Aspect_Drawable )theXWindow, (Aspect_Display )theXDisp, (Aspect_RenderingContext )theGlxCtx);
253 //! @endcode
254 //!
255 //! @param theSurface [in] surface / window (EGLSurface | HWND | GLXDrawable/Window)
256 //! @param theDisplay [in] display or device context (EGLDisplay | HDC | Display*)
257 //! @param theContext [in] rendering context (EGLContext | HGLRC | GLXContext | EAGLContext* | NSOpenGLContext*)
258 //! @param theIsCoreProfile [in] flag indicating that passed GL rendering context has been created with Core Profile
da8bb41d 259 //! @return false if OpenGL context can not be bound to specified surface
b69e576a 260 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theSurface,
261 const Aspect_Display theDisplay,
262 const Aspect_RenderingContext theContext,
4e1523ef 263 const Standard_Boolean theIsCoreProfile = Standard_False);
7e7c2f0b 264
b69e576a 265 //! Return window handle currently bound to this OpenGL context (EGLSurface | HWND | GLXDrawable).
266 Aspect_Drawable Window() const { return myWindow; }
267
268 //! Return display / window device context (EGLDisplay | HDC | Display*).
269 Aspect_Display GetDisplay() const { return myDisplay; }
270
271 //! Return rendering context (EGLContext | HGLRC | GLXContext | EAGLContext* | NSOpenGLContext*).
272 Aspect_RenderingContext RenderingContext() const { return myGContext; }
7e7c2f0b 273
b69e576a 274#if defined(__APPLE__) && !defined(HAVE_XLIB)
a2e4f780 275 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
276
277 //! Initialize class from specified OpenGL ES context (EAGLContext). Method should be called only once.
b69e576a 278 Standard_Boolean Init (EAGLContext* theGContext,
279 const Standard_Boolean theIsCoreProfile = Standard_False)
280 {
281 return Init ((Aspect_Drawable )0, (Aspect_Display )0, (Aspect_RenderingContext )theGContext, theIsCoreProfile);
282 }
a2e4f780 283 #else
7e7c2f0b 284 //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
b69e576a 285 Standard_Boolean Init (NSOpenGLContext* theGContext,
286 const Standard_Boolean theIsCoreProfile = Standard_False)
7e7c2f0b 287 {
b69e576a 288 return Init ((Aspect_Drawable )0, (Aspect_Display )0, (Aspect_RenderingContext )theGContext, theIsCoreProfile);
7e7c2f0b 289 }
b69e576a 290 #endif
2bd4c032 291#endif
292
4e1523ef 293 //! Read OpenGL version information from active context.
294 Standard_EXPORT static void ReadGlVersion (Standard_Integer& theGlVerMajor,
295 Standard_Integer& theGlVerMinor);
296
5f8b738e 297 //! Check if theExtName extension is supported by active GL context.
2bd4c032 298 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
2166f0fa 299
58655684 300 //! Check if theExtName extension is in extensions string.
301 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
302 const char* theExtName);
303
2166f0fa 304 //! Auxiliary template to retrieve GL function pointer.
5f8b738e 305 //! Pointer to function retrieved from library is statically casted
306 //! to requested type - there no way to check real signature of exported function.
2166f0fa 307 //! The context should be bound before call.
0ae9ac21 308 //! @param theLastFailFuncName [out] set to theFuncName in case of failure, unmodified on success
309 //! @param theFuncName [in] function name to find
310 //! @param theFuncPtr [out] retrieved function pointer
311 //! @return TRUE on success
312 template <typename FuncType_t>
313 Standard_Boolean FindProcVerbose (const char*& theLastFailFuncName,
314 const char* theFuncName,
315 FuncType_t& theFuncPtr)
316 {
317 theFuncPtr = (FuncType_t )findProc (theFuncName);
318 if (theFuncPtr == NULL)
319 {
320 theLastFailFuncName = theFuncName;
321 return Standard_False;
322 }
323 return Standard_True;
324 }
325
326 //! Auxiliary template to retrieve GL function pointer.
327 //! Same as FindProcVerbose() but without auxiliary last function name argument.
2166f0fa 328 template <typename FuncType_t>
5f8b738e 329 Standard_Boolean FindProc (const char* theFuncName,
330 FuncType_t& theFuncPtr)
2166f0fa 331 {
5f8b738e 332 theFuncPtr = (FuncType_t )findProc (theFuncName);
2166f0fa
SK
333 return (theFuncPtr != NULL);
334 }
335
c8365a1c 336 //! Return active graphics library.
337 Aspect_GraphicsLibrary GraphicsLibrary() const { return myGapi; }
338
2bd4c032 339 //! @return true if detected GL version is greater or equal to requested one.
340 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
7e7c2f0b 341 const Standard_Integer theVerMinor) const
5f8b738e 342 {
343 return (myGlVerMajor > theVerMajor)
344 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
345 }
346
26d9c835 347 //! Return cached GL version major number.
348 Standard_Integer VersionMajor() const { return myGlVerMajor; }
349
350 //! Return cached GL version minor number.
351 Standard_Integer VersionMinor() const { return myGlVerMinor; }
352
f978241f 353 //! Access entire map of loaded OpenGL functions.
7ce8fe05 354 const OpenGl_GlFunctions* Functions() const { return myFuncs.get(); }
f978241f 355
5f8b738e 356 //! Clean up errors stack for this GL context (glGetError() in loop).
f9f740d6 357 //! @return true if some error has been cleared
358 Standard_EXPORT bool ResetErrors (const bool theToPrintErrors = false);
5f8b738e 359
86fa64d9 360 //! This method uses system-dependent API to retrieve information
361 //! about GL context bound to the current thread.
362 //! @return true if current thread is bound to this GL context
363 Standard_EXPORT Standard_Boolean IsCurrent() const;
364
2bd4c032 365 //! Activates current context.
366 //! Class should be initialized with appropriate info.
367 Standard_EXPORT Standard_Boolean MakeCurrent();
368
5e27df78 369 //! Swap front/back buffers for this GL context (should be activated before!).
370 Standard_EXPORT void SwapBuffers();
371
f978241f 372 //! Setup swap interval (VSync).
373 Standard_EXPORT Standard_Boolean SetSwapInterval (const Standard_Integer theInterval);
374
b5ac8292 375 //! Return true if active mode is GL_RENDER (cached state)
c8365a1c 376 Standard_EXPORT Standard_Boolean IsRender() const;
664cae74 377
b5ac8292 378 //! Return true if active mode is GL_FEEDBACK (cached state)
c8365a1c 379 Standard_EXPORT Standard_Boolean IsFeedback() const;
664cae74 380
f0430952 381 //! This function retrieves information from GL about free GPU memory that is:
382 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
383 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
384 //! - Numbers meaning may vary.
385 //! You should use this info only for diagnostics purposes.
386 //! @return free GPU dedicated memory in bytes.
387 Standard_EXPORT Standard_Size AvailableMemory() const;
388
389 //! This function retrieves information from GL about GPU memory
390 //! and contains more vendor-specific values than AvailableMemory().
391 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
392
26d9c835 393 //! This function retrieves information from GL about GPU memory.
394 Standard_EXPORT void MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const;
395
396 //! Fill in the dictionary with OpenGL info.
397 //! Should be called with bound context.
398 Standard_EXPORT void DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
399 Graphic3d_DiagnosticInfo theFlags) const;
400
5e27df78 401 //! Access shared resource by its name.
402 //! @param theKey - unique identifier;
403 //! @return handle to shared resource or NULL.
404 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
405
406 //! Access shared resource by its name.
407 //! @param theKey - unique identifier;
408 //! @param theValue - handle to fill;
409 //! @return true if resource was shared.
410 template<typename TheHandleType>
411 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
412 TheHandleType& theValue) const
413 {
414 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
415 if (aResource.IsNull())
416 {
417 return Standard_False;
418 }
419
420 theValue = TheHandleType::DownCast (aResource);
421 return !theValue.IsNull();
422 }
423
424 //! Register shared resource.
425 //! Notice that after registration caller shouldn't release it by himself -
426 //! it will be automatically released on context destruction.
427 //! @param theKey - unique identifier, shouldn't be empty;
428 //! @param theResource - new resource to register, shouldn't be NULL.
429 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
430 const Handle(OpenGl_Resource)& theResource);
431
432 //! Release shared resource.
433 //! If there are more than one reference to this resource
434 //! (also used by some other existing object) then call will be ignored.
435 //! This means that current object itself should nullify handle before this call.
436 //! Notice that this is unrecommended operation at all and should be used
437 //! only in case of fat resources to release memory for other needs.
a174a3c5 438 //! @param theKey unique identifier
439 //! @param theToDelay postpone release until next redraw call
440 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
441 const Standard_Boolean theToDelay = Standard_False);
5e27df78 442
443 //! Append resource to queue for delayed clean up.
444 //! Resources in this queue will be released at next redraw call.
aa00364d 445 template <class T>
446 void DelayedRelease (Handle(T)& theResource)
447 {
448 myUnusedResources->Prepend (theResource);
449 theResource.Nullify();
450 }
5e27df78 451
452 //! Clean up the delayed release queue.
453 Standard_EXPORT void ReleaseDelayed();
454
15669413 455 //! Return map of shared resources.
456 const OpenGl_ResourcesMap& SharedResources() const { return *mySharedResources; }
457
4269bd1b 458 //! @return tool for management of clippings within this context.
30f0ad28 459 inline OpenGl_Clipping& ChangeClipping() { return myClippingState; }
4269bd1b 460
461 //! @return tool for management of clippings within this context.
30f0ad28 462 inline const OpenGl_Clipping& Clipping() const { return myClippingState; }
463
464 //! @return tool for management of shader programs within this context.
465 inline const Handle(OpenGl_ShaderManager)& ShaderManager() const { return myShaderManager; }
4269bd1b 466
467public:
468
ca3c13d1 469 //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
470 Standard_Integer TextureWrapClamp() const { return myTexClamp; }
471
cd43c08f 472 //! @return true if texture parameters GL_TEXTURE_BASE_LEVEL/GL_TEXTURE_MAX_LEVEL are supported.
473 Standard_Boolean HasTextureBaseLevel() const
474 {
c8365a1c 475 return myGapi == Aspect_GraphicsLibrary_OpenGLES
476 ? IsGlGreaterEqual (3, 0)
477 : IsGlGreaterEqual (1, 2);
cd43c08f 478 }
479
faff3767 480 //! Return map of supported texture formats.
481 const Handle(Image_SupportedFormats)& SupportedTextureFormats() const { return mySupportedFormats; }
482
bf75be98 483 //! @return maximum degree of anisotropy texture filter
3c4b62a4 484 Standard_Integer MaxDegreeOfAnisotropy() const { return myAnisoMax; }
bf75be98 485
486 //! @return value for GL_MAX_TEXTURE_SIZE
3c4b62a4 487 Standard_Integer MaxTextureSize() const { return myMaxTexDim; }
488
cc8cbabe 489 //! @return value for GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
490 Standard_Integer MaxCombinedTextureUnits() const { return myMaxTexCombined; }
491
5c225e8e 492 //! This method returns the multi-texture limit for obsolete fixed-function pipeline.
493 //! Use MaxCombinedTextureUnits() instead for limits for using programmable pipeline.
494 //! @return value for GL_MAX_TEXTURE_UNITS
495 Standard_Integer MaxTextureUnitsFFP() const { return myMaxTexUnitsFFP; }
496
72f6dc61 497 //! Return texture unit to be used for sprites (Graphic3d_TextureUnit_PointSprite by default).
737e9a8d 498 Graphic3d_TextureUnit SpriteTextureUnit() const { return mySpriteTexUnit; }
499
c8365a1c 500 //! @return true if MSAA textures are supported.
501 Standard_Boolean HasTextureMultisampling() const { return myHasMsaaTextures; }
502
3c4b62a4 503 //! @return value for GL_MAX_SAMPLES
504 Standard_Integer MaxMsaaSamples() const { return myMaxMsaaSamples; }
bf75be98 505
6997ff1c 506 //! @return maximum FBO width for image dump
507 Standard_Integer MaxDumpSizeX() const { return myMaxDumpSizeX; }
508
509 //! @return maximum FBO height for image dump
510 Standard_Integer MaxDumpSizeY() const { return myMaxDumpSizeY; }
511
a1073ae2 512 //! @return value for GL_MAX_DRAW_BUFFERS
513 Standard_Integer MaxDrawBuffers() const { return myMaxDrawBuffers; }
514
515 //! @return value for GL_MAX_COLOR_ATTACHMENTS
516 Standard_Integer MaxColorAttachments() const { return myMaxColorAttachments; }
517
4269bd1b 518 //! Get maximum number of clip planes supported by OpenGl.
7e7c2f0b 519 //! This value is implementation dependent. At least 6
4269bd1b 520 //! planes should be supported by OpenGl (see specs).
521 //! @return value for GL_MAX_CLIP_PLANES
3c4b62a4 522 Standard_Integer MaxClipPlanes() const { return myMaxClipPlanes; }
4269bd1b 523
3a9b5dc8 524 //! @return TRUE if ray tracing mode is supported
525 Standard_Boolean HasRayTracing() const { return myHasRayTracing; }
526
527 //! @return TRUE if textures in ray tracing mode are supported
528 Standard_Boolean HasRayTracingTextures() const { return myHasRayTracingTextures; }
529
530 //! @return TRUE if adaptive screen sampling in ray tracing mode is supported
531 Standard_Boolean HasRayTracingAdaptiveSampling() const { return myHasRayTracingAdaptiveSampling; }
532
e084dbbc 533 //! @return TRUE if atomic adaptive screen sampling in ray tracing mode is supported
534 Standard_Boolean HasRayTracingAdaptiveSamplingAtomic() const { return myHasRayTracingAdaptiveSamplingAtomic; }
535
ba00aab7 536 //! Returns TRUE if sRGB rendering is supported.
537 bool HasSRGB() const
538 {
539 return hasTexSRGB
540 && hasFboSRGB;
541 }
542
543 //! Returns TRUE if sRGB rendering is supported and permitted.
544 bool ToRenderSRGB() const
545 {
546 return HasSRGB()
547 && !caps->sRGBDisable
548 && !caps->ffpEnable;
549 }
550
551 //! Returns TRUE if window/surface buffer is sRGB-ready.
552 //!
553 //! When offscreen FBOs are created in sRGB, but window is not sRGB-ready,
554 //! blitting into window should be done with manual gamma correction.
555 //!
556 //! In desktop OpenGL, window buffer can be considered as sRGB-ready by default,
557 //! even when application has NOT requested sRGB-ready pixel format,
558 //! and rendering is managed via GL_FRAMEBUFFER_SRGB state.
559 //!
560 //! In OpenGL ES, sRGB-ready window surface should be explicitly requested on construction,
561 //! and cannot be disabled/enabled without GL_EXT_sRGB_write_control extension afterwards
562 //! (GL_FRAMEBUFFER_SRGB can be considered as always tuned ON).
563 bool IsWindowSRGB() const { return myIsSRgbWindow; }
564
e5c11edd 565 //! Overrides if window/surface buffer is sRGB-ready or not (initialized with the context).
566 void SetWindowSRGB (bool theIsSRgb) { myIsSRgbWindow = theIsSRgb; }
567
ba00aab7 568 //! Convert Quantity_ColorRGBA into vec4
569 //! with conversion or no conversion into non-linear sRGB
570 //! basing on ToRenderSRGB() flag.
571 OpenGl_Vec4 Vec4FromQuantityColor (const OpenGl_Vec4& theColor) const
572 {
565baee6 573 return myIsSRgbActive
ba00aab7 574 ? Vec4LinearFromQuantityColor(theColor)
575 : Vec4sRGBFromQuantityColor (theColor);
576 }
577
578 //! Convert Quantity_ColorRGBA into vec4.
579 //! Quantity_Color is expected to be linear RGB, hence conversion is NOT required
580 const OpenGl_Vec4& Vec4LinearFromQuantityColor (const OpenGl_Vec4& theColor) const { return theColor; }
581
582 //! Convert Quantity_ColorRGBA (linear RGB) into non-linear sRGB vec4.
583 OpenGl_Vec4 Vec4sRGBFromQuantityColor (const OpenGl_Vec4& theColor) const
584 {
585 return Quantity_ColorRGBA::Convert_LinearRGB_To_sRGB (theColor);
586 }
587
67312b79 588 //! Returns TRUE if PBR shading model is supported.
589 //! Basically, feature requires OpenGL 3.0+ / OpenGL ES 3.0+ hardware; more precisely:
590 //! - Graphics hardware with moderate capabilities for compiling long enough GLSL program.
591 //! - FBO (e.g. for baking environment).
592 //! - Multi-texturing with >= 4 units (LUT and IBL textures).
593 //! - GL_RG32F texture format (arbTexRG + arbTexFloat)
594 //! - Cubemap texture lookup textureCubeLod()/textureLod() with LOD index within Fragment Shader,
595 //! which requires GLSL OpenGL 3.0+ / OpenGL ES 3.0+ or OpenGL 2.1 + GL_EXT_gpu_shader4 extension.
596 Standard_Boolean HasPBR() const { return myHasPBR; }
597
598 //! Returns texture unit where Environment Lookup Table is expected to be bound, or 0 if PBR is unavailable.
599 Graphic3d_TextureUnit PBREnvLUTTexUnit() const { return myPBREnvLUTTexUnit; }
600
601 //! Returns texture unit where Diffuse (irradiance) IBL map's spherical harmonics coefficients is expected to be bound, or 0 if PBR is unavailable.
602 Graphic3d_TextureUnit PBRDiffIBLMapSHTexUnit() const { return myPBRDiffIBLMapSHTexUnit; }
603
604 //! Returns texture unit where Specular IBL map is expected to be bound, or 0 if PBR is unavailable.
605 Graphic3d_TextureUnit PBRSpecIBLMapTexUnit() const { return myPBRSpecIBLMapTexUnit; }
606
d84e8669 607 //! Returns texture unit where shadow map is expected to be bound, or 0 if unavailable.
608 Graphic3d_TextureUnit ShadowMapTexUnit() const { return myShadowMapTexUnit; }
609
78c4e836 610 //! Returns texture unit for occDepthPeelingDepth within enabled Depth Peeling.
611 Graphic3d_TextureUnit DepthPeelingDepthTexUnit() const { return myDepthPeelingDepthTexUnit; }
612
613 //! Returns texture unit for occDepthPeelingFrontColor within enabled Depth Peeling.
614 Graphic3d_TextureUnit DepthPeelingFrontColorTexUnit() const { return myDepthPeelingFrontColorTexUnit; }
615
7d3e64ef 616 //! Returns true if VBO is supported and permitted.
617 inline bool ToUseVbo() const
618 {
619 return core15fwd != NULL
620 && !caps->vboDisable;
621 }
622
7d9e854b 623 //! @return cached state of GL_NORMALIZE.
624 Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
625
626 //! Sets GL_NORMALIZE enabled or disabled.
627 //! @return old value of the flag
628 Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
629
6d0e6be5 630 //! @return cached state of polygon rasterization mode (glPolygonMode()).
631 Standard_Integer PolygonMode() const { return myPolygonMode; }
632
633 //! Sets polygon rasterization mode (glPolygonMode() function).
634 //! @return old value of the rasterization mode.
635 Standard_EXPORT Standard_Integer SetPolygonMode (const Standard_Integer theMode);
636
637 //! @return cached enabled state of polygon hatching rasterization.
76fada68 638 bool IsPolygonHatchEnabled() const { return myHatchIsEnabled; }
6d0e6be5 639
640 //! Sets enabled state of polygon hatching rasterization
641 //! without affecting currently selected hatching pattern.
642 //! @return previous state of polygon hatching mode.
643 Standard_EXPORT bool SetPolygonHatchEnabled (const bool theIsEnabled);
644
645 //! @return cached state of polygon hatch type.
76fada68 646 Standard_Integer PolygonHatchStyle() const { return myActiveHatchType; }
6d0e6be5 647
648 //! Sets polygon hatch pattern.
649 //! Zero-index value is a default alias for solid filling.
f291ad25 650 //! @param theStyle type of hatch supported by base implementation of
6d0e6be5 651 //! OpenGl_LineAttributes (Aspect_HatchStyle) or the type supported by custom
652 //! implementation derived from OpenGl_LineAttributes class.
653 //! @return old type of hatch.
640d5fe2 654 Standard_EXPORT Standard_Integer SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle);
6d0e6be5 655
8d1a539c 656 //! Sets and applies current polygon offset.
657 Standard_EXPORT void SetPolygonOffset (const Graphic3d_PolygonOffset& theOffset);
658
659 //! Returns currently applied polygon offset parameters.
660 const Graphic3d_PolygonOffset& PolygonOffset() const { return myPolygonOffset; }
661
b40cdc2b 662 //! Returns camera object.
663 const Handle(Graphic3d_Camera)& Camera() const { return myCamera; }
664
665 //! Sets camera object to the context and update matrices.
666 Standard_EXPORT void SetCamera (const Handle(Graphic3d_Camera)& theCamera);
667
bc73b006 668 //! Applies matrix into shader manager stored in ModelWorldState to OpenGl.
669 //! In "model -> world -> view -> projection" it performs:
670 //! model -> world
5e94009d 671 Standard_EXPORT void ApplyModelWorldMatrix();
c827ea3a 672
673 //! Applies matrix stored in WorldViewState to OpenGl.
bc73b006 674 //! In "model -> world -> view -> projection" it performs:
675 //! model -> world -> view,
676 //! where model -> world is identical matrix
5e94009d 677 Standard_EXPORT void ApplyWorldViewMatrix();
c827ea3a 678
679 //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
bc73b006 680 //! In "model -> world -> view -> projection" it performs:
681 //! model -> world -> view
5e94009d 682 Standard_EXPORT void ApplyModelViewMatrix();
c827ea3a 683
684 //! Applies matrix stored in ProjectionState to OpenGl.
bc73b006 685 //! In "model -> world -> view -> projection" it performs:
686 //! view -> projection
5e94009d 687 Standard_EXPORT void ApplyProjectionMatrix();
c827ea3a 688
cbf18624 689public:
690
7e7c2f0b 691 //! @return messenger instance
857ffd5e 692 inline const Handle(Message_Messenger)& Messenger() const
cbf18624 693 {
8fa64b52 694 return ::Message::DefaultMessenger();
cbf18624 695 }
696
697 //! Callback for GL_ARB_debug_output extension
698 //! @param theSource message source within GL_DEBUG_SOURCE_ enumeration
699 //! @param theType message type within GL_DEBUG_TYPE_ enumeration
700 //! @param theId message ID within source
701 //! @param theSeverity message severity within GL_DEBUG_SEVERITY_ enumeration
702 //! @param theMessage the message itself
703 Standard_EXPORT void PushMessage (const unsigned int theSource,
704 const unsigned int theType,
705 const unsigned int theId,
706 const unsigned int theSeverity,
707 const TCollection_ExtendedString& theMessage);
708
c87535af 709 //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
710 Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
711 const unsigned int theId);
b5ac8292 712
c87535af 713 //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
714 Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
715 const unsigned int theId);
b5ac8292 716
c8365a1c 717 //! @return true if OpenGl context supports left and right rendering buffers.
718 Standard_Boolean HasStereoBuffers() const { return myIsStereoBuffers; }
b5ac8292 719
7d3e64ef 720public: //! @name methods to alter or retrieve current state
721
15669413 722 //! Return structure holding frame statistics.
723 const Handle(OpenGl_FrameStats)& FrameStats() const { return myFrameStats; }
724
5e30547b 725 //! Set structure holding frame statistics.
726 //! This call makes sense only if application defines OpenGl_FrameStats sub-class.
727 void SetFrameStats (const Handle(OpenGl_FrameStats)& theStats) { myFrameStats = theStats; }
728
3bffef55 729 //! Return cached viewport definition (x, y, width, height).
730 const Standard_Integer* Viewport() const { return myViewport; }
731
732 //! Resize the viewport (alias for glViewport).
733 //! @param theRect viewport definition (x, y, width, height)
734 Standard_EXPORT void ResizeViewport (const Standard_Integer theRect[4]);
735
56689b27 736 //! Return virtual viewport definition (x, y, width, height).
737 const Standard_Integer* VirtualViewport() const { return myViewportVirt; }
738
38a0206f 739 //! Return active read buffer.
740 Standard_Integer ReadBuffer() { return myReadBuffer; }
b5ac8292 741
38a0206f 742 //! Switch read buffer, wrapper for ::glReadBuffer().
743 Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
b5ac8292 744
a1073ae2 745 //! Return active draw buffer attached to a render target referred by index (layout location).
c3487460 746 Standard_Integer DrawBuffer (Standard_Integer theIndex = 0) const
a1073ae2 747 {
c3487460 748 return theIndex >= myDrawBuffers.Lower()
749 && theIndex <= myDrawBuffers.Upper()
750 ? myDrawBuffers.Value (theIndex)
7ce8fe05 751 : 0; // GL_NONE
a1073ae2 752 }
38a0206f 753
754 //! Switch draw buffer, wrapper for ::glDrawBuffer().
755 Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
756
a1073ae2 757 //! Switch draw buffer, wrapper for ::glDrawBuffers (GLsizei, const GLenum*).
758 Standard_EXPORT void SetDrawBuffers (const Standard_Integer theNb, const Standard_Integer* theDrawBuffers);
759
38a0206f 760 //! Switch read/draw buffers.
761 void SetReadDrawBuffer (const Standard_Integer theBuffer)
762 {
763 SetReadBuffer (theBuffer);
764 SetDrawBuffer (theBuffer);
765 }
b5ac8292 766
ba00aab7 767 //! Returns cached GL_FRAMEBUFFER_SRGB state.
768 //! If TRUE, GLSL program is expected to write linear RGB color.
769 //! Otherwise, GLSL program might need manually converting result color into sRGB color space.
770 bool IsFrameBufferSRGB() const { return myIsSRgbActive; }
771
772 //! Enables/disables GL_FRAMEBUFFER_SRGB flag.
773 //! This flag can be set to:
774 //! - TRUE when writing into offscreen FBO (always expected to be in sRGB or RGBF formats).
775 //! - TRUE when writing into sRGB-ready window buffer (might require choosing proper pixel format on window creation).
776 //! - FALSE if sRGB rendering is not supported or sRGB-not-ready window buffer is used for drawing.
f291ad25 777 //! @param[in] theIsFbo flag indicating writing into offscreen FBO (always expected sRGB-ready when sRGB FBO is supported)
778 //! or into window buffer (FALSE, sRGB-readiness might vary).
779 //! @param[in] theIsFboSRgb flag indicating off-screen FBO is sRGB-ready
e5c11edd 780 Standard_EXPORT void SetFrameBufferSRGB (bool theIsFbo, bool theIsFboSRgb = true);
ba00aab7 781
f88457e6 782 //! Return cached flag indicating writing into color buffer is enabled or disabled (glColorMask).
31174e1a 783 const NCollection_Vec4<bool>& ColorMaskRGBA() const { return myColorMask; }
f88457e6 784
785 //! Enable/disable writing into color buffer (wrapper for glColorMask).
31174e1a 786 Standard_EXPORT void SetColorMaskRGBA (const NCollection_Vec4<bool>& theToWriteColor);
787
788 //! Return cached flag indicating writing into color buffer is enabled or disabled (glColorMask).
789 bool ColorMask() const { return myColorMask.r(); }
790
791 //! Enable/disable writing into color buffer (wrapper for glColorMask).
792 //! Alpha component writes will be disabled unconditionally in case of caps->buffersOpaqueAlpha.
f88457e6 793 Standard_EXPORT bool SetColorMask (bool theToWriteColor);
794
2a332745 795 //! Return TRUE if GL_SAMPLE_ALPHA_TO_COVERAGE usage is allowed.
796 bool AllowSampleAlphaToCoverage() const { return myAllowAlphaToCov; }
797
798 //! Allow GL_SAMPLE_ALPHA_TO_COVERAGE usage.
799 void SetAllowSampleAlphaToCoverage (bool theToEnable) { myAllowAlphaToCov = theToEnable; }
800
c40eb6b9 801 //! Return GL_SAMPLE_ALPHA_TO_COVERAGE state.
802 bool SampleAlphaToCoverage() const { return myAlphaToCoverage; }
803
804 //! Enable/disable GL_SAMPLE_ALPHA_TO_COVERAGE.
805 Standard_EXPORT bool SetSampleAlphaToCoverage (bool theToEnable);
806
b6472664 807 //! Return back face culling state.
808 bool ToCullBackFaces() const { return myToCullBackFaces; }
809
810 //! Enable or disable back face culling (glEnable (GL_CULL_FACE)).
811 Standard_EXPORT void SetCullBackFaces (bool theToEnable);
812
b5ac8292 813 //! Fetch OpenGl context state. This class tracks value of several OpenGl
814 //! state variables. Consulting the cached values is quicker than
815 //! doing the same via OpenGl API. Call this method if any of the controlled
816 //! OpenGl state variables has a possibility of being out-of-date.
817 Standard_EXPORT void FetchState();
818
cc8cbabe 819 //! @return active textures
820 const Handle(OpenGl_TextureSet)& ActiveTextures() const { return myActiveTextures; }
821
72f6dc61 822 //! Bind specified texture set to current context taking into account active GLSL program.
823 Standard_DEPRECATED("BindTextures() with explicit GLSL program should be used instead")
824 Handle(OpenGl_TextureSet) BindTextures (const Handle(OpenGl_TextureSet)& theTextures)
825 {
826 return BindTextures (theTextures, myActiveProgram);
827 }
828
829 //! Bind specified texture set to current context, or unbind previous one when NULL specified.
830 //! @param theTextures [in] texture set to bind
831 //! @param theProgram [in] program attributes; when not NULL,
832 //! mock textures will be bound to texture units expected by GLSL program, but undefined by texture set
833 //! @return previous texture set
834 Standard_EXPORT Handle(OpenGl_TextureSet) BindTextures (const Handle(OpenGl_TextureSet)& theTextures,
835 const Handle(OpenGl_ShaderProgram)& theProgram);
cc8cbabe 836
7d3e64ef 837 //! @return active GLSL program
838 const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
839 {
840 return myActiveProgram;
841 }
842
843 //! Bind specified program to current context,
844 //! or unbind previous one when NULL specified.
8625ef7e 845 //! @return true if some program is bound to context
846 Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
847
299e0ab9 848 //! Setup current shading material.
bf5f0ca2 849 Standard_EXPORT void SetShadingMaterial (const OpenGl_Aspects* theAspect,
a1073ae2 850 const Handle(Graphic3d_PresentationAttributes)& theHighlight);
851
852 //! Checks if transparency is required for the given aspect and highlight style.
bf5f0ca2 853 Standard_EXPORT static Standard_Boolean CheckIsTransparent (const OpenGl_Aspects* theAspect,
a1073ae2 854 const Handle(Graphic3d_PresentationAttributes)& theHighlight,
a71a71de 855 Standard_ShortReal& theAlphaFront,
856 Standard_ShortReal& theAlphaBack);
857
858 //! Checks if transparency is required for the given aspect and highlight style.
bf5f0ca2 859 static Standard_Boolean CheckIsTransparent (const OpenGl_Aspects* theAspect,
a71a71de 860 const Handle(Graphic3d_PresentationAttributes)& theHighlight)
861 {
862 Standard_ShortReal anAlphaFront = 1.0f, anAlphaBack = 1.0f;
863 return CheckIsTransparent (theAspect, theHighlight, anAlphaFront, anAlphaBack);
864 }
299e0ab9 865
8625ef7e 866 //! Setup current color.
867 Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
868
ac116c22 869 //! Setup type of line.
870 Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine theType,
871 const Standard_ShortReal theFactor = 1.0f);
872
3f1675c9 873 //! Setup stipple line pattern with 1.0f factor; wrapper for glLineStipple().
874 void SetLineStipple (const uint16_t thePattern) { SetLineStipple (1.0f, thePattern); }
875
876 //! Setup type of line; wrapper for glLineStipple().
877 Standard_EXPORT void SetLineStipple (const Standard_ShortReal theFactor,
878 const uint16_t thePattern);
879
ac116c22 880 //! Setup width of line.
881 Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
882
8625ef7e 883 //! Setup point size.
884 Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
7d3e64ef 885
fd59283a 886 //! Setup point sprite origin using GL_POINT_SPRITE_COORD_ORIGIN state:
887 //! - GL_UPPER_LEFT when GLSL program is active;
888 //! flipping should be handled in GLSL program for compatibility with OpenGL ES
889 //! - GL_LOWER_LEFT for FFP
890 Standard_EXPORT void SetPointSpriteOrigin();
891
79f4f036 892 //! Setup texture matrix to active GLSL program or to FFP global state using glMatrixMode (GL_TEXTURE).
faff3767 893 //! @param theParams [in] texture parameters
894 //! @param theIsTopDown [in] texture top-down flag
895 Standard_EXPORT void SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams,
896 const Standard_Boolean theIsTopDown);
79f4f036 897
4e1523ef 898 //! Bind default Vertex Array Object
899 Standard_EXPORT void BindDefaultVao();
900
a2e4f780 901 //! Default Frame Buffer Object.
902 const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
903 {
904 return myDefaultFbo;
905 }
906
907 //! Setup new Default Frame Buffer Object and return previously set.
908 //! This call doesn't change Active FBO!
909 Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
910
4e1523ef 911 //! Return debug context initialization state.
912 Standard_Boolean IsDebugContext() const
913 {
914 return myIsGlDebugCtx;
915 }
916
c357e426 917 Standard_EXPORT void EnableFeatures() const;
918
919 Standard_EXPORT void DisableFeatures() const;
920
56689b27 921 //! Return resolution for rendering text.
922 unsigned int Resolution() const { return myResolution; }
923
924 //! Resolution scale factor (rendered resolution to standard resolution).
925 //! This scaling factor for parameters like text size to be properly displayed on device (screen / printer).
926 Standard_ShortReal ResolutionRatio() const { return myResolutionRatio; }
927
928 //! Rendering scale factor (rendering viewport height to real window buffer height).
929 Standard_ShortReal RenderScale() const { return myRenderScale; }
930
931 //! Return TRUE if rendering scale factor is not 1.
932 Standard_Boolean HasRenderScale() const { return Abs (myRenderScale - 1.0f) > 0.0001f; }
933
934 //! Rendering scale factor (inverted value).
935 Standard_ShortReal RenderScaleInv() const { return myRenderScaleInv; }
936
2a332745 937 //! Return scale factor for line width.
938 Standard_ShortReal LineWidthScale() const { return myLineWidthScale; }
939
56689b27 940 //! Set resolution ratio.
941 //! Note that this method rounds @theRatio to nearest integer.
942 void SetResolution (unsigned int theResolution,
943 Standard_ShortReal theRatio,
944 Standard_ShortReal theScale)
945 {
946 myResolution = (unsigned int )(theScale * theResolution + 0.5f);
947 myRenderScale = theScale;
948 myRenderScaleInv = 1.0f / theScale;
949 SetResolutionRatio (theRatio * theScale);
950 }
951
75c262a9 952 //! Set resolution ratio.
953 //! Note that this method rounds @theRatio to nearest integer.
954 void SetResolutionRatio (const Standard_ShortReal theRatio)
955 {
56689b27 956 myResolutionRatio = theRatio;
957 myLineWidthScale = Max (1.0f, std::floor (theRatio + 0.5f));
75c262a9 958 }
959
2a332745 960 //! Return line feater width in pixels.
961 Standard_ShortReal LineFeather() const { return myLineFeather; }
962
963 //! Set line feater width.
964 void SetLineFeather(Standard_ShortReal theValue) { myLineFeather = theValue; }
965
d4cefcc0 966 //! Wrapper over glGetBufferSubData(), implemented as:
967 //! - OpenGL 1.5+ (desktop) via glGetBufferSubData();
968 //! - OpenGL ES 3.0+ via glMapBufferRange();
969 //! - WebGL 2.0+ via gl.getBufferSubData().
7ce8fe05 970 //! @param[in] theTarget target buffer to map {GLenum}
971 //! @param[in] theOffset offset to the beginning of sub-data {GLintptr}
972 //! @param[in] theSize number of bytes to read {GLsizeiptr}
973 //! @param[out] theData destination pointer to fill
d4cefcc0 974 //! @return FALSE if functionality is unavailable
7ce8fe05 975 Standard_EXPORT bool GetBufferSubData (unsigned int theTarget, intptr_t theOffset, intptr_t theSize, void* theData);
d4cefcc0 976
c39bb31b 977 //! Return Graphics Driver's vendor.
978 const TCollection_AsciiString& Vendor() const { return myVendor; }
979
bc73b006 980 //! Dumps the content of me into the stream
981 Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
982
983 //! Dumps the content of openGL state into the stream
8f7159cb 984 Standard_EXPORT void DumpJsonOpenGlState (Standard_OStream& theOStream, Standard_Integer theDepth = -1);
bc73b006 985
08669adf 986 //! Set GL_SHADE_MODEL value.
987 Standard_EXPORT void SetShadeModel (Graphic3d_TypeOfShadingModel theModel);
988
5f8b738e 989private:
990
991 //! Wrapper to system function to retrieve GL function pointer by name.
992 Standard_EXPORT void* findProc (const char* theFuncName);
993
ee51a9fe 994 //! Print error if not all functions have been exported by context for reported version.
995 //! Note that this will never happen when using GLX, since returned functions can not be validated.
996 //! @param theGlVerMajor the OpenGL major version with missing functions
997 //! @param theGlVerMinor the OpenGL minor version with missing functions
0ae9ac21 998 //! @param theLastFailedProc function name which cannot be found
999 Standard_EXPORT void checkWrongVersion (Standard_Integer theGlVerMajor, Standard_Integer theGlVerMinor,
1000 const char* theLastFailedProc);
ee51a9fe 1001
5f8b738e 1002 //! Private initialization function that should be called only once.
4e1523ef 1003 Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
5f8b738e 1004
01ca42b2 1005public: //! @name core profiles
1006
43eddb47 1007 OpenGl_GlCore11* core11ffp; //!< OpenGL 1.1 core functionality
01ca42b2 1008 OpenGl_GlCore11Fwd* core11fwd; //!< OpenGL 1.1 without deprecated entry points
43eddb47 1009 OpenGl_GlCore15* core15; //!< OpenGL 1.5 without deprecated entry points
1010 OpenGl_GlCore20* core20; //!< OpenGL 2.0 without deprecated entry points
1011 OpenGl_GlCore30* core30; //!< OpenGL 3.0 without deprecated entry points
01ca42b2 1012 OpenGl_GlCore32* core32; //!< OpenGL 3.2 core profile
25ef750e 1013 OpenGl_GlCore33* core33; //!< OpenGL 3.3 core profile
01ca42b2 1014 OpenGl_GlCore41* core41; //!< OpenGL 4.1 core profile
01ca42b2 1015 OpenGl_GlCore42* core42; //!< OpenGL 4.2 core profile
01ca42b2 1016 OpenGl_GlCore43* core43; //!< OpenGL 4.3 core profile
01ca42b2 1017 OpenGl_GlCore44* core44; //!< OpenGL 4.4 core profile
9491df8c 1018 OpenGl_GlCore45* core45; //!< OpenGL 4.5 core profile
952a0565 1019 OpenGl_GlCore46* core46; //!< OpenGL 4.6 core profile
43eddb47 1020
1021 OpenGl_GlCore15* core15fwd; //!< obsolete entry left for code portability; core15 should be used instead
1022 OpenGl_GlCore20* core20fwd; //!< obsolete entry left for code portability; core20 should be used instead
5f8b738e 1023
58655684 1024 Handle(OpenGl_Caps) caps; //!< context options
1025
01ca42b2 1026public: //! @name extensions
5f8b738e 1027
d4cefcc0 1028 Standard_Boolean hasGetBufferData; //!< flag indicating if GetBufferSubData() is supported
e1d17ceb 1029 Standard_Boolean hasPackRowLength; //!< supporting of GL_PACK_ROW_LENGTH parameters (any desktop OpenGL; OpenGL ES 3.0)
1030 Standard_Boolean hasUnpackRowLength; //!< supporting of GL_UNPACK_ROW_LENGTH parameters (any desktop OpenGL; OpenGL ES 3.0)
a1073ae2 1031 Standard_Boolean hasHighp; //!< highp in GLSL ES fragment shader is supported
1032 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)
1033 Standard_Boolean hasTexRGBA8; //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
67312b79 1034 Standard_Boolean hasTexFloatLinear; //!< texture-filterable state for 32-bit floating texture formats (always on desktop, GL_OES_texture_float_linear within OpenGL ES)
eee6a80a 1035 Standard_Boolean hasTexSRGB; //!< sRGB texture formats (desktop OpenGL 2.1, OpenGL ES 3.0 or OpenGL ES 2.0 + GL_EXT_sRGB)
ba00aab7 1036 Standard_Boolean hasFboSRGB; //!< sRGB FBO render targets (desktop OpenGL 2.1, OpenGL ES 3.0)
1037 Standard_Boolean hasSRGBControl; //!< sRGB write control (any desktop OpenGL, OpenGL ES + GL_EXT_sRGB_write_control extension)
e1d17ceb 1038 Standard_Boolean hasFboRenderMipmap; //!< FBO render target could be non-zero mipmap level of texture
13b36bb1 1039 OpenGl_FeatureFlag hasFlatShading; //!< Complex flag indicating support of Flat shading (Graphic3d_TypeOfShadingModel_Phong) (always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_standard_derivatives)
59515ca6 1040 OpenGl_FeatureFlag hasGlslBitwiseOps; //!< GLSL supports bitwise operations; OpenGL 3.0 / OpenGL ES 3.0 (GLSL 130 / GLSL ES 300) or OpenGL 2.1 + GL_EXT_gpu_shader4
a1073ae2 1041 OpenGl_FeatureFlag hasDrawBuffers; //!< Complex flag indicating support of multiple draw buffers (desktop OpenGL 2.0, OpenGL ES 3.0, GL_ARB_draw_buffers, GL_EXT_draw_buffers)
1042 OpenGl_FeatureFlag hasFloatBuffer; //!< Complex flag indicating support of float color buffer format (desktop OpenGL 3.0, GL_ARB_color_buffer_float, GL_EXT_color_buffer_float)
1043 OpenGl_FeatureFlag hasHalfFloatBuffer; //!< Complex flag indicating support of half-float color buffer format (desktop OpenGL 3.0, GL_ARB_color_buffer_float, GL_EXT_color_buffer_half_float)
1044 OpenGl_FeatureFlag hasSampleVariables; //!< Complex flag indicating support of MSAA variables in GLSL shader (desktop OpenGL 4.0, GL_ARB_sample_shading)
2a332745 1045 OpenGl_FeatureFlag hasGeometryStage; //!< Complex flag indicating support of Geometry shader (desktop OpenGL 3.2, OpenGL ES 3.2, GL_EXT_geometry_shader)
a1073ae2 1046 Standard_Boolean arbDrawBuffers; //!< GL_ARB_draw_buffers
1047 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
1048 Standard_Boolean arbTexRG; //!< GL_ARB_texture_rg
67312b79 1049 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); @sa hasTexFloatLinear for linear filtering support
cc8cbabe 1050 OpenGl_ArbSamplerObject* arbSamplerObject; //!< GL_ARB_sampler_objects (on desktop OpenGL - since 3.3 or as extension GL_ARB_sampler_objects; on OpenGL ES - since 3.0)
a1073ae2 1051 OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
872f98d9 1052 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object (on desktop OpenGL - since 3.1 or as extension GL_ARB_texture_buffer_object; on OpenGL ES - since 3.2)
93cdaa76 1053 Standard_Boolean arbTboRGB32; //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0 (on OpenGL ES - since 3.2)
e70625d6 1054 Standard_Boolean arbClipControl; //!< GL_ARB_clip_control, in core since 4.5
e1d17ceb 1055 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced (on desktop OpenGL - since 3.1 or as extension GL_ARB_draw_instanced; on OpenGL ES - since 3.0 or as extension GL_ANGLE_instanced_arrays to WebGL 1.0)
872f98d9 1056 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output (on desktop OpenGL - since 4.3 or as extension GL_ARB_debug_output; on OpenGL ES - since 3.2 or as extension GL_KHR_debug)
a1073ae2 1057 OpenGl_ArbFBO* arbFBO; //!< GL_ARB_framebuffer_object
1058 OpenGl_ArbFBOBlit* arbFBOBlit; //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
1059 Standard_Boolean arbSampleShading; //!< GL_ARB_sample_shading
4b52faa5 1060 Standard_Boolean arbDepthClamp; //!< GL_ARB_depth_clamp (on desktop OpenGL - since 3.2 or as extensions GL_ARB_depth_clamp,NV_depth_clamp; unavailable on OpenGL ES)
a1073ae2 1061 Standard_Boolean extFragDepth; //!< GL_EXT_frag_depth on OpenGL ES 2.0 (gl_FragDepthEXT built-in variable, before OpenGL ES 3.0)
1062 Standard_Boolean extDrawBuffers; //!< GL_EXT_draw_buffers
1063 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
1064 Standard_Boolean extBgra; //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
1065 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
1066 Standard_Boolean extPDS; //!< GL_EXT_packed_depth_stencil
1067 Standard_Boolean atiMem; //!< GL_ATI_meminfo
1068 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
1069 Standard_Boolean oesSampleVariables; //!< GL_OES_sample_variables
8c3237d4 1070 Standard_Boolean oesStdDerivatives; //!< GL_OES_standard_derivatives
5f8b738e 1071
8613985b 1072public: //! @name public properties tracking current state
1073
1074 OpenGl_MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
1075 OpenGl_MatrixState<Standard_ShortReal> WorldViewState; //!< state of orientation matrix
1076 OpenGl_MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection matrix
1077
5e27df78 1078private: // system-dependent fields
2166f0fa 1079
b69e576a 1080 Aspect_Drawable myWindow; //!< surface EGLSurface | HWND | GLXDrawable
1081 Aspect_Display myDisplay; //!< display EGLDisplay | HDC | Display*
1082 Aspect_RenderingContext myGContext; //!< rendering context EGLContext | HGLRC | GLXContext | EAGLContext* | NSOpenGLContext*
2bd4c032 1083
5e27df78 1084private: // context info
1085
c04c30b3 1086 typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Standard_Integer> > OpenGl_DelayReleaseMap;
c04c30b3 1087 typedef NCollection_Shared< NCollection_List<Handle(OpenGl_Resource)> > OpenGl_ResourcesStack;
5e27df78 1088
a174a3c5 1089 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
1090 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
3125ebb6 1091 Handle(OpenGl_ResourcesStack) myUnusedResources; //!< stack of resources for delayed clean up
5e27df78 1092
30f0ad28 1093 OpenGl_Clipping myClippingState; //!< state of clip planes
4269bd1b 1094
7d9e854b 1095 void* myGlLibHandle; //!< optional handle to GL library
7ce8fe05 1096 std::unique_ptr<OpenGl_GlFunctions>
7d9e854b 1097 myFuncs; //!< mega structure for all GL functions
c8365a1c 1098 Aspect_GraphicsLibrary myGapi; //!< GAPI name
faff3767 1099 Handle(Image_SupportedFormats)
1100 mySupportedFormats; //!< map of supported texture formats
7d9e854b 1101 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
1102 Standard_Integer myTexClamp; //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
1103 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
cc8cbabe 1104 Standard_Integer myMaxTexCombined; //!< value for GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
5c225e8e 1105 Standard_Integer myMaxTexUnitsFFP; //!< value for GL_MAX_TEXTURE_UNITS (fixed-function pipeline only)
6997ff1c 1106 Standard_Integer myMaxDumpSizeX; //!< maximum FBO width for image dump
1107 Standard_Integer myMaxDumpSizeY; //!< maximum FBO height for image dump
7d9e854b 1108 Standard_Integer myMaxClipPlanes; //!< value for GL_MAX_CLIP_PLANES
3c4b62a4 1109 Standard_Integer myMaxMsaaSamples; //!< value for GL_MAX_SAMPLES
a1073ae2 1110 Standard_Integer myMaxDrawBuffers; //!< value for GL_MAX_DRAW_BUFFERS
1111 Standard_Integer myMaxColorAttachments; //!< value for GL_MAX_COLOR_ATTACHMENTS
7d9e854b 1112 Standard_Integer myGlVerMajor; //!< cached GL version major number
1113 Standard_Integer myGlVerMinor; //!< cached GL version minor number
1114 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
1115 Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
c8365a1c 1116 Standard_Boolean myHasMsaaTextures; //!< context supports MSAA textures
7d9e854b 1117 Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
1118 //!< Used to tell OpenGl that normals should be normalized
72f6dc61 1119 Graphic3d_TextureUnit mySpriteTexUnit; //!< sampler2D occSamplerPointSprite, texture unit for point sprite texture
2166f0fa 1120
e084dbbc 1121 Standard_Boolean myHasRayTracing; //! indicates whether ray tracing mode is supported
1122 Standard_Boolean myHasRayTracingTextures; //! indicates whether textures in ray tracing mode are supported
1123 Standard_Boolean myHasRayTracingAdaptiveSampling; //! indicates whether adaptive screen sampling in ray tracing mode is supported
1124 Standard_Boolean myHasRayTracingAdaptiveSamplingAtomic; //! indicates whether atomic adaptive screen sampling in ray tracing mode is supported
3a9b5dc8 1125
67312b79 1126 Standard_Boolean myHasPBR; //!< indicates whether PBR shading model is supported
72f6dc61 1127 Graphic3d_TextureUnit myPBREnvLUTTexUnit; //!< sampler2D occEnvLUT, texture unit where environment lookup table is expected to be binded (0 if PBR is not supported)
1128 Graphic3d_TextureUnit myPBRDiffIBLMapSHTexUnit; //!< sampler2D occDiffIBLMapSHCoeffs, texture unit where diffuse (irradiance) IBL map's spherical harmonics coefficients is expected to be binded
67312b79 1129 //! (0 if PBR is not supported)
72f6dc61 1130 Graphic3d_TextureUnit myPBRSpecIBLMapTexUnit; //!< samplerCube occSpecIBLMap, texture unit where specular IBL map is expected to be binded (0 if PBR is not supported)
d84e8669 1131 Graphic3d_TextureUnit myShadowMapTexUnit; //!< sampler2D occShadowMapSampler
67312b79 1132
78c4e836 1133 Graphic3d_TextureUnit myDepthPeelingDepthTexUnit; //!< sampler2D occDepthPeelingDepth, texture unit for Depth Peeling lookups
1134 Graphic3d_TextureUnit myDepthPeelingFrontColorTexUnit; //!< sampler2D occDepthPeelingFrontColor, texture unit for Depth Peeling lookups
1135
30f0ad28 1136 Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
1137
7d3e64ef 1138private: //! @name fields tracking current state
1139
b40cdc2b 1140 Handle(Graphic3d_Camera) myCamera; //!< active camera object
15669413 1141 Handle(OpenGl_FrameStats) myFrameStats; //!< structure accumulating frame statistics
6d0e6be5 1142 Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
cc8cbabe 1143 Handle(OpenGl_TextureSet) myActiveTextures; //!< currently bound textures
1144 //!< currently active sampler objects
72f6dc61 1145 Standard_Integer myActiveMockTextures; //!< currently active mock sampler objects
6d0e6be5 1146 Handle(OpenGl_FrameBuffer) myDefaultFbo; //!< default Frame Buffer Object
1147 Handle(OpenGl_LineAttributes) myHatchStyles; //!< resource holding predefined hatch styles patterns
76fada68 1148 Standard_Integer myActiveHatchType; //!< currently activated type of polygon hatch
1149 Standard_Boolean myHatchIsEnabled; //!< current enabled state of polygon hatching rasterization
72f6dc61 1150 Handle(OpenGl_Texture) myTextureRgbaBlack;//!< mock black texture returning (0, 0, 0, 0)
1151 Handle(OpenGl_Texture) myTextureRgbaWhite;//!< mock white texture returning (1, 1, 1, 1)
3bffef55 1152 Standard_Integer myViewport[4]; //!< current viewport
56689b27 1153 Standard_Integer myViewportVirt[4]; //!< virtual viewport
6d0e6be5 1154 Standard_Integer myPointSpriteOrig; //!< GL_POINT_SPRITE_COORD_ORIGIN state (GL_UPPER_LEFT by default)
1155 Standard_Integer myRenderMode; //!< value for active rendering mode
08669adf 1156 Standard_Integer myShadeModel; //!< currently used shade model (glShadeModel)
6d0e6be5 1157 Standard_Integer myPolygonMode; //!< currently used polygon rasterization mode (glPolygonMode)
8d1a539c 1158 Graphic3d_PolygonOffset myPolygonOffset; //!< currently applied polygon offset
6d0e6be5 1159 bool myToCullBackFaces; //!< back face culling mode enabled state (glIsEnabled (GL_CULL_FACE))
1160 Standard_Integer myReadBuffer; //!< current read buffer
c3487460 1161 NCollection_Array1<Standard_Integer>
1162 myDrawBuffers; //!< current draw buffers
6d0e6be5 1163 unsigned int myDefaultVao; //!< default Vertex Array Object
31174e1a 1164 NCollection_Vec4<bool> myColorMask; //!< flag indicating writing into color buffer is enabled or disabled (glColorMask)
2a332745 1165 Standard_Boolean myAllowAlphaToCov; //!< flag allowing GL_SAMPLE_ALPHA_TO_COVERAGE usage
c40eb6b9 1166 Standard_Boolean myAlphaToCoverage; //!< flag indicating GL_SAMPLE_ALPHA_TO_COVERAGE state
6d0e6be5 1167 Standard_Boolean myIsGlDebugCtx; //!< debug context initialization state
ba00aab7 1168 Standard_Boolean myIsSRgbWindow; //!< indicates that window buffer is sRGB-ready
1169 Standard_Boolean myIsSRgbActive; //!< flag indicating GL_FRAMEBUFFER_SRGB state
6d0e6be5 1170 TCollection_AsciiString myVendor; //!< Graphics Driver's vendor
1171 TColStd_PackedMapOfInteger myFilters[6]; //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
56689b27 1172 unsigned int myResolution; //!< Pixels density (PPI), defines scaling factor for parameters like text size
6d0e6be5 1173 Standard_ShortReal myResolutionRatio; //!< scaling factor for parameters like text size
56689b27 1174 //! to be properly displayed on device (screen / printer)
1175 Standard_ShortReal myLineWidthScale; //!< scaling factor for line width
2a332745 1176 Standard_ShortReal myLineFeather; //!< line feater width in pixels
56689b27 1177 Standard_ShortReal myRenderScale; //!< scaling factor for rendering resolution
1178 Standard_ShortReal myRenderScaleInv; //!< scaling factor for rendering resolution (inverted value)
941f6cae 1179 OpenGl_Material myMaterial; //!< current front/back material state (cached to reduce GL context updates)
c827ea3a 1180
5e27df78 1181private:
1182
1183 //! Copying allowed only within Handles
1184 OpenGl_Context (const OpenGl_Context& );
1185 OpenGl_Context& operator= (const OpenGl_Context& );
1186
2166f0fa
SK
1187};
1188
1189#endif // _OpenGl_Context_H__