0023226: Extend OpenGl_Context to store map of shared GPU resources
[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>
5e27df78 28#include <Handle_OpenGl_Context.hxx>
29#include <NCollection_DataMap.hxx>
30#include <NCollection_Handle.hxx>
31#include <NCollection_Queue.hxx>
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;
5f8b738e 46struct OpenGl_ExtFBO;
2166f0fa 47
5f8b738e 48//! This class generalize access to the GL context and available extensions.
49//!
50//! Functions are grouped into structures and accessed as fields.
51//! You should check the group for NULL before usage (if group is not NULL
52//! then all functions are available):
53//! @code
54//! if (myContext->core20 != NULL)
55//! {
56//! myGlProgram = myContext->core20->glCreateProgram();
57//! .. do more stuff ..
58//! }
59//! else
60//! {
61//! .. compatibility with outdated configurations ..
62//! }
63//! @endcode
64//!
65//! Current implementation provide access to OpenGL core functionality up to 2.0 version
66//! (core12, core13, core14, core15, fields core20).
67//! within several extensions (arbVBO, extFBO, etc.).
68//!
69//! Simplified extensions classification:
70//! - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
71//! - prefixed with EXT are accepted by 2+ vendors;
72//! - prefixed with ARB are accepted by Architecture Review Board and are candidates
73//! for inclusion into GL core functionality.
74//! Some functionality can be represented in several extensions simultaneously.
75//! In this case developer should be careful because different specification may differ
76//! in aspects (like enumeration values and error-handling).
77//!
78//! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
79//! with different capabilities. Thats why OpenGl_Context should be initialized and used
80//! for each GL context individually.
2166f0fa
SK
81class OpenGl_Context : public Standard_Transient
82{
83public:
84
5f8b738e 85 //! Empty constructor. You should call Init() to perform initialization with bound GL context.
86 Standard_EXPORT OpenGl_Context();
87
88 //! Destructor.
89 Standard_EXPORT virtual ~OpenGl_Context();
2166f0fa 90
5e27df78 91 //! Share GL context resources.
92 //! theShareCtx - handle to context to retrieve handles to shared resources.
93 Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
94
2166f0fa
SK
95 //! Initialize available extensions.
96 //! GL context should be active!
f0430952 97 Standard_EXPORT Standard_Boolean Init();
2166f0fa 98
2bd4c032 99#if (defined(_WIN32) || defined(__WIN32__))
f0430952 100 Standard_EXPORT Standard_Boolean Init (const Aspect_Handle theWindow,
101 const Aspect_Handle theWindowDC,
102 const Aspect_RenderingContext theGContext);
2bd4c032 103#else
f0430952 104 Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable theWindow,
105 const Aspect_Display theDisplay,
106 const Aspect_RenderingContext theGContext);
2bd4c032 107#endif
108
5f8b738e 109 //! Check if theExtName extension is supported by active GL context.
2bd4c032 110 Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
2166f0fa
SK
111
112 //! Auxiliary template to retrieve GL function pointer.
5f8b738e 113 //! Pointer to function retrieved from library is statically casted
114 //! to requested type - there no way to check real signature of exported function.
2166f0fa
SK
115 //! The context should be bound before call.
116 template <typename FuncType_t>
5f8b738e 117 Standard_Boolean FindProc (const char* theFuncName,
118 FuncType_t& theFuncPtr)
2166f0fa 119 {
5f8b738e 120 theFuncPtr = (FuncType_t )findProc (theFuncName);
2166f0fa
SK
121 return (theFuncPtr != NULL);
122 }
123
2bd4c032 124 //! @return true if detected GL version is greater or equal to requested one.
125 inline Standard_Boolean IsGlGreaterEqual (const Standard_Integer theVerMajor,
126 const Standard_Integer theVerMinor)
5f8b738e 127 {
128 return (myGlVerMajor > theVerMajor)
129 || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
130 }
131
132 //! Clean up errors stack for this GL context (glGetError() in loop).
133 Standard_EXPORT void ResetErrors();
134
86fa64d9 135 //! This method uses system-dependent API to retrieve information
136 //! about GL context bound to the current thread.
137 //! @return true if current thread is bound to this GL context
138 Standard_EXPORT Standard_Boolean IsCurrent() const;
139
2bd4c032 140 //! Activates current context.
141 //! Class should be initialized with appropriate info.
142 Standard_EXPORT Standard_Boolean MakeCurrent();
143
5e27df78 144 //! Swap front/back buffers for this GL context (should be activated before!).
145 Standard_EXPORT void SwapBuffers();
146
664cae74 147 //! Return true if active mode is GL_FEEDBACK (cached state)
148 Standard_EXPORT Standard_Boolean IsFeedback() const;
149
150 //! Setup feedback mode cached state
151 Standard_EXPORT void SetFeedback (const Standard_Boolean theFeedbackOn);
152
f0430952 153 //! This function retrieves information from GL about free GPU memory that is:
154 //! - OS-dependent. On some OS it is per-process and on others - for entire system.
155 //! - Vendor-dependent. Currently available only on NVIDIA and AMD/ATi drivers only.
156 //! - Numbers meaning may vary.
157 //! You should use this info only for diagnostics purposes.
158 //! @return free GPU dedicated memory in bytes.
159 Standard_EXPORT Standard_Size AvailableMemory() const;
160
161 //! This function retrieves information from GL about GPU memory
162 //! and contains more vendor-specific values than AvailableMemory().
163 Standard_EXPORT TCollection_AsciiString MemoryInfo() const;
164
5e27df78 165 //! Access shared resource by its name.
166 //! @param theKey - unique identifier;
167 //! @return handle to shared resource or NULL.
168 Standard_EXPORT const Handle(OpenGl_Resource)& GetResource (const TCollection_AsciiString& theKey) const;
169
170 //! Access shared resource by its name.
171 //! @param theKey - unique identifier;
172 //! @param theValue - handle to fill;
173 //! @return true if resource was shared.
174 template<typename TheHandleType>
175 Standard_Boolean GetResource (const TCollection_AsciiString& theKey,
176 TheHandleType& theValue) const
177 {
178 const Handle(OpenGl_Resource)& aResource = GetResource (theKey);
179 if (aResource.IsNull())
180 {
181 return Standard_False;
182 }
183
184 theValue = TheHandleType::DownCast (aResource);
185 return !theValue.IsNull();
186 }
187
188 //! Register shared resource.
189 //! Notice that after registration caller shouldn't release it by himself -
190 //! it will be automatically released on context destruction.
191 //! @param theKey - unique identifier, shouldn't be empty;
192 //! @param theResource - new resource to register, shouldn't be NULL.
193 Standard_EXPORT Standard_Boolean ShareResource (const TCollection_AsciiString& theKey,
194 const Handle(OpenGl_Resource)& theResource);
195
196 //! Release shared resource.
197 //! If there are more than one reference to this resource
198 //! (also used by some other existing object) then call will be ignored.
199 //! This means that current object itself should nullify handle before this call.
200 //! Notice that this is unrecommended operation at all and should be used
201 //! only in case of fat resources to release memory for other needs.
202 //! @param theKey - unique identifier.
203 Standard_EXPORT void ReleaseResource (const TCollection_AsciiString& theKey);
204
205 //! Append resource to queue for delayed clean up.
206 //! Resources in this queue will be released at next redraw call.
207 Standard_EXPORT void DelayedRelease (Handle(OpenGl_Resource)& theResource);
208
209 //! Clean up the delayed release queue.
210 Standard_EXPORT void ReleaseDelayed();
211
5f8b738e 212private:
213
214 //! Wrapper to system function to retrieve GL function pointer by name.
215 Standard_EXPORT void* findProc (const char* theFuncName);
216
217 //! Read OpenGL version information from active context.
218 Standard_EXPORT void readGlVersion();
219
220 //! Private initialization function that should be called only once.
221 Standard_EXPORT void init();
222
223public: // core profiles
224
225 OpenGl_GlCore12* core12;
226 OpenGl_GlCore13* core13;
227 OpenGl_GlCore14* core14;
228 OpenGl_GlCore15* core15;
229 OpenGl_GlCore20* core20;
230
231public: // extensions
232
f0430952 233 OpenGl_ArbVBO* arbVBO; //!< GL_ARB_vertex_buffer_object
5e27df78 234 OpenGl_ArbTBO* arbTBO; //!< GL_ARB_texture_buffer_object
235 OpenGl_ArbIns* arbIns; //!< GL_ARB_draw_instanced
f0430952 236 OpenGl_ExtFBO* extFBO; //!< GL_EXT_framebuffer_object
237 Standard_Boolean atiMem; //!< GL_ATI_meminfo
238 Standard_Boolean nvxMem; //!< GL_NVX_gpu_memory_info
5f8b738e 239
5e27df78 240private: // system-dependent fields
2166f0fa 241
2bd4c032 242#if (defined(_WIN32) || defined(__WIN32__))
243 Aspect_Handle myWindow; //!< window handle (owner of GL context) : HWND
244 Aspect_Handle myWindowDC; //!< Device Descriptor handle : HDC
245 Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
246#else
247 Aspect_Drawable myWindow; //!< window handle (owner of GL context) : GLXDrawable
248 Aspect_Display myDisplay; //!< connection to the X-server : Display*
249 Aspect_RenderingContext myGContext; //!< X-GLX rendering context : GLXContext
250#endif
251
5e27df78 252private: // context info
253
254 typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
255 typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
256 typedef NCollection_Queue<Handle(OpenGl_Resource)> OpenGl_ResourcesQueue;
257 typedef NCollection_Handle<OpenGl_ResourcesQueue> Handle(OpenGl_ResourcesQueue);
258
259 Handle(OpenGl_ResourcesMap) mySharedResources; //!< shared resourced with unique identification key
260 Handle(OpenGl_ResourcesQueue) myReleaseQueue; //!< queue of resources for delayed clean up
261
5f8b738e 262 void* myGlLibHandle; //!< optional handle to GL library
263 OpenGl_GlCore20* myGlCore20; //!< common structure for GL core functions upto 2.0
264 Standard_Integer myGlVerMajor; //!< cached GL version major number
265 Standard_Integer myGlVerMinor; //!< cached GL version minor number
664cae74 266 Standard_Boolean myIsFeedback; //!< flag indicates GL_FEEDBACK mode
f0430952 267 Standard_Boolean myIsInitialized; //!< flag indicates initialization state
2166f0fa 268
5e27df78 269private:
270
271 //! Copying allowed only within Handles
272 OpenGl_Context (const OpenGl_Context& );
273 OpenGl_Context& operator= (const OpenGl_Context& );
274
2166f0fa
SK
275public:
276
5f8b738e 277 DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
2166f0fa 278
5e27df78 279 friend class OpenGl_Window;
280
2166f0fa
SK
281};
282
283#endif // _OpenGl_Context_H__