Debian Codegraph @

tests/manual/shader-image-qml/main.qml

raw
1 // Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 import Qt3D.Core 2.14
5 import Qt3D.Render 2.14
6 import Qt3D.Input 2.0
7 import Qt3D.Extras 2.0
8
9 Entity {
10 id: sceneRoot
11
12 components: [
13 RenderSettings {
14 id : external_forward_renderer
15 activeFrameGraph : RenderSurfaceSelector {
16 Viewport {
17 // Launch Compute Shader Instances
18 DispatchCompute {
19 }
20
21 // Draw Textures
22 CameraSelector {
23 camera: camera
24 ClearBuffers { buffers: ClearBuffers.ColorDepthBuffer
25 // Ensure we won't be reading from textures if Compute Shader instances aren't completed
26 MemoryBarrier { waitFor: MemoryBarrier.ShaderImageAccess|MemoryBarrier.TextureFetch }
27 }
28 }
29 }
30 }
31 },
32 // Event Source will be set by the Qt3DQuickWindow
33 InputSettings { id: inputSettings }
34 ]
35
36 Camera {
37 id: camera
38 projectionType: CameraLens.PerspectiveProjection
39 fieldOfView: 45
40 aspectRatio: 16/9
41 nearPlane : 0.1
42 farPlane : 1000.0
43 position: Qt.vector3d( 0.0, 0.0, 3.0 )
44 upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
45 viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
46 }
47
48 Texture2D {
49 id: inputTexture
50 width: 512
51 height: 512
52 format: Texture.RGBA8_UNorm
53 generateMipMaps: false
54 magnificationFilter: Texture.Linear
55 minificationFilter: Texture.Linear
56 wrapMode {
57 x: WrapMode.ClampToEdge
58 y: WrapMode.ClampToEdge
59 }
60
61 // Texture image which are referencing regular image extensions (jpg, png)
62 // are loaded through a QImage based texture image generator which generates
63 // RGBA8_UNorm based content
64 TextureImage {
65 source: "qrc:/image_512x512.jpg"
66 mirrored: false
67 }
68 }
69
70 Texture2D {
71 id: outputTexture
72 width: 512
73 height: 512
74 format: Texture.RGBA8_UNorm
75 generateMipMaps: false
76 magnificationFilter: Texture.Linear
77 minificationFilter: Texture.Linear
78 wrapMode {
79 x: WrapMode.ClampToEdge
80 y: WrapMode.ClampToEdge
81 }
82 }
83
84 // Compute -> Generate Output Texture Content
85 Entity {
86 readonly property Material material: Material {
87 parameters: [
88 Parameter {
89 name: "inputImage"
90 value: ShaderImage {
91 texture: inputTexture
92 layer: 0 // default
93 layered: false // default
94 mipLevel: 0 // default
95 access: ShaderImage.ReadOnly // default is ReadWrite
96 format: ShaderImage.Automatic // default
97 }
98 },
99 Parameter {
100 name: "outputImage"
101 value: ShaderImage {
102 texture: outputTexture
103 access: ShaderImage.WriteOnly // default is ReadWrite
104 format: ShaderImage.RGBA8_UNorm
105 }
106 }
107 ]
108
109 effect: Effect {
110 techniques: Technique {
111 graphicsApiFilter {
112 api: GraphicsApiFilter.OpenGL
113 profile: GraphicsApiFilter.CoreProfile
114 majorVersion: 4
115 minorVersion: 3
116 }
117 renderPasses: RenderPass {
118 shaderProgram: ShaderProgram {
119 computeShaderCode:
120 "#version 430
121
122 layout(local_size_x = 16, local_size_y = 16) in;
123 layout(rgba8) readonly uniform image2D inputImage;
124 layout(rgba8) writeonly uniform image2D outputImage;
125 uniform float time;
126 void main(void) {
127 ivec2 imageCoords = ivec2(gl_GlobalInvocationID.xy);
128
129 // Early return if we are past the image's bounds
130 if (any(greaterThan(imageCoords, imageSize(inputImage))))
131 return;
132
133 // Read from input image
134 vec4 inputContent = imageLoad(inputImage, imageCoords);
135
136 // Store inputContent into output Image
137 imageStore(outputImage, imageCoords, inputContent * abs(sin(time)));
138 }"
139 }
140 }
141 }
142 }
143 }
144
145 readonly property ComputeCommand command: ComputeCommand {
146 readonly property int localWorkGroupSize: 16
147 workGroupX: Math.ceil(inputTexture.width / localWorkGroupSize)
148 workGroupY: Math.ceil(inputTexture.height / localWorkGroupSize)
149 }
150
151 components: [command, material]
152 }
153
154 // Preview Texture Scene
155 Entity {
156 PlaneMesh {
157 id: planeMesh
158 }
159
160 components: Transform {
161 rotationX: 90
162 }
163
164 Entity {
165 id: inputTexturePreview
166 readonly property Material material: TextureMaterial {
167 texture: inputTexture
168 }
169 readonly property Transform transform: Transform {
170 translation: Qt.vector3d(-1, 0, 0)
171 }
172 components: [planeMesh, material, transform]
173 }
174
175 Entity {
176 id: outputTexturePreview
177 readonly property Material material: TextureMaterial {
178 texture: outputTexture
179 }
180 readonly property Transform transform: Transform {
181 translation: Qt.vector3d(1, 0, 0)
182 }
183 components: [planeMesh, material, transform]
184 }
185 }
186 }