From 6eb24bf3742d6851cde2e733c39f42fed83db121 Mon Sep 17 00:00:00 2001 From: Jammy Zhou Date: Mon, 14 Feb 2011 14:38:56 +0800 Subject: [PATCH] improve GLVertexBuffer::render function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add a new function fallbackPainting to GLVertexBufferPrivate class replace the hard-coded index values for vertex and texCoords attributes in corePainting Reviewed by: Martin Gräßlin --- lib/kwinglutils.cpp | 86 +++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/lib/kwinglutils.cpp b/lib/kwinglutils.cpp index 9772d110bf..245acfa6f1 100644 --- a/lib/kwinglutils.cpp +++ b/lib/kwinglutils.cpp @@ -1529,8 +1529,12 @@ public: bool useTexCoords; QColor color; + //! VBO is not supported void legacyPainting(QRegion region, GLenum primitiveMode); + //! VBO and shaders are both supported void corePainting(const QRegion& region, GLenum primitiveMode); + //! VBO is supported, but shaders are not supported + void fallbackPainting(const QRegion& region, GLenum primitiveMode); }; bool GLVertexBufferPrivate::supported = false; GLVertexBuffer *GLVertexBufferPrivate::streamingBuffer = NULL; @@ -1572,15 +1576,15 @@ void GLVertexBufferPrivate::legacyPainting(QRegion region, GLenum primitiveMode) void GLVertexBufferPrivate::corePainting(const QRegion& region, GLenum primitiveMode) { - glEnableVertexAttribArray(0); - if (useTexCoords) { - glEnableVertexAttribArray(1); - } - GLShader *shader = ShaderManager::instance()->getBoundShader(); GLint vertexAttrib = shader->attributeLocation("vertex"); GLint texAttrib = shader->attributeLocation("texCoord"); + glEnableVertexAttribArray(vertexAttrib); + if (useTexCoords) { + glEnableVertexAttribArray(texAttrib); + } + if (useColor) { shader->setUniform("geometryColor", color); } @@ -1606,9 +1610,44 @@ void GLVertexBufferPrivate::corePainting(const QRegion& region, GLenum primitive glBindBuffer(GL_ARRAY_BUFFER, 0); if (useTexCoords) { - glDisableVertexAttribArray(1); + glDisableVertexAttribArray(texAttrib); } - glDisableVertexAttribArray(0); + glDisableVertexAttribArray(vertexAttrib); +} + +void GLVertexBufferPrivate::fallbackPainting(const QRegion& region, GLenum primitiveMode) +{ +#ifdef KWIN_HAVE_OPENGLES + Q_UNUSED(region) + Q_UNUSED(primitiveMode) +#else + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER, buffers[ 0 ]); + glVertexPointer(dimension, GL_FLOAT, 0, 0); + + glBindBuffer(GL_ARRAY_BUFFER, buffers[ 1 ]); + glTexCoordPointer(2, GL_FLOAT, 0, 0); + + if (useColor) { + glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); + } + + // Clip using scissoring + if (region != infiniteRegion()) { + PaintClipper pc(region); + for (PaintClipper::Iterator iterator; !iterator.isDone(); iterator.next()) { + glDrawArrays(primitiveMode, 0, numberVertices); + } + } else { + glDrawArrays(primitiveMode, 0, numberVertices); + } + + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); +#endif } //********************************* @@ -1681,40 +1720,11 @@ void GLVertexBuffer::render(const QRegion& region, GLenum primitiveMode) { if (!GLVertexBufferPrivate::supported) { d->legacyPainting(region, primitiveMode); - return; - } - if (ShaderManager::instance()->isShaderBound()) { + } else if (ShaderManager::instance()->isShaderBound()) { d->corePainting(region, primitiveMode); - return; - } -#ifndef KWIN_HAVE_OPENGLES - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER, d->buffers[ 0 ]); - glVertexPointer(d->dimension, GL_FLOAT, 0, 0); - - glBindBuffer(GL_ARRAY_BUFFER, d->buffers[ 1 ]); - glTexCoordPointer(2, GL_FLOAT, 0, 0); - - if (d->useColor) { - glColor4f(d->color.redF(), d->color.greenF(), d->color.blueF(), d->color.alphaF()); - } - - // Clip using scissoring - if (region != infiniteRegion()) { - PaintClipper pc(region); - for (PaintClipper::Iterator iterator; !iterator.isDone(); iterator.next()) { - glDrawArrays(primitiveMode, 0, d->numberVertices); - } } else { - glDrawArrays(primitiveMode, 0, d->numberVertices); + d->fallbackPainting(region, primitiveMode); } - - glBindBuffer(GL_ARRAY_BUFFER, 0); - - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); -#endif } bool GLVertexBuffer::isSupported()