0024005: Intersecting a slightly off angle plane with a cylinder takes 7+ seconds
[occt.git] / src / OpenGl / OpenGl_Context.hxx
CommitLineData
b311480e 1// Created on: 2012-01-26
2// Created by: Kirill GAVRILOV
3// Copyright (c) 2012-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
2166f0fa
SK
20#ifndef _OpenGl_Context_H__
21#define _OpenGl_Context_H__
22
2bd4c032 23#include <Aspect_Handle.hxx>
24#include <Aspect_Drawable.hxx>
25#include <Aspect_Display.hxx>
26#include <Aspect_RenderingContext.hxx>
5e27df78 27#include <Handle_OpenGl_Context.hxx>
28#include <NCollection_DataMap.hxx>
29#include <NCollection_Handle.hxx>
30#include <NCollection_Queue.hxx>
58655684 31#include <OpenGl_Caps.hxx>
5e27df78 32#include <OpenGl_Resource.hxx>
2166f0fa 33#include <Standard_Transient.hxx>
f0430952 34#include <TCollection_AsciiString.hxx>
2166f0fa
SK
35#include <Handle_OpenGl_Context.hxx>
36
5f8b738e 37//! Forward declarations
38struct OpenGl_GlCore12;
39struct OpenGl_GlCore13;
40struct OpenGl_GlCore14;
41struct OpenGl_GlCore15;
42struct OpenGl_GlCore20;
43struct OpenGl_ArbVBO;
5e27df78 44struct OpenGl_ArbTBO;
45struct OpenGl_ArbIns;
58655684 46struct OpenGl_ArbDbg;
5f8b738e 47struct OpenGl_ExtFBO;
37eb4787 48struct OpenGl_ExtGS;
2166f0fa 49
5f8b738e 50//! This class generalize access to the GL context and available extensions.
51//!
52//! Functions are grouped into structures and accessed as fields.
53//! You should check the group for NULL before usage (if group is not NULL
54//! then all functions are available):
55//! @code
56//! if (myContext->core20 != NULL)
57//! {
58//! myGlProgram = myContext->core20->glCreateProgram();
59//! .. do more stuff ..
60//! }
61//! else
62//! {
63//! .. compatibility with outdated configurations ..
64//! }
65//! @endcode
66//!
67//! Current implementation provide access to OpenGL core functionality up to 2.0 version
68//! (core12, core13, core14, core15, fields core20).
69//! within several extensions (arbVBO, extFBO, etc.).
70//!
71//! Simplified extensions classification:
72//! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
73//! - prefixed with EXT are accepted by 2+ vendors;
74//! - prefixed with ARB are accepted by Architecture Review Board and are candidates
75//! for inclusion into GL core functionality.
76//! Some functionality can be represented in several extensions simultaneously.
77//! In this case developer should be careful because different specification may differ
78//! in aspects (like enumeration values and error-handling).
79//!
80//! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
81//! with different capabilities. Thats why OpenGl_Context should be initialized and used
82//! for each GL context individually.
2166f0fa
SK
83class OpenGl_Context : public Standard_Transient
84{
a174a3c5 85public:
86
87 //! Function for getting power of to number larger or equal to input number.
88 //! @param theNumber number to 'power of two'
89 //! @param theThreshold upper threshold
90 //! @return power of two number
91 inline static Standard_Integer GetPowerOfTwo (const Standard_Integer theNumber,
92 const Standard_Integer theThreshold)
93 {
94 for (Standard_Integer p2 = 2; p2 <= theThreshold; p2 <<= 1)
95 {
96 if (theNumber <= p2)
97 {
98 return p2;
99 }
100 }
101 return theThreshold;
102 }
103
2166f0fa
SK
104public:
105
5f8b738e 106 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
58655684 107 Standard_EXPORT OpenGl_Context (const Handle(OpenGl_Caps)& theCaps = NULL);
5f8b738e 108
109 //! Destructor.
110 Standard_EXPORT virtual ~OpenGl_Context();
2166f0fa 111
5e27df78 112 //! Share GL context resources.
113 //! theShareCtx - handle to context to retrieve handles to shared resources.
114 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
115
2166f0fa
SK
116 //! Initialize available extensions.
117 //! GL context should be active!
f0430952 118 Standard_EXPORT Standard_Boolean Init();
2166f0fa 119
58655684 120#if defined(_WIN32)
f0430952 121 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
122 const Aspect_Handle theWindowDC,
123 const Aspect_RenderingContext theGContext);
4fe56619 124#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
125 Standard_EXPORT Standard_Boolean Init (const void* theGContext);
2bd4c032 126#else
f0430952 127 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
128 const Aspect_Display theDisplay,
129 const Aspect_RenderingContext theGContext);
2bd4c032 130#endif
131
5f8b738e 132 //! Check if theExtName extension is supported by active GL context.
2bd4c032 133 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
2166f0fa 134
58655684 135 //! Check if theExtName extension is in extensions string.
136 Standard_EXPORT static Standard_Boolean CheckExtension (const char* theExtString,
137 const char* theExtName);
138
2166f0fa 139 //! Auxiliary template to retrieve GL function pointer.
5f8b738e 140 //! Pointer to function retrieved from library is statically casted
141 //! to requested type - there no way to check real signature of exported function.
2166f0fa
SK
142 //! The context should be bound before call.
143 template <typename FuncType_t>
5f8b738e 144 Standard_Boolean FindProc (const char* theFuncName,
145 FuncType_t& theFuncPtr)
2166f0fa 146 {
5f8b738e 147 theFuncPtr = (FuncType_t )findProc (theFuncName);
2166f0fa
SK
148 return (theFuncPtr != NULL);
149 }
150
2bd4c032 151 //! @return true if detected GL version is greater or equal to requested one.
152 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
153 const Standard_Integer theVerMinor)
5f8b738e 154 {
155 return (myGlVerMajor > theVerMajor)
156 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
157 }
158
159 //! Clean up errors stack for this GL context (glGetError() in loop).
160 Standard_EXPORT void ResetErrors();
161
86fa64d9 162 //! This method uses system-dependent API to retrieve information
163 //! about GL context bound to the current thread.
164 //! @return true if current thread is bound to this GL context
165 Standard_EXPORT Standard_Boolean IsCurrent() const;
166
2bd4c032 167 //! Activates current context.
168 //! Class should be initialized with appropriate info.
169 Standard_EXPORT Standard_Boolean MakeCurrent();
170
5e27df78 171 //! Swap front/back buffers for this GL context (should be activated before!).
172 Standard_EXPORT void SwapBuffers();
173
664cae74 174 //! Return true if active mode is GL_FEEDBACK (cached state)
175 Standard_EXPORT Standard_Boolean IsFeedback() const;
176
177 //! Setup feedback mode cached state
178 Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
179
f0430952 180 //! This function retrieves information from GL about free GPU memory that is:
181 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
182 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
183 //! - Numbers meaning may vary.
184 //! You should use this info only for diagnostics purposes.
185 //! @return free GPU dedicated memory in bytes.
186 Standard_EXPORT Standard_Size AvailableMemory() const;
187
188 //! This function retrieves information from GL about GPU memory
189 //! and contains more vendor-specific values than AvailableMemory().
190 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
191
5e27df78 192 //! Access shared resource by its name.
193 //! @param theKey - unique identifier;
194 //! @return handle to shared resource or NULL.
195 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
196
197 //! Access shared resource by its name.
198 //! @param theKey - unique identifier;
199 //! @param theValue - handle to fill;
200 //! @return true if resource was shared.
201 template<typename TheHandleType>
202 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
203 TheHandleType& theValue) const
204 {
205 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
206 if (aResource.IsNull())
207 {
208 return Standard_False;
209 }
210
211 theValue = TheHandleType::DownCast (aResource);
212 return !theValue.IsNull();
213 }
214
215 //! Register shared resource.
216 //! Notice that after registration caller shouldn't release it by himself -
217 //! it will be automatically released on context destruction.
218 //! @param theKey - unique identifier, shouldn't be empty;
219 //! @param theResource - new resource to register, shouldn't be NULL.
220 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
221 const Handle(OpenGl_Resource)& theResource);
222
223 //! Release shared resource.
224 //! If there are more than one reference to this resource
225 //! (also used by some other existing object) then call will be ignored.
226 //! This means that current object itself should nullify handle before this call.
227 //! Notice that this is unrecommended operation at all and should be used
228 //! only in case of fat resources to release memory for other needs.
a174a3c5 229 //! @param theKey unique identifier
230 //! @param theToDelay postpone release until next redraw call
231 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey,
232 const Standard_Boolean theToDelay = Standard_False);
5e27df78 233
234 //! Append resource to queue for delayed clean up.
235 //! Resources in this queue will be released at next redraw call.
236 Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
237
238 //! Clean up the delayed release queue.
239 Standard_EXPORT void ReleaseDelayed();
240
bf75be98 241 //! @return maximum degree of anisotropy texture filter
242 Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
243
244 //! @return value for GL_MAX_TEXTURE_SIZE
245 Standard_EXPORT Standard_Integer MaxTextureSize() const;
246
5f8b738e 247private:
248
249 //! Wrapper to system function to retrieve GL function pointer by name.
250 Standard_EXPORT void* findProc (const char* theFuncName);
251
252 //! Read OpenGL version information from active context.
253 Standard_EXPORT void readGlVersion();
254
255 //! Private initialization function that should be called only once.
256 Standard_EXPORT void init();
257
258public: // core profiles
259
260 OpenGl_GlCore12* core12;
261 OpenGl_GlCore13* core13;
262 OpenGl_GlCore14* core14;
263 OpenGl_GlCore15* core15;
264 OpenGl_GlCore20* core20;
265
58655684 266 Handle(OpenGl_Caps) caps; //!< context options
267
5f8b738e 268public: // extensions
269
bf75be98 270 Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
271 OpenGl_ArbVBO* arbVBO; //!< GL_ARB_vertex_buffer_object
272 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
273 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
58655684 274 OpenGl_ArbDbg* arbDbg; //!< GL_ARB_debug_output
bf75be98 275 OpenGl_ExtFBO* extFBO; //!< GL_EXT_framebuffer_object
276 OpenGl_ExtGS* extGS; //!< GL_EXT_geometry_shader4
277 Standard_Boolean extBgra; //!< GL_EXT_bgra
278 Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
279 Standard_Boolean atiMem; //!< GL_ATI_meminfo
280 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
5f8b738e 281
5e27df78 282private: // system-dependent fields
2166f0fa 283
58655684 284#if defined(_WIN32)
2bd4c032 285 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
286 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
287 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
4fe56619 288#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
289 void* myGContext; //!< Rendering Context handle : NSOpenGLContext
2bd4c032 290#else
291 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
292 Aspect_Display myDisplay; //!< connection to the X-server : Display*
293 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
294#endif
295
5e27df78 296private: // context info
297
a174a3c5 298 typedef NCollection_DataMap<TCollection_AsciiString, Standard_Integer> OpenGl_DelayReleaseMap;
299 typedef NCollection_Handle<OpenGl_DelayReleaseMap> Handle(OpenGl_DelayReleaseMap);
5e27df78 300 typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
301 typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
302 typedef NCollection_Queue<Handle(OpenGl_Resource)> OpenGl_ResourcesQueue;
303 typedef NCollection_Handle<OpenGl_ResourcesQueue> Handle(OpenGl_ResourcesQueue);
304
a174a3c5 305 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resources with unique identification key
306 Handle(OpenGl_DelayReleaseMap) myDelayed; //!< shared resources for delayed release
307 Handle(OpenGl_ResourcesQueue) myReleaseQueue; //!< queue of resources for delayed clean up
5e27df78 308
5f8b738e 309 void* myGlLibHandle; //!< optional handle to GL library
310 OpenGl_GlCore20* myGlCore20; //!< common structure for GL core functions upto 2.0
bf75be98 311 Standard_Integer myAnisoMax; //!< maximum level of anisotropy texture filter
312 Standard_Integer myMaxTexDim; //!< value for GL_MAX_TEXTURE_SIZE
5f8b738e 313 Standard_Integer myGlVerMajor; //!< cached GL version major number
314 Standard_Integer myGlVerMinor; //!< cached GL version minor number
664cae74 315 Standard_Boolean myIsFeedback; //!< flag indicates GL_FEEDBACK mode
f0430952 316 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
2166f0fa 317
5e27df78 318private:
319
320 //! Copying allowed only within Handles
321 OpenGl_Context (const OpenGl_Context& );
322 OpenGl_Context& operator= (const OpenGl_Context& );
323
2166f0fa
SK
324public:
325
5f8b738e 326 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
2166f0fa 327
5e27df78 328 friend class OpenGl_Window;
329
2166f0fa
SK
330};
331
332#endif // _OpenGl_Context_H__