Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CAPI_GL_DistortionRenderer.cpp
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : CAPI_GL_DistortionRenderer.h
4 Content : Distortion renderer header for GL
5 Created : November 11, 2013
6 Authors : David Borel, Lee Cooper
7 
8 Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
9 
10 Use of this software is subject to the terms of the Oculus Inc license
11 agreement provided at the time of installation or download, or which
12 otherwise accompanies this software in either electronic or hard copy form.
13 
14 ************************************************************************************/
15 
17 
19 
20 #include "../../OVR_CAPI_GL.h"
21 
22 namespace OVR { namespace CAPI { namespace GL {
23 
24 // Distortion pixel shader lookup.
25 // Bit 0: Chroma Correction
26 // Bit 1: Timewarp
27 
28 enum {
33 };
34 
35 struct ShaderInfo
36 {
37  const char* ShaderData;
38  size_t ShaderSize;
41 };
42 
43 // Do add a new distortion shader use these macros (with or w/o reflection)
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)) }
46 
47 
49 {
54 };
55 
57 {
60 };
61 
63 {
66 }
67 
68 
69 
71 {
77 };
78 
79 
80 // Vertex type; same format is used for all shapes for simplicity.
81 // Shapes are built by adding vertices to Model.
83 {
85  LatencyVertex (const Vector3f& p) : Pos(p) {}
86 };
87 
88 
89 //----------------------------------------------------------------------------
90 // ***** GL::DistortionRenderer
91 
93  const HMDRenderState& renderState)
94  : CAPI::DistortionRenderer(ovrRenderAPI_OpenGL, hmd, timeManager, renderState)
95  , LatencyVAO(0)
96 {
97  DistortionMeshVAOs[0] = 0;
98  DistortionMeshVAOs[1] = 0;
99 }
100 
102 {
103  destroy();
104 }
105 
106 // static
108  FrameTimeManager& timeManager,
109  const HMDRenderState& renderState)
110 {
111 #if !defined(OVR_OS_MAC)
113 #endif
114  return new DistortionRenderer(hmd, timeManager, renderState);
115 }
116 
117 
119  unsigned distortionCaps)
120 {
121  GfxState = *new GraphicsState();
122 
123  const ovrGLConfig* config = (const ovrGLConfig*)apiConfig;
124 
125  if (!config)
126  {
127  // Cleanup
128  pEyeTextures[0].Clear();
129  pEyeTextures[1].Clear();
130  memset(&RParams, 0, sizeof(RParams));
131  return true;
132  }
133 
135  RParams.RTSize = config->OGL.Header.RTSize;
136 #if defined(OVR_OS_WIN32)
137  RParams.Window = (config->OGL.Window) ? config->OGL.Window : GetActiveWindow();
138 #elif defined(OVR_OS_LINUX)
139  RParams.Disp = (config->OGL.Disp) ? config->OGL.Disp : XOpenDisplay(NULL);
140  RParams.Win = config->OGL.Win;
141  if (!RParams.Win)
142  {
143  int unused;
144  XGetInputFocus(RParams.Disp, &RParams.Win, &unused);
145  }
146 #endif
147 
148  DistortionCaps = distortionCaps;
149 
150  //DistortionWarper.SetVsync((hmdCaps & ovrHmdCap_NoVSync) ? false : true);
151 
152  pEyeTextures[0] = *new Texture(&RParams, 0, 0);
153  pEyeTextures[1] = *new Texture(&RParams, 0, 0);
154 
156 
157  return true;
158 }
159 
160 
161 void DistortionRenderer::SubmitEye(int eyeId, ovrTexture* eyeTexture)
162 {
163  // Doesn't do a lot in here??
164  const ovrGLTexture* tex = (const ovrGLTexture*)eyeTexture;
165 
166  // Write in values
167  eachEye[eyeId].texture = tex->OGL.TexId;
168 
169  if (tex)
170  {
171  // Its only at this point we discover what the viewport of the texture is.
172  // because presumably we allow users to realtime adjust the resolution.
173  eachEye[eyeId].TextureSize = tex->OGL.Header.TextureSize;
175 
176  const ovrEyeRenderDesc& erd = RState.EyeRenderDesc[eyeId];
177 
179  eachEye[eyeId].TextureSize, eachEye[eyeId].RenderViewport,
180  eachEye[eyeId].UVScaleOffset );
181 
182  pEyeTextures[eyeId]->UpdatePlaceholderTexture(tex->OGL.TexId,
183  tex->OGL.Header.TextureSize);
184  }
185 }
186 
187 void DistortionRenderer::EndFrame(bool swapBuffers,
188  unsigned char* latencyTesterDrawColor, unsigned char* latencyTester2DrawColor)
189 {
191  {
193  {
194  // Wait for timewarp distortion if it is time and Gpu idle
196  }
197 
199  }
200  else
201  {
202  // If needed, measure distortion time so that TimeManager can better estimate
203  // latency-reducing time-warp wait timing.
205  double distortionStartTime = ovr_GetTimeInSeconds();
206 
208 
211  }
212 
213  if(latencyTesterDrawColor)
214  {
215  renderLatencyQuad(latencyTesterDrawColor);
216  }
217  else if(latencyTester2DrawColor)
218  {
219  renderLatencyPixel(latencyTester2DrawColor);
220  }
221 
222  if (swapBuffers)
223  {
224  bool useVsync = ((RState.EnabledHmdCaps & ovrHmdCap_NoVSync) == 0);
225  int swapInterval = (useVsync) ? 1 : 0;
226 #if defined(OVR_OS_WIN32)
227  if (wglGetSwapIntervalEXT() != swapInterval)
228  wglSwapIntervalEXT(swapInterval);
229 
230  HDC dc = GetDC(RParams.Window);
231  BOOL success = SwapBuffers(dc);
232  ReleaseDC(RParams.Window, dc);
233  OVR_ASSERT(success);
234  OVR_UNUSED(success);
235 #elif defined(OVR_OS_MAC)
236  CGLContextObj context = CGLGetCurrentContext();
237  GLint currentSwapInterval = 0;
238  CGLGetParameter(context, kCGLCPSwapInterval, &currentSwapInterval);
239  if (currentSwapInterval != swapInterval)
240  CGLSetParameter(context, kCGLCPSwapInterval, &swapInterval);
241 
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"));
246  if (supportsVSync)
247  {
248  GLuint currentSwapInterval = 0;
249  glXQueryDrawable(RParams.Disp, RParams.Win, GLX_SWAP_INTERVAL_EXT, &currentSwapInterval);
250  if (currentSwapInterval != swapInterval)
251  glXSwapIntervalEXT(RParams.Disp, RParams.Win, swapInterval);
252  }
253 
254  glXSwapBuffers(RParams.Disp, RParams.Win);
255 #endif
256  }
257 }
258 
260 {
261  glFlush();
262  glFinish();
263 }
264 
266 {
267  double initialTime = ovr_GetTimeInSeconds();
268  if (initialTime >= absTime)
269  return 0.0;
270 
271  glFlush();
272  glFinish();
273 
274  double newTime = initialTime;
275  volatile int i;
276 
277  while (newTime < absTime)
278  {
279  for (int j = 0; j < 50; j++)
280  i = 0;
281 
282  newTime = ovr_GetTimeInSeconds();
283  }
284 
285  // How long we waited
286  return newTime - initialTime;
287 }
288 
289 
291 {
292  const char* glVersionString = (const char*)glGetString(GL_VERSION);
293  OVR_DEBUG_LOG(("GL_VERSION STRING: %s", (const char*)glVersionString));
294  char prefix[64];
295  bool foundVersion = false;
296 
297  for (int i = 10; i < 30; ++i)
298  {
299  int major = i / 10;
300  int minor = i % 10;
301  OVR_sprintf(prefix, 64, "%d.%d", major, minor);
302  if (strstr(glVersionString, prefix) == glVersionString)
303  {
306  foundVersion = true;
307  break;
308  }
309  }
310 
311  if (!foundVersion)
312  {
313  glGetIntegerv(GL_MAJOR_VERSION, &GlMajorVersion);
314  glGetIntegerv(GL_MAJOR_VERSION, &GlMinorVersion);
315  }
316 
318 
319  if (GlMajorVersion >= 3)
320  {
321  SupportsVao = true;
322  }
323  else
324  {
325  const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
326  SupportsVao = (strstr("GL_ARB_vertex_array_object", extensions) != NULL);
327  }
328 }
329 
330 
331 void DistortionRenderer::GraphicsState::ApplyBool(GLenum Name, GLint Value)
332 {
333  if (Value != 0)
334  glEnable(Name);
335  else
336  glDisable(Name);
337 }
338 
339 
341 {
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);
358 
359  IsValid = true;
360 }
361 
362 
364 {
365  // Don't allow restore-before-save.
366  if (!IsValid)
367  return;
368 
369  glViewport(Viewport[0], Viewport[1], Viewport[2], Viewport[3]);
370  glClearColor(ClearColor[0], ClearColor[1], ClearColor[2], ClearColor[3]);
371 
372  ApplyBool(GL_DEPTH_TEST, DepthTest);
373  ApplyBool(GL_CULL_FACE, CullFace);
374 
375  glUseProgram(Program);
376  glActiveTexture(ActiveTexture);
377  glBindTexture(GL_TEXTURE_2D, TextureBinding);
378  if (SupportsVao)
379  glBindVertexArray(VertexArray);
380  glBindFramebuffer(GL_FRAMEBUFFER, FrameBufferBinding);
381 
382  ApplyBool(GL_BLEND, Blend);
383 
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);
390 }
391 
392 
394 {
395  for ( int eyeNum = 0; eyeNum < 2; eyeNum++ )
396  {
397  // Allocate & generate distortion mesh vertices.
398  ovrDistortionMesh meshData;
399 
400 // double startT = ovr_GetTimeInSeconds();
401 
403  RState.EyeRenderDesc[eyeNum].Eye,
404  RState.EyeRenderDesc[eyeNum].Fov,
406  &meshData) )
407  {
408  OVR_ASSERT(false);
409  continue;
410  }
411 
412  // Now parse the vertex data and create a render ready vertex buffer from it
413  DistortionVertex * pVBVerts = (DistortionVertex*)OVR_ALLOC ( sizeof(DistortionVertex) * meshData.VertexCount );
414  DistortionVertex * pCurVBVert = pVBVerts;
415  ovrDistortionVertex* pCurOvrVert = meshData.pVertexData;
416 
417  for ( unsigned vertNum = 0; vertNum < meshData.VertexCount; vertNum++ )
418  {
419  pCurVBVert->Pos.x = pCurOvrVert->Pos.x;
420  pCurVBVert->Pos.y = pCurOvrVert->Pos.y;
421  pCurVBVert->TexR = (*(Vector2f*)&pCurOvrVert->TexR);
422  pCurVBVert->TexG = (*(Vector2f*)&pCurOvrVert->TexG);
423  pCurVBVert->TexB = (*(Vector2f*)&pCurOvrVert->TexB);
424  // Convert [0.0f,1.0f] to [0,255]
425  pCurVBVert->Col.R = (OVR::UByte)( pCurOvrVert->VignetteFactor * 255.99f );
426  pCurVBVert->Col.G = pCurVBVert->Col.R;
427  pCurVBVert->Col.B = pCurVBVert->Col.R;
428  pCurVBVert->Col.A = (OVR::UByte)( pCurOvrVert->TimeWarpFactor * 255.99f );;
429  pCurOvrVert++;
430  pCurVBVert++;
431  }
432 
433  DistortionMeshVBs[eyeNum] = *new Buffer(&RParams);
434  DistortionMeshVBs[eyeNum]->Data ( Buffer_Vertex | Buffer_ReadOnly, pVBVerts, sizeof(DistortionVertex) * meshData.VertexCount );
435  DistortionMeshIBs[eyeNum] = *new Buffer(&RParams);
436  DistortionMeshIBs[eyeNum]->Data ( Buffer_Index | Buffer_ReadOnly, meshData.pIndexData, ( sizeof(SInt16) * meshData.IndexCount ) );
437 
438  OVR_FREE ( pVBVerts );
439  ovrHmd_DestroyDistortionMesh( &meshData );
440  }
441 
442  initShaders();
443 }
444 
445 void DistortionRenderer::renderDistortion(Texture* leftEyeTexture, Texture* rightEyeTexture)
446 {
447  GraphicsState* glState = (GraphicsState*)GfxState.GetPtr();
448 
449  glBindFramebuffer(GL_FRAMEBUFFER, 0);
451 
452  glDisable(GL_CULL_FACE);
453  glDisable(GL_DEPTH_TEST);
454  glDisable(GL_BLEND);
455 
456  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
457  glDisable(GL_DITHER);
458  glDisable(GL_RASTERIZER_DISCARD);
459  if (glState->GlMajorVersion >= 3 && glState->GlMajorVersion >= 2)
460  glDisable(GL_SAMPLE_MASK);
461  glDisable(GL_SCISSOR_TEST);
462 
463  glClearColor(
464  RState.ClearColor[0],
465  RState.ClearColor[1],
466  RState.ClearColor[2],
467  RState.ClearColor[3] );
468 
469  glClear(GL_COLOR_BUFFER_BIT);
470 
471  for (int eyeNum = 0; eyeNum < 2; eyeNum++)
472  {
473  ShaderFill distortionShaderFill(DistortionShader);
474  distortionShaderFill.SetTexture(0, eyeNum == 0 ? leftEyeTexture : rightEyeTexture);
475 
476  DistortionShader->SetUniform2f("EyeToSourceUVScale", eachEye[eyeNum].UVScaleOffset[0].x, eachEye[eyeNum].UVScaleOffset[0].y);
477  DistortionShader->SetUniform2f("EyeToSourceUVOffset", eachEye[eyeNum].UVScaleOffset[1].x, eachEye[eyeNum].UVScaleOffset[1].y);
478 
480  {
481  ovrMatrix4f timeWarpMatrices[2];
483  RState.EyeRenderPoses[eyeNum], timeWarpMatrices);
484 
485  // Feed identity like matrices in until we get proper timewarp calculation going on
486  DistortionShader->SetUniform4x4f("EyeRotationStart", Matrix4f(timeWarpMatrices[0]).Transposed());
487  DistortionShader->SetUniform4x4f("EyeRotationEnd", Matrix4f(timeWarpMatrices[1]).Transposed());
488 
489  renderPrimitives(&distortionShaderFill, DistortionMeshVBs[eyeNum], DistortionMeshIBs[eyeNum],
490  0, (int)DistortionMeshIBs[eyeNum]->GetSize()/2, Prim_Triangles, &DistortionMeshVAOs[eyeNum], true);
491  }
492  else
493  {
494  renderPrimitives(&distortionShaderFill, DistortionMeshVBs[eyeNum], DistortionMeshIBs[eyeNum],
495  0, (int)DistortionMeshIBs[eyeNum]->GetSize()/2, Prim_Triangles, &DistortionMeshVAOs[eyeNum], true);
496  }
497  }
498 }
499 
501 {
502  const int numQuadVerts = 4;
505  {
506  return;
507  }
508 
509  LatencyTesterQuadVB->Data(Buffer_Vertex, NULL, numQuadVerts * sizeof(LatencyVertex));
510  LatencyVertex* vertices = (LatencyVertex*)LatencyTesterQuadVB->Map(0, numQuadVerts * sizeof(LatencyVertex), Map_Discard);
511  if(!vertices)
512  {
513  OVR_ASSERT(false); // failed to lock vertex buffer
514  return;
515  }
516 
517  const float left = -1.0f;
518  const float top = -1.0f;
519  const float right = 1.0f;
520  const float bottom = 1.0f;
521 
522  vertices[0] = LatencyVertex(Vector3f(left, top, 0.0f));
523  vertices[1] = LatencyVertex(Vector3f(left, bottom, 0.0f));
524  vertices[2] = LatencyVertex(Vector3f(right, top, 0.0f));
525  vertices[3] = LatencyVertex(Vector3f(right, bottom, 0.0f));
526 
527  LatencyTesterQuadVB->Unmap(vertices);
528 }
529 
530 void DistortionRenderer::renderLatencyQuad(unsigned char* latencyTesterDrawColor)
531 {
532  const int numQuadVerts = 4;
533 
535  {
536  createDrawQuad();
537  }
538 
539  ShaderFill quadFill(SimpleQuadShader);
540  //quadFill.SetInputLayout(SimpleQuadVertexIL);
541 
543 
544  SimpleQuadShader->SetUniform2f("Scale", 0.2f, 0.2f);
545  SimpleQuadShader->SetUniform4f("Color", (float)latencyTesterDrawColor[0] / 255.99f,
546  (float)latencyTesterDrawColor[0] / 255.99f,
547  (float)latencyTesterDrawColor[0] / 255.99f,
548  1.0f);
549 
550  for(int eyeNum = 0; eyeNum < 2; eyeNum++)
551  {
552  SimpleQuadShader->SetUniform2f("PositionOffset", eyeNum == 0 ? -0.4f : 0.4f, 0.0f);
553  renderPrimitives(&quadFill, LatencyTesterQuadVB, NULL, 0, numQuadVerts, Prim_TriangleStrip, &LatencyVAO, false);
554  }
555 }
556 
557 void DistortionRenderer::renderLatencyPixel(unsigned char* latencyTesterPixelColor)
558 {
559  const int numQuadVerts = 4;
560 
562  {
563  createDrawQuad();
564  }
565 
566  ShaderFill quadFill(SimpleQuadShader);
567 
569 
570  SimpleQuadShader->SetUniform4f("Color", (float)latencyTesterPixelColor[0] / 255.99f,
571  (float)latencyTesterPixelColor[0] / 255.99f,
572  (float)latencyTesterPixelColor[0] / 255.99f,
573  1.0f);
574 
575  Vector2f scale(2.0f / RParams.RTSize.w, 2.0f / RParams.RTSize.h);
576  SimpleQuadShader->SetUniform2f("Scale", scale.x, scale.y);
577  SimpleQuadShader->SetUniform2f("PositionOffset", 1.0f, 1.0f);
578  renderPrimitives(&quadFill, LatencyTesterQuadVB, NULL, 0, numQuadVerts, Prim_TriangleStrip, &LatencyVAO, false);
579 }
580 
582  const ShaderFill* fill,
583  Buffer* vertices, Buffer* indices,
584  int offset, int count,
585  PrimitiveType rprim, GLuint* vao, bool isDistortionMesh)
586 {
587  GraphicsState* glState = (GraphicsState*)GfxState.GetPtr();
588 
589  GLenum prim;
590  switch (rprim)
591  {
592  case Prim_Triangles:
593  prim = GL_TRIANGLES;
594  break;
595  case Prim_Lines:
596  prim = GL_LINES;
597  break;
598  case Prim_TriangleStrip:
599  prim = GL_TRIANGLE_STRIP;
600  break;
601  default:
602  OVR_ASSERT(false);
603  return;
604  }
605 
606  fill->Set();
607 
608  GLuint prog = fill->GetShaders()->Prog;
609 
610  if (vao != NULL)
611  {
612  if (*vao != 0)
613  {
614  glBindVertexArray(*vao);
615 
616  if (isDistortionMesh)
617  glDrawElements(prim, count, GL_UNSIGNED_SHORT, NULL);
618  else
619  glDrawArrays(prim, 0, count);
620  }
621  else
622  {
623  if (glState->SupportsVao)
624  {
625  glGenVertexArrays(1, vao);
626  glBindVertexArray(*vao);
627  }
628 
629  int attributeCount = (isDistortionMesh) ? 5 : 1;
630  int* locs = new int[attributeCount];
631 
632  glBindBuffer(GL_ARRAY_BUFFER, ((Buffer*)vertices)->GLBuffer);
633 
634  if (isDistortionMesh)
635  {
636  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ((Buffer*)indices)->GLBuffer);
637 
638  locs[0] = glGetAttribLocation(prog, "Position");
639  locs[1] = glGetAttribLocation(prog, "Color");
640  locs[2] = glGetAttribLocation(prog, "TexCoord0");
641  locs[3] = glGetAttribLocation(prog, "TexCoord1");
642  locs[4] = glGetAttribLocation(prog, "TexCoord2");
643 
644  glVertexAttribPointer(locs[0], 2, GL_FLOAT, false, sizeof(DistortionVertex), reinterpret_cast<char*>(offset)+offsetof(DistortionVertex, Pos));
645  glVertexAttribPointer(locs[1], 4, GL_UNSIGNED_BYTE, true, sizeof(DistortionVertex), reinterpret_cast<char*>(offset)+offsetof(DistortionVertex, Col));
646  glVertexAttribPointer(locs[2], 2, GL_FLOAT, false, sizeof(DistortionVertex), reinterpret_cast<char*>(offset)+offsetof(DistortionVertex, TexR));
647  glVertexAttribPointer(locs[3], 2, GL_FLOAT, false, sizeof(DistortionVertex), reinterpret_cast<char*>(offset)+offsetof(DistortionVertex, TexG));
648  glVertexAttribPointer(locs[4], 2, GL_FLOAT, false, sizeof(DistortionVertex), reinterpret_cast<char*>(offset)+offsetof(DistortionVertex, TexB));
649  }
650  else
651  {
652  locs[0] = glGetAttribLocation(prog, "Position");
653 
654  glVertexAttribPointer(locs[0], 3, GL_FLOAT, false, sizeof(LatencyVertex), reinterpret_cast<char*>(offset)+offsetof(LatencyVertex, Pos));
655  }
656 
657  for (int i = 0; i < attributeCount; ++i)
658  glEnableVertexAttribArray(locs[i]);
659 
660  if (isDistortionMesh)
661  glDrawElements(prim, count, GL_UNSIGNED_SHORT, NULL);
662  else
663  glDrawArrays(prim, 0, count);
664 
665 
666  if (!glState->SupportsVao)
667  {
668  for (int i = 0; i < attributeCount; ++i)
670  }
671 
672  delete[] locs;
673  }
674  }
675 }
676 
678 {
679  glViewport(vp.x, vp.y, vp.w, vp.h);
680 }
681 
682 
684 {
685  GraphicsState* glState = (GraphicsState*)GfxState.GetPtr();
686 
687  const char* shaderPrefix =
688  (glState->GlMajorVersion < 3 || (glState->GlMajorVersion == 3 && glState->GlMinorVersion < 2)) ?
690 
691  {
693 
694  size_t vsSize = strlen(shaderPrefix)+vsInfo.ShaderSize;
695  char* vsSource = new char[vsSize];
696  OVR_strcpy(vsSource, vsSize, shaderPrefix);
697  OVR_strcat(vsSource, vsSize, vsInfo.ShaderData);
698 
700  &RParams,
701  (void*)vsSource, vsSize,
702  vsInfo.ReflectionData, vsInfo.ReflectionSize);
703 
705  DistortionShader->SetShader(vs);
706 
707  delete[](vsSource);
708 
710 
711  size_t psSize = strlen(shaderPrefix)+psInfo.ShaderSize;
712  char* psSource = new char[psSize];
713  OVR_strcpy(psSource, psSize, shaderPrefix);
714  OVR_strcat(psSource, psSize, psInfo.ShaderData);
715 
717  &RParams,
718  (void*)psSource, psSize,
719  psInfo.ReflectionData, psInfo.ReflectionSize);
720 
721  DistortionShader->SetShader(ps);
722 
723  delete[](psSource);
724  }
725  {
726  size_t vsSize = strlen(shaderPrefix)+sizeof(SimpleQuad_vs);
727  char* vsSource = new char[vsSize];
728  OVR_strcpy(vsSource, vsSize, shaderPrefix);
729  OVR_strcat(vsSource, vsSize, SimpleQuad_vs);
730 
732  &RParams,
733  (void*)vsSource, vsSize,
735 
737  SimpleQuadShader->SetShader(vs);
738 
739  delete[](vsSource);
740 
741  size_t psSize = strlen(shaderPrefix)+sizeof(SimpleQuad_fs);
742  char* psSource = new char[psSize];
743  OVR_strcpy(psSource, psSize, shaderPrefix);
744  OVR_strcat(psSource, psSize, SimpleQuad_fs);
745 
747  &RParams,
748  (void*)psSource, psSize,
750 
751  SimpleQuadShader->SetShader(ps);
752 
753  delete[](psSource);
754  }
755 }
756 
757 
759 {
760  GraphicsState* glState = (GraphicsState*)GfxState.GetPtr();
761 
762  for(int eyeNum = 0; eyeNum < 2; eyeNum++)
763  {
764  if (glState->SupportsVao)
766 
767  DistortionMeshVAOs[eyeNum] = 0;
768 
769  DistortionMeshVBs[eyeNum].Clear();
770  DistortionMeshIBs[eyeNum].Clear();
771  }
772 
773  if (DistortionShader)
774  {
775  DistortionShader->UnsetShader(Shader_Vertex);
776  DistortionShader->UnsetShader(Shader_Pixel);
777  DistortionShader.Clear();
778  }
779 
780  LatencyTesterQuadVB.Clear();
781  LatencyVAO = 0;
782 }
783 
784 }}} // OVR::CAPI::GL
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)
Definition: OVR_CAPI.cpp:679
static const char glsl2Prefix[]
virtual void SubmitEye(int eyeId, ovrTexture *eyeTexture)
static const char Distortion_vs[]
ShaderImpl< Shader_Fragment, GL_FRAGMENT_SHADER > FragmentShader
Definition: CAPI_GL_Util.h:533
ovrEyeType Eye
Definition: OVR_CAPI.h:321
static ShaderInfo DistortionVertexShaderLookup[DistortionVertexShaderCount]
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer
Timing GetFrameTiming(unsigned frameIndex)
#define NULL
UByte R
Definition: OVR_Color.h:36
static const char DistortionTimewarp_vs[]
int h
Definition: OVR_CAPI.h:54
const ShaderBase::Uniform * ReflectionData
UByte G
Definition: OVR_Color.h:36
ovrFovPort Fov
Definition: OVR_CAPI.h:322
ovrVector2f TexG
Definition: OVR_CAPI.h:616
float y
Definition: OVR_CAPI.h:69
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays
unsigned int VertexCount
Definition: OVR_CAPI.h:626
__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])
Definition: OVR_CAPI.cpp:631
OVR_EXPORT void ovrHmd_DestroyDistortionMesh(ovrDistortionMesh *meshData)
Definition: OVR_CAPI.cpp:740
OVR_EXPORT double ovr_GetTimeInSeconds()
Definition: OVR_CAPI.cpp:182
#define OVR_UNUSED(a)
#define SI_REFL__(shader)
static const char glsl3Prefix[]
ovrDistortionVertex * pVertexData
Definition: OVR_CAPI.h:624
uint8_t UByte
Definition: OVR_Types.h:249
Matrix4< float > Matrix4f
Definition: OVR_Math.h:1834
ovrGLTextureData OGL
Definition: OVR_CAPI_GL.h:70
ShaderSet * GetShaders() const
Definition: CAPI_GL_Util.h:398
OVR_EXPORT void ovrHmd_GetRenderScaleAndOffset(ovrFovPort fov, ovrSizei textureSize, ovrRecti renderViewport, ovrVector2f uvScaleOffsetOut[2])
Definition: OVR_CAPI.cpp:755
char *OVR_CDECL OVR_strcpy(char *dest, UPInt destsize, const char *src)
Definition: OVR_Std.h:148
UByte B
Definition: OVR_Color.h:36
ovrEyeType
Definition: OVR_CAPI.h:177
static const char SimpleQuad_vs[]
PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer
void renderDistortion(Texture *leftEyeTexture, Texture *rightEyeTexture)
#define major(dev)
Definition: OVR_Types.h:61
static ShaderInfo DistortionPixelShaderLookup[DistortionPixelShaderCount]
ovrVector2f TexB
Definition: OVR_CAPI.h:617
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[]
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays
unsigned int IndexCount
Definition: OVR_CAPI.h:627
float x
Definition: OVR_CAPI.h:69
PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation
#define OVR_ASSERT(p)
PFNGLUSEPROGRAMPROC glUseProgram
virtual void Set(PrimitiveType prim=Prim_Unknown) const
Definition: CAPI_GL_Util.h:401
void AddDistortionTimeMeasurement(double distortionTimeSeconds)
#define SI_NOREFL(shader)
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray
ovrVector2f Pos
Definition: OVR_CAPI.h:612
ovrVector2f TexR
Definition: OVR_CAPI.h:615
void renderLatencyQuad(unsigned char *latencyTesterDrawColor)
ovrRecti RenderViewport
Definition: OVR_CAPI.h:374
ovrSizei TextureSize
Definition: OVR_CAPI.h:373
static const char Distortion_fs[]
char *OVR_CDECL OVR_strcat(char *dest, UPInt destsize, const char *src)
Definition: OVR_Std.h:170
PFNGLBINDVERTEXARRAYPROC glBindVertexArray
ovrTextureHeader Header
Definition: OVR_CAPI_GL.h:63
int16_t SInt16
Definition: OVR_Types.h:250
ovrGLConfigData OGL
Definition: OVR_CAPI_GL.h:56
void renderLatencyPixel(unsigned char *latencyTesterPixelColor)
static const char DistortionTimewarpChroma_vs[]
PFNGLBINDBUFFERPROC glBindBuffer
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
Definition: OVR_CAPI_GL.h:43
virtual void SetTexture(int i, class Texture *tex)
Definition: CAPI_GL_Util.h:412
UPInt OVR_CDECL OVR_sprintf(char *dest, UPInt destsize, const char *format,...)
Definition: OVR_Std.h:280
UByte A
Definition: OVR_Color.h:36
unsigned short * pIndexData
Definition: OVR_CAPI.h:625
ShaderImpl< Shader_Vertex, GL_VERTEX_SHADER > VertexShader
Definition: CAPI_GL_Util.h:532
#define OVR_ALLOC(s)
int w
Definition: OVR_CAPI.h:54
#define OVR_DEBUG_LOG(args)
Definition: OVR_Log.h:196
PFNGLACTIVETEXTUREPROC glActiveTexture
virtual bool Initialize(const ovrRenderAPIConfig *apiConfig, unsigned distortionCaps)
#define OVR_FREE(p)
Rect< int > Recti
Definition: OVR_Math.h:681
Vector3< float > Vector3f
Definition: OVR_Math.h:554
__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]
void InitGLExtensions()
ovrEyeRenderDesc EyeRenderDesc[2]