Commit 0855a4d2 authored by Laszlo Agocs's avatar Laszlo Agocs
Browse files

Start implementing QRhiImgui

parent c18b80c5
......@@ -120,6 +120,8 @@ void Window::customInit()
void Window::customRelease()
{
d.imgui.releaseResources();
for (QRhiResource *r : d.releasePool)
r->releaseAndDestroy();
d.releasePool.clear();
......@@ -129,6 +131,7 @@ void Window::customRender()
{
const QSize outputSizeInPixels = m_sc->currentPixelSize();
QRhiCommandBuffer *cb = m_sc->currentFrameCommandBuffer();
QRhiRenderTarget *rt = m_sc->currentFrameRenderTarget();
QRhiResourceUpdateBatch *u = m_r->nextResourceUpdateBatch();
if (d.initialUpdates) {
......@@ -142,10 +145,12 @@ void Window::customRender()
mvp.rotate(d.rotation, 0, 1, 0);
u->updateDynamicBuffer(d.ubuf, 0, 64, mvp.constData());
cb->beginPass(m_sc->currentFrameRenderTarget(), { 0.4f, 0.7f, 0.0f, 1.0f }, { 1.0f, 0 }, u);
cb->beginPass(rt, { 0.4f, 0.7f, 0.0f, 1.0f }, { 1.0f, 0 }, u);
cb->setGraphicsPipeline(d.ps);
cb->setViewport(QRhiViewport(0, 0, outputSizeInPixels.width(), outputSizeInPixels.height()));
cb->setVertexInput(0, { { d.vbuf, 0 } });
cb->draw(36);
cb->endPass();
d.imgui.imguiPass(cb, rt);
}
......@@ -45,9 +45,86 @@ QRhiImgui::QRhiImgui()
QRhiImgui::~QRhiImgui()
{
releaseResources();
delete d;
}
void QRhiImgui::setFrameFunc(FrameFunc f)
{
d->frame = f;
}
void QRhiImgui::demoWindow()
{
ImGui::ShowDemoWindow(&d->showDemoWindow);
}
bool QRhiImgui::imguiPass(QRhiCommandBuffer *cb, QRhiRenderTarget *rt)
{
ImGuiIO &io(ImGui::GetIO());
if (d->textures.isEmpty()) {
unsigned char *pixels;
int w, h;
io.Fonts->GetTexDataAsRGBA32(&pixels, &w, &h);
const QImage wrapperImg((const uchar *) pixels, w, h, QImage::Format_RGBA8888);
d->textures.append(wrapperImg.copy());
io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(quintptr(d->textures.count() - 1)));
}
const QSize outputSize = rt->sizeInPixels();
const float dpr = rt->devicePixelRatio();
io.DisplaySize.x = outputSize.width() / dpr;
io.DisplaySize.y = outputSize.height() / dpr;
io.DisplayFramebufferScale = ImVec2(dpr, dpr);
ImGui::NewFrame();
if (d->frame)
d->frame();
ImGui::Render();
ImDrawData *draw = ImGui::GetDrawData();
draw->ScaleClipRects(ImVec2(dpr, dpr));
for (int n = 0; n < draw->CmdListsCount; ++n) {
const ImDrawList *cmdList = draw->CmdLists[n];
const ImDrawIdx *indexBufOffset = nullptr;
// e.vbuf = QByteArray((const char *) cmdList->VtxBuffer.Data, cmdList->VtxBuffer.Size * sizeof(ImDrawVert));
// e.ibuf = QByteArray((const char *) cmdList->IdxBuffer.Data, cmdList->IdxBuffer.Size * sizeof(ImDrawIdx));
for (int i = 0; i < cmdList->CmdBuffer.Size; ++i) {
const ImDrawCmd *cmd = &cmdList->CmdBuffer[i];
if (!cmd->UserCallback) {
// ImGuiRenderer::FrameDesc::Cmd qcmd;
// qcmd.elemCount = cmd->ElemCount;
// qcmd.indexOffset = indexBufOffset;
// qcmd.scissorPixelBottomLeft = QPointF(cmd->ClipRect.x, io.DisplaySize.y * m_dpr - cmd->ClipRect.w);
// qcmd.scissorPixelSize = QSizeF(cmd->ClipRect.z - cmd->ClipRect.x, cmd->ClipRect.w - cmd->ClipRect.y);
// qcmd.textureIndex = uint(reinterpret_cast<quintptr>(cmd->TextureId));
// e.cmds.append(qcmd);
} else {
cmd->UserCallback(cmdList, cmd);
}
indexBufOffset += cmd->ElemCount;
}
}
return true;
}
void QRhiImgui::releaseResources()
{
}
QRhiImgui::FrameFunc QRhiImgui::frameFunc() const
{
return d->frame;
}
QRhiImguiPrivate::QRhiImguiPrivate()
{
ImGui::CreateContext();
......
......@@ -37,7 +37,8 @@
#ifndef QRHIIMGUI_H
#define QRHIIMGUI_H
#include <QtRhi/qtrhiglobal.h>
#include <QtRhi/qrhi.h>
#include <functional>
QT_BEGIN_NAMESPACE
......@@ -49,6 +50,14 @@ public:
QRhiImgui();
~QRhiImgui();
typedef std::function<void()> FrameFunc;
void setFrameFunc(FrameFunc f);
FrameFunc frameFunc() const;
void demoWindow();
bool imguiPass(QRhiCommandBuffer *cb, QRhiRenderTarget *rt);
void releaseResources();
private:
Q_DISABLE_COPY(QRhiImgui)
QRhiImguiPrivate *d = nullptr;
......
......@@ -53,6 +53,10 @@ class QRhiImguiPrivate
public:
QRhiImguiPrivate();
~QRhiImguiPrivate();
QRhiImgui::FrameFunc frame = nullptr;
bool showDemoWindow = true;
QVector<QImage> textures;
};
QT_END_NAMESPACE
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment