1/****************************************************************************
2**
3** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
4** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the Qt3D module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QT3DRENDER_RENDER_OPENGL_RENDERCOMMAND_H
42#define QT3DRENDER_RENDER_OPENGL_RENDERCOMMAND_H
43
44//
45// W A R N I N G
46// -------------
47//
48// This file is not part of the Qt API. It exists for the convenience
49// of other Qt classes. This header file may change from version to
50// version without notice, or even be removed.
51//
52// We mean it.
53//
54
55#include <qglobal.h>
56#include <shaderparameterpack_p.h>
57#include <gl_handle_types_p.h>
58#include <renderviewjobutils_p.h>
59#include <Qt3DRender/private/handle_types_p.h>
60#include <Qt3DRender/qgeometryrenderer.h>
61#include <QOpenGLShaderProgram>
62#include <QOpenGLTexture>
63#include <QMatrix4x4>
64#include <vector>
65
66QT_BEGIN_NAMESPACE
67
68class QOpenGLVertexArrayObject;
69
70namespace Qt3DRender {
71
72namespace Render {
73
74class RenderStateSet;
75using RenderStateSetPtr = QSharedPointer<RenderStateSet>;
76
77enum RebuildFlag {
78 FullCommandRebuild = 1 << 0,
79 LayerCacheRebuild = 1 << 1,
80 MaterialCacheRebuild = 1 << 2,
81 LightCacheRebuild = 1 << 3
82};
83Q_DECLARE_FLAGS(RebuildFlagSet, RebuildFlag)
84Q_DECLARE_OPERATORS_FOR_FLAGS(RebuildFlagSet)
85
86namespace OpenGL {
87
88class GLShader;
89
90class Q_AUTOTEST_EXPORT RenderCommand
91{
92public:
93 RenderCommand();
94
95 HVao m_vao; // VAO used during the submission step to store all states and VBOs
96 HMaterial m_material; // Purely used to ease sorting (minimize stage changes, binding changes ....)
97 GLShader *m_glShader; // GL Shader to be used at render time
98 Qt3DCore::QNodeId m_shaderId; // Shader for given pass and mesh
99 ShaderParameterPack m_parameterPack; // Might need to be reworked so as to be able to destroy the
100 // Texture while submission is happening.
101 RenderStateSetPtr m_stateSet;
102
103 HGeometry m_geometry;
104 HGeometryRenderer m_geometryRenderer;
105
106 HBuffer m_indirectDrawBuffer; // Reference to indirect draw buffer (valid only m_drawIndirect == true)
107 HComputeCommand m_computeCommand;
108
109 // A QAttribute pack might be interesting
110 // This is a temporary fix in the meantime, to remove the hacked methods in Technique
111 QVector<int> m_activeAttributes;
112
113 float m_depth;
114 int m_changeCost;
115
116 enum CommandType {
117 Draw,
118 Compute
119 };
120
121 CommandType m_type;
122 int m_workGroups[3];
123
124 // Values filled for draw calls by Renderer (in prepare Submission)
125 GLsizei m_primitiveCount;
126 QGeometryRenderer::PrimitiveType m_primitiveType;
127 int m_restartIndexValue;
128 int m_firstInstance;
129 int m_firstVertex;
130 int m_verticesPerPatch;
131 int m_instanceCount;
132 int m_indexOffset;
133 uint m_indexAttributeByteOffset;
134 GLint m_indexAttributeDataType;
135 uint m_indirectAttributeByteOffset;
136 bool m_drawIndexed;
137 bool m_drawIndirect;
138 bool m_primitiveRestartEnabled;
139 bool m_isValid;
140};
141
142Q_AUTOTEST_EXPORT bool operator==(const RenderCommand &a, const RenderCommand &b) noexcept;
143
144inline bool operator!=(const RenderCommand &lhs, const RenderCommand &rhs) noexcept
145{ return !operator==(a: lhs, b: rhs); }
146
147struct EntityRenderCommandData
148{
149 std::vector<Entity *> entities;
150 std::vector<RenderCommand> commands;
151 std::vector<RenderPassParameterData> passesData;
152
153 void reserve(size_t size)
154 {
155 entities.reserve(n: size);
156 commands.reserve(n: size);
157 passesData.reserve(n: size);
158 }
159
160 inline size_t size() const { return entities.size(); }
161
162 inline void push_back(Entity *e, const RenderCommand &c, const RenderPassParameterData &p)
163 {
164 entities.push_back(x: e);
165 commands.push_back(x: c);
166 passesData.push_back(x: p);
167 }
168
169 inline void push_back(Entity *e, RenderCommand &&c, RenderPassParameterData &&p)
170 {
171 entities.push_back(x: e);
172 commands.push_back(x: std::move(c));
173 passesData.push_back(x: std::move(p));
174 }
175
176 EntityRenderCommandData &operator+=(EntityRenderCommandData &&t)
177 {
178 entities.insert(position: entities.cend(),
179 first: std::make_move_iterator(i: t.entities.begin()),
180 last: std::make_move_iterator(i: t.entities.end()));
181 commands.insert(position: commands.cend(),
182 first: std::make_move_iterator(i: t.commands.begin()),
183 last: std::make_move_iterator(i: t.commands.end()));
184 passesData.insert(position: passesData.cend(),
185 first: std::make_move_iterator(i: t.passesData.begin()),
186 last: std::make_move_iterator(i: t.passesData.end()));
187 return *this;
188 }
189
190};
191
192struct EntityRenderCommandDataView
193{
194 EntityRenderCommandData data;
195 std::vector<size_t> indices;
196
197 size_t size() const noexcept { return indices.size(); }
198
199 template<typename F>
200 void forEachCommand(F func)
201 {
202 for (size_t idx : indices)
203 func(data.commands[idx]);
204 }
205};
206using EntityRenderCommandDataViewPtr = QSharedPointer<EntityRenderCommandDataView>;
207
208struct EntityRenderCommandDataSubView
209{
210 EntityRenderCommandDataViewPtr view;
211 size_t offset;
212 size_t count;
213
214 template<typename F>
215 void forEach(F func)
216 {
217 for (size_t i = 0, m = size_t(count); i < m; ++i) {
218 const size_t idx = view->indices[offset + i];
219 func(view->data.entities[idx],
220 view->data.passesData[idx],
221 view->data.commands[idx]);
222 }
223 }
224
225 template<typename F>
226 void forEach(F func) const
227 {
228 for (size_t i = 0, m = size_t(count); i < m; ++i) {
229 const size_t idx = view->indices[offset + i];
230 func(view->data.entities[idx],
231 view->data.passesData[idx],
232 view->data.commands[idx]);
233 }
234 }
235};
236
237} // namespace OpenGL
238
239} // namespace Render
240
241} // namespace Qt3DRender
242
243QT_END_NAMESPACE
244
245#endif // QT3DRENDER_RENDER_OPENGL_RENDERCOMMAND_H
246

source code of qt3d/src/plugins/renderers/opengl/renderer/rendercommand_p.h