Add GLTexture::setSwizzle()

This method allows the caller to specify which component of a texel
is placed in each respective component of the vector returned to the
shader.
This commit is contained in:
Fredrik Höglund 2014-12-13 14:30:59 +01:00
parent 4d738b86ea
commit 7fd4cf0227
3 changed files with 44 additions and 0 deletions

View file

@ -46,6 +46,7 @@ bool GLTexturePrivate::s_supportsFramebufferObjects = false;
bool GLTexturePrivate::s_supportsARGB32 = false;
bool GLTexturePrivate::s_supportsUnpack = false;
bool GLTexturePrivate::s_supportsTextureStorage = false;
bool GLTexturePrivate::s_supportsTextureSwizzle = false;
uint GLTexturePrivate::s_textureObjectCounter = 0;
uint GLTexturePrivate::s_fbo = 0;
@ -234,11 +235,13 @@ void GLTexturePrivate::initStatic()
s_supportsFramebufferObjects = hasGLVersion(3, 0) ||
hasGLExtension("GL_ARB_framebuffer_object") || hasGLExtension(QByteArrayLiteral("GL_EXT_framebuffer_object"));
s_supportsTextureStorage = hasGLVersion(4, 2) || hasGLExtension(QByteArrayLiteral("GL_ARB_texture_storage"));
s_supportsTextureSwizzle = hasGLVersion(3, 3) || hasGLExtension(QByteArrayLiteral("GL_ARB_texture_swizzle"));
s_supportsARGB32 = true;
s_supportsUnpack = true;
} else {
s_supportsFramebufferObjects = true;
s_supportsTextureStorage = hasGLVersion(3, 0) || hasGLExtension(QByteArrayLiteral("GL_EXT_texture_storage"));
s_supportsTextureSwizzle = hasGLVersion(3, 0);
// QImage::Format_ARGB32_Premultiplied is a packed-pixel format, so it's only
// equivalent to GL_BGRA/GL_UNSIGNED_BYTE on little-endian systems.
@ -551,6 +554,21 @@ void GLTexture::setYInverted(bool inverted)
d->updateMatrix();
}
void GLTexture::setSwizzle(GLenum red, GLenum green, GLenum blue, GLenum alpha)
{
Q_D(GLTexture);
if (!GLPlatform::instance()->isGLES()) {
const GLuint swizzle[] = { red, green, blue, alpha };
glTexParameteriv(d->m_target, GL_TEXTURE_SWIZZLE_RGBA, (const GLint *) swizzle);
} else {
glTexParameteri(d->m_target, GL_TEXTURE_SWIZZLE_R, red);
glTexParameteri(d->m_target, GL_TEXTURE_SWIZZLE_G, green);
glTexParameteri(d->m_target, GL_TEXTURE_SWIZZLE_B, blue);
glTexParameteri(d->m_target, GL_TEXTURE_SWIZZLE_A, alpha);
}
}
int GLTexture::width() const
{
Q_D(const GLTexture);
@ -574,4 +592,9 @@ bool GLTexture::framebufferObjectSupported()
return GLTexturePrivate::s_supportsFramebufferObjects;
}
bool GLTexture::supportsSwizzle()
{
return GLTexturePrivate::s_supportsTextureSwizzle;
}
} // namespace KWin

View file

@ -74,6 +74,17 @@ public:
**/
void setYInverted(bool inverted);
/**
* Specifies which component of a texel is placed in each respective
* component of the vector returned to the shader.
*
* Valid values are GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_ONE and GL_ZERO.
*
* @see swizzleSupported()
* @since 5.2
*/
void setSwizzle(GLenum red, GLenum green, GLenum blue, GLenum alpha);
/**
* Returns a matrix that transforms texture coordinates of the given type,
* taking the texture target and the y-inversion flag into account.
@ -107,6 +118,15 @@ public:
static bool framebufferObjectSupported();
/**
* Returns true if texture swizzle is supported, and false otherwise
*
* Texture swizzle requires OpenGL 3.3, GL_ARB_texture_swizzle, or OpenGL ES 3.0.
*
* @since 5.2
*/
static bool supportsSwizzle();
protected:
QExplicitlySharedDataPointer<GLTexturePrivate> d_ptr;
GLTexture(GLTexturePrivate& dd);

View file

@ -73,6 +73,7 @@ public:
static bool s_supportsARGB32;
static bool s_supportsUnpack;
static bool s_supportsTextureStorage;
static bool s_supportsTextureSwizzle;
static GLuint s_fbo;
static uint s_textureObjectCounter;
private: