0023087: Upgrade of the OCCT test system
[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
21#ifndef _OpenGl_Context_H__
22#define _OpenGl_Context_H__
23
2bd4c032 24#include <Aspect_Handle.hxx>
25#include <Aspect_Drawable.hxx>
26#include <Aspect_Display.hxx>
27#include <Aspect_RenderingContext.hxx>
2166f0fa 28#include <Standard_Transient.hxx>
f0430952 29#include <TCollection_AsciiString.hxx>
2166f0fa
SK
30#include <Handle_OpenGl_Context.hxx>
31
5f8b738e 32//! Forward declarations
33struct OpenGl_GlCore12;
34struct OpenGl_GlCore13;
35struct OpenGl_GlCore14;
36struct OpenGl_GlCore15;
37struct OpenGl_GlCore20;
38struct OpenGl_ArbVBO;
39struct OpenGl_ExtFBO;
2166f0fa 40
5f8b738e 41//! This class generalize access to the GL context and available extensions.
42//!
43//! Functions are grouped into structures and accessed as fields.
44//! You should check the group for NULL before usage (if group is not NULL
45//! then all functions are available):
46//! @code
47//! if (myContext->core20 != NULL)
48//! {
49//! myGlProgram = myContext->core20->glCreateProgram();
50//! .. do more stuff ..
51//! }
52//! else
53//! {
54//! .. compatibility with outdated configurations ..
55//! }
56//! @endcode
57//!
58//! Current implementation provide access to OpenGL core functionality up to 2.0 version
59//! (core12, core13, core14, core15, fields core20).
60//! within several extensions (arbVBO, extFBO, etc.).
61//!
62//! Simplified extensions classification:
63//! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
64//! - prefixed with EXT are accepted by 2+ vendors;
65//! - prefixed with ARB are accepted by Architecture Review Board and are candidates
66//! for inclusion into GL core functionality.
67//! Some functionality can be represented in several extensions simultaneously.
68//! In this case developer should be careful because different specification may differ
69//! in aspects (like enumeration values and error-handling).
70//!
71//! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
72//! with different capabilities. Thats why OpenGl_Context should be initialized and used
73//! for each GL context individually.
2166f0fa
SK
74class OpenGl_Context : public Standard_Transient
75{
76public:
77
5f8b738e 78 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
79 Standard_EXPORT OpenGl_Context();
80
81 //! Destructor.
82 Standard_EXPORT virtual ~OpenGl_Context();
2166f0fa
SK
83
84 //! Initialize available extensions.
85 //! GL context should be active!
f0430952 86 Standard_EXPORT Standard_Boolean Init();
2166f0fa 87
2bd4c032 88#if (defined(_WIN32) || defined(__WIN32__))
f0430952 89 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
90 const Aspect_Handle theWindowDC,
91 const Aspect_RenderingContext theGContext);
2bd4c032 92#else
f0430952 93 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
94 const Aspect_Display theDisplay,
95 const Aspect_RenderingContext theGContext);
2bd4c032 96#endif
97
5f8b738e 98 //! Check if theExtName extension is supported by active GL context.
2bd4c032 99 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
2166f0fa
SK
100
101 //! Auxiliary template to retrieve GL function pointer.
5f8b738e 102 //! Pointer to function retrieved from library is statically casted
103 //! to requested type - there no way to check real signature of exported function.
2166f0fa
SK
104 //! The context should be bound before call.
105 template <typename FuncType_t>
5f8b738e 106 Standard_Boolean FindProc (const char* theFuncName,
107 FuncType_t& theFuncPtr)
2166f0fa 108 {
5f8b738e 109 theFuncPtr = (FuncType_t )findProc (theFuncName);
2166f0fa
SK
110 return (theFuncPtr != NULL);
111 }
112
2bd4c032 113 //! @return true if detected GL version is greater or equal to requested one.
114 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
115 const Standard_Integer theVerMinor)
5f8b738e 116 {
117 return (myGlVerMajor > theVerMajor)
118 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
119 }
120
121 //! Clean up errors stack for this GL context (glGetError() in loop).
122 Standard_EXPORT void ResetErrors();
123
86fa64d9 124 //! This method uses system-dependent API to retrieve information
125 //! about GL context bound to the current thread.
126 //! @return true if current thread is bound to this GL context
127 Standard_EXPORT Standard_Boolean IsCurrent() const;
128
2bd4c032 129 //! Activates current context.
130 //! Class should be initialized with appropriate info.
131 Standard_EXPORT Standard_Boolean MakeCurrent();
132
664cae74 133 //! Return true if active mode is GL_FEEDBACK (cached state)
134 Standard_EXPORT Standard_Boolean IsFeedback() const;
135
136 //! Setup feedback mode cached state
137 Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
138
f0430952 139 //! This function retrieves information from GL about free GPU memory that is:
140 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
141 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
142 //! - Numbers meaning may vary.
143 //! You should use this info only for diagnostics purposes.
144 //! @return free GPU dedicated memory in bytes.
145 Standard_EXPORT Standard_Size AvailableMemory() const;
146
147 //! This function retrieves information from GL about GPU memory
148 //! and contains more vendor-specific values than AvailableMemory().
149 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
150
5f8b738e 151private:
152
153 //! Wrapper to system function to retrieve GL function pointer by name.
154 Standard_EXPORT void* findProc (const char* theFuncName);
155
156 //! Read OpenGL version information from active context.
157 Standard_EXPORT void readGlVersion();
158
159 //! Private initialization function that should be called only once.
160 Standard_EXPORT void init();
161
162public: // core profiles
163
164 OpenGl_GlCore12* core12;
165 OpenGl_GlCore13* core13;
166 OpenGl_GlCore14* core14;
167 OpenGl_GlCore15* core15;
168 OpenGl_GlCore20* core20;
169
170public: // extensions
171
f0430952 172 OpenGl_ArbVBO* arbVBO; //!< GL_ARB_vertex_buffer_object
173 OpenGl_ExtFBO* extFBO; //!< GL_EXT_framebuffer_object
174 Standard_Boolean atiMem; //!< GL_ATI_meminfo
175 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
5f8b738e 176
177private:
2166f0fa 178
2bd4c032 179#if (defined(_WIN32) || defined(__WIN32__))
180 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
181 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
182 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
183#else
184 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
185 Aspect_Display myDisplay; //!< connection to the X-server : Display*
186 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
187#endif
188
5f8b738e 189 void* myGlLibHandle; //!< optional handle to GL library
190 OpenGl_GlCore20* myGlCore20; //!< common structure for GL core functions upto 2.0
191 Standard_Integer myGlVerMajor; //!< cached GL version major number
192 Standard_Integer myGlVerMinor; //!< cached GL version minor number
664cae74 193 Standard_Boolean myIsFeedback; //!< flag indicates GL_FEEDBACK mode
f0430952 194 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
2166f0fa
SK
195
196public:
197
5f8b738e 198 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
2166f0fa
SK
199
200};
201
202#endif // _OpenGl_Context_H__