From ec354d9d8cdc47a268ec085cdce6f01498f8bc0a Mon Sep 17 00:00:00 2001 From: Bastien Date: Sun, 23 Sep 2018 19:47:40 +0200 Subject: [PATCH] Refactoring --- include/lvrc.h | 4 ++ include/lvrc_GLX.h | 14 ++++ src/CMakeLists.txt | 2 +- src/OpenGL/context.inl | 69 ++++++++++++++++++ src/OpenGL/data.h | 13 ++++ src/OpenGL/init.c | 78 ++++++++++++++++++++ src/{frame.c => OpenGL/resources.c} | 52 ++++++-------- src/{ => OpenGL}/swapChain.c | 75 ++++++++++++++------ src/backend.c | 17 +++++ src/backend.h | 21 ++++++ src/frame.h | 10 --- src/instance.c | 37 +++++----- src/instance.h | 15 ++-- src/lvrc.c | 106 +++++++++++++++++++++++++++- src/lvrc_EGL.c | 31 ++++++++ src/lvrc_GLX.c | 19 +++++ src/lvrc_internal.h | 9 --- src/rendering_EGL.c | 85 ---------------------- src/swapChain.h | 17 ----- 19 files changed, 473 insertions(+), 201 deletions(-) create mode 100644 include/lvrc_GLX.h create mode 100644 src/OpenGL/context.inl create mode 100644 src/OpenGL/data.h create mode 100644 src/OpenGL/init.c rename src/{frame.c => OpenGL/resources.c} (82%) rename src/{ => OpenGL}/swapChain.c (68%) create mode 100644 src/backend.c create mode 100644 src/backend.h delete mode 100644 src/frame.h create mode 100644 src/lvrc_EGL.c create mode 100644 src/lvrc_GLX.c delete mode 100644 src/lvrc_internal.h delete mode 100644 src/rendering_EGL.c delete mode 100644 src/swapChain.h diff --git a/include/lvrc.h b/include/lvrc.h index e128795..020c5fe 100644 --- a/include/lvrc.h +++ b/include/lvrc.h @@ -43,4 +43,8 @@ LVRC_API bool lvrcEndFrame(struct lvrcInstance * state); # include "lvrc_EGL.h" #endif // __egl_h_ +#ifdef GLX_H +# include "lvrc_GLX.h" +#endif // GLX_H + #endif // LINUX_VR_COMPOSITOR_H diff --git a/include/lvrc_GLX.h b/include/lvrc_GLX.h new file mode 100644 index 0000000..ee45c06 --- /dev/null +++ b/include/lvrc_GLX.h @@ -0,0 +1,14 @@ +#ifndef LINUX_VR_COMPOSITOR_GLX_H +#define LINUX_VR_COMPOSITOR_GLX_H + +#ifdef __cplusplus +extern "C" { +#endif + +LVRC_API bool lvrcInitRenderingGLX(struct lvrcInstance * state, Display* display, GLXDrawable drawable, GLXContext context); + +#ifdef __cplusplus +} +#endif + +#endif // LINUX_VR_COMPOSITOR_GLX_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e45d5b4..cc11947 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,7 @@ set(OpenGL_GL_PREFERENCE GLVND) find_package(OpenGL REQUIRED) find_package(OpenHMD REQUIRED) -add_library(lvrc ../include/lvrc.h ../include/lvrc_EGL.h lvrc.c lvrc_internal.h instance.c instance.h swapChain.c swapChain.h frame.c frame.h rendering_EGL.c) +add_library(lvrc ../include/lvrc.h ../include/lvrc_EGL.h ../include/lvrc_GLX.h lvrc.c lvrc_EGL.c lvrc_GLX.c backend.c backend.h instance.c instance.h OpenGL/swapChain.c OpenGL/context.inl OpenGL/init.c OpenGL/resources.c OpenGL/data.h) target_link_libraries(lvrc PUBLIC openhmd ${OPENGL_LIBRARIES} EGL) target_link_libraries(lvrc PUBLIC "${DRM_LIBRARY}" "gbm") diff --git a/src/OpenGL/context.inl b/src/OpenGL/context.inl new file mode 100644 index 0000000..b26de11 --- /dev/null +++ b/src/OpenGL/context.inl @@ -0,0 +1,69 @@ +#include +#include + +static inline bool create_context(EGLenum platform, EGLNativeDisplayType nativeDisplay, EGLNativeWindowType nativeWindow, const EGLint * config_attribs, const EGLint * context_attribs, const EGLint * surface_attribs, EGLDisplay * outDisplay, EGLContext * outContext, EGLSurface * outSurface) +{ + // + // get an EGL display connection + EGLDisplay display = eglGetPlatformDisplay(platform, nativeDisplay, (EGLAttrib*)0); + + if (EGL_NO_DISPLAY == display) + { + return false; + } + + // + // initialize the EGL display connection + EGLint major, minor; + + if (!eglInitialize(display, &major, &minor)) + { + return false; + } + + // + // bind OpenGL API + eglBindAPI(EGL_OPENGL_API); + + // + // get an appropriate EGL frame buffer configuration + EGLint num_config; + EGLConfig configs[128]; + + if (!eglChooseConfig(display, config_attribs, configs, 128, &num_config)) + { + return false; + } + + EGLConfig config = configs[0]; // FIXME + + // + // create context + EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs); + + if (EGL_NO_CONTEXT == context) + { + return false; + } + + // + // Create Surface + EGLSurface surface = eglCreateWindowSurface(display, config, nativeWindow, surface_attribs); + if (EGL_NO_SURFACE == surface) + { + return false; + } + + // + // Set current context + if (!eglMakeCurrent(display, surface, surface, context)) + { + return false; + } + + *outDisplay = display; + *outContext = context; + *outSurface = surface; + + return true; +} diff --git a/src/OpenGL/data.h b/src/OpenGL/data.h new file mode 100644 index 0000000..5184951 --- /dev/null +++ b/src/OpenGL/data.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +struct data +{ + EGLDisplay vr_display; + EGLContext vr_context; + EGLSurface vr_surface; + + GLuint textures [2]; +}; diff --git a/src/OpenGL/init.c b/src/OpenGL/init.c new file mode 100644 index 0000000..f8152bf --- /dev/null +++ b/src/OpenGL/init.c @@ -0,0 +1,78 @@ +#include "../instance.h" + +#include +#include + +#include +#include + +#include "context.inl" + +#include + +#include "data.h" + +static const EGLint attribs[] = +{ + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + //EGL_ALPHA_SIZE, 8, + //EGL_DEPTH_SIZE, 16, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_NONE +}; + +static const EGLint context_attributes[] = +{ + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE +}; + +extern bool InitFrameResources(Instance * instance); +extern void OpenGL_Compose(Instance * instance); +extern bool OpenGL_InitSwapChainEGL(Instance * instance, unsigned int width, unsigned int height, void * textures); +extern bool OpenGL_ReleaseSwapChain(Instance * instance); + +bool init_OpenGL_backend(Instance * instance) +{ + // + // Save context + EGLDisplay old_display = eglGetCurrentDisplay(); + EGLSurface old_surface_draw = eglGetCurrentSurface(EGL_DRAW); + EGLSurface old_surface_read = eglGetCurrentSurface(EGL_READ); + EGLContext old_context = eglGetCurrentContext(); + + EGLNativeDisplayType nativeDisplay = instance->device; + EGLNativeWindowType nativeWindow = instance->surface; + + EGLDisplay display = EGL_NO_DISPLAY; + EGLSurface surface = EGL_NO_SURFACE; + EGLContext context = EGL_NO_CONTEXT; + + bool bContextCreated = create_context(EGL_PLATFORM_GBM_KHR, nativeDisplay, nativeWindow, attribs, context_attributes, NULL, &display, &context, &surface); + + if (!bContextCreated) + { + fprintf(stderr, "couldn't create compositor context\n"); + return false; + } + + InitFrameResources(instance); + + struct data * data = calloc(1, sizeof(struct data)); + + data->vr_display = display; + data->vr_context = context; + data->vr_surface = surface; + + instance->backend.Compose = OpenGL_Compose; + instance->backend.InitSwapChainEGL = OpenGL_InitSwapChainEGL; + instance->backend.ReleaseSwapChain = OpenGL_ReleaseSwapChain; + instance->backend.data = data; + + // + // Restore context + eglMakeCurrent(old_display, old_surface_draw, old_surface_read, old_context); +} diff --git a/src/frame.c b/src/OpenGL/resources.c similarity index 82% rename from src/frame.c rename to src/OpenGL/resources.c index ba553f0..5105844 100644 --- a/src/frame.c +++ b/src/OpenGL/resources.c @@ -1,11 +1,15 @@ -#include "frame.h" - -#include "lvrc_internal.h" -#include "instance.h" #include #include +#include + +#include + +#include "../instance.h" + +#include "data.h" + #define USE_ES_SHADERS 1 static const GLfloat textureVertices[] = @@ -144,11 +148,6 @@ bool InitFrameResources(Instance * instance) return true; } -bool lvrcBeginFrame(struct lvrcInstance * state) -{ - return true; // nothing yet -} - static void prepare(GLuint texture) { glActiveTexture(GL_TEXTURE0); @@ -170,17 +169,15 @@ static void draw() assert(GL_NO_ERROR == glGetError()); } -bool lvrcSubmitFrameLeft(struct lvrcInstance * instance, GLuint texture) +static bool SubmitFrameLeft(Instance * instance, GLuint texture) { - Instance * internalInstance = (Instance*)instance; - - if (!internalInstance->mode) + if (!instance->mode) { return false; } - unsigned int hmd_w = internalInstance->mode->hdisplay; - unsigned int hmd_h = internalInstance->mode->vdisplay; + unsigned int hmd_w = instance->mode->hdisplay; + unsigned int hmd_h = instance->mode->vdisplay; prepare(texture); @@ -194,17 +191,15 @@ bool lvrcSubmitFrameLeft(struct lvrcInstance * instance, GLuint texture) return true; } -bool lvrcSubmitFrameRight(struct lvrcInstance * instance, GLuint texture) +static bool SubmitFrameRight(Instance * instance, GLuint texture) { - Instance * internalInstance = (Instance*)instance; - - if (!internalInstance->mode) + if (!instance->mode) { return false; } - unsigned int hmd_w = internalInstance->mode->hdisplay; - unsigned int hmd_h = internalInstance->mode->vdisplay; + unsigned int hmd_w = instance->mode->hdisplay; + unsigned int hmd_h = instance->mode->vdisplay; prepare(texture); @@ -218,18 +213,15 @@ bool lvrcSubmitFrameRight(struct lvrcInstance * instance, GLuint texture) return true; } -bool lvrcEndFrame(struct lvrcInstance * instance) +void OpenGL_Compose(Instance * instance) { - Instance * internalInstance = (Instance*)instance; + struct data * data = instance->backend.data; - eglMakeCurrent(internalInstance->vr_display, internalInstance->vr_surface, internalInstance->vr_surface, internalInstance->vr_context); + eglMakeCurrent(data->vr_display, data->vr_surface, data->vr_surface, data->vr_context); - lvrcSubmitFrameLeft(instance, internalInstance->textures[0]); - lvrcSubmitFrameRight(instance, internalInstance->textures[1]); + SubmitFrameLeft(instance, data->textures[0]); + SubmitFrameRight(instance, data->textures[1]); - eglSwapBuffers(internalInstance->vr_display, internalInstance->vr_surface); + eglSwapBuffers(data->vr_display, data->vr_surface); - SwapBuffers(internalInstance); - - return true; } diff --git a/src/swapChain.c b/src/OpenGL/swapChain.c similarity index 68% rename from src/swapChain.c rename to src/OpenGL/swapChain.c index cb7118f..2efa1bb 100644 --- a/src/swapChain.c +++ b/src/OpenGL/swapChain.c @@ -1,18 +1,18 @@ -#include "swapChain.h" - -#include "lvrc_internal.h" -#include "instance.h" +#include "../instance.h" #include #include #include #include +#include +#include + +#include + #include -#include "context.inl" - -#include "frame.h" +#include "data.h" typedef struct { @@ -26,15 +26,21 @@ typedef struct EGLuint64KHR modifiers; } dma_texture_params; -bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsigned int height, GLuint * textures) +#define USE_GBM 0 // just a test + +bool OpenGL_InitSwapChainEGL(Instance * instance, unsigned int width, unsigned int height, GLuint * textures) { Instance * internalInstance = (Instance*)instance; - if (!internalInstance->user_context || !internalInstance->vr_context) + struct data * data = internalInstance->backend.data; + + if (!internalInstance->user_context || !data->vr_context) { return false; } + // + // Save context EGLDisplay old_display = eglGetCurrentDisplay(); EGLSurface old_surface_draw = eglGetCurrentSurface(EGL_DRAW); EGLSurface old_surface_read = eglGetCurrentSurface(EGL_READ); @@ -64,11 +70,31 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig return false; } +#if USE_GBM + int fd = open("/dev/dri/renderD129", O_RDWR); + struct gbm_device * device = gbm_create_device(fd); + + EGLDisplay display = eglGetPlatformDisplay(EGL_PLATFORM_GBM_KHR, device, (EGLAttrib*)0); + + EGLint major, minor; + eglInitialize(display, &major, &minor); + + eglBindAPI(EGL_OPENGL_API); + +#else + EGLDisplay display = internalInstance->user_display; +#endif // USE_GBM + // // Create and export left image // +#if USE_GBM + struct gbm_bo * left_bo = gbm_bo_create(device, width, height, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_LINEAR | GBM_BO_USE_RENDERING); + EGLImage left_image = eglCreateImage(display, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR, left_bo, NULL); +#else EGLImage left_image = eglCreateDRMImage(internalInstance->user_display, imageAttribs); +#endif // USE_GBM if (!left_image) { @@ -78,14 +104,19 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig dma_texture_params left_texture_params; left_texture_params.width = width; left_texture_params.height = height; - eglExportDMABUFImageQuery(internalInstance->user_display, left_image, &left_texture_params.fourcc, &left_texture_params.numplanes, &left_texture_params.modifiers); - eglExportDMABUFImage(internalInstance->user_display, left_image, &left_texture_params.fd, &left_texture_params.stride, &left_texture_params.offset); + eglExportDMABUFImageQuery(display, left_image, &left_texture_params.fourcc, &left_texture_params.numplanes, &left_texture_params.modifiers); + eglExportDMABUFImage(display, left_image, &left_texture_params.fd, &left_texture_params.stride, &left_texture_params.offset); // // Create and export right image // +#if USE_GBM + struct gbm_bo * right_bo = gbm_bo_create(device, width, height, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_LINEAR | GBM_BO_USE_RENDERING); + EGLImage right_image = eglCreateImage(display, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR, right_bo, NULL); +#else EGLImage right_image = eglCreateDRMImage(internalInstance->user_display, imageAttribs); +#endif // USE_GBM if (!right_image) { @@ -95,8 +126,8 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig dma_texture_params right_texture_params; right_texture_params.width = width; right_texture_params.height = height; - eglExportDMABUFImageQuery(internalInstance->user_display, right_image, &right_texture_params.fourcc, &right_texture_params.numplanes, &right_texture_params.modifiers); - eglExportDMABUFImage(internalInstance->user_display, right_image, &right_texture_params.fd, &right_texture_params.stride, &right_texture_params.offset); + eglExportDMABUFImageQuery(display, right_image, &right_texture_params.fourcc, &right_texture_params.numplanes, &right_texture_params.modifiers); + eglExportDMABUFImage(display, right_image, &right_texture_params.fd, &right_texture_params.stride, &right_texture_params.offset); // // Import textures in user context @@ -130,11 +161,11 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig // Import textures in VR context // - eglMakeCurrent(internalInstance->vr_display, EGL_NO_SURFACE, EGL_NO_SURFACE, internalInstance->vr_context); + eglMakeCurrent(data->vr_display, EGL_NO_SURFACE, EGL_NO_SURFACE, data->vr_context); PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2D_VR = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) eglGetProcAddress ("glEGLImageTargetTexture2DOES"); - glGenTextures(2, internalInstance->textures); + glGenTextures(2, data->textures); // // LEFT @@ -151,9 +182,9 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig EGL_NONE, }; - EGLImage image_left = eglCreateImage(internalInstance->vr_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list_left); + EGLImage image_left = eglCreateImage(data->vr_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list_left); - glBindTexture(GL_TEXTURE_2D, internalInstance->textures[0]); + glBindTexture(GL_TEXTURE_2D, data->textures[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); @@ -180,9 +211,9 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig EGL_NONE, }; - EGLImage image_right = eglCreateImage(internalInstance->vr_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list_right); + EGLImage image_right = eglCreateImage(data->vr_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list_right); - glBindTexture(GL_TEXTURE_2D, internalInstance->textures[1]); + glBindTexture(GL_TEXTURE_2D, data->textures[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); @@ -201,8 +232,10 @@ bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsig return true; } -bool lvrcReleaseSwapChain(struct lvrcInstance * instance) +bool OpenGL_ReleaseSwapChain(Instance * instance) { - return false; + (void)instance; + + return false; // TODO } diff --git a/src/backend.c b/src/backend.c new file mode 100644 index 0000000..ebbbec3 --- /dev/null +++ b/src/backend.c @@ -0,0 +1,17 @@ +#include "backend.h" + +#include + +extern bool init_OpenGL_backend(Instance * instance); + +static const bool bUseOpenGL = true; + +bool init_rendering_backend(Instance * instance) +{ + if (bUseOpenGL) + { + return init_OpenGL_backend(instance); + } + + return false; +} diff --git a/src/backend.h b/src/backend.h new file mode 100644 index 0000000..13148bd --- /dev/null +++ b/src/backend.h @@ -0,0 +1,21 @@ +#ifndef LINUX_VR_COMPOSITOR_BACKEND_H +#define LINUX_VR_COMPOSITOR_BACKEND_H + +#include + +struct Instance; +typedef struct Instance Instance; + +struct backend +{ + void (*Compose)(Instance * instance); + bool (*InitSwapChainEGL)(Instance * instance, unsigned int width, unsigned int height, void * textures); + bool (*ReleaseSwapChain)(Instance * instance); + void * data; +}; + +typedef struct backend Backend; + +bool init_rendering_backend(Instance * instance); + +#endif // LINUX_VR_COMPOSITOR_BACKEND_H diff --git a/src/frame.h b/src/frame.h deleted file mode 100644 index d412d17..0000000 --- a/src/frame.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef LINUX_VR_COMPOSITOR_FRAME_H -#define LINUX_VR_COMPOSITOR_FRAME_H - -#include - -#include "instance.h" - -bool InitFrameResources(Instance * instance); - -#endif // LINUX_VR_COMPOSITOR_FRAME_H diff --git a/src/instance.c b/src/instance.c index 2fe1d32..09d62e5 100644 --- a/src/instance.c +++ b/src/instance.c @@ -1,7 +1,5 @@ #include "instance.h" -#include "lvrc_internal.h" - #include #include #include @@ -284,12 +282,8 @@ static int drm_find_psvr_xcb(Instance * instance) #endif // ENABLE_XCB -struct lvrcInstance * lvrcCreateInstance(ohmd_device * hmd) +bool InitInstance(Instance * instance) { - Instance * instance = calloc(1, sizeof(Instance)); - - instance->hmd = hmd; - #ifdef ENABLE_XCB instance->connection = xcb_connect(NULL, &instance->screen); @@ -306,34 +300,37 @@ struct lvrcInstance * lvrcCreateInstance(ohmd_device * hmd) else { fprintf(stderr, "couldn't find devicee\n"); - free(instance); - return NULL; + return false; } - instance->device = gbm_create_device(instance->fd); + struct gbm_device * device = gbm_create_device(instance->fd); - if (instance->device == NULL) + if (device == NULL) { fprintf(stderr, "couldn't create gbm device\n"); - free(instance); - return NULL; + return false; } - instance->surface = gbm_surface_create(instance->device, instance->mode->hdisplay, instance->mode->vdisplay, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); + struct gbm_surface * surface = gbm_surface_create(device, instance->mode->hdisplay, instance->mode->vdisplay, GBM_BO_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); - if (instance->surface == NULL) + if (surface == NULL) { fprintf(stderr, "couldn't create gbm surface\n"); - free(instance); - return NULL; + gbm_device_destroy(device); + return false; } - return (struct lvrcInstance *)instance; + instance->device = device; + instance->surface = surface; + + return true; } -void lvrcDestroyInstance(struct lvrcInstance * instance) +bool ReleaseInstance(Instance * instance) { - free(instance); + (void)instance; + + return false; // TODO } void SwapBuffers(Instance * instance) diff --git a/src/instance.h b/src/instance.h index 028d134..05397bc 100644 --- a/src/instance.h +++ b/src/instance.h @@ -16,7 +16,9 @@ #include -typedef struct +#include "backend.h" + +struct Instance { ohmd_device * hmd; @@ -37,18 +39,17 @@ typedef struct uint32_t fbid; struct gbm_bo * bo; - EGLDisplay vr_display; - EGLContext vr_context; - EGLSurface vr_surface; - EGLDisplay user_display; EGLContext user_context; EGLSurface user_surface; - GLuint textures [2]; + Backend backend; +}; -} Instance; +typedef struct Instance Instance; +bool InitInstance(Instance * instance); +bool ReleaseInstance(Instance * instance); void SwapBuffers(Instance * instance); #endif // LINUX_VR_COMPOSITOR_STATE_H diff --git a/src/lvrc.c b/src/lvrc.c index adf3e2c..6ce63db 100644 --- a/src/lvrc.c +++ b/src/lvrc.c @@ -1 +1,105 @@ -#include "lvrc_internal.h" +#include // GLuint +#include + +#include "instance.h" + +#include + +/** + * @brief lvrcCreateInstance + * @param hmd + * @return + */ +struct lvrcInstance * lvrcCreateInstance(ohmd_device * hmd) +{ + Instance * internalInstance = calloc(1, sizeof(Instance)); + + internalInstance->hmd = hmd; + + if (!InitInstance(internalInstance)) + { + free(internalInstance); + return NULL; + } + + return (struct lvrcInstance *)internalInstance; +} + +/** + * @brief lvrcDestroyInstance + * @param instance + */ +void lvrcDestroyInstance(struct lvrcInstance * instance) +{ + Instance * internalInstance = (Instance*)instance; + + ReleaseInstance(internalInstance); + + free(internalInstance); +} + +/** + * @brief lvrcInitSwapChain + * @param instance + * @param width + * @param height + * @param textures + * @return + */ +bool lvrcInitSwapChain(struct lvrcInstance * instance, unsigned int width, unsigned int height, GLuint * textures) +{ + Instance * internalInstance = (Instance*)instance; + + if (internalInstance->user_display && internalInstance->user_context && internalInstance->user_surface) // rendering was initialized with an EGL context ? + { + return internalInstance->backend.InitSwapChainEGL(internalInstance, width, height, textures); + } + else + { + // currently not possible + } + + return false; +} + +/** + * @brief lvrcReleaseSwapChain + * @param instance + * @return + */ +bool lvrcReleaseSwapChain(struct lvrcInstance * instance) +{ + Instance * internalInstance = (Instance*)instance; + + return internalInstance->backend.ReleaseSwapChain(internalInstance); +} + +/** + * @brief lvrcBeginFrame + * @param instance + * @return + */ +bool lvrcBeginFrame(struct lvrcInstance * instance) +{ + Instance * internalInstance = (Instance*)instance; + + (void)internalInstance; // nothing yet + + return true; +} + +/** + * @brief lvrcEndFrame + * @param instance + * @return + */ +bool lvrcEndFrame(struct lvrcInstance * instance) +{ + Instance * internalInstance = (Instance*)instance; + + internalInstance->backend.Compose(internalInstance); + + SwapBuffers(internalInstance); + + return true; +} diff --git a/src/lvrc_EGL.c b/src/lvrc_EGL.c new file mode 100644 index 0000000..26cf41e --- /dev/null +++ b/src/lvrc_EGL.c @@ -0,0 +1,31 @@ +#include +#include +#include // GLuint +#include +#include + +#include "instance.h" + +/** + * @brief lvrcInitRenderingEGL + * @param instance + * @param user_display + * @param user_context + * @param user_surface + * @return + */ +bool lvrcInitRenderingEGL(struct lvrcInstance * instance, EGLDisplay user_display, EGLContext user_context, EGLSurface user_surface) +{ + Instance * internalInstance = (Instance*)instance; + + internalInstance->user_display = user_display; + internalInstance->user_context = user_context; + internalInstance->user_surface = user_surface; + + if (!init_rendering_backend(internalInstance)) + { + return false; + } + + return true; +} diff --git a/src/lvrc_GLX.c b/src/lvrc_GLX.c new file mode 100644 index 0000000..2df9086 --- /dev/null +++ b/src/lvrc_GLX.c @@ -0,0 +1,19 @@ +#include +#include // GLuint +#include +#include + +#include "instance.h" + +/** + * @brief lvrcInitRenderingGLX + * @param instance + * @param display + * @param drawable + * @param context + * @return + */ +bool lvrcInitRenderingGLX(struct lvrcInstance * instance, Display* display, GLXDrawable drawable, GLXContext context) +{ + return false; // TODO : currently not supported +} diff --git a/src/lvrc_internal.h b/src/lvrc_internal.h deleted file mode 100644 index ccf8823..0000000 --- a/src/lvrc_internal.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef LINUX_VR_COMPOSITOR_INTERNAL_H -#define LINUX_VR_COMPOSITOR_INTERNAL_H - -#define GL_GLES_PROTOTYPES 1 -#include // FIXME - -#include - -#endif // LINUX_VR_COMPOSITOR_INTERNAL_H diff --git a/src/rendering_EGL.c b/src/rendering_EGL.c deleted file mode 100644 index 5f115cf..0000000 --- a/src/rendering_EGL.c +++ /dev/null @@ -1,85 +0,0 @@ -#include "instance.h" -#include "frame.h" - -#include -#include - -#include "lvrc_internal.h" - -#include "context.inl" - -#include - -bool lvrcInitRenderingEGL(struct lvrcInstance * instance, EGLDisplay user_display, EGLContext user_context, EGLSurface user_surface) -{ - Instance * internalInstance = (Instance*)instance; - - if (!internalInstance->device) - { - return false; - } - - static const EGLint attribs[] = - { - EGL_RED_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_BLUE_SIZE, 8, - //EGL_ALPHA_SIZE, 8, - //EGL_DEPTH_SIZE, 16, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_NONE - }; - - static const EGLint context_attributes[] = - { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - // - // Get Native Display - EGLNativeDisplayType nativeDisplay = internalInstance->device; - - if (!nativeDisplay) - { - fprintf(stderr, "invalid native display\n"); - return false; - } - - // - // Get Native Window - EGLNativeWindowType nativeWindow = internalInstance->surface; - - if (!nativeWindow) - { - fprintf(stderr, "invalid native window\n"); - return false; - } - - EGLDisplay display; - EGLSurface surface; - EGLContext context; - - bool bContextCreated = create_context(EGL_PLATFORM_GBM_KHR, nativeDisplay, nativeWindow, attribs, context_attributes, NULL, &display, &context, &surface); - - if (!bContextCreated) - { - fprintf(stderr, "couldn't create compositor context\n"); - return false; - } - - InitFrameResources(internalInstance); - - internalInstance->vr_display = display; - internalInstance->vr_context = context; - internalInstance->vr_surface = surface; - - internalInstance->user_display = user_display; - internalInstance->user_context = user_context; - internalInstance->user_surface = user_surface; - - eglMakeCurrent(user_display, user_surface, user_surface, user_context); - - return true; -} diff --git a/src/swapChain.h b/src/swapChain.h deleted file mode 100644 index a5985f5..0000000 --- a/src/swapChain.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef LINUX_VR_COMPOSITOR_SWAPCHAIN_H -#define LINUX_VR_COMPOSITOR_SWAPCHAIN_H - -#include - -#include -#include - -#include - -typedef struct -{ - -} SwapChain; - - -#endif // LINUX_VR_COMPOSITOR_SWAPCHAIN_H