| 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 |  | 
|---|
| 13 | QT_BEGIN_NAMESPACE | 
|---|
| 14 |  | 
|---|
| 15 | class QSGMaterial; | 
|---|
| 16 | class QSGMaterialShaderPrivate; | 
|---|
| 17 | class QSGTexture; | 
|---|
| 18 | class QRhiResourceUpdateBatch; | 
|---|
| 19 | class QRhi; | 
|---|
| 20 | class QShader; | 
|---|
| 21 |  | 
|---|
| 22 | class Q_QUICK_EXPORT QSGMaterialShader | 
|---|
| 23 | { | 
|---|
| 24 | public: | 
|---|
| 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 combinedMatrix(qsizetype index) const; | 
|---|
| 44 | QMatrix4x4 modelViewMatrix() const; | 
|---|
| 45 | QMatrix4x4 projectionMatrix() const; | 
|---|
| 46 | QMatrix4x4 projectionMatrix(qsizetype index) const; | 
|---|
| 47 | qsizetype projectionMatrixCount() const; | 
|---|
| 48 | QRect viewportRect() const; | 
|---|
| 49 | QRect deviceRect() const; | 
|---|
| 50 | float determinant() const; | 
|---|
| 51 | float devicePixelRatio() const; | 
|---|
| 52 |  | 
|---|
| 53 | QByteArray *uniformData(); | 
|---|
| 54 | QRhiResourceUpdateBatch *resourceUpdateBatch(); | 
|---|
| 55 | QRhi *rhi(); | 
|---|
| 56 |  | 
|---|
| 57 | private: | 
|---|
| 58 | friend class QSGRenderer; | 
|---|
| 59 | DirtyStates m_dirty; | 
|---|
| 60 | const void *m_data; | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | struct GraphicsPipelineState { | 
|---|
| 64 | enum BlendFactor { | 
|---|
| 65 | Zero, | 
|---|
| 66 | One, | 
|---|
| 67 | SrcColor, | 
|---|
| 68 | OneMinusSrcColor, | 
|---|
| 69 | DstColor, | 
|---|
| 70 | OneMinusDstColor, | 
|---|
| 71 | SrcAlpha, | 
|---|
| 72 | OneMinusSrcAlpha, | 
|---|
| 73 | DstAlpha, | 
|---|
| 74 | OneMinusDstAlpha, | 
|---|
| 75 | ConstantColor, | 
|---|
| 76 | OneMinusConstantColor, | 
|---|
| 77 | ConstantAlpha, | 
|---|
| 78 | OneMinusConstantAlpha, | 
|---|
| 79 | SrcAlphaSaturate, | 
|---|
| 80 | Src1Color, | 
|---|
| 81 | OneMinusSrc1Color, | 
|---|
| 82 | Src1Alpha, | 
|---|
| 83 | OneMinusSrc1Alpha | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | enum class BlendOp { | 
|---|
| 87 | Add, | 
|---|
| 88 | Subtract, | 
|---|
| 89 | ReverseSubtract, | 
|---|
| 90 | Min, | 
|---|
| 91 | Max, | 
|---|
| 92 | }; | 
|---|
| 93 |  | 
|---|
| 94 | enum ColorMaskComponent { | 
|---|
| 95 | R = 1 << 0, | 
|---|
| 96 | G = 1 << 1, | 
|---|
| 97 | B = 1 << 2, | 
|---|
| 98 | A = 1 << 3 | 
|---|
| 99 | }; | 
|---|
| 100 | Q_DECLARE_FLAGS(ColorMask, ColorMaskComponent) | 
|---|
| 101 |  | 
|---|
| 102 | enum CullMode { | 
|---|
| 103 | CullNone, | 
|---|
| 104 | CullFront, | 
|---|
| 105 | CullBack | 
|---|
| 106 | }; | 
|---|
| 107 |  | 
|---|
| 108 | enum PolygonMode { | 
|---|
| 109 | Fill, | 
|---|
| 110 | Line, | 
|---|
| 111 | }; | 
|---|
| 112 |  | 
|---|
| 113 | bool blendEnable; | 
|---|
| 114 | BlendFactor srcColor; | 
|---|
| 115 | BlendFactor dstColor; | 
|---|
| 116 | ColorMask colorWrite; | 
|---|
| 117 | QColor blendConstant; | 
|---|
| 118 | CullMode cullMode; | 
|---|
| 119 | PolygonMode polygonMode; | 
|---|
| 120 | bool separateBlendFactors; | 
|---|
| 121 | BlendFactor srcAlpha; | 
|---|
| 122 | BlendFactor dstAlpha; | 
|---|
| 123 | BlendOp opColor; | 
|---|
| 124 | BlendOp opAlpha; | 
|---|
| 125 |  | 
|---|
| 126 | // This struct is extensible while keeping BC since apps only ever get | 
|---|
| 127 | // a ptr to the struct, it is not created by them. | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 | enum Flag { | 
|---|
| 131 | UpdatesGraphicsPipelineState = 0x0001 | 
|---|
| 132 | }; | 
|---|
| 133 | Q_DECLARE_FLAGS(Flags, Flag) | 
|---|
| 134 |  | 
|---|
| 135 | enum Stage { | 
|---|
| 136 | VertexStage, | 
|---|
| 137 | FragmentStage, | 
|---|
| 138 | }; | 
|---|
| 139 |  | 
|---|
| 140 | QSGMaterialShader(); | 
|---|
| 141 | virtual ~QSGMaterialShader(); | 
|---|
| 142 |  | 
|---|
| 143 | virtual bool updateUniformData(RenderState &state, | 
|---|
| 144 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial); | 
|---|
| 145 |  | 
|---|
| 146 | virtual void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, | 
|---|
| 147 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial); | 
|---|
| 148 |  | 
|---|
| 149 | virtual bool updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps, | 
|---|
| 150 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial); | 
|---|
| 151 |  | 
|---|
| 152 | Flags flags() const; | 
|---|
| 153 | void setFlag(Flags flags, bool on = true); | 
|---|
| 154 | void setFlags(Flags flags); | 
|---|
| 155 |  | 
|---|
| 156 | int combinedImageSamplerCount(int binding) const; | 
|---|
| 157 |  | 
|---|
| 158 | protected: | 
|---|
| 159 | Q_DECLARE_PRIVATE(QSGMaterialShader) | 
|---|
| 160 | QSGMaterialShader(QSGMaterialShaderPrivate &dd); | 
|---|
| 161 |  | 
|---|
| 162 | // filename is for a file containing a serialized QShader. | 
|---|
| 163 | void setShaderFileName(Stage stage, const QString &filename); | 
|---|
| 164 | void setShaderFileName(Stage stage, const QString &filename, int viewCount); | 
|---|
| 165 |  | 
|---|
| 166 | void setShader(Stage stage, const QShader &shader); | 
|---|
| 167 |  | 
|---|
| 168 | private: | 
|---|
| 169 | QScopedPointer<QSGMaterialShaderPrivate> d_ptr; | 
|---|
| 170 | }; | 
|---|
| 171 |  | 
|---|
| 172 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::RenderState::DirtyStates) | 
|---|
| 173 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::GraphicsPipelineState::ColorMask) | 
|---|
| 174 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::Flags) | 
|---|
| 175 |  | 
|---|
| 176 | QT_END_NAMESPACE | 
|---|
| 177 |  | 
|---|
| 178 | #endif | 
|---|
| 179 |  | 
|---|