• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KWinLibraries

kwinshadereffect.cpp

Go to the documentation of this file.
00001 /********************************************************************
00002  KWin - the KDE window manager
00003  This file is part of the KDE project.
00004 
00005 Copyright (C) 2006-2007 Rivo Laks <rivolaks@hot.ee>
00006 
00007 This program is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 This program is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with this program.  If not, see <http://www.gnu.org/licenses/>.
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     // If NPOT textures are not supported, use nearest power-of-two sized
00060     //  texture. It wastes memory, but it's possible to support systems without
00061     //  NPOT textures that way
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     // Create texture and render target
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(1212) << "Couldn't locate shader files" << endl;
00084         return false;
00085     }
00086     mShader = new GLShader(vertexshader, fragmentshader);
00087     if(!mShader->isValid())
00088     {
00089         kError(1212) << "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     // Everything needs to be repainted
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         // Start rendering to texture
00138         effects->pushRenderTarget(mRenderTarget);
00139     }
00140 #endif
00141 
00142     effects->prePaintScreen(data, time);
00143 }
00144 
00145 void ShaderEffect::postPaintScreen()
00146 {
00147     // Call the next effect.
00148     effects->postPaintScreen();
00149 
00150 #ifdef KWIN_HAVE_OPENGL_COMPOSITING
00151     if( mValid && mEnabled )
00152     {
00153         // Disable render texture
00154         assert( effects->popRenderTarget() == mRenderTarget );
00155         mTexture->bind();
00156 
00157         // Use the shader
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         // Render fullscreen quad with screen contents
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 } // namespace
00178 
00179 #endif

KWinLibraries

Skip menu "KWinLibraries"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal