1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
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 QT3DRENDER_RENDER_SHADER_H |
5 | #define QT3DRENDER_RENDER_SHADER_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists for the convenience |
12 | // of other Qt classes. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <Qt3DRender/private/backendnode_p.h> |
19 | #include <Qt3DRender/qshaderprogram.h> |
20 | |
21 | QT_BEGIN_NAMESPACE |
22 | |
23 | class QOpenGLShaderProgram; |
24 | |
25 | namespace Qt3DRender { |
26 | |
27 | namespace Render { |
28 | |
29 | class ShaderManager; |
30 | class AttachmentPack; |
31 | |
32 | class Q_3DRENDERSHARED_PRIVATE_EXPORT Shader : public BackendNode |
33 | { |
34 | public: |
35 | static const int modelMatrixNameId; |
36 | static const int viewMatrixNameId; |
37 | static const int projectionMatrixNameId; |
38 | static const int modelViewMatrixNameId; |
39 | static const int viewProjectionMatrixNameId; |
40 | static const int modelViewProjectionNameId; |
41 | static const int mvpNameId; |
42 | static const int inverseModelMatrixNameId; |
43 | static const int inverseViewMatrixNameId; |
44 | static const int inverseProjectionMatrixNameId; |
45 | static const int inverseModelViewNameId; |
46 | static const int inverseViewProjectionMatrixNameId; |
47 | static const int inverseModelViewProjectionNameId; |
48 | static const int modelNormalMatrixNameId; |
49 | static const int modelViewNormalNameId; |
50 | static const int viewportMatrixNameId; |
51 | static const int inverseViewportMatrixNameId; |
52 | static const int textureTransformMatrixNameId; |
53 | static const int aspectRatioNameId; |
54 | static const int exposureNameId; |
55 | static const int gammaNameId; |
56 | static const int timeNameId; |
57 | static const int eyePositionNameId; |
58 | static const int skinningPaletteNameId; |
59 | static const int yUpInFBOId; |
60 | static const int yUpInNDCId; |
61 | |
62 | Shader(); |
63 | ~Shader(); |
64 | |
65 | void cleanup(); |
66 | |
67 | const std::vector<QByteArray> &shaderCode() const; |
68 | void setShaderCode(QShaderProgram::ShaderType type, const QByteArray &code); |
69 | |
70 | void syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) override; |
71 | |
72 | inline QString log() const { return m_log; } |
73 | inline QShaderProgram::Status status() const { return m_status; } |
74 | |
75 | void setFormat(QShaderProgram::Format format); |
76 | QShaderProgram::Format format() const { return m_format; } |
77 | bool isDirty() const { return m_dirty; } |
78 | void unsetDirty() { m_dirty = false; } |
79 | |
80 | inline bool requiresFrontendSync() const { return m_requiresFrontendSync; } |
81 | inline void unsetRequiresFrontendSync() { m_requiresFrontendSync = false; } |
82 | |
83 | // Set by Renderer plugin |
84 | void setLog(const QString &log); |
85 | void setStatus(QShaderProgram::Status status); |
86 | void initializeFromReference(const Shader &other); |
87 | |
88 | void requestCacheRebuild(); |
89 | |
90 | private: |
91 | std::vector<QByteArray> m_shaderCode; |
92 | |
93 | QString m_log; |
94 | bool m_requiresFrontendSync; |
95 | QShaderProgram::Status m_status; |
96 | QShaderProgram::Format m_format; |
97 | bool m_dirty; |
98 | }; |
99 | |
100 | #ifndef QT_NO_DEBUG_STREAM |
101 | inline QDebug operator<<(QDebug dbg, const Shader &shader) |
102 | { |
103 | QDebugStateSaver saver(dbg); |
104 | dbg << "QNodeId =" << shader.peerId() << Qt::endl; |
105 | return dbg; |
106 | } |
107 | #endif |
108 | |
109 | class Q_AUTOTEST_EXPORT ShaderFunctor : public Qt3DCore::QBackendNodeMapper |
110 | { |
111 | public: |
112 | explicit ShaderFunctor(AbstractRenderer *renderer, |
113 | ShaderManager *manager); |
114 | Qt3DCore::QBackendNode *create(Qt3DCore::QNodeId id) const final; |
115 | Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const final; |
116 | void destroy(Qt3DCore::QNodeId id) const final; |
117 | |
118 | private: |
119 | AbstractRenderer *m_renderer; |
120 | ShaderManager *m_shaderManager; |
121 | }; |
122 | |
123 | } // namespace Render |
124 | } // namespace Qt3DRender |
125 | |
126 | QT_END_NAMESPACE |
127 | |
128 | #endif // QT3DRENDER_RENDER_SHADER_H |
129 | |