summaryrefslogtreecommitdiffstats
path: root/src/widgets/kernel/qrhiwidget.h
blob: 45fe7b7482ab0b9f50a43861d265389a868ea541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QRHIWIDGET_H
#define QRHIWIDGET_H

#include <QtWidgets/qwidget.h>

QT_BEGIN_NAMESPACE

class QRhiWidgetPrivate;
class QRhi;
class QRhiTexture;
class QRhiRenderBuffer;
class QRhiRenderTarget;
class QRhiCommandBuffer;

class Q_WIDGETS_EXPORT QRhiWidget : public QWidget
{
    Q_OBJECT
    Q_DECLARE_PRIVATE(QRhiWidget)
    Q_PROPERTY(int sampleCount READ sampleCount WRITE setSampleCount NOTIFY sampleCountChanged)
    Q_PROPERTY(TextureFormat colorBufferFormat READ colorBufferFormat WRITE setColorBufferFormat NOTIFY colorBufferFormatChanged)
    Q_PROPERTY(QSize fixedColorBufferSize READ fixedColorBufferSize WRITE setFixedColorBufferSize NOTIFY fixedColorBufferSizeChanged)
    Q_PROPERTY(bool mirrorVertically READ isMirrorVerticallyEnabled WRITE setMirrorVertically NOTIFY mirrorVerticallyChanged)
    QDOC_PROPERTY(bool autoRenderTarget READ isAutoRenderTargetEnabled WRITE setAutoRenderTarget)

public:
    explicit QRhiWidget(QWidget *parent = nullptr, Qt::WindowFlags f = {});
    ~QRhiWidget() override;

    enum class Api {
        Null,
        OpenGL,
        Metal,
        Vulkan,
        Direct3D11,
        Direct3D12,
    };
    Q_ENUM(Api)

    enum class TextureFormat {
        RGBA8,
        RGBA16F,
        RGBA32F,
        RGB10A2,
    };
    Q_ENUM(TextureFormat)

    Api api() const;
    void setApi(Api api);

    bool isDebugLayerEnabled() const;
    void setDebugLayerEnabled(bool enable);

    int sampleCount() const;
    void setSampleCount(int samples);

    TextureFormat colorBufferFormat() const;
    void setColorBufferFormat(TextureFormat format);

    QSize fixedColorBufferSize() const;
    void setFixedColorBufferSize(QSize pixelSize);
    void setFixedColorBufferSize(int w, int h) { setFixedColorBufferSize(QSize(w, h)); }

    bool isMirrorVerticallyEnabled() const;
    void setMirrorVertically(bool enabled);

    QImage grabFramebuffer() const;

protected:
    bool isAutoRenderTargetEnabled() const;
    void setAutoRenderTarget(bool enabled);

    virtual void initialize(QRhiCommandBuffer *cb);
    virtual void render(QRhiCommandBuffer *cb);
    virtual void releaseResources();

    QRhi *rhi() const;
    QRhiTexture *colorTexture() const;
    QRhiRenderBuffer *msaaColorBuffer() const;
    QRhiTexture *resolveTexture() const;
    QRhiRenderBuffer *depthStencilBuffer() const;
    QRhiRenderTarget *renderTarget() const;

    void resizeEvent(QResizeEvent *e) override;
    void paintEvent(QPaintEvent *e) override;
    bool event(QEvent *e) override;

Q_SIGNALS:
    void frameSubmitted();
    void renderFailed();
    void sampleCountChanged(int samples);
    void colorBufferFormatChanged(TextureFormat format);
    void fixedColorBufferSizeChanged(const QSize &pixelSize);
    void mirrorVerticallyChanged(bool enabled);
};

QT_END_NAMESPACE

#endif