Skip to content

Commit

Permalink
Load GLX dynamically with GLAD
Browse files Browse the repository at this point in the history
This is accomplished through the addition of a GLAD GLX loader in the
`thirdparty` directory.

This is another step towards a nice Wayland/X11 interoperation.
  • Loading branch information
Riteo committed Nov 15, 2022
1 parent 91fcc39 commit dc2919d
Show file tree
Hide file tree
Showing 7 changed files with 1,028 additions and 28 deletions.
1 change: 0 additions & 1 deletion platform/linuxbsd/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ def configure(env: "Environment"):

if env["opengl3"]:
env.Append(CPPDEFINES=["GLES3_ENABLED"])
env.ParseConfig("pkg-config gl --cflags --libs")

env.Append(LIBS=["pthread"])

Expand Down
2 changes: 1 addition & 1 deletion platform/linuxbsd/x11/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if env["vulkan"]:
source_files.append("vulkan_context_x11.cpp")

if env["opengl3"]:
source_files.append(["gl_manager_x11.cpp", "detect_prime_x11.cpp"])
source_files.append(["gl_manager_x11.cpp", "detect_prime_x11.cpp", "#thirdparty/glad/glx.c"])

objects = []

Expand Down
23 changes: 17 additions & 6 deletions platform/linuxbsd/x11/detect_prime_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@

#include <stdlib.h>

#include <GL/gl.h>
#include <GL/glx.h>
#include "thirdparty/glad/glad/gl.h"
#include "thirdparty/glad/glad/glx.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>

Expand Down Expand Up @@ -77,8 +78,6 @@ void create_context() {
Window x11_window;
GLXContext glx_context;

GLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (GLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte *)"glXCreateContextAttribsARB");

static int visual_attribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
Expand All @@ -101,7 +100,7 @@ void create_context() {

GLXFBConfig *fbc = glXChooseFBConfig(x11_display, DefaultScreen(x11_display), visual_attribs, &fbcount);
if (!fbc) {
exit(1);
quick_exit(1);
}

vi = glXGetVisualFromFBConfig(x11_display, fbc[0]);
Expand All @@ -122,7 +121,7 @@ void create_context() {
x11_window = XCreateWindow(x11_display, RootWindow(x11_display, vi->screen), 0, 0, 10, 10, 0, vi->depth, InputOutput, vi->visual, valuemask, &swa);

if (!x11_window) {
exit(1);
quick_exit(1);
}

glXMakeCurrent(x11_display, x11_window, glx_context);
Expand Down Expand Up @@ -189,8 +188,20 @@ int detect_prime() {
if (i) {
setenv("DRI_PRIME", "1", 1);
}

if (gladLoaderLoadGLX(NULL, 0) == 0) {
print_verbose("Unable to load GLX, GPU detection skipped.");
quick_exit(1);
}

create_context();

PFNGLGETSTRINGPROC glGetString = (PFNGLGETSTRINGPROC)glXGetProcAddressARB((GLubyte *)"glGetString");
if (!glGetString) {
print_verbose("Unable to get glGetString, GPU detection skipped.");
quick_exit(1);
}

const char *vendor = (const char *)glGetString(GL_VENDOR);
const char *renderer = (const char *)glGetString(GL_RENDERER);

Expand Down
28 changes: 8 additions & 20 deletions platform/linuxbsd/x11/gl_manager_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
#include <stdlib.h>
#include <unistd.h>

#define GLX_GLXEXT_PROTOTYPES
#include <GL/glx.h>
#include <GL/glxext.h>
#include "thirdparty/glad/glad/glx.h"

#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
Expand Down Expand Up @@ -324,11 +322,14 @@ void GLManager_X11::swap_buffers() {
}

Error GLManager_X11::initialize() {
if (!gladLoaderLoadGLX(nullptr, 0)) {
return ERR_CANT_CREATE;
}

return OK;
}

void GLManager_X11::set_use_vsync(bool p_use) {
static bool setup = false;
static PFNGLXSWAPINTERVALEXTPROC glXSwapIntervalEXT = nullptr;
static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalMESA = nullptr;
static PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI = nullptr;
Expand All @@ -345,25 +346,12 @@ void GLManager_X11::set_use_vsync(bool p_use) {
}
const GLDisplay &disp = get_current_display();

if (!setup) {
setup = true;
String extensions = glXQueryExtensionsString(disp.x11_display, DefaultScreen(disp.x11_display));
if (extensions.find("GLX_EXT_swap_control") != -1) {
glXSwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalEXT");
}
if (extensions.find("GLX_MESA_swap_control") != -1) {
glXSwapIntervalMESA = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalMESA");
}
if (extensions.find("GLX_SGI_swap_control") != -1) {
glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte *)"glXSwapIntervalSGI");
}
}
int val = p_use ? 1 : 0;
if (glXSwapIntervalMESA) {
if (GLAD_GLX_MESA_swap_control) {
glXSwapIntervalMESA(val);
} else if (glXSwapIntervalSGI) {
} else if (GLAD_GLX_SGI_swap_control) {
glXSwapIntervalSGI(val);
} else if (glXSwapIntervalEXT) {
} else if (GLAD_GLX_EXT_swap_control) {
GLXDrawable drawable = glXGetCurrentDrawable();
glXSwapIntervalEXT(disp.x11_display, drawable, val);
} else {
Expand Down
2 changes: 2 additions & 0 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ Files generated from [upstream web instance](https://gen.glad.sh/):
- `KHR/khrplatform.h`
- `gl.c`
- `glad/gl.h`
- `glx.c`
- `glad/glx.h`


## glslang
Expand Down
Loading

0 comments on commit dc2919d

Please sign in to comment.