Cleanup: move all GLX to render_context_glx.

This commit is contained in:
Roz K 2022-09-19 02:02:58 +02:00
parent 94fa36def4
commit eb15d04f2c
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
5 changed files with 73 additions and 76 deletions

View File

@ -185,7 +185,9 @@ def mat4_mul_mat4(ret, a, b):
initialize = _lib.rk_initialize initialize = _lib.rk_initialize
initialize.restype = ctypes.c_void_p initialize.restype = ctypes.c_void_p
initialize.argtypes = ( initialize.argtypes = (
ctypes.c_char_p,) # name ctypes.c_char_p, # name
ctypes.c_uint, # width
ctypes.c_uint) # height
_load_shader = _lib.rk_load_shader _load_shader = _lib.rk_load_shader
_load_shader.restype = ctypes.c_void_p _load_shader.restype = ctypes.c_void_p

View File

@ -13,16 +13,19 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef _RK_ENGINE_RENDER_OPENGL_CONTEXT_H #ifndef _RK_ENGINE_RENDER_CONTEXT_H
#define _RK_ENGINE_RENDER_OPENGL_CONTEXT_H #define _RK_ENGINE_RENDER_CONTEXT_H
#include "../render.hpp" #include "../render.hpp"
#include <GL/glx.h>
extern PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC rk_DrawElementsInstancedBaseInstance;
extern PFNGLMULTIDRAWELEMENTSINDIRECTPROC rk_MultiDrawElementsIndirect;
RK_EXPORT rk_window_t rk_create_context( RK_EXPORT rk_window_t rk_create_context(
char const * name, char const * name,
int profile, rk_uint width,
int major, rk_uint height);
int minor);
RK_EXPORT char ** rk_load_shader_source( RK_EXPORT char ** rk_load_shader_source(
char const * filename, char const * filename,
@ -35,4 +38,4 @@ RK_EXPORT void rk_free_shader_source(
RK_EXPORT void rk_swap_buffers(); RK_EXPORT void rk_swap_buffers();
RK_EXPORT void rk_destroy_context(); RK_EXPORT void rk_destroy_context();
#endif // _RK_ENGINE_RENDER_OPENGL_CONTEXT_H #endif // _RK_ENGINE_RENDER_CONTEXT_H

View File

@ -18,7 +18,7 @@
#include "render_context.hpp" #include "render_context.hpp"
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <GL/glx.h> #include <GLES3/gl32.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
static Display * rk_display = nullptr; static Display * rk_display = nullptr;
@ -31,6 +31,31 @@ static bool rk_error_occured = false;
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display *, GLXFBConfig, GLXContext, Bool, int const *); typedef GLXContext (*glXCreateContextAttribsARBProc)(Display *, GLXFBConfig, GLXContext, Bool, int const *);
PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC rk_DrawElementsInstancedBaseInstance = nullptr;
PFNGLMULTIDRAWELEMENTSINDIRECTPROC rk_MultiDrawElementsIndirect = nullptr;
static int const rk_visual_attribs[] = {
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,
None
};
static int const rk_context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 2,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES_PROFILE_BIT_EXT,
None
};
static void rk_printf( static void rk_printf(
char const * messsage) { char const * messsage) {
printf("[GLX] %s\n", messsage); printf("[GLX] %s\n", messsage);
@ -66,30 +91,14 @@ static int rk_error_handler(
rk_window_t rk_create_context( rk_window_t rk_create_context(
char const * name, char const * name,
int profile, rk_uint width,
int major, rk_uint height) {
int minor) {
rk_display = XOpenDisplay(nullptr); rk_display = XOpenDisplay(nullptr);
if (!rk_display) { if (!rk_display) {
rk_printf("Failed to open X display."); rk_printf("Failed to open X display.");
return nullptr; return nullptr;
} }
static int const visual_attribs[] = {
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,
None
};
int glx_major, glx_minor; int glx_major, glx_minor;
if (!glXQueryVersion(rk_display, &glx_major, &glx_minor) || (glx_major == 1 && glx_minor < 3) || glx_major < 1) { if (!glXQueryVersion(rk_display, &glx_major, &glx_minor) || (glx_major == 1 && glx_minor < 3) || glx_major < 1) {
rk_printf("Invalid GLX version."); rk_printf("Invalid GLX version.");
@ -98,7 +107,7 @@ rk_window_t rk_create_context(
} }
int fbcount; int fbcount;
GLXFBConfig * const fbc = glXChooseFBConfig(rk_display, DefaultScreen(rk_display), visual_attribs, &fbcount); GLXFBConfig * const fbc = glXChooseFBConfig(rk_display, DefaultScreen(rk_display), rk_visual_attribs, &fbcount);
if (!fbc) { if (!fbc) {
rk_printf("Failed to retrieve a framebuffer config."); rk_printf("Failed to retrieve a framebuffer config.");
rk_destroy_context(); rk_destroy_context();
@ -141,7 +150,7 @@ rk_window_t rk_create_context(
swa.border_pixel = 0; swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask; swa.event_mask = StructureNotifyMask;
rk_window = XCreateWindow(rk_display, RootWindow(rk_display, vi->screen), rk_window = XCreateWindow(rk_display, RootWindow(rk_display, vi->screen),
0, 0, 1600, 900, 0, vi->depth, InputOutput, vi->visual, 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa); CWBorderPixel | CWColormap | CWEventMask, &swa);
if (!rk_window) { if (!rk_window) {
rk_printf("Failed to create window."); rk_printf("Failed to create window.");
@ -153,24 +162,18 @@ rk_window_t rk_create_context(
XMapWindow(rk_display, rk_window); XMapWindow(rk_display, rk_window);
char const * const glxExts = glXQueryExtensionsString(rk_display, DefaultScreen(rk_display)); char const * const glxExts = glXQueryExtensionsString(rk_display, DefaultScreen(rk_display));
glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0; glXCreateContextAttribsARBProc const glXCreateContextAttribsARB =
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) reinterpret_cast<glXCreateContextAttribsARBProc>(
glXGetProcAddressARB((const GLubyte *)("glXCreateContextAttribsARB")); glXGetProcAddressARB(reinterpret_cast<const GLubyte *>("glXCreateContextAttribsARB")));
rk_error_occured = false; rk_error_occured = false;
int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&rk_error_handler); int (*oldHandler)(Display *, XErrorEvent *) = XSetErrorHandler(&rk_error_handler);
if (!rk_extension_supported(glxExts, "GLX_ARB_create_context") || !glXCreateContextAttribsARB) { if (!rk_extension_supported(glxExts, "GLX_ARB_create_context") || !glXCreateContextAttribsARB) {
rk_printf("glXCreateContextAttribsARB() not found."); rk_printf("glXCreateContextAttribsARB() extension not found.");
rk_destroy_context(); rk_destroy_context();
return nullptr; return nullptr;
} else { } else {
const int context_attribs[] = { rk_context = glXCreateContextAttribsARB(rk_display, bestFbc, 0, True, rk_context_attribs);
GLX_CONTEXT_MAJOR_VERSION_ARB, major,
GLX_CONTEXT_MINOR_VERSION_ARB, minor,
GLX_CONTEXT_PROFILE_MASK_ARB, profile,
None
};
rk_context = glXCreateContextAttribsARB(rk_display, bestFbc, 0, True, context_attribs);
XSync(rk_display, False); XSync(rk_display, False);
if (rk_error_occured || !rk_context) { if (rk_error_occured || !rk_context) {
rk_printf("Failed to create context."); rk_printf("Failed to create context.");
@ -184,6 +187,24 @@ rk_window_t rk_create_context(
rk_printf("Warning: Rendering context is indirect."); rk_printf("Warning: Rendering context is indirect.");
} }
glXMakeCurrent(rk_display, rk_window, rk_context); glXMakeCurrent(rk_display, rk_window, rk_context);
if (rk_extension_supported(glxExts, "GL_EXT_base_instance")) {
rk_DrawElementsInstancedBaseInstance =
reinterpret_cast<PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte *>("DrawElementsInstancedBaseInstance")));
if (rk_DrawElementsInstancedBaseInstance) {
rk_printf("Using extension GL_EXT_base_instance::DrawElementsInstancedBaseInstance.");
if (rk_extension_supported(glxExts, "GL_EXT_multi_draw_indirect")) {
rk_MultiDrawElementsIndirect =
reinterpret_cast<PFNGLMULTIDRAWELEMENTSINDIRECTPROC>(
glXGetProcAddressARB(reinterpret_cast<const GLubyte *>("MultiDrawElementsIndirectEXT")));
if (rk_MultiDrawElementsIndirect) {
rk_printf("Using extension GL_EXT_multi_draw_indirect::MultiDrawElementsIndirectEXT.");
}
}
}
}
return reinterpret_cast<rk_window_t>(rk_window); return reinterpret_cast<rk_window_t>(rk_window);
} }

View File

@ -15,18 +15,11 @@
#include "render_opengles.hpp" #include "render_opengles.hpp"
#include <cstdio> #include <cstdio>
#include <cstring>
//TODO: move the GLX stuff to render_context_glx.cpp
#include <GL/glx.h>
static rk_shader const * rk_current_shader = nullptr; static rk_shader const * rk_current_shader = nullptr;
static rk_vertices const * rk_current_vertices = nullptr; static rk_vertices const * rk_current_vertices = nullptr;
static bool rk_frame = false; static bool rk_frame = false;
static PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC rk_DrawElementsInstancedBaseInstance = nullptr;
static PFNGLMULTIDRAWELEMENTSINDIRECTPROC rk_MultiDrawElementsIndirect = nullptr;
static void rk_printf(char const * messsage) { static void rk_printf(char const * messsage) {
printf("[RK_ENGINE] %s\n", messsage); printf("[RK_ENGINE] %s\n", messsage);
} }
@ -47,26 +40,11 @@ static void rk_debug_message_callback(
printf("[RK_ENGINE][GL] (id=%d) %s\n", id, message); printf("[RK_ENGINE][GL] (id=%d) %s\n", id, message);
} }
static __GLXextFuncPtr rk_extension(
char const * const name,
char const * const func) {
__GLXextFuncPtr ext_ptr = nullptr;
GLint num_exts = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
for (int ext_index = 0; ext_index < num_exts; ++ext_index) {
char const * const ext_name = reinterpret_cast<char const *>(glGetStringi(GL_EXTENSIONS, ext_index));
if (strcmp(name, ext_name) == 0) {
ext_ptr = glXGetProcAddressARB(reinterpret_cast<GLubyte const *>(func));
printf("[RK_ENGINE] Using extension %s::%s\n", name, func);
break;
}
}
return ext_ptr;
}
rk_window_t rk_initialize( rk_window_t rk_initialize(
char const * name) { char const * name,
rk_window_t const window = rk_create_context(name, GLX_CONTEXT_ES_PROFILE_BIT_EXT, 3, 2); rk_uint width,
rk_uint height) {
rk_window_t const window = rk_create_context(name, width, height);
if (window) { if (window) {
GLubyte const * const vendor = glGetString(GL_VENDOR); GLubyte const * const vendor = glGetString(GL_VENDOR);
GLubyte const * const renderer = glGetString(GL_RENDERER); GLubyte const * const renderer = glGetString(GL_RENDERER);
@ -75,15 +53,6 @@ rk_window_t rk_initialize(
GLubyte const * const language = glGetString(GL_SHADING_LANGUAGE_VERSION); GLubyte const * const language = glGetString(GL_SHADING_LANGUAGE_VERSION);
printf("[RK_ENGINE] version: %s, language: %s\n", version, language); printf("[RK_ENGINE] version: %s, language: %s\n", version, language);
rk_DrawElementsInstancedBaseInstance =
reinterpret_cast<PFNGLDRAWELEMENTSINSTANCEDBASEINSTANCEPROC>(
rk_extension("GL_EXT_base_instance", "DrawElementsInstancedBaseInstance"));
if (rk_DrawElementsInstancedBaseInstance) {
rk_MultiDrawElementsIndirect =
reinterpret_cast<PFNGLMULTIDRAWELEMENTSINDIRECTPROC>(
rk_extension("GL_EXT_multi_draw_indirect", "MultiDrawElementsIndirectEXT"));
}
GLint context_flags = 0; GLint context_flags = 0;
glGetIntegerv(GL_CONTEXT_FLAGS, &context_flags); glGetIntegerv(GL_CONTEXT_FLAGS, &context_flags);
if (context_flags & GL_CONTEXT_FLAG_DEBUG_BIT) { if (context_flags & GL_CONTEXT_FLAG_DEBUG_BIT) {

View File

@ -99,7 +99,9 @@ union rk_mesh {
}; };
RK_EXPORT rk_window_t rk_initialize( RK_EXPORT rk_window_t rk_initialize(
char const * name); char const * name,
rk_uint width,
rk_uint height);
RK_EXPORT rk_shader_t rk_load_shader( RK_EXPORT rk_shader_t rk_load_shader(
char const * name); char const * name);