| 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 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | namespace Qt3DRR = Qt3DRender::Render; |
| 35 | namespace Qt3DRRD = Qt3DRR::Debug; |
| 36 | |
| 37 | namespace { |
| 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 | |
| 129 | Qt3DRRD::ImGuiRenderer::ImGuiRenderer(Qt3DRR::OpenGL::Renderer *renderer) |
| 130 | : m_renderer(renderer) |
| 131 | { |
| 132 | ImGui::CreateContext(); |
| 133 | |
| 134 | ImGuiIO &io = ImGui::GetIO(); |
| 135 | |
| 136 | #ifndef QT_NO_CLIPBOARD |
| 137 | io.SetClipboardTextFn = [](void *user_data, const char *text) { |
| 138 | Q_UNUSED(user_data); |
| 139 | QGuiApplication::clipboard()->setText(QString::fromLatin1(ba: text)); |
| 140 | }; |
| 141 | io.GetClipboardTextFn = [](void *user_data) { |
| 142 | Q_UNUSED(user_data); |
| 143 | g_currentClipboardText = QGuiApplication::clipboard()->text().toUtf8(); |
| 144 | return static_cast<const char *>(g_currentClipboardText.data()); |
| 145 | }; |
| 146 | #endif |
| 147 | |
| 148 | std::fill(first: std::begin(arr&: m_fpsLog), last: std::end(arr&: m_fpsLog), value: 0.f); |
| 149 | std::fill(first: std::begin(arr&: m_jobsLog), last: std::end(arr&: m_jobsLog), value: 0.f); |
| 150 | m_fpsRange.first = m_fpsRange.second = 0.f; |
| 151 | m_jobsRange.first = m_jobsRange.second = 0.f; |
| 152 | } |
| 153 | |
| 154 | Qt3DRRD::ImGuiRenderer::~ImGuiRenderer() = default; |
| 155 | |
| 156 | void Qt3DRRD::ImGuiRenderer::renderDebugOverlay(const std::vector<Qt3DRR::OpenGL::RenderView *> &renderViews, const Qt3DRR::OpenGL::RenderView *renderView, int jobsInLastFrame) |
| 157 | { |
| 158 | if (!newFrame(renderView)) |
| 159 | return; |
| 160 | |
| 161 | const int renderViewsCount = int(renderViews.size()); |
| 162 | |
| 163 | int logIndex = qMin(IMGUI_PERF_LOG_SIZE - 1, b: ImGui::GetFrameCount()); |
| 164 | if (logIndex == IMGUI_PERF_LOG_SIZE - 1) { |
| 165 | std::rotate(first: std::begin(arr&: m_fpsLog), middle: std::begin(arr&: m_fpsLog) + 1, last: std::end(arr&: m_fpsLog)); |
| 166 | std::rotate(first: std::begin(arr&: m_jobsLog), middle: std::begin(arr&: m_jobsLog) + 1, last: std::end(arr&: m_jobsLog)); |
| 167 | } |
| 168 | m_fpsLog[logIndex] = ImGui::GetIO().Framerate; |
| 169 | m_fpsRange.first = m_fpsRange.second = 0.f; |
| 170 | for (float v: m_fpsLog) { |
| 171 | m_fpsRange.first = qMin(a: m_fpsRange.first, b: qMax(a: v - 5.f, b: 0.f)); |
| 172 | m_fpsRange.second = qMax(a: m_fpsRange.second, b: v + 2.f); |
| 173 | } |
| 174 | m_jobsLog[logIndex] = jobsInLastFrame; |
| 175 | m_jobsRange.first = m_jobsRange.second = 0.f; |
| 176 | for (float v: m_jobsLog) { |
| 177 | m_jobsRange.first = qMin(a: m_jobsRange.first, b: qMax(a: v - 5.f, b: 0.f)); |
| 178 | m_jobsRange.second = qMax(a: m_jobsRange.second, b: v + 2.f); |
| 179 | } |
| 180 | |
| 181 | { |
| 182 | ImGui::Begin(name: "Qt3D Profiling" ); |
| 183 | char caption[50]; |
| 184 | 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)); |
| 185 | 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)); |
| 186 | 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)); |
| 187 | |
| 188 | int nCommands = 0; |
| 189 | int nVertices = 0; |
| 190 | int nPrimitives = 0; |
| 191 | QSet<HGeometryRenderer> inUseGeometries; |
| 192 | QSet<Qt3DCore::QNodeId> inUseTextures; |
| 193 | for (int j=0; j<renderViewsCount; j++) { |
| 194 | Qt3DRR::OpenGL::RenderView *rv = renderViews.at(n: j); |
| 195 | nCommands += rv->commandCount(); |
| 196 | rv->forEachCommand(func: [&](const Qt3DRR::OpenGL::RenderCommand &command) { |
| 197 | if (command.m_type != Qt3DRR::OpenGL::RenderCommand::Draw) |
| 198 | return; |
| 199 | nVertices += command.m_primitiveCount; |
| 200 | nPrimitives += vertexToPrimitiveCount(primitiveType: command.m_primitiveType, numVertices: command.m_primitiveCount); |
| 201 | inUseGeometries.insert(value: command.m_geometryRenderer); |
| 202 | const auto &textures = command.m_parameterPack.textures(); |
| 203 | for (const auto &ns: textures) |
| 204 | inUseTextures.insert(value: ns.nodeId); |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | auto columnNumber = [](int i) { |
| 209 | if (i == 0) |
| 210 | ImGui::Text(fmt: "--" ); |
| 211 | else |
| 212 | ImGui::Text(fmt: " %d" , i); |
| 213 | ImGui::NextColumn(); |
| 214 | }; |
| 215 | auto column2Numbers = [](int i, int of) { |
| 216 | if (of == 0) |
| 217 | ImGui::Text(fmt: "--" ); |
| 218 | else |
| 219 | ImGui::Text(fmt: " %d of %d" , i, of); |
| 220 | ImGui::NextColumn(); |
| 221 | }; |
| 222 | |
| 223 | ImGui::Columns(count: 5); |
| 224 | ImGui::Separator(); |
| 225 | for (auto s: {"Jobs" , "RV" , "Cmds" , "Verts" , "Prims" }) { |
| 226 | ImGui::Text(fmt: "#%s" , s); |
| 227 | ImGui::NextColumn(); |
| 228 | } |
| 229 | for (auto s: {jobsInLastFrame, renderViewsCount, nCommands, nVertices, nPrimitives}) |
| 230 | columnNumber(s); |
| 231 | |
| 232 | ImGui::Columns(count: 3); |
| 233 | ImGui::Separator(); |
| 234 | for (auto s: {"Entities" , "Geometries" , "Textures" }) { |
| 235 | ImGui::Text(fmt: "#%s" , s); |
| 236 | ImGui::NextColumn(); |
| 237 | } |
| 238 | columnNumber(m_renderer->nodeManagers()->renderNodesManager()->count()); |
| 239 | column2Numbers(inUseGeometries.size(), m_renderer->nodeManagers()->geometryRendererManager()->count()); |
| 240 | column2Numbers(inUseTextures.size(), m_renderer->nodeManagers()->textureManager()->count()); |
| 241 | |
| 242 | ImGui::Columns(count: 1); |
| 243 | ImGui::Separator(); |
| 244 | |
| 245 | bool pj = m_renderer->services()->systemInformation()->isTraceEnabled(); |
| 246 | ImGui::AlignTextToFramePadding(); |
| 247 | ImGui::Text(fmt: "Profiling: " ); |
| 248 | ImGui::SameLine(); |
| 249 | if (ImGui::Checkbox(label: "Jobs" , v: &pj)) |
| 250 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "setTraceEnabled" , c: Qt::QueuedConnection, Q_ARG(bool, pj)); |
| 251 | ImGui::SameLine(); |
| 252 | if (ImGui::Button(label: "Reveal" )) |
| 253 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "revealLogFolder" , c: Qt::QueuedConnection); |
| 254 | |
| 255 | ImGui::AlignTextToFramePadding(); |
| 256 | ImGui::Text(fmt: "Show:" ); |
| 257 | ImGui::SameLine(); |
| 258 | if (ImGui::Button(label: "GL Info" )) |
| 259 | m_showGLInfoWindow = !m_showGLInfoWindow; |
| 260 | ImGui::SameLine(); |
| 261 | if (ImGui::Button(label: "Render Views" )) |
| 262 | m_showRenderDetailsWindow = !m_showRenderDetailsWindow; |
| 263 | |
| 264 | ImGui::AlignTextToFramePadding(); |
| 265 | ImGui::Text(fmt: "Dump:" ); |
| 266 | ImGui::SameLine(); |
| 267 | if (ImGui::Button(label: "SceneGraph##1" )) |
| 268 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand" , |
| 269 | c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render scenegraph" ))); |
| 270 | ImGui::SameLine(); |
| 271 | if (ImGui::Button(label: "FrameGraph##1" )) |
| 272 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand" , |
| 273 | c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render framegraph" ))); |
| 274 | ImGui::SameLine(); |
| 275 | if (ImGui::Button(label: "Render Views##1" )) |
| 276 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand" , |
| 277 | c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render framepaths" ))); |
| 278 | |
| 279 | ImGui::AlignTextToFramePadding(); |
| 280 | ImGui::Text(fmt: " " ); |
| 281 | ImGui::SameLine(); |
| 282 | if (ImGui::Button(label: "Filter State##1" )) |
| 283 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand" , |
| 284 | c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render filterstates" ))); |
| 285 | ImGui::SameLine(); |
| 286 | if (ImGui::Button(label: "JobsGraph##1" )) |
| 287 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand" , |
| 288 | c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("dump jobs" ))); |
| 289 | |
| 290 | ImGui::End(); |
| 291 | |
| 292 | if (m_showGLInfoWindow) |
| 293 | showGLInfo(); |
| 294 | if (m_showRenderDetailsWindow) |
| 295 | showRenderDetails(renderViews); |
| 296 | } |
| 297 | |
| 298 | ImGui::Render(); |
| 299 | renderDrawList(draw_data: ImGui::GetDrawData()); |
| 300 | } |
| 301 | |
| 302 | void Qt3DRRD::ImGuiRenderer::setCapabilities(const QString &capabilities) |
| 303 | { |
| 304 | m_capabilities = capabilities.toLatin1(); |
| 305 | } |
| 306 | |
| 307 | void Qt3DRRD::ImGuiRenderer::showGLInfo() |
| 308 | { |
| 309 | ImGui::Begin(name: "Open GL Details" , p_open: &m_showGLInfoWindow); |
| 310 | ImGui::Text(fmt: "%s" , m_capabilities.data()); |
| 311 | ImGui::End(); |
| 312 | } |
| 313 | |
| 314 | void Qt3DRRD::ImGuiRenderer::showRenderDetails(const std::vector<Qt3DRR::OpenGL::RenderView *> &renderViews) |
| 315 | { |
| 316 | ImGui::Begin(name: "Render Views" , p_open: &m_showRenderDetailsWindow); |
| 317 | |
| 318 | int i = 1; |
| 319 | for (const Qt3DRR::OpenGL::RenderView *view : renderViews) { |
| 320 | QString label(QLatin1String("View " ) + QString::number(i++)); |
| 321 | if (ImGui::TreeNode(label: label.toLatin1().data())) { |
| 322 | ImGui::Text(fmt: "Viewport: (%.1f, %.1f, %.1f, %.1f)" , view->viewport().x(), view->viewport().y(), |
| 323 | view->viewport().width(), view->viewport().height()); |
| 324 | ImGui::Text(fmt: "Surface Size: (%d, %d)" , view->surfaceSize().width(), view->surfaceSize().height()); |
| 325 | ImGui::Text(fmt: "Pixel Ratio: %.1f" , view->devicePixelRatio()); |
| 326 | ImGui::Text(fmt: "No Draw: %s" , view->noDraw() ? "TRUE" : "FALSE" ); |
| 327 | ImGui::Text(fmt: "Frustum Culling: %s" , view->frustumCulling() ? "TRUE" : "FALSE" ); |
| 328 | ImGui::Text(fmt: "Compute: %s" , view->isCompute() ? "TRUE" : "FALSE" ); |
| 329 | ImGui::Text(fmt: "Clear Depth Value: %f" , static_cast<double>(view->clearDepthValue())); |
| 330 | ImGui::Text(fmt: "Clear Stencil Value: %d" , view->clearStencilValue()); |
| 331 | int j = 1; |
| 332 | |
| 333 | view->forEachCommand(func: [&](const Qt3DRR::OpenGL::RenderCommand &command) { |
| 334 | GeometryRenderer *rGeometryRenderer = m_renderer->nodeManagers()->data<GeometryRenderer, GeometryRendererManager>(handle: command.m_geometryRenderer); |
| 335 | QString label = QString(QLatin1String("Command %1 {%2}" )).arg(args: QString::number(j++), args: QString::number(rGeometryRenderer->peerId().id())); |
| 336 | if (ImGui::TreeNode(label: label.toLatin1().data())) { |
| 337 | ImGui::Text(fmt: "Primitive Type: %s %s" , primitiveTypeName(primitiveType: command.m_primitiveType), |
| 338 | command.m_drawIndexed ? "(indexed)" : "" ); |
| 339 | ImGui::Text(fmt: "# Vertices: %d" , command.m_primitiveCount); |
| 340 | ImGui::Text(fmt: "# Primitives: %d" , vertexToPrimitiveCount(primitiveType: command.m_primitiveType, numVertices: command.m_primitiveCount)); |
| 341 | ImGui::Text(fmt: "# Instances: %d" , command.m_instanceCount); |
| 342 | ImGui::TreePop(); |
| 343 | } |
| 344 | }); |
| 345 | ImGui::TreePop(); |
| 346 | ImGui::Separator(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | if (ImGui::Button(label: "Dump" )) |
| 351 | QMetaObject::invokeMethod(obj: m_renderer->services()->systemInformation(), member: "dumpCommand" , |
| 352 | c: Qt::QueuedConnection, Q_ARG(QString, QLatin1String("render rendercommands" ))); |
| 353 | ImGui::End(); |
| 354 | } |
| 355 | |
| 356 | void Qt3DRRD::ImGuiRenderer::renderDrawList(ImDrawData *draw_data) |
| 357 | { |
| 358 | // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) |
| 359 | ImGuiIO& io = ImGui::GetIO(); |
| 360 | int fb_width = int(io.DisplaySize.x * io.DisplayFramebufferScale.x); |
| 361 | int fb_height = int(io.DisplaySize.y * io.DisplayFramebufferScale.y); |
| 362 | if (fb_width == 0 || fb_height == 0) |
| 363 | return; |
| 364 | draw_data->ScaleClipRects(fb_scale: io.DisplayFramebufferScale); |
| 365 | |
| 366 | // Backup GL state |
| 367 | GLint last_active_texture; m_funcs->glGetIntegerv(GL_ACTIVE_TEXTURE, params: &last_active_texture); |
| 368 | m_funcs->glActiveTexture(GL_TEXTURE0); |
| 369 | GLint last_program; m_funcs->glGetIntegerv(GL_CURRENT_PROGRAM, params: &last_program); |
| 370 | GLint last_texture; m_funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, params: &last_texture); |
| 371 | GLint last_array_buffer; m_funcs->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, params: &last_array_buffer); |
| 372 | GLint last_element_array_buffer; m_funcs->glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, params: &last_element_array_buffer); |
| 373 | GLint last_vertex_array; m_funcs->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, params: &last_vertex_array); |
| 374 | GLint last_blend_src_rgb; m_funcs->glGetIntegerv(GL_BLEND_SRC_RGB, params: &last_blend_src_rgb); |
| 375 | GLint last_blend_dst_rgb; m_funcs->glGetIntegerv(GL_BLEND_DST_RGB, params: &last_blend_dst_rgb); |
| 376 | GLint last_blend_src_alpha; m_funcs->glGetIntegerv(GL_BLEND_SRC_ALPHA, params: &last_blend_src_alpha); |
| 377 | GLint last_blend_dst_alpha; m_funcs->glGetIntegerv(GL_BLEND_DST_ALPHA, params: &last_blend_dst_alpha); |
| 378 | GLint last_blend_equation_rgb; m_funcs->glGetIntegerv(GL_BLEND_EQUATION_RGB, params: &last_blend_equation_rgb); |
| 379 | GLint last_blend_equation_alpha; m_funcs->glGetIntegerv(GL_BLEND_EQUATION_ALPHA, params: &last_blend_equation_alpha); |
| 380 | GLint last_viewport[4]; m_funcs->glGetIntegerv(GL_VIEWPORT, params: last_viewport); |
| 381 | GLint last_scissor_box[4]; m_funcs->glGetIntegerv(GL_SCISSOR_BOX, params: last_scissor_box); |
| 382 | GLboolean last_enable_blend = m_funcs->glIsEnabled(GL_BLEND); |
| 383 | GLboolean last_enable_cull_face = m_funcs->glIsEnabled(GL_CULL_FACE); |
| 384 | GLboolean last_enable_depth_test = m_funcs->glIsEnabled(GL_DEPTH_TEST); |
| 385 | GLboolean last_enable_scissor_test = m_funcs->glIsEnabled(GL_SCISSOR_TEST); |
| 386 | |
| 387 | // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled |
| 388 | m_funcs->glEnable(GL_BLEND); |
| 389 | m_funcs->glBlendEquation(GL_FUNC_ADD); |
| 390 | m_funcs->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 391 | m_funcs->glDisable(GL_CULL_FACE); |
| 392 | m_funcs->glDisable(GL_DEPTH_TEST); |
| 393 | m_funcs->glEnable(GL_SCISSOR_TEST); |
| 394 | |
| 395 | // Setup viewport, orthographic projection matrix |
| 396 | m_funcs->glViewport(x: 0, y: 0, width: static_cast<GLsizei>(fb_width), height: static_cast<GLsizei>(fb_height)); |
| 397 | const float ortho_projection[4][4] = { |
| 398 | { 2.0f/io.DisplaySize.x, 0.0f, 0.0f, 0.0f }, |
| 399 | { 0.0f, 2.0f/-io.DisplaySize.y, 0.0f, 0.0f }, |
| 400 | { 0.0f, 0.0f, -1.0f, 0.0f }, |
| 401 | {-1.0f, 1.0f, 0.0f, 1.0f }, |
| 402 | }; |
| 403 | m_funcs->glUseProgram(program: m_shaderHandle); |
| 404 | m_funcs->glUniform1i(location: m_attribLocationTex, x: 0); |
| 405 | m_funcs->glUniformMatrix4fv(location: m_attribLocationProjMtx, count: 1, GL_FALSE, value: &ortho_projection[0][0]); |
| 406 | m_funcs->glBindVertexArray(array: m_vaoHandle); |
| 407 | |
| 408 | for (int n = 0; n < draw_data->CmdListsCount; n++) { |
| 409 | const ImDrawList* cmd_list = draw_data->CmdLists[n]; |
| 410 | const ImDrawIdx* idx_buffer_offset = nullptr; |
| 411 | |
| 412 | m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: m_vboHandle); |
| 413 | 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); |
| 414 | |
| 415 | m_funcs->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer: m_elementsHandle); |
| 416 | 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); |
| 417 | |
| 418 | for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { |
| 419 | const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; |
| 420 | if (pcmd->UserCallback) { |
| 421 | pcmd->UserCallback(cmd_list, pcmd); |
| 422 | } else { |
| 423 | m_funcs->glBindTexture(GL_TEXTURE_2D, texture: (GLuint)(intptr_t)pcmd->TextureId); |
| 424 | m_funcs->glScissor(x: static_cast<int>(pcmd->ClipRect.x), y: static_cast<int>(fb_height - pcmd->ClipRect.w), |
| 425 | width: static_cast<int>(pcmd->ClipRect.z - pcmd->ClipRect.x), height: static_cast<int>(pcmd->ClipRect.w - pcmd->ClipRect.y)); |
| 426 | 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); |
| 427 | } |
| 428 | idx_buffer_offset += pcmd->ElemCount; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // Restore modified GL state |
| 433 | m_funcs->glUseProgram(program: last_program); |
| 434 | m_funcs->glBindTexture(GL_TEXTURE_2D, texture: last_texture); |
| 435 | m_funcs->glActiveTexture(texture: last_active_texture); |
| 436 | m_funcs->glBindVertexArray(array: last_vertex_array); |
| 437 | m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: last_array_buffer); |
| 438 | m_funcs->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer: last_element_array_buffer); |
| 439 | m_funcs->glBlendEquationSeparate(modeRGB: last_blend_equation_rgb, modeAlpha: last_blend_equation_alpha); |
| 440 | m_funcs->glBlendFuncSeparate(srcRGB: last_blend_src_rgb, dstRGB: last_blend_dst_rgb, srcAlpha: last_blend_src_alpha, dstAlpha: last_blend_dst_alpha); |
| 441 | if (last_enable_blend) |
| 442 | m_funcs->glEnable(GL_BLEND); else m_funcs->glDisable(GL_BLEND); |
| 443 | if (last_enable_cull_face) |
| 444 | m_funcs->glEnable(GL_CULL_FACE); else m_funcs->glDisable(GL_CULL_FACE); |
| 445 | if (last_enable_depth_test) |
| 446 | m_funcs->glEnable(GL_DEPTH_TEST); else m_funcs->glDisable(GL_DEPTH_TEST); |
| 447 | if (last_enable_scissor_test) |
| 448 | m_funcs->glEnable(GL_SCISSOR_TEST); else m_funcs->glDisable(GL_SCISSOR_TEST); |
| 449 | 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])); |
| 450 | 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])); |
| 451 | } |
| 452 | |
| 453 | bool Qt3DRRD::ImGuiRenderer::createFontsTexture() |
| 454 | { |
| 455 | // Build texture atlas |
| 456 | ImGuiIO& io = ImGui::GetIO(); |
| 457 | unsigned char* pixels; |
| 458 | int width, height; |
| 459 | 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. |
| 460 | |
| 461 | // Upload texture to graphics system |
| 462 | GLint last_texture; |
| 463 | m_funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, params: &last_texture); |
| 464 | m_funcs->glGenTextures(n: 1, textures: &m_fontTexture); |
| 465 | m_funcs->glBindTexture(GL_TEXTURE_2D, texture: m_fontTexture); |
| 466 | m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 467 | m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 468 | m_funcs->glTexImage2D(GL_TEXTURE_2D, level: 0, GL_RGBA, width, height, border: 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 469 | |
| 470 | // Store our identifier |
| 471 | io.Fonts->TexID = (void *)(intptr_t)m_fontTexture; |
| 472 | |
| 473 | // Restore state |
| 474 | m_funcs->glBindTexture(GL_TEXTURE_2D, texture: last_texture); |
| 475 | |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | bool Qt3DRRD::ImGuiRenderer::createDeviceObjects() |
| 480 | { |
| 481 | auto *glContext = m_renderer->submissionContext()->openGLContext(); |
| 482 | if (glContext->format().majorVersion() < 3) { |
| 483 | qWarning() << "Qt3D Profiling overlay requires GL or GL ES >= 3" ; |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | // Backup GL state |
| 488 | GLint last_texture, last_array_buffer, last_vertex_array; |
| 489 | m_funcs->glGetIntegerv(GL_TEXTURE_BINDING_2D, params: &last_texture); |
| 490 | m_funcs->glGetIntegerv(GL_ARRAY_BUFFER_BINDING, params: &last_array_buffer); |
| 491 | m_funcs->glGetIntegerv(GL_VERTEX_ARRAY_BINDING, params: &last_vertex_array); |
| 492 | |
| 493 | const GLchar *vertex_shader = |
| 494 | "#version 330\n" |
| 495 | "uniform mat4 ProjMtx;\n" |
| 496 | "in vec2 Position;\n" |
| 497 | "in vec2 UV;\n" |
| 498 | "in vec4 Color;\n" |
| 499 | "out vec2 Frag_UV;\n" |
| 500 | "out vec4 Frag_Color;\n" |
| 501 | "void main()\n" |
| 502 | "{\n" |
| 503 | " Frag_UV = UV;\n" |
| 504 | " Frag_Color = Color;\n" |
| 505 | " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" |
| 506 | "}\n" ; |
| 507 | |
| 508 | const GLchar* fragment_shader = |
| 509 | "#version 330\n" |
| 510 | "uniform sampler2D Texture;\n" |
| 511 | "in vec2 Frag_UV;\n" |
| 512 | "in vec4 Frag_Color;\n" |
| 513 | "out vec4 Out_Color;\n" |
| 514 | "void main()\n" |
| 515 | "{\n" |
| 516 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" |
| 517 | "}\n" ; |
| 518 | |
| 519 | const GLchar *vertex_shader_es3 = |
| 520 | "#version 300 es\n" |
| 521 | "uniform mat4 ProjMtx;\n" |
| 522 | "in vec2 Position;\n" |
| 523 | "in vec2 UV;\n" |
| 524 | "in vec4 Color;\n" |
| 525 | "out vec2 Frag_UV;\n" |
| 526 | "out vec4 Frag_Color;\n" |
| 527 | "void main()\n" |
| 528 | "{\n" |
| 529 | " Frag_UV = UV;\n" |
| 530 | " Frag_Color = Color;\n" |
| 531 | " gl_Position = ProjMtx * vec4(Position.xy, 0, 1);\n" |
| 532 | "}\n" ; |
| 533 | |
| 534 | const GLchar* fragment_shader_es3 = |
| 535 | "#version 300 es\n" |
| 536 | "precision highp float;\n" |
| 537 | "uniform sampler2D Texture;\n" |
| 538 | "in vec2 Frag_UV;\n" |
| 539 | "in vec4 Frag_Color;\n" |
| 540 | "out vec4 Out_Color;\n" |
| 541 | "void main()\n" |
| 542 | "{\n" |
| 543 | " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" |
| 544 | "}\n" ; |
| 545 | |
| 546 | QString logs; |
| 547 | m_shader = new QOpenGLShaderProgram(this); |
| 548 | if (glContext->isOpenGLES()) { |
| 549 | if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Vertex, source: vertex_shader_es3)) |
| 550 | logs += m_shader->log(); |
| 551 | if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Fragment, source: fragment_shader_es3)) |
| 552 | logs += m_shader->log(); |
| 553 | } else { |
| 554 | if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Vertex, source: vertex_shader)) |
| 555 | logs += m_shader->log(); |
| 556 | if (!m_shader->addShaderFromSourceCode(type: QOpenGLShader::Fragment, source: fragment_shader)) |
| 557 | logs += m_shader->log(); |
| 558 | } |
| 559 | m_shader->link(); |
| 560 | logs += m_shader->log(); |
| 561 | if (!logs.isEmpty()) |
| 562 | qWarning() << logs; |
| 563 | m_shaderHandle = m_shader->programId(); |
| 564 | |
| 565 | m_attribLocationTex = m_funcs->glGetUniformLocation(program: m_shaderHandle, name: "Texture" ); |
| 566 | m_attribLocationProjMtx = m_funcs->glGetUniformLocation(program: m_shaderHandle, name: "ProjMtx" ); |
| 567 | m_attribLocationPosition = m_funcs->glGetAttribLocation(program: m_shaderHandle, name: "Position" ); |
| 568 | m_attribLocationUV = m_funcs->glGetAttribLocation(program: m_shaderHandle, name: "UV" ); |
| 569 | m_attribLocationColor = m_funcs->glGetAttribLocation(program: m_shaderHandle, name: "Color" ); |
| 570 | |
| 571 | m_funcs->glGenBuffers(n: 1, buffers: &m_vboHandle); |
| 572 | m_funcs->glGenBuffers(n: 1, buffers: &m_elementsHandle); |
| 573 | |
| 574 | m_funcs->glGenVertexArrays(n: 1, arrays: &m_vaoHandle); |
| 575 | m_funcs->glBindVertexArray(array: m_vaoHandle); |
| 576 | m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: m_vboHandle); |
| 577 | m_funcs->glEnableVertexAttribArray(index: m_attribLocationPosition); |
| 578 | m_funcs->glEnableVertexAttribArray(index: m_attribLocationUV); |
| 579 | m_funcs->glEnableVertexAttribArray(index: m_attribLocationColor); |
| 580 | |
| 581 | #define OFFSETOF(TYPE, ELEMENT) (reinterpret_cast<size_t>(&((static_cast<TYPE *>(nullptr))->ELEMENT))) |
| 582 | m_funcs->glVertexAttribPointer(indx: m_attribLocationPosition, size: 2, GL_FLOAT, GL_FALSE, stride: sizeof(ImDrawVert), ptr: reinterpret_cast<GLvoid*>(OFFSETOF(ImDrawVert, pos))); |
| 583 | m_funcs->glVertexAttribPointer(indx: m_attribLocationUV, size: 2, GL_FLOAT, GL_FALSE, stride: sizeof(ImDrawVert), ptr: reinterpret_cast<GLvoid*>(OFFSETOF(ImDrawVert, uv))); |
| 584 | m_funcs->glVertexAttribPointer(indx: m_attribLocationColor, size: 4, GL_UNSIGNED_BYTE, GL_TRUE, stride: sizeof(ImDrawVert), ptr: reinterpret_cast<GLvoid*>(OFFSETOF(ImDrawVert, col))); |
| 585 | #undef OFFSETOF |
| 586 | |
| 587 | createFontsTexture(); |
| 588 | |
| 589 | // Restore modified GL state |
| 590 | m_funcs->glBindTexture(GL_TEXTURE_2D, texture: last_texture); |
| 591 | m_funcs->glBindBuffer(GL_ARRAY_BUFFER, buffer: last_array_buffer); |
| 592 | m_funcs->glBindVertexArray(array: last_vertex_array); |
| 593 | |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | bool Qt3DRRD::ImGuiRenderer::newFrame(const Qt3DRR::OpenGL::RenderView *renderView) |
| 598 | { |
| 599 | if (!m_funcs) |
| 600 | m_funcs = m_renderer->submissionContext()->openGLContext()->extraFunctions(); |
| 601 | if (!m_fontTexture) |
| 602 | createDeviceObjects(); |
| 603 | if (!m_shader) |
| 604 | return false; |
| 605 | |
| 606 | ImGuiIO& io = ImGui::GetIO(); |
| 607 | |
| 608 | // Setup display size (every frame to accommodate for window resizing) |
| 609 | io.DisplaySize = ImVec2(renderView->surfaceSize().width() / renderView->devicePixelRatio(), renderView->surfaceSize().height() / renderView->devicePixelRatio()); |
| 610 | io.DisplayFramebufferScale = ImVec2(renderView->devicePixelRatio(), renderView->devicePixelRatio()); |
| 611 | |
| 612 | // Setup time step |
| 613 | double current_time = QDateTime::currentMSecsSinceEpoch() / 1000.; |
| 614 | io.DeltaTime = m_time > 0.0 ? static_cast<float>(current_time - m_time) : 1.0f / 60.0f; |
| 615 | if (io.DeltaTime == 0.f) |
| 616 | io.DeltaTime = 1.0f / 60.0f; |
| 617 | m_time = current_time; |
| 618 | |
| 619 | // Setup inputs |
| 620 | for (int i = 0; i < 3; i++) |
| 621 | io.MouseDown[i] = m_mousePressed[i]; |
| 622 | |
| 623 | io.MouseWheelH = m_mouseWheelH; |
| 624 | io.MouseWheel = m_mouseWheel; |
| 625 | m_mouseWheelH = 0; |
| 626 | m_mouseWheel = 0; |
| 627 | |
| 628 | // Start the frame |
| 629 | ImGui::NewFrame(); |
| 630 | return true; |
| 631 | } |
| 632 | |
| 633 | void Qt3DRRD::ImGuiRenderer::onMouseChange(QMouseEvent *event) |
| 634 | { |
| 635 | ImGuiIO& io = ImGui::GetIO(); |
| 636 | io.MousePos = ImVec2(event->pos().x(), event->pos().y()); |
| 637 | m_mousePressed[0] = event->buttons() & Qt::LeftButton; |
| 638 | m_mousePressed[1] = event->buttons() & Qt::RightButton; |
| 639 | m_mousePressed[2] = event->buttons() & Qt::MiddleButton; |
| 640 | } |
| 641 | |
| 642 | void Qt3DRRD::ImGuiRenderer::onWheel(QWheelEvent *event) |
| 643 | { |
| 644 | // 5 lines per unit |
| 645 | m_mouseWheelH += event->pixelDelta().x() / (ImGui::GetTextLineHeight()); |
| 646 | m_mouseWheel += event->pixelDelta().y() / (5.f * ImGui::GetTextLineHeight()); |
| 647 | } |
| 648 | |
| 649 | void Qt3DRRD::ImGuiRenderer::onKeyPressRelease(QKeyEvent *event) |
| 650 | { |
| 651 | ImGuiIO& io = ImGui::GetIO(); |
| 652 | if (keyMap.contains(key: event->key())) |
| 653 | io.AddKeyEvent(key: keyMap[event->key()], down: event->type() == QEvent::KeyPress); |
| 654 | |
| 655 | if (event->type() == QEvent::KeyPress) { |
| 656 | QString text = event->text(); |
| 657 | if (text.size() == 1) |
| 658 | io.AddInputCharacter(c: static_cast<ImWchar>(text.at(i: 0).unicode())); |
| 659 | } |
| 660 | |
| 661 | #ifdef Q_OS_DARWIN |
| 662 | io.KeyCtrl = event->modifiers() & Qt::MetaModifier; |
| 663 | io.KeyShift = event->modifiers() & Qt::ShiftModifier; |
| 664 | io.KeyAlt = event->modifiers() & Qt::AltModifier; |
| 665 | io.KeySuper = event->modifiers() & Qt::ControlModifier; // Command key |
| 666 | #else |
| 667 | io.KeyCtrl = event->modifiers() & Qt::ControlModifier; |
| 668 | io.KeyShift = event->modifiers() & Qt::ShiftModifier; |
| 669 | io.KeyAlt = event->modifiers() & Qt::AltModifier; |
| 670 | io.KeySuper = event->modifiers() & Qt::MetaModifier; |
| 671 | #endif |
| 672 | } |
| 673 | |
| 674 | void Qt3DRRD::ImGuiRenderer::processEvent(QEvent *event) |
| 675 | { |
| 676 | switch (event->type()) { |
| 677 | case QEvent::MouseMove: |
| 678 | case QEvent::MouseButtonPress: |
| 679 | case QEvent::MouseButtonRelease: |
| 680 | this->onMouseChange(event: static_cast<QMouseEvent *>(event)); |
| 681 | break; |
| 682 | case QEvent::Wheel: |
| 683 | this->onWheel(event: static_cast<QWheelEvent *>(event)); |
| 684 | break; |
| 685 | case QEvent::KeyPress: |
| 686 | case QEvent::KeyRelease: |
| 687 | this->onKeyPressRelease(event: static_cast<QKeyEvent *>(event)); |
| 688 | break; |
| 689 | default: |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | QT_END_NAMESPACE |
| 695 | |