1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef QT3DRENDER_RENDER_FRAMEPROFILER_P_H |
30 | #define QT3DRENDER_RENDER_FRAMEPROFILER_P_H |
31 | |
32 | // |
33 | // W A R N I N G |
34 | // ------------- |
35 | // |
36 | // This file is not part of the Qt API. It exists for the convenience |
37 | // of other Qt classes. This header file may change from version to |
38 | // version without notice, or even be removed. |
39 | // |
40 | // We mean it. |
41 | // |
42 | |
43 | #include <QOpenGLTimerQuery> |
44 | #include <Qt3DCore/private/qthreadpooler_p.h> |
45 | #include <Qt3DCore/private/qt3dcore_global_p.h> |
46 | #include <memory> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_2) |
51 | #define QT3D_SUPPORTS_GL_MONITOR |
52 | #endif |
53 | |
54 | namespace Qt3DCore { |
55 | class QSystemInformationService; |
56 | } |
57 | |
58 | namespace Qt3DRender { |
59 | |
60 | namespace Render { |
61 | |
62 | namespace Profiling { |
63 | |
64 | enum RecordingType |
65 | { |
66 | DrawArray = 512, |
67 | DrawElement, |
68 | DispatchCompute, |
69 | StateUpdate, |
70 | UniformUpdate, |
71 | ShaderUpdate, |
72 | TextureUpload, |
73 | BufferUpload, |
74 | ShaderUpload, |
75 | ClearBuffer, |
76 | VAOUpdate, |
77 | VAOUpload, |
78 | RenderTargetUpdate |
79 | }; |
80 | |
81 | class FrameTimeRecorder |
82 | { |
83 | public: |
84 | FrameTimeRecorder(Qt3DCore::QSystemInformationService *service) |
85 | : m_service(service) |
86 | { |
87 | } |
88 | |
89 | ~FrameTimeRecorder() |
90 | { |
91 | } |
92 | |
93 | void init(int eventCount) |
94 | { |
95 | #ifdef QT3D_SUPPORTS_GL_MONITOR |
96 | if (m_monitor.isCreated()) { |
97 | m_remainingEvents = m_monitor.sampleCount(); |
98 | reset(); |
99 | } else { |
100 | m_monitor.setSampleCount(eventCount * 2); |
101 | m_monitor.create(); |
102 | m_remainingEvents = eventCount; |
103 | } |
104 | #else |
105 | m_remainingEvents = eventCount; |
106 | #endif |
107 | } |
108 | |
109 | void startRecordEvent() |
110 | { |
111 | #ifdef QT3D_SUPPORTS_GL_MONITOR |
112 | m_monitor.recordSample(); |
113 | #endif |
114 | --m_remainingEvents; |
115 | } |
116 | |
117 | void recordEvent(RecordingType type) |
118 | { |
119 | #ifdef QT3D_SUPPORTS_GL_MONITOR |
120 | m_monitor.recordSample(); |
121 | #endif |
122 | --m_remainingEvents; |
123 | |
124 | GLRecording rec; |
125 | rec.type = type; |
126 | rec.startTime = Qt3DCore::QSystemInformationServicePrivate::get(q: m_service)->m_jobsStatTimer.nsecsElapsed(); |
127 | m_recordings.push_back(t: rec); |
128 | } |
129 | |
130 | void reset() |
131 | { |
132 | #ifdef QT3D_SUPPORTS_GL_MONITOR |
133 | m_monitor.reset(); |
134 | #endif |
135 | m_recordings.clear(); |
136 | } |
137 | |
138 | inline bool canStillRecord() { return m_remainingEvents > 0; } |
139 | |
140 | bool tryWriteResults() |
141 | { |
142 | #ifdef QT3D_SUPPORTS_GL_MONITOR |
143 | if (m_monitor.isResultAvailable()) { |
144 | const QVector<GLuint64> samples = m_monitor.waitForSamples(); |
145 | Q_ASSERT(samples.count() >= 2 * m_recordings.count()); |
146 | |
147 | Qt3DCore::QSystemInformationServicePrivate *dservice = Qt3DCore::QSystemInformationServicePrivate::get(q: m_service); |
148 | |
149 | int j = 0; |
150 | for (int i = 0, m = m_recordings.size(); i < m; ++i) { |
151 | const GLRecording rec = m_recordings.at(i); |
152 | Qt3DCore::QSystemInformationServicePrivate::JobRunStats glRecordingStat; |
153 | |
154 | glRecordingStat.jobId.typeAndInstance[0] = rec.type; |
155 | glRecordingStat.jobId.typeAndInstance[1] = 0; |
156 | glRecordingStat.threadId = FrameTimeRecorder::GLThreadID; |
157 | glRecordingStat.startTime = rec.startTime; |
158 | glRecordingStat.endTime = rec.startTime + (samples.at(i: j + 1) - (samples.at(i: j))); |
159 | |
160 | dservice->addSubmissionLogStatsEntry(stats&: glRecordingStat); |
161 | j += 2; |
162 | } |
163 | return true; |
164 | } |
165 | #endif |
166 | return false; |
167 | } |
168 | |
169 | private: |
170 | struct GLRecording |
171 | { |
172 | RecordingType type; |
173 | qint64 startTime; |
174 | }; |
175 | |
176 | static const int GLThreadID = 0x454; |
177 | |
178 | Qt3DCore::QSystemInformationService *m_service; |
179 | #ifdef QT3D_SUPPORTS_GL_MONITOR |
180 | QOpenGLTimeMonitor m_monitor; |
181 | #endif |
182 | QVector<GLRecording> m_recordings; |
183 | int m_remainingEvents = 0; |
184 | }; |
185 | |
186 | class FrameProfiler |
187 | { |
188 | public: |
189 | FrameProfiler(Qt3DCore::QSystemInformationService *service) |
190 | : m_service(service) |
191 | , m_currentRecorder(nullptr) |
192 | {} |
193 | |
194 | ~FrameProfiler() |
195 | { |
196 | qDeleteAll(c: m_recorders); |
197 | } |
198 | |
199 | void startRecordEvent() |
200 | { |
201 | if (m_currentRecorder == nullptr) { |
202 | if (!m_availableRecorders.empty()) { |
203 | m_currentRecorder = m_availableRecorders.takeFirst(); |
204 | } else { |
205 | m_recorders.push_back(t: new FrameTimeRecorder(m_service)); |
206 | m_currentRecorder = m_recorders.last(); |
207 | } |
208 | // We record events 10 by 10 |
209 | m_currentRecorder->init(eventCount: 10); |
210 | } |
211 | m_currentRecorder->startRecordEvent(); |
212 | } |
213 | |
214 | void recordEvent(RecordingType type) |
215 | { |
216 | m_currentRecorder->recordEvent(type); |
217 | if (!m_currentRecorder->canStillRecord()) { |
218 | m_busyRecorders.push_back(t: m_currentRecorder); |
219 | m_currentRecorder = nullptr; |
220 | } |
221 | } |
222 | |
223 | void writeResults() |
224 | { |
225 | for (int i = m_busyRecorders.size() - 1; i >= 0; --i) { |
226 | FrameTimeRecorder *recorder = m_busyRecorders.at(i); |
227 | if (recorder->tryWriteResults()) { |
228 | m_availableRecorders.push_back(t: m_busyRecorders.takeAt(i)); |
229 | } |
230 | } |
231 | } |
232 | |
233 | private: |
234 | Qt3DCore::QSystemInformationService *m_service; |
235 | QVector<FrameTimeRecorder *> m_recorders; |
236 | QVector<FrameTimeRecorder *> m_availableRecorders; |
237 | QVector<FrameTimeRecorder *> m_busyRecorders; |
238 | FrameTimeRecorder *m_currentRecorder; |
239 | }; |
240 | |
241 | |
242 | class GLTimeRecorder |
243 | { |
244 | public: |
245 | explicit GLTimeRecorder(RecordingType type, FrameProfiler *profiler) |
246 | : m_type(type) |
247 | , m_frameProfiler(profiler) |
248 | { |
249 | if (m_frameProfiler) |
250 | m_frameProfiler->startRecordEvent(); |
251 | } |
252 | |
253 | ~GLTimeRecorder() |
254 | { |
255 | if (m_frameProfiler) |
256 | m_frameProfiler->recordEvent(type: m_type); |
257 | } |
258 | |
259 | private: |
260 | RecordingType m_type; |
261 | FrameProfiler *m_frameProfiler; |
262 | }; |
263 | |
264 | } // Profiling |
265 | |
266 | } // Render |
267 | |
268 | } // Qt3DRender |
269 | |
270 | QT_END_NAMESPACE |
271 | |
272 | #endif // QT3DRENDER_RENDER_FRAMEPROFILER_P_H |
273 | |