Kaydet (Commit) bdce4e29 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl

opengl: push mask coords to the shaders along the image coords

If using the same texture to store the image and mask data (for
example when using texture atlas) the mask and image (RGB) coords
aren't the same anymore. With this commit we always define the mask
coords separately.

Change-Id: Ie33f87a6e9ab398972c6a3d5938e5f1364c82d36
üst b26ba85d
......@@ -19,6 +19,7 @@ $(eval $(call gb_Package_add_files,vcl_opengl_shader,$(LIBO_ETC_FOLDER)/opengl,\
convolutionFragmentShader.glsl \
linearGradientFragmentShader.glsl \
maskFragmentShader.glsl \
maskedTextureVertexShader.glsl \
maskedTextureFragmentShader.glsl \
radialGradientFragmentShader.glsl \
replaceColorFragmentShader.glsl \
......
......@@ -36,6 +36,7 @@ private:
GLuint mnPositionAttrib;
GLuint mnTexCoordAttrib;
GLuint mnAlphaCoordAttrib;
GLuint mnMaskCoordAttrib;
TextureList maTextures;
bool mbBlending;
......@@ -51,6 +52,7 @@ public:
void SetVertices( const GLvoid* pData );
void SetTextureCoord( const GLvoid* pData );
void SetAlphaCoord( const GLvoid* pData );
void SetMaskCoord(const GLvoid* pData);
void SetUniform1f( const OString& rName, GLfloat v1 );
void SetUniform2f( const OString& rName, GLfloat v1, GLfloat v2 );
......
......@@ -21,6 +21,7 @@ varying vec2 tex_coord;
// This mode makes the scaling work like maskedTextureFragmentShader.glsl
// (instead of like plain textureVertexShader.glsl).
#ifdef MASKED
varying vec2 mask_coord;
uniform sampler2D mask;
#endif
......@@ -41,7 +42,7 @@ void main(void)
#else
vec4 texel;
texel = texture2D( sampler, tex_coord.st + offset );
texel.a = 1.0 - texture2D( mask, tex_coord.st + offset ).r;
texel.a = 1.0 - texture2D( mask, mask_coord.st + offset ).r;
sum += texel;
#endif
offset.x += xstep;
......
......@@ -30,6 +30,7 @@ varying vec2 tex_coord;
// This mode makes the scaling work like maskedTextureFragmentShader.glsl
// (instead of like plain textureVertexShader.glsl).
#ifdef MASKED
varying vec2 mask_coord;
uniform sampler2D mask;
#endif
......
......@@ -9,14 +9,18 @@
varying vec2 tex_coord;
varying vec2 alpha_coord;
varying vec2 mask_coord;
uniform sampler2D sampler;
uniform sampler2D mask;
uniform sampler2D alpha;
void main() {
void main()
{
vec4 texel0, texel1, texel2;
texel0 = texture2D(sampler, tex_coord);
texel1 = texture2D(mask, tex_coord);
texel1 = texture2D(mask, mask_coord);
texel2 = texture2D(alpha, alpha_coord);
gl_FragColor = texel0;
......
......@@ -10,14 +10,17 @@
attribute vec4 position;
attribute vec2 tex_coord_in;
attribute vec2 alpha_coord_in;
attribute vec2 mask_coord_in;
varying vec2 tex_coord;
varying vec2 alpha_coord;
varying vec2 mask_coord;
uniform mat4 mvp;
void main() {
gl_Position = mvp * position;
tex_coord = tex_coord_in;
alpha_coord = alpha_coord_in;
mask_coord = mask_coord_in;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -9,14 +9,16 @@
/*precision mediump float;*/
varying vec2 tex_coord;
varying vec2 mask_coord;
uniform sampler2D texture; /* white background */
uniform sampler2D mask; /* black background */
void main() {
void main()
{
float alpha;
vec4 texel0, texel1;
texel0 = texture2D(texture, tex_coord);
texel1 = texture2D(mask, tex_coord);
texel1 = texture2D(mask, mask_coord);
alpha = 1.0 - abs(texel0.r - texel1.r);
if(alpha > 0.0)
gl_FragColor = texel1 / alpha;
......
......@@ -959,6 +959,9 @@ void OpenGLSalGraphicsImpl::DrawTransformedTexture(
"#define MASKED" ) )
return;
mpProgram->SetTexture( "mask", rMask );
GLfloat aMaskCoord[8];
rMask.GetWholeCoord(aMaskCoord);
mpProgram->SetMaskCoord(aMaskCoord);
rMask.SetFilter( GL_LINEAR );
mpProgram->SetBlendMode( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
}
......@@ -1027,25 +1030,39 @@ void OpenGLSalGraphicsImpl::DrawTextureDiff( OpenGLTexture& rTexture, OpenGLText
{
OpenGLZone aZone;
if( !UseProgram( "textureVertexShader", "diffTextureFragmentShader" ) )
if( !UseProgram( "maskedTextureVertexShader", "diffTextureFragmentShader" ) )
return;
mpProgram->SetTexture( "texture", rTexture );
mpProgram->SetTexture( "mask", rMask );
mpProgram->SetBlendMode( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
GLfloat aMaskCoord[8];
rMask.GetCoord(aMaskCoord, rPosAry, bInverted);
mpProgram->SetMaskCoord(aMaskCoord);
DrawTextureRect( rTexture, rPosAry, bInverted );
mpProgram->Clean();
}
void OpenGLSalGraphicsImpl::DrawTextureWithMask( OpenGLTexture& rTexture, OpenGLTexture& rMask, const SalTwoRect& pPosAry )
void OpenGLSalGraphicsImpl::DrawTextureWithMask( OpenGLTexture& rTexture, OpenGLTexture& rMask, const SalTwoRect& rPosAry )
{
OpenGLZone aZone;
if( !UseProgram( "textureVertexShader", "maskedTextureFragmentShader" ) )
if( !UseProgram( "maskedTextureVertexShader", "maskedTextureFragmentShader" ) )
return;
mpProgram->SetTexture( "sampler", rTexture );
mpProgram->SetTexture( "mask", rMask );
mpProgram->SetBlendMode( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
DrawTextureRect( rTexture, pPosAry );
GLfloat aTexCoord[8];
rTexture.GetCoord(aTexCoord, rPosAry);
mpProgram->SetTextureCoord(aTexCoord);
GLfloat aMaskCoord[8];
rMask.GetCoord(aMaskCoord, rPosAry);
mpProgram->SetMaskCoord(aMaskCoord);
DrawRect(rPosAry.mnDestX, rPosAry.mnDestY, rPosAry.mnDestWidth, rPosAry.mnDestHeight);
mpProgram->Clean();
}
......@@ -1053,14 +1070,20 @@ void OpenGLSalGraphicsImpl::DrawBlendedTexture( OpenGLTexture& rTexture, OpenGLT
{
OpenGLZone aZone;
GLfloat aTexCoord[8];
if( !UseProgram( "blendedTextureVertexShader", "blendedTextureFragmentShader" ) )
return;
mpProgram->SetTexture( "sampler", rTexture );
mpProgram->SetTexture( "mask", rMask );
mpProgram->SetTexture( "alpha", rAlpha );
rAlpha.GetCoord( aTexCoord, rPosAry );
mpProgram->SetAlphaCoord( aTexCoord );
GLfloat aAlphaCoord[8];
rAlpha.GetCoord(aAlphaCoord, rPosAry);
mpProgram->SetAlphaCoord(aAlphaCoord);
GLfloat aMaskCoord[8];
rMask.GetCoord(aMaskCoord, rPosAry);
mpProgram->SetMaskCoord(aMaskCoord);
mpProgram->SetBlendMode( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
DrawTextureRect( rTexture, rPosAry );
mpProgram->Clean();
......
......@@ -9,13 +9,15 @@
/*precision mediump float;*/
varying vec2 tex_coord;
varying vec2 mask_coord;
uniform sampler2D sampler;
uniform sampler2D mask;
void main() {
void main()
{
vec4 texel0, texel1;
texel0 = texture2D(sampler, tex_coord);
texel1 = texture2D(mask, tex_coord);
texel1 = texture2D(mask, mask_coord);
gl_FragColor = texel0;
gl_FragColor.a = 1.0 - texel1.r;
}
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
attribute vec4 position;
attribute vec2 tex_coord_in;
attribute vec2 mask_coord_in;
varying vec2 tex_coord;
varying vec2 mask_coord;
uniform mat4 mvp;
void main()
{
gl_Position = mvp * position;
tex_coord = tex_coord_in;
mask_coord = mask_coord_in;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -21,6 +21,7 @@ OpenGLProgram::OpenGLProgram() :
mnPositionAttrib( SAL_MAX_UINT32 ),
mnTexCoordAttrib( SAL_MAX_UINT32 ),
mnAlphaCoordAttrib( SAL_MAX_UINT32 ),
mnMaskCoordAttrib( SAL_MAX_UINT32 ),
mbBlending( false )
{
}
......@@ -112,6 +113,11 @@ void OpenGLProgram::SetAlphaCoord( const GLvoid* pData )
SetVertexAttrib( mnAlphaCoordAttrib, "alpha_coord_in", pData );
}
void OpenGLProgram::SetMaskCoord(const GLvoid* pData)
{
SetVertexAttrib(mnMaskCoordAttrib, "mask_coord_in", pData);
}
GLuint OpenGLProgram::GetUniformLocation( const OString& rName )
{
auto it = maUniformLocations.find( rName );
......
......@@ -9,15 +9,18 @@
attribute vec4 position;
attribute vec2 tex_coord_in;
attribute vec2 mask_coord_in;
uniform vec2 viewport;
uniform mat4 transform;
uniform mat4 mvp;
varying vec2 tex_coord;
varying vec2 mask_coord;
void main() {
vec4 pos = mvp * transform * position;
gl_Position = pos;
tex_coord = tex_coord_in;
mask_coord = mask_coord_in;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment