/
LinuxGLContext.cpp
217 lines (169 loc) · 6.09 KB
/
LinuxGLContext.cpp
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
/*
* LinuxGLContext.cpp
*
* This file is part of the "LLGL" project (Copyright (c) 2015 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include "LinuxGLContext.h"
#include "../../Ext/GLExtensions.h"
#include "../../Ext/GLExtensionLoader.h"
#include "../../../CheckedCast.h"
#include "../../../../Core/Helper.h"
#include <LLGL/Log.h>
#include <algorithm>
namespace LLGL
{
#ifndef GLX_CONTEXT_MAJOR_VERSION_ARB
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#endif
#ifndef GLX_CONTEXT_MINOR_VERSION_ARB
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#endif
typedef GLXContext (*GXLCREATECONTEXTATTRIBARBPROC)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
/*
* GLContext class
*/
std::unique_ptr<GLContext> GLContext::Create(RenderContextDescriptor& desc, Surface& surface, GLContext* sharedContext)
{
LinuxGLContext* sharedContextGLX = (sharedContext != nullptr ? LLGL_CAST(LinuxGLContext*, sharedContext) : nullptr);
return MakeUnique<LinuxGLContext>(desc, surface, sharedContextGLX);
}
/*
* LinuxGLContext class
*/
LinuxGLContext::LinuxGLContext(RenderContextDescriptor& desc, Surface& surface, LinuxGLContext* sharedContext) :
GLContext { sharedContext }
{
NativeHandle nativeHandle;
surface.GetNativeHandle(&nativeHandle);
if (sharedContext)
{
auto sharedContextGLX = LLGL_CAST(LinuxGLContext*, sharedContext);
CreateContext(desc, nativeHandle, sharedContextGLX);
}
else
CreateContext(desc, nativeHandle, nullptr);
}
LinuxGLContext::~LinuxGLContext()
{
DeleteContext();
}
bool LinuxGLContext::SetSwapInterval(int interval)
{
/* Load GL extension "glXSwapIntervalSGI" to set v-sync interval */
if (glXSwapIntervalSGI || LoadSwapIntervalProcs())
return (glXSwapIntervalSGI(interval) == 0);
else
return false;
}
bool LinuxGLContext::SwapBuffers()
{
glXSwapBuffers(display_, wnd_);
return true;
}
void LinuxGLContext::Resize(const Size& resolution)
{
//TODO...
}
/*
* ======= Private: =======
*/
bool LinuxGLContext::Activate(bool activate)
{
if (activate)
return glXMakeCurrent(display_, wnd_, glc_);
else
return glXMakeCurrent(nullptr, 0, 0);
}
void LinuxGLContext::CreateContext(const RenderContextDescriptor& contextDesc, const NativeHandle& nativeHandle, LinuxGLContext* sharedContext)
{
GLXContext glcShared = (sharedContext != nullptr ? sharedContext->glc_ : nullptr);
/* Get X11 display, window, and visual information */
display_ = nativeHandle.display;
wnd_ = nativeHandle.window;
visual_ = nativeHandle.visual;
if (!display_ || !wnd_ || !visual_)
throw std::invalid_argument("failed to create OpenGL context on X11 client, due to missing arguments");
/* Create OpenGL context with X11 lib */
const auto& profileDesc = contextDesc.profileOpenGL;
if (profileDesc.contextProfile == OpenGLContextProfile::CoreProfile)
{
/* Create core profile */
glc_ = CreateContextCoreProfile(glcShared, profileDesc.majorVersion, profileDesc.minorVersion);
}
if (!glc_)
{
/* Create compatibility profile */
glc_ = CreateContextCompatibilityProfile(glcShared);
}
/* Make new OpenGL context current */
if (glXMakeCurrent(display_, wnd_, glc_) != True)
Log::StdErr() << "failed to make OpenGL render context current (glXMakeCurrent)" << std::endl;
}
void LinuxGLContext::DeleteContext()
{
glXDestroyContext(display_, glc_);
}
GLXContext LinuxGLContext::CreateContextCoreProfile(GLXContext glcShared, int major, int minor)
{
/* Check if highest version possible shall be used */
if (major < 0 || minor < 0)
{
/* Set to fixed value since 'glGetIntegerv' can not be used until a valid GL context has been created */
major = 3;
minor = 2;
}
/* Load GL extension to create core profile */
GXLCREATECONTEXTATTRIBARBPROC glXCreateContextAttribsARB = nullptr;
glXCreateContextAttribsARB = (GXLCREATECONTEXTATTRIBARBPROC)glXGetProcAddressARB(reinterpret_cast<const GLubyte*>("glXCreateContextAttribsARB"));
if (glXCreateContextAttribsARB != nullptr)
{
/* Create core profile */
const int fbAttribs[] =
{
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, True,
//GLX_SAMPLE_BUFFERS, 1,
//GLX_SAMPLES, 1,//static_cast<int>(desc_.multiSampling.samples),
None
};
int attribs[100];
memcpy(attribs, fbAttribs, sizeof(fbAttribs));
int screen = DefaultScreen(display_);
int fbCount = 0;
GLXFBConfig* fbcList = glXChooseFBConfig(display_, screen, attribs, &fbCount);
if (fbcList != nullptr)
{
int contextAttribs[] =
{
GLX_CONTEXT_MAJOR_VERSION_ARB, major,
GLX_CONTEXT_MINOR_VERSION_ARB, minor,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
//GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
None
};
auto glc = glXCreateContextAttribsARB(display_, fbcList[0], nullptr, True, contextAttribs);
XFree(fbcList);
return glc;
}
}
/* Context creation failed */
Log::StdErr() << "failed to create OpenGL core profile" << std::endl;
return nullptr;
}
GLXContext LinuxGLContext::CreateContextCompatibilityProfile(GLXContext glcShared)
{
/* Create compatibility profile */
return glXCreateContext(display_, visual_, glcShared, GL_TRUE);
}
} // /namespace LLGL
// ================================================================================