1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick 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 <private/qquickshadereffect_p.h>
41#include <private/qsgcontextplugin_p.h>
42#include <private/qquickitem_p.h>
43#if QT_CONFIG(opengl)
44#include <private/qquickopenglshadereffect_p.h>
45#endif
46#include <private/qquickgenericshadereffect_p.h>
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \qmltype ShaderEffect
52 \instantiates QQuickShaderEffect
53 \inqmlmodule QtQuick
54 \inherits Item
55 \ingroup qtquick-effects
56 \brief Applies custom shaders to a rectangle
57
58 The ShaderEffect type applies a custom
59 \l{vertexShader}{vertex} and \l{fragmentShader}{fragment (pixel)} shader to a
60 rectangle. It allows you to write effects such as drop shadow, blur,
61 colorize and page curl directly in QML.
62
63 \note Depending on the Qt Quick scenegraph backend in use, the ShaderEffect
64 type may not be supported (for example, with the software backend), or may
65 use a different shading language with rules and expectations different from
66 OpenGL and GLSL.
67
68 \section1 OpenGL and GLSL
69
70 There are two types of input to the \l vertexShader:
71 uniform variables and attributes. Some are predefined:
72 \list
73 \li uniform mat4 qt_Matrix - combined transformation
74 matrix, the product of the matrices from the root item to this
75 ShaderEffect, and an orthogonal projection.
76 \li uniform float qt_Opacity - combined opacity, the product of the
77 opacities from the root item to this ShaderEffect.
78 \li attribute vec4 qt_Vertex - vertex position, the top-left vertex has
79 position (0, 0), the bottom-right (\l{Item::width}{width},
80 \l{Item::height}{height}).
81 \li attribute vec2 qt_MultiTexCoord0 - texture coordinate, the top-left
82 coordinate is (0, 0), the bottom-right (1, 1). If \l supportsAtlasTextures
83 is true, coordinates will be based on position in the atlas instead.
84 \endlist
85
86 In addition, any property that can be mapped to an OpenGL Shading Language
87 (GLSL) type is available as a uniform variable. The following list shows
88 how properties are mapped to GLSL uniform variables:
89 \list
90 \li bool, int, qreal -> bool, int, float - If the type in the shader is not
91 the same as in QML, the value is converted automatically.
92 \li QColor -> vec4 - When colors are passed to the shader, they are first
93 premultiplied. Thus Qt.rgba(0.2, 0.6, 1.0, 0.5) becomes
94 vec4(0.1, 0.3, 0.5, 0.5) in the shader, for example.
95 \li QRect, QRectF -> vec4 - Qt.rect(x, y, w, h) becomes vec4(x, y, w, h) in
96 the shader.
97 \li QPoint, QPointF, QSize, QSizeF -> vec2
98 \li QVector3D -> vec3
99 \li QVector4D -> vec4
100 \li QTransform -> mat3
101 \li QMatrix4x4 -> mat4
102 \li QQuaternion -> vec4, scalar value is \c w.
103 \li \l Image -> sampler2D - Origin is in the top-left corner, and the
104 color values are premultiplied. The texture is provided as is,
105 excluding the Image item's fillMode. To include fillMode, use a
106 ShaderEffectSource or Image::layer::enabled.
107 \li \l ShaderEffectSource -> sampler2D - Origin is in the top-left
108 corner, and the color values are premultiplied.
109 \endlist
110
111 The QML scene graph back-end may choose to allocate textures in texture
112 atlases. If a texture allocated in an atlas is passed to a ShaderEffect,
113 it is by default copied from the texture atlas into a stand-alone texture
114 so that the texture coordinates span from 0 to 1, and you get the expected
115 wrap modes. However, this will increase the memory usage. To avoid the
116 texture copy, set \l supportsAtlasTextures for simple shaders using
117 qt_MultiTexCoord0, or for each "uniform sampler2D <name>" declare a
118 "uniform vec4 qt_SubRect_<name>" which will be assigned the texture's
119 normalized source rectangle. For stand-alone textures, the source rectangle
120 is [0, 1]x[0, 1]. For textures in an atlas, the source rectangle corresponds
121 to the part of the texture atlas where the texture is stored.
122 The correct way to calculate the texture coordinate for a texture called
123 "source" within a texture atlas is
124 "qt_SubRect_source.xy + qt_SubRect_source.zw * qt_MultiTexCoord0".
125
126 The output from the \l fragmentShader should be premultiplied. If
127 \l blending is enabled, source-over blending is used. However, additive
128 blending can be achieved by outputting zero in the alpha channel.
129
130 \table 70%
131 \row
132 \li \image declarative-shadereffectitem.png
133 \li \qml
134 import QtQuick 2.0
135
136 Rectangle {
137 width: 200; height: 100
138 Row {
139 Image { id: img;
140 sourceSize { width: 100; height: 100 } source: "qt-logo.png" }
141 ShaderEffect {
142 width: 100; height: 100
143 property variant src: img
144 vertexShader: "
145 uniform highp mat4 qt_Matrix;
146 attribute highp vec4 qt_Vertex;
147 attribute highp vec2 qt_MultiTexCoord0;
148 varying highp vec2 coord;
149 void main() {
150 coord = qt_MultiTexCoord0;
151 gl_Position = qt_Matrix * qt_Vertex;
152 }"
153 fragmentShader: "
154 varying highp vec2 coord;
155 uniform sampler2D src;
156 uniform lowp float qt_Opacity;
157 void main() {
158 lowp vec4 tex = texture2D(src, coord);
159 gl_FragColor = vec4(vec3(dot(tex.rgb,
160 vec3(0.344, 0.5, 0.156))),
161 tex.a) * qt_Opacity;
162 }"
163 }
164 }
165 }
166 \endqml
167 \endtable
168
169 \note Scene Graph textures have origin in the top-left corner rather than
170 bottom-left which is common in OpenGL.
171
172 For information about the GLSL version being used, see \l QtQuick::GraphicsInfo.
173
174 Starting from Qt 5.8 ShaderEffect also supports reading the GLSL source
175 code from files. Whenever the fragmentShader or vertexShader property value
176 is a URL with the \c file or \c qrc schema, it is treated as a file
177 reference and the source code is read from the specified file.
178
179 \section1 Direct3D and HLSL
180
181 Direct3D backends provide ShaderEffect support with HLSL. The Direct3D 12
182 backend requires using at least Shader Model 5.0 both for vertex and pixel
183 shaders. When necessary, GraphicsInfo.shaderType can be used to decide
184 at runtime what kind of value to assign to \l fragmentShader or
185 \l vertexShader.
186
187 All concepts described above for OpenGL and GLSL apply to Direct3D and HLSL
188 as well. There are however a number of notable practical differences, which
189 are the following:
190
191 Instead of uniforms, HLSL shaders are expected to use a single constant
192 buffer, assigned to register \c b0. The special names \c qt_Matrix,
193 \c qt_Opacity, and \c qt_SubRect_<name> function the same way as with GLSL.
194 All other members of the buffer are expected to map to properties in the
195 ShaderEffect item.
196
197 \note The buffer layout must be compatible for both shaders. This means
198 that application-provided shaders must make sure \c qt_Matrix and
199 \c qt_Opacity are included in the buffer, starting at offset 0, when custom
200 code is provided for one type of shader only, leading to ShaderEffect
201 providing the other shader. This is due to ShaderEffect's built-in shader code
202 declaring a constant buffer containing \c{float4x4 qt_Matrix; float qt_Opacity;}.
203
204 Unlike GLSL's attributes, no names are used for vertex input elements.
205 Therefore qt_Vertex and qt_MultiTexCoord0 are not relevant. Instead, the
206 standard Direct3D semantics, \c POSITION and \c TEXCOORD (or \c TEXCOORD0)
207 are used for identifying the correct input layout.
208
209 Unlike GLSL's samplers, texture and sampler objects are separate in HLSL.
210 Shaders are expected to expect 2D, non-array, non-multisample textures.
211 Both the texture and sampler binding points are expected to be sequential
212 and start from 0 (meaning registers \c{t0, t1, ...}, and \c{s0, s1, ...},
213 respectively). Unlike with OpenGL, samplers are not mapped to Qt Quick item
214 properties and therefore the name of the sampler is not relevant. Instead,
215 it is the textures that map to properties referencing \l Image or
216 \l ShaderEffectSource items.
217
218 Unlike OpenGL, backends for modern APIs will typically prefer offline
219 compilation and shipping pre-compiled bytecode with applications instead of
220 inlined shader source strings. In this case the string properties for
221 vertex and fragment shaders are treated as URLs referring to local files or
222 files shipped via the Qt resource system.
223
224 To check at runtime what is supported, use the
225 GraphicsInfo.shaderSourceType and GraphicsInfo.shaderCompilationType
226 properties. Note that these are bitmasks, because some backends may support
227 multiple approaches.
228
229 In case of Direct3D 12, all combinations are supported. If the vertexShader
230 and fragmentShader properties form a valid URL with the \c file or \c qrc
231 schema, the bytecode or HLSL source code is read from the specified file.
232 The type of the file contents is detected automatically. Otherwise, the
233 string is treated as HLSL source code and is compiled at runtime, assuming
234 Shader Model 5.0 and an entry point of \c{"main"}. This allows dynamically
235 constructing shader strings. However, whenever the shader source code is
236 static, it is strongly recommended to pre-compile to bytecode using the
237 \c fxc tool and refer to these files from QML. This will be a lot more
238 efficient at runtime and allows catching syntax errors in the shaders at
239 compile time.
240
241 Unlike OpenGL, the Direct3D backend is able to perform runtime shader
242 compilation on dedicated threads. This is managed transparently to the
243 applications, and means that ShaderEffect items that contain HLSL source
244 strings do not block the rendering or other parts of the application until
245 the bytecode is ready.
246
247 Using files with bytecode is more flexible also when it comes to the entry
248 point name (it can be anything, not limited to \c main) and the shader
249 model (it can be something newer than 5.0, for instance 5.1).
250
251 \table 70%
252 \row
253 \li \qml
254 import QtQuick 2.0
255
256 Rectangle {
257 width: 200; height: 100
258 Row {
259 Image { id: img;
260 sourceSize { width: 100; height: 100 } source: "qt-logo.png" }
261 ShaderEffect {
262 width: 100; height: 100
263 property variant src: img
264 fragmentShader: "qrc:/effect_ps.cso"
265 }
266 }
267 }
268 \endqml
269 \row
270 \li where \c effect_ps.cso is the compiled bytecode for the following HLSL shader:
271 \code
272 cbuffer ConstantBuffer : register(b0)
273 {
274 float4x4 qt_Matrix;
275 float qt_Opacity;
276 };
277 Texture2D src : register(t0);
278 SamplerState srcSampler : register(s0);
279 float4 ExamplePixelShader(float4 position : SV_POSITION, float2 coord : TEXCOORD0) : SV_TARGET
280 {
281 float4 tex = src.Sample(srcSampler, coord);
282 float3 col = dot(tex.rgb, float3(0.344, 0.5, 0.156));
283 return float4(col, tex.a) * qt_Opacity;
284 }
285 \endcode
286 \endtable
287
288 The above is equivalent to the OpenGL example presented earlier. The vertex
289 shader is provided implicitly by ShaderEffect. Note that the output of the
290 pixel shader is using premultiplied alpha and that \c qt_Matrix is present
291 in the constant buffer at offset 0, even though the pixel shader does not
292 use the value.
293
294 If desired, the HLSL source code can be placed directly into the QML
295 source, similarly to how its done with GLSL. The only difference in this
296 case is the entry point name, which must be \c main when using inline
297 source strings.
298
299 Alternatively, we could also have referred to a file containing the source
300 of the effect instead of the compiled bytecode version.
301
302 Some effects will want to provide a vertex shader as well. Below is a
303 similar effect with both the vertex and fragment shader provided by the
304 application. This time the colorization factor is provided by the QML item
305 instead of hardcoding it in the shader. This can allow, among others,
306 animating the value using QML's and Qt Quick's standard facilities.
307
308 \table 70%
309 \row
310 \li \qml
311 import QtQuick 2.0
312
313 Rectangle {
314 width: 200; height: 100
315 Row {
316 Image { id: img;
317 sourceSize { width: 100; height: 100 } source: "qt-logo.png" }
318 ShaderEffect {
319 width: 100; height: 100
320 property variant src: img
321 property variant color: Qt.vector3d(0.344, 0.5, 0.156)
322 vertexShader: "qrc:/effect_vs.cso"
323 fragmentShader: "qrc:/effect_ps.cso"
324 }
325 }
326 }
327 \endqml
328 \row
329 \li where \c effect_vs.cso and \c effect_ps.cso are the compiled bytecode
330 for \c ExampleVertexShader and \c ExamplePixelShader. The source code is
331 presented as one snippet here, the shaders can however be placed in
332 separate source files as well.
333 \code
334 cbuffer ConstantBuffer : register(b0)
335 {
336 float4x4 qt_Matrix;
337 float qt_Opacity;
338 float3 color;
339 };
340 Texture2D src : register(t0);
341 SamplerState srcSampler : register(s0);
342 struct PSInput
343 {
344 float4 position : SV_POSITION;
345 float2 coord : TEXCOORD0;
346 };
347 PSInput ExampleVertexShader(float4 position : POSITION, float2 coord : TEXCOORD0)
348 {
349 PSInput result;
350 result.position = mul(qt_Matrix, position);
351 result.coord = coord;
352 return result;
353 }
354 float4 ExamplePixelShader(PSInput input) : SV_TARGET
355 {
356 float4 tex = src.Sample(srcSampler, coord);
357 float3 col = dot(tex.rgb, color);
358 return float4(col, tex.a) * qt_Opacity;
359 }
360 \endcode
361 \endtable
362
363 \note With OpenGL the \c y coordinate runs from bottom to top whereas with
364 Direct 3D it goes top to bottom. For shader effect sources Qt Quick hides
365 the difference by treating QtQuick::ShaderEffectSource::textureMirroring as
366 appropriate, meaning texture coordinates in HLSL version of the shaders
367 will not need any adjustments compared to the equivalent GLSL code.
368
369 \section1 Cross-platform, Cross-API ShaderEffect Items
370
371 Some applications will want to be functional with multiple accelerated
372 graphics backends. This has consequences for ShaderEffect items because the
373 supported shading languages may vary from backend to backend.
374
375 There are two approaches to handle this: either write conditional property
376 values based on GraphicsInfo.shaderType, or use file selectors. In practice
377 the latter is strongly recommended as it leads to more concise and cleaner
378 application code. The only case it is not suitable is when the source
379 strings are constructed dynamically.
380
381 \table 70%
382 \row
383 \li \qml
384 import QtQuick 2.8 // for GraphicsInfo
385
386 Rectangle {
387 width: 200; height: 100
388 Row {
389 Image { id: img;
390 sourceSize { width: 100; height: 100 } source: "qt-logo.png" }
391 ShaderEffect {
392 width: 100; height: 100
393 property variant src: img
394 property variant color: Qt.vector3d(0.344, 0.5, 0.156)
395 fragmentShader: GraphicsInfo.shaderType === GraphicsInfo.GLSL ?
396 "varying highp vec2 coord;
397 uniform sampler2D src;
398 uniform lowp float qt_Opacity;
399 void main() {
400 lowp vec4 tex = texture2D(src, coord);
401 gl_FragColor = vec4(vec3(dot(tex.rgb,
402 vec3(0.344, 0.5, 0.156))),
403 tex.a) * qt_Opacity;"
404 : GraphicsInfo.shaderType === GraphicsInfo.HLSL ?
405 "cbuffer ConstantBuffer : register(b0)
406 {
407 float4x4 qt_Matrix;
408 float qt_Opacity;
409 };
410 Texture2D src : register(t0);
411 SamplerState srcSampler : register(s0);
412 float4 ExamplePixelShader(float4 position : SV_POSITION, float2 coord : TEXCOORD0) : SV_TARGET
413 {
414 float4 tex = src.Sample(srcSampler, coord);
415 float3 col = dot(tex.rgb, float3(0.344, 0.5, 0.156));
416 return float4(col, tex.a) * qt_Opacity;
417 }"
418 : ""
419 }
420 }
421 }
422 \endqml
423 \row
424
425 \li This is the first approach based on GraphicsInfo. Note that the value
426 reported by GraphicsInfo is not up-to-date until the ShaderEffect item gets
427 associated with a QQuickWindow. Before that, the reported value is
428 GraphicsInfo.UnknownShadingLanguage. The alternative is to place the GLSL
429 source code and the compiled D3D bytecode into the files
430 \c{shaders/effect.frag} and \c{shaders/+hlsl/effect.frag}, include them in
431 the Qt resource system, and let the ShaderEffect's internal QFileSelector
432 do its job. The selector-less version is the GLSL source, while the \c hlsl
433 selector is used when running on the D3D12 backend. The file under
434 \c{+hlsl} can then contain either HLSL source code or compiled bytecode
435 from the \c fxc tool. Additionally, when using a version 3.2 or newer core
436 profile context with OpenGL, GLSL sources with a core profile compatible
437 syntax can be placed under \c{+glslcore}.
438 \qml
439 import QtQuick 2.8 // for GraphicsInfo
440
441 Rectangle {
442 width: 200; height: 100
443 Row {
444 Image { id: img;
445 sourceSize { width: 100; height: 100 } source: "qt-logo.png" }
446 ShaderEffect {
447 width: 100; height: 100
448 property variant src: img
449 property variant color: Qt.vector3d(0.344, 0.5, 0.156)
450 fragmentShader: "qrc:shaders/effect.frag" // selects the correct variant automatically
451 }
452 }
453 }
454 \endqml
455 \endtable
456
457 \section1 ShaderEffect and Item Layers
458
459 The ShaderEffect type can be combined with \l {Item Layers} {layered items}.
460
461 \table
462 \row
463 \li \b {Layer with effect disabled} \inlineimage qml-shadereffect-nolayereffect.png
464 \li \b {Layer with effect enabled} \inlineimage qml-shadereffect-layereffect.png
465 \row
466 \li \snippet qml/layerwitheffect.qml 1
467 \endtable
468
469 It is also possible to combine multiple layered items:
470
471 \table
472 \row
473 \li \inlineimage qml-shadereffect-opacitymask.png
474 \row
475 \li \snippet qml/opacitymask.qml 1
476 \endtable
477
478 \section1 Other Notes
479
480 By default, the ShaderEffect consists of four vertices, one for each
481 corner. For non-linear vertex transformations, like page curl, you can
482 specify a fine grid of vertices by specifying a \l mesh resolution.
483
484 The \l {Qt Graphical Effects} module contains several ready-made effects
485 for using with Qt Quick applications.
486
487 \sa {Item Layers}
488*/
489
490class QQuickShaderEffectPrivate : public QQuickItemPrivate
491{
492 Q_DECLARE_PUBLIC(QQuickShaderEffect)
493
494public:
495 void updatePolish() override;
496};
497
498QSGContextFactoryInterface::Flags qsg_backend_flags();
499
500QQuickShaderEffect::QQuickShaderEffect(QQuickItem *parent)
501 : QQuickItem(*new QQuickShaderEffectPrivate, parent),
502#if QT_CONFIG(opengl)
503 m_glImpl(nullptr),
504#endif
505 m_impl(nullptr)
506{
507 setFlag(QQuickItem::ItemHasContents);
508
509#if QT_CONFIG(opengl)
510 if (!qsg_backend_flags().testFlag(QSGContextFactoryInterface::SupportsShaderEffectNode))
511 m_glImpl = new QQuickOpenGLShaderEffect(this, this);
512
513 if (!m_glImpl)
514#endif
515 m_impl = new QQuickGenericShaderEffect(this, this);
516}
517
518/*!
519 \qmlproperty string QtQuick::ShaderEffect::fragmentShader
520
521 This property holds the fragment (pixel) shader's source code or a
522 reference to the pre-compiled bytecode. Some APIs, like OpenGL, always
523 support runtime compilation and therefore the traditional Qt Quick way of
524 inlining shader source strings is functional. Qt Quick backends for other
525 APIs may however limit support to pre-compiled bytecode like SPIR-V or D3D
526 shader bytecode. There the string is simply a filename, which may be a file
527 in the filesystem or bundled with the executable via Qt's resource system.
528
529 With GLSL the default shader expects the texture coordinate to be passed
530 from the vertex shader as \c{varying highp vec2 qt_TexCoord0}, and it
531 samples from a sampler2D named \c source. With HLSL the texture is named
532 \c source, while the vertex shader is expected to provide
533 \c{float2 coord : TEXCOORD0} in its output in addition to
534 \c{float4 position : SV_POSITION} (names can differ since linking is done
535 based on the semantics).
536
537 \sa vertexShader, GraphicsInfo
538*/
539
540QByteArray QQuickShaderEffect::fragmentShader() const
541{
542#if QT_CONFIG(opengl)
543 if (m_glImpl)
544 return m_glImpl->fragmentShader();
545#endif
546 return m_impl->fragmentShader();
547}
548
549void QQuickShaderEffect::setFragmentShader(const QByteArray &code)
550{
551#if QT_CONFIG(opengl)
552 if (m_glImpl) {
553 m_glImpl->setFragmentShader(code);
554 return;
555 }
556#endif
557 m_impl->setFragmentShader(code);
558}
559
560/*!
561 \qmlproperty string QtQuick::ShaderEffect::vertexShader
562
563 This property holds the vertex shader's source code or a reference to the
564 pre-compiled bytecode. Some APIs, like OpenGL, always support runtime
565 compilation and therefore the traditional Qt Quick way of inlining shader
566 source strings is functional. Qt Quick backends for other APIs may however
567 limit support to pre-compiled bytecode like SPIR-V or D3D shader bytecode.
568 There the string is simply a filename, which may be a file in the
569 filesystem or bundled with the executable via Qt's resource system.
570
571 With GLSL the default shader passes the texture coordinate along to the
572 fragment shader as \c{varying highp vec2 qt_TexCoord0}. With HLSL it is
573 enough to use the standard \c TEXCOORD0 semantic, for example
574 \c{float2 coord : TEXCOORD0}.
575
576 \sa fragmentShader, GraphicsInfo
577*/
578
579QByteArray QQuickShaderEffect::vertexShader() const
580{
581#if QT_CONFIG(opengl)
582 if (m_glImpl)
583 return m_glImpl->vertexShader();
584#endif
585 return m_impl->vertexShader();
586}
587
588void QQuickShaderEffect::setVertexShader(const QByteArray &code)
589{
590#if QT_CONFIG(opengl)
591 if (m_glImpl) {
592 m_glImpl->setVertexShader(code);
593 return;
594 }
595#endif
596 m_impl->setVertexShader(code);
597}
598
599/*!
600 \qmlproperty bool QtQuick::ShaderEffect::blending
601
602 If this property is true, the output from the \l fragmentShader is blended
603 with the background using source-over blend mode. If false, the background
604 is disregarded. Blending decreases the performance, so you should set this
605 property to false when blending is not needed. The default value is true.
606*/
607
608bool QQuickShaderEffect::blending() const
609{
610#if QT_CONFIG(opengl)
611 if (m_glImpl)
612 return m_glImpl->blending();
613#endif
614 return m_impl->blending();
615}
616
617void QQuickShaderEffect::setBlending(bool enable)
618{
619#if QT_CONFIG(opengl)
620 if (m_glImpl) {
621 m_glImpl->setBlending(enable);
622 return;
623 }
624#endif
625 m_impl->setBlending(enable);
626}
627
628/*!
629 \qmlproperty variant QtQuick::ShaderEffect::mesh
630
631 This property defines the mesh used to draw the ShaderEffect. It can hold
632 any \l GridMesh object.
633 If a size value is assigned to this property, the ShaderEffect implicitly
634 uses a \l GridMesh with the value as
635 \l{GridMesh::resolution}{mesh resolution}. By default, this property is
636 the size 1x1.
637
638 \sa GridMesh
639*/
640
641QVariant QQuickShaderEffect::mesh() const
642{
643#if QT_CONFIG(opengl)
644 if (m_glImpl)
645 return m_glImpl->mesh();
646#endif
647 return m_impl->mesh();
648}
649
650void QQuickShaderEffect::setMesh(const QVariant &mesh)
651{
652#if QT_CONFIG(opengl)
653 if (m_glImpl) {
654 m_glImpl->setMesh(mesh);
655 return;
656 }
657#endif
658 m_impl->setMesh(mesh);
659}
660
661/*!
662 \qmlproperty enumeration QtQuick::ShaderEffect::cullMode
663
664 This property defines which sides of the item should be visible.
665
666 \list
667 \li ShaderEffect.NoCulling - Both sides are visible
668 \li ShaderEffect.BackFaceCulling - only front side is visible
669 \li ShaderEffect.FrontFaceCulling - only back side is visible
670 \endlist
671
672 The default is NoCulling.
673*/
674
675QQuickShaderEffect::CullMode QQuickShaderEffect::cullMode() const
676{
677#if QT_CONFIG(opengl)
678 if (m_glImpl)
679 return m_glImpl->cullMode();
680#endif
681 return m_impl->cullMode();
682}
683
684void QQuickShaderEffect::setCullMode(CullMode face)
685{
686#if QT_CONFIG(opengl)
687 if (m_glImpl) {
688 m_glImpl->setCullMode(face);
689 return;
690 }
691#endif
692 return m_impl->setCullMode(face);
693}
694
695/*!
696 \qmlproperty bool QtQuick::ShaderEffect::supportsAtlasTextures
697
698 Set this property true to confirm that your shader code doesn't rely on
699 qt_MultiTexCoord0 ranging from (0,0) to (1,1) relative to the mesh.
700 In this case the range of qt_MultiTexCoord0 will rather be based on the position
701 of the texture within the atlas. This property currently has no effect if there
702 is less, or more, than one sampler uniform used as input to your shader.
703
704 This differs from providing qt_SubRect_<name> uniforms in that the latter allows
705 drawing one or more textures from the atlas in a single ShaderEffect item, while
706 supportsAtlasTextures allows multiple instances of a ShaderEffect component using
707 a different source image from the atlas to be batched in a single draw.
708 Both prevent a texture from being copied out of the atlas when referenced by a ShaderEffect.
709
710 The default value is false.
711
712 \since 5.4
713 \since QtQuick 2.4
714*/
715
716bool QQuickShaderEffect::supportsAtlasTextures() const
717{
718#if QT_CONFIG(opengl)
719 if (m_glImpl)
720 return m_glImpl->supportsAtlasTextures();
721#endif
722 return m_impl->supportsAtlasTextures();
723}
724
725void QQuickShaderEffect::setSupportsAtlasTextures(bool supports)
726{
727#if QT_CONFIG(opengl)
728 if (m_glImpl) {
729 m_glImpl->setSupportsAtlasTextures(supports);
730 return;
731 }
732#endif
733 m_impl->setSupportsAtlasTextures(supports);
734}
735
736/*!
737 \qmlproperty enumeration QtQuick::ShaderEffect::status
738
739 This property tells the current status of the OpenGL shader program.
740
741 \list
742 \li ShaderEffect.Compiled - the shader program was successfully compiled and linked.
743 \li ShaderEffect.Uncompiled - the shader program has not yet been compiled.
744 \li ShaderEffect.Error - the shader program failed to compile or link.
745 \endlist
746
747 When setting the fragment or vertex shader source code, the status will
748 become Uncompiled. The first time the ShaderEffect is rendered with new
749 shader source code, the shaders are compiled and linked, and the status is
750 updated to Compiled or Error.
751
752 When runtime compilation is not in use and the shader properties refer to
753 files with bytecode, the status is always Compiled. The contents of the
754 shader is not examined (apart from basic reflection to discover vertex
755 input elements and constant buffer data) until later in the rendering
756 pipeline so potential errors (like layout or root signature mismatches)
757 will only be detected at a later point.
758
759 \sa log
760*/
761
762/*!
763 \qmlproperty string QtQuick::ShaderEffect::log
764
765 This property holds a log of warnings and errors from the latest attempt at
766 compiling and linking the OpenGL shader program. It is updated at the same
767 time \l status is set to Compiled or Error.
768
769 \sa status
770*/
771
772QString QQuickShaderEffect::log() const
773{
774#if QT_CONFIG(opengl)
775 if (m_glImpl)
776 return m_glImpl->log();
777#endif
778 return m_impl->log();
779}
780
781QQuickShaderEffect::Status QQuickShaderEffect::status() const
782{
783#if QT_CONFIG(opengl)
784 if (m_glImpl)
785 return m_glImpl->status();
786#endif
787 return m_impl->status();
788}
789
790bool QQuickShaderEffect::event(QEvent *e)
791{
792#if QT_CONFIG(opengl)
793 if (m_glImpl) {
794 m_glImpl->handleEvent(e);
795 return QQuickItem::event(e);
796 }
797#endif
798 m_impl->handleEvent(e);
799 return QQuickItem::event(e);
800}
801
802void QQuickShaderEffect::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
803{
804#if QT_CONFIG(opengl)
805 if (m_glImpl) {
806 m_glImpl->handleGeometryChanged(newGeometry, oldGeometry);
807 QQuickItem::geometryChanged(newGeometry, oldGeometry);
808 return;
809 }
810#endif
811 m_impl->handleGeometryChanged(newGeometry, oldGeometry);
812 QQuickItem::geometryChanged(newGeometry, oldGeometry);
813}
814
815QSGNode *QQuickShaderEffect::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *updatePaintNodeData)
816{
817#if QT_CONFIG(opengl)
818 if (m_glImpl)
819 return m_glImpl->handleUpdatePaintNode(oldNode, updatePaintNodeData);
820#endif
821 return m_impl->handleUpdatePaintNode(oldNode, updatePaintNodeData);
822}
823
824void QQuickShaderEffect::componentComplete()
825{
826#if QT_CONFIG(opengl)
827 if (m_glImpl) {
828 m_glImpl->maybeUpdateShaders();
829 QQuickItem::componentComplete();
830 return;
831 }
832#endif
833 m_impl->maybeUpdateShaders();
834 QQuickItem::componentComplete();
835}
836
837void QQuickShaderEffect::itemChange(ItemChange change, const ItemChangeData &value)
838{
839#if QT_CONFIG(opengl)
840 if (m_glImpl) {
841 m_glImpl->handleItemChange(change, value);
842 QQuickItem::itemChange(change, value);
843 return;
844 }
845#endif
846 m_impl->handleItemChange(change, value);
847 QQuickItem::itemChange(change, value);
848}
849
850bool QQuickShaderEffect::isComponentComplete() const
851{
852 return QQuickItem::isComponentComplete();
853}
854
855QString QQuickShaderEffect::parseLog() // for OpenGL-based autotests
856{
857#if QT_CONFIG(opengl)
858 if (m_glImpl)
859 return m_glImpl->parseLog();
860#endif
861 return m_impl->parseLog();
862}
863
864void QQuickShaderEffectPrivate::updatePolish()
865{
866 Q_Q(QQuickShaderEffect);
867#if QT_CONFIG(opengl)
868 if (q->m_glImpl) {
869 q->m_glImpl->maybeUpdateShaders();
870 return;
871 }
872#endif
873 q->m_impl->maybeUpdateShaders();
874}
875
876#if QT_CONFIG(opengl)
877bool QQuickShaderEffect::isOpenGLShaderEffect() const
878{
879 return m_glImpl != nullptr;
880}
881#endif
882
883QT_END_NAMESPACE
884
885#include "moc_qquickshadereffect_p.cpp"
886