Bug 1012407 - Part 7: Use a single GL draw call to draw up to 4 texture quads (r=Bas)

This commit is contained in:
Andreas Gal 2014-05-22 00:44:40 +02:00
parent 5ef859e3ca
commit 2886fdd985
5 changed files with 75 additions and 40 deletions

View File

@ -307,11 +307,24 @@ CompositorOGL::Initialize()
mGLContext->fGenBuffers(1, &mQuadVBO); mGLContext->fGenBuffers(1, &mQuadVBO);
mGLContext->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mQuadVBO); mGLContext->fBindBuffer(LOCAL_GL_ARRAY_BUFFER, mQuadVBO);
// 4 quads, with the number of the quad (vertexID) encoded in w.
GLfloat vertices[] = { GLfloat vertices[] = {
/* First quad vertices */ 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
/* Then quad texcoords */ 0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 2.0f,
1.0f, 0.0f, 0.0f, 2.0f,
0.0f, 1.0f, 0.0f, 2.0f,
1.0f, 1.0f, 0.0f, 2.0f,
0.0f, 0.0f, 0.0f, 3.0f,
1.0f, 0.0f, 0.0f, 3.0f,
0.0f, 1.0f, 0.0f, 3.0f,
1.0f, 1.0f, 0.0f, 3.0f,
}; };
HeapCopyOfStackArray<GLfloat> verticesOnHeap(vertices); HeapCopyOfStackArray<GLfloat> verticesOnHeap(vertices);
mGLContext->fBufferData(LOCAL_GL_ARRAY_BUFFER, mGLContext->fBufferData(LOCAL_GL_ARRAY_BUFFER,
@ -516,9 +529,7 @@ CompositorOGL::BindAndDrawQuadWithTextureRect(ShaderProgramOGL *aProg,
aTexCoordRect, aTexCoordRect,
layerRects, layerRects,
textureRects); textureRects);
for (int n = 0; n < rects; ++n) { BindAndDrawQuads(aProg, rects, layerRects, textureRects);
BindAndDrawQuad(aProg, layerRects[n], textureRects[n]);
}
} }
void void
@ -1482,26 +1493,27 @@ CompositorOGL::BindQuadVBO() {
void void
CompositorOGL::QuadVBOVerticesAttrib(GLuint aAttribIndex) { CompositorOGL::QuadVBOVerticesAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2, mGLContext->fVertexAttribPointer(aAttribIndex, 4,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOVertexOffset()); (GLvoid*) 0);
} }
void void
CompositorOGL::QuadVBOTexCoordsAttrib(GLuint aAttribIndex) { CompositorOGL::QuadVBOTexCoordsAttrib(GLuint aAttribIndex) {
mGLContext->fVertexAttribPointer(aAttribIndex, 2, mGLContext->fVertexAttribPointer(aAttribIndex, 4,
LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0, LOCAL_GL_FLOAT, LOCAL_GL_FALSE, 0,
(GLvoid*) QuadVBOTexCoordOffset()); (GLvoid*) 0);
} }
void void
CompositorOGL::BindAndDrawQuad(ShaderProgramOGL *aProg, CompositorOGL::BindAndDrawQuads(ShaderProgramOGL *aProg,
const Rect& aLayerRect, int aQuads,
const Rect& aTextureRect) const Rect* aLayerRects,
const Rect* aTextureRects)
{ {
NS_ASSERTION(aProg->HasInitialized(), "Shader program not correctly initialized"); NS_ASSERTION(aProg->HasInitialized(), "Shader program not correctly initialized");
aProg->SetLayerRect(aLayerRect); aProg->SetLayerRects(aLayerRects);
GLuint vertAttribIndex = aProg->AttribLocation(ShaderProgramOGL::VertexCoordAttrib); GLuint vertAttribIndex = aProg->AttribLocation(ShaderProgramOGL::VertexCoordAttrib);
GLuint texCoordAttribIndex = aProg->AttribLocation(ShaderProgramOGL::TexCoordAttrib); GLuint texCoordAttribIndex = aProg->AttribLocation(ShaderProgramOGL::TexCoordAttrib);
@ -1513,11 +1525,11 @@ CompositorOGL::BindAndDrawQuad(ShaderProgramOGL *aProg,
QuadVBOTexCoordsAttrib(texCoordAttribIndex); QuadVBOTexCoordsAttrib(texCoordAttribIndex);
mGLContext->fEnableVertexAttribArray(texCoordAttribIndex); mGLContext->fEnableVertexAttribArray(texCoordAttribIndex);
aProg->SetTextureRect(aTextureRect); aProg->SetTextureRects(aTextureRects);
} }
mGLContext->fEnableVertexAttribArray(vertAttribIndex); mGLContext->fEnableVertexAttribArray(vertAttribIndex);
mGLContext->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4); mGLContext->fDrawArrays(LOCAL_GL_TRIANGLE_STRIP, 0, 4 * aQuads);
} }
GLuint GLuint

View File

