| 1 |
// Copyright (C) 2022 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#ifndef QRHIIMGUI_H |
| 5 |
#define QRHIIMGUI_H |
| 6 |
|
| 7 |
#include <rhi/qrhi.h> |
| 8 |
|
| 9 |
QT_BEGIN_NAMESPACE |
| 10 |
|
| 11 |
class QEvent; |
| 12 |
|
| 13 |
class QRhiImguiRenderer |
| 14 |
{ |
| 15 |
public: |
| 16 |
~QRhiImguiRenderer(); |
| 17 |
|
| 18 |
struct CmdListBuffer { |
| 19 |
quint32 offset; |
| 20 |
QByteArray data; |
| 21 |
}; |
| 22 |
|
| 23 |
struct DrawCmd { |
| 24 |
int cmdListBufferIdx; |
| 25 |
void *textureId; |
| 26 |
quint32 indexOffset; |
| 27 |
quint32 elemCount; |
| 28 |
QPointF itemPixelOffset; |
| 29 |
QVector4D clipRect; |
| 30 |
}; |
| 31 |
|
| 32 |
struct StaticRenderData { |
| 33 |
QImage fontTextureData; |
| 34 |
bool isValid() const { return !fontTextureData.isNull(); } |
| 35 |
void reset() { fontTextureData = QImage(); } |
| 36 |
}; |
| 37 |
|
| 38 |
struct FrameRenderData { |
| 39 |
quint32 totalVbufSize = 0; |
| 40 |
quint32 totalIbufSize = 0; |
| 41 |
QVarLengthArray<CmdListBuffer, 4> vbuf; |
| 42 |
QVarLengthArray<CmdListBuffer, 4> ibuf; |
| 43 |
QVarLengthArray<DrawCmd, 4> draw; |
| 44 |
QSize outputPixelSize; |
| 45 |
}; |
| 46 |
|
| 47 |
StaticRenderData sf; |
| 48 |
FrameRenderData f; |
| 49 |
|
| 50 |
void prepare(QRhi *rhi, |
| 51 |
QRhiRenderTarget *rt, |
| 52 |
QRhiCommandBuffer *cb, |
| 53 |
const QMatrix4x4 &mvp, |
| 54 |
float opacity = 1.0f, |
| 55 |
float hdrWhiteLevelMultiplierOrZeroForSDRsRGB = 0.0f); |
| 56 |
void render(); |
| 57 |
void releaseResources(); |
| 58 |
|
| 59 |
enum CustomTextureOwnership { |
| 60 |
TakeCustomTextureOwnership, |
| 61 |
NoCustomTextureOwnership |
| 62 |
}; |
| 63 |
void registerCustomTexture(void *id, |
| 64 |
QRhiTexture *texture, |
| 65 |
QRhiSampler::Filter filter, |
| 66 |
CustomTextureOwnership ownership); |
| 67 |
|
| 68 |
private: |
| 69 |
QRhi *m_rhi = nullptr; |
| 70 |
QRhiRenderTarget *m_rt = nullptr; |
| 71 |
QRhiCommandBuffer *m_cb = nullptr; |
| 72 |
|
| 73 |
std::unique_ptr<QRhiBuffer> m_vbuf; |
| 74 |
std::unique_ptr<QRhiBuffer> m_ibuf; |
| 75 |
std::unique_ptr<QRhiBuffer> m_ubuf; |
| 76 |
std::unique_ptr<QRhiGraphicsPipeline> m_ps; |
| 77 |
QVector<quint32> m_renderPassFormat; |
| 78 |
std::unique_ptr<QRhiSampler> m_linearSampler; |
| 79 |
std::unique_ptr<QRhiSampler> m_nearestSampler; |
| 80 |
|
| 81 |
struct Texture { |
| 82 |
QImage image; |
| 83 |
QRhiTexture *tex = nullptr; |
| 84 |
QRhiShaderResourceBindings *srb = nullptr; |
| 85 |
QRhiSampler::Filter filter = QRhiSampler::Linear; |
| 86 |
bool ownTex = true; |
| 87 |
}; |
| 88 |
QHash<void *, Texture> m_textures; |
| 89 |
}; |
| 90 |
|
| 91 |
class QRhiImgui |
| 92 |
{ |
| 93 |
public: |
| 94 |
QRhiImgui(); |
| 95 |
~QRhiImgui(); |
| 96 |
|
| 97 |
using FrameFunc = std::function<void()>; |
| 98 |
void nextFrame(const QSizeF &logicalOutputSize, float dpr, const QPointF &logicalOffset, FrameFunc frameFunc); |
| 99 |
void syncRenderer(QRhiImguiRenderer *renderer); |
| 100 |
bool processEvent(QEvent *e); |
| 101 |
|
| 102 |
void rebuildFontAtlas(); |
| 103 |
void rebuildFontAtlasWithFont(const QString &filename); |
| 104 |
|
| 105 |
private: |
| 106 |
void *context; |
| 107 |
QRhiImguiRenderer::StaticRenderData sf; |
| 108 |
QRhiImguiRenderer::FrameRenderData f; |
| 109 |
Qt::MouseButtons pressedMouseButtons; |
| 110 |
}; |
| 111 |
|
| 112 |
QT_END_NAMESPACE |
| 113 |
|
| 114 |
#endif |