Bug 1148009; [webvr] Move distortion vertex struct into more generic system; r=jrmuizel

This commit is contained in:
Vladimir Vukicevic 2015-04-01 16:02:20 -04:00
parent 2671867f5c
commit 06307ff21e
5 changed files with 49 additions and 24 deletions

View File

@ -178,6 +178,7 @@ struct CompositorOGLVRObjects {
GLint mUTexture[2];
GLint mUVREyeToSource[2];
GLint mUVRDestionatinScaleAndOffset[2];
GLint mUHeight[2];
};
// If you want to make this class not final, first remove calls to virtual

View File

@ -55,6 +55,7 @@ CompositorOGL::InitializeVR()
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";
@ -66,12 +67,12 @@ CompositorOGL::InitializeVR()
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.xy * aTexCoord0 + uVREyeToSource.zw;\n";
vs << " vTexCoord1 = uVREyeToSource.xy * aTexCoord1 + uVREyeToSource.zw;\n";
vs << " vTexCoord2 = uVREyeToSource.xy * aTexCoord2 + uVREyeToSource.zw;\n";
vs << " vTexCoord0.y = 1.0 - vTexCoord0.y;\n";
vs << " vTexCoord1.y = 1.0 - vTexCoord1.y;\n";
vs << " vTexCoord2.y = 1.0 - vTexCoord2.y;\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";
@ -97,7 +98,6 @@ CompositorOGL::InitializeVR()
}
fs << "uniform " << sampler2D << " uTexture;\n";
fs << "varying vec2 vTexCoord0;\n";
fs << "varying vec2 vTexCoord1;\n";
fs << "varying vec2 vTexCoord2;\n";
@ -179,6 +179,7 @@ CompositorOGL::InitializeVR()
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;
@ -267,6 +268,10 @@ CompositorOGL::DrawVRDistortion(const gfx::Rect& aRect,
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);
@ -293,21 +298,32 @@ CompositorOGL::DrawVRDistortion(const gfx::Rect& aRect,
vpSize, aRect,
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] *= size.width;
texScaleAndOffset[1] *= size.height;
texScaleAndOffset[2] *= size.width;
texScaleAndOffset[3] *= size.height;
height = size.height;
}
gl()->fUniform4fv(mVR.mUVRDestionatinScaleAndOffset[programIndex], 1, shaderConstants.destinationScaleAndOffset);
gl()->fUniform4fv(mVR.mUVREyeToSource[programIndex], 1, shaderConstants.eyeToSourceScaleAndOffset);
gl()->fUniform4fv(mVR.mUVREyeToSource[programIndex], 1, texScaleAndOffset);
gl()->fUniform1f(mVR.mUHeight[programIndex], height);
gl()->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mVR.mDistortionVertices[eye]);
gl()->fVertexAttribPointer(mVR.mAPosition, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex),
(void*) offsetof(gfx::VRDistortionVertex, pos));
gl()->fVertexAttribPointer(mVR.mATexCoord0, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex),
(void*) offsetof(gfx::VRDistortionVertex, texR));
gl()->fVertexAttribPointer(mVR.mATexCoord1, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex),
(void*) offsetof(gfx::VRDistortionVertex, texG));
gl()->fVertexAttribPointer(mVR.mATexCoord2, 2, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex),
(void*) offsetof(gfx::VRDistortionVertex, texB));
gl()->fVertexAttribPointer(mVR.mAGenericAttribs, 4, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, sizeof(gfx::VRDistortionVertex),
(void*) offsetof(gfx::VRDistortionVertex, genericAttribs));
/* 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);

View File

@ -69,11 +69,7 @@ struct VRDistortionConstants {
};
struct VRDistortionVertex {
float pos[2];
float texR[2];
float texG[2];
float texB[2];
float genericAttribs[4];
float values[12];
};
struct VRDistortionMesh {

View File

@ -208,6 +208,9 @@ HMDInfoOculus::HMDInfoOculus(ovrHmd aHMD)
, mHMD(aHMD)
, mStartCount(0)
{
MOZ_ASSERT(sizeof(HMDInfoOculus::DistortionVertex) == sizeof(VRDistortionVertex),
"HMDInfoOculus::DistortionVertex must match the size of VRDistortionVertex");
MOZ_COUNT_CTOR_INHERITED(HMDInfoOculus, VRHMDInfo);
mSupportedSensorBits = 0;
@ -279,7 +282,7 @@ HMDInfoOculus::SetFOV(const VRFieldOfView& aFOVLeft, const VRFieldOfView& aFOVRi
mDistortionMesh[eye].mIndices.SetLength(mesh.IndexCount);
ovrDistortionVertex *srcv = mesh.pVertexData;
VRDistortionVertex *destv = mDistortionMesh[eye].mVertices.Elements();
HMDInfoOculus::DistortionVertex *destv = reinterpret_cast<HMDInfoOculus::DistortionVertex*>(mDistortionMesh[eye].mVertices.Elements());
memset(destv, 0, mesh.VertexCount * sizeof(VRDistortionVertex));
for (uint32_t i = 0; i < mesh.VertexCount; ++i) {
destv[i].pos[0] = srcv[i].ScreenPosNDC.x;

View File

@ -41,6 +41,15 @@ public:
void Destroy();
protected:
// must match the size of VRDistortionVertex
struct DistortionVertex {
float pos[2];
float texR[2];
float texG[2];
float texB[2];
float genericAttribs[4];
};
virtual ~HMDInfoOculus() {
Destroy();
MOZ_COUNT_DTOR_INHERITED(HMDInfoOculus, VRHMDInfo);