Ocean
Loading...
Searching...
No Matches
platform/gl/Context.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_PLATFORM_GL_CONTEXT_H
9#define META_OCEAN_PLATFORM_GL_CONTEXT_H
10
12
13#include "ocean/base/Thread.h"
14
15namespace Ocean
16{
17
18namespace Platform
19{
20
21namespace GL
22{
23
24/**
25 * This class encapsulates an OpenGL context.
26 * @ingroup platformgl
27 */
28class OCEAN_PLATFORM_GL_EXPORT Context
29{
30 public:
31
32 /**
33 * Definition of a set holding strings.
34 */
35 typedef std::set<std::string> StringSet;
36
37 /**
38 * Definition of the glGetStringi function pointer type.
39 */
40 typedef const GLubyte*(*glGetStringiFunction)(GLenum, GLuint);
41
42 /**
43 * Definition of the glGenFramebuffers function pointer type.
44 */
45 typedef void (*glGenFramebuffersFunction)(GLsizei, GLuint*);
46
47 /**
48 * Definition of the glDeleteFramebuffers function pointer type.
49 */
50 typedef void (*glDeleteFramebuffersFunction)(GLsizei, const GLuint*);
51
52 /**
53 * Definition of the glBindFramebuffer function pointer type.
54 */
55 typedef void (*glBindFramebufferFunction)(GLenum, GLuint);
56
57 /**
58 * Definition of the glGenRenderbuffers function pointer type.
59 */
60 typedef void (*glGenRenderbuffersFunction)(GLsizei, GLuint*);
61
62 /**
63 * Definition of the glDeleteRenderbuffers function pointer type.
64 */
65 typedef void (*glDeleteRenderbuffersFunction)(GLsizei, const GLuint*);
66
67 /**
68 * Definition of the glBindRenderbuffer function pointer type.
69 */
70 typedef void (*glBindRenderbufferFunction)(GLenum, GLuint);
71
72 /**
73 * Definition of the glRenderbufferStorage function pointer type.
74 */
75 typedef void (*glRenderbufferStorageFunction)(GLenum, GLenum, GLsizei, GLsizei);
76
77 /**
78 * Definition of the glRenderbufferStorageMultisample function pointer type.
79 */
80 typedef void (*glRenderbufferStorageMultisampleFunction)(GLenum, GLsizei, GLenum, GLsizei, GLsizei);
81
82 /**
83 * Definition of the glFramebufferRenderbuffer function pointer type.
84 */
85 typedef void (*glFramebufferRenderbufferFunction)(GLenum, GLenum, GLenum, GLuint);
86
87 /**
88 * Definition of the glBlitFramebuffer function pointer type.
89 */
90 typedef void(*glBlitFramebufferFunction)(GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLbitfield, GLenum);
91
92 /**
93 * Definition of the glCheckFramebufferStatus function pointer type.
94 */
95 typedef GLenum (*glCheckFramebufferStatusFunction)(GLenum);
96
97 /**
98 * Definition of the glGenBuffers function pointer type.
99 */
100 typedef void (*glGenBuffersFunction)(GLsizei, GLuint*);
101
102 /**
103 * Definition of the glDeleteBuffers function pointer type.
104 */
105 typedef void (*glDeleteBuffersFunction)(GLsizei, const GLuint*);
106
107 /**
108 * Definition of the glBindBuffer function pointer type.
109 */
110 typedef void (*glBindBufferFunction)(GLenum, GLuint);
111
112 /**
113 * Definition of the glBufferData function pointer type.
114 */
115 typedef void (*glBufferDataFunction)(GLenum, GLsizeiptr, const GLvoid*, GLenum);
116
117 /**
118 * Definition of the glCreateProgram function pointer type.
119 */
120 typedef GLuint (*glCreateProgramFunction)();
121
122 /**
123 * Definition of the glDeleteProgram function pointer type.
124 */
125 typedef void (*glDeleteProgramFunction)(GLuint);
126
127 /**
128 * Definition of the glIsProgram function pointer type.
129 */
130 typedef GLboolean (*glIsProgramFunction)(GLuint);
131
132 /**
133 * Definition of the glAttachShader function pointer type.
134 */
135 typedef void (*glAttachShaderFunction)(GLuint, GLuint);
136
137 /**
138 * Definition of the glDetachShader function pointer type.
139 */
140 typedef void (*glDetachShaderFunction)(GLuint, GLuint);
141
142 /**
143 * Definition of the glLinkProgram function pointer type.
144 */
145 typedef void (*glLinkProgramFunction)(GLuint);
146
147 /**
148 * Definition of the glUseProgram function pointer type.
149 */
150 typedef void (*glUseProgramFunction)(GLuint);
151
152 /**
153 * Definition of the glCreateShader function pointer type.
154 */
155 typedef GLuint (*glCreateShaderFunction)(GLenum);
156
157 /**
158 * Definition of the glDeleteShader function pointer type.
159 */
160 typedef void (*glDeleteShaderFunction)(GLuint);
161
162 /**
163 * Definition of the glCompileShader function pointer type.
164 */
165 typedef void (*glCompileShaderFunction)(GLuint);
166
167 /**
168 * Definition of the glShaderSource function pointer type.
169 */
170 typedef void (*glShaderSourceFunction)(GLuint, GLsizei, const GLchar**, const GLint*);
171
172 /**
173 * Definition of the glGetProgramiv function pointer type.
174 */
175 typedef void (*glGetProgramivFunction)(GLuint, GLenum, GLint*);
176
177 /**
178 * Definition of the glGetShaderiv function pointer type.
179 */
180 typedef void (*glGetShaderivFunction)(GLuint, GLenum, GLint*);
181
182 /**
183 * Definition of the glGetProgramInfoLog function pointer type.
184 */
185 typedef void (*glGetProgramInfoLogFunction)(GLuint, GLsizei, GLsizei*, GLchar*);
186
187 /**
188 * Definition of the glGetShaderInfoLog function pointer type.
189 */
190 typedef void (*glGetShaderInfoLogFunction)(GLuint, GLsizei, GLsizei*, GLchar*);
191
192 /**
193 * Definition of the glGetAttribLocation function pointer type.
194 */
195 typedef GLint (*glGetAttribLocationFunction)(GLuint, const GLchar*);
196
197 /**
198 * Definition of the glGetUniformLocation function pointer type.
199 */
200 typedef GLint (*glGetUniformLocationFunction)(GLuint, const GLchar*);
201
202 /**
203 * Definition of the glUniform1i function pointer type.
204 */
205 typedef void (*glUniform1iFunction)(GLint, GLint);
206
207 /**
208 * Definition of the glUniform1fv function pointer type.
209 */
210 typedef void (*glUniform1fvFunction)(GLint, GLsizei, const GLfloat*);
211
212 /**
213 * Definition of the glUniform2fv function pointer type.
214 */
215 typedef void (*glUniform2fvFunction)(GLint, GLsizei, const GLfloat*);
216
217 /**
218 * Definition of the glUniform3fv function pointer type.
219 */
220 typedef void (*glUniform3fvFunction)(GLint, GLsizei, const GLfloat*);
221
222 /**
223 * Definition of the glUniform4fv function pointer type.
224 */
225 typedef void (*glUniform4fvFunction)(GLint, GLsizei, const GLfloat*);
226
227 /**
228 * Definition of the glUniformMatrix3fv function pointer type.
229 */
230 typedef void(*glUniformMatrix3fvFunction)(GLint, GLsizei, GLboolean, const GLfloat*);
231
232 /**
233 * Definition of the glUniformMatrix4fv function pointer type.
234 */
235 typedef void (*glUniformMatrix4fvFunction)(GLint, GLsizei, GLboolean, const GLfloat*);
236
237 /**
238 * Definition of the glEnableVertexAttribArray function pointer type.
239 */
240 typedef void (*glEnableVertexAttribArrayFunction)(GLuint);
241
242 /**
243 * Definition of the glDisableVertexAttribArray function pointer type.
244 */
245 typedef void (*glDisableVertexAttribArrayFunction)(GLuint);
246
247 /**
248 * Definition of the glVertexAttribPointer function pointer type.
249 */
250 typedef void (*glVertexAttribPointerFunction)(GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid*);
251
252 /**
253 * Definition of the glActiveTexture function pointer type.
254 */
255 typedef void (*glActiveTextureFunction)(GLenum);
256
257 /**
258 * Definition of the glGenerateMipmap function pointer type.
259 */
260 typedef void (*glGenerateMipmapFunction)(GLenum);
261
262 /**
263 * Definition of the glBlendFuncSeparate function pointer type.
264 */
265 typedef void (*glBlendFuncSeparateFunction)(GLenum, GLenum, GLenum, GLenum);
266
267 /**
268 * Definition of the glTexImage2DMultisample function pointer type.
269 */
270 typedef void (*glTexImage2DMultisampleFunction)(GLenum, GLsizei, GLenum, GLsizei, GLsizei, GLboolean);
271
272 /**
273 * Definition of the glFramebufferTexture2D function pointer type.
274 */
275 typedef void (*glFramebufferTexture2DFunction)(GLenum, GLenum, GLenum, GLuint, GLint);
276
277 /**
278 * Definition of the glBindFragDataLocation function pointer type.
279 */
280 typedef void (*glBindFragDataLocationFunction)(GLuint, GLuint, const char*);
281
282 /**
283 * Definition of the glGenVertexArrays function pointer type.
284 */
285 typedef void (*glGenVertexArraysFunction)(GLsizei, GLuint*);
286
287 /**
288 * Definition of the glDeleteVertexArrays function pointer type.
289 */
290 typedef void (*glDeleteVertexArraysFunction)(GLsizei, const GLuint*);
291
292 /**
293 * Definition of the glBindVertexArray function pointer type.
294 */
295 typedef void(*glBindVertexArrayFunction)(GLuint);
296
297#ifdef _WINDOWS
298
299 typedef HGLRC (*wglCreateContextAttribsARBFunction)(HDC, HGLRC, const int*);
300
301#endif
302
303 public:
304
305 /**
306 * Releases the context.
307 */
308 virtual ~Context();
309
310 /**
311 * Makes the OpenGL rendering context of this framebuffer the calling thread's current rendering context or makes the calling thread's current rendering context not longer current.
312 * @param state True, to make the context current; False, to make the current context not current any longer
313 */
314 virtual void makeCurrent(const bool state = true) = 0;
315
316 /**
317 * Swaps the foreground and background buffer if this context has two buffers.
318 * @return True, if this context has two buffers
319 */
320 virtual bool swapBuffers();
321
322 /**
323 * Returns the major OpenGL version of this context.
324 * The determination of the version may fail if the version is below 3.0.<br>
325 * @return The context's major OpenGL version, 0 if the major version could not be determined
326 */
327 inline unsigned int majorVersion() const;
328
329 /**
330 * Returns the minor OpenGL version of this context.
331 * The determination of the version may fail if the version is below 3.0.<br>
332 * @return The context's minor OpenGL version, 0 if the major version could not be determined
333 */
334 inline unsigned int minorVersion() const;
335
336 /**
337 * Returns whether this context provides the compatibility profile or the core profile.
338 * In general, the compatibility profile allows to use also deprecated functions.<br>
339 * However, an existing core profile does not guarantee a compatibility profile with same features (OpenGL version).
340 * @return True, if this context provides the compatibility profile
341 */
342 inline bool compatibilityProfile() const;
343
344 /**
345 * Returns the set of supported extensions.
346 * @return The supported extensions
347 */
348 inline const StringSet& supportedExtensions() const;
349
350 /**
351 * Returns whether a specific extension is supported.
352 * @param extension The extension to check
353 * @return True, if so
354 */
355 inline bool isExtensionSupported(const std::string& extension) const;
356
357 /**
358 * Returns the number of multi samples that this context supports.
359 * @return The context's number of multi samples
360 */
361 inline unsigned int multisamples() const;
362
363 /**
364 * Releases the context explicitly.
365 */
366 virtual void release();
367
368 /**
369 * Returns whether this context is valid.
370 * @return True, if so
371 */
372 virtual bool isValid() const;
373
374 protected:
375
376 /**
377 * Creates a new context.
378 */
379 Context() = default;
380
381 /**
382 * Creates the OpenGL context for this object.
383 * @param initializeOpenGL33 True, to initialize the capabilities of OpenGL 3.3 and higher
384 * @param multisamples The number of multisamples requested for the context
385 * @return True, if succeeded
386 */
387 virtual bool createOpenGLContext(const bool initializeOpenGL33 = true, const unsigned int multisamples = 1u);
388
389 /**
390 * Releases the OpenGL context of this framebuffer.
391 * @return True, if succeeded
392 */
393 virtual bool releaseOpenGLContext();
394
395#ifdef _WINDOWS
396 /**
397 * Creates a the OpennGL context handle on Windows platforms.
398 * @param dc The device context for which the OpenGL context will be created, must be valid
399 * @param initializeOpenGL33 True, to initialize the capabilities of OpenGL 3.3 and higher
400 * @param multisamples The number of multisamples requested for the context and also the resulting number of multisamples if the context could be created successfully, with range [1, infinity)
401 * @return Resulting OpenGL context handle, if the context could be created
402 */
403 static HGLRC windowsCreateOpenGLContextHandle(HDC dc, const bool initializeOpenGL33, unsigned int& multisamples);
404#endif
405
406 public:
407
408 /// The glGetStringi function.
409 glGetStringiFunction glGetStringi = nullptr;
410
411 /// The glGenFramebuffers function.
412 glGenFramebuffersFunction glGenFramebuffers = nullptr;
413
414 /// The glDeleteFramebuffer function.
415 glDeleteFramebuffersFunction glDeleteFramebuffers = nullptr;
416
417 /// The glBindFramebuffer function.
418 glBindFramebufferFunction glBindFramebuffer = nullptr;
419
420 /// The glGenRenderbuffers functions.
421 glGenRenderbuffersFunction glGenRenderbuffers = nullptr;
422
423 /// The glDeleteRenderbuffers function.
424 glDeleteRenderbuffersFunction glDeleteRenderbuffers = nullptr;
425
426 /// The glBindRenderbuffer function.
427 glBindRenderbufferFunction glBindRenderbuffer = nullptr;
428
429 /// The glRenderbufferStorage function.
430 glRenderbufferStorageFunction glRenderbufferStorage = nullptr;
431
432 /// The glRenderbufferStorageMultisample function.
433 glRenderbufferStorageMultisampleFunction glRenderbufferStorageMultisample = nullptr;
434
435 /// The glFramebufferRenderbuffer function.
436 glFramebufferRenderbufferFunction glFramebufferRenderbuffer = nullptr;
437
438 /// The glBlitFramebuffer function;
439 glBlitFramebufferFunction glBlitFramebuffer = nullptr;
440
441 /// The glCheckFramebufferStatus function.
442 glCheckFramebufferStatusFunction glCheckFramebufferStatus = nullptr;
443
444 /// The glGenBuffers function.
445 glGenBuffersFunction glGenBuffers = nullptr;
446
447 /// The glDeleteBuffers function.
448 glDeleteBuffersFunction glDeleteBuffers = nullptr;
449
450 /// The glBindBuffer function.
451 glBindBufferFunction glBindBuffer = nullptr;
452
453 /// The glBufferData function.
454 glBufferDataFunction glBufferData = nullptr;
455
456 /// The glCreateProgram function.
457 glCreateProgramFunction glCreateProgram = nullptr;
458
459 /// The glDeleteProgram function.
460 glDeleteProgramFunction glDeleteProgram = nullptr;
461
462 /// The glIsProgram function.
463 glIsProgramFunction glIsProgram = nullptr;
464
465 /// The glAttachShader function.
466 glAttachShaderFunction glAttachShader = nullptr;
467
468 /// The glDetachShader function.
469 glDetachShaderFunction glDetachShader = nullptr;
470
471 /// The glLinkProgram function.
472 glLinkProgramFunction glLinkProgram = nullptr;
473
474 /// The glUseProgram function.
475 glUseProgramFunction glUseProgram = nullptr;
476
477 /// The glCreateShader function.
478 glCreateShaderFunction glCreateShader = nullptr;
479
480 /// The glDeleteShader function.
481 glDeleteShaderFunction glDeleteShader = nullptr;
482
483 /// The glCompileShader function.
484 glCompileShaderFunction glCompileShader = nullptr;
485
486 /// The glShaderSource function.
487 glShaderSourceFunction glShaderSource = nullptr;
488
489 /// The glGetProgramiv function.
490 glGetProgramivFunction glGetProgramiv = nullptr;
491
492 /// The glGetShaderiv function.
493 glGetShaderivFunction glGetShaderiv = nullptr;
494
495 /// The glGetProgramInfoLog function.
496 glGetProgramInfoLogFunction glGetProgramInfoLog = nullptr;
497
498 /// The glGetShaderInfoLog function.
499 glGetShaderInfoLogFunction glGetShaderInfoLog = nullptr;
500
501 /// The glGetAttribLocation function.
502 glGetAttribLocationFunction glGetAttribLocation = nullptr;
503
504 /// The glGetUniformLocation function.
505 glGetUniformLocationFunction glGetUniformLocation = nullptr;
506
507 /// The glUniform1i function.
508 glUniform1iFunction glUniform1i = nullptr;
509
510 /// The glUniform1fv function.
511 glUniform1fvFunction glUniform1fv = nullptr;
512
513 /// The glUniform2fv function.
514 glUniform2fvFunction glUniform2fv = nullptr;
515
516 /// The glUniform3fv function.
517 glUniform3fvFunction glUniform3fv = nullptr;
518
519 /// The glUniform4fv function.
520 glUniform4fvFunction glUniform4fv = nullptr;
521
522 /// The glUniformMatrix3fv function.
523 glUniformMatrix3fvFunction glUniformMatrix3fv = nullptr;
524
525 /// The glUniformMatrix4fv function.
526 glUniformMatrix4fvFunction glUniformMatrix4fv = nullptr;
527
528 /// The glEnableVertexAttribArray function.
529 glEnableVertexAttribArrayFunction glEnableVertexAttribArray = nullptr;
530
531 /// The glEnableVertexAttribArray function.
532 glDisableVertexAttribArrayFunction glDisableVertexAttribArray = nullptr;
533
534 /// The glVertexAttribPointer function.
535 glVertexAttribPointerFunction glVertexAttribPointer = nullptr;
536
537 /// The glActiveTexture function.
538 glActiveTextureFunction glActiveTexture = nullptr;
539
540 /// The glGenerateMipmap function.
541 glGenerateMipmapFunction glGenerateMipmap = nullptr;
542
543 /// The glBlendFuncSeparate function.
544 glBlendFuncSeparateFunction glBlendFuncSeparate = nullptr;
545
546 /// The glTexImage2DMultisample function.
547 glTexImage2DMultisampleFunction glTexImage2DMultisample = nullptr;
548
549 /// The glFramebufferTexture2D function.
550 glFramebufferTexture2DFunction glFramebufferTexture2D = nullptr;
551
552 /// The glGenVertexArrays function.
553 glGenVertexArraysFunction glGenVertexArrays = nullptr;
554
555 /// The glDeleteVertexArrays function.
556 glDeleteVertexArraysFunction glDeleteVertexArrays = nullptr;
557
558 /// glBindVertexArray function.
559 glBindVertexArrayFunction glBindVertexArray = nullptr;
560
561 protected:
562
563 /// The OpenGL context handle.
564 void* handle_ = nullptr;
565
566 /// The major version of this OpenGL context.
567 unsigned int majorVersion_ = 0u;
568
569 /// The minor version of this OpenGL context.
570 unsigned int minorVersion_ = 0u;
571
572 /// True, if this context provides the compatibility profile.
573 bool compatibilityProfile_ = false;
574
575 /// The set of supported extensions.
577
578 /// The number of multi samples of this context.
579 unsigned int multisamples_ = 0u;
580
581#ifdef OCEAN_DEBUG
582 // The id of the thread which is responsible for the context.
584#endif
585};
586
587inline unsigned int Context::majorVersion() const
588{
589 return majorVersion_;
590}
591
592inline unsigned int Context::minorVersion() const
593{
594 return minorVersion_;
595}
596
598{
600}
601
603{
604 return extensions_;
605}
606
607inline bool Context::isExtensionSupported(const std::string& extension) const
608{
609 return extensions_.find(extension) != extensions_.end();
610}
611
612inline unsigned int Context::multisamples() const
613{
614 return multisamples_;
615}
616
617}
618
619}
620
621}
622
623#endif // META_OCEAN_PLATFORM_GL_CONTEXT_H
This class encapsulates an OpenGL context.
Definition platform/gl/Context.h:29
virtual bool createOpenGLContext(const bool initializeOpenGL33=true, const unsigned int multisamples=1u)
Creates the OpenGL context for this object.
virtual ~Context()
Releases the context.
bool isExtensionSupported(const std::string &extension) const
Returns whether a specific extension is supported.
Definition platform/gl/Context.h:607
std::set< std::string > StringSet
Definition of a set holding strings.
Definition platform/gl/Context.h:35
unsigned int minorVersion_
The minor version of this OpenGL context.
Definition platform/gl/Context.h:570
virtual bool isValid() const
Returns whether this context is valid.
unsigned int multisamples() const
Returns the number of multi samples that this context supports.
Definition platform/gl/Context.h:612
unsigned int minorVersion() const
Returns the minor OpenGL version of this context.
Definition platform/gl/Context.h:592
const StringSet & supportedExtensions() const
Returns the set of supported extensions.
Definition platform/gl/Context.h:602
unsigned int majorVersion() const
Returns the major OpenGL version of this context.
Definition platform/gl/Context.h:587
virtual void release()
Releases the context explicitly.
virtual void makeCurrent(const bool state=true)=0
Makes the OpenGL rendering context of this framebuffer the calling thread's current rendering context...
bool compatibilityProfile_
True, if this context provides the compatibility profile.
Definition platform/gl/Context.h:573
bool compatibilityProfile() const
Returns whether this context provides the compatibility profile or the core profile.
Definition platform/gl/Context.h:597
StringSet extensions_
The set of supported extensions.
Definition platform/gl/Context.h:576
Thread::ThreadId threadId_
Definition platform/gl/Context.h:583
unsigned int majorVersion_
The major version of this OpenGL context.
Definition platform/gl/Context.h:567
static HGLRC windowsCreateOpenGLContextHandle(HDC dc, const bool initializeOpenGL33, unsigned int &multisamples)
Creates a the OpennGL context handle on Windows platforms.
virtual bool releaseOpenGLContext()
Releases the OpenGL context of this framebuffer.
unsigned int multisamples_
The number of multi samples of this context.
Definition platform/gl/Context.h:579
virtual bool swapBuffers()
Swaps the foreground and background buffer if this context has two buffers.
Context()=default
Creates a new context.
This class implements a platform independent wrapper for thread ids.
Definition Thread.h:122
The namespace covering the entire Ocean framework.
Definition Accessor.h:15