20 #include "../../OVR_CAPI_GL.h"
22 namespace OVR {
namespace CAPI {
namespace GL {
44 #define SI_NOREFL(shader) { shader, sizeof(shader), NULL, 0 }
45 #define SI_REFL__(shader) { shader, sizeof(shader), shader ## _refl, sizeof( shader ## _refl )/sizeof(*(shader ## _refl)) }
111 #if !defined(OVR_OS_MAC)
119 unsigned distortionCaps)
136 #if defined(OVR_OS_WIN32)
137 RParams.Window = (config->
OGL.Window) ? config->
OGL.Window : GetActiveWindow();
138 #elif defined(OVR_OS_LINUX)
188 unsigned char* latencyTesterDrawColor,
unsigned char* latencyTester2DrawColor)
213 if(latencyTesterDrawColor)
217 else if(latencyTester2DrawColor)
225 int swapInterval = (useVsync) ? 1 : 0;
226 #if defined(OVR_OS_WIN32)
227 if (wglGetSwapIntervalEXT() != swapInterval)
228 wglSwapIntervalEXT(swapInterval);
230 HDC dc = GetDC(
RParams.Window);
231 BOOL success = SwapBuffers(dc);
235 #elif defined(OVR_OS_MAC)
236 CGLContextObj context = CGLGetCurrentContext();
237 GLint currentSwapInterval = 0;
238 CGLGetParameter(context, kCGLCPSwapInterval, ¤tSwapInterval);
239 if (currentSwapInterval != swapInterval)
240 CGLSetParameter(context, kCGLCPSwapInterval, &swapInterval);
242 CGLFlushDrawable(context);
243 #elif defined(OVR_OS_LINUX)
244 static const char* extensions = glXQueryExtensionsString(
RParams.Disp, 0);
245 static bool supportsVSync = (extensions !=
NULL &&
strstr(extensions,
"GLX_EXT_swap_control"));
248 GLuint currentSwapInterval = 0;
249 glXQueryDrawable(
RParams.Disp,
RParams.Win, GLX_SWAP_INTERVAL_EXT, ¤tSwapInterval);
250 if (currentSwapInterval != swapInterval)
268 if (initialTime >= absTime)
274 double newTime = initialTime;
277 while (newTime < absTime)
279 for (
int j = 0; j < 50; j++)
286 return newTime - initialTime;
292 const char* glVersionString = (
const char*)glGetString(GL_VERSION);
293 OVR_DEBUG_LOG((
"GL_VERSION STRING: %s", (
const char*)glVersionString));
295 bool foundVersion =
false;
297 for (
int i = 10; i < 30; ++i)
302 if (
strstr(glVersionString, prefix) == glVersionString)
325 const char* extensions = (
const char*)glGetString(GL_EXTENSIONS);
342 glGetIntegerv(GL_VIEWPORT, Viewport);
343 glGetFloatv(GL_COLOR_CLEAR_VALUE, ClearColor);
344 glGetIntegerv(GL_DEPTH_TEST, &DepthTest);
345 glGetIntegerv(GL_CULL_FACE, &CullFace);
346 glGetIntegerv(GL_CURRENT_PROGRAM, &Program);
347 glGetIntegerv(GL_ACTIVE_TEXTURE, &ActiveTexture);
348 glGetIntegerv(GL_TEXTURE_BINDING_2D, &TextureBinding);
349 glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &VertexArray);
350 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &FrameBufferBinding);
351 glGetIntegerv(GL_BLEND, &Blend);
352 glGetIntegerv(GL_COLOR_WRITEMASK, ColorWritemask);
353 glGetIntegerv(GL_DITHER, &Dither);
354 glGetIntegerv(GL_RASTERIZER_DISCARD, &RasterizerDiscard);
355 if (GlMajorVersion >= 3 && GlMajorVersion >= 2)
356 glGetIntegerv(GL_SAMPLE_MASK, &SampleMask);
357 glGetIntegerv(GL_SCISSOR_TEST, &ScissorTest);
369 glViewport(Viewport[0], Viewport[1], Viewport[2], Viewport[3]);
370 glClearColor(ClearColor[0], ClearColor[1], ClearColor[2], ClearColor[3]);
372 ApplyBool(GL_DEPTH_TEST, DepthTest);
373 ApplyBool(GL_CULL_FACE, CullFace);
377 glBindTexture(GL_TEXTURE_2D, TextureBinding);
382 ApplyBool(GL_BLEND, Blend);
384 glColorMask((GLboolean)ColorWritemask[0], (GLboolean)ColorWritemask[1], (GLboolean)ColorWritemask[2], (GLboolean)ColorWritemask[3]);
385 ApplyBool(GL_DITHER, Dither);
386 ApplyBool(GL_RASTERIZER_DISCARD, RasterizerDiscard);
387 if (GlMajorVersion >= 3 && GlMajorVersion >= 2)
388 ApplyBool(GL_SAMPLE_MASK, SampleMask);
389 ApplyBool(GL_SCISSOR_TEST, ScissorTest);
395 for (
int eyeNum = 0; eyeNum < 2; eyeNum++ )
417 for (
unsigned vertNum = 0; vertNum < meshData.
VertexCount; vertNum++ )
419 pCurVBVert->
Pos.
x = pCurOvrVert->
Pos.
x;
420 pCurVBVert->
Pos.
y = pCurOvrVert->
Pos.
y;
426 pCurVBVert->
Col.
G = pCurVBVert->
Col.
R;
427 pCurVBVert->
Col.
B = pCurVBVert->
Col.
R;
452 glDisable(GL_CULL_FACE);
453 glDisable(GL_DEPTH_TEST);
456 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
457 glDisable(GL_DITHER);
458 glDisable(GL_RASTERIZER_DISCARD);
460 glDisable(GL_SAMPLE_MASK);
461 glDisable(GL_SCISSOR_TEST);
469 glClear(GL_COLOR_BUFFER_BIT);
471 for (
int eyeNum = 0; eyeNum < 2; eyeNum++)
474 distortionShaderFill.
SetTexture(0, eyeNum == 0 ? leftEyeTexture : rightEyeTexture);
502 const int numQuadVerts = 4;
517 const float left = -1.0f;
518 const float top = -1.0f;
519 const float right = 1.0f;
520 const float bottom = 1.0f;
532 const int numQuadVerts = 4;
545 SimpleQuadShader->SetUniform4f(
"Color", (
float)latencyTesterDrawColor[0] / 255.99f,
546 (
float)latencyTesterDrawColor[0] / 255.99f,
547 (
float)latencyTesterDrawColor[0] / 255.99f,
550 for(
int eyeNum = 0; eyeNum < 2; eyeNum++)
552 SimpleQuadShader->SetUniform2f(
"PositionOffset", eyeNum == 0 ? -0.4f : 0.4f, 0.0f);
559 const int numQuadVerts = 4;
570 SimpleQuadShader->SetUniform4f(
"Color", (
float)latencyTesterPixelColor[0] / 255.99f,
571 (
float)latencyTesterPixelColor[0] / 255.99f,
572 (
float)latencyTesterPixelColor[0] / 255.99f,
584 int offset,
int count,
599 prim = GL_TRIANGLE_STRIP;
616 if (isDistortionMesh)
617 glDrawElements(prim, count, GL_UNSIGNED_SHORT,
NULL);
619 glDrawArrays(prim, 0, count);
629 int attributeCount = (isDistortionMesh) ? 5 : 1;
630 int* locs =
new int[attributeCount];
634 if (isDistortionMesh)
657 for (
int i = 0; i < attributeCount; ++i)
660 if (isDistortionMesh)
661 glDrawElements(prim, count, GL_UNSIGNED_SHORT,
NULL);
663 glDrawArrays(prim, 0, count);
668 for (
int i = 0; i < attributeCount; ++i)
679 glViewport(vp.
x, vp.
y, vp.
w, vp.
h);
687 const char* shaderPrefix =
695 char* vsSource =
new char[vsSize];
701 (
void*)vsSource, vsSize,
712 char* psSource =
new char[psSize];
718 (
void*)psSource, psSize,
727 char* vsSource =
new char[vsSize];
733 (
void*)vsSource, vsSize,
742 char* psSource =
new char[psSize];
748 (
void*)psSource, psSize,
762 for(
int eyeNum = 0; eyeNum < 2; eyeNum++)
PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray
const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_vs_refl[]
OVR_EXPORT ovrBool ovrHmd_CreateDistortionMesh(ovrHmd hmd, ovrEyeType eyeType, ovrFovPort fov, unsigned int distortionCaps, ovrDistortionMesh *meshData)
static const char glsl2Prefix[]
void ApplyBool(GLenum Name, GLint Value)
virtual void SubmitEye(int eyeId, ovrTexture *eyeTexture)
bool NeedDistortionTimeMeasurement() const
static const char Distortion_vs[]
ShaderImpl< Shader_Fragment, GL_FRAGMENT_SHADER > FragmentShader
static ShaderInfo DistortionVertexShaderLookup[DistortionVertexShaderCount]
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer
Timing GetFrameTiming(unsigned frameIndex)
static const char DistortionTimewarp_vs[]
const ShaderBase::Uniform * ReflectionData
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays
__END_NAMESPACE_STD char char __BEGIN_NAMESPACE_STD size_t strlen(const char *__s) __THROW __attribute_pure__ __nonnull((1))
OVR_EXPORT void ovrHmd_GetEyeTimewarpMatrices(ovrHmd hmd, ovrEyeType eye, ovrPosef renderPose, ovrMatrix4f twmOut[2])
OVR_EXPORT void ovrHmd_DestroyDistortionMesh(ovrDistortionMesh *meshData)
const HMDRenderState & RState
OVR_EXPORT double ovr_GetTimeInSeconds()
GLuint DistortionMeshVAOs[2]
Ptr< Buffer > DistortionMeshVBs[2]
#define SI_REFL__(shader)
FrameTimeManager & TimeManager
static const char glsl3Prefix[]
Ptr< ShaderSet > SimpleQuadShader
ovrDistortionVertex * pVertexData
Matrix4< float > Matrix4f
ShaderSet * GetShaders() const
OVR_EXPORT void ovrHmd_GetRenderScaleAndOffset(ovrFovPort fov, ovrSizei textureSize, ovrRecti renderViewport, ovrVector2f uvScaleOffsetOut[2])
char *OVR_CDECL OVR_strcpy(char *dest, UPInt destsize, const char *src)
static const char SimpleQuad_vs[]
PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer
void renderDistortion(Texture *leftEyeTexture, Texture *rightEyeTexture)
static ShaderInfo DistortionPixelShaderLookup[DistortionPixelShaderCount]
static CAPI::DistortionRenderer * Create(ovrHmd hmd, FrameTimeManager &timeManager, const HMDRenderState &renderState)
const OVR::CAPI::GL::ShaderBase::Uniform SimpleQuad_fs_refl[]
static const char SimpleQuad_fs[]
Ptr< Buffer > LatencyTesterQuadVB
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays
ovrPosef EyeRenderPoses[2]
PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation
PFNGLUSEPROGRAMPROC glUseProgram
ovrVector2f UVScaleOffset[2]
virtual void Set(PrimitiveType prim=Prim_Unknown) const
void AddDistortionTimeMeasurement(double distortionTimeSeconds)
#define SI_NOREFL(shader)
Ptr< GraphicsState > GfxState
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray
void renderLatencyQuad(unsigned char *latencyTesterDrawColor)
static const char Distortion_fs[]
char *OVR_CDECL OVR_strcat(char *dest, UPInt destsize, const char *src)
PFNGLBINDVERTEXARRAYPROC glBindVertexArray
void renderLatencyPixel(unsigned char *latencyTesterPixelColor)
void DistortionShaderBitIndexCheck()
LatencyVertex(const Vector3f &p)
static const char DistortionTimewarpChroma_vs[]
PFNGLBINDBUFFERPROC glBindBuffer
Ptr< ShaderSet > DistortionShader
static const char DistortionChroma_vs[]
virtual void EndFrame(bool swapBuffers, unsigned char *latencyTesterDrawColor, unsigned char *latencyTester2DrawColor)
DistortionRenderer(ovrHmd hmd, FrameTimeManager &timeManager, const HMDRenderState &renderState)
__BEGIN_NAMESPACE_STD void void __END_NAMESPACE_STD void __BEGIN_NAMESPACE_STD void * memset(void *__s, int __c, size_t __n) __THROW __nonnull((1))
static const char DistortionChroma_fs[]
ovrRenderAPIConfigHeader Header
Ptr< Texture > pEyeTextures[2]
virtual void SetTexture(int i, class Texture *tex)
UPInt OVR_CDECL OVR_sprintf(char *dest, UPInt destsize, const char *format,...)
unsigned short * pIndexData
Ptr< Buffer > DistortionMeshIBs[2]
ShaderImpl< Shader_Vertex, GL_VERTEX_SHADER > VertexShader
#define OVR_DEBUG_LOG(args)
PFNGLACTIVETEXTUREPROC glActiveTexture
void initBuffersAndShaders()
void setViewport(const Recti &vp)
virtual bool Initialize(const ovrRenderAPIConfig *apiConfig, unsigned distortionCaps)
Vector3< float > Vector3f
__END_NAMESPACE_STD __BEGIN_NAMESPACE_STD size_t size_t char char * strstr(const char *__haystack, const char *__needle) __THROW __attribute_pure__ __nonnull((1
void renderPrimitives(const ShaderFill *fill, Buffer *vertices, Buffer *indices, int offset, int count, PrimitiveType rprim, GLuint *vao, bool isDistortionMesh)
#define OVR_COMPILER_ASSERT(x)
struct OVR::CAPI::GL::DistortionRenderer::FOR_EACH_EYE eachEye[2]
double FlushGpuAndWaitTillTime(double absTime)
ovrEyeRenderDesc EyeRenderDesc[2]