1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
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 | |
40 | #include <QtTest/QTest> |
41 | #include <Qt3DRender/qshaderimage.h> |
42 | #include <Qt3DRender/private/qshaderimage_p.h> |
43 | #include <Qt3DRender/private/shaderimage_p.h> |
44 | #include <Qt3DRender/qtexture.h> |
45 | #include "qbackendnodetester.h" |
46 | #include "testrenderer.h" |
47 | |
48 | class tst_ShaderImage : public Qt3DCore::QBackendNodeTester |
49 | { |
50 | Q_OBJECT |
51 | |
52 | private Q_SLOTS: |
53 | |
54 | void checkInitialState() |
55 | { |
56 | // GIVEN |
57 | Qt3DRender::Render::ShaderImage backendShaderImage; |
58 | |
59 | // THEN |
60 | QCOMPARE(backendShaderImage.isEnabled(), false); |
61 | QVERIFY(backendShaderImage.peerId().isNull()); |
62 | QCOMPARE(backendShaderImage.textureId(), Qt3DCore::QNodeId()); |
63 | QCOMPARE(backendShaderImage.mipLevel(), int(0)); |
64 | QCOMPARE(backendShaderImage.layer(), int(0)); |
65 | QCOMPARE(backendShaderImage.layered(), false); |
66 | QCOMPARE(backendShaderImage.access(), Qt3DRender::QShaderImage::ReadWrite); |
67 | QCOMPARE(backendShaderImage.format(), Qt3DRender::QShaderImage::NoFormat); |
68 | } |
69 | |
70 | void checkCleanupState() |
71 | { |
72 | // GIVEN |
73 | Qt3DRender::Render::ShaderImage backendShaderImage; |
74 | |
75 | // WHEN |
76 | backendShaderImage.setEnabled(true); |
77 | backendShaderImage.setTextureId(Qt3DCore::QNodeId::createId()); |
78 | backendShaderImage.setMipLevel(8); |
79 | backendShaderImage.setLayer(200); |
80 | backendShaderImage.setLayered(true); |
81 | backendShaderImage.setAccess(Qt3DRender::QShaderImage::WriteOnly); |
82 | backendShaderImage.setFormat(Qt3DRender::QShaderImage::R32F); |
83 | |
84 | backendShaderImage.cleanup(); |
85 | |
86 | // THEN |
87 | QCOMPARE(backendShaderImage.isEnabled(), false); |
88 | QCOMPARE(backendShaderImage.textureId(), Qt3DCore::QNodeId()); |
89 | QCOMPARE(backendShaderImage.mipLevel(), int(0)); |
90 | QCOMPARE(backendShaderImage.layer(), int(0)); |
91 | QCOMPARE(backendShaderImage.layered(), false); |
92 | QCOMPARE(backendShaderImage.access(), Qt3DRender::QShaderImage::ReadWrite); |
93 | QCOMPARE(backendShaderImage.format(), Qt3DRender::QShaderImage::NoFormat); |
94 | |
95 | } |
96 | |
97 | void checkInitializeFromPeer() |
98 | { |
99 | // GIVEN |
100 | Qt3DRender::QShaderImage shaderImage; |
101 | TestRenderer renderer; |
102 | |
103 | { |
104 | // WHEN |
105 | Qt3DRender::Render::ShaderImage backendShaderImage; |
106 | backendShaderImage.setRenderer(&renderer); |
107 | simulateInitializationSync(frontend: &shaderImage, backend: &backendShaderImage); |
108 | |
109 | // THEN |
110 | QCOMPARE(backendShaderImage.isEnabled(), true); |
111 | QCOMPARE(backendShaderImage.peerId(), shaderImage.id()); |
112 | QCOMPARE(backendShaderImage.textureId(), Qt3DCore::QNodeId()); |
113 | QCOMPARE(backendShaderImage.mipLevel(), 0); |
114 | QCOMPARE(backendShaderImage.layer(), 0); |
115 | QCOMPARE(backendShaderImage.layered(), false); |
116 | QCOMPARE(backendShaderImage.access(), Qt3DRender::QShaderImage::ReadWrite); |
117 | QCOMPARE(backendShaderImage.format(), Qt3DRender::QShaderImage::Automatic); |
118 | } |
119 | { |
120 | // WHEN |
121 | Qt3DRender::Render::ShaderImage backendShaderImage; |
122 | shaderImage.setEnabled(false); |
123 | backendShaderImage.setRenderer(&renderer); |
124 | simulateInitializationSync(frontend: &shaderImage, backend: &backendShaderImage); |
125 | |
126 | // THEN |
127 | QCOMPARE(backendShaderImage.peerId(), shaderImage.id()); |
128 | QCOMPARE(backendShaderImage.isEnabled(), false); |
129 | } |
130 | } |
131 | |
132 | void checkSceneChangeEvents() |
133 | { |
134 | // GIVEN |
135 | Qt3DRender::QShaderImage shaderImage; |
136 | Qt3DRender::Render::ShaderImage backendShaderImage; |
137 | TestRenderer renderer; |
138 | backendShaderImage.setRenderer(&renderer); |
139 | simulateInitializationSync(frontend: &shaderImage, backend: &backendShaderImage); |
140 | |
141 | { |
142 | // WHEN |
143 | const bool newValue = false; |
144 | shaderImage.setEnabled(newValue); |
145 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
146 | |
147 | // THEN |
148 | QCOMPARE(backendShaderImage.isEnabled(), newValue); |
149 | } |
150 | { |
151 | // WHEN |
152 | Qt3DRender::QTexture2D tex2D; |
153 | shaderImage.setTexture(&tex2D); |
154 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
155 | |
156 | // THEN |
157 | QCOMPARE(backendShaderImage.textureId(), tex2D.id()); |
158 | } |
159 | { |
160 | // WHEN |
161 | const int newValue = 883; |
162 | shaderImage.setMipLevel(newValue); |
163 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
164 | |
165 | // THEN |
166 | QCOMPARE(backendShaderImage.mipLevel(), newValue); |
167 | } |
168 | { |
169 | // WHEN |
170 | const int newValue = 1584; |
171 | shaderImage.setLayer(newValue); |
172 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
173 | |
174 | // THEN |
175 | QCOMPARE(backendShaderImage.layer(), newValue); |
176 | } |
177 | { |
178 | // WHEN |
179 | const bool newValue = true; |
180 | shaderImage.setLayered(newValue); |
181 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
182 | |
183 | // THEN |
184 | QCOMPARE(backendShaderImage.layered(), newValue); |
185 | } |
186 | { |
187 | // WHEN |
188 | const Qt3DRender::QShaderImage::Access newValue = Qt3DRender::QShaderImage::WriteOnly; |
189 | shaderImage.setAccess(newValue); |
190 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
191 | |
192 | // THEN |
193 | QCOMPARE(backendShaderImage.access(), newValue); |
194 | } |
195 | { |
196 | // WHEN |
197 | const Qt3DRender::QShaderImage::ImageFormat newValue = Qt3DRender::QShaderImage::RG16F; |
198 | shaderImage.setFormat(newValue); |
199 | backendShaderImage.syncFromFrontEnd(frontEnd: &shaderImage, firstTime: false); |
200 | |
201 | // THEN |
202 | QCOMPARE(backendShaderImage.format(), newValue); |
203 | } |
204 | } |
205 | |
206 | }; |
207 | |
208 | QTEST_MAIN(tst_ShaderImage) |
209 | |
210 | #include "tst_shaderimage.moc" |
211 | |