From cba45b4d123b9843bddcdbc536d5502bba8e0305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Thu, 18 Oct 2018 11:22:09 +0100 Subject: [PATCH] Headless renderer. Attempting something like in #76. --- BUILD | 35 ++++++++----- engine/code/deepmind/headless_macos_glimp.c | 58 +++++++++++++++++++++ 2 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 engine/code/deepmind/headless_macos_glimp.c diff --git a/BUILD b/BUILD index 8d6331243..91837e763 100644 --- a/BUILD +++ b/BUILD @@ -802,10 +802,26 @@ cc_library( hdrs = ["public/dmlab.h"], copts = IOQ3_COMMON_COPTS, defines = IOQ3_COMMON_DEFINES, - linkopts = [ - "-lGL", - "-lrt", + linkopts = ["-framework OpenGL"], + deps = IOQ3_COMMON_DEPS, +) + +cc_library( + name = "game_lib_headless_macos", + srcs = IOQ3_COMMON_SRCS + [ + CODE_DIR + "/deepmind/dmlab_connect.c", + CODE_DIR + "/null/null_input.c", + CODE_DIR + "/null/null_snddma.c", + + ## OpenGL rendering + CODE_DIR + "/deepmind/headless_macos_glimp.c", + CODE_DIR + "/deepmind/glimp_common.h", + CODE_DIR + "/deepmind/glimp_common.c", ], + hdrs = ["public/dmlab.h"], + copts = IOQ3_COMMON_COPTS, + defines = IOQ3_COMMON_DEFINES, + linkopts = ["-framework OpenGL"], deps = IOQ3_COMMON_DEPS, ) @@ -897,27 +913,18 @@ config_setting( cc_binary( name = "libdmlab_headless_hw.so", - linkopts = ["-Wl,--version-script,$(location :dmlab.lds)"], linkshared = 1, linkstatic = 1, visibility = ["//testing:__subpackages__"], - deps = [":dmlab.lds"] + select({ - "dmlab_graphics_osmesa_or_egl": [":game_lib_headless_egl"], - "dmlab_graphics_osmesa_or_glx": [":game_lib_headless_glx"], - "//conditions:default": [":game_lib_headless_egl"], - }), + deps = [":game_lib_headless_macos"], ) cc_binary( name = "libdmlab_headless_sw.so", - linkopts = ["-Wl,--version-script,$(location :dmlab.lds)"], linkshared = 1, linkstatic = 1, visibility = ["//testing:__subpackages__"], - deps = [ - ":dmlab.lds", - ":game_lib_headless_osmesa", - ], + deps = [":game_lib_headless_osmesa"], ) cc_library( diff --git a/engine/code/deepmind/headless_macos_glimp.c b/engine/code/deepmind/headless_macos_glimp.c new file mode 100644 index 000000000..659b704eb --- /dev/null +++ b/engine/code/deepmind/headless_macos_glimp.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#include +#include + +#include "glimp_common.h" + +static CGLContextObj context; + +void GLimp_MakeCurrent(void) { +} + +void GLimp_Init(void) { + CGLPixelFormatObj pix; + GLint npix; + int attribs[] = { + kCGLPFAAccelerated, // no software rendering + kCGLPFAOpenGLProfile, + kCGLOGLPVersion_Legacy, + 0 + }; + + GLimp_CommonPreInit(); + + // NOTE: in headless mode there is no GUI, hence output to console instead of message boxes + + if (CGLChoosePixelFormat((CGLPixelFormatAttribute*)attribs, &pix, &npix) != kCGLNoError) { + // Sys_Error("GLimp_Init - choose pixel format error!\n"); + printf("GLimp_Init - choose pixel format error!\n"); + exit(1); + } + if (CGLCreateContext(pix, NULL, &context) != kCGLNoError) { + // Sys_Error("GLimp_Init - create context error!\n"); + printf("GLimp_Init - create context error!\n"); + exit(1); + } + if (CGLSetCurrentContext(context) != kCGLNoError) { + // Sys_Error("GLimp_Init - set current context error!"); + printf("GLimp_Init - set current context error!\n"); + exit(1); + } + CGLDestroyPixelFormat(pix); + + printf("Renderer: %s\nVersion: %s\n", glGetString(GL_RENDERER), glGetString(GL_VERSION)); + + GLimp_CommonPostInit(); +} + +void* GLimp_GetProcAddress(const char* func) { + return dlsym(RTLD_SELF, func); +} + +void GLimp_Shutdown(void) { + CGLSetCurrentContext(NULL); + CGLDestroyContext(context); +}