first draft at glx headless rendering

[skip ci]
This commit is contained in:
Konstantin Käfer
2014-07-07 14:49:23 -07:00
parent 9b61e39ef3
commit f51bd7fcf1
2 changed files with 78 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ void notify_map_change(MapChange change) {
}
HeadlessView::HeadlessView() {
#if LLMR_USE_CGL
// TODO: test if OpenGL 4.1 with GL_ARB_ES2_compatibility is supported
// If it is, use kCGLOGLPVersion_3_2_Core and enable that extension.
CGLPixelFormatAttribute attributes[] = {
@@ -36,6 +37,26 @@ HeadlessView::HeadlessView() {
fprintf(stderr, "Error creating GL context object\n");
return;
}
#endif
#ifdef USE_GLX
x_display = XOpenDisplay(0);
static int pixelFormat[] = {
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
None
};
x_info = glXChooseVisual(x_display, DefaultScreen(x_display), pixelFormat);
gl_context = glXCreateContext(display, x_info, 0, GL_TRUE);
#endif
make_active();
}
@@ -44,6 +65,7 @@ HeadlessView::HeadlessView() {
void HeadlessView::resize(int width, int height) {
clear_buffers();
#if LLMR_USE_CGL
// Create depth/stencil buffer
glGenRenderbuffersEXT(1, &fbo_depth_stencil);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth_stencil);
@@ -74,9 +96,17 @@ void HeadlessView::resize(int width, int height) {
}
return;
}
#endif
#if LLMR_USE_GLX
x_pixmap = XCreatePixmap(display, DefaultRootWindow(display), width, height, 32);
glx_pixmap = glXCreateGLXPixmap(display, x_info, x_pixmap);
#endif
}
void HeadlessView::clear_buffers() {
#if LLMR_USE_CGL
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
if (fbo) {
@@ -93,24 +123,52 @@ void HeadlessView::clear_buffers() {
glDeleteRenderbuffersEXT(1, &fbo_depth_stencil);
fbo_depth_stencil = 0;
}
#endif
#if LLMR_USE_GLX
if (glx_pixmap) {
glXDestroyGLXPixmap(x_display, glx_pixmap);
glx_pixmap = nullptr;
}
if (x_pixmap) {
XFreePixmap(x_display, x_pixmap);
x_pixmap = nullptr;
}
#endif
}
HeadlessView::~HeadlessView() {
clear_buffers();
#if LLMR_USE_CGL
CGLDestroyContext(gl_context);
#endif
}
void HeadlessView::make_active() {
#if LLMR_USE_CGL
CGLError error = CGLSetCurrentContext(gl_context);
if (error) {
fprintf(stderr, "Switching OpenGL context failed\n");
}
#endif
#if LLMR_USE_GLX
if (!glXMakeCurrent(display, glx_pixmap, gl_context)) {
fprintf(stderr, "Switching OpenGL context failed\n");
}
#endif
}
void HeadlessView::swap() {}
unsigned int HeadlessView::root_fbo() {
#if LLMR_USE_CGL
return fbo;
#endif
return 0;
}
}

View File

@@ -1,6 +1,15 @@
#ifndef LLMR_COMMON_HEADLESS_CGL
#define LLMR_COMMON_HEADLESS_CGL
#ifdef __APPLE__
#define LLMR_USE_CGL 1
#define LLMR_CONTEXT_OBJ CGLContextObj
#else
#include <GL/glx.h>
#define LLMR_USE_GLX 1
#define LLMR_CONTEXT_OBJ GLXContext
#endif
#include <llmr/map/view.hpp>
#include <llmr/platform/gl.hpp>
@@ -22,10 +31,20 @@ private:
private:
CGLContextObj gl_context;
LLMR_CONTEXT_OBJ gl_context;
#ifdef LLMR_USE_CGL
GLuint fbo = 0;
GLuint fbo_depth_stencil = 0;
GLuint fbo_color = 0;
#endif
#ifdef LLMR_USE_GLX
XVisualInfo *x_info = nullptr;
Display *x_display = nullptr;
Pixmap x_pixmap;
GLXPixmap glx_pixmap;
#endif
};
}