215 lines
5.4 KiB
C
215 lines
5.4 KiB
C
#include "vr_context.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <lvrc.h>
|
|
|
|
#include <EGL/eglext.h>
|
|
|
|
static EGLDisplay display;
|
|
static EGLSurface surface;
|
|
static EGLContext context;
|
|
|
|
static void create_vr_context(struct lvrcInstance * compositor)
|
|
{
|
|
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 = lvrcSwapChainGetNativeDisplay(compositor);
|
|
|
|
if (!nativeDisplay)
|
|
{
|
|
fprintf(stderr, "invalid native display\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
// get an EGL display connection
|
|
display = eglGetDisplay(nativeDisplay);
|
|
|
|
if (EGL_NO_DISPLAY == display)
|
|
{
|
|
fprintf(stderr, "eglGetDisplay() failed\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
// initialize the EGL display connection
|
|
EGLint major, minor;
|
|
|
|
if (!eglInitialize(display, &major, &minor))
|
|
{
|
|
fprintf(stderr, "eglInitialize() failed\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
// bind OpenGL API
|
|
eglBindAPI(EGL_OPENGL_API);
|
|
|
|
//
|
|
// get an appropriate EGL frame buffer configuration
|
|
EGLint num_config;
|
|
EGLConfig configs[128];
|
|
|
|
if (!eglChooseConfig(display, attribs, configs, 128, &num_config))
|
|
{
|
|
fprintf(stderr, "eglChooseConfig() failed\n");
|
|
exit(-1);
|
|
}
|
|
|
|
EGLConfig config = configs[0]; // FIXME
|
|
|
|
//
|
|
// create context
|
|
context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes);
|
|
|
|
if (EGL_NO_CONTEXT == context)
|
|
{
|
|
fprintf(stderr, "failed to create context\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
// Get Native Window
|
|
EGLNativeWindowType nativeWindow = lvrcSwapChainGetNativeWindow(compositor);
|
|
|
|
if (!nativeWindow)
|
|
{
|
|
fprintf(stderr, "invalid native window\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
// Create Surface
|
|
surface = eglCreateWindowSurface(display, config, nativeWindow, NULL);
|
|
if (EGL_NO_SURFACE == surface)
|
|
{
|
|
fprintf(stderr, "eglCreateWindowSurface() failed\n");
|
|
exit(-1);
|
|
}
|
|
|
|
//
|
|
// Set current context
|
|
if (!eglMakeCurrent(display, surface, surface, context))
|
|
{
|
|
fprintf(stderr, "failed to make context current\n");
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
GLuint left_color_tex = 0;
|
|
GLuint right_color_tex = 0;
|
|
|
|
void init_vr_context(struct lvrcInstance * compositor, dma_texture_params * left_texture_params, dma_texture_params * right_texture_params)
|
|
{
|
|
bool bSwapChainCreated = lvrcInitSwapChain(compositor);
|
|
|
|
if (!bSwapChainCreated)
|
|
{
|
|
fprintf(stderr, "swap chain not created\n");
|
|
exit(-1);
|
|
}
|
|
|
|
create_vr_context(compositor);
|
|
|
|
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) eglGetProcAddress ("glEGLImageTargetTexture2DOES");
|
|
|
|
//
|
|
// LEFT
|
|
EGLAttrib attrib_list_left[] =
|
|
{
|
|
EGL_DMA_BUF_PLANE0_FD_EXT, left_texture_params->fd,
|
|
EGL_DMA_BUF_PLANE0_PITCH_EXT, left_texture_params->stride,
|
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT, left_texture_params->offset,
|
|
EGL_WIDTH, left_texture_params->width,
|
|
EGL_HEIGHT, left_texture_params->height,
|
|
EGL_LINUX_DRM_FOURCC_EXT, left_texture_params->fourcc,
|
|
//EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, left_texture_params->modifiers & 0xFFFFFFFF,
|
|
//EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, left_texture_params->modifiers >> 32,
|
|
EGL_NONE,
|
|
};
|
|
|
|
EGLImage image_left = eglCreateImage(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list_left);
|
|
|
|
glGenTextures(1, &left_color_tex);
|
|
glBindTexture(GL_TEXTURE_2D, left_color_tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image_left);
|
|
assert(glGetError() == GL_NO_ERROR);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
//
|
|
// RIGHT
|
|
EGLAttrib attrib_list_right[] =
|
|
{
|
|
EGL_DMA_BUF_PLANE0_FD_EXT, right_texture_params->fd,
|
|
EGL_DMA_BUF_PLANE0_PITCH_EXT, right_texture_params->stride,
|
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT, right_texture_params->offset,
|
|
EGL_WIDTH, right_texture_params->width,
|
|
EGL_HEIGHT, right_texture_params->height,
|
|
EGL_LINUX_DRM_FOURCC_EXT, right_texture_params->fourcc,
|
|
//EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, right_texture_params->modifiers & 0xFFFFFFFF,
|
|
//EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, right_texture_params->modifiers >> 32,
|
|
EGL_NONE,
|
|
};
|
|
|
|
EGLImage image_right = eglCreateImage(display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list_right);
|
|
|
|
glGenTextures(1, &right_color_tex);
|
|
glBindTexture(GL_TEXTURE_2D, right_color_tex);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image_right);
|
|
assert(glGetError() == GL_NO_ERROR);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
}
|
|
|
|
void vr_context_submit_eyes(struct lvrcInstance * compositor)
|
|
{
|
|
eglMakeCurrent(display, surface, surface, context);
|
|
|
|
glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
// Draw left eye
|
|
lvrcSubmitFrameLeft(compositor, left_color_tex);
|
|
|
|
// Draw right eye
|
|
lvrcSubmitFrameRight(compositor, right_color_tex);
|
|
|
|
eglSwapBuffers(display, surface);
|
|
}
|