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