1// Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4/*
5 * Based on https://github.com/seanchas116/qtimgui/
6 *
7 * MIT License https://github.com/seanchas116/qtimgui/blob/master/LICENSE
8 *
9 */
10
11#include "imguirenderer_p.h"
12#include <renderview_p.h>
13#include <rendercommand_p.h>
14#include <renderer_p.h>
15#include <submissioncontext_p.h>
16#include <Qt3DRender/private/geometryrenderermanager_p.h>
17
18#include <QDateTime>
19#include <QGuiApplication>
20#include <QMouseEvent>
21#include <QClipboard>
22#include <QCursor>
23#include <QOpenGLExtraFunctions>
24
25#include "imgui.h"
26
27#ifndef GL_VERTEX_ARRAY_BINDING
28// just for building on some platforms, won't run anyway as this requires GL/ES > 2
29#define GL_VERTEX_ARRAY_BINDING 0x85B5
30#endif
31
32QT_BEGIN_NAMESPACE
33
34namespace Qt3DRR = Qt3DRender::Render;
35namespace Qt3DRRD = Qt3DRR::Debug;
36
37namespace {
38
39 const QHash<int, ImGuiKey> keyMap = {
40 { Qt::Key_Tab, ImGuiKey_Tab },
41 { Qt::Key_Left, ImGuiKey_LeftArrow },
42 { Qt::Key_Right, ImGuiKey_RightArrow },
43 { Qt::Key_Up, ImGuiKey_UpArrow },
44 { Qt::Key_Down, ImGuiKey_DownArrow },
45 { Qt::Key_PageUp, ImGuiKey_PageUp },
46 { Qt::Key_PageDown, ImGuiKey_PageDown },
47 { Qt::Key_Home, ImGuiKey_Home },
48 { Qt::Key_End, ImGuiKey_End },
49 { Qt::Key_Delete, ImGuiKey_Delete },
50 { Qt::Key_Backspace, ImGuiKey_Backspace },
51 { Qt::Key_Enter, ImGuiKey_Enter },
52 { Qt::Key_Escape, ImGuiKey_Escape },
53 { Qt::Key_A, ImGuiKey_A },
54 { Qt::Key_C, ImGuiKey_C },
55 { Qt::Key_V, ImGuiKey_V },
56 { Qt::Key_X, ImGuiKey_X },
57 { Qt::Key_Y, ImGuiKey_Y },
58 { Qt::Key_Z, ImGuiKey_Z },
59 };
60
61 QByteArray g_currentClipboardText;
62
63 // doesn't handle primitive restart when using indexes
64 int vertexToPrimitiveCount(Qt3DRender::QGeometryRenderer::PrimitiveType primitiveType, int numVertices) {
65 int nPrimitives = 0;
66 switch (primitiveType) {
67 case Qt3DRender::QGeometryRenderer::Points:
68 case Qt3DRender::QGeometryRenderer::LineLoop:
69 nPrimitives += numVertices;
70 break;
71 case Qt3DRender::QGeometryRenderer::Triangles:
72 nPrimitives += numVertices / 3;
73 break;
74 case Qt3DRender::QGeometryRenderer::Lines:
75 nPrimitives += numVertices / 2;
76 break;
77 case Qt3DRender::QGeometryRenderer::TriangleFan:
78 case Qt3DRender::QGeometryRenderer::TriangleStrip:
79 case Qt3DRender::QGeometryRenderer::LineStrip:
80 nPrimitives += numVertices - 1;
81 break;
82 case Qt3DRender::QGeometryRenderer::TrianglesAdjacency:
83 nPrimitives += numVertices / 6;
84 break;
85 case Qt3DRender::QGeometryRenderer::TriangleStripAdjacency:
86 case Qt3DRender::QGeometryRenderer::LineStripAdjacency:
87 nPrimitives += numVertices / 2 - 1;
88 break;
89 case Qt3DRender::QGeometryRenderer::LinesAdjacency:
90 nPrimitives += numVertices / 4;
91 break;
92 case Qt3DRender::QGeometryRenderer::Patches:
93 nPrimitives += 1;
94 }
95 return nPrimitives;
96 }
97
98 const char *primitiveTypeName(Qt3DRender::QGeometryRenderer::PrimitiveType primitiveType) {
99 switch (primitiveType) {
100 case Qt3DRender::QGeometryRenderer::Points:
101 return "Points";
102 case Qt3DRender::QGeometryRenderer::LineLoop:
103 return "LineLoop";
104 case Qt3DRender::QGeometryRenderer::Triangles:
105 return "Triangles";
106 case Qt3DRender::QGeometryRenderer::TrianglesAdjacency:
107 return "TriangleAdjacency";
108 case Qt3DRender::QGeometryRenderer::TriangleFan:
109 return "TriangleFan";
110 case Qt3DRender::QGeometryRenderer::TriangleStrip:
111 return "TriangleStrip";
112 case Qt3DRender::QGeometryRenderer::TriangleStripAdjacency:
113 return "TriangleStringAdjacency";
114 case Qt3DRender::QGeometryRenderer::LineStrip:
115 return "LineStrip";
116 case Qt3DRender::QGeometryRenderer::LineStripAdjacency:
117 return "LineStripAdjacency";
118 case Qt3DRender::QGeometryRenderer::Lines:
119 return "Lines";
120 case Qt3DRender::QGeometryRenderer::LinesAdjacency:
121 return "LinesAdjacency";
122 case Qt3DRender::QGeometryRenderer::Patches:
123 return "Patches";
124 }
125 return "";
126 }
127}
128
129Qt3DRRD::ImGuiRenderer::ImGuiRenderer(Qt3DRR::OpenGL::Renderer *renderer)
130 : m_renderer(renderer)
131{
132 ImGui::CreateContext();
133
134 ImGuiIO &io = ImGui::GetIO();
135 for (ImGuiKey key : keyMap.values())
136 io.KeyMap[key] = key;
137
138#ifndef QT_NO_CLIPBOARD
139 io.SetClipboardTextFn = [](void *user_data, const char *text) {
140 Q_UNUSED(user_data);
141 QGuiApplication::clipboard()->setText(QString::fromLatin1(ba: text));
142 };
143 io.GetClipboardTextFn = [](void *user_data) {
144 Q_UNUSED(user_data);
145 g_currentClipboardText = QGuiApplication::clipboard()->text().toUtf8();
146 return static_cast<const char *>(g_currentClipboardText.data());
147 };
148#endif
149
150 std::fill(first: std::begin(arr&: m_fpsLog), last: std::end(arr&: m_fpsLog), value: 0.f);
151 std::fill(first: std::begin(arr&: m_jobsLog), last: std::end(arr&: m_jobsLog), value: 0.f);
152 m_fpsRange.first = m_fpsRange.second = 0.f;
153 m_jobsRange.first = m_jobsRange.second = 0.f;
154}
155
156Qt3DRRD::ImGuiRenderer::~ImGuiRenderer() = default;
157
158void Qt3DRRD::ImGuiRenderer::renderDebugOverlay(const std::vector<Qt3DRR::OpenGL::RenderView *> &renderViews, const Qt3DRR::OpenGL::RenderView *renderView, int jobsInLastFrame)
159{
160 if (!newFrame(renderView))
161 return;
162
163 const int renderViewsCount = int(renderViews.size());
164
165 int logIndex = qMin(IMGUI_PERF_LOG_SIZE - 1, b: ImGui::GetFrameCount());
166 if (logIndex == IMGUI_PERF_LOG_SIZE - 1) {
167 std::rotate(first: std::begin(arr&: m_fpsLog), middle: std::begin(arr&: m_fpsLog) + 1, last: std::end(arr&: m_fpsLog));
168 std::rotate(first: std::begin(arr&: m_jobsLog), middle: std::begin(arr&: m_jobsLog) + 1, last: std::end(arr&: m_jobsLog));
169 }
170 m_fpsLog[logIndex] = ImGui::GetIO().Framerate;
171 m_fpsRange.first = m_fpsRange.second = 0.f;
172 for (float v: m_fpsLog) {
173 m_fpsRange.first = qMin(a: m_fpsRange.first, b: qMax(a: v - 5.f, b: 0.f));
174 m_fpsRange.second = qMax(a: m_fpsRange.second, b: v + 2.f);
175 }
176 m_jobsLog[logIndex] = jobsInLastFrame;
177 m_jobsRange.first = m_jobsRange.second = 0.f;
178 for (float v: m_jobsLog) {
179 m_jobsRange.first = qMin(a: m_jobsRange.first, b: qMax(a: v - 5.f, b: 0.f));
180 m_jobsRange.second = qMax(a: m_jobsRange.second, b: v + 2.f);
181 }
182
183 {
184 ImGui::Begin(name: "Qt3D Profiling");
185 char caption[50];
186 snprintf(s: caption, maxlen: sizeof(caption), format: "Avg %.3f ms/frame (%.1f FPS)", static_cast<double>(1000.0f / ImGui::GetIO().Framerate), static_cast<double>(ImGui::GetIO().Framerate));
187 ImGui::PlotLines(label: "FPS", values: m_fpsLog, values_count: logIndex + 1, values_offset: 0, overlay_text: caption, scale_min: m_fpsRange.first, scale_max: m_fpsRange.second, graph_size: ImVec2(0, 80));
188 ImGui::PlotHistogram(label: "Jobs", values: m_jobsLog, values_count: logIndex + 1, values_offset: 0, overlay_text: nullptr, scale_min: m_jobsRange.first, scale_max: m_jobsRange.second, graph_size: ImVec2(0, 80));
189
190 int nCommands = 0;
191 int nVertices = 0;
192 int nPrimitives = 0;
193 QSet<HGeometryRenderer> inUseGeometries;
194 QSet<Qt3DCore::QNodeId> inUseTextures;
195 for (int j=0; j<renderViewsCount; j++) {
196 Qt3DRR::OpenGL::RenderView *rv = renderViews.at(n: j);
197 nCommands += rv->commandCount();
198 rv->forEachCommand(func: [&](const Qt3DRR::OpenGL::RenderCommand &command) {
199 if (command.m_type != Qt3DRR::OpenGL::RenderCommand::Draw)
200 return;
201 nVertices += command.m_primitiveCount;
202 nPrimitives += vertexToPrimitiveCount(primitiveType: command.m_primitiveType, numVertices: command.m_primitiveCount);
203 inUseGeometries.insert(value: command.m_geometryRenderer);
204 const auto &textures = command.m_parameterPack.textures();
205 for (const auto &ns: textures)
206 inUseTextures.insert(value: ns.nodeId);
207 });
208 }
209
210 auto columnNumber = [](int i) {
211 if (i == 0)
212 ImGui::Text(fmt: "--");
213 else
214 ImGui::Text(fmt: " %d", i);
215 ImGui::NextColumn();
216 };
217 auto column2Numbers = [](int i, int of) {
218 if (of == 0)
219 ImGui::Text(fmt: "--");
220 else
221 ImGui::Text(fmt: " %d of %d", i, of);
222 ImGui::NextColumn();
223 };
224
225 ImGui::Columns(count: 5);
226 ImGui::Separator();
227 for (auto s: {"Jobs", "RV", "Cmds", "Verts", "Prims"}) {
228 ImGui::Text(fmt: "#%s", s);
229 ImGui::NextColumn();
230 }
231 for (auto s: {jobsInLastFrame, renderViewsCount, nCommands, nVertices, nPrimitives})
232 columnNumber(s);
233
234 ImGui::Columns(count: 3);
235 ImGui::Separator();
236 for (auto s: {"Entities", "Geometries", "Textures"}) {
237 ImGui::Text(fmt: "#%s", s);
238 ImGui::NextColumn();
239 }
240 columnNumber(m_renderer->nodeManagers()->renderNodesManager()->count());
241 column2Numbers(inUseGeometries.size(), m_renderer->nodeManagers()->geometryRendererManager()->count());
242 column2Numbers(inUseTextures.size(), m_renderer->nodeManagers()->textureManager()->count());
243
244 ImGui::Columns(count: 1);
245 ImGui::Separator();
246
247 bool pj = m_renderer->services()->systemInformation()->isTraceEnabled();
248 ImGui::AlignTextToFramePadding();
249 ImGui::Text(fmt: "Profiling: ");
250 ImGui::SameLine();
251 if (ImGui::Checkbox(label: "Jobs", v: &pj))
252 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "setTraceEnabled", c: Qt::QueuedConnection, Q_ARG(bool, pj));
253 ImGui::SameLine();
254 if (ImGui::Button(label: "Reveal"))
255 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "revealLogFolder", c: Qt::QueuedConnection);
256
257 ImGui::AlignTextToFramePadding();
258 ImGui::Text(fmt: "Show:");
259 ImGui::SameLine();
260 if (ImGui::Button(label: "GL Info"))
261 m_showGLInfoWindow = !m_showGLInfoWindow;
262 ImGui::SameLine();
263 if (ImGui::Button(label: "Render Views"))
264 m_showRenderDetailsWindow = !m_showRenderDetailsWindow;
265
266 ImGui::AlignTextToFramePadding();
267 ImGui::Text(fmt: "Dump:");
268 ImGui::SameLine();
269 if (ImGui::Button(label: "SceneGraph##1"))
270 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand",
271 c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render scenegraph")));
272 ImGui::SameLine();
273 if (ImGui::Button(label: "FrameGraph##1"))
274 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand",
275 c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render framegraph")));
276 ImGui::SameLine();
277 if (ImGui::Button(label: "Render Views##1"))
278 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand",
279 c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render framepaths")));
280
281 ImGui::AlignTextToFramePadding();
282 ImGui::Text(fmt: " ");
283 ImGui::SameLine();
284 if (ImGui::Button(label: "Filter State##1"))
285 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand",
286 c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render filterstates")));
287 ImGui::SameLine();
288 if (ImGui::Button(label: "JobsGraph##1"))
289 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand",
290 c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("dump jobs")));
291
292 ImGui::End();
293
294 if (m_showGLInfoWindow)
295 showGLInfo();
296 if (m_showRenderDetailsWindow)
297 showRenderDetails(renderViews);
298 }
299
300 ImGui::Render();
301 renderDrawList(draw_data: ImGui::GetDrawData());
302}
303
304void Qt3DRRD::ImGuiRenderer::setCapabilities(const QString &capabilities)
305{
306 m_capabilities = capabilities.toLatin1();
307}
308
309void Qt3DRRD::ImGuiRenderer::showGLInfo()
310{
311 ImGui::Begin(name: "Open GL Details", p_open: &m_showGLInfoWindow);
312 ImGui::Text(fmt: "%s", m_capabilities.data());
313 ImGui::End();
314}
315
316void Qt3DRRD::ImGuiRenderer::showRenderDetails(const std::vector<Qt3DRR::OpenGL::RenderView *> &renderViews)
317{
318 ImGui::Begin(name: "Render Views", p_open: &m_showRenderDetailsWindow);
319
320 int i = 1;
321 for (const Qt3DRR::OpenGL::RenderView *view : renderViews) {
322 QString label(QLatin1String("View ") + QString::number(i++));
323 if (ImGui::TreeNode(label: label.toLatin1().data())) {
324 ImGui::Text(fmt: "Viewport: (%.1f, %.1f, %.1f, %.1f)", view->viewport().x(), view->viewport().y(),
325 view->viewport().width(), view->viewport().height());
326 ImGui::Text(fmt: "Surface Size: (%d, %d)", view->surfaceSize().width(), view->surfaceSize().height());
327 ImGui::Text(fmt: "Pixel Ratio: %.1f", view->devicePixelRatio());
328 ImGui::Text(fmt: "No Draw: %s", view->noDraw() ? "TRUE" : "FALSE");
329 ImGui::Text(fmt: "Frustum Culling: %s", view->frustumCulling() ? "TRUE" : "FALSE");
330 ImGui::Text(fmt: "Compute: %s", view->isCompute() ? "TRUE" : "FALSE");
331 ImGui::Text(fmt: "Clear Depth Value: %f", static_cast<double>(view->clearDepthValue()));
332 ImGui::Text(fmt: "Clear Stencil Value: %d", view->clearStencilValue());
333 int j = 1;
334
335 view->forEachCommand(func: [&](const Qt3DRR::OpenGL::RenderCommand &command) {
336 GeometryRenderer *rGeometryRenderer = m_renderer->nodeManagers()->data<GeometryRenderer, GeometryRendererManager>(handle: command.m_geometryRenderer);
337 QString label = QString(QLatin1String("Command %1 {%2}")).arg(args: QString::number(j++), args: QString::number(rGeometryRenderer->peerId().id()));
338 if (ImGui::TreeNode(label: label.toLatin1().data())) {
339 ImGui::Text(fmt: "Primitive Type: %s %s", primitiveTypeName(primitiveType: command.m_primitiveType),
340 command.m_drawIndexed ? "(indexed)" : "");
341 ImGui::Text(fmt: "# Vertices: %d", command.m_primitiveCount);
342 ImGui::Text(fmt: "# Primitives: %d", vertexToPrimitiveCount(primitiveType: command.m_primitiveType, numVertices: command.m_primitiveCount));
343 ImGui::Text(fmt: "# Instances: %d", command.m_instanceCount);
344 ImGui::TreePop();
345 }
346 });
347 ImGui::TreePop();
348 ImGui::Separator();
349 }
350 }
351
352 if (ImGui::Button(label: "Dump"))
353 QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand",
354 c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render rendercommands")));
355 ImGui::End();
356}
357
358void Qt3DRRD::ImGuiRenderer::renderDrawList(ImDrawData *draw_data)
359{
360 // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
361 ImGuiIO& io = ImGui::GetIO();
362 int fb_width = int(io.DisplaySize.x * io.DisplayFramebufferScale.x);
363 int fb_height = int(io.DisplaySize.y * io.DisplayFramebufferScale.y);
364 if (fb_width == 0 || fb_height == 0)
365 return;
366 draw_data->ScaleClipRects(sc: io.DisplayFramebufferScale);
367
368 // Backup GL state
369 GLint last_active_texture; m_funcs->glGetIntegerv(GL_ACTIVE_TEXTURE, params: &last_active_texture);
370 m_funcs->glActiveTexture(GL_TEXTURE0);
371 GLint last_program; m_funcs->glGetIntegerv(GL_CURRENT_PROGRAM, params: &last_program);
372 GLint last_texture; m_funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, params: &last_texture);
373 GLint last_array_buffer; m_funcs->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, params: &last_array_buffer);
374 GLint last_element_array_buffer; m_funcs->glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, params: &last_element_array_buffer);
375 GLint last_vertex_array; m_funcs->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, params: &last_vertex_array);
376 GLint last_blend_src_rgb; m_funcs->glGetIntegerv(GL_BLEND_SRC_RGB, params: &last_blend_src_rgb);
377 GLint last_blend_dst_rgb; m_funcs->glGetIntegerv(GL_BLEND_DST_RGB, params: &last_blend_dst_rgb);
378 GLint last_blend_src_alpha; m_funcs->glGetIntegerv(GL_BLEND_SRC_ALPHA, params: &last_blend_src_alpha);
379 GLint last_blend_dst_alpha; m_funcs->glGetIntegerv(GL_BLEND_DST_ALPHA, params: &last_blend_dst_alpha);
380 GLint last_blend_equation_rgb; m_funcs->glGetIntegerv(GL_BLEND_EQUATION_RGB, params: &last_blend_equation_rgb);
381 GLint last_blend_equation_alpha; m_funcs->glGetIntegerv(GL_BLEND_EQUATION_ALPHA, params: &last_blend_equation_alpha);
382 GLint last_viewport[4]; m_funcs->glGetIntegerv(GL_VIEWPORT, params: last_viewport);
383 GLint last_scissor_box[4]; m_funcs->glGetIntegerv(GL_SCISSOR_BOX, params: last_scissor_box);
384 GLboolean last_enable_blend = m_funcs->glIsEnabled(GL_BLEND);
385 GLboolean last_enable_cull_face = m_funcs->glIsEnabled(GL_CULL_FACE);
386 GLboolean last_enable_depth_test = m_funcs->glIsEnabled(GL_DEPTH_TEST);
387 GLboolean last_enable_scissor_test = m_funcs->glIsEnabled(GL_SCISSOR_TEST);
388
389 // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
390 m_funcs->glEnable(GL_BLEND);
391 m_funcs->glBlendEquation(GL_FUNC_ADD);
392 m_funcs->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
393 m_funcs->glDisable(GL_CULL_FACE);
394 m_funcs->glDisable(GL_DEPTH_TEST);
395 m_funcs->glEnable(GL_SCISSOR_TEST);
396
397 // Setup viewport, orthographic projection matrix
398 m_funcs->glViewport(x: 0, y: 0, width: static_cast<GLsizei>(fb_width), height: static_cast<GLsizei>(fb_height));
399 const float ortho_projection[4][4] = {
400 { 2.0f/io.DisplaySize.x, 0.0f, 0.0f, 0.0f },
401 { 0.0f, 2.0f/-io.DisplaySize.y, 0.0f, 0.0f },
402 { 0.0f, 0.0f, -1.0f, 0.0f },
403 {-1.0f, 1.0f, 0.0f, 1.0f },
404 };
405 m_funcs->glUseProgram(program: m_shaderHandle);
406 m_funcs->glUniform1i(location: m_attribLocationTex, x: 0);
407 m_funcs->glUniformMatrix4fv(location: m_attribLocationProjMtx, count: 1, GL_FALSE, value: &ortho_projection[0][0]);
408 m_funcs->glBindVertexArray(array: m_vaoHandle);
409
410 for (int n = 0; n < draw_data->CmdListsCount; n++) {
411 const ImDrawList* cmd_list = draw_data->CmdLists[n];
412 const ImDrawIdx* idx_buffer_offset = nullptr;
413
414 m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: m_vboHandle);
415 m_funcs->glBufferData(GL_ARRAY_BUFFER, size: static_cast<GLsizeiptr>(cmd_list->VtxBuffer.Size) * sizeof(ImDrawVert), data: static_cast<const GLvoid*>(cmd_list->VtxBuffer.Data), GL_STREAM_DRAW);
416
417 m_funcs->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer: m_elementsHandle);
418 m_funcs->glBufferData(GL_ELEMENT_ARRAY_BUFFER, size: static_cast<GLsizeiptr>(cmd_list->IdxBuffer.Size) * sizeof(ImDrawIdx), data: static_cast<const GLvoid*>(cmd_list->IdxBuffer.Data), GL_STREAM_DRAW);
419
420 for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) {
421 const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
422 if (pcmd->UserCallback) {
423 pcmd->UserCallback(cmd_list, pcmd);
424 } else {
425 m_funcs->glBindTexture(GL_TEXTURE_2D, texture: (GLuint)(intptr_t)pcmd->TextureId);
426 m_funcs->glScissor(x: static_cast<int>(pcmd->ClipRect.x), y: static_cast<int>(fb_height - pcmd->ClipRect.w),
427 width: static_cast<int>(pcmd->ClipRect.z - pcmd->ClipRect.x), height: static_cast<int>(pcmd->ClipRect.w - pcmd->ClipRect.y));
428 m_funcs->glDrawElements(GL_TRIANGLES, count: static_cast<GLsizei>(pcmd->ElemCount), type: sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, indices: idx_buffer_offset);
429 }
430 idx_buffer_offset += pcmd->ElemCount;
431 }
432 }
433
434 // Restore modified GL state
435 m_funcs->glUseProgram(program: last_program);
436 m_funcs->glBindTexture(GL_TEXTURE_2D, texture: last_texture);
437 m_funcs->glActiveTexture(texture: last_active_texture);
438 m_funcs->glBindVertexArray(array: last_vertex_array);
439 m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: last_array_buffer);
440 m_funcs->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer: last_element_array_buffer);
441 m_funcs->glBlendEquationSeparate(modeRGB: last_blend_equation_rgb, modeAlpha: last_blend_equation_alpha);
442 m_funcs->glBlendFuncSeparate(srcRGB: last_blend_src_rgb, dstRGB: last_blend_dst_rgb, srcAlpha: last_blend_src_alpha, dstAlpha: last_blend_dst_alpha);
443 if (last_enable_blend)
444 m_funcs->glEnable(GL_BLEND); else m_funcs->glDisable(GL_BLEND);
445 if (last_enable_cull_face)
446 m_funcs->glEnable(GL_CULL_FACE); else m_funcs->glDisable(GL_CULL_FACE);
447 if (last_enable_depth_test)
448 m_funcs->glEnable(GL_DEPTH_TEST); else m_funcs->glDisable(GL_DEPTH_TEST);
449 if (last_enable_scissor_test)
450 m_funcs->glEnable(GL_SCISSOR_TEST); else m_funcs->glDisable(GL_SCISSOR_TEST);
451 m_funcs->glViewport(x: last_viewport[0], y: last_viewport[1], width: static_cast<GLsizei>(last_viewport[2]), height: static_cast<GLsizei>(last_viewport[3]));
452 m_funcs->glScissor(x: last_scissor_box[0], y: last_scissor_box[1], width: static_cast<GLsizei>(last_scissor_box[2]), height: static_cast<GLsizei>(last_scissor_box[3]));
453}
454
455bool Qt3DRRD::ImGuiRenderer::createFontsTexture()
456{
457 // Build texture atlas
458 ImGuiIO& io = ImGui::GetIO();
459 unsigned char* pixels;
460 int width, height;
461 io.Fonts->GetTexDataAsRGBA32(out_pixels: &pixels, out_width: &width, out_height: &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
462
463 // Upload texture to graphics system
464 GLint last_texture;
465 m_funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, params: &last_texture);
466 m_funcs->glGenTextures(n: 1, textures: &m_fontTexture);
467 m_funcs->glBindTexture(GL_TEXTURE_2D, texture: m_fontTexture);
468 m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
469 m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
470 m_funcs->glTexImage2D(GL_TEXTURE_2D, level: 0, GL_RGBA, width, height, border: 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
471
472 // Store our identifier
473 io.Fonts->TexID = (void *)(intptr_t)m_fontTexture;
474
475 // Restore state
476 m_funcs->glBindTexture(GL_TEXTURE_2D, texture: last_texture);
477
478 return true;
479}
480
481bool Qt3DRRD::ImGuiRenderer::createDeviceObjects()
482{
483 auto *glContext = m_renderer->submissionContext()->openGLContext();
484 if (glContext->format().majorVersion() < 3) {
485 qWarning() << "Qt3D Profiling overlay requires GL or GL ES >= 3";
486 return false;
487 }
488
489 // Backup GL state
490 GLint last_texture, last_array_buffer, last_vertex_array;
491 m_funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, params: &last_texture);
492 m_funcs->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, params: &last_array_buffer);
493 m_funcs->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, params: &last_vertex_array);
494
495 const GLchar *vertex_shader =
496 "#version 330\n"
497 "uniform mat4 ProjMtx;\n"
498 "in vec2 Position;\n"
499 "in vec2 UV;\n"
500 "in vec4 Color;\n"
501 "out vec2 Frag_UV;\n"
502 "out vec4 Frag_Color;\n"
503 "void main()\n"
504 "{\n"
505 " Frag_UV = UV;\n"
506 " Frag_Color = Color;\n"
507 " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
508 "}\n";
509
510 const GLchar* fragment_shader =
511 "#version 330\n"
512 "uniform sampler2D Texture;\n"
513 "in vec2 Frag_UV;\n"
514 "in vec4 Frag_Color;\n"
515 "out vec4 Out_Color;\n"
516 "void main()\n"
517 "{\n"
518 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
519 "}\n";
520
521 const GLchar *vertex_shader_es3 =
522 "#version 300 es\n"
523 "uniform mat4 ProjMtx;\n"
524 "in vec2 Position;\n"
525 "in vec2 UV;\n"
526 "in vec4 Color;\n"
527 "out vec2 Frag_UV;\n"
528 "out vec4 Frag_Color;\n"
529 "void main()\n"
530 "{\n"
531 " Frag_UV = UV;\n"
532 " Frag_Color = Color;\n"
533 " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n"
534 "}\n";
535
536 const GLchar* fragment_shader_es3 =
537 "#version 300 es\n"
538 "precision highp float;\n"
539 "uniform sampler2D Texture;\n"
540 "in vec2 Frag_UV;\n"
541 "in vec4 Frag_Color;\n"
542 "out vec4 Out_Color;\n"
543 "void main()\n"
544 "{\n"
545 " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n"
546 "}\n";
547
548 QString logs;
549 m_shader = new QOpenGLShaderProgram(this);
550 if (glContext->isOpenGLES()) {
551 if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Vertex, source: vertex_shader_es3))
552 logs += m_shader->log();
553 if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Fragment, source: fragment_shader_es3))
554 logs += m_shader->log();
555 } else {
556 if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Vertex, source: vertex_shader))
557 logs += m_shader->log();
558 if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Fragment, source: fragment_shader))
559 logs += m_shader->log();
560 }
561 m_shader->link();
562 logs += m_shader->log();
563 if (!logs.isEmpty())
564 qWarning() << logs;
565 m_shaderHandle = m_shader->programId();
566
567 m_attribLocationTex = m_funcs->glGetUniformLocation(program: m_shaderHandle, name: "Texture");
568 m_attribLocationProjMtx = m_funcs->glGetUniformLocation(program: m_shaderHandle, name: "ProjMtx");
569 m_attribLocationPosition = m_funcs->glGetAttribLocation(program: m_shaderHandle, name: "Position");
570 m_attribLocationUV = m_funcs->glGetAttribLocation(program: m_shaderHandle, name: "UV");
571 m_attribLocationColor = m_funcs->glGetAttribLocation(program: m_shaderHandle, name: "Color");
572
573 m_funcs->glGenBuffers(n: 1, buffers: &m_vboHandle);
574 m_funcs->glGenBuffers(n: 1, buffers: &m_elementsHandle);
575
576 m_funcs->glGenVertexArrays(n: 1, arrays: &m_vaoHandle);
577 m_funcs->glBindVertexArray(array: m_vaoHandle);
578 m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: m_vboHandle);
579 m_funcs->glEnableVertexAttribArray(index: m_attribLocationPosition);
580 m_funcs->glEnableVertexAttribArray(index: m_attribLocationUV);
581 m_funcs->glEnableVertexAttribArray(index: m_attribLocationColor);
582
583#define OFFSETOF(TYPE, ELEMENT) (reinterpret_cast<size_t>(&((static_cast<TYPE *>(nullptr))->ELEMENT)))
584 m_funcs->glVertexAttribPointer(indx: m_attribLocationPosition, size: 2, GL_FLOAT, GL_FALSE, stride: sizeof(ImDrawVert), ptr: reinterpret_cast<GLvoid*>(OFFSETOF(ImDrawVert, pos)));
585 m_funcs->glVertexAttribPointer(indx: m_attribLocationUV, size: 2, GL_FLOAT, GL_FALSE, stride: sizeof(ImDrawVert), ptr: reinterpret_cast<GLvoid*>(OFFSETOF(ImDrawVert, uv)));
586 m_funcs->glVertexAttribPointer(indx: m_attribLocationColor, size: 4, GL_UNSIGNED_BYTE, GL_TRUE, stride: sizeof(ImDrawVert), ptr: reinterpret_cast<GLvoid*>(OFFSETOF(ImDrawVert, col)));
587#undef OFFSETOF
588
589 createFontsTexture();
590
591 // Restore modified GL state
592 m_funcs->glBindTexture(GL_TEXTURE_2D, texture: last_texture);
593 m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: last_array_buffer);
594 m_funcs->glBindVertexArray(array: last_vertex_array);
595
596 return true;
597}
598
599bool Qt3DRRD::ImGuiRenderer::newFrame(const Qt3DRR::OpenGL::RenderView *renderView)
600{
601 if (!m_funcs)
602 m_funcs = m_renderer->submissionContext()->openGLContext()->extraFunctions();
603 if (!m_fontTexture)
604 createDeviceObjects();
605 if (!m_shader)
606 return false;
607
608 ImGuiIO& io = ImGui::GetIO();
609
610 // Setup display size (every frame to accommodate for window resizing)
611 io.DisplaySize = ImVec2(renderView->surfaceSize().width() / renderView->devicePixelRatio(), renderView->surfaceSize().height() / renderView->devicePixelRatio());
612 io.DisplayFramebufferScale = ImVec2(renderView->devicePixelRatio(), renderView->devicePixelRatio());
613
614 // Setup time step
615 double current_time = QDateTime::currentMSecsSinceEpoch() / 1000.;
616 io.DeltaTime = m_time > 0.0 ? static_cast<float>(current_time - m_time) : 1.0f / 60.0f;
617 if (io.DeltaTime == 0.f)
618 io.DeltaTime = 1.0f / 60.0f;
619 m_time = current_time;
620
621 // Setup inputs
622 for (int i = 0; i < 3; i++)
623 io.MouseDown[i] = m_mousePressed[i];
624
625 io.MouseWheelH = m_mouseWheelH;
626 io.MouseWheel = m_mouseWheel;
627 m_mouseWheelH = 0;
628 m_mouseWheel = 0;
629
630 // Start the frame
631 ImGui::NewFrame();
632 return true;
633}
634
635void Qt3DRRD::ImGuiRenderer::onMouseChange(QMouseEvent *event)
636{
637 ImGuiIO& io = ImGui::GetIO();
638 io.MousePos = ImVec2(event->pos().x(), event->pos().y());
639 m_mousePressed[0] = event->buttons() & Qt::LeftButton;
640 m_mousePressed[1] = event->buttons() & Qt::RightButton;
641 m_mousePressed[2] = event->buttons() & Qt::MiddleButton;
642}
643
644void Qt3DRRD::ImGuiRenderer::onWheel(QWheelEvent *event)
645{
646 // 5 lines per unit
647 m_mouseWheelH += event->pixelDelta().x() / (ImGui::GetTextLineHeight());
648 m_mouseWheel += event->pixelDelta().y() / (5.f * ImGui::GetTextLineHeight());
649}
650
651void Qt3DRRD::ImGuiRenderer::onKeyPressRelease(QKeyEvent *event)
652{
653 ImGuiIO& io = ImGui::GetIO();
654 if (keyMap.contains(key: event->key()))
655 io.KeysDown[keyMap[event->key()]] = event->type() == QEvent::KeyPress;
656
657 if (event->type() == QEvent::KeyPress) {
658 QString text = event->text();
659 if (text.size() == 1)
660 io.AddInputCharacter(c: static_cast<ImWchar>(text.at(i: 0).unicode()));
661 }
662
663#ifdef Q_OS_DARWIN
664 io.KeyCtrl = event->modifiers() & Qt::MetaModifier;
665 io.KeyShift = event->modifiers() & Qt::ShiftModifier;
666 io.KeyAlt = event->modifiers() & Qt::AltModifier;
667 io.KeySuper = event->modifiers() & Qt::ControlModifier; // Command key
668#else
669 io.KeyCtrl = event->modifiers() & Qt::ControlModifier;
670 io.KeyShift = event->modifiers() & Qt::ShiftModifier;
671 io.KeyAlt = event->modifiers() & Qt::AltModifier;
672 io.KeySuper = event->modifiers() & Qt::MetaModifier;
673#endif
674}
675
676void Qt3DRRD::ImGuiRenderer::processEvent(QEvent *event)
677{
678 switch (event->type()) {
679 case QEvent::MouseMove:
680 case QEvent::MouseButtonPress:
681 case QEvent::MouseButtonRelease:
682 this->onMouseChange(event: static_cast<QMouseEvent *>(event));
683 break;
684 case QEvent::Wheel:
685 this->onWheel(event: static_cast<QWheelEvent *>(event));
686 break;
687 case QEvent::KeyPress:
688 case QEvent::KeyRelease:
689 this->onKeyPressRelease(event: static_cast<QKeyEvent *>(event));
690 break;
691 default:
692 break;
693 }
694}
695
696QT_END_NAMESPACE
697

source code of qt3d/src/plugins/renderers/opengl/debug/imguirenderer.cpp