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