1// Copyright (C) 2008-2012 NVIDIA Corporation.
2// Copyright (C) 2019 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#ifndef QSSG_RENDER_CONTEXT_CORE_H
6#define QSSG_RENDER_CONTEXT_CORE_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQuick3DRuntimeRender/private/qssgrendershaderlibrarymanager_p.h>
20#include <QtQuick3DRuntimeRender/private/qssgrhicustommaterialsystem_p.h>
21#include <QtQuick3DRuntimeRender/private/qtquick3druntimerenderglobal_p.h>
22#include <QtQuick3DRuntimeRender/private/qssgperframeallocator_p.h>
23#include <QtQuick3DRuntimeRender/private/qssgrendershadercache_p.h>
24#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterialshadergenerator_p.h>
25#include <QtQuick3DRuntimeRender/private/qssgrenderbuffermanager_p.h>
26#include <QtQuick3DRuntimeRender/private/qssgrenderer_p.h>
27#include <QtQuick3DRuntimeRender/private/qssgdebugdrawsystem_p.h>
28
29#include <QtCore/QPair>
30#include <QtCore/QSize>
31
32QT_BEGIN_NAMESPACE
33
34class QSSGCustomMaterialSystem;
35class QSSGRendererInterface;
36class QQuickWindow;
37class QSSGDebugDrawSystem;
38
39class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRenderContextInterface
40{
41 Q_DISABLE_COPY(QSSGRenderContextInterface)
42public:
43 // The commonly used version (from QQuick3DSceneRenderer). There is one
44 // rendercontext per QQuickWindow (and so scenegraph render thread).
45 explicit QSSGRenderContextInterface(QRhi *rhi);
46
47 // This overload must only be used in special cases, e.g. by the genshaders tool.
48 QSSGRenderContextInterface(std::unique_ptr<QSSGBufferManager> &&bufferManager,
49 std::unique_ptr<QSSGRenderer> renderer,
50 std::shared_ptr<QSSGShaderLibraryManager> shaderLibraryManager,
51 std::unique_ptr<QSSGShaderCache> shaderCache,
52 std::unique_ptr<QSSGCustomMaterialSystem> customMaterialSystem,
53 std::unique_ptr<QSSGProgramGenerator> shaderProgramGenerator,
54 std::unique_ptr<QSSGRhiContext> ctx,
55 std::unique_ptr<QSSGDebugDrawSystem> debugDrawSystem = nullptr);
56
57 ~QSSGRenderContextInterface();
58
59 const std::unique_ptr<QSSGRenderer> &renderer() const;
60 const std::unique_ptr<QSSGBufferManager> &bufferManager() const;
61 const std::unique_ptr<QSSGRhiContext> &rhiContext() const;
62 const std::unique_ptr<QSSGShaderCache> &shaderCache() const;
63 const std::shared_ptr<QSSGShaderLibraryManager> &shaderLibraryManager() const;
64 const std::unique_ptr<QSSGCustomMaterialSystem> &customMaterialSystem() const;
65 const std::unique_ptr<QSSGProgramGenerator> &shaderProgramGenerator() const;
66 const std::unique_ptr<QSSGDebugDrawSystem> &debugDrawSystem() const;
67 QRhi *rhi() const;
68
69 // The memory used for the per frame allocator is released as the first step in BeginFrame.
70 // This is useful for short lived objects and datastructures.
71 QSSGPerFrameAllocator &perFrameAllocator() { return m_perFrameAllocator; }
72
73 // Get the number of times EndFrame has been called
74 quint32 frameCount() { return m_frameCount; }
75
76 void setSceneColor(const QColor &inSceneColor) { m_sceneColor = inSceneColor; }
77
78 void setViewport(QRect inViewport) { m_viewport = inViewport; }
79 QRect viewport() const { return m_viewport; }
80
81 void setDpr(float dpr) { m_dpr = dpr; }
82 float dpr() const { return m_dpr; }
83
84 void setScissorRect(QRect inScissorRect) { m_scissorRect = inScissorRect; }
85 QRect scissorRect() const { return m_scissorRect; }
86
87 void cleanupResources(QList<QSSGRenderGraphObject*> &resources);
88 void cleanupResources(QSet<QSSGRenderGraphObject *> &resources);
89 void cleanupUnreferencedBuffers(QSSGRenderLayer *inLayer);
90 void resetResourceCounters(QSSGRenderLayer *inLayer);
91
92 // Steps needed to render:
93 // 1. beginFrame - Reset the per-frame allocator
94 // 2. Make sure state is set (viewport, windowDimensions, dpr, sceneColor, scissorRect)
95 // 3. prepareLayerForRenderer - process the layer scene (prepare the scene for rendering)
96 // 4. rhiPrepare - start rendering necessary sub-scenes and prepare resources
97 // 5. rhiRender - render the layer scene
98 // 6. endFrame
99
100 // Clients need to call this every frame in order for various subsystems to release
101 // temporary per-frame allocated objects.
102 void beginFrame(QSSGRenderLayer *layer, bool allowRecursion = true);
103
104 bool prepareLayerForRender(QSSGRenderLayer &inLayer);
105
106 void rhiPrepare(QSSGRenderLayer &inLayer);
107 void rhiRender(QSSGRenderLayer &inLayer);
108
109 // When allowRecursion is true, the cleanup is only done when all
110 // beginFrames got their corresponding endFrame. This is indicated by the
111 // return value (false if nothing's been done due to pending "frames")
112 bool endFrame(QSSGRenderLayer *layer, bool allowRecursion = true);
113
114private:
115 friend class QQuick3DWindowAttachment;
116
117 void init();
118 void releaseCachedResources();
119
120 std::unique_ptr<QSSGRhiContext> m_rhiContext;
121 std::unique_ptr<QSSGShaderCache> m_shaderCache;
122 std::unique_ptr<QSSGBufferManager> m_bufferManager;
123 std::unique_ptr<QSSGRenderer> m_renderer;
124 std::shared_ptr<QSSGShaderLibraryManager> m_shaderLibraryManager;
125 std::unique_ptr<QSSGCustomMaterialSystem> m_customMaterialSystem;
126 std::unique_ptr<QSSGProgramGenerator> m_shaderProgramGenerator;
127 std::unique_ptr<QSSGDebugDrawSystem> m_debugDrawSystem;
128
129 QSSGPerFrameAllocator m_perFrameAllocator;
130 quint32 m_activeFrameRef = 0;
131 quint32 m_frameCount = 0;
132
133 // Viewport that this render context should use
134 QRect m_viewport;
135 float m_dpr = 1.0;
136 QRect m_scissorRect;
137 QColor m_sceneColor;
138};
139QT_END_NAMESPACE
140
141#endif
142

source code of qtquick3d/src/runtimerender/qssgrendercontextcore_p.h