File: CompositorOGLVR.cpp

package info (click to toggle)
icedove 1%3A45.8.0-3~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 1,488,584 kB
  • ctags: 1,068,813
  • sloc: cpp: 4,801,496; ansic: 1,929,291; python: 379,296; java: 252,018; xml: 173,182; asm: 146,741; sh: 89,229; makefile: 23,462; perl: 16,380; objc: 4,088; yacc: 1,841; lex: 1,222; exp: 499; php: 437; lisp: 228; awk: 152; pascal: 116; sed: 51; ruby: 47; csh: 31; ada: 16
file content (338 lines) | stat: -rw-r--r-- 13,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "CompositorOGL.h"
#include <sstream>                      // for ostringstream
#include <stddef.h>                     // for size_t
#include <stdint.h>                     // for uint32_t, uint8_t
#include <stdlib.h>                     // for free, malloc
#include "mozilla/layers/LayerManagerComposite.h"  // for LayerComposite, etc
#include "mozilla/layers/Effects.h"     // for EffectChain, TexturedEffect, etc
#include "mozilla/layers/CompositingRenderTargetOGL.h"
#include "mozilla/layers/TextureHost.h"  // for TextureSource, etc
#include "mozilla/layers/TextureHostOGL.h"  // for TextureSourceOGL, etc
#include "GLContextProvider.h"          // for GLContextProvider
#include "GLContext.h"                  // for GLContext
#include "GLUploadHelpers.h"
#include "Layers.h"                     // for WriteSnapshotToDumpFile
#include "LayerScope.h"                 // for LayerScope
#include "gfxCrashReporterUtils.h"      // for ScopedGfxFeatureReporter
#include "gfxMatrix.h"                  // for gfxMatrix
#include "gfxPlatform.h"                // for gfxPlatform
#include "gfxPrefs.h"                   // for gfxPrefs
#include "gfxRect.h"                    // for gfxRect
#include "gfxUtils.h"                   // for NextPowerOfTwo, gfxUtils, etc
#include "gfxVR.h"

