forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSGLContext.h
288 lines (239 loc) · 8.24 KB
/
OSGLContext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
#ifndef OSGLCONTEXT_H
#define OSGLCONTEXT_H
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Global/Macros.h"
#if !defined(Q_MOC_RUN) && !defined(SBK_RUN)
#include <boost/scoped_ptr.hpp>
#include <boost/weak_ptr.hpp>
#endif
//========================================================================
// GLFW 3.2 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <[email protected]>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include <cstddef>
#include <string>
#include <vector>
#include <list>
#if !defined(Q_MOC_RUN) && !defined(SBK_RUN)
#include <boost/noncopyable.hpp>
#endif
#include "Engine/EngineFwd.h"
#include "Global/GLIncludes.h"
NATRON_NAMESPACE_ENTER
/* @brief Framebuffer configuration.
*
* This describes buffers and their sizes. It also contains
* a platform-specific ID used to map back to the backend API object.
*
* It is used to pass framebuffer parameters from shared code to the platform
* API and also to enumerate and select available framebuffer configs.
*/
class FramebufferConfig
{
public:
static const int ATTR_DONT_CARE = -1;
int redBits;
int greenBits;
int blueBits;
int alphaBits;
int depthBits;
int stencilBits;
int accumRedBits;
int accumGreenBits;
int accumBlueBits;
int accumAlphaBits;
int auxBuffers;
GLboolean stereo;
int samples;
GLboolean sRGB;
GLboolean doublebuffer;
uintptr_t handle;
FramebufferConfig();
};
class GLRendererID
{
public:
int renderID;
// wgl NV extension use handles and not integers
void* rendererHandle;
GLRendererID()
: renderID(-1)
, rendererHandle(0) {}
explicit GLRendererID(int id) : renderID(id), rendererHandle(0) {}
explicit GLRendererID(void* handle) : renderID(0), rendererHandle(handle) {}
};
class OpenGLRendererInfo
{
public:
std::size_t maxMemBytes;
std::string rendererName;
std::string vendorName;
std::string glVersionString;
std::string glslVersionString;
int maxTextureSize;
GLRendererID rendererID;
};
/**
* @brief This class encapsulates a cross-platform OpenGL context used for offscreen rendering.
**/
struct OSGLContextPrivate;
class OSGLContext
: public boost::noncopyable
{
public:
/**
* @brief Creates a new OpenGL context for offscreen rendering. The constructor may throw an exception if the context
* creation failed.
* The context must be made current with makeContextCurrent before being ready to use.
**/
explicit OSGLContext(const FramebufferConfig& pixelFormatAttrs,
const OSGLContext* shareContext,
int major = GLVersion.major,
int minor = GLVersion.minor,
const GLRendererID& rendererID = GLRendererID(),
bool coreProfile = false);
virtual ~OSGLContext();
/**
* @brief This function checks that the context has at least the OpenGL version required by Natron, otherwise
* it fails by throwing an exception with the required version.
* Note: the context must be made current before calling this function
**/
static void checkOpenGLVersion();
GLuint getPBOId() const;
GLuint getFBOId() const;
// Helper functions used by platform dependent implementations
static bool stringInExtensionString(const char* string, const char* extensions);
static const FramebufferConfig& chooseFBConfig(const FramebufferConfig& desired, const std::vector<FramebufferConfig>& alternatives, int count);
/**
* @brief Returns one of the built-in shaders, used in the Image class.
* Note: this context must be made current before calling this function
**/
GLShaderPtr getOrCreateFillShader();
GLShaderPtr getOrCreateMaskMixShader(bool maskEnabled);
GLShaderPtr getOrCreateCopyUnprocessedChannelsShader(bool doR, bool doG, bool doB, bool doA);
/**
* @brief Same as setContextCurrent() except that it should be used to bind the context to perform NON-RENDER operations!
**/
void setContextCurrentNoRender();
static void unsetCurrentContextNoRender();
/**
* @brief Returns all renderers capable of rendering OpenGL
**/
static void getGPUInfos(std::list<OpenGLRendererInfo>& renderers);
private:
/* @brief Makes the context current for the calling
* thread. A context can only be made current on
* a single thread at a time and each thread can have only a single current
* context at a time.
*
* @thread_safety This function may be called from any thread.
*/
void setContextCurrent(const AbortableRenderInfoPtr& render
#ifdef DEBUG
, double frameTime
#endif
);
/**
* @brief Releases the OpenGL context from this thread.
* @param unlockContext If true, the context will be made available for other renders as well
**/
void unsetCurrentContext(const AbortableRenderInfoPtr& abortInfo);
friend class OSGLContextAttacher;
boost::scoped_ptr<OSGLContextPrivate> _imp;
};
/**
* @brief RAII style class to safely call setContextCurrent() and unsetCurrentContext()
**/
class OSGLContextAttacher
{
OSGLContextPtr _c;
AbortableRenderInfoWPtr _a;
#ifdef DEBUG
double _frameTime;
#endif
bool _attached;
public:
OSGLContextAttacher(const OSGLContextPtr& c,
const AbortableRenderInfoPtr& render
#ifdef DEBUG
,
double frameTime
#endif
)
: _c(c)
, _a(render)
#ifdef DEBUG
, _frameTime(frameTime)
#endif
, _attached(false)
{
}
void attach()
{
if (!_attached) {
_c->setContextCurrent(_a.lock()
#ifdef DEBUG
, _frameTime
#endif
);
_attached = true;
}
}
void dettach()
{
if (_attached) {
_c->unsetCurrentContext( _a.lock() );
_attached = false;
}
}
~OSGLContextAttacher()
{
dettach();
}
};
NATRON_NAMESPACE_EXIT
#endif // OSGLCONTEXT_H