summaryrefslogtreecommitdiffstats
path: root/tests/manual/rhi/shared/examplefw.h
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@qt.io>2023-03-07 16:15:34 +0100
committerLaszlo Agocs <laszlo.agocs@qt.io>2023-03-23 16:39:19 +0100
commitfbb26c2b8832ade041ac9d81213ce3f486e85ef5 (patch)
tree58477e312375fb58a2587cdf3c3e53b3b2d9405c /tests/manual/rhi/shared/examplefw.h
parent401edea98297ac69ed5f52f63ea044ab56dcfac8 (diff)
rhi manual tests: allow having some gui controls
Having a simple Dear ImGui bridge is not just useful for the manual tests, which do not have any other means to displays GUIs, but is in itself an important exercise for the QRhi machinery. Have a new manual test that exercises the built-in ImGui demo window. Then use it in the displacement test for real, to replace the myriads of key presses with on-screen sliders and checkboxes (with less code). Change-Id: I296bafae2a5cce6fc7a447d97e68e5bcec15f451 Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
Diffstat (limited to 'tests/manual/rhi/shared/examplefw.h')
-rw-r--r--tests/manual/rhi/shared/examplefw.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/manual/rhi/shared/examplefw.h b/tests/manual/rhi/shared/examplefw.h
index 853dcc9569..9879e8c5ba 100644
--- a/tests/manual/rhi/shared/examplefw.h
+++ b/tests/manual/rhi/shared/examplefw.h
@@ -36,6 +36,11 @@
#include <QtGui/private/qrhimetal_p.h>
#endif
+#ifdef EXAMPLEFW_IMGUI
+#include "qrhiimgui_p.h"
+#include "imgui.h"
+#endif
+
QShader getShader(const QString &name)
{
QFile f(name);
@@ -45,6 +50,15 @@ QShader getShader(const QString &name)
return QShader();
}
+QByteArray getResource(const QString &name)
+{
+ QFile f(name);
+ if (f.open(QIODevice::ReadOnly))
+ return f.readAll();
+
+ return QByteArray();
+}
+
enum GraphicsApi
{
Null,
@@ -102,6 +116,9 @@ protected:
void customInit();
void customRelease();
void customRender();
+#ifdef EXAMPLEFW_IMGUI
+ void customGui();
+#endif
void exposeEvent(QExposeEvent *) override;
bool event(QEvent *) override;
@@ -130,6 +147,11 @@ protected:
QColor m_clearColor;
+#ifdef EXAMPLEFW_IMGUI
+ QRhiImguiRenderer *m_imguiRenderer;
+ QRhiImgui m_imgui;
+#endif
+
friend int main(int, char**);
};
@@ -204,6 +226,10 @@ bool Window::event(QEvent *e)
break;
default:
+#ifdef EXAMPLEFW_IMGUI
+ if (m_imgui.processEvent(e))
+ return true;
+#endif
break;
}
@@ -276,6 +302,21 @@ void Window::init()
m_rp = m_sc->newCompatibleRenderPassDescriptor();
m_sc->setRenderPassDescriptor(m_rp);
+#ifdef EXAMPLEFW_IMGUI
+ ImGuiIO &io(ImGui::GetIO());
+ io.FontAllowUserScaling = true; // enable ctrl+wheel on windows
+ io.IniFilename = nullptr; // no imgui.ini
+
+ QByteArray font = getResource(QLatin1String(":/fonts/RobotoMono-Medium.ttf"));
+ ImFontConfig fontCfg;
+ fontCfg.FontDataOwnedByAtlas = false;
+ io.Fonts->Clear();
+ io.Fonts->AddFontFromMemoryTTF(font.data(), font.size(), 20.0f, &fontCfg);
+ m_imgui.rebuildFontAtlas();
+
+ m_imguiRenderer = new QRhiImguiRenderer;
+#endif
+
customInit();
}
@@ -292,6 +333,11 @@ void Window::releaseResources()
delete m_sc;
m_sc = nullptr;
+#ifdef EXAMPLEFW_IMGUI
+ delete m_imguiRenderer;
+ m_imguiRenderer = nullptr;
+#endif
+
delete m_r;
m_r = nullptr;
@@ -360,6 +406,20 @@ void Window::render()
m_frameCount = 0;
}
+#ifdef EXAMPLEFW_IMGUI
+ m_imgui.nextFrame(size(), devicePixelRatio(), QPointF(0, 0), std::bind(&Window::customGui, this));
+ m_imgui.syncRenderer(m_imguiRenderer);
+
+ QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
+ QRhiRenderTarget *rt = m_sc->currentFrameRenderTarget();
+ const QSize outputSizeInPixels = m_sc->currentPixelSize();
+ const float dpr = devicePixelRatio();
+
+ QMatrix4x4 guiMvp = m_r->clipSpaceCorrMatrix();
+ guiMvp.ortho(0, outputSizeInPixels.width() / dpr, outputSizeInPixels.height() / dpr, 0, 1, -1);
+ m_imguiRenderer->prepare(m_r, rt, cb, guiMvp, 1.0f);
+#endif
+
customRender();
m_r->endFrame(m_sc, endFrameFlags);