@ -362,15 +362,22 @@ private:
GLuint aSourceFrameBuffer, GLuint aSourceFrameBuffer,
GLuint *aFBO, GLuint *aTexture); GLuint *aFBO, GLuint *aTexture);
GLintptr QuadVBOVertexOffset() { return 0; }
GLintptr QuadVBOTexCoordOffset() { return sizeof(float)*4*2; }
void BindQuadVBO(); void BindQuadVBO();
void QuadVBOVerticesAttrib(GLuint aAttribIndex); void QuadVBOVerticesAttrib(GLuint aAttribIndex);
void QuadVBOTexCoordsAttrib(GLuint aAttribIndex); void QuadVBOTexCoordsAttrib(GLuint aAttribIndex);
void BindAndDrawQuads(ShaderProgramOGL *aProg,
int aQuads,
const gfx::Rect* aLayerRect,
const gfx::Rect* aTextureRect);
void BindAndDrawQuad(ShaderProgramOGL *aProg, void BindAndDrawQuad(ShaderProgramOGL *aProg,
const gfx::Rect& aLayerRect, const gfx::Rect& aLayerRect,
const gfx::Rect& aTextureRect = gfx::Rect(0.0f, 0.0f, 1.0f, 1.0f)); const gfx::Rect& aTextureRect = gfx::Rect(0.0f, 0.0f, 1.0f, 1.0f)) {
gfx::Rect layerRects[4];
gfx::Rect textureRects[4];
layerRects[0] = aLayerRect;
textureRects[0] = aTextureRect;
BindAndDrawQuads(aProg, 1, layerRects, textureRects);
}
void BindAndDrawQuadWithTextureRect(ShaderProgramOGL *aProg, void BindAndDrawQuadWithTextureRect(ShaderProgramOGL *aProg,
const gfx::Rect& aRect, const gfx::Rect& aRect,
const gfx::Rect& aTexCoordRect, const gfx::Rect& aTexCoordRect,

View File

@ -32,10 +32,10 @@ AddUniforms(ProgramProfileOGL& aProfile)
static const char *sKnownUniformNames[] = { static const char *sKnownUniformNames[] = {
"uLayerTransform", "uLayerTransform",
"uMaskTransform", "uMaskTransform",
"uLayerRect", "uLayerRects",
"uMatrixProj", "uMatrixProj",
"uTextureTransform", "uTextureTransform",
"uTextureRect", "uTextureRects",
"uRenderTargetOffset", "uRenderTargetOffset",
"uLayerOpacity", "uLayerOpacity",
"uTexture", "uTexture",
@ -146,7 +146,7 @@ ProgramProfileOGL::GetProfileFor(ShaderConfigOGL aConfig)
AddUniforms(result); AddUniforms(result);
vs << "uniform mat4 uMatrixProj;" << endl; vs << "uniform mat4 uMatrixProj;" << endl;
vs << "uniform vec4 uLayerRect;" << endl; vs << "uniform vec4 uLayerRects[4];" << endl;
vs << "uniform mat4 uLayerTransform;" << endl; vs << "uniform mat4 uLayerTransform;" << endl;
vs << "uniform vec4 uRenderTargetOffset;" << endl; vs << "uniform vec4 uRenderTargetOffset;" << endl;
@ -154,8 +154,8 @@ ProgramProfileOGL::GetProfileFor(ShaderConfigOGL aConfig)
if (!(aConfig.mFeatures & ENABLE_RENDER_COLOR)) { if (!(aConfig.mFeatures & ENABLE_RENDER_COLOR)) {
vs << "uniform mat4 uTextureTransform;" << endl; vs << "uniform mat4 uTextureTransform;" << endl;
vs << "uniform vec4 uTextureRect;" << endl; vs << "uniform vec4 uTextureRects[4];" << endl;
vs << "attribute vec2 aTexCoord;" << endl; vs << "attribute vec4 aTexCoord;" << endl;
vs << "varying vec2 vTexCoord;" << endl; vs << "varying vec2 vTexCoord;" << endl;
} }
@ -166,7 +166,9 @@ ProgramProfileOGL::GetProfileFor(ShaderConfigOGL aConfig)
} }
vs << "void main() {" << endl; vs << "void main() {" << endl;
vs << " vec4 finalPosition = vec4(aVertexCoord.xy * uLayerRect.zw + uLayerRect.xy, 0.0, 1.0);" << endl; vs << " int vertexID = int(aVertexCoord.w);" << endl;
vs << " vec4 layerRect = uLayerRects[vertexID];" << endl;
vs << " vec4 finalPosition = vec4(aVertexCoord.xy * layerRect.zw + layerRect.xy, 0.0, 1.0);" << endl;
vs << " finalPosition = uLayerTransform * finalPosition;" << endl; vs << " finalPosition = uLayerTransform * finalPosition;" << endl;
vs << " finalPosition.xyz /= finalPosition.w;" << endl; vs << " finalPosition.xyz /= finalPosition.w;" << endl;
@ -184,7 +186,8 @@ ProgramProfileOGL::GetProfileFor(ShaderConfigOGL aConfig)
vs << " finalPosition = uMatrixProj * finalPosition;" << endl; vs << " finalPosition = uMatrixProj * finalPosition;" << endl;
if (!(aConfig.mFeatures & ENABLE_RENDER_COLOR)) { if (!(aConfig.mFeatures & ENABLE_RENDER_COLOR)) {
vs << " vec2 texCoord = aTexCoord * uTextureRect.zw + uTextureRect.xy;" << endl; vs << " vec4 textureRect = uTextureRects[vertexID];" << endl;
vs << " vec2 texCoord = aTexCoord.xy * textureRect.zw + textureRect.xy;" << endl;
vs << " vTexCoord = (uTextureTransform * vec4(texCoord, 0.0, 1.0)).xy;" << endl; vs << " vTexCoord = (uTextureTransform * vec4(texCoord, 0.0, 1.0)).xy;" << endl;
} }

View File

@ -52,10 +52,10 @@ public:
LayerTransform = 0, LayerTransform = 0,
MaskTransform, MaskTransform,
LayerRect, LayerRects,
MatrixProj, MatrixProj,
TextureTransform, TextureTransform,
TextureRect, TextureRects,
RenderTargetOffset, RenderTargetOffset,
LayerOpacity, LayerOpacity,
Texture, Texture,
@ -322,9 +322,12 @@ public:
SetMatrixUniform(KnownUniform::MaskTransform, aMatrix); SetMatrixUniform(KnownUniform::MaskTransform, aMatrix);
} }
void SetLayerRect(const gfx::Rect& aRect) { void SetLayerRects(const gfx::Rect* aRects) {
float vals[4] = { float(aRect.x), float(aRect.y), float(aRect.width), float(aRect.height) }; float vals[16] = { aRects[0].x, aRects[0].y, aRects[0].width, aRects[0].height,
SetUniform(KnownUniform::LayerRect, 4, vals); aRects[1].x, aRects[1].y, aRects[1].width, aRects[1].height,
aRects[2].x, aRects[2].y, aRects[2].width, aRects[2].height,
aRects[3].x, aRects[3].y, aRects[3].width, aRects[3].height };
SetUniform(KnownUniform::LayerRects, 16, vals);
} }
void SetProjectionMatrix(const gfx::Matrix4x4& aMatrix) { void SetProjectionMatrix(const gfx::Matrix4x4& aMatrix) {
@ -336,9 +339,12 @@ public:
SetMatrixUniform(KnownUniform::TextureTransform, aMatrix); SetMatrixUniform(KnownUniform::TextureTransform, aMatrix);
} }
void SetTextureRect(const gfx::Rect& aRect) { void SetTextureRects(const gfx::Rect* aRects) {
float vals[4] = { float(aRect.x), float(aRect.y), float(aRect.width), float(aRect.height) }; float vals[16] = { aRects[0].x, aRects[0].y, aRects[0].width, aRects[0].height,
SetUniform(KnownUniform::TextureRect, 4, vals); aRects[1].x, aRects[1].y, aRects[1].width, aRects[1].height,
aRects[2].x, aRects[2].y, aRects[2].width, aRects[2].height,
aRects[3].x, aRects[3].y, aRects[3].width, aRects[3].height };
SetUniform(KnownUniform::TextureRects, 16, vals);
} }
void SetRenderOffset(const nsIntPoint& aOffset) { void SetRenderOffset(const nsIntPoint& aOffset) {
@ -471,6 +477,7 @@ protected:
case 2: mGL->fUniform2fv(ku.mLocation, 1, ku.mValue.f16v); break; case 2: mGL->fUniform2fv(ku.mLocation, 1, ku.mValue.f16v); break;
case 3: mGL->fUniform3fv(ku.mLocation, 1, ku.mValue.f16v); break; case 3: mGL->fUniform3fv(ku.mLocation, 1, ku.mValue.f16v); break;
case 4: mGL->fUniform4fv(ku.mLocation, 1, ku.mValue.f16v); break; case 4: mGL->fUniform4fv(ku.mLocation, 1, ku.mValue.f16v); break;
case 16: mGL->fUniform4fv(ku.mLocation, 4, ku.mValue.f16v); break;
default: default:
NS_NOTREACHED("Bogus aLength param"); NS_NOTREACHED("Bogus aLength param");
} }

View File

@ -2810,8 +2810,14 @@ GLPresenter::BindAndDrawQuad(ShaderProgramOGL *aProgram,
{ {
mGLContext->MakeCurrent(); mGLContext->MakeCurrent();
aProgram->SetLayerRect(aLayerRect); gfx::Rect layerRects[4];
aProgram->SetTextureRect(aTextureRect); gfx::Rect textureRects[4];
layerRects[0] = aLayerRect;
textureRects[0] = aTextureRect;
aProgram->SetLayerRects(layerRects);
aProgram->SetTextureRects(textureRects);
GLuint vertAttribIndex = aProgram->AttribLocation(ShaderProgramOGL::VertexCoordAttrib); GLuint vertAttribIndex = aProgram->AttribLocation(ShaderProgramOGL::VertexCoordAttrib);
GLuint texCoordAttribIndex = aProgram->AttribLocation(ShaderProgramOGL::TexCoordAttrib); GLuint texCoordAttribIndex = aProgram->AttribLocation(ShaderProgramOGL::TexCoordAttrib);