1// Copyright (C) 2019 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
4#ifndef QSGMATERIALSHADER_H
5#define QSGMATERIALSHADER_H
6
7#include <QtQuick/qtquickglobal.h>
8#include <QtCore/QRect>
9#include <QtGui/QMatrix4x4>
10#include <QtGui/QColor>
11#include <QtQuick/qsgmaterialtype.h>
12
13QT_BEGIN_NAMESPACE
14
15class QSGMaterial;
16class QSGMaterialShaderPrivate;
17class QSGTexture;
18class QRhiResourceUpdateBatch;
19class QRhi;
20class QShader;
21
22class Q_QUICK_EXPORT QSGMaterialShader
23{
24public:
25 class Q_QUICK_EXPORT RenderState {
26 public:
27 enum DirtyState
28 {
29 DirtyMatrix = 0x0001,
30 DirtyOpacity = 0x0002,
31 DirtyCachedMaterialData = 0x0004,
32 DirtyAll = 0xFFFF
33 };
34 Q_DECLARE_FLAGS(DirtyStates, DirtyState)
35
36 inline DirtyStates dirtyStates() const { return m_dirty; }
37
38 inline bool isMatrixDirty() const { return bool(m_dirty & QSGMaterialShader::RenderState::DirtyMatrix); }
39 inline bool isOpacityDirty() const { return bool(m_dirty & QSGMaterialShader::RenderState::DirtyOpacity); }
40
41 float opacity() const;
42 QMatrix4x4 combinedMatrix() const;
43 QMatrix4x4 modelViewMatrix() const;
44 QMatrix4x4 projectionMatrix() const;
45 QRect viewportRect() const;
46 QRect deviceRect() const;
47 float determinant() const;
48 float devicePixelRatio() const;
49
50 QByteArray *uniformData();
51 QRhiResourceUpdateBatch *resourceUpdateBatch();
52 QRhi *rhi();
53
54 private:
55 friend class QSGRenderer;
56 DirtyStates m_dirty;
57 const void *m_data;
58 };
59
60 struct GraphicsPipelineState {
61 enum BlendFactor {
62 Zero,
63 One,
64 SrcColor,
65 OneMinusSrcColor,
66 DstColor,
67 OneMinusDstColor,
68 SrcAlpha,
69 OneMinusSrcAlpha,
70 DstAlpha,
71 OneMinusDstAlpha,
72 ConstantColor,
73 OneMinusConstantColor,
74 ConstantAlpha,
75 OneMinusConstantAlpha,
76 SrcAlphaSaturate,
77 Src1Color,
78 OneMinusSrc1Color,
79 Src1Alpha,
80 OneMinusSrc1Alpha
81 };
82
83 enum ColorMaskComponent {
84 R = 1 << 0,
85 G = 1 << 1,
86 B = 1 << 2,
87 A = 1 << 3
88 };
89 Q_DECLARE_FLAGS(ColorMask, ColorMaskComponent)
90
91 enum CullMode {
92 CullNone,
93 CullFront,
94 CullBack
95 };
96
97 enum PolygonMode {
98 Fill,
99 Line,
100 };
101
102 bool blendEnable;
103 BlendFactor srcColor;
104 BlendFactor dstColor;
105 ColorMask colorWrite;
106 QColor blendConstant;
107 CullMode cullMode;
108 PolygonMode polygonMode;
109 bool separateBlendFactors;
110 BlendFactor srcAlpha;
111 BlendFactor dstAlpha;
112 // This struct is extensible while keeping BC since apps only ever get
113 // a ptr to the struct, it is not created by them.
114 };
115
116 enum Flag {
117 UpdatesGraphicsPipelineState = 0x0001
118 };
119 Q_DECLARE_FLAGS(Flags, Flag)
120
121 enum Stage {
122 VertexStage,
123 FragmentStage,
124 };
125
126 QSGMaterialShader();
127 virtual ~QSGMaterialShader();
128
129 virtual bool updateUniformData(RenderState &state,
130 QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
131
132 virtual void updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
133 QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
134
135 virtual bool updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps,
136 QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
137
138 Flags flags() const;
139 void setFlag(Flags flags, bool on = true);
140 void setFlags(Flags flags);
141
142 int combinedImageSamplerCount(int binding) const;
143
144protected:
145 Q_DECLARE_PRIVATE(QSGMaterialShader)
146 QSGMaterialShader(QSGMaterialShaderPrivate &dd);
147
148 // filename is for a file containing a serialized QShader.
149 void setShaderFileName(Stage stage, const QString &filename);
150
151 void setShader(Stage stage, const QShader &shader);
152
153private:
154 QScopedPointer<QSGMaterialShaderPrivate> d_ptr;
155};
156
157Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::RenderState::DirtyStates)
158Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::GraphicsPipelineState::ColorMask)
159Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::Flags)
160
161QT_END_NAMESPACE
162
163#endif
164

source code of qtdeclarative/src/quick/scenegraph/coreapi/qsgmaterialshader.h