2007-11-27 19:40:25 +00:00
|
|
|
/********************************************************************
|
2007-04-29 17:35:43 +00:00
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2006-2007 Rivo Laks <rivolaks@hot.ee>
|
2011-03-16 18:39:10 +00:00
|
|
|
Copyright (C) 2010, 2011 Martin Gräßlin <mgraesslin@kde.org>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2007-11-27 19:40:25 +00:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include "kwinglutils.h"
|
|
|
|
|
2011-07-18 15:55:39 +00:00
|
|
|
// need to call GLTexturePrivate::initStatic()
|
|
|
|
#include "kwingltexture_p.h"
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
#include "kwinglobals.h"
|
|
|
|
#include "kwineffects.h"
|
2011-02-05 11:06:32 +00:00
|
|
|
#include "kwinglplatform.h"
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include "kdebug.h"
|
2007-07-04 20:33:35 +00:00
|
|
|
#include <kstandarddirs.h>
|
2011-04-29 14:41:11 +00:00
|
|
|
#include <KDE/KConfig>
|
|
|
|
#include <KDE/KConfigGroup>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QHash>
|
|
|
|
#include <QFile>
|
2010-11-14 19:49:00 +00:00
|
|
|
#include <QVector2D>
|
|
|
|
#include <QVector3D>
|
|
|
|
#include <QVector4D>
|
2010-12-04 11:25:49 +00:00
|
|
|
#include <QMatrix4x4>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
#include <math.h>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-06-08 20:29:17 +00:00
|
|
|
#define DEBUG_GLRENDERTARGET 0
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
#define MAKE_GL_VERSION(major, minor, release) ( ((major) << 16) | ((minor) << 8) | (release) )
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
// Variables
|
|
|
|
// GL version, use MAKE_GL_VERSION() macro for comparing with a specific version
|
|
|
|
static int glVersion;
|
|
|
|
// GLX version, use MAKE_GL_VERSION() macro for comparing with a specific version
|
|
|
|
static int glXVersion;
|
2010-12-05 10:55:19 +00:00
|
|
|
// EGL version, use MAKE_GL_VERSION() macro for comparing with a specific version
|
|
|
|
static int eglVersion;
|
|
|
|
// List of all supported GL, EGL and GLX extensions
|
2007-04-29 17:35:43 +00:00
|
|
|
static QStringList glExtensions;
|
2007-05-30 13:55:29 +00:00
|
|
|
static QStringList glxExtensions;
|
2010-12-05 10:55:19 +00:00
|
|
|
static QStringList eglExtension;
|
2011-04-29 14:41:11 +00:00
|
|
|
static bool legacyGl;
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
int glTextureUnitsCount;
|
|
|
|
|
|
|
|
|
|
|
|
// Functions
|
|
|
|
void initGLX()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-11-20 09:27:06 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2007-04-29 17:35:43 +00:00
|
|
|
// Get GLX version
|
|
|
|
int major, minor;
|
2011-01-30 14:34:42 +00:00
|
|
|
glXQueryVersion(display(), &major, &minor);
|
|
|
|
glXVersion = MAKE_GL_VERSION(major, minor, 0);
|
2007-05-30 13:55:29 +00:00
|
|
|
// Get list of supported GLX extensions
|
|
|
|
glxExtensions = QString((const char*)glXQueryExtensionsString(
|
2011-01-30 14:34:42 +00:00
|
|
|
display(), DefaultScreen(display()))).split(' ');
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
glxResolveFunctions();
|
2010-11-20 09:27:06 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-12-05 10:55:19 +00:00
|
|
|
void initEGL()
|
|
|
|
{
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
EGLDisplay dpy = eglGetCurrentDisplay();
|
|
|
|
int major, minor;
|
|
|
|
eglInitialize(dpy, &major, &minor);
|
|
|
|
eglVersion = MAKE_GL_VERSION(major, minor, 0);
|
|
|
|
eglExtension = QString((const char*)eglQueryString(dpy, EGL_EXTENSIONS)).split(' ');
|
2010-12-05 11:35:40 +00:00
|
|
|
|
|
|
|
eglResolveFunctions();
|
2010-12-05 10:55:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
void initGL()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
// Get OpenGL version
|
|
|
|
QString glversionstring = QString((const char*)glGetString(GL_VERSION));
|
|
|
|
QStringList glversioninfo = glversionstring.left(glversionstring.indexOf(' ')).split('.');
|
2011-02-02 15:39:45 +00:00
|
|
|
while (glversioninfo.count() < 3)
|
|
|
|
glversioninfo << "0";
|
2011-04-29 14:41:11 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
legacyGl = false;
|
|
|
|
#else
|
|
|
|
KSharedConfig::Ptr kwinconfig = KSharedConfig::openConfig("kwinrc", KConfig::NoGlobals);
|
|
|
|
KConfigGroup config(kwinconfig, "Compositing");
|
|
|
|
legacyGl = config.readEntry<bool>("GLLegacy", false);
|
2011-02-02 15:39:45 +00:00
|
|
|
glVersion = MAKE_GL_VERSION(glversioninfo[0].toInt(), glversioninfo[1].toInt(), glversioninfo[2].toInt());
|
2010-11-28 15:16:56 +00:00
|
|
|
#endif
|
2007-04-29 17:35:43 +00:00
|
|
|
// Get list of supported OpenGL extensions
|
2009-10-30 06:51:59 +00:00
|
|
|
glExtensions = QString((const char*)glGetString(GL_EXTENSIONS)).split(' ');
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
// handle OpenGL extensions functions
|
|
|
|
glResolveFunctions();
|
|
|
|
|
2011-07-18 15:55:39 +00:00
|
|
|
GLTexturePrivate::initStatic();
|
2007-04-29 17:35:43 +00:00
|
|
|
GLRenderTarget::initStatic();
|
2010-07-19 20:53:32 +00:00
|
|
|
GLVertexBuffer::initStatic();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-04-27 12:51:36 +00:00
|
|
|
void cleanupGL()
|
|
|
|
{
|
|
|
|
ShaderManager::cleanup();
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
bool hasGLVersion(int major, int minor, int release)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
return glVersion >= MAKE_GL_VERSION(major, minor, release);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
bool hasGLXVersion(int major, int minor, int release)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
return glXVersion >= MAKE_GL_VERSION(major, minor, release);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-12-05 10:55:19 +00:00
|
|
|
bool hasEGLVersion(int major, int minor, int release)
|
|
|
|
{
|
|
|
|
return eglVersion >= MAKE_GL_VERSION(major, minor, release);
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
bool hasGLExtension(const QString& extension)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-12-05 10:55:19 +00:00
|
|
|
return glExtensions.contains(extension) || glxExtensions.contains(extension) || eglExtension.contains(extension);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static QString formatGLError(GLenum err)
|
|
|
|
{
|
|
|
|
switch(err) {
|
|
|
|
case GL_NO_ERROR: return "GL_NO_ERROR";
|
|
|
|
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
|
|
|
|
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
|
|
|
|
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
|
2010-11-20 09:27:06 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
|
|
|
|
case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
|
2010-11-20 09:27:06 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
|
|
|
|
default: return QString("0x") + QString::number(err, 16);
|
2010-06-08 20:29:17 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-08 20:29:17 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
bool checkGLError(const char* txt)
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
GLenum err = glGetError();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (err != GL_NO_ERROR) {
|
|
|
|
kWarning(1212) << "GL error (" << txt << "): " << formatGLError(err);
|
2008-01-16 12:44:45 +00:00
|
|
|
return true;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
int nearestPowerOfTwo(int x)
|
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
// This method had been copied from Qt's nearest_gl_texture_size()
|
|
|
|
int n = 0, last = 0;
|
|
|
|
for (int s = 0; s < 32; ++s) {
|
2011-01-30 14:34:42 +00:00
|
|
|
if (((x >> s) & 1) == 1) {
|
2007-04-29 17:35:43 +00:00
|
|
|
++n;
|
|
|
|
last = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > 1)
|
2011-01-30 14:34:42 +00:00
|
|
|
return 1 << (last + 1);
|
2007-04-29 17:35:43 +00:00
|
|
|
return 1 << last;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-01 17:36:40 +00:00
|
|
|
void pushMatrix()
|
|
|
|
{
|
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
|
|
|
glPushMatrix();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void pushMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(matrix)
|
|
|
|
#else
|
|
|
|
glPushMatrix();
|
|
|
|
multiplyMatrix(matrix);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void multiplyMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(matrix)
|
|
|
|
#else
|
|
|
|
GLfloat m[16];
|
|
|
|
const qreal *data = matrix.constData();
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int j = 0; j < 4; ++j) {
|
2011-01-01 17:36:40 +00:00
|
|
|
m[i*4+j] = data[i*4+j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glMultMatrixf(m);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-01 17:55:51 +00:00
|
|
|
void loadMatrix(const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(matrix)
|
|
|
|
#else
|
|
|
|
GLfloat m[16];
|
|
|
|
const qreal *data = matrix.constData();
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int j = 0; j < 4; ++j) {
|
2011-01-01 17:55:51 +00:00
|
|
|
m[i*4+j] = data[i*4+j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glLoadMatrixf(m);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-01 17:36:40 +00:00
|
|
|
void popMatrix()
|
|
|
|
{
|
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
|
|
|
glPopMatrix();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
//****************************************
|
|
|
|
// GLShader
|
|
|
|
//****************************************
|
|
|
|
|
2011-01-08 18:53:11 +00:00
|
|
|
GLShader::GLShader()
|
2011-02-04 16:06:28 +00:00
|
|
|
: mProgram(0)
|
|
|
|
, mValid(false)
|
2011-02-10 18:16:04 +00:00
|
|
|
, mLocationsResolved(false)
|
2011-01-08 18:53:11 +00:00
|
|
|
{
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
GLShader::GLShader(const QString& vertexfile, const QString& fragmentfile)
|
2011-02-12 00:20:20 +00:00
|
|
|
: mProgram(0)
|
|
|
|
, mValid(false)
|
|
|
|
, mLocationsResolved(false)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
loadFromFiles(vertexfile, fragmentfile);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
GLShader::~GLShader()
|
|
|
|
{
|
2011-01-30 14:34:42 +00:00
|
|
|
if (mProgram) {
|
2007-04-29 17:35:43 +00:00
|
|
|
glDeleteProgram(mProgram);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-04 16:03:56 +00:00
|
|
|
bool GLShader::loadFromFiles(const QString &vertexFile, const QString &fragmentFile)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-02-04 16:03:56 +00:00
|
|
|
QFile vf(vertexFile);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!vf.open(QIODevice::ReadOnly)) {
|
2011-02-04 16:03:56 +00:00
|
|
|
kError(1212) << "Couldn't open" << vertexFile << "for reading!" << endl;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-02-04 15:41:55 +00:00
|
|
|
const QByteArray vertexSource = vf.readAll();
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 16:03:56 +00:00
|
|
|
QFile ff(fragmentFile);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!ff.open(QIODevice::ReadOnly)) {
|
2011-02-04 16:03:56 +00:00
|
|
|
kError(1212) << "Couldn't open" << fragmentFile << "for reading!" << endl;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-02-04 15:41:55 +00:00
|
|
|
const QByteArray fragmentSource = ff.readAll();
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 15:41:55 +00:00
|
|
|
return load(vertexSource, fragmentSource);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 16:03:56 +00:00
|
|
|
bool GLShader::compile(GLuint program, GLenum shaderType, const QByteArray &source) const
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(shaderType);
|
|
|
|
|
|
|
|
// Prepare the source code
|
|
|
|
QByteArray ba;
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
ba.append("#ifdef GL_ES\nprecision highp float;\n#endif\n");
|
|
|
|
#endif
|
2011-07-23 16:57:50 +00:00
|
|
|
if (ShaderManager::instance()->isShaderDebug()) {
|
|
|
|
ba.append("#define KWIN_SHADER_DEBUG 1\n");
|
|
|
|
}
|
2011-02-04 16:03:56 +00:00
|
|
|
ba.append(source);
|
|
|
|
|
|
|
|
const char* src = ba.constData();
|
|
|
|
glShaderSource(shader, 1, &src, NULL);
|
|
|
|
|
|
|
|
// Compile the shader
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
// Get the shader info log
|
|
|
|
int maxLength, length;
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
|
|
|
|
QByteArray log(maxLength, 0);
|
|
|
|
glGetShaderInfoLog(shader, maxLength, &length, log.data());
|
|
|
|
|
|
|
|
// Check the status
|
|
|
|
int status;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
|
|
|
|
|
|
|
if (status == 0) {
|
|
|
|
const char *typeName = (shaderType == GL_VERTEX_SHADER ? "vertex" : "fragment");
|
|
|
|
kError(1212) << "Failed to compile" << typeName << "shader:" << endl << log << endl;
|
|
|
|
} else if (length > 0)
|
|
|
|
kDebug(1212) << "Shader compile log:" << log;
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
glAttachShader(program, shader);
|
|
|
|
|
|
|
|
glDeleteShader(shader);
|
|
|
|
return status != 0;
|
|
|
|
}
|
|
|
|
|
2011-02-04 15:41:55 +00:00
|
|
|
bool GLShader::load(const QByteArray &vertexSource, const QByteArray &fragmentSource)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-08-19 19:30:45 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2007-04-29 17:35:43 +00:00
|
|
|
// Make sure shaders are actually supported
|
2011-06-01 16:21:19 +00:00
|
|
|
if (!GLPlatform::instance()->supports(GLSL) || GLPlatform::instance()->supports(LimitedGLSL)) {
|
2011-02-04 16:03:56 +00:00
|
|
|
kError(1212) << "Shaders are not supported";
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-08-19 19:30:45 +00:00
|
|
|
#endif
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 16:03:56 +00:00
|
|
|
// Create the shader program
|
2007-04-29 17:35:43 +00:00
|
|
|
mProgram = glCreateProgram();
|
2011-02-04 16:03:56 +00:00
|
|
|
|
|
|
|
// Compile the vertex shader
|
2011-02-04 15:41:55 +00:00
|
|
|
if (!vertexSource.isEmpty()) {
|
2011-02-04 16:03:56 +00:00
|
|
|
bool success = compile(mProgram, GL_VERTEX_SHADER, vertexSource);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
glDeleteProgram(mProgram);
|
|
|
|
mProgram = 0;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-02-04 16:03:56 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 16:03:56 +00:00
|
|
|
// Compile the fragment shader
|
2011-02-04 15:41:55 +00:00
|
|
|
if (!fragmentSource.isEmpty()) {
|
2011-02-04 16:03:56 +00:00
|
|
|
bool success = compile(mProgram, GL_FRAGMENT_SHADER, fragmentSource);
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
glDeleteProgram(mProgram);
|
|
|
|
mProgram = 0;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-02-04 16:03:56 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
glLinkProgram(mProgram);
|
2011-02-04 16:03:56 +00:00
|
|
|
|
|
|
|
// Get the program info log
|
|
|
|
int maxLength, length;
|
|
|
|
glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &maxLength);
|
|
|
|
|
|
|
|
QByteArray log(maxLength, 0);
|
|
|
|
glGetProgramInfoLog(mProgram, maxLength, &length, log.data());
|
|
|
|
|
|
|
|
// Make sure the program linked successfully
|
|
|
|
int status;
|
|
|
|
glGetProgramiv(mProgram, GL_LINK_STATUS, &status);
|
|
|
|
|
|
|
|
if (status == 0) {
|
|
|
|
kError(1212) << "Failed to link shader:" << endl << log << endl;
|
|
|
|
glDeleteProgram(mProgram);
|
|
|
|
mProgram = 0;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-02-04 16:03:56 +00:00
|
|
|
} else if (length > 0)
|
|
|
|
kDebug(1212) << "Shader link log:" << log;
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
mValid = true;
|
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void GLShader::bind()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
glUseProgram(mProgram);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void GLShader::unbind()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
glUseProgram(0);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
void GLShader::resolveLocations()
|
|
|
|
{
|
|
|
|
if (mLocationsResolved)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mMatrixLocation[TextureMatrix] = uniformLocation("textureMatrix");
|
|
|
|
mMatrixLocation[ProjectionMatrix] = uniformLocation("projection");
|
|
|
|
mMatrixLocation[ModelViewMatrix] = uniformLocation("modelview");
|
|
|
|
mMatrixLocation[WindowTransformation] = uniformLocation("windowTransformation");
|
|
|
|
mMatrixLocation[ScreenTransformation] = uniformLocation("screenTransformation");
|
|
|
|
|
|
|
|
mVec2Location[Offset] = uniformLocation("offset");
|
|
|
|
|
2011-02-12 00:36:21 +00:00
|
|
|
mVec4Location[ModulationConstant] = uniformLocation("modulation");
|
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
mFloatLocation[Saturation] = uniformLocation("saturation");
|
|
|
|
|
|
|
|
mIntLocation[AlphaToOne] = uniformLocation("u_forceAlpha");
|
|
|
|
|
|
|
|
mLocationsResolved = true;
|
|
|
|
}
|
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
int GLShader::uniformLocation(const char *name)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-02-10 18:16:04 +00:00
|
|
|
const int location = glGetUniformLocation(mProgram, name);
|
2009-08-06 13:57:09 +00:00
|
|
|
return location;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
bool GLShader::setUniform(GLShader::MatrixUniform uniform, const QMatrix4x4 &matrix)
|
|
|
|
{
|
|
|
|
resolveLocations();
|
|
|
|
return setUniform(mMatrixLocation[uniform], matrix);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(GLShader::Vec2Uniform uniform, const QVector2D &value)
|
|
|
|
{
|
|
|
|
resolveLocations();
|
|
|
|
return setUniform(mVec2Location[uniform], value);
|
|
|
|
}
|
|
|
|
|
2011-02-12 00:36:21 +00:00
|
|
|
bool GLShader::setUniform(GLShader::Vec4Uniform uniform, const QVector4D &value)
|
|
|
|
{
|
|
|
|
resolveLocations();
|
|
|
|
return setUniform(mVec4Location[uniform], value);
|
|
|
|
}
|
|
|
|
|
2011-02-10 18:16:04 +00:00
|
|
|
bool GLShader::setUniform(GLShader::FloatUniform uniform, float value)
|
|
|
|
{
|
|
|
|
resolveLocations();
|
|
|
|
return setUniform(mFloatLocation[uniform], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(GLShader::IntUniform uniform, int value)
|
|
|
|
{
|
|
|
|
resolveLocations();
|
|
|
|
return setUniform(mIntLocation[uniform], value);
|
|
|
|
}
|
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(const char *name, float value)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const char *name, int value)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const char *name, const QVector2D& value)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const char *name, const QVector3D& value)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const char *name, const QVector4D& value)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const char *name, const QMatrix4x4& value)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const char *name, const QColor& color)
|
|
|
|
{
|
|
|
|
const int location = uniformLocation(name);
|
|
|
|
return setUniform(location, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(int location, float value)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
2007-04-29 17:35:43 +00:00
|
|
|
glUniform1f(location, value);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return (location >= 0);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(int location, int value)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
2007-04-29 17:35:43 +00:00
|
|
|
glUniform1i(location, value);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return (location >= 0);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(int location, const QVector2D &value)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
2011-02-04 18:57:19 +00:00
|
|
|
glUniform2fv(location, 1, (const GLfloat*)&value);
|
2010-11-14 19:49:00 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return (location >= 0);
|
|
|
|
}
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(int location, const QVector3D &value)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
2011-02-04 18:57:19 +00:00
|
|
|
glUniform3fv(location, 1, (const GLfloat*)&value);
|
2010-11-14 19:49:00 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return (location >= 0);
|
|
|
|
}
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(int location, const QVector4D &value)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
2011-02-04 18:57:19 +00:00
|
|
|
glUniform4fv(location, 1, (const GLfloat*)&value);
|
2010-11-14 19:49:00 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return (location >= 0);
|
|
|
|
}
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(int location, const QMatrix4x4 &value)
|
2010-12-04 11:25:49 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
|
|
|
GLfloat m[16];
|
|
|
|
const qreal *data = value.constData();
|
|
|
|
// i is column, j is row for m
|
2011-03-06 08:10:21 +00:00
|
|
|
for (int i = 0; i < 16; ++i) {
|
|
|
|
m[i] = data[i];
|
2010-12-04 11:25:49 +00:00
|
|
|
}
|
|
|
|
glUniformMatrix4fv(location, 1, GL_FALSE, m);
|
|
|
|
}
|
|
|
|
return (location >= 0);
|
|
|
|
}
|
|
|
|
|
2011-02-04 18:57:19 +00:00
|
|
|
bool GLShader::setUniform(int location, const QColor &color)
|
2010-12-11 10:10:33 +00:00
|
|
|
{
|
|
|
|
if (location >= 0) {
|
|
|
|
glUniform4f(location, color.redF(), color.greenF(), color.blueF(), color.alphaF());
|
|
|
|
}
|
|
|
|
return (location >= 0);
|
|
|
|
}
|
|
|
|
|
2009-08-06 13:57:09 +00:00
|
|
|
int GLShader::attributeLocation(const char* name)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2009-08-06 13:57:09 +00:00
|
|
|
int location = glGetAttribLocation(mProgram, name);
|
|
|
|
return location;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2009-08-06 13:57:09 +00:00
|
|
|
bool GLShader::setAttribute(const char* name, float value)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
int location = attributeLocation(name);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (location >= 0) {
|
2007-04-29 17:35:43 +00:00
|
|
|
glVertexAttrib1f(location, value);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return (location >= 0);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-12-18 07:45:47 +00:00
|
|
|
QMatrix4x4 GLShader::getUniformMatrix4x4(const char* name)
|
|
|
|
{
|
|
|
|
int location = uniformLocation(name);
|
|
|
|
if (location >= 0) {
|
|
|
|
GLfloat m[16];
|
|
|
|
glGetUniformfv(mProgram, location, m);
|
2011-03-06 08:10:21 +00:00
|
|
|
QMatrix4x4 matrix(m[0], m[4], m[8], m[12],
|
|
|
|
m[1], m[5], m[9], m[13],
|
|
|
|
m[2], m[6], m[10], m[14],
|
|
|
|
m[3], m[7], m[11], m[15]);
|
2010-12-18 07:45:47 +00:00
|
|
|
matrix.optimize();
|
|
|
|
return matrix;
|
|
|
|
} else {
|
|
|
|
return QMatrix4x4();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
//****************************************
|
|
|
|
// ShaderManager
|
|
|
|
//****************************************
|
|
|
|
ShaderManager *ShaderManager::s_shaderManager = NULL;
|
|
|
|
|
|
|
|
ShaderManager *ShaderManager::instance()
|
|
|
|
{
|
|
|
|
if (!s_shaderManager) {
|
|
|
|
s_shaderManager = new ShaderManager();
|
2011-07-23 19:32:12 +00:00
|
|
|
s_shaderManager->initShaders();
|
|
|
|
s_shaderManager->m_inited = true;
|
2010-12-11 09:25:46 +00:00
|
|
|
}
|
|
|
|
return s_shaderManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderManager::cleanup()
|
|
|
|
{
|
|
|
|
delete s_shaderManager;
|
2011-04-27 12:51:36 +00:00
|
|
|
s_shaderManager = NULL;
|
2010-12-11 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderManager::ShaderManager()
|
|
|
|
: m_orthoShader(NULL)
|
|
|
|
, m_genericShader(NULL)
|
|
|
|
, m_colorShader(NULL)
|
|
|
|
, m_inited(false)
|
|
|
|
, m_valid(false)
|
|
|
|
{
|
2011-07-23 16:57:50 +00:00
|
|
|
m_debug = qstrcmp(qgetenv("KWIN_GL_DEBUG"), "1") == 0;
|
2010-12-11 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderManager::~ShaderManager()
|
|
|
|
{
|
|
|
|
while (!m_boundShaders.isEmpty()) {
|
|
|
|
popShader();
|
|
|
|
}
|
|
|
|
delete m_orthoShader;
|
|
|
|
delete m_genericShader;
|
|
|
|
delete m_colorShader;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLShader *ShaderManager::getBoundShader() const
|
|
|
|
{
|
|
|
|
if (m_boundShaders.isEmpty()) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return m_boundShaders.top();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderManager::isShaderBound() const
|
|
|
|
{
|
|
|
|
return !m_boundShaders.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderManager::isValid() const
|
|
|
|
{
|
|
|
|
return m_valid;
|
|
|
|
}
|
|
|
|
|
2011-07-23 16:57:50 +00:00
|
|
|
bool ShaderManager::isShaderDebug() const
|
|
|
|
{
|
|
|
|
return m_debug;
|
|
|
|
}
|
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
GLShader *ShaderManager::pushShader(ShaderType type, bool reset)
|
|
|
|
{
|
|
|
|
if (m_inited && !m_valid) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
GLShader *shader;
|
2011-01-30 14:34:42 +00:00
|
|
|
switch(type) {
|
2010-12-11 09:25:46 +00:00
|
|
|
case SimpleShader:
|
|
|
|
shader = m_orthoShader;
|
|
|
|
break;
|
|
|
|
case GenericShader:
|
|
|
|
shader = m_genericShader;
|
|
|
|
break;
|
|
|
|
case ColorShader:
|
|
|
|
shader = m_colorShader;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pushShader(shader);
|
|
|
|
if (reset) {
|
|
|
|
resetShader(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderManager::pushShader(GLShader *shader)
|
|
|
|
{
|
|
|
|
// only bind shader if it is not already bound
|
|
|
|
if (shader != getBoundShader()) {
|
|
|
|
shader->bind();
|
|
|
|
}
|
|
|
|
m_boundShaders.push(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderManager::popShader()
|
|
|
|
{
|
|
|
|
if (m_boundShaders.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GLShader *shader = m_boundShaders.pop();
|
|
|
|
if (m_boundShaders.isEmpty()) {
|
|
|
|
// no more shader bound - unbind
|
|
|
|
shader->unbind();
|
|
|
|
} else if (shader != m_boundShaders.top()) {
|
|
|
|
// only rebind if a different shader is on top of stack
|
|
|
|
m_boundShaders.top()->bind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-19 13:38:01 +00:00
|
|
|
GLShader *ShaderManager::loadFragmentShader(ShaderType vertex, const QString &fragmentFile)
|
|
|
|
{
|
|
|
|
QString vertexShader;
|
2011-01-30 14:34:42 +00:00
|
|
|
switch(vertex) {
|
2010-12-19 13:38:01 +00:00
|
|
|
case SimpleShader:
|
|
|
|
vertexShader = ":/resources/scene-vertex.glsl";
|
|
|
|
break;
|
|
|
|
case GenericShader:
|
|
|
|
vertexShader = ":/resources/scene-generic-vertex.glsl";
|
|
|
|
break;
|
|
|
|
case ColorShader:
|
|
|
|
vertexShader = ":/resources/scene-color-vertex.glsl";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GLShader *shader = new GLShader(vertexShader, fragmentFile);
|
|
|
|
if (shader->isValid()) {
|
|
|
|
pushShader(shader);
|
|
|
|
resetShader(vertex);
|
|
|
|
popShader();
|
|
|
|
}
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:11:19 +00:00
|
|
|
GLShader *ShaderManager::loadVertexShader(ShaderType fragment, const QString &vertexFile)
|
|
|
|
{
|
|
|
|
QString fragmentShader;
|
2011-01-30 14:34:42 +00:00
|
|
|
switch(fragment) {
|
|
|
|
// Simple and Generic Shader use same fragment Shader
|
2011-01-09 19:11:19 +00:00
|
|
|
case SimpleShader:
|
|
|
|
case GenericShader:
|
|
|
|
fragmentShader = ":/resources/scene-fragment.glsl";
|
|
|
|
break;
|
|
|
|
case ColorShader:
|
|
|
|
fragmentShader = ":/resources/scene-color-fragment.glsl";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GLShader *shader = new GLShader(vertexFile, fragmentShader);
|
|
|
|
if (shader->isValid()) {
|
|
|
|
pushShader(shader);
|
|
|
|
resetShader(fragment);
|
|
|
|
popShader();
|
|
|
|
}
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2011-02-04 15:41:55 +00:00
|
|
|
GLShader *ShaderManager::loadShaderFromCode(const QByteArray &vertexSource, const QByteArray &fragmentSource)
|
2011-01-08 18:53:11 +00:00
|
|
|
{
|
|
|
|
GLShader *shader = new GLShader();
|
|
|
|
shader->load(vertexSource, fragmentSource);
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
void ShaderManager::initShaders()
|
|
|
|
{
|
2011-04-29 14:41:11 +00:00
|
|
|
if (legacyGl) {
|
|
|
|
kDebug(1212) << "OpenGL Shaders disabled by config option";
|
|
|
|
return;
|
|
|
|
}
|
2010-12-11 09:25:46 +00:00
|
|
|
m_orthoShader = new GLShader(":/resources/scene-vertex.glsl", ":/resources/scene-fragment.glsl");
|
|
|
|
if (m_orthoShader->isValid()) {
|
|
|
|
pushShader(SimpleShader, true);
|
|
|
|
popShader();
|
|
|
|
kDebug(1212) << "Ortho Shader is valid";
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-12-11 09:25:46 +00:00
|
|
|
delete m_orthoShader;
|
|
|
|
m_orthoShader = NULL;
|
|
|
|
kDebug(1212) << "Orho Shader is not valid";
|
|
|
|
return;
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
m_genericShader = new GLShader(":/resources/scene-generic-vertex.glsl", ":/resources/scene-fragment.glsl");
|
2010-12-11 09:25:46 +00:00
|
|
|
if (m_genericShader->isValid()) {
|
|
|
|
pushShader(GenericShader, true);
|
|
|
|
popShader();
|
|
|
|
kDebug(1212) << "Generic Shader is valid";
|
2011-01-30 14:34:42 +00:00
|
|
|
} else {
|
2010-12-11 09:25:46 +00:00
|
|
|
delete m_genericShader;
|
|
|
|
m_genericShader = NULL;
|
|
|
|
delete m_orthoShader;
|
|
|
|
m_orthoShader = NULL;
|
|
|
|
kDebug(1212) << "Generic Shader is not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_colorShader = new GLShader(":/resources/scene-color-vertex.glsl", ":/resources/scene-color-fragment.glsl");
|
|
|
|
if (m_colorShader->isValid()) {
|
|
|
|
pushShader(ColorShader, true);
|
|
|
|
popShader();
|
|
|
|
kDebug(1212) << "Color Shader is valid";
|
|
|
|
} else {
|
|
|
|
delete m_genericShader;
|
|
|
|
m_genericShader = NULL;
|
|
|
|
delete m_orthoShader;
|
|
|
|
m_orthoShader = NULL;
|
|
|
|
delete m_colorShader;
|
|
|
|
m_colorShader = NULL;
|
|
|
|
kDebug(1212) << "Color Scene Shader is not valid";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderManager::resetShader(ShaderType type)
|
|
|
|
{
|
|
|
|
// resetShader is either called from init or from push, we know that a built-in shader is bound
|
2011-02-10 20:36:24 +00:00
|
|
|
const QMatrix4x4 identity;
|
|
|
|
|
|
|
|
QMatrix4x4 projection;
|
|
|
|
QMatrix4x4 modelView;
|
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
GLShader *shader = getBoundShader();
|
2011-02-10 20:36:24 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
switch(type) {
|
2011-02-10 20:36:24 +00:00
|
|
|
case SimpleShader:
|
2010-12-12 12:40:39 +00:00
|
|
|
projection.ortho(0, displayWidth(), displayHeight(), 0, 0, 65535);
|
2010-12-11 09:25:46 +00:00
|
|
|
break;
|
2011-02-10 20:36:24 +00:00
|
|
|
|
2010-12-11 09:25:46 +00:00
|
|
|
case GenericShader: {
|
2011-02-10 20:36:24 +00:00
|
|
|
// Set up the projection matrix
|
|
|
|
float fovy = 60.0f;
|
2010-12-11 09:25:46 +00:00
|
|
|
float aspect = 1.0f;
|
2011-02-10 20:36:24 +00:00
|
|
|
float zNear = 0.1f;
|
|
|
|
float zFar = 100.0f;
|
|
|
|
float ymax = zNear * tan(fovy * M_PI / 360.0f);
|
|
|
|
float ymin = -ymax;
|
|
|
|
float xmin = ymin * aspect;
|
|
|
|
float xmax = ymax * aspect;
|
2010-12-11 09:25:46 +00:00
|
|
|
projection.frustum(xmin, xmax, ymin, ymax, zNear, zFar);
|
2011-02-10 20:36:24 +00:00
|
|
|
|
|
|
|
// Set up the model-view matrix
|
2011-01-30 14:34:42 +00:00
|
|
|
float scaleFactor = 1.1 * tan(fovy * M_PI / 360.0f) / ymax;
|
2011-02-10 20:36:24 +00:00
|
|
|
modelView.translate(xmin * scaleFactor, ymax * scaleFactor, -1.1);
|
|
|
|
modelView.scale((xmax - xmin)*scaleFactor / displayWidth(), -(ymax - ymin)*scaleFactor / displayHeight(), 0.001);
|
2010-12-11 09:25:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-02-10 20:36:24 +00:00
|
|
|
|
|
|
|
case ColorShader:
|
2010-12-12 12:40:39 +00:00
|
|
|
projection.ortho(0, displayWidth(), displayHeight(), 0, 0, 65535);
|
2010-12-11 09:25:46 +00:00
|
|
|
shader->setUniform("geometryColor", QVector4D(0, 0, 0, 1));
|
|
|
|
break;
|
|
|
|
}
|
2011-02-10 20:36:24 +00:00
|
|
|
|
2011-02-17 17:44:52 +00:00
|
|
|
shader->setUniform("sampler", 0);
|
2011-02-10 20:36:24 +00:00
|
|
|
|
|
|
|
shader->setUniform(GLShader::ProjectionMatrix, projection);
|
|
|
|
shader->setUniform(GLShader::ModelViewMatrix, modelView);
|
|
|
|
shader->setUniform(GLShader::ScreenTransformation, identity);
|
|
|
|
shader->setUniform(GLShader::WindowTransformation, identity);
|
|
|
|
|
|
|
|
shader->setUniform(GLShader::Offset, QVector2D(0, 0));
|
2011-02-12 00:36:21 +00:00
|
|
|
shader->setUniform(GLShader::ModulationConstant, QVector4D(1.0, 1.0, 1.0, 1.0));
|
2011-02-10 20:36:24 +00:00
|
|
|
|
|
|
|
shader->setUniform(GLShader::Saturation, 1.0f);
|
|
|
|
shader->setUniform(GLShader::AlphaToOne, 0);
|
2010-12-11 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
/*** GLRenderTarget ***/
|
2011-02-04 16:30:36 +00:00
|
|
|
bool GLRenderTarget::sSupported = false;
|
2011-08-17 17:32:36 +00:00
|
|
|
bool GLRenderTarget::s_blitSupported = false;
|
2011-03-13 13:34:30 +00:00
|
|
|
QStack<GLRenderTarget*> GLRenderTarget::s_renderTargets = QStack<GLRenderTarget*>();
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void GLRenderTarget::initStatic()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-11-28 15:16:56 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
2011-02-04 16:30:36 +00:00
|
|
|
sSupported = true;
|
2011-08-17 17:32:36 +00:00
|
|
|
s_blitSupported = false;
|
2010-11-28 15:16:56 +00:00
|
|
|
#else
|
2011-02-04 16:30:36 +00:00
|
|
|
sSupported = hasGLExtension("GL_EXT_framebuffer_object") && glFramebufferTexture2D;
|
2011-08-17 17:32:36 +00:00
|
|
|
s_blitSupported = hasGLExtension("GL_EXT_framebuffer_blit");
|
2010-11-28 15:16:56 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-03-13 13:34:30 +00:00
|
|
|
bool GLRenderTarget::isRenderTargetBound()
|
|
|
|
{
|
|
|
|
return !s_renderTargets.isEmpty();
|
|
|
|
}
|
|
|
|
|
2011-08-17 17:32:36 +00:00
|
|
|
bool GLRenderTarget::blitSupported()
|
|
|
|
{
|
|
|
|
return s_blitSupported;
|
|
|
|
}
|
|
|
|
|
2011-03-13 13:34:30 +00:00
|
|
|
void GLRenderTarget::pushRenderTarget(GLRenderTarget* target)
|
|
|
|
{
|
|
|
|
target->enable();
|
|
|
|
s_renderTargets.push(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLRenderTarget* GLRenderTarget::popRenderTarget()
|
|
|
|
{
|
|
|
|
GLRenderTarget* ret = s_renderTargets.pop();
|
|
|
|
ret->disable();
|
|
|
|
if (!s_renderTargets.isEmpty())
|
|
|
|
s_renderTargets.top()->enable();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
GLRenderTarget::GLRenderTarget(GLTexture* color)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
// Reset variables
|
|
|
|
mValid = false;
|
|
|
|
|
|
|
|
mTexture = color;
|
|
|
|
|
|
|
|
// Make sure FBO is supported
|
2011-02-04 16:30:36 +00:00
|
|
|
if (sSupported && mTexture && !mTexture->isNull()) {
|
2007-04-29 17:35:43 +00:00
|
|
|
initFBO();
|
2011-01-30 14:34:42 +00:00
|
|
|
} else
|
2007-08-20 21:33:48 +00:00
|
|
|
kError(1212) << "Render targets aren't supported!" << endl;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
GLRenderTarget::~GLRenderTarget()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (mValid) {
|
2007-04-29 17:35:43 +00:00
|
|
|
glDeleteFramebuffers(1, &mFramebuffer);
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
bool GLRenderTarget::enable()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (!valid()) {
|
2007-08-20 21:33:48 +00:00
|
|
|
kError(1212) << "Can't enable invalid render target!" << endl;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-11-20 09:07:16 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
|
2007-04-29 17:35:43 +00:00
|
|
|
mTexture->setDirty();
|
|
|
|
|
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
bool GLRenderTarget::disable()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
if (!valid()) {
|
2007-08-20 21:33:48 +00:00
|
|
|
kError(1212) << "Can't disable invalid render target!" << endl;
|
2007-04-29 17:35:43 +00:00
|
|
|
return false;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-11-20 09:07:16 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2007-04-29 17:35:43 +00:00
|
|
|
mTexture->setDirty();
|
|
|
|
|
|
|
|
return true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
static QString formatFramebufferStatus(GLenum status)
|
|
|
|
{
|
|
|
|
switch(status) {
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
|
|
|
// An attachment is the wrong type / is invalid / has 0 width or height
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
|
|
|
|
// There are no images attached to the framebuffer
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
|
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED:
|
|
|
|
// A format or the combination of formats of the attachments is unsupported
|
|
|
|
return "GL_FRAMEBUFFER_UNSUPPORTED";
|
2010-11-20 09:07:16 +00:00
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
|
|
|
|
// Not all attached images have the same width and height
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT";
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
|
|
|
|
// The color attachments don't have the same format
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT";
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
|
|
|
|
// The attachments don't have the same number of samples
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE";
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
|
|
|
// The draw buffer is missing
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER";
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
|
|
|
// The read buffer is missing
|
|
|
|
return "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER";
|
2010-11-20 09:07:16 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
default:
|
|
|
|
return "Unknown (0x" + QString::number(status, 16) + ')';
|
2010-06-08 20:29:17 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-08 20:29:17 +00:00
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
void GLRenderTarget::initFBO()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-06-08 20:29:17 +00:00
|
|
|
#if DEBUG_GLRENDERTARGET
|
|
|
|
GLenum err = glGetError();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (err != GL_NO_ERROR)
|
|
|
|
kError(1212) << "Error status when entering GLRenderTarget::initFBO: " << formatGLError(err);
|
2010-06-08 20:29:17 +00:00
|
|
|
#endif
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
glGenFramebuffers(1, &mFramebuffer);
|
2010-06-08 20:29:17 +00:00
|
|
|
|
|
|
|
#if DEBUG_GLRENDERTARGET
|
2011-01-30 14:34:42 +00:00
|
|
|
if ((err = glGetError()) != GL_NO_ERROR) {
|
|
|
|
kError(1212) << "glGenFramebuffers failed: " << formatGLError(err);
|
2010-06-08 20:29:17 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-08 20:29:17 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-20 09:07:16 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-06-08 20:29:17 +00:00
|
|
|
#if DEBUG_GLRENDERTARGET
|
2011-01-30 14:34:42 +00:00
|
|
|
if ((err = glGetError()) != GL_NO_ERROR) {
|
|
|
|
kError(1212) << "glBindFramebuffer failed: " << formatGLError(err);
|
2010-06-08 20:29:17 +00:00
|
|
|
glDeleteFramebuffers(1, &mFramebuffer);
|
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-08 20:29:17 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-20 09:07:16 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
2007-04-29 17:35:43 +00:00
|
|
|
mTexture->target(), mTexture->texture(), 0);
|
|
|
|
|
2010-06-08 20:29:17 +00:00
|
|
|
#if DEBUG_GLRENDERTARGET
|
2011-01-30 14:34:42 +00:00
|
|
|
if ((err = glGetError()) != GL_NO_ERROR) {
|
|
|
|
kError(1212) << "glFramebufferTexture2D failed: " << formatGLError(err);
|
2010-11-20 09:07:16 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2010-06-08 20:29:17 +00:00
|
|
|
glDeleteFramebuffers(1, &mFramebuffer);
|
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-06-08 20:29:17 +00:00
|
|
|
#endif
|
|
|
|
|
2010-11-20 09:07:16 +00:00
|
|
|
const GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2010-11-20 09:07:16 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
// We have an incomplete framebuffer, consider it invalid
|
2010-06-08 20:29:17 +00:00
|
|
|
if (status == 0)
|
2011-01-30 14:34:42 +00:00
|
|
|
kError(1212) << "glCheckFramebufferStatus failed: " << formatGLError(glGetError());
|
2010-06-08 20:29:17 +00:00
|
|
|
else
|
2011-01-30 14:34:42 +00:00
|
|
|
kError(1212) << "Invalid framebuffer status: " << formatFramebufferStatus(status);
|
2009-10-16 10:36:17 +00:00
|
|
|
glDeleteFramebuffers(1, &mFramebuffer);
|
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2009-10-16 10:36:17 +00:00
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
mValid = true;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-08-17 17:32:36 +00:00
|
|
|
void GLRenderTarget::blitFromFramebuffer(const QRect &source, const QRect &destination, GLenum filter)
|
|
|
|
{
|
|
|
|
if (!GLRenderTarget::blitSupported()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifndef KWIN_HAVE_OPENGLES
|
|
|
|
GLRenderTarget::pushRenderTarget(this);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer);
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
const QRect s = source.isNull() ? QRect(0, 0, displayWidth(), displayHeight()) : source;
|
|
|
|
const QRect d = destination.isNull() ? QRect(0, 0, mTexture->width(), mTexture->height()) : destination;
|
|
|
|
|
|
|
|
glBlitFramebuffer(s.x(), displayHeight() - s.y() - s.height(), s.x() + s.width(), displayHeight() - s.y(),
|
|
|
|
d.x(), mTexture->height() - d.y() - d.height(), d.x() + d.width(), mTexture->height() - d.y(),
|
|
|
|
GL_COLOR_BUFFER_BIT, filter);
|
|
|
|
GLRenderTarget::popRenderTarget();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-19 20:53:32 +00:00
|
|
|
|
|
|
|
//*********************************
|
|
|
|
// GLVertexBufferPrivate
|
|
|
|
//*********************************
|
|
|
|
class GLVertexBufferPrivate
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GLVertexBufferPrivate(GLVertexBuffer::UsageHint usageHint)
|
|
|
|
: hint(usageHint)
|
|
|
|
, numberVertices(0)
|
|
|
|
, dimension(2)
|
|
|
|
, useColor(false)
|
|
|
|
, useTexCoords(true)
|
|
|
|
, color(0, 0, 0, 255) {
|
|
|
|
if (GLVertexBufferPrivate::supported) {
|
|
|
|
glGenBuffers(2, buffers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~GLVertexBufferPrivate() {
|
|
|
|
if (GLVertexBufferPrivate::supported) {
|
|
|
|
glDeleteBuffers(2, buffers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GLVertexBuffer::UsageHint hint;
|
|
|
|
GLuint buffers[2];
|
|
|
|
int numberVertices;
|
|
|
|
int dimension;
|
|
|
|
static bool supported;
|
|
|
|
static GLVertexBuffer *streamingBuffer;
|
|
|
|
QVector<float> legacyVertices;
|
|
|
|
QVector<float> legacyTexCoords;
|
|
|
|
bool useColor;
|
|
|
|
bool useTexCoords;
|
|
|
|
QColor color;
|
|
|
|
|
2011-02-14 06:38:56 +00:00
|
|
|
//! VBO is not supported
|
2011-01-30 14:34:42 +00:00
|
|
|
void legacyPainting(QRegion region, GLenum primitiveMode);
|
2011-02-14 06:38:56 +00:00
|
|
|
//! VBO and shaders are both supported
|
2011-01-30 14:34:42 +00:00
|
|
|
void corePainting(const QRegion& region, GLenum primitiveMode);
|
2011-02-14 06:38:56 +00:00
|
|
|
//! VBO is supported, but shaders are not supported
|
|
|
|
void fallbackPainting(const QRegion& region, GLenum primitiveMode);
|
2011-01-30 14:34:42 +00:00
|
|
|
};
|
2010-07-19 20:53:32 +00:00
|
|
|
bool GLVertexBufferPrivate::supported = false;
|
2010-12-11 14:52:21 +00:00
|
|
|
GLVertexBuffer *GLVertexBufferPrivate::streamingBuffer = NULL;
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void GLVertexBufferPrivate::legacyPainting(QRegion region, GLenum primitiveMode)
|
|
|
|
{
|
2010-12-31 13:48:10 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(primitiveMode)
|
|
|
|
#else
|
2011-06-26 10:01:13 +00:00
|
|
|
Q_UNUSED(region)
|
2010-07-24 07:32:42 +00:00
|
|
|
// Enable arrays
|
2011-01-30 14:34:42 +00:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glVertexPointer(dimension, GL_FLOAT, 0, legacyVertices.constData());
|
2010-12-05 08:13:46 +00:00
|
|
|
if (!legacyTexCoords.isEmpty()) {
|
2011-01-30 14:34:42 +00:00
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 0, legacyTexCoords.constData());
|
2010-12-05 08:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (useColor) {
|
|
|
|
glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF());
|
|
|
|
}
|
2010-07-24 07:32:42 +00:00
|
|
|
|
2011-06-26 10:01:13 +00:00
|
|
|
glDrawArrays(primitiveMode, 0, numberVertices);
|
2010-07-24 07:32:42 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
2010-12-05 08:13:46 +00:00
|
|
|
if (!legacyTexCoords.isEmpty()) {
|
2011-01-30 14:34:42 +00:00
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
2010-12-05 08:13:46 +00:00
|
|
|
}
|
2010-11-20 08:16:53 +00:00
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-24 07:32:42 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void GLVertexBufferPrivate::corePainting(const QRegion& region, GLenum primitiveMode)
|
|
|
|
{
|
2011-06-26 10:01:13 +00:00
|
|
|
Q_UNUSED(region)
|
2010-12-11 10:10:53 +00:00
|
|
|
GLShader *shader = ShaderManager::instance()->getBoundShader();
|
|
|
|
GLint vertexAttrib = shader->attributeLocation("vertex");
|
2011-01-30 14:34:42 +00:00
|
|
|
GLint texAttrib = shader->attributeLocation("texCoord");
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2011-02-14 06:38:56 +00:00
|
|
|
glEnableVertexAttribArray(vertexAttrib);
|
|
|
|
if (useTexCoords) {
|
|
|
|
glEnableVertexAttribArray(texAttrib);
|
|
|
|
}
|
|
|
|
|
2010-12-05 08:13:46 +00:00
|
|
|
if (useColor) {
|
2010-12-11 10:10:53 +00:00
|
|
|
shader->setUniform("geometryColor", color);
|
2010-12-05 08:13:46 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffers[ 0 ]);
|
|
|
|
glVertexAttribPointer(vertexAttrib, dimension, GL_FLOAT, GL_FALSE, 0, 0);
|
2010-12-07 20:50:00 +00:00
|
|
|
|
|
|
|
if (texAttrib != -1 && useTexCoords) {
|
2011-01-30 14:34:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, buffers[ 1 ]);
|
|
|
|
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
2010-12-07 20:50:00 +00:00
|
|
|
}
|
|
|
|
|
2011-06-26 10:01:13 +00:00
|
|
|
glDrawArrays(primitiveMode, 0, numberVertices);
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2010-12-07 20:50:00 +00:00
|
|
|
if (useTexCoords) {
|
2011-02-14 06:38:56 +00:00
|
|
|
glDisableVertexAttribArray(texAttrib);
|
2010-11-14 19:49:00 +00:00
|
|
|
}
|
2011-02-14 06:38:56 +00:00
|
|
|
glDisableVertexAttribArray(vertexAttrib);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLVertexBufferPrivate::fallbackPainting(const QRegion& region, GLenum primitiveMode)
|
|
|
|
{
|
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
Q_UNUSED(primitiveMode)
|
|
|
|
#else
|
2011-06-26 10:01:13 +00:00
|
|
|
Q_UNUSED(region)
|
2011-02-14 06:38:56 +00:00
|
|
|
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
|
2011-06-26 10:01:13 +00:00
|
|
|
glDrawArrays(primitiveMode, 0, numberVertices);
|
2011-02-14 06:38:56 +00:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
#endif
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-14 19:49:00 +00:00
|
|
|
|
2010-07-19 20:53:32 +00:00
|
|
|
//*********************************
|
|
|
|
// GLVertexBuffer
|
|
|
|
//*********************************
|
2011-01-30 14:34:42 +00:00
|
|
|
GLVertexBuffer::GLVertexBuffer(UsageHint hint)
|
|
|
|
: d(new GLVertexBufferPrivate(hint))
|
|
|
|
{
|
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
|
|
|
GLVertexBuffer::~GLVertexBuffer()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-07-19 20:53:32 +00:00
|
|
|
delete d;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void GLVertexBuffer::setData(int numberVertices, int dim, const float* vertices, const float* texcoords)
|
|
|
|
{
|
2010-07-19 20:53:32 +00:00
|
|
|
d->numberVertices = numberVertices;
|
|
|
|
d->dimension = dim;
|
2010-12-07 20:50:00 +00:00
|
|
|
d->useTexCoords = (texcoords != NULL);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (!GLVertexBufferPrivate::supported) {
|
2010-07-24 07:32:42 +00:00
|
|
|
// legacy data
|
|
|
|
d->legacyVertices.clear();
|
2011-01-30 14:34:42 +00:00
|
|
|
d->legacyVertices.reserve(numberVertices * dim);
|
|
|
|
for (int i = 0; i < numberVertices * dim; ++i) {
|
2010-07-24 07:32:42 +00:00
|
|
|
d->legacyVertices << vertices[i];
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-24 07:32:42 +00:00
|
|
|
d->legacyTexCoords.clear();
|
2010-12-07 20:50:00 +00:00
|
|
|
if (d->useTexCoords) {
|
2011-01-30 14:34:42 +00:00
|
|
|
d->legacyTexCoords.reserve(numberVertices * 2);
|
|
|
|
for (int i = 0; i < numberVertices * 2; ++i) {
|
2010-12-05 08:13:46 +00:00
|
|
|
d->legacyTexCoords << texcoords[i];
|
2010-07-24 07:32:42 +00:00
|
|
|
}
|
2010-12-05 08:13:46 +00:00
|
|
|
}
|
2010-07-24 07:32:42 +00:00
|
|
|
return;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
GLenum hint;
|
2011-01-30 14:34:42 +00:00
|
|
|
switch(d->hint) {
|
|
|
|
case Dynamic:
|
|
|
|
hint = GL_DYNAMIC_DRAW;
|
|
|
|
break;
|
|
|
|
case Static:
|
|
|
|
hint = GL_STATIC_DRAW;
|
|
|
|
break;
|
|
|
|
case Stream:
|
|
|
|
hint = GL_STREAM_DRAW;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// just to make the compiler happy
|
|
|
|
hint = GL_STREAM_DRAW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, d->buffers[ 0 ]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numberVertices * d->dimension, vertices, hint);
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2010-12-07 20:50:00 +00:00
|
|
|
if (d->useTexCoords) {
|
2011-01-30 14:34:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, d->buffers[ 1 ]);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*numberVertices * 2, texcoords, hint);
|
2010-12-05 08:13:46 +00:00
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void GLVertexBuffer::render(GLenum primitiveMode)
|
|
|
|
{
|
|
|
|
render(infiniteRegion(), primitiveMode);
|
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
void GLVertexBuffer::render(const QRegion& region, GLenum primitiveMode)
|
|
|
|
{
|
|
|
|
if (!GLVertexBufferPrivate::supported) {
|
|
|
|
d->legacyPainting(region, primitiveMode);
|
2011-02-14 06:38:56 +00:00
|
|
|
} else if (ShaderManager::instance()->isShaderBound()) {
|
2011-01-30 14:34:42 +00:00
|
|
|
d->corePainting(region, primitiveMode);
|
2011-01-02 09:08:13 +00:00
|
|
|
} else {
|
2011-02-14 06:38:56 +00:00
|
|
|
d->fallbackPainting(region, primitiveMode);
|
2011-01-02 09:08:13 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
|
|
|
bool GLVertexBuffer::isSupported()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-07-19 20:53:32 +00:00
|
|
|
return GLVertexBufferPrivate::supported;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2010-12-05 08:13:46 +00:00
|
|
|
bool GLVertexBuffer::isUseColor() const
|
|
|
|
{
|
|
|
|
return d->useColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLVertexBuffer::setUseColor(bool enable)
|
|
|
|
{
|
|
|
|
d->useColor = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLVertexBuffer::setColor(const QColor& color, bool enable)
|
|
|
|
{
|
|
|
|
d->useColor = enable;
|
|
|
|
d->color = color;
|
|
|
|
}
|
|
|
|
|
2010-12-11 14:52:21 +00:00
|
|
|
void GLVertexBuffer::reset()
|
|
|
|
{
|
|
|
|
d->useColor = false;
|
|
|
|
d->color = QColor(0, 0, 0, 255);
|
|
|
|
d->numberVertices = 0;
|
|
|
|
d->dimension = 2;
|
|
|
|
d->useTexCoords = true;
|
|
|
|
}
|
|
|
|
|
2010-07-19 20:53:32 +00:00
|
|
|
void GLVertexBuffer::initStatic()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-11-20 09:17:27 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
|
|
|
GLVertexBufferPrivate::supported = true;
|
|
|
|
#else
|
2011-01-30 14:34:42 +00:00
|
|
|
GLVertexBufferPrivate::supported = hasGLExtension("GL_ARB_vertex_buffer_object");
|
2010-11-20 09:17:27 +00:00
|
|
|
#endif
|
2010-12-11 14:52:21 +00:00
|
|
|
GLVertexBufferPrivate::streamingBuffer = new GLVertexBuffer(GLVertexBuffer::Stream);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2010-12-11 14:52:21 +00:00
|
|
|
GLVertexBuffer *GLVertexBuffer::streamingBuffer()
|
|
|
|
{
|
|
|
|
return GLVertexBufferPrivate::streamingBuffer;
|
|
|
|
}
|
2010-07-19 20:53:32 +00:00
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
} // namespace
|