aboutsummaryrefslogtreecommitdiffstats
path: root/src/effects/MotionBlur.qml
blob: 58c6b3b80a384a2e8b476a9d3d1b525190431c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtQuick
import QtQuick3D

Effect {
    id: effectRoot
    // there are only here to get the sampler2Ds declared in the shader
    readonly property TextureInput sprite: TextureInput {
        texture: Texture {}
    }
    readonly property TextureInput glowSampler: TextureInput {
        texture: Texture {}
    }

    property real fadeAmount: 0.25  // 0 - 1
    property real blurQuality: 0.25 // 0.1 - 1.0

    Shader {
        id: vblurVert
        stage: Shader.Vertex
        shader: "qrc:/qtquick3deffects/shaders/motionblurvertical.vert"
    }
    Shader {
        id: vblurFrag
        stage: Shader.Fragment
        shader: "qrc:/qtquick3deffects/shaders/motionblurvertical.frag"
    }

    Shader {
        id: hblurVert
        stage: Shader.Vertex
        shader: "qrc:/qtquick3deffects/shaders/motionblurhorizontal.vert"
    }
    Shader {
        id: hblurFrag
        stage: Shader.Fragment
        shader: "qrc:/qtquick3deffects/shaders/motionblurhorizontal.frag"
    }

    Shader {
        id: blend
        stage: Shader.Fragment
        shader: "qrc:/qtquick3deffects/shaders/blend.frag"
    }

    Buffer {
        id: glowBuffer
        name: "glowBuffer"
        format: Buffer.RGBA8
        textureFilterOperation: Buffer.Nearest
        textureCoordOperation: Buffer.ClampToEdge
        bufferFlags: Buffer.SceneLifetime
        sizeMultiplier: effectRoot.blurQuality
    }

    Buffer {
        id: tempBuffer
        name: "tempBuffer"
        format: Buffer.RGBA8
        textureFilterOperation: Buffer.Linear
        textureCoordOperation: Buffer.ClampToEdge
        bufferFlags: Buffer.None
        sizeMultiplier: effectRoot.blurQuality
    }

    passes: [
        Pass {
            shaders: [ hblurVert, hblurFrag ]
            commands: [
                BufferInput {
                    // Expose the initially empty glowBuffer texture under the
                    // sampler2D glowSampler in the shader. Note the
                    // SceneLifetime and that the next pass writes to the same
                    // texture (accumulate).
                    sampler: "glowSampler"
                    buffer: glowBuffer
                }
            ]
            output: tempBuffer
        },
        Pass {
            shaders: [ vblurVert, vblurFrag ]
            commands: [
                // the texture for tempBuffer will be INPUT in this pass
                BufferInput {
                    buffer: tempBuffer
                }
            ]
            output: glowBuffer
        },
        Pass {
            shaders: [ blend ]
            commands: [
                // the texture for glowBuffer will be INPUT in this pass
                BufferInput {
                    buffer: glowBuffer
                },
                // the input texture (that would normally be INPUT) for this pass is exposed to the shader as sprite
                BufferInput {
                    sampler: "sprite"
                }
            ]
        }
    ]
}