namespace mozilla {

using namespace std;
using namespace gfx;

namespace layers {

using namespace mozilla::gl;

bool
CompositorOGL::InitializeVR()
{
  gl()->fGenBuffers(2, mVR.mDistortionVertices);
  gl()->fGenBuffers(2, mVR.mDistortionIndices);

  // these are explicitly bound later
  mVR.mAPosition = 0;
  mVR.mATexCoord0 = 1;
  mVR.mATexCoord1 = 2;
  mVR.mATexCoord2 = 3;
  mVR.mAGenericAttribs = 4;

  ostringstream vs;

  vs << "uniform vec4 uVREyeToSource;\n";
  vs << "uniform vec4 uVRDestinationScaleAndOffset;\n";
  vs << "uniform float uHeight;\n";
  vs << "attribute vec2 aPosition;\n";
  vs << "attribute vec2 aTexCoord0;\n";
  vs << "attribute vec2 aTexCoord1;\n";
  vs << "attribute vec2 aTexCoord2;\n";
  vs << "attribute vec4 aGenericAttribs;\n";
  vs << "varying vec2 vTexCoord0;\n";
  vs << "varying vec2 vTexCoord1;\n";
  vs << "varying vec2 vTexCoord2;\n";
  vs << "varying vec4 vGenericAttribs;\n";
  vs << "void main() {\n";
  vs << "  gl_Position = vec4(aPosition.xy * uVRDestinationScaleAndOffset.zw + uVRDestinationScaleAndOffset.xy, 0.5, 1.0);\n";
  vs << "  vTexCoord0 = uVREyeToSource.zw * aTexCoord0 + uVREyeToSource.xy;\n";
  vs << "  vTexCoord1 = uVREyeToSource.zw * aTexCoord1 + uVREyeToSource.xy;\n";
  vs << "  vTexCoord2 = uVREyeToSource.zw * aTexCoord2 + uVREyeToSource.xy;\n";
  vs << "  vTexCoord0.y = uHeight - vTexCoord0.y;\n";
  vs << "  vTexCoord1.y = uHeight - vTexCoord1.y;\n";
  vs << "  vTexCoord2.y = uHeight - vTexCoord2.y;\n";
  vs << "  vGenericAttribs = aGenericAttribs;\n";
  vs << "}\n";

  std::string vsSrcString(vs.str());
  const char *vsSrc = vsSrcString.c_str();

  for (int programIndex = 0; programIndex < 2; programIndex++) {
    GLenum textureTarget = programIndex == 0 ? LOCAL_GL_TEXTURE_2D : LOCAL_GL_TEXTURE_RECTANGLE;

    const char *sampler2D = "sampler2D";
    const char *texture2D = "texture2D";

    ostringstream fs;

    if (textureTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB) {
      fs << "#extension GL_ARB_texture_rectangle : require\n";
      sampler2D = "sampler2DRect";
      texture2D = "texture2DRect";
    }

    if (gl()->IsGLES()) {
      fs << "precision highp float;\n";
    }

    fs << "uniform " << sampler2D << " uTexture;\n";
    fs << "varying vec2 vTexCoord0;\n";
    fs << "varying vec2 vTexCoord1;\n";
    fs << "varying vec2 vTexCoord2;\n";
    fs << "varying vec4 vGenericAttribs;\n";
    fs << "void main() {\n";
    fs << "  float resR = " << texture2D << "(uTexture, vTexCoord0.xy).r;\n";
    fs << "  float resG = " << texture2D << "(uTexture, vTexCoord1.xy).g;\n";
    fs << "  float resB = " << texture2D << "(uTexture, vTexCoord2.xy).b;\n";
    fs << "  gl_FragColor = vec4(resR * vGenericAttribs.r, resG * vGenericAttribs.r, resB * vGenericAttribs.r, 1.0);\n";
    fs << "}\n";

    std::string fsSrcString(fs.str());
    const char *fsSrc = fsSrcString.c_str();

    GLint status;
#ifdef DEBUG
    GLint length;
    nsCString logStr;
#endif

    GLuint shaders[2];
    shaders[0] = gl()->fCreateShader(LOCAL_GL_VERTEX_SHADER);
    shaders[1] = gl()->fCreateShader(LOCAL_GL_FRAGMENT_SHADER);

    GLint shaderLen[2] = { (GLint) strlen(vsSrc), (GLint) strlen(fsSrc) };
    gl()->fShaderSource(shaders[0], 1, &vsSrc, &shaderLen[0]);
    gl()->fShaderSource(shaders[1], 1, &fsSrc, &shaderLen[1]);

    for (int k = 0; k < 2; ++k) {
      gl()->fCompileShader(shaders[k]);
      gl()->fGetShaderiv(shaders[k], LOCAL_GL_COMPILE_STATUS, &status);
      if (!status) {
        NS_WARNING("VR GL shader failed to compile!");

#ifdef DEBUG
        gl()->fGetShaderiv(shaders[k], LOCAL_GL_INFO_LOG_LENGTH, &length);
        logStr.SetLength(length);
        gl()->fGetShaderInfoLog(shaders[k], length, &length, logStr.BeginWriting());
        printf_stderr("GL %s shader failed to compile:\n%s\n", k == 0 ? "vertex" : "fragment", logStr.BeginReading());
#endif

        gl()->fDeleteShader(shaders[0]);
        gl()->fDeleteShader(shaders[1]);

        return false;
      }
    }

    GLuint prog = gl()->fCreateProgram();
    gl()->fAttachShader(prog, shaders[0]);
    gl()->fAttachShader(prog, shaders[1]);

    gl()->fBindAttribLocation(prog, mVR.mAPosition, "aPosition");
    gl()->fBindAttribLocation(prog, mVR.mATexCoord0, "aTexCoord0");
    gl()->fBindAttribLocation(prog, mVR.mATexCoord1, "aTexCoord1");
    gl()->fBindAttribLocation(prog, mVR.mATexCoord2, "aTexCoord2");
    gl()->fBindAttribLocation(prog, mVR.mAGenericAttribs, "aGenericAttribs");

    gl()->fLinkProgram(prog);

    gl()->fGetProgramiv(prog, LOCAL_GL_LINK_STATUS, &status);
    if (!status) {
      NS_WARNING("VR GL program failed to link!");

#ifdef DEBUG
      gl()->fGetProgramiv(prog, LOCAL_GL_INFO_LOG_LENGTH, &length);
      logStr.SetLength(length);
      gl()->fGetProgramInfoLog(prog, length, &length, logStr.BeginWriting());
      printf_stderr("GL shader failed to link:\n%s\n", logStr.BeginReading());
#endif

      gl()->fDeleteProgram(prog);
      gl()->fDeleteShader(shaders[0]);
      gl()->fDeleteShader(shaders[1]);

      return false;
    }

    mVR.mUTexture[programIndex] = gl()->fGetUniformLocation(prog, "uTexture");
    mVR.mUVREyeToSource[programIndex] = gl()->fGetUniformLocation(prog, "uVREyeToSource");
    mVR.mUVRDestionatinScaleAndOffset[programIndex] = gl()->fGetUniformLocation(prog, "uVRDestinationScaleAndOffset");
    mVR.mUHeight[programIndex] = gl()->fGetUniformLocation(prog, "uHeight");

    mVR.mDistortionProgram[programIndex] = prog;

    gl()->fDeleteShader(shaders[0]);
    gl()->fDeleteShader(shaders[1]);
  }

  mVR.mInitialized = true;
  return true;
}

void
CompositorOGL::DestroyVR(GLContext *gl)
{
  if (!mVR.mInitialized)
    return;

  gl->fDeleteBuffers(2, mVR.mDistortionVertices);
  gl->fDeleteBuffers(2, mVR.mDistortionIndices);
  gl->fDeleteProgram(mVR.mDistortionProgram[0]);
  gl->fDeleteProgram(mVR.mDistortionProgram[1]);

  mVR.mInitialized = false;
}

void
CompositorOGL::DrawVRDistortion(const gfx::Rect& aRect,
                                const gfx::Rect& aClipRect,
                                const EffectChain& aEffectChain,
                                gfx::Float aOpacity,
                                const gfx::Matrix4x4& aTransform)
{
  MOZ_ASSERT(aEffectChain.mPrimaryEffect->mType == EffectTypes::VR_DISTORTION);
  MOZ_ASSERT(mVR.mInitialized);

  if (aEffectChain.mSecondaryEffects[EffectTypes::MASK] ||
      aEffectChain.mSecondaryEffects[EffectTypes::BLEND_MODE])
  {
    NS_WARNING("DrawVRDistortion: ignoring secondary effect!");
  }

  EffectVRDistortion* vrEffect =
    static_cast<EffectVRDistortion*>(aEffectChain.mPrimaryEffect.get());

  GLenum textureTarget = LOCAL_GL_TEXTURE_2D;
  if (vrEffect->mRenderTarget)
    textureTarget = mFBOTextureTarget;

  RefPtr<CompositingRenderTargetOGL> surface =
    static_cast<CompositingRenderTargetOGL*>(vrEffect->mRenderTarget.get());

  VRHMDInfo* hmdInfo = vrEffect->mHMD;
  VRDistortionConstants shaderConstants;

  if (hmdInfo->GetConfiguration() != mVR.mConfiguration) {
    for (uint32_t eye = 0; eye < 2; eye++) {
      const gfx::VRDistortionMesh& mesh = hmdInfo->GetDistortionMesh(eye);
      gl()->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mVR.mDistortionVertices[eye]);
      gl()->fBufferData(LOCAL_GL_ARRAY_BUFFER,
                       mesh.mVertices.Length() * sizeof(gfx::VRDistortionVertex),
                       mesh.mVertices.Elements(),
                       LOCAL_GL_STATIC_DRAW);

      gl()->fBindBuffer(LOCAL_GL_ELEMENT_ARRAY_BUFFER, mVR.mDistortionIndices[eye]);
      gl()->fBufferData(LOCAL_GL_ELEMENT_ARRAY_BUFFER,
                       mesh.mIndices.Length() * sizeof(uint16_t),
                       mesh.mIndices.Elements(),
                       LOCAL_GL_STATIC_DRAW);

      mVR.mDistortionIndexCount[eye] = mesh.mIndices.Length();
    }

    mVR.mConfiguration = hmdInfo->GetConfiguration();
  }

