2016-09-05 22:04:26 +02:00
|
|
|
#include "CompositorGL.h"
|
2016-09-23 22:35:19 +02:00
|
|
|
#include "TextureGL.h"
|
2016-09-09 01:58:55 +02:00
|
|
|
#include "OVR_CAPI.h"
|
2016-09-05 22:04:26 +02:00
|
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
|
2016-09-06 16:48:15 +02:00
|
|
|
GLboolean CompositorGL::glewInitialized = GL_FALSE;
|
|
|
|
|
|
|
|
|
|
void CompositorGL::DebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
|
|
|
|
|
{
|
|
|
|
|
OutputDebugStringA(message);
|
|
|
|
|
OutputDebugStringA("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompositorGL* CompositorGL::Create()
|
|
|
|
|
{
|
|
|
|
|
if (!glewInitialized)
|
|
|
|
|
{
|
|
|
|
|
GLenum nGlewError = glewInit();
|
|
|
|
|
if (nGlewError != GLEW_OK)
|
|
|
|
|
return nullptr;
|
|
|
|
|
#ifdef DEBUG
|
2016-09-09 01:58:55 +02:00
|
|
|
glEnable(GL_DEBUG_OUTPUT);
|
2016-09-06 16:48:15 +02:00
|
|
|
glDebugMessageCallback((GLDEBUGPROC)DebugCallback, nullptr);
|
|
|
|
|
#endif // DEBUG
|
|
|
|
|
glGetError(); // to clear the error caused deep in GLEW
|
|
|
|
|
glewInitialized = GL_TRUE;
|
|
|
|
|
}
|
|
|
|
|
return new CompositorGL();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 22:04:26 +02:00
|
|
|
CompositorGL::CompositorGL()
|
|
|
|
|
{
|
2017-08-22 16:16:17 +02:00
|
|
|
// Get the mirror textures
|
|
|
|
|
glGenFramebuffers(ovrEye_Count, m_mirrorFB);
|
|
|
|
|
for (int i = 0; i < ovrEye_Count; i++)
|
|
|
|
|
{
|
|
|
|
|
vr::VRCompositor()->GetMirrorTextureGL((vr::EVREye)i, &m_mirror[i].first, &m_mirror[i].second);
|
|
|
|
|
|
2017-08-27 22:39:27 +02:00
|
|
|
GLint drawFboId = 0, readFboId = 0;
|
|
|
|
|
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId);
|
|
|
|
|
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId);
|
|
|
|
|
|
2017-08-22 16:16:17 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_mirrorFB[i]);
|
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_mirror[i].first, 0);
|
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
|
glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
2017-08-27 22:39:27 +02:00
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, drawFboId);
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, readFboId);
|
2017-08-22 16:16:17 +02:00
|
|
|
}
|
2016-09-05 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompositorGL::~CompositorGL()
|
|
|
|
|
{
|
2017-08-22 16:16:17 +02:00
|
|
|
for (int i = 0; i < ovrEye_Count; i++)
|
|
|
|
|
vr::VRCompositor()->ReleaseSharedGLTexture(m_mirror[i].first, m_mirror[i].second);
|
2016-09-05 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 18:02:48 +02:00
|
|
|
TextureBase* CompositorGL::CreateTexture()
|
2016-09-09 01:58:55 +02:00
|
|
|
{
|
2017-08-22 18:02:48 +02:00
|
|
|
return new TextureGL();
|
2016-09-05 22:04:26 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 16:16:17 +02:00
|
|
|
void CompositorGL::RenderMirrorTexture(ovrMirrorTexture mirrorTexture)
|
2016-09-05 22:04:26 +02:00
|
|
|
{
|
2016-09-10 22:34:06 +02:00
|
|
|
uint32_t width, height;
|
|
|
|
|
vr::VRSystem()->GetRecommendedRenderTargetSize(&width, &height);
|
|
|
|
|
|
2017-08-27 22:39:27 +02:00
|
|
|
GLint drawFboId = 0, readFboId = 0;
|
|
|
|
|
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &drawFboId);
|
|
|
|
|
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &readFboId);
|
|
|
|
|
|
2016-09-23 22:35:19 +02:00
|
|
|
TextureGL* texture = (TextureGL*)mirrorTexture->Texture.get();
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, texture->Framebuffer);
|
|
|
|
|
|
2016-09-10 22:34:06 +02:00
|
|
|
for (int i = 0; i < ovrEye_Count; i++)
|
|
|
|
|
{
|
2017-08-22 16:16:17 +02:00
|
|
|
vr::VRCompositor()->LockGLSharedTextureForAccess(m_mirror[i].second);
|
2017-07-05 16:16:08 +02:00
|
|
|
|
2016-09-10 22:34:06 +02:00
|
|
|
// Bind the buffer to copy from the compositor to the mirror texture
|
2017-08-22 16:16:17 +02:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, m_mirrorFB[i]);
|
2016-09-10 22:34:06 +02:00
|
|
|
GLint offset = (mirrorTexture->Desc.Width / 2) * i;
|
2017-08-22 16:16:17 +02:00
|
|
|
glBlitFramebuffer(0, 0, width, height, offset, 0, offset + mirrorTexture->Desc.Width / 2, mirrorTexture->Desc.Height, GL_COLOR_BUFFER_BIT, GL_LINEAR);
|
|
|
|
|
|
|
|
|
|
vr::VRCompositor()->UnlockGLSharedTextureForAccess(m_mirror[i].second);
|
2016-09-10 22:34:06 +02:00
|
|
|
}
|
2017-08-27 22:39:27 +02:00
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, drawFboId);
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, readFboId);
|
2016-09-05 22:04:26 +02:00
|
|
|
}
|
2016-09-08 23:07:21 +02:00
|
|
|
|
2016-09-23 17:04:31 +02:00
|
|
|
void CompositorGL::RenderTextureSwapChain(vr::EVREye eye, ovrTextureSwapChain swapChain, ovrTextureSwapChain sceneChain, ovrRecti viewport, vr::VRTextureBounds_t bounds, vr::HmdVector4_t quad)
|
2016-09-08 23:07:21 +02:00
|
|
|
{
|
2016-09-09 01:58:55 +02:00
|
|
|
// TODO: Support blending multiple scene layers
|
|
|
|
|
}
|