2006-11-08 19:10:07 +00:00
|
|
|
/*****************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
2007-02-05 19:57:05 +00:00
|
|
|
Copyright (C) 2006-2007 Rivo Laks <rivolaks@hot.ee>
|
2006-11-08 19:10:07 +00:00
|
|
|
|
|
|
|
You can Freely distribute this program under the GNU General Public
|
|
|
|
License. See the file "COPYING" for the exact licensing terms.
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
#include "glutils.h"
|
|
|
|
|
2007-02-05 18:11:15 +00:00
|
|
|
#include <QHash>
|
|
|
|
#include <QFile>
|
|
|
|
|
2006-11-08 19:10:07 +00:00
|
|
|
|
2007-01-29 21:06:56 +00:00
|
|
|
#define MAKE_GL_VERSION(major, minor, release) ( ((major) << 16) | ((minor) << 8) | (release) )
|
|
|
|
|
|
|
|
|
2006-11-08 19:10:07 +00:00
|
|
|
namespace KWinInternal
|
|
|
|
{
|
|
|
|
// Variables
|
2007-01-29 21:06:56 +00:00
|
|
|
// GL version, use MAKE_GL_VERSION() macro for comparing with a specific version
|
2006-11-14 11:52:20 +00:00
|
|
|
static int glVersion;
|
2007-01-29 21:06:56 +00:00
|
|
|
// GLX version, use MAKE_GL_VERSION() macro for comparing with a specific version
|
2006-11-14 11:52:20 +00:00
|
|
|
static int glXVersion;
|
|
|
|
// List of all supported GL and GLX extensions
|
|
|
|
static QStringList glExtensions;
|
2006-11-08 19:10:07 +00:00
|
|
|
|
2007-02-05 19:57:05 +00:00
|
|
|
int glTextureUnitsCount;
|
2006-11-08 19:10:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Functions
|
|
|
|
void initGLX()
|
2006-11-14 11:41:52 +00:00
|
|
|
{
|
|
|
|
// Get GLX version
|
|
|
|
int major, minor;
|
|
|
|
glXQueryVersion( display(), &major, &minor );
|
2007-01-29 21:06:56 +00:00
|
|
|
glXVersion = MAKE_GL_VERSION( major, minor, 0 );
|
2006-11-14 11:52:20 +00:00
|
|
|
// Get list of supported GLX extensions. Simply add it to the list of OpenGL extensions.
|
|
|
|
glExtensions += QString((const char*)glXQueryExtensionsString(
|
|
|
|
display(), DefaultScreen( display()))).split(" ");
|
2006-11-19 20:41:10 +00:00
|
|
|
|
2007-02-05 19:57:05 +00:00
|
|
|
glxResolveFunctions();
|
2006-11-14 11:41:52 +00:00
|
|
|
}
|
2006-11-08 19:10:07 +00:00
|
|
|
|
|
|
|
void initGL()
|
2006-11-14 11:41:52 +00:00
|
|
|
{
|
2006-11-08 19:10:07 +00:00
|
|
|
// Get OpenGL version
|
|
|
|
QString glversionstring = QString((const char*)glGetString(GL_VERSION));
|
|
|
|
QStringList glversioninfo = glversionstring.left(glversionstring.indexOf(' ')).split('.');
|
2007-01-29 21:06:56 +00:00
|
|
|
glVersion = MAKE_GL_VERSION(glversioninfo[0].toInt(), glversioninfo[1].toInt(),
|
2006-11-08 19:10:07 +00:00
|
|
|
glversioninfo.count() > 2 ? glversioninfo[2].toInt() : 0);
|
|
|
|
// Get list of supported OpenGL extensions
|
|
|
|
glExtensions = QString((const char*)glGetString(GL_EXTENSIONS)).split(" ");
|
2006-11-19 20:41:10 +00:00
|
|
|
|
|
|
|
// handle OpenGL extensions functions
|
2007-02-05 19:57:05 +00:00
|
|
|
glResolveFunctions();
|
2006-11-14 11:41:52 +00:00
|
|
|
}
|
2006-11-08 19:10:07 +00:00
|
|
|
|
|
|
|
bool hasGLVersion(int major, int minor, int release)
|
2006-11-14 11:41:52 +00:00
|
|
|
{
|
2007-01-29 21:06:56 +00:00
|
|
|
return glVersion >= MAKE_GL_VERSION(major, minor, release);
|
2006-11-14 11:41:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasGLXVersion(int major, int minor, int release)
|
|
|
|
{
|
2007-01-29 21:06:56 +00:00
|
|
|
return glXVersion >= MAKE_GL_VERSION(major, minor, release);
|
2006-11-14 11:41:52 +00:00
|
|
|
}
|
|
|
|
|
2006-11-08 19:10:07 +00:00
|
|
|
bool hasGLExtension(const QString& extension)
|
2006-11-14 11:41:52 +00:00
|
|
|
{
|
2006-11-08 19:10:07 +00:00
|
|
|
return glExtensions.contains(extension);
|
2006-11-14 11:41:52 +00:00
|
|
|
}
|
2006-11-08 19:10:07 +00:00
|
|
|
|
2007-02-05 18:11:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
GLShader::GLShader(const QString& vertexfile, const QString& fragmentfile)
|
|
|
|
{
|
|
|
|
mValid = false;
|
|
|
|
mVariableLocations = 0;
|
|
|
|
|
|
|
|
loadFromFiles(vertexfile, fragmentfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::loadFromFiles(const QString& vertexfile, const QString& fragmentfile)
|
|
|
|
{
|
|
|
|
QFile vf(vertexfile);
|
|
|
|
if(!vf.open(IO_ReadOnly))
|
|
|
|
{
|
|
|
|
kError(1212) << k_funcinfo << "Couldn't open '" << vertexfile << "' for reading!" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QString vertexsource(vf.readAll());
|
|
|
|
|
|
|
|
QFile ff(fragmentfile);
|
|
|
|
if(!ff.open(IO_ReadOnly))
|
|
|
|
{
|
|
|
|
kError(1212) << k_funcinfo << "Couldn't open '" << fragmentfile << "' for reading!" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QString fragsource(ff.readAll());
|
|
|
|
|
|
|
|
return load(vertexsource, fragsource);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::load(const QString& vertexsource, const QString& fragmentsource)
|
|
|
|
{
|
|
|
|
GLuint vertexshader;
|
|
|
|
GLuint fragmentshader;
|
|
|
|
|
|
|
|
GLsizei logsize, logarraysize;
|
|
|
|
char* log = 0;
|
|
|
|
|
|
|
|
// Create program object
|
|
|
|
mProgram = glCreateProgram();
|
|
|
|
if(!vertexsource.isEmpty())
|
|
|
|
{
|
|
|
|
// Create shader object
|
|
|
|
vertexshader = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
// Load it
|
|
|
|
const QByteArray& srcba = vertexsource.toLatin1();
|
|
|
|
const char* src = srcba.data();
|
|
|
|
glShaderSource(vertexshader, 1, &src, NULL);
|
|
|
|
// Compile the shader
|
|
|
|
glCompileShader(vertexshader);
|
|
|
|
// Make sure it compiled correctly
|
|
|
|
int compiled;
|
|
|
|
glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &compiled);
|
|
|
|
// Get info log
|
|
|
|
glGetShaderiv(vertexshader, GL_INFO_LOG_LENGTH, &logarraysize);
|
|
|
|
log = new char[logarraysize];
|
|
|
|
glGetShaderInfoLog(vertexshader, logarraysize, &logsize, log);
|
|
|
|
if(!compiled)
|
|
|
|
{
|
|
|
|
kError(1212) << k_funcinfo << "Couldn't compile vertex shader! Log:" << endl << log << endl;
|
|
|
|
delete[] log;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(logsize > 0)
|
|
|
|
kDebug(1212) << "Vertex shader compilation log:" << endl << log << endl;
|
|
|
|
// Attach the shader to the program
|
|
|
|
glAttachShader(mProgram, vertexshader);
|
|
|
|
// Delete shader
|
|
|
|
glDeleteShader(vertexshader);
|
|
|
|
delete[] log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!fragmentsource.isEmpty())
|
|
|
|
{
|
|
|
|
fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
// Load it
|
|
|
|
const QByteArray& srcba = fragmentsource.toLatin1();
|
|
|
|
const char* src = srcba.data();
|
|
|
|
glShaderSource(fragmentshader, 1, &src, NULL);
|
|
|
|
//glShaderSource(fragmentshader, 1, &fragmentsrc.latin1(), NULL);
|
|
|
|
// Compile the shader
|
|
|
|
glCompileShader(fragmentshader);
|
|
|
|
// Make sure it compiled correctly
|
|
|
|
int compiled;
|
|
|
|
glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &compiled);
|
|
|
|
// Get info log
|
|
|
|
glGetShaderiv(fragmentshader, GL_INFO_LOG_LENGTH, &logarraysize);
|
|
|
|
log = new char[logarraysize];
|
|
|
|
glGetShaderInfoLog(fragmentshader, logarraysize, &logsize, log);
|
|
|
|
if(!compiled)
|
|
|
|
{
|
|
|
|
kError(1212) << k_funcinfo << "Couldn't compile fragment shader! Log:" << endl << log << endl;
|
|
|
|
delete[] log;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(logsize > 0)
|
|
|
|
kDebug(1212) << "Fragment shader compilation log:" << endl << log << endl;
|
|
|
|
// Attach the shader to the program
|
|
|
|
glAttachShader(mProgram, fragmentshader);
|
|
|
|
// Delete shader
|
|
|
|
glDeleteShader(fragmentshader);
|
|
|
|
delete[] log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Link the program
|
|
|
|
glLinkProgram(mProgram);
|
|
|
|
// Make sure it linked correctly
|
|
|
|
int linked;
|
|
|
|
glGetProgramiv(mProgram, GL_LINK_STATUS, &linked);
|
|
|
|
// Get info log
|
|
|
|
glGetProgramiv(mProgram, GL_INFO_LOG_LENGTH, &logarraysize);
|
|
|
|
log = new char[logarraysize];
|
|
|
|
glGetProgramInfoLog(mProgram, logarraysize, &logsize, log);
|
|
|
|
if(!linked)
|
|
|
|
{
|
|
|
|
kError(1212) << k_funcinfo << "Couldn't link the program! Log" << endl << log << endl;
|
|
|
|
delete[] log;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if(logsize > 0)
|
|
|
|
kDebug(1212) << "Shader linking log:" << endl << log << endl;
|
|
|
|
delete[] log;
|
|
|
|
|
|
|
|
mVariableLocations = new QHash<QString, int>;
|
|
|
|
|
|
|
|
mValid = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLShader::bind()
|
|
|
|
{
|
|
|
|
glUseProgram(mProgram);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLShader::unbind()
|
|
|
|
{
|
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GLShader::uniformLocation(const QString& name)
|
|
|
|
{
|
|
|
|
if(!mVariableLocations)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!mVariableLocations->contains(name))
|
|
|
|
{
|
|
|
|
int location = glGetUniformLocation(mProgram, name.toLatin1().data());
|
|
|
|
mVariableLocations->insert(name, location);
|
|
|
|
}
|
|
|
|
return mVariableLocations->value(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const QString& name, float value)
|
|
|
|
{
|
|
|
|
int location = uniformLocation(name);
|
|
|
|
if(location >= 0)
|
|
|
|
{
|
|
|
|
glUniform1f(location, value);
|
|
|
|
}
|
|
|
|
return (location >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setUniform(const QString& name, int value)
|
|
|
|
{
|
|
|
|
int location = uniformLocation(name);
|
|
|
|
if(location >= 0)
|
|
|
|
{
|
|
|
|
glUniform1i(location, value);
|
|
|
|
}
|
|
|
|
return (location >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int GLShader::attributeLocation(const QString& name)
|
|
|
|
{
|
|
|
|
if(!mVariableLocations)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(!mVariableLocations->contains(name))
|
|
|
|
{
|
|
|
|
int location = glGetAttribLocation(mProgram, name.toLatin1().data());
|
|
|
|
mVariableLocations->insert(name, location);
|
|
|
|
}
|
|
|
|
return mVariableLocations->value(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GLShader::setAttribute(const QString& name, float value)
|
|
|
|
{
|
|
|
|
int location = attributeLocation(name);
|
|
|
|
if(location >= 0)
|
|
|
|
{
|
|
|
|
glVertexAttrib1f(location, value);
|
|
|
|
}
|
|
|
|
return (location >= 0);
|
|
|
|
}
|
|
|
|
|
2006-11-08 19:10:07 +00:00
|
|
|
} // namespace
|