  int programIndex = textureTarget == LOCAL_GL_TEXTURE_2D ? 0 : 1;

  gl()->fScissor(aClipRect.x, FlipY(aClipRect.y + aClipRect.height),
                 aClipRect.width, aClipRect.height);

  // Clear out the entire area that we want to render; this ensures that
  // the layer will be opaque, even though the mesh geometry we'll be
  // drawing below won't cover the full rectangle.
  gl()->fClearColor(0.0, 0.0, 0.0, 1.0);
  gl()->fClear(LOCAL_GL_COLOR_BUFFER_BIT | LOCAL_GL_DEPTH_BUFFER_BIT);

  // Make sure that the cached current program is reset for the
  // rest of the compositor, since we're using a custom program here
  ResetProgram();

  gl()->fUseProgram(mVR.mDistortionProgram[programIndex]);

  gl()->fEnableVertexAttribArray(mVR.mAPosition);
  gl()->fEnableVertexAttribArray(mVR.mATexCoord0);
  gl()->fEnableVertexAttribArray(mVR.mATexCoord1);
  gl()->fEnableVertexAttribArray(mVR.mATexCoord2);
  gl()->fEnableVertexAttribArray(mVR.mAGenericAttribs);

  surface->BindTexture(LOCAL_GL_TEXTURE0, mFBOTextureTarget);
  gl()->fUniform1i(mVR.mUTexture[programIndex], 0);

