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_RENDERVIEW_H
42#define QT3DRENDER_RENDER_OPENGL_RENDERVIEW_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 <Qt3DRender/qparameter.h>
56#include <Qt3DRender/qclearbuffers.h>
57#include <Qt3DRender/qlayerfilter.h>
58#include <Qt3DRender/private/clearbuffers_p.h>
59#include <Qt3DRender/private/cameralens_p.h>
60#include <Qt3DRender/private/attachmentpack_p.h>
61#include <Qt3DRender/private/handle_types_p.h>
62#include <Qt3DRender/private/qsortpolicy_p.h>
63#include <Qt3DRender/private/lightsource_p.h>
64#include <Qt3DRender/private/qmemorybarrier_p.h>
65#include <Qt3DRender/private/qrendercapture_p.h>
66#include <Qt3DRender/private/qblitframebuffer_p.h>
67#include <Qt3DRender/private/qwaitfence_p.h>
68
69#include <Qt3DCore/private/qframeallocator_p.h>
70#include <Qt3DRender/private/aligned_malloc_p.h>
71
72#include <renderer_p.h>
73// TODO: Move out once this is all refactored
74#include <renderviewjobutils_p.h>
75
76#include <QVector>
77#include <QSurface>
78#include <QMutex>
79#include <QColor>
80
81QT_BEGIN_NAMESPACE
82
83namespace Qt3DRender {
84
85class QRenderPass;
86
87namespace Render {
88
89class NodeManagers;
90class RenderPassFilter;
91class TechniqueFilter;
92class ViewportNode;
93class Effect;
94class RenderPass;
95
96namespace OpenGL {
97
98class Renderer;
99class RenderCommand;
100
101typedef QPair<ShaderUniform, QVariant> ActivePropertyContent;
102typedef QPair<QString, ActivePropertyContent > ActiveProperty;
103
104struct Q_AUTOTEST_EXPORT ClearBufferInfo
105{
106 int drawBufferIndex = 0;
107 QRenderTargetOutput::AttachmentPoint attchmentPoint = QRenderTargetOutput::Color0;
108 QVector4D clearColor;
109};
110
111struct Q_AUTOTEST_EXPORT BlitFramebufferInfo
112{
113 Qt3DCore::QNodeId sourceRenderTargetId;
114 Qt3DCore::QNodeId destinationRenderTargetId;
115 QRect sourceRect;
116 QRect destinationRect;
117 Qt3DRender::QRenderTargetOutput::AttachmentPoint sourceAttachmentPoint;
118 Qt3DRender::QRenderTargetOutput::AttachmentPoint destinationAttachmentPoint;
119 QBlitFramebuffer::InterpolationMethod interpolationMethod;
120};
121
122// This class is kind of analogous to RenderBin but I want to avoid trampling
123// on that until we get this working
124
125class Q_AUTOTEST_EXPORT RenderView
126{
127public:
128 RenderView();
129 ~RenderView();
130
131 QT3D_ALIGNED_MALLOC_AND_FREE()
132
133 // TODO: Add a way to specify a sort predicate for the RenderCommands
134 void sort();
135
136 void setRenderer(Renderer *renderer);
137 inline void setSurfaceSize(const QSize &size) Q_DECL_NOTHROW { m_surfaceSize = size; }
138 inline Renderer *renderer() const Q_DECL_NOTHROW { return m_renderer; }
139 inline NodeManagers *nodeManagers() const Q_DECL_NOTHROW { return m_manager; }
140 inline const QSize &surfaceSize() const Q_DECL_NOTHROW { return m_surfaceSize; }
141 inline void setDevicePixelRatio(qreal r) Q_DECL_NOTHROW { m_devicePixelRatio = r; }
142 inline qreal devicePixelRatio() const Q_DECL_NOTHROW { return m_devicePixelRatio; }
143
144 inline void setRenderCameraLens(CameraLens *renderCameraLens) Q_DECL_NOTHROW { m_renderCameraLens = renderCameraLens; }
145 inline CameraLens *renderCameraLens() const Q_DECL_NOTHROW { return m_renderCameraLens; }
146
147 inline void setRenderCameraEntity(Entity *renderCameraNode) Q_DECL_NOTHROW { m_renderCameraNode = renderCameraNode; }
148 inline Entity *renderCameraEntity() const Q_DECL_NOTHROW { return m_renderCameraNode; }
149
150 inline void setViewMatrix(const Matrix4x4 &viewMatrix) Q_DECL_NOTHROW { m_viewMatrix = viewMatrix; }
151 inline Matrix4x4 viewMatrix() const Q_DECL_NOTHROW { return m_viewMatrix; }
152
153 inline void setViewProjectionMatrix(const Matrix4x4 &viewProjectionMatrix) Q_DECL_NOTHROW { m_viewProjectionMatrix = viewProjectionMatrix; }
154 inline Matrix4x4 viewProjectionMatrix() const Q_DECL_NOTHROW { return m_viewProjectionMatrix; }
155
156 inline void setEyePosition(const Vector3D &eyePos) Q_DECL_NOTHROW { m_eyePos = eyePos; }
157 inline Vector3D eyePosition() const Q_DECL_NOTHROW { return m_eyePos; }
158
159 inline void setEyeViewDirection(const Vector3D &dir) Q_DECL_NOTHROW { m_eyeViewDir = dir; }
160 inline Vector3D eyeViewDirection() const Q_DECL_NOTHROW { return m_eyeViewDir; }
161
162 inline void appendLayerFilter(const Qt3DCore::QNodeId layerFilterId) Q_DECL_NOTHROW { m_layerFilterIds.push_back(t: layerFilterId); }
163 inline Qt3DCore::QNodeIdVector layerFilters() const Q_DECL_NOTHROW { return m_layerFilterIds; }
164
165 inline void appendProximityFilterId(const Qt3DCore::QNodeId proximityFilterId) { m_proximityFilterIds.push_back(t: proximityFilterId); }
166 inline Qt3DCore::QNodeIdVector proximityFilterIds() const { return m_proximityFilterIds; }
167
168 inline void appendInsertFenceId(const Qt3DCore::QNodeId setFenceId) { m_insertFenceIds.push_back(t: setFenceId); }
169 // We prefix with get to avoid confusion when it is called
170 inline Qt3DCore::QNodeIdVector insertFenceIds() const { return m_insertFenceIds; }
171
172 inline void appendWaitFence(const QWaitFenceData &data) { m_waitFences.push_back(t: data); }
173 inline QVector<QWaitFenceData> waitFences() const { return m_waitFences; }
174
175 inline void setRenderPassFilter(const RenderPassFilter *rpFilter) Q_DECL_NOTHROW { m_passFilter = rpFilter; }
176 inline const RenderPassFilter *renderPassFilter() const Q_DECL_NOTHROW { return m_passFilter; }
177
178 inline void setTechniqueFilter(const TechniqueFilter *filter) Q_DECL_NOTHROW { m_techniqueFilter = filter; }
179 inline const TechniqueFilter *techniqueFilter() const Q_DECL_NOTHROW { return m_techniqueFilter; }
180
181 inline void setRenderCommandDataView(const EntityRenderCommandDataViewPtr &renderCommandDataView) Q_DECL_NOTHROW { m_renderCommandDataView = renderCommandDataView; }
182 inline EntityRenderCommandDataViewPtr renderCommandDataView() const Q_DECL_NOTHROW { return m_renderCommandDataView; }
183
184 RenderStateSet *getOrCreateStateSet();
185 RenderStateSet *stateSet() const Q_DECL_NOTHROW { return m_stateSet.data(); }
186
187 inline bool noDraw() const Q_DECL_NOTHROW { return m_noDraw; }
188 void setNoDraw(bool noDraw) Q_DECL_NOTHROW { m_noDraw = noDraw; }
189
190 inline bool isCompute() const Q_DECL_NOTHROW { return m_compute; }
191 void setCompute(bool compute) Q_DECL_NOTHROW { m_compute = compute; }
192
193 void setComputeWorkgroups(int x, int y, int z) Q_DECL_NOTHROW { m_workGroups[0] = x; m_workGroups[1] = y; m_workGroups[2] = z; }
194 const int *computeWorkGroups() const Q_DECL_NOTHROW { return m_workGroups; }
195 inline bool frustumCulling() const Q_DECL_NOTHROW { return m_frustumCulling; }
196 void setFrustumCulling(bool frustumCulling) Q_DECL_NOTHROW { m_frustumCulling = frustumCulling; }
197 bool showDebugOverlay() const Q_DECL_NOTHROW { return m_showDebugOverlay; }
198 void setShowDebugOverlay(bool showDebugOverlay) Q_DECL_NOTHROW { m_showDebugOverlay = showDebugOverlay; }
199
200 inline void setMaterialParameterTable(const MaterialParameterGathererData &parameters) Q_DECL_NOTHROW { m_parameters = parameters; }
201
202 // TODO: Get rid of this overly complex memory management by splitting out the
203 // InnerData as a RenderViewConfig struct. This can be created by setRenderViewConfigFromFrameGraphLeafNode
204 // and passed along with the RenderView to the functions that populate the renderview
205 inline void setViewport(const QRectF &vp) Q_DECL_NOTHROW { m_viewport = vp; }
206 inline QRectF viewport() const Q_DECL_NOTHROW { return m_viewport; }
207
208 inline float gamma() const Q_DECL_NOTHROW { return m_gamma; }
209 inline void setGamma(float gamma) Q_DECL_NOTHROW { m_gamma = gamma; }
210
211 // depth and stencil ClearBuffers are cached locally
212 // color ClearBuffers are collected, as there may be multiple
213 // color buffers to be cleared. we need to apply all these at rendering
214 void addClearBuffers(const ClearBuffers *cb);
215 inline QVector<ClearBufferInfo> specificClearColorBufferInfo() const { return m_specificClearColorBuffers; }
216 inline QVector<ClearBufferInfo> &specificClearColorBufferInfo() { return m_specificClearColorBuffers; }
217 inline ClearBufferInfo globalClearColorBufferInfo() const { return m_globalClearColorBuffer; }
218
219 inline QClearBuffers::BufferTypeFlags clearTypes() const { return m_clearBuffer; }
220 inline float clearDepthValue() const { return m_clearDepthValue; }
221 inline int clearStencilValue() const { return m_clearStencilValue; }
222
223 RenderPassList passesAndParameters(ParameterInfoList *parameter, Entity *node, bool useDefaultMaterials = true);
224
225 EntityRenderCommandData buildDrawRenderCommands(const QVector<Entity *> &entities,
226 int offset, int count) const;
227 EntityRenderCommandData buildComputeRenderCommands(const QVector<Entity *> &entities,
228 int offset, int count) const;
229
230 void updateRenderCommand(const EntityRenderCommandDataSubView &subView);
231
232 void setAttachmentPack(const AttachmentPack &pack) { m_attachmentPack = pack; }
233 const AttachmentPack &attachmentPack() const { return m_attachmentPack; }
234
235 void setRenderTargetId(Qt3DCore::QNodeId renderTargetId) Q_DECL_NOTHROW { m_renderTarget = renderTargetId; }
236 Qt3DCore::QNodeId renderTargetId() const Q_DECL_NOTHROW { return m_renderTarget; }
237
238 void addSortType(const QVector<Qt3DRender::QSortPolicy::SortType> &sortTypes) { m_sortingTypes.append(l: sortTypes); }
239
240 void setSurface(QSurface *surface) { m_surface = surface; }
241 QSurface *surface() const { return m_surface; }
242
243 void setLightSources(const QVector<LightSource> &lightSources) Q_DECL_NOTHROW { m_lightSources = lightSources; }
244 void setEnvironmentLight(EnvironmentLight *environmentLight) Q_DECL_NOTHROW { m_environmentLight = environmentLight; }
245
246 void updateMatrices();
247
248 inline void setRenderCaptureNodeId(const Qt3DCore::QNodeId nodeId) Q_DECL_NOTHROW { m_renderCaptureNodeId = nodeId; }
249 inline const Qt3DCore::QNodeId renderCaptureNodeId() const Q_DECL_NOTHROW { return m_renderCaptureNodeId; }
250 inline void setRenderCaptureRequest(const QRenderCaptureRequest& request) Q_DECL_NOTHROW { m_renderCaptureRequest = request; }
251 inline const QRenderCaptureRequest renderCaptureRequest() const Q_DECL_NOTHROW { return m_renderCaptureRequest; }
252
253 void setMemoryBarrier(QMemoryBarrier::Operations barrier) Q_DECL_NOTHROW { m_memoryBarrier = barrier; }
254 QMemoryBarrier::Operations memoryBarrier() const Q_DECL_NOTHROW { return m_memoryBarrier; }
255
256 bool isDownloadBuffersEnable() const;
257 void setIsDownloadBuffersEnable(bool isDownloadBuffersEnable);
258
259 BlitFramebufferInfo blitFrameBufferInfo() const;
260 void setBlitFrameBufferInfo(const BlitFramebufferInfo &blitFrameBufferInfo);
261
262 bool hasBlitFramebufferInfo() const;
263 void setHasBlitFramebufferInfo(bool hasBlitFramebufferInfo);
264
265 bool shouldSkipSubmission() const;
266
267 template<typename F>
268 inline void forEachCommand(F func) const
269 {
270 if (!m_renderCommandDataView)
271 return;
272 m_renderCommandDataView->forEachCommand(func);
273 }
274
275 template<typename F>
276 inline void forEachCommand(F func)
277 {
278 if (!m_renderCommandDataView)
279 return;
280 m_renderCommandDataView->forEachCommand(func);
281 }
282
283 inline int commandCount() const { return m_renderCommandDataView ? m_renderCommandDataView->size() : 0; }
284
285private:
286 void setShaderAndUniforms(RenderCommand *command,
287 const ParameterInfoList &parameters,
288 const Entity *entity) const;
289
290 void updateLightUniforms(RenderCommand *command,
291 const Entity *entity) const;
292
293 mutable QThreadStorage<UniformBlockValueBuilder*> m_localData;
294
295 Renderer *m_renderer = nullptr;
296 NodeManagers *m_manager = nullptr;
297 EntityRenderCommandDataViewPtr m_renderCommandDataView;
298
299 QSize m_surfaceSize;
300 float m_devicePixelRatio = 1.0f;
301 QRectF m_viewport = QRectF(0.0f, 0.0f, 1.0f, 1.0f);
302 float m_gamma = 2.2f;
303
304 Qt3DCore::QNodeId m_renderCaptureNodeId;
305 QRenderCaptureRequest m_renderCaptureRequest;
306 bool m_isDownloadBuffersEnable = false;
307
308 bool m_hasBlitFramebufferInfo = false;
309 BlitFramebufferInfo m_blitFrameBufferInfo;
310
311 QSurface *m_surface = nullptr;
312 Qt3DCore::QNodeId m_renderTarget;
313 AttachmentPack m_attachmentPack;
314 QClearBuffers::BufferTypeFlags m_clearBuffer = QClearBuffers::None;
315 float m_clearDepthValue = 1.0f;
316 int m_clearStencilValue = 0;
317 ClearBufferInfo m_globalClearColorBuffer; // global ClearColor
318 QVector<ClearBufferInfo> m_specificClearColorBuffers; // different draw buffers with distinct colors
319
320 QScopedPointer<RenderStateSet> m_stateSet;
321 CameraLens *m_renderCameraLens = nullptr;
322 Entity *m_renderCameraNode = nullptr;
323 const TechniqueFilter *m_techniqueFilter = nullptr;
324 const RenderPassFilter *m_passFilter = nullptr;
325 bool m_noDraw = false;
326 bool m_compute = false;
327 bool m_frustumCulling = false;
328 bool m_showDebugOverlay = false;
329 int m_workGroups[3] = { 1, 1, 1};
330 QMemoryBarrier::Operations m_memoryBarrier = QMemoryBarrier::None;
331 QVector<Qt3DCore::QNodeId> m_insertFenceIds;
332 QVector<QWaitFenceData> m_waitFences;
333 QVector<Qt3DRender::QSortPolicy::SortType> m_sortingTypes;
334 Qt3DCore::QNodeIdVector m_proximityFilterIds;
335 Qt3DCore::QNodeIdVector m_layerFilterIds;
336 Matrix4x4 m_viewMatrix;
337 Matrix4x4 m_viewProjectionMatrix;
338 Vector3D m_eyePos;
339 Vector3D m_eyeViewDir;
340
341 MaterialParameterGathererData m_parameters;
342 mutable QVector<LightSource> m_lightSources;
343 EnvironmentLight *m_environmentLight = nullptr;
344
345 enum StandardUniform
346 {
347 ModelMatrix,
348 ViewMatrix,
349 ProjectionMatrix,
350 ModelViewMatrix,
351 ViewProjectionMatrix,
352 ModelViewProjectionMatrix,
353 InverseModelMatrix,
354 InverseViewMatrix,
355 InverseProjectionMatrix,
356 InverseModelViewMatrix,
357 InverseViewProjectionMatrix,
358 InverseModelViewProjectionMatrix,
359 ModelNormalMatrix,
360 ModelViewNormalMatrix,
361 ViewportMatrix,
362 InverseViewportMatrix,
363 AspectRatio,
364 Time,
365 Exposure,
366 Gamma,
367 EyePosition,
368 SkinningPalette
369 };
370
371 typedef QHash<int, StandardUniform> StandardUniformsNameToTypeHash;
372 static StandardUniformsNameToTypeHash ms_standardUniformSetters;
373 static StandardUniformsNameToTypeHash initializeStandardUniformSetters();
374
375 UniformValue standardUniformValue(StandardUniform standardUniformType,
376 const Entity *entity) const;
377
378 void setUniformValue(ShaderParameterPack &uniformPack, int nameId, const UniformValue &value) const;
379 void setStandardUniformValue(ShaderParameterPack &uniformPack,
380 int nameId,
381 const Entity *entity) const;
382 void setUniformBlockValue(ShaderParameterPack &uniformPack,
383 const ShaderUniformBlock &block,
384 const UniformValue &value) const;
385 void setShaderStorageValue(ShaderParameterPack &uniformPack,
386 const ShaderStorageBlock &block,
387 const UniformValue &value) const;
388 void setDefaultUniformBlockShaderDataValue(ShaderParameterPack &uniformPack,
389 const GLShader *shader,
390 const ShaderData *shaderData,
391 const QString &structName) const;
392 void applyParameter(const Parameter *param,
393 RenderCommand *command,
394 const GLShader *shader) const noexcept;
395};
396
397} // namespace OpenGL
398} // namespace Render
399} // namespace Qt3DRender
400
401QT_END_NAMESPACE
402
403#endif // QT3DRENDER_RENDER_OPENGL_ENDERVIEW_H
404

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