OGLplus  (0.59.0) a C++ wrapper for rendering APIs

context.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_INTEROP_GLX_CONTEXT_HPP
10 #define EAGINE_INTEROP_GLX_CONTEXT_HPP
11 
12 #include "../x11/display.hpp"
13 #include "../x11/window.hpp"
14 #include "drawable.hpp"
15 #include "fb_config.hpp"
16 
17 #include <GL/glx.h>
18 #include <X11/Xlib.h>
19 #include <stdexcept>
20 
21 namespace eagine::glx {
22 
23 class context
24  : public x11::display_object<::GLXContext, void(::Display*, ::GLXContext)> {
25 private:
26  static auto make_context(
27  const x11::display& dpy,
28  const fb_config& fbc,
29  int version_major,
30  int version_minor,
31  bool debugging,
32  bool compatibility,
33  ::GLXContext share_context = ::GLXContext(nullptr)) -> ::GLXContext {
34  using glXCreateContextAttribsARBProc = GLXContext (*)(
35  ::Display*, ::GLXFBConfig, ::GLXContext, Bool, const int*);
36 
37  glXCreateContextAttribsARBProc glXCreateContextAttribsARB = nullptr;
38  glXCreateContextAttribsARB =
39  reinterpret_cast<glXCreateContextAttribsARBProc>(glXGetProcAddressARB(
40  reinterpret_cast<const GLubyte*>("glXCreateContextAttribsARB")));
41 
42  const int CONTEXT_MAJOR_VERSION_ARB = 0x2091;
43  const int CONTEXT_MINOR_VERSION_ARB = 0x2092;
44  const int CONTEXT_FLAGS_ARB = 0x2094;
45  const int CONTEXT_PROFILE_MASK_ARB = 0x9126;
46  const int CONTEXT_DEBUG_BIT_ARB = 0x0001;
47  const int CONTEXT_CORE_PROFILE_BIT_ARB = 0x00000001;
48  const int CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB = 0x00000002;
49 
50  ::GLXContext res{};
51 
52  if(compatibility) {
53  int context_attribs[] = {
54  CONTEXT_MAJOR_VERSION_ARB,
55  version_major,
56  CONTEXT_MINOR_VERSION_ARB,
57  version_minor,
58  CONTEXT_FLAGS_ARB,
59  (debugging ? CONTEXT_DEBUG_BIT_ARB : 0),
60  CONTEXT_PROFILE_MASK_ARB,
61  (compatibility ? CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB
62  : CONTEXT_CORE_PROFILE_BIT_ARB),
63  None};
64  res = glXCreateContextAttribsARB(
65  dpy,
66  fbc.handle(),
67  share_context,
68  True,
69  static_cast<const int*>(context_attribs));
70  } else {
71  int context_attribs[] = {
72  CONTEXT_MAJOR_VERSION_ARB,
73  version_major,
74  CONTEXT_MINOR_VERSION_ARB,
75  version_minor,
76  CONTEXT_FLAGS_ARB,
77  (debugging ? CONTEXT_DEBUG_BIT_ARB : 0),
78  None};
79  res = glXCreateContextAttribsARB(
80  dpy,
81  fbc.handle(),
82  share_context,
83  True,
84  static_cast<const int*>(context_attribs));
85  }
86  ::XSync(dpy, False);
87  return res;
88  }
89 
90 public:
91  context(
92  const x11::display& dpy,
93  const fb_config& fbc,
94  int version_major,
95  int version_minor,
96  bool debugging = true,
97  bool compatibility = false)
98  : x11::display_object<::GLXContext, void(::Display*, ::GLXContext)>(
99  dpy,
100  make_context(
101  dpy,
102  fbc,
103  version_major,
104  version_minor,
105  debugging,
106  compatibility),
107  ::glXDestroyContext,
108  "Error creating glX context") {}
109 
110  context(
111  const x11::display& dpy,
112  const fb_config& fbc,
113  const context& share_context,
114  int version_major,
115  int version_minor,
116  bool debugging = true,
117  bool compatibility = false)
118  : x11::display_object<::GLXContext, void(::Display*, ::GLXContext)>(
119  dpy,
120  make_context(
121  dpy,
122  fbc,
123  version_major,
124  version_minor,
125  debugging,
126  compatibility,
127  share_context.handle()),
128  ::glXDestroyContext,
129  "Error creating sharing glX context") {}
130 
131  void make_current(const drawable& surface) const {
132  ::glXMakeCurrent(this->display_ref(), surface, this->handle());
133  }
134 
135  static void release(const x11::display& dpy) {
136  ::glXMakeCurrent(dpy, 0, nullptr);
137  }
138 
139  void swap_buffers(const drawable& surface) const {
140  ::glXSwapBuffers(this->display_ref(), surface);
141  }
142 };
143 
144 } // namespace eagine::glx
145 
146 #endif
GLX wrapper code is placed in this namespace.
Definition: x11.hpp:14

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).