DirectionalBlur.qml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. /*!
  7. \qmltype DirectionalBlur
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since QtGraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-motion-blur
  12. \brief Applies blur effect to the specified direction.
  13. Effect creates perceived impression that the source item appears to be
  14. moving in the direction of the blur. Blur is applied to both sides of
  15. each pixel, therefore setting the direction to 0 and 180 provides the
  16. same result.
  17. Other available motionblur effects are \l{Qt5Compat.GraphicalEffects::ZoomBlur}{ZoomBlur} and
  18. \l{Qt5Compat.GraphicalEffects::RadialBlur}{RadialBlur}.
  19. \table
  20. \header
  21. \li Source
  22. \li Effect applied
  23. \row
  24. \li \image Original_bug.png
  25. \li \image DirectionalBlur_bug.png
  26. \endtable
  27. \note This effect is available when running with OpenGL.
  28. \section1 Example
  29. The following example shows how to apply the effect.
  30. \snippet DirectionalBlur-example.qml example
  31. */
  32. Item {
  33. id: rootItem
  34. /*!
  35. This property defines the source item that is going to be blurred.
  36. \note It is not supported to let the effect include itself, for
  37. instance by setting source to the effect's parent.
  38. */
  39. property variant source
  40. /*!
  41. This property defines the perceived amount of movement for each pixel.
  42. The movement is divided evenly to both sides of each pixel.
  43. The quality of the blur depends on \l{DirectionalBlur::samples}{samples}
  44. property. If length value is large, more samples are needed to keep the
  45. visual quality at high level.
  46. The value ranges from 0.0 to inf.
  47. By default the property is set to \c 0.0 (no blur).
  48. \table
  49. \header
  50. \li Output examples with different length values
  51. \li
  52. \li
  53. \row
  54. \li \image DirectionalBlur_length1.png
  55. \li \image DirectionalBlur_length2.png
  56. \li \image DirectionalBlur_length3.png
  57. \row
  58. \li \b { length: 0.0 }
  59. \li \b { length: 32.0 }
  60. \li \b { length: 48.0 }
  61. \row
  62. \li \l samples: 24
  63. \li \l samples: 24
  64. \li \l samples: 24
  65. \row
  66. \li \l angle: 0
  67. \li \l angle: 0
  68. \li \l angle: 0
  69. \endtable
  70. */
  71. property real length: 0.0
  72. /*!
  73. This property defines how many samples are taken per pixel when blur
  74. calculation is done. Larger value produces better quality, but is slower
  75. to render.
  76. This property is not intended to be animated. Changing this property may
  77. cause the underlying OpenGL shaders to be recompiled.
  78. Allowed values are between 0 and inf (practical maximum depends on GPU).
  79. By default the property is set to \c 0 (no samples).
  80. */
  81. property int samples: 0
  82. /*!
  83. This property defines the direction for the blur. Blur is applied to
  84. both sides of each pixel, therefore setting the direction to 0 and 180
  85. produces the same result.
  86. The value ranges from -180.0 to 180.0.
  87. By default the property is set to \c 0.0.
  88. \table
  89. \header
  90. \li Output examples with different angle values
  91. \li
  92. \li
  93. \row
  94. \li \image DirectionalBlur_angle1.png
  95. \li \image DirectionalBlur_angle2.png
  96. \li \image DirectionalBlur_angle3.png
  97. \row
  98. \li \b { angle: 0.0 }
  99. \li \b { angle: 45.0 }
  100. \li \b { angle: 90.0 }
  101. \row
  102. \li \l samples: 24
  103. \li \l samples: 24
  104. \li \l samples: 24
  105. \row
  106. \li \l length: 32
  107. \li \l length: 32
  108. \li \l length: 32
  109. \endtable
  110. */
  111. property real angle: 0.0
  112. /*!
  113. This property defines the blur behavior near the edges of the item,
  114. where the pixel blurring is affected by the pixels outside the source
  115. edges.
  116. If the property is set to \c true, the pixels outside the source are
  117. interpreted to be transparent, which is similar to OpenGL
  118. clamp-to-border extension. The blur is expanded slightly outside the
  119. effect item area.
  120. If the property is set to \c false, the pixels outside the source are
  121. interpreted to contain the same color as the pixels at the edge of the
  122. item, which is similar to OpenGL clamp-to-edge behavior. The blur does
  123. not expand outside the effect item area.
  124. By default, the property is set to \c false.
  125. */
  126. property bool transparentBorder: false
  127. /*!
  128. This property allows the effect output pixels to be cached in order to
  129. improve the rendering performance.
  130. Every time the source or effect properties are changed, the pixels in
  131. the cache must be updated. Memory consumption is increased, because an
  132. extra buffer of memory is required for storing the effect output.
  133. It is recommended to disable the cache when the source or the effect
  134. properties are animated.
  135. By default, the property is set to \c false.
  136. */
  137. property bool cached: false
  138. SourceProxy {
  139. id: sourceProxy
  140. input: rootItem.source
  141. sourceRect: rootItem.transparentBorder ? Qt.rect(-1, -1, parent.width + 2.0, parent.height + 2.0) : Qt.rect(0, 0, 0, 0)
  142. }
  143. ShaderEffectSource {
  144. id: cacheItem
  145. anchors.fill: shaderItem
  146. visible: rootItem.cached
  147. smooth: true
  148. sourceItem: shaderItem
  149. live: true
  150. hideSource: visible
  151. }
  152. ShaderEffect {
  153. id: shaderItem
  154. property variant source: sourceProxy.output
  155. property real len: rootItem.length
  156. property bool transparentBorder: rootItem.transparentBorder
  157. property real samples: rootItem.samples
  158. property real weight: 1.0 / Math.max(1.0, rootItem.samples)
  159. property variant expandPixels: transparentBorder ? Qt.size(rootItem.samples, rootItem.samples) : Qt.size(0,0)
  160. property variant expand: transparentBorder ? Qt.size(expandPixels.width / width, expandPixels.height / height) : Qt.size(0,0)
  161. property variant delta: Qt.size(1.0 / rootItem.width * Math.cos((rootItem.angle + 90) * Math.PI/180), 1.0 / rootItem.height * Math.sin((rootItem.angle + 90) * Math.PI/180))
  162. x: transparentBorder ? -expandPixels.width - 1: 0
  163. y: transparentBorder ? -expandPixels.height - 1 : 0
  164. width: transparentBorder ? parent.width + 2.0 * expandPixels.width + 2 : parent.width
  165. height: transparentBorder ? parent.height + 2.0 * expandPixels.height + 2 : parent.height
  166. property string fragmentShaderSkeleton: "#version 440
  167. layout(location = 0) in vec2 qt_TexCoord0;
  168. layout(location = 0) out vec4 fragColor;
  169. layout(std140, binding = 0) uniform buf {
  170. mat4 qt_Matrix;
  171. float qt_Opacity;
  172. float len;
  173. float samples;
  174. float weight;
  175. vec2 expand;
  176. vec2 delta;
  177. };
  178. layout(binding = 1) uniform sampler2D source;
  179. void main(void) {
  180. vec2 shift = delta * len / max(1.0, samples - 1.0);
  181. vec2 texCoord = qt_TexCoord0;
  182. fragColor = vec4(0.0);
  183. PLACEHOLDER_EXPAND_STEPS
  184. texCoord -= shift * max(0.0, samples - 1.0) * 0.5;
  185. PLACEHOLDER_UNROLLED_LOOP
  186. fragColor *= weight * qt_Opacity;
  187. }
  188. "
  189. function buildFragmentShader() {
  190. var shader = fragmentShaderSkeleton
  191. var expandSteps = ""
  192. if (transparentBorder) {
  193. expandSteps += "texCoord = (texCoord - expand) / (1.0 - 2.0 * expand);"
  194. }
  195. var unrolledLoop = "fragColor += texture(source, texCoord);\n"
  196. if (rootItem.samples > 1) {
  197. unrolledLoop = ""
  198. for (var i = 0; i < rootItem.samples; i++)
  199. unrolledLoop += "fragColor += texture(source, texCoord); texCoord += shift;\n"
  200. }
  201. shader = shader.replace("PLACEHOLDER_EXPAND_STEPS", expandSteps)
  202. fragmentShader = ShaderBuilder.buildFragmentShader(shader.replace("PLACEHOLDER_UNROLLED_LOOP", unrolledLoop))
  203. }
  204. onFragmentShaderChanged: sourceChanged()
  205. onSamplesChanged: buildFragmentShader()
  206. onTransparentBorderChanged: buildFragmentShader()
  207. Component.onCompleted: buildFragmentShader()
  208. }
  209. }