KWinLibraries
kwinshadereffect.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kwinshadereffect.h"
00022
00023 #include "kwinglutils.h"
00024
00025 #include <kstandarddirs.h>
00026 #include <kdebug.h>
00027
00028 #include <assert.h>
00029
00030 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
00031
00032 namespace KWin
00033 {
00034
00035
00036 ShaderEffect::ShaderEffect(const QString& shadername) : Effect()
00037 {
00038 mTexture = 0;
00039 mRenderTarget = 0;
00040 mShader = 0;
00041
00042 mTime = 0.0f;
00043 mValid = loadData(shadername);
00044 mEnabled = false;
00045 }
00046
00047 ShaderEffect::~ShaderEffect()
00048 {
00049 delete mTexture;
00050 delete mRenderTarget;
00051 delete mShader;
00052 }
00053
00054 bool ShaderEffect::loadData(const QString& shadername)
00055 {
00056 #ifndef KWIN_HAVE_OPENGL_COMPOSITING
00057 return false;
00058 #else
00059
00060
00061
00062 int texw = displayWidth();
00063 int texh = displayHeight();
00064 if( !GLTexture::NPOTTextureSupported() )
00065 {
00066 kWarning( 1212 ) << "NPOT textures not supported, wasting some memory" ;
00067 texw = nearestPowerOfTwo(texw);
00068 texh = nearestPowerOfTwo(texh);
00069 }
00070
00071 mTexture = new GLTexture(texw, texh);
00072 mTexture->setFilter(GL_LINEAR_MIPMAP_LINEAR);
00073 mTexture->setWrapMode(GL_CLAMP);
00074
00075 mRenderTarget = new GLRenderTarget(mTexture);
00076 if( !mRenderTarget->valid() )
00077 return false;
00078
00079 QString fragmentshader = KGlobal::dirs()->findResource("data", "kwin/" + shadername + ".frag");
00080 QString vertexshader = KGlobal::dirs()->findResource("data", "kwin/" + shadername + ".vert");
00081 if(fragmentshader.isEmpty() || vertexshader.isEmpty())
00082 {
00083 kError() << "Couldn't locate shader files" << endl;
00084 return false;
00085 }
00086 mShader = new GLShader(vertexshader, fragmentshader);
00087 if(!mShader->isValid())
00088 {
00089 kError() << "The shader failed to load!" << endl;
00090 return false;
00091 }
00092 mShader->bind();
00093 mShader->setUniform("sceneTex", 0);
00094 mShader->setUniform("textureWidth", (float)texw);
00095 mShader->setUniform("textureHeight", (float)texh);
00096 mShader->unbind();
00097
00098 return true;
00099 #endif
00100 }
00101
00102 bool ShaderEffect::supported()
00103 {
00104 #ifndef KWIN_HAVE_OPENGL_COMPOSITING
00105 return false;
00106 #else
00107 return GLRenderTarget::supported() &&
00108 GLShader::fragmentShaderSupported() &&
00109 (effects->compositingType() == OpenGLCompositing);
00110 #endif
00111 }
00112
00113 bool ShaderEffect::isEnabled() const
00114 {
00115 return mEnabled;
00116 }
00117
00118 void ShaderEffect::setEnabled(bool enabled)
00119 {
00120 mEnabled = enabled;
00121
00122 effects->addRepaintFull();
00123 }
00124
00125 GLShader* ShaderEffect::shader() const
00126 {
00127 return mShader;
00128 }
00129
00130 void ShaderEffect::prePaintScreen( ScreenPrePaintData& data, int time )
00131 {
00132 mTime += time / 1000.0f;
00133 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
00134 if( mValid && mEnabled )
00135 {
00136 data.mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS;
00137
00138 effects->pushRenderTarget(mRenderTarget);
00139 }
00140 #endif
00141
00142 effects->prePaintScreen(data, time);
00143 }
00144
00145 void ShaderEffect::postPaintScreen()
00146 {
00147
00148 effects->postPaintScreen();
00149
00150 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
00151 if( mValid && mEnabled )
00152 {
00153
00154 assert( effects->popRenderTarget() == mRenderTarget );
00155 mTexture->bind();
00156
00157
00158 mShader->bind();
00159 mShader->setUniform("time", (float)mTime);
00160 mShader->setUniform("cursorX", (float)cursorPos().x());
00161 mShader->setUniform("cursorY", (float)cursorPos().y());
00162
00163
00164 glBegin(GL_QUADS);
00165 glVertex2f(0.0, displayHeight());
00166 glVertex2f(displayWidth(), displayHeight());
00167 glVertex2f(displayWidth(), 0.0);
00168 glVertex2f(0.0, 0.0);
00169 glEnd();
00170
00171 mShader->unbind();
00172 mTexture->unbind();
00173 }
00174 #endif
00175 }
00176
00177 }
00178
00179 #endif