Remove textureWidth/textureHeight from all Shaders

The uniforms textureWidth and textureHeight were only needed for
normal windows. For everything else it was just 1.0/1.0, that is
normalized.

The makeArrays method is changed to produce normalized texcoords
obsoleting the need for these uniforms. So two uniforms less, one
calculation in vertex shaders less and many many lines of code
removed.

At the same time makeArrays is also adjusted to take care of
yInverted of the texture, which is needed as we no longer can use
the enableUnnormalizedTexCoords which did the yInverted transformation.

REVIEW: 101646
icc-effect-5.14.5
Martin Gräßlin 2011-06-16 20:33:06 +02:00
parent 16771b043c
commit a05ad98896
14 changed files with 40 additions and 116 deletions

View File

@ -25,8 +25,6 @@ uniform float width;
uniform float cubeAngle;
uniform float xCoord;
uniform float timeLine;
uniform float textureWidth;
uniform float textureHeight;
attribute vec4 vertex;
attribute vec2 texCoord;
@ -35,7 +33,7 @@ varying vec2 varyingTexCoords;
void main()
{
varyingTexCoords = texCoord / vec2(textureWidth, textureHeight);
varyingTexCoords = texCoord;
vec4 transformedVertex = vec4(vertex.x - ( width - xCoord ), vertex.yzw);
float radian = radians(cubeAngle);
float radius = (width)*tan(radian);

View File

@ -26,8 +26,6 @@ uniform float height;
uniform float cubeAngle;
uniform vec2 u_offset;
uniform float timeLine;
uniform float textureWidth;
uniform float textureHeight;
attribute vec4 vertex;
attribute vec2 texCoord;
@ -36,7 +34,7 @@ varying vec2 varyingTexCoords;
void main()
{
varyingTexCoords = texCoord / vec2(textureWidth, textureHeight);
varyingTexCoords = texCoord;
vec4 transformedVertex = vertex;
transformedVertex.x = transformedVertex.x - width;
transformedVertex.y = transformedVertex.y - height;

View File

@ -1,7 +1,5 @@
uniform sampler2D sampler;
uniform vec4 modulation;
uniform float textureWidth;
uniform float textureHeight;
uniform float saturation;
uniform int u_forceAlpha;

View File

@ -358,8 +358,6 @@ void PresentWindowsEffect::paintWindow(EffectWindow *w, int mask, QRegion region
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
if (effects->compositingType() == KWin::OpenGLCompositing && data.shader) {
const float a = 0.9 * data.opacity * m_decalOpacity * 0.75;
data.shader->setUniform(GLShader::TextureWidth, 1.0f);
data.shader->setUniform(GLShader::TextureHeight, 1.0f);
data.shader->setUniform(GLShader::ModulationConstant, QVector4D(a, a, a, a));
}
#endif
@ -372,8 +370,6 @@ void PresentWindowsEffect::paintWindow(EffectWindow *w, int mask, QRegion region
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
if (effects->compositingType() == KWin::OpenGLCompositing && data.shader) {
const float a = 0.9 * data.opacity * m_decalOpacity * 0.75;
data.shader->setUniform(GLShader::TextureWidth, 1.0f);
data.shader->setUniform(GLShader::TextureHeight, 1.0f);
data.shader->setUniform(GLShader::ModulationConstant, QVector4D(a, a, a, a));
}
#endif

View File

@ -1,6 +1,4 @@
uniform sampler2D sampler;
uniform float textureWidth;
uniform float textureHeight;
uniform vec4 u_color;
varying vec2 varyingTexCoords;

View File

@ -91,17 +91,7 @@ void TaskbarThumbnailEffect::paintWindow(EffectWindow* w, int mask, QRegion regi
#ifdef KWIN_HAVE_OPENGL_COMPOSITING
if (effects->compositingType() == KWin::OpenGLCompositing) {
if (data.shader) {
// there is a shader - update texture width and height
int texw = thumbw->width();
int texh = thumbw->height();
if (!GLTexture::NPOTTextureSupported()) {
kWarning(1212) << "NPOT textures not supported, wasting some memory" ;
texw = nearestPowerOfTwo(texw);
texh = nearestPowerOfTwo(texh);
}
thumbData.shader = data.shader;
thumbData.shader->setTextureWidth((float)texw);
thumbData.shader->setTextureHeight((float)texh);
}
} // if ( effects->compositingType() == KWin::OpenGLCompositing )
#endif

View File

@ -547,7 +547,7 @@ WindowQuadList WindowQuadList::makeRegularGrid(int xSubdivisions, int ySubdivisi
return ret;
}
void WindowQuadList::makeArrays(float** vertices, float** texcoords) const
void WindowQuadList::makeArrays(float** vertices, float** texcoords, const QSizeF& size, bool yInverted) const
{
*vertices = new float[ count() * 6 * 2 ];
*texcoords = new float[ count() * 6 * 2 ];
@ -569,18 +569,33 @@ void WindowQuadList::makeArrays(float** vertices, float** texcoords) const
*vpos++ = at(i)[ 1 ].x();
*vpos++ = at(i)[ 1 ].y();
*tpos++ = at(i)[ 1 ].tx;
*tpos++ = at(i)[ 1 ].ty;
*tpos++ = at(i)[ 0 ].tx;
*tpos++ = at(i)[ 0 ].ty;
*tpos++ = at(i)[ 3 ].tx;
*tpos++ = at(i)[ 3 ].ty;
*tpos++ = at(i)[ 3 ].tx;
*tpos++ = at(i)[ 3 ].ty;
*tpos++ = at(i)[ 2 ].tx;
*tpos++ = at(i)[ 2 ].ty;
*tpos++ = at(i)[ 1 ].tx;
*tpos++ = at(i)[ 1 ].ty;
if (yInverted) {
*tpos++ = at(i)[ 1 ].tx / size.width();
*tpos++ = at(i)[ 1 ].ty / size.height();
*tpos++ = at(i)[ 0 ].tx / size.width();
*tpos++ = at(i)[ 0 ].ty / size.height();
*tpos++ = at(i)[ 3 ].tx / size.width();
*tpos++ = at(i)[ 3 ].ty / size.height();
*tpos++ = at(i)[ 3 ].tx / size.width();
*tpos++ = at(i)[ 3 ].ty / size.height();
*tpos++ = at(i)[ 2 ].tx / size.width();
*tpos++ = at(i)[ 2 ].ty / size.height();
*tpos++ = at(i)[ 1 ].tx / size.width();
*tpos++ = at(i)[ 1 ].ty / size.height();
} else {
*tpos++ = at(i)[ 1 ].tx / size.width();
*tpos++ = 1.0f - at(i)[ 1 ].ty / size.height();
*tpos++ = at(i)[ 0 ].tx / size.width();
*tpos++ = 1.0f - at(i)[ 0 ].ty / size.height();
*tpos++ = at(i)[ 3 ].tx / size.width();
*tpos++ = 1.0f - at(i)[ 3 ].ty / size.height();
*tpos++ = at(i)[ 3 ].tx / size.width();
*tpos++ = 1.0f - at(i)[ 3 ].ty / size.height();
*tpos++ = at(i)[ 2 ].tx / size.width();
*tpos++ = 1.0f - at(i)[ 2 ].ty / size.height();
*tpos++ = at(i)[ 1 ].tx / size.width();
*tpos++ = 1.0f - at(i)[ 1 ].ty / size.height();
}
}
}

View File

@ -1332,7 +1332,7 @@ public:
WindowQuadList select(WindowQuadType type) const;
WindowQuadList filterOut(WindowQuadType type) const;
bool smoothNeeded() const;
void makeArrays(float** vertices, float** texcoords) const;
void makeArrays(float** vertices, float** texcoords, const QSizeF &size, bool yInverted) const;
bool isTransformed() const;
};

View File

@ -458,8 +458,6 @@ void GLTexture::render(QRegion region, const QRect& rect)
GLShader *shader = ShaderManager::instance()->getBoundShader();
shader->setUniform(GLShader::Offset, QVector2D(rect.x(), rect.y()));
shader->setUniform(GLShader::WindowTransformation, translation);
shader->setUniform(GLShader::TextureWidth, 1.0f);
shader->setUniform(GLShader::TextureHeight, 1.0f);
} else {
pushMatrix(translation);
}
@ -705,8 +703,6 @@ GLShader::GLShader()
: mProgram(0)
, mValid(false)
, mLocationsResolved(false)
, mTextureWidth(-1.0f)
, mTextureHeight(-1.0f)
{
}
@ -714,8 +710,6 @@ GLShader::GLShader(const QString& vertexfile, const QString& fragmentfile)
: mProgram(0)
, mValid(false)
, mLocationsResolved(false)
, mTextureWidth(-1.0f)
, mTextureHeight(-1.0f)
{
loadFromFiles(vertexfile, fragmentfile);
}
@ -871,8 +865,6 @@ void GLShader::resolveLocations()
mVec4Location[ModulationConstant] = uniformLocation("modulation");
mFloatLocation[Saturation] = uniformLocation("saturation");
mFloatLocation[TextureWidth] = uniformLocation("textureWidth");
mFloatLocation[TextureHeight] = uniformLocation("textureHeight");
mIntLocation[AlphaToOne] = uniformLocation("u_forceAlpha");
@ -1034,26 +1026,6 @@ bool GLShader::setAttribute(const char* name, float value)
return (location >= 0);
}
void GLShader::setTextureHeight(float height)
{
mTextureHeight = height;
}
void GLShader::setTextureWidth(float width)
{
mTextureWidth = width;
}
float GLShader::textureHeight()
{
return mTextureHeight;
}
float GLShader::textureWidth()
{
return mTextureWidth;
}
QMatrix4x4 GLShader::getUniformMatrix4x4(const char* name)
{
int location = uniformLocation(name);
@ -1335,10 +1307,6 @@ void ShaderManager::resetShader(ShaderType type)
shader->setUniform(GLShader::Saturation, 1.0f);
shader->setUniform(GLShader::AlphaToOne, 0);
// TODO: has to become textureSize
shader->setUniform(GLShader::TextureWidth, 1.0f);
shader->setUniform(GLShader::TextureHeight, 1.0f);
}
/*** GLRenderTarget ***/

View File

@ -280,12 +280,6 @@ public:
**/
QMatrix4x4 getUniformMatrix4x4(const char* name);
void setTextureWidth(float width);
void setTextureHeight(float height);
float textureWidth();
float textureHeight();
enum MatrixUniform {
TextureMatrix = 0,
ProjectionMatrix,
@ -307,8 +301,6 @@ public:
enum FloatUniform {
Saturation,
TextureWidth,
TextureHeight,
FloatUniformCount
};
@ -341,8 +333,6 @@ private:
int mVec4Location[Vec4UniformCount];
int mFloatLocation[FloatUniformCount];
int mIntLocation[IntUniformCount];
float mTextureWidth;
float mTextureHeight;
friend class ShaderManager;
};

View File

@ -3,9 +3,6 @@ uniform mat4 modelview;
uniform mat4 screenTransformation;
uniform mat4 windowTransformation;
uniform float textureWidth;
uniform float textureHeight;
// passed in vertex - only x and y are used
attribute vec4 vertex;
// passed in texCoords - to be forwarded
@ -15,6 +12,6 @@ attribute vec2 texCoord;
varying vec2 varyingTexCoords;
void main() {
varyingTexCoords = texCoord / vec2(textureWidth, textureHeight);
varyingTexCoords = texCoord;
gl_Position = projection*(modelview*screenTransformation*windowTransformation)*vertex;
}

View File

@ -2,9 +2,6 @@ uniform mat4 projection;
// offset of the window/texture to be rendered
uniform vec2 offset;
uniform float textureWidth;
uniform float textureHeight;
// passed in vertex - only x and y are used
attribute vec4 vertex;
// passed in texCoords - to be forwarded
@ -14,6 +11,6 @@ attribute vec2 texCoord;
varying vec2 varyingTexCoords;
void main() {
varyingTexCoords = texCoord / vec2(textureWidth, textureHeight);
varyingTexCoords = texCoord;
gl_Position = projection*vec4(vertex.xy + offset, vertex.zw);
}

View File

@ -585,16 +585,14 @@ void SceneOpenGL::Window::performPaint(int mask, QRegion region, WindowPaintData
// paint the content
if (!(mask & PAINT_DECORATION_ONLY)) {
texture.bind();
texture.enableUnnormalizedTexCoords();
prepareStates(Content, data.opacity * data.contents_opacity, data.brightness, data.saturation, data.shader);
renderQuads(mask, region, data.quads.select(WindowQuadContents));
renderQuads(mask, region, data.quads.select(WindowQuadContents), toplevel->size(), texture.getYInverted());
restoreStates(Content, data.opacity * data.contents_opacity, data.brightness, data.saturation, data.shader);
texture.disableUnnormalizedTexCoords();
texture.unbind();
#ifndef KWIN_HAVE_OPENGLES
if (static_cast<SceneOpenGL*>(scene)->debug) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
renderQuads(mask, region, data.quads.select(WindowQuadContents));
renderQuads(mask, region, data.quads.select(WindowQuadContents), toplevel->size(), texture.getYInverted());
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
#endif
@ -649,10 +647,6 @@ void SceneOpenGL::Window::paintDecoration(const QPixmap* decoration, TextureType
prepareStates(decorationType, data.opacity * data.decoration_opacity, data.brightness, data.saturation, data.shader);
makeDecorationArrays(quads, rect, decorationTexture->getYInverted());
if (data.shader) {
data.shader->setUniform(GLShader::TextureWidth, 1.0f);
data.shader->setUniform(GLShader::TextureHeight, 1.0f);
}
GLVertexBuffer::streamingBuffer()->render(region, GL_TRIANGLES);
restoreStates(decorationType, data.opacity * data.decoration_opacity, data.brightness, data.saturation, data.shader);
decorationTexture->unbind();
@ -679,17 +673,13 @@ void SceneOpenGL::Window::paintShadow(WindowQuadType type, const QRegion &region
texture->setWrapMode(GL_CLAMP_TO_EDGE);
texture->bind();
prepareStates(Shadow, data.opacity, data.brightness, data.saturation, data.shader, texture);
if (data.shader) {
data.shader->setUniform(GLShader::TextureWidth, 1.0f);
data.shader->setUniform(GLShader::TextureHeight, 1.0f);
}
renderQuads(0, region, quads);
renderQuads(0, region, quads, QSizeF(1.0, 1.0), texture->getYInverted());
restoreStates(Shadow, data.opacity, data.brightness, data.saturation, data.shader, texture);
texture->unbind();
#ifndef KWIN_HAVE_OPENGLES
if (static_cast<SceneOpenGL*>(scene)->debug) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
renderQuads(0, region, quads);
renderQuads(0, region, quads, QSizeF(1.0, 1.0), texture->getYInverted());
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
#endif
@ -748,14 +738,14 @@ void SceneOpenGL::Window::makeDecorationArrays(const WindowQuadList& quads, cons
GLVertexBuffer::streamingBuffer()->setData(quads.count() * 6, 2, vertices.data(), texcoords.data());
}
void SceneOpenGL::Window::renderQuads(int, const QRegion& region, const WindowQuadList& quads)
void SceneOpenGL::Window::renderQuads(int, const QRegion& region, const WindowQuadList& quads, const QSizeF &size, bool yInverted)
{
if (quads.isEmpty())
return;
// Render geometry
float* vertices;
float* texcoords;
quads.makeArrays(&vertices, &texcoords);
quads.makeArrays(&vertices, &texcoords, size, yInverted);
GLVertexBuffer::streamingBuffer()->setData(quads.count() * 6, 2, vertices, texcoords);
GLVertexBuffer::streamingBuffer()->render(region, GL_TRIANGLES);
delete[] vertices;
@ -826,14 +816,6 @@ void SceneOpenGL::Window::prepareShaderRenderStates(TextureType type, double opa
shader->setUniform(GLShader::ModulationConstant, QVector4D(rgb, rgb, rgb, a));
shader->setUniform(GLShader::Saturation, saturation);
shader->setUniform(GLShader::AlphaToOne, opaque ? 1 : 0);
const float texw = shader->textureWidth();
const float texh = shader->textureHeight();
// setting texture width and height stored in shader
// only set if it is set by an effect that is not negative
shader->setUniform(GLShader::TextureWidth, texw >= 0.0 ? texw : toplevel->width());
shader->setUniform(GLShader::TextureHeight, texh >= 0.0 ? texh : toplevel->height());
}
void SceneOpenGL::Window::prepareRenderStates(TextureType type, double opacity, double brightness, double saturation, Texture *tex)
@ -1167,9 +1149,6 @@ void SceneOpenGL::EffectFrame::render(QRegion region, double opacity, double fra
shader->setUniform(GLShader::ModulationConstant, QVector4D(1.0, 1.0, 1.0, 1.0));
shader->setUniform(GLShader::Saturation, 1.0f);
shader->setUniform(GLShader::AlphaToOne, 0);
shader->setUniform(GLShader::TextureWidth, 1.0f);
shader->setUniform(GLShader::TextureHeight, 1.0f);
}
#ifndef KWIN_HAVE_OPENGLES

View File

@ -169,7 +169,7 @@ protected:
void paintDecoration(const QPixmap* decoration, TextureType decorationType, const QRegion& region, const QRect& rect, const WindowPaintData& data, const WindowQuadList& quads, bool updateDeco);
void paintShadow(WindowQuadType type, const QRegion &region, const WindowPaintData &data);
void makeDecorationArrays(const WindowQuadList& quads, const QRect& rect, bool y_inverted) const;
void renderQuads(int mask, const QRegion& region, const WindowQuadList& quads);
void renderQuads(int, const QRegion& region, const KWin::WindowQuadList& quads, const QSizeF& size, bool yInverted);
void prepareStates(TextureType type, double opacity, double brightness, double saturation, GLShader* shader);
void prepareStates(TextureType type, double opacity, double brightness, double saturation, GLShader* shader, Texture *texture);
void prepareRenderStates(TextureType type, double opacity, double brightness, double saturation, Texture *tex);