GaussianDirectionalBlur.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. Item {
  42. id: rootItem
  43. property variant source
  44. property real deviation: (radius + 1) / 3.3333
  45. property real radius: 0.0
  46. property int maximumRadius: 0
  47. property real horizontalStep: 0.0
  48. property real verticalStep: 0.0
  49. property bool transparentBorder: false
  50. property bool cached: false
  51. property bool enableColor: false
  52. property color color: "white"
  53. property real spread: 0.0
  54. property bool enableMask: false
  55. property variant maskSource
  56. SourceProxy {
  57. id: sourceProxy
  58. input: rootItem.source
  59. }
  60. SourceProxy {
  61. id: maskSourceProxy
  62. input: rootItem.maskSource
  63. }
  64. ShaderEffectSource {
  65. id: cacheItem
  66. anchors.fill: rootItem
  67. visible: rootItem.cached
  68. smooth: true
  69. sourceItem: shaderItem
  70. live: true
  71. hideSource: visible
  72. }
  73. ShaderEffect {
  74. id: shaderItem
  75. property variant source: sourceProxy.output
  76. property real deviation: Math.max(0.1, rootItem.deviation)
  77. property real radius: rootItem.radius
  78. property int maxRadius: rootItem.maximumRadius
  79. property bool transparentBorder: rootItem.transparentBorder
  80. property real gaussianSum: 0.0
  81. property real startIndex: 0.0
  82. property real deltaFactor: (2 * radius - 1) / (maxRadius * 2 - 1)
  83. property real expandX: transparentBorder && rootItem.horizontalStep > 0 ? maxRadius / width : 0.0
  84. property real expandY: transparentBorder && rootItem.verticalStep > 0 ? maxRadius / height : 0.0
  85. property variant gwts: []
  86. property variant delta: Qt.vector3d(rootItem.horizontalStep * deltaFactor, rootItem.verticalStep * deltaFactor, startIndex);
  87. property variant factor_0_2: Qt.vector3d(gwts[0], gwts[1], gwts[2]);
  88. property variant factor_3_5: Qt.vector3d(gwts[3], gwts[4], gwts[5]);
  89. property variant factor_6_8: Qt.vector3d(gwts[6], gwts[7], gwts[8]);
  90. property variant factor_9_11: Qt.vector3d(gwts[9], gwts[10], gwts[11]);
  91. property variant factor_12_14: Qt.vector3d(gwts[12], gwts[13], gwts[14]);
  92. property variant factor_15_17: Qt.vector3d(gwts[15], gwts[16], gwts[17]);
  93. property variant factor_18_20: Qt.vector3d(gwts[18], gwts[19], gwts[20]);
  94. property variant factor_21_23: Qt.vector3d(gwts[21], gwts[22], gwts[23]);
  95. property variant factor_24_26: Qt.vector3d(gwts[24], gwts[25], gwts[26]);
  96. property variant factor_27_29: Qt.vector3d(gwts[27], gwts[28], gwts[29]);
  97. property variant factor_30_31: Qt.point(gwts[30], gwts[31]);
  98. property color color: rootItem.color
  99. property real spread: 1.0 - (rootItem.spread * 0.98)
  100. property variant maskSource: maskSourceProxy.output
  101. anchors.fill: rootItem
  102. function gausFunc(x){
  103. //Gaussian function = h(x):=(1/sqrt(2*3.14159*(D^2))) * %e^(-(x^2)/(2*(D^2)));
  104. return (1.0 / Math.sqrt(2 * Math.PI * (Math.pow(shaderItem.deviation, 2)))) * Math.pow(Math.E, -((Math.pow(x, 2)) / (2 * (Math.pow(shaderItem.deviation, 2)))));
  105. }
  106. function updateGaussianWeights() {
  107. gaussianSum = 0.0;
  108. startIndex = -maxRadius + 0.5
  109. var n = new Array(32);
  110. for (var j = 0; j < 32; j++)
  111. n[j] = 0;
  112. var max = maxRadius * 2
  113. var delta = (2 * radius - 1) / (max - 1);
  114. for (var i = 0; i < max; i++) {
  115. n[i] = gausFunc(-radius + 0.5 + i * delta);
  116. gaussianSum += n[i];
  117. }
  118. gwts = n;
  119. }
  120. function buildFragmentShader() {
  121. var shaderSteps = [
  122. "gl_FragColor += texture2D(source, texCoord) * factor_0_2.x; texCoord += shift;",
  123. "gl_FragColor += texture2D(source, texCoord) * factor_0_2.y; texCoord += shift;",
  124. "gl_FragColor += texture2D(source, texCoord) * factor_0_2.z; texCoord += shift;",
  125. "gl_FragColor += texture2D(source, texCoord) * factor_3_5.x; texCoord += shift;",
  126. "gl_FragColor += texture2D(source, texCoord) * factor_3_5.y; texCoord += shift;",
  127. "gl_FragColor += texture2D(source, texCoord) * factor_3_5.z; texCoord += shift;",
  128. "gl_FragColor += texture2D(source, texCoord) * factor_6_8.x; texCoord += shift;",
  129. "gl_FragColor += texture2D(source, texCoord) * factor_6_8.y; texCoord += shift;",
  130. "gl_FragColor += texture2D(source, texCoord) * factor_6_8.z; texCoord += shift;",
  131. "gl_FragColor += texture2D(source, texCoord) * factor_9_11.x; texCoord += shift;",
  132. "gl_FragColor += texture2D(source, texCoord) * factor_9_11.y; texCoord += shift;",
  133. "gl_FragColor += texture2D(source, texCoord) * factor_9_11.z; texCoord += shift;",
  134. "gl_FragColor += texture2D(source, texCoord) * factor_12_14.x; texCoord += shift;",
  135. "gl_FragColor += texture2D(source, texCoord) * factor_12_14.y; texCoord += shift;",
  136. "gl_FragColor += texture2D(source, texCoord) * factor_12_14.z; texCoord += shift;",
  137. "gl_FragColor += texture2D(source, texCoord) * factor_15_17.x; texCoord += shift;",
  138. "gl_FragColor += texture2D(source, texCoord) * factor_15_17.y; texCoord += shift;",
  139. "gl_FragColor += texture2D(source, texCoord) * factor_15_17.z; texCoord += shift;",
  140. "gl_FragColor += texture2D(source, texCoord) * factor_18_20.x; texCoord += shift;",
  141. "gl_FragColor += texture2D(source, texCoord) * factor_18_20.y; texCoord += shift;",
  142. "gl_FragColor += texture2D(source, texCoord) * factor_18_20.z; texCoord += shift;",
  143. "gl_FragColor += texture2D(source, texCoord) * factor_21_23.x; texCoord += shift;",
  144. "gl_FragColor += texture2D(source, texCoord) * factor_21_23.y; texCoord += shift;",
  145. "gl_FragColor += texture2D(source, texCoord) * factor_21_23.z; texCoord += shift;",
  146. "gl_FragColor += texture2D(source, texCoord) * factor_24_26.x; texCoord += shift;",
  147. "gl_FragColor += texture2D(source, texCoord) * factor_24_26.y; texCoord += shift;",
  148. "gl_FragColor += texture2D(source, texCoord) * factor_24_26.z; texCoord += shift;",
  149. "gl_FragColor += texture2D(source, texCoord) * factor_27_29.x; texCoord += shift;",
  150. "gl_FragColor += texture2D(source, texCoord) * factor_27_29.y; texCoord += shift;",
  151. "gl_FragColor += texture2D(source, texCoord) * factor_27_29.z; texCoord += shift;",
  152. "gl_FragColor += texture2D(source, texCoord) * factor_30_31.x; texCoord += shift;",
  153. "gl_FragColor += texture2D(source, texCoord) * factor_30_31.y; texCoord += shift;"
  154. ]
  155. var shader = ""
  156. if (GraphicsInfo.profile == GraphicsInfo.OpenGLCoreProfile)
  157. shader += "#version 150 core\n#define varying in\n#define gl_FragColor fragColor\n#define texture2D texture\nout vec4 fragColor;\n"
  158. shader += fragmentShaderBegin
  159. var samples = maxRadius * 2
  160. if (samples > 32) {
  161. console.log("DirectionalGaussianBlur.qml WARNING: Maximum of blur radius (16) exceeded!")
  162. samples = 32
  163. }
  164. for (var i = 0; i < samples; i++) {
  165. shader += shaderSteps[i]
  166. }
  167. shader += fragmentShaderEnd
  168. var colorizeSteps = ""
  169. var colorizeUniforms = ""
  170. var maskSteps = ""
  171. var maskUniforms = ""
  172. if (enableColor) {
  173. colorizeSteps += "gl_FragColor = mix(vec4(0), color, clamp((gl_FragColor.a - 0.0) / (spread - 0.0), 0.0, 1.0));\n"
  174. colorizeUniforms += "uniform highp vec4 color;\n"
  175. colorizeUniforms += "uniform highp float spread;\n"
  176. }
  177. if (enableMask) {
  178. maskSteps += "shift *= texture2D(maskSource, qt_TexCoord0).a;\n"
  179. maskUniforms += "uniform sampler2D maskSource;\n"
  180. }
  181. shader = shader.replace("PLACEHOLDER_COLORIZE_STEPS", colorizeSteps)
  182. shader = shader.replace("PLACEHOLDER_COLORIZE_UNIFORMS", colorizeUniforms)
  183. shader = shader.replace("PLACEHOLDER_MASK_STEPS", maskSteps)
  184. shader = shader.replace("PLACEHOLDER_MASK_UNIFORMS", maskUniforms)
  185. fragmentShader = shader
  186. }
  187. onDeviationChanged: updateGaussianWeights()
  188. onRadiusChanged: updateGaussianWeights()
  189. onTransparentBorderChanged: {
  190. buildFragmentShader()
  191. updateGaussianWeights()
  192. }
  193. onMaxRadiusChanged: {
  194. buildFragmentShader()
  195. updateGaussianWeights()
  196. }
  197. Component.onCompleted: {
  198. buildFragmentShader()
  199. updateGaussianWeights()
  200. }
  201. property string fragmentShaderBegin: "
  202. varying mediump vec2 qt_TexCoord0;
  203. uniform highp float qt_Opacity;
  204. uniform lowp sampler2D source;
  205. uniform highp vec3 delta;
  206. uniform highp vec3 factor_0_2;
  207. uniform highp vec3 factor_3_5;
  208. uniform highp vec3 factor_6_8;
  209. uniform highp vec3 factor_9_11;
  210. uniform highp vec3 factor_12_14;
  211. uniform highp vec3 factor_15_17;
  212. uniform highp vec3 factor_18_20;
  213. uniform highp vec3 factor_21_23;
  214. uniform highp vec3 factor_24_26;
  215. uniform highp vec3 factor_27_29;
  216. uniform highp vec3 factor_30_31;
  217. uniform highp float gaussianSum;
  218. uniform highp float expandX;
  219. uniform highp float expandY;
  220. PLACEHOLDER_MASK_UNIFORMS
  221. PLACEHOLDER_COLORIZE_UNIFORMS
  222. void main() {
  223. highp vec2 shift = vec2(delta.x, delta.y);
  224. PLACEHOLDER_MASK_STEPS
  225. highp float index = delta.z;
  226. mediump vec2 texCoord = qt_TexCoord0;
  227. texCoord.s = (texCoord.s - expandX) / (1.0 - 2.0 * expandX);
  228. texCoord.t = (texCoord.t - expandY) / (1.0 - 2.0 * expandY);
  229. texCoord += (shift * index);
  230. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  231. "
  232. property string fragmentShaderEnd: "
  233. gl_FragColor /= gaussianSum;
  234. PLACEHOLDER_COLORIZE_STEPS
  235. gl_FragColor *= qt_Opacity;
  236. }
  237. "
  238. }
  239. }