1 | // Copyright (C) 2017 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 QT3DRENDER_RENDER_RENDERERCACHE_P_H |
5 | #define QT3DRENDER_RENDER_RENDERERCACHE_P_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 <Qt3DCore/private/vector_helper_p.h> |
19 | #include <Qt3DRender/QFrameGraphNode> |
20 | #include <Qt3DRender/private/entity_p.h> |
21 | #include <Qt3DRender/private/renderviewjobutils_p.h> |
22 | #include <Qt3DRender/private/lightsource_p.h> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | namespace Qt3DRender { |
27 | |
28 | namespace Render { |
29 | |
30 | template<class RenderCommand> |
31 | struct EntityRenderCommandData |
32 | { |
33 | std::vector<const Entity *> entities; |
34 | std::vector<RenderCommand> commands; |
35 | std::vector<RenderPassParameterData> passesData; |
36 | |
37 | void reserve(size_t size) |
38 | { |
39 | entities.reserve(n: size); |
40 | commands.reserve(size); |
41 | passesData.reserve(n: size); |
42 | } |
43 | |
44 | inline size_t size() const { return entities.size(); } |
45 | |
46 | inline void push_back(const Entity *e, const RenderCommand &c, const RenderPassParameterData &p) |
47 | { |
48 | entities.push_back(x: e); |
49 | commands.push_back(c); |
50 | passesData.push_back(x: p); |
51 | } |
52 | |
53 | inline void push_back(const Entity *e, RenderCommand &&c, RenderPassParameterData &&p) |
54 | { |
55 | entities.push_back(x: e); |
56 | commands.push_back(std::move(c)); |
57 | passesData.push_back(x: std::move(p)); |
58 | } |
59 | |
60 | EntityRenderCommandData &operator+=(EntityRenderCommandData &&t) |
61 | { |
62 | Qt3DCore::moveAtEnd(entities, std::move(t.entities)); |
63 | Qt3DCore::moveAtEnd(commands, std::move(t.commands)); |
64 | Qt3DCore::moveAtEnd(passesData, std::move(t.passesData)); |
65 | return *this; |
66 | } |
67 | |
68 | }; |
69 | |
70 | template<class RenderCommand> |
71 | struct EntityRenderCommandDataView |
72 | { |
73 | EntityRenderCommandData<RenderCommand> data; |
74 | std::vector<size_t> indices; |
75 | |
76 | size_t size() const noexcept { return indices.size(); } |
77 | |
78 | template<typename F> |
79 | void forEachCommand(F func) |
80 | { |
81 | for (size_t idx : indices) |
82 | func(data.commands[idx]); |
83 | } |
84 | }; |
85 | |
86 | template<class RenderCommand> |
87 | using EntityRenderCommandDataViewPtr = QSharedPointer<EntityRenderCommandDataView<RenderCommand>>; |
88 | |
89 | template<class RenderCommand> |
90 | struct EntityRenderCommandDataSubView |
91 | { |
92 | EntityRenderCommandDataViewPtr<RenderCommand> view; |
93 | size_t offset = 0; |
94 | size_t count = 0; |
95 | |
96 | template<typename F> |
97 | void forEach(F func) |
98 | { |
99 | for (size_t i = 0, m = size_t(count); i < m; ++i) { |
100 | const size_t idx = view->indices[offset + i]; |
101 | func(view->data.entities[idx], |
102 | view->data.passesData[idx], |
103 | view->data.commands[idx]); |
104 | } |
105 | } |
106 | |
107 | template<typename F> |
108 | void forEach(F func) const |
109 | { |
110 | for (size_t i = 0, m = size_t(count); i < m; ++i) { |
111 | const size_t idx = view->indices[offset + i]; |
112 | func(view->data.entities[idx], |
113 | view->data.passesData[idx], |
114 | view->data.commands[idx]); |
115 | } |
116 | } |
117 | }; |
118 | |
119 | template<class RenderCommand> |
120 | struct RendererCache |
121 | { |
122 | struct LeafNodeData |
123 | { |
124 | Matrix4x4 viewProjectionMatrix; |
125 | // Set by the FilterLayerJob |
126 | // Contains all Entities that satisfy the layer filtering for the RV |
127 | std::vector<Entity *> filterEntitiesByLayer; |
128 | |
129 | // Set by the MaterialParameterGatherJob |
130 | MaterialParameterGathererData materialParameterGatherer; |
131 | |
132 | // Set by the SyncRenderViewPreCommandUpdateJob |
133 | // Contains caches of different filtering stages that can |
134 | // be cached across frame |
135 | std::vector<Entity *> layeredFilteredRenderables; // Changes rarely |
136 | std::vector<Entity *> filteredAndCulledRenderables; // Changes if camera is modified |
137 | std::vector<LightSource> layeredFilteredLightSources; |
138 | |
139 | // Cache of RenderCommands |
140 | EntityRenderCommandDataViewPtr<RenderCommand> filteredRenderCommandDataViews; |
141 | }; |
142 | |
143 | // Variabled below are shared amongst all RV |
144 | |
145 | // Set by CachingRenderableEntityFilterJob |
146 | std::vector<Entity *> renderableEntities; |
147 | |
148 | // Set by CachingComputableEntityFilterJob |
149 | std::vector<Entity *> computeEntities; |
150 | |
151 | // Set by CachingLightGathererJob |
152 | std::vector<LightSource> gatheredLights; |
153 | |
154 | EnvironmentLight* environmentLight; |
155 | |
156 | // Per RV cache |
157 | // Leaves inserted by SyncRenderViewPostInitialization |
158 | QHash<FrameGraphNode *, LeafNodeData> leafNodeCache; |
159 | |
160 | QMutex *mutex() { return &m_mutex; } |
161 | |
162 | private: |
163 | QMutex m_mutex; |
164 | }; |
165 | |
166 | } // namespace Render |
167 | |
168 | } // namespace Qt3DRender |
169 | |
170 | QT_END_NAMESPACE |
171 | |
172 | #endif // QT3DRENDER_RENDER_RENDERERCACHE_P_H |
173 | |