  Rect destRect = aTransform.TransformBounds(aRect);
  gfx::IntSize preDistortionSize = surface->GetInitSize(); // XXX source->GetSize()
  gfx::Size vpSize = destRect.Size();

  for (uint32_t eye = 0; eye < 2; eye++) {
    gfx::IntRect eyeViewport;
    eyeViewport.x = eye * preDistortionSize.width / 2;
    eyeViewport.y = 0;
    eyeViewport.width = preDistortionSize.width / 2;
    eyeViewport.height = preDistortionSize.height;

    hmdInfo->FillDistortionConstants(eye,
                                     preDistortionSize, eyeViewport,
                                     vpSize, destRect,
                                     shaderConstants);

    float height = 1.0f;
    float texScaleAndOffset[4] = { shaderConstants.eyeToSourceScaleAndOffset[0],
                                   shaderConstants.eyeToSourceScaleAndOffset[1],
                                   shaderConstants.eyeToSourceScaleAndOffset[2],
                                   shaderConstants.eyeToSourceScaleAndOffset[3] };
    if (textureTarget == LOCAL_GL_TEXTURE_RECTANGLE_ARB) {
      texScaleAndOffset[0] *= preDistortionSize.width;
      texScaleAndOffset[1] *= preDistortionSize.height;
      texScaleAndOffset[2] *= preDistortionSize.width;
      texScaleAndOffset[3] *= preDistortionSize.height;
      height = preDistortionSize.height;
    }

    gl()->fUniform4fv(mVR.mUVRDestionatinScaleAndOffset[programIndex], 1, shaderConstants.destinationScaleAndOffset);
    gl()->fUniform4fv(mVR.mUVREyeToSource[programIndex], 1, texScaleAndOffset);
    gl()->fUniform1f(mVR.mUHeight[programIndex], height);

    gl()->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mVR.mDistortionVertices[eye]);

    /* This is for Oculus DistortionVertex */

    gl()->fVertexAttribPointer(mVR.mAPosition,  2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex), (void*) (sizeof(float) * 0));
    gl()->fVertexAttribPointer(mVR.mATexCoord0, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex), (void*) (sizeof(float) * 2));
    gl()->fVertexAttribPointer(mVR.mATexCoord1, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex), (void*) (sizeof(float) * 4));
    gl()->fVertexAttribPointer(mVR.mATexCoord2, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex), (void*) (sizeof(float) * 6));
    gl()->fVertexAttribPointer(mVR.mAGenericAttribs, 4, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex),  (void*) (sizeof(float) * 8));

    gl()->fBindBuffer(LOCAL_GL_ELEMENT_ARRAY_BUFFER, mVR.mDistortionIndices[eye]);
    gl()->fDrawElements(LOCAL_GL_TRIANGLES, mVR.mDistortionIndexCount[eye], LOCAL_GL_UNSIGNED_SHORT, 0);
  }

  // Not clear if I should disable all of this; but going to do it and hope that
  // any later code will enable what it needs.
  gl()->fDisableVertexAttribArray(mVR.mAPosition);
  gl()->fDisableVertexAttribArray(mVR.mATexCoord0);
  gl()->fDisableVertexAttribArray(mVR.mATexCoord1);
  gl()->fDisableVertexAttribArray(mVR.mATexCoord2);
  gl()->fDisableVertexAttribArray(mVR.mAGenericAttribs);
}

} // namespace layers
} // namespace mozilla