[Libreoffice-commits] core.git: vcl/inc vcl/opengl vcl/Package_opengl.mk

2016-05-23 Thread Tomaž Vajngerl
 vcl/Package_opengl.mk |4 +
 vcl/inc/opengl/program.hxx|   19 +++
 vcl/opengl/combinedFragmentShader.glsl|   45 
 vcl/opengl/combinedTextureFragmentShader.glsl |   64 
 vcl/opengl/combinedTextureVertexShader.glsl   |   32 
 vcl/opengl/combinedVertexShader.glsl  |   47 +
 vcl/opengl/gdiimpl.cxx|   69 --
 vcl/opengl/program.cxx|   18 ++
 8 files changed, 273 insertions(+), 25 deletions(-)

New commits:
commit 3cac38b2311538a0aecca765eb62c30c5098a85c
Author: Tomaž Vajngerl 
Date:   Fri May 20 14:59:24 2016 +0900

opengl: combined shaders to reduce shader switching

Combine most common shaders for non-texture drawing and texture
drawing into two combined shaders. Inside the shader we switch
between the code paths with if statements.

Using if statements (or any other branching statements) is
discouraged inside shaders but on the other hand we reduce program
state changes if we have less shader changes - which is more
important for us as we want to push more work to the GPU.

Change-Id: I6701b93faa9b0f55dd0af6d983ce4c2de4539c70
Reviewed-on: https://gerrit.libreoffice.org/25357
Reviewed-by: Tomaž Vajngerl 
Tested-by: Tomaž Vajngerl 

diff --git a/vcl/Package_opengl.mk b/vcl/Package_opengl.mk
index a0f6e9a..2fa917e 100644
--- a/vcl/Package_opengl.mk
+++ b/vcl/Package_opengl.mk
@@ -21,6 +21,10 @@ $(eval $(call 
gb_Package_add_files,vcl_opengl_shader,$(LIBO_ETC_FOLDER)/opengl,\
 invert50FragmentShader.glsl \
convolutionFragmentShader.glsl \
linearGradientFragmentShader.glsl \
+   combinedTextureFragmentShader.glsl \
+   combinedTextureVertexShader.glsl \
+   combinedFragmentShader.glsl \
+   combinedVertexShader.glsl \
lineFragmentShader.glsl \
lineVertexShader.glsl \
maskFragmentShader.glsl \
diff --git a/vcl/inc/opengl/program.hxx b/vcl/inc/opengl/program.hxx
index 5944c72..2fab98c 100644
--- a/vcl/inc/opengl/program.hxx
+++ b/vcl/inc/opengl/program.hxx
@@ -27,6 +27,21 @@
 typedef std::unordered_map< OString, GLuint, OStringHash > UniformCache;
 typedef std::list< OpenGLTexture > TextureList;
 
+enum class TextureShaderType
+{
+Normal = 0,
+Blend,
+Masked,
+Diff,
+MaskedColor
+};
+
+enum class DrawShaderType
+{
+Normal = 0,
+Line
+};
+
 class VCL_PLUGIN_PUBLIC OpenGLProgram
 {
 private:
@@ -78,6 +93,10 @@ public:
 void SetTransform( const OString& rName, const OpenGLTexture& rTexture,
const basegfx::B2DPoint& rNull, const 
basegfx::B2DPoint& rX,
const basegfx::B2DPoint& rY );
+void SetIdentityTransform(const OString& rName);
+void SetShaderType(TextureShaderType eTextureShaderType);
+void SetShaderType(DrawShaderType eDrawShaderType);
+
 void SetBlendMode( GLenum nSFactor, GLenum nDFactor );
 
 void ApplyMatrix(float fWidth, float fHeight, float fPixelOffset = 0.0f);
diff --git a/vcl/opengl/combinedFragmentShader.glsl 
b/vcl/opengl/combinedFragmentShader.glsl
new file mode 100644
index 000..c44e75c
--- /dev/null
+++ b/vcl/opengl/combinedFragmentShader.glsl
@@ -0,0 +1,45 @@
+/* -*- 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/.
+ */
+
+varying float fade_factor; // 0->1 fade factor used for AA
+uniform vec4 color;
+
+uniform float line_width;
+uniform float feather;
+
+#define TYPE_NORMAL 0
+#define TYPE_LINE   1
+
+uniform int type;
+
+void main()
+{
+float alpha = 1.0;
+
+if (type == TYPE_LINE)
+{
+float start = (line_width / 2.0) - feather; // where we start to apply 
alpha
+float end = (line_width / 2.0) + feather; // where we end to apply 
alpha
+
+// Calculate the multiplier so we can transform the 0->1 fade factor
+// to take feather and line width into account.
+float multiplied = 1.0 / (1.0 - (start / end));
+
+float dist = (1.0 - abs(fade_factor)) * multiplied;
+
+alpha = clamp(dist, 0.0, 1.0);
+}
+
+vec4 result_color = color;
+result_color.a = result_color.a * alpha;
+
+gl_FragColor = result_color;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/opengl/combinedTextureFragmentShader.glsl 
b/vcl/opengl/combinedTextureFragmentShader.glsl
new file mode 100644
index 000..d8864cf
--- /dev/null
+++ b/vcl/opengl/combinedTextureFragmentShader.glsl
@@ -0,0 +1,64 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: 

[Libreoffice-commits] core.git: vcl/inc vcl/opengl vcl/Package_opengl.mk vcl/source vcl/workben

2016-02-09 Thread Tomaž Vajngerl
 vcl/Package_opengl.mk   |1 
 vcl/inc/impbmp.hxx  |1 
 vcl/inc/opengl/salbmp.hxx   |1 
 vcl/inc/salbmp.hxx  |4 +++
 vcl/opengl/greyscaleFragmentShader.glsl |   18 
 vcl/opengl/salbmp.cxx   |   36 
 vcl/source/gdi/bitmap3.cxx  |   17 +++
 vcl/source/gdi/impbmp.cxx   |   10 
 vcl/workben/vcldemo.cxx |   13 +++
 9 files changed, 101 insertions(+)

New commits:
commit ad8d6d9ef53717e072005fe518ba8ded61e96711
Author: Tomaž Vajngerl 
Date:   Mon Feb 8 22:25:59 2016 +0100

tdf#97666 -opengl: convert the bitmap to 8bit grays using GL shader

Change-Id: I4d48d29ab752814f71c697a201e70a26ae937775
Reviewed-on: https://gerrit.libreoffice.org/3
Reviewed-by: Tor Lillqvist 
Tested-by: Tor Lillqvist 

diff --git a/vcl/Package_opengl.mk b/vcl/Package_opengl.mk
index 9d42502..b8851df 100644
--- a/vcl/Package_opengl.mk
+++ b/vcl/Package_opengl.mk
@@ -17,6 +17,7 @@ $(eval $(call 
gb_Package_add_files,vcl_opengl_shader,$(LIBO_ETC_FOLDER)/opengl,\
blendedTextureVertexShader.glsl \
dumbVertexShader.glsl \
diffTextureFragmentShader.glsl \
+   greyscaleFragmentShader.glsl \
 invert50FragmentShader.glsl \
convolutionFragmentShader.glsl \
linearGradientFragmentShader.glsl \
diff --git a/vcl/inc/impbmp.hxx b/vcl/inc/impbmp.hxx
index feb94a8..5787b3f 100644
--- a/vcl/inc/impbmp.hxx
+++ b/vcl/inc/impbmp.hxx
@@ -72,6 +72,7 @@ public:
 
 boolImplScale( const double& rScaleX, const double& 
rScaleY, BmpScaleFlag nScaleFlag );
 boolImplReplace( const Color& rSearchColor, const Color& 
rReplaceColor, sal_uLong nTol );
+boolImplConvert( BmpConversion eConversion );
 };
 
 #endif // INCLUDED_VCL_INC_IMPBMP_HXX
diff --git a/vcl/inc/opengl/salbmp.hxx b/vcl/inc/opengl/salbmp.hxx
index 98e09b3..7577f9f 100644
--- a/vcl/inc/opengl/salbmp.hxx
+++ b/vcl/inc/opengl/salbmp.hxx
@@ -80,6 +80,7 @@ public:
 
 boolScale( const double& rScaleX, const double& rScaleY, 
BmpScaleFlag nScaleFlag ) override;
 boolReplace( const Color& rSearchColor, const Color& 
rReplaceColor, sal_uLong nTol ) override;
+boolConvertToGreyscale() override;
 
 public:
 
diff --git a/vcl/inc/salbmp.hxx b/vcl/inc/salbmp.hxx
index 574b11c..305f8a6 100644
--- a/vcl/inc/salbmp.hxx
+++ b/vcl/inc/salbmp.hxx
@@ -69,6 +69,10 @@ public:
 virtual boolScale( const double& rScaleX, const double& 
rScaleY, BmpScaleFlag nScaleFlag ) = 0;
 virtual boolReplace( const Color& rSearchColor, const Color& 
rReplaceColor, sal_uLong nTol ) = 0;
 
+virtual boolConvertToGreyscale()
+{
+return false;
+}
 
 void GetChecksum(ChecksumType& rChecksum) const
 {
diff --git a/vcl/opengl/greyscaleFragmentShader.glsl 
b/vcl/opengl/greyscaleFragmentShader.glsl
new file mode 100644
index 000..758109e
--- /dev/null
+++ b/vcl/opengl/greyscaleFragmentShader.glsl
@@ -0,0 +1,18 @@
+/* -*- 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/.
+ */
+
+varying vec2 tex_coord;
+uniform sampler2D sampler;
+
+void main() {
+vec4 texel = texture2D(sampler, tex_coord);
+gl_FragColor = vec4(vec3(dot(texel.rgb, vec3(0.301, 0.591, 0.108))), 1.0);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/opengl/salbmp.cxx b/vcl/opengl/salbmp.cxx
index 703aedc..683ca5f 100644
--- a/vcl/opengl/salbmp.cxx
+++ b/vcl/opengl/salbmp.cxx
@@ -902,4 +902,40 @@ bool OpenGLSalBitmap::Replace( const Color& rSearchColor, 
const Color& rReplaceC
 return true;
 }
 
+// Convert texture to greyscale and adjust bitmap metadata
+bool OpenGLSalBitmap::ConvertToGreyscale()
+{
+VCL_GL_INFO("::ConvertToGreyscale");
+
+// avoid re-converting to 8bits.
+if ( mnBits == 8 && maPalette == Bitmap::GetGreyPalette(256) )
+return false;
+
+OpenGLZone aZone;
+rtl::Reference xContext = OpenGLContext::getVCLContext();
+
+OpenGLFramebuffer* pFramebuffer;
+OpenGLProgram* pProgram;
+
+GetTexture();
+pProgram = xContext->UseProgram("textureVertexShader", 
"greyscaleFragmentShader");
+
+if (!pProgram)
+return false;
+
+OpenGLTexture aNewTex(mnWidth, mnHeight);
+pFramebuffer = xContext->AcquireFramebuffer(aNewTex);
+pProgram->SetTexture("sampler", maTexture);
+pProgram->DrawTexture(maTexture);
+pProgram->Clean();
+
+OpenGLContext::ReleaseFramebuffer( 

[Libreoffice-commits] core.git: vcl/inc vcl/opengl vcl/Package_opengl.mk vcl/workben

2015-12-31 Thread Michael Meeks
 vcl/Package_opengl.mk  |1 
 vcl/inc/openglgdiimpl.hxx  |3 +
 vcl/opengl/gdiimpl.cxx |   60 ++---
 vcl/opengl/invert50FragmentShader.glsl |   23 
 vcl/workben/vcldemo.cxx|   11 +-
 5 files changed, 62 insertions(+), 36 deletions(-)

New commits:
commit e81c4d3ea00949cb4d8c3f44e09e70b19eebb826
Author: Michael Meeks 
Date:   Thu Dec 31 21:19:48 2015 +

tdf#95507 - implement opengl / 50% invert method.

Change-Id: I8488cb8e8074831a6f81e6c8c122462c9819d25d
Reviewed-on: https://gerrit.libreoffice.org/21025
Reviewed-by: Michael Meeks 
Tested-by: Michael Meeks 

diff --git a/vcl/Package_opengl.mk b/vcl/Package_opengl.mk
index df3520c..9d42502 100644
--- a/vcl/Package_opengl.mk
+++ b/vcl/Package_opengl.mk
@@ -17,6 +17,7 @@ $(eval $(call 
gb_Package_add_files,vcl_opengl_shader,$(LIBO_ETC_FOLDER)/opengl,\
blendedTextureVertexShader.glsl \
dumbVertexShader.glsl \
diffTextureFragmentShader.glsl \
+invert50FragmentShader.glsl \
convolutionFragmentShader.glsl \
linearGradientFragmentShader.glsl \
maskFragmentShader.glsl \
diff --git a/vcl/inc/openglgdiimpl.hxx b/vcl/inc/openglgdiimpl.hxx
index a81b1e8..63bebdc 100644
--- a/vcl/inc/openglgdiimpl.hxx
+++ b/vcl/inc/openglgdiimpl.hxx
@@ -111,7 +111,8 @@ public:
 bool UseSolid( SalColor nColor );
 bool UseSolidAA( SalColor nColor, double fTransparency );
 bool UseSolidAA( SalColor nColor );
-bool UseInvert();
+bool UseInvert50();
+bool UseInvert(SalInvert nFlags);
 
 void DrawPoint( long nX, long nY );
 void DrawLine( double nX1, double nY1, double nX2, double nY2 );
diff --git a/vcl/opengl/gdiimpl.cxx b/vcl/opengl/gdiimpl.cxx
index 549f8ae..21ff211 100644
--- a/vcl/opengl/gdiimpl.cxx
+++ b/vcl/opengl/gdiimpl.cxx
@@ -551,6 +551,13 @@ bool OpenGLSalGraphicsImpl::UseSolid( SalColor nColor, 
double fTransparency )
 return true;
 }
 
+bool OpenGLSalGraphicsImpl::UseInvert50()
+{
+if( !UseProgram( "dumbVertexShader", "invert50FragmentShader" ) )
+return false;
+return true;
+}
+
 bool OpenGLSalGraphicsImpl::UseSolid( SalColor nColor )
 {
 return UseSolid( nColor, 0.0f );
@@ -575,13 +582,24 @@ bool OpenGLSalGraphicsImpl::UseSolidAA( SalColor nColor )
 return UseSolidAA( nColor, 0.0 );
 }
 
-bool OpenGLSalGraphicsImpl::UseInvert()
+bool OpenGLSalGraphicsImpl::UseInvert( SalInvert nFlags )
 {
 OpenGLZone aZone;
 
-if( !UseSolid( MAKE_SALCOLOR( 255, 255, 255 ) ) )
-return false;
-mpProgram->SetBlendMode( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
+if( ( nFlags & SAL_INVERT_50 ) ||
+( nFlags & SAL_INVERT_TRACKFRAME ) )
+{
+if( !UseInvert50() )
+return false;
+mpProgram->SetBlendMode( GL_ONE_MINUS_DST_COLOR,
+ GL_ONE_MINUS_SRC_COLOR );
+}
+else
+{
+if( !UseSolid( MAKE_SALCOLOR( 255, 255, 255 ) ) )
+return false;
+mpProgram->SetBlendMode( GL_ONE_MINUS_DST_COLOR, GL_ZERO );
+}
 return true;
 }
 
@@ -1742,25 +1760,10 @@ void OpenGLSalGraphicsImpl::invert(
 long nWidth, long nHeight,
 SalInvert nFlags)
 {
-// TODO Figure out what are those:
-//   * SAL_INVERT_50 (50/50 pattern?)
-//   * SAL_INVERT_TRACKFRAME (dash-line rectangle?)
-
 PreDraw();
 
-if( nFlags & SAL_INVERT_TRACKFRAME )
-{
-
-}
-else if( nFlags & SAL_INVERT_50 )
-{
-
-}
-else // just invert
-{
-if( UseInvert() )
-DrawRect( nX, nY, nWidth, nHeight );
-}
+if( UseInvert( nFlags ) )
+DrawRect( nX, nY, nWidth, nHeight );
 
 PostDraw();
 }
@@ -1769,19 +1772,8 @@ void OpenGLSalGraphicsImpl::invert( sal_uInt32 nPoints, 
const SalPoint* pPtAry,
 {
 PreDraw();
 
-if( nFlags & SAL_INVERT_TRACKFRAME )
-{
-
-}
-else if( nFlags & SAL_INVERT_50 )
-{
-
-}
-else // just invert
-{
-if( UseInvert() )
-DrawPolygon( nPoints, pPtAry );
-}
+if( UseInvert( nFlags ) )
+DrawPolygon( nPoints, pPtAry );
 
 PostDraw();
 }
diff --git a/vcl/opengl/invert50FragmentShader.glsl 
b/vcl/opengl/invert50FragmentShader.glsl
new file mode 100644
index 000..76f3e1f
--- /dev/null
+++ b/vcl/opengl/invert50FragmentShader.glsl
@@ -0,0 +1,23 @@
+/* -*- 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/.
+ */
+
+/*precision mediump float;*/
+
+void main() {
+vec2 tex_mod = mod(gl_FragCoord, 2);
+bool bLeft = tex_mod.x > 0 &&