GaussianDirectionalBlur.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import Qt5Compat.GraphicalEffects.private
  6. Item {
  7. id: rootItem
  8. property variant source
  9. property real deviation: (radius + 1) / 3.3333
  10. property real radius: 0.0
  11. property int maximumRadius: 0
  12. property real horizontalStep: 0.0
  13. property real verticalStep: 0.0
  14. property bool transparentBorder: false
  15. property bool cached: false
  16. property bool enableColor: false
  17. property color color: "white"
  18. property real spread: 0.0
  19. property bool enableMask: false
  20. property variant maskSource
  21. SourceProxy {
  22. id: sourceProxy
  23. input: rootItem.source
  24. }
  25. SourceProxy {
  26. id: maskSourceProxy
  27. input: rootItem.maskSource
  28. }
  29. ShaderEffectSource {
  30. id: cacheItem
  31. anchors.fill: rootItem
  32. visible: rootItem.cached
  33. smooth: true
  34. sourceItem: shaderItem
  35. live: true
  36. hideSource: visible
  37. }
  38. ShaderEffect {
  39. id: shaderItem
  40. property variant source: sourceProxy.output
  41. property real deviation: Math.max(0.1, rootItem.deviation)
  42. property real radius: rootItem.radius
  43. property int maxRadius: rootItem.maximumRadius
  44. property bool transparentBorder: rootItem.transparentBorder
  45. property real gaussianSum: 0.0
  46. property real startIndex: 0.0
  47. property real deltaFactor: (2 * radius - 1) / (maxRadius * 2 - 1)
  48. property real expandX: transparentBorder && rootItem.horizontalStep > 0 ? maxRadius / width : 0.0
  49. property real expandY: transparentBorder && rootItem.verticalStep > 0 ? maxRadius / height : 0.0
  50. property variant gwts: []
  51. property variant delta: Qt.vector3d(rootItem.horizontalStep * deltaFactor, rootItem.verticalStep * deltaFactor, startIndex);
  52. property variant factor_0_2: Qt.vector3d(gwts[0], gwts[1], gwts[2]);
  53. property variant factor_3_5: Qt.vector3d(gwts[3], gwts[4], gwts[5]);
  54. property variant factor_6_8: Qt.vector3d(gwts[6], gwts[7], gwts[8]);
  55. property variant factor_9_11: Qt.vector3d(gwts[9], gwts[10], gwts[11]);
  56. property variant factor_12_14: Qt.vector3d(gwts[12], gwts[13], gwts[14]);
  57. property variant factor_15_17: Qt.vector3d(gwts[15], gwts[16], gwts[17]);
  58. property variant factor_18_20: Qt.vector3d(gwts[18], gwts[19], gwts[20]);
  59. property variant factor_21_23: Qt.vector3d(gwts[21], gwts[22], gwts[23]);
  60. property variant factor_24_26: Qt.vector3d(gwts[24], gwts[25], gwts[26]);
  61. property variant factor_27_29: Qt.vector3d(gwts[27], gwts[28], gwts[29]);
  62. property variant factor_30_31: Qt.point(gwts[30], gwts[31]);
  63. property color color: rootItem.color
  64. property real spread: 1.0 - (rootItem.spread * 0.98)
  65. property variant maskSource: maskSourceProxy.output
  66. anchors.fill: rootItem
  67. function gausFunc(x){
  68. //Gaussian function = h(x):=(1/sqrt(2*3.14159*(D^2))) * %e^(-(x^2)/(2*(D^2)));
  69. 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)))));
  70. }
  71. function updateGaussianWeights() {
  72. gaussianSum = 0.0;
  73. startIndex = -maxRadius + 0.5
  74. var n = new Array(32);
  75. for (var j = 0; j < 32; j++)
  76. n[j] = 0;
  77. var max = maxRadius * 2
  78. var delta = (2 * radius - 1) / (max - 1);
  79. for (var i = 0; i < max; i++) {
  80. n[i] = gausFunc(-radius + 0.5 + i * delta);
  81. gaussianSum += n[i];
  82. }
  83. gwts = n;
  84. }
  85. function buildFragmentShader() {
  86. var shaderSteps = [
  87. "fragColor += texture(source, texCoord) * factor_0_2.x; texCoord += shift;",
  88. "fragColor += texture(source, texCoord) * factor_0_2.y; texCoord += shift;",
  89. "fragColor += texture(source, texCoord) * factor_0_2.z; texCoord += shift;",
  90. "fragColor += texture(source, texCoord) * factor_3_5.x; texCoord += shift;",
  91. "fragColor += texture(source, texCoord) * factor_3_5.y; texCoord += shift;",
  92. "fragColor += texture(source, texCoord) * factor_3_5.z; texCoord += shift;",
  93. "fragColor += texture(source, texCoord) * factor_6_8.x; texCoord += shift;",
  94. "fragColor += texture(source, texCoord) * factor_6_8.y; texCoord += shift;",
  95. "fragColor += texture(source, texCoord) * factor_6_8.z; texCoord += shift;",
  96. "fragColor += texture(source, texCoord) * factor_9_11.x; texCoord += shift;",
  97. "fragColor += texture(source, texCoord) * factor_9_11.y; texCoord += shift;",
  98. "fragColor += texture(source, texCoord) * factor_9_11.z; texCoord += shift;",
  99. "fragColor += texture(source, texCoord) * factor_12_14.x; texCoord += shift;",
  100. "fragColor += texture(source, texCoord) * factor_12_14.y; texCoord += shift;",
  101. "fragColor += texture(source, texCoord) * factor_12_14.z; texCoord += shift;",
  102. "fragColor += texture(source, texCoord) * factor_15_17.x; texCoord += shift;",
  103. "fragColor += texture(source, texCoord) * factor_15_17.y; texCoord += shift;",
  104. "fragColor += texture(source, texCoord) * factor_15_17.z; texCoord += shift;",
  105. "fragColor += texture(source, texCoord) * factor_18_20.x; texCoord += shift;",
  106. "fragColor += texture(source, texCoord) * factor_18_20.y; texCoord += shift;",
  107. "fragColor += texture(source, texCoord) * factor_18_20.z; texCoord += shift;",
  108. "fragColor += texture(source, texCoord) * factor_21_23.x; texCoord += shift;",
  109. "fragColor += texture(source, texCoord) * factor_21_23.y; texCoord += shift;",
  110. "fragColor += texture(source, texCoord) * factor_21_23.z; texCoord += shift;",
  111. "fragColor += texture(source, texCoord) * factor_24_26.x; texCoord += shift;",
  112. "fragColor += texture(source, texCoord) * factor_24_26.y; texCoord += shift;",
  113. "fragColor += texture(source, texCoord) * factor_24_26.z; texCoord += shift;",
  114. "fragColor += texture(source, texCoord) * factor_27_29.x; texCoord += shift;",
  115. "fragColor += texture(source, texCoord) * factor_27_29.y; texCoord += shift;",
  116. "fragColor += texture(source, texCoord) * factor_27_29.z; texCoord += shift;",
  117. "fragColor += texture(source, texCoord) * factor_30_31.x; texCoord += shift;",
  118. "fragColor += texture(source, texCoord) * factor_30_31.y; texCoord += shift;"
  119. ]
  120. var shader = fragmentShaderBegin
  121. var samples = maxRadius * 2
  122. if (samples > 32) {
  123. console.log("DirectionalGaussianBlur.qml WARNING: Maximum of blur radius (16) exceeded!")
  124. samples = 32
  125. }
  126. for (var i = 0; i < samples; i++) {
  127. shader += shaderSteps[i]
  128. }
  129. shader += fragmentShaderEnd
  130. var colorizeSteps = ""
  131. var colorizeUniforms = ""
  132. var maskSteps = ""
  133. var maskUniforms = ""
  134. if (enableColor) {
  135. colorizeSteps += "fragColor = mix(vec4(0), color, clamp((fragColor.a - 0.0) / (spread - 0.0), 0.0, 1.0));\n"
  136. colorizeUniforms += "vec4 color;\n"
  137. colorizeUniforms += "float spread;\n"
  138. }
  139. if (enableMask) {
  140. maskSteps += "shift *= texture(maskSource, qt_TexCoord0).a;\n"
  141. maskUniforms += "layout(binding = 2) uniform sampler2D maskSource;\n"
  142. }
  143. shader = shader.replace("PLACEHOLDER_COLORIZE_STEPS", colorizeSteps)
  144. shader = shader.replace("PLACEHOLDER_COLORIZE_UNIFORMS", colorizeUniforms)
  145. shader = shader.replace("PLACEHOLDER_MASK_STEPS", maskSteps)
  146. shader = shader.replace("PLACEHOLDER_MASK_UNIFORMS", maskUniforms)
  147. fragmentShader = ShaderBuilder.buildFragmentShader(shader)
  148. }
  149. onDeviationChanged: updateGaussianWeights()
  150. onRadiusChanged: updateGaussianWeights()
  151. onTransparentBorderChanged: {
  152. buildFragmentShader()
  153. updateGaussianWeights()
  154. }
  155. onMaxRadiusChanged: {
  156. buildFragmentShader()
  157. updateGaussianWeights()
  158. }
  159. Component.onCompleted: {
  160. buildFragmentShader()
  161. updateGaussianWeights()
  162. }
  163. property string fragmentShaderBegin: "#version 440
  164. layout(location = 0) in vec2 qt_TexCoord0;
  165. layout(location = 0) out vec4 fragColor;
  166. layout(std140, binding = 0) uniform buf {
  167. mat4 qt_Matrix;
  168. float qt_Opacity;
  169. vec3 delta;
  170. vec3 factor_0_2;
  171. vec3 factor_3_5;
  172. vec3 factor_6_8;
  173. vec3 factor_9_11;
  174. vec3 factor_12_14;
  175. vec3 factor_15_17;
  176. vec3 factor_18_20;
  177. vec3 factor_21_23;
  178. vec3 factor_24_26;
  179. vec3 factor_27_29;
  180. vec2 factor_30_31;
  181. float gaussianSum;
  182. float expandX;
  183. float expandY;
  184. PLACEHOLDER_COLORIZE_UNIFORMS
  185. };
  186. layout(binding = 1) uniform sampler2D source;
  187. PLACEHOLDER_MASK_UNIFORMS
  188. void main() {
  189. vec2 shift = vec2(delta.x, delta.y);
  190. PLACEHOLDER_MASK_STEPS
  191. float index = delta.z;
  192. vec2 texCoord = qt_TexCoord0;
  193. texCoord.s = (texCoord.s - expandX) / (1.0 - 2.0 * expandX);
  194. texCoord.t = (texCoord.t - expandY) / (1.0 - 2.0 * expandY);
  195. texCoord += (shift * index);
  196. fragColor = vec4(0.0, 0.0, 0.0, 0.0);
  197. "
  198. property string fragmentShaderEnd: "
  199. fragColor /= gaussianSum;
  200. PLACEHOLDER_COLORIZE_STEPS
  201. fragColor *= qt_Opacity;
  202. }
  203. "
  204. }
  205. }