qrhi.h 29.9 KB
Newer Older
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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt RHI module
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QRHI_H
#define QRHI_H

#include <QtRhi/qtrhiglobal.h>
#include <QVector4D>
#include <QVector2D>
#include <QSize>
#include <QMatrix4x4>
#include <QVector>
#include <QImage>
#include <QtShaderTools/QBakedShader>
48
#include <functional>
49 50 51 52

QT_BEGIN_NAMESPACE

class QWindow;
Laszlo Agocs's avatar
Laszlo Agocs committed
53 54 55 56 57
class QRhiImplementation;
class QRhiBuffer;
class QRhiRenderBuffer;
class QRhiTexture;
class QRhiSampler;
58 59
class QRhiCommandBuffer;
class QRhiResourceUpdateBatch;
Laszlo Agocs's avatar
Laszlo Agocs committed
60
struct QRhiResourceUpdateBatchPrivate;
61
class QRhiProfiler;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

struct Q_RHI_EXPORT QRhiColorClearValue
{
    QRhiColorClearValue() : rgba(0, 0, 0, 1) { }
    explicit QRhiColorClearValue(const QVector4D &rgba_) : rgba(rgba_) { }
    QRhiColorClearValue(float r, float g, float b, float a) : rgba(r, g, b, a) { }
    QVector4D rgba;
};

Q_DECLARE_TYPEINFO(QRhiColorClearValue, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiDepthStencilClearValue
{
    QRhiDepthStencilClearValue() : d(1), s(0) { }
    QRhiDepthStencilClearValue(float d_, quint32 s_) : d(d_), s(s_) { }
    float d;
    quint32 s;
};

Q_DECLARE_TYPEINFO(QRhiDepthStencilClearValue, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiViewport
{
Laszlo Agocs's avatar
Laszlo Agocs committed
85 86 87
    QRhiViewport()
        : r(0, 0, 1280, 720), minDepth(0), maxDepth(1)
    { }
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    QRhiViewport(float x, float y, float w, float h, float minDepth_ = 0.0f, float maxDepth_ = 1.0f)
        : r(x, y, w, h), minDepth(minDepth_), maxDepth(maxDepth_)
    { }
    QVector4D r;
    float minDepth;
    float maxDepth;
};

Q_DECLARE_TYPEINFO(QRhiViewport, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiScissor
{
    QRhiScissor() { }
    QRhiScissor(int x, int y, int w, int h)
        : r(x, y, w, h)
    { }
    QVector4D r;
};

Q_DECLARE_TYPEINFO(QRhiScissor, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiVertexInputLayout
{
    struct Q_RHI_EXPORT Binding {
        enum Classification {
            PerVertex,
            PerInstance
        };
        Binding() { }
117 118
        Binding(quint32 stride_, Classification cls = PerVertex, int stepRate = 1)
            : stride(stride_), classification(cls), instanceStepRate(stepRate)
119
        { }
120
        quint32 stride;
121
        Classification classification;
122
        int instanceStepRate;
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    };

    struct Q_RHI_EXPORT Attribute {
        enum Format {
            Float4,
            Float3,
            Float2,
            Float,
            UNormByte4,
            UNormByte2,
            UNormByte
        };
        Attribute() { }
        Attribute(int binding_, int location_, Format format_, quint32 offset_)
            : binding(binding_), location(location_), format(format_), offset(offset_)
        { }
        int binding;
        int location;
        Format format;
        quint32 offset;
    };

145
    QVector<Binding> bindings;
146 147 148 149 150 151 152 153 154 155 156 157
    QVector<Attribute> attributes;
};

Q_DECLARE_TYPEINFO(QRhiVertexInputLayout::Binding, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiVertexInputLayout::Attribute, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiVertexInputLayout, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiGraphicsShaderStage
{
    enum Type {
        Vertex,
        Fragment,
158 159
        TessellationControl,
        TessellationEvaluation
160 161 162 163 164 165 166 167 168 169 170 171 172
    };

    QRhiGraphicsShaderStage() { }
    QRhiGraphicsShaderStage(Type type_, const QBakedShader &shader_)
        : type(type_), shader(shader_)
    { }

    Type type;
    QBakedShader shader;
};

Q_DECLARE_TYPEINFO(QRhiGraphicsShaderStage, Q_MOVABLE_TYPE);

Laszlo Agocs's avatar
Laszlo Agocs committed
173 174 175 176 177 178 179 180 181 182
struct Q_RHI_EXPORT QRhiShaderResourceBinding
{
    enum Type {
        UniformBuffer,
        SampledTexture
    };

    enum StageFlag {
        VertexStage = 1 << 0,
        FragmentStage = 1 << 1,
183 184
        TessellationControlStage = 1 << 2,
        TessellationEvaluationStage = 1 << 3
Laszlo Agocs's avatar
Laszlo Agocs committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    };
    Q_DECLARE_FLAGS(StageFlags, StageFlag)

    static QRhiShaderResourceBinding uniformBuffer(int binding_, StageFlags stage_, QRhiBuffer *buf_);
    static QRhiShaderResourceBinding uniformBuffer(int binding_, StageFlags stage_, QRhiBuffer *buf_, int offset_, int size_);
    static QRhiShaderResourceBinding sampledTexture(int binding_, StageFlags stage_, QRhiTexture *tex_, QRhiSampler *sampler_);

    int binding;
    StageFlags stage;
    Type type;
    struct UniformBufferData {
        QRhiBuffer *buf;
        int offset;
        int maybeSize;
    };
    struct SampledTextureData {
        QRhiTexture *tex;
        QRhiSampler *sampler;
    };
    union {
        UniformBufferData ubuf;
        SampledTextureData stex;
    };
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiShaderResourceBinding::StageFlags)
Q_DECLARE_TYPEINFO(QRhiShaderResourceBinding, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiTextureRenderTargetDescription
{
    struct Q_RHI_EXPORT ColorAttachment {
        ColorAttachment() { }
        ColorAttachment(QRhiTexture *texture_) : texture(texture_) { }
218 219
        ColorAttachment(QRhiRenderBuffer *renderBuffer_) : renderBuffer(renderBuffer_) { }

Laszlo Agocs's avatar
Laszlo Agocs committed
220
        QRhiTexture *texture = nullptr;
221
        QRhiRenderBuffer *renderBuffer = nullptr;
222

223 224
        int layer = 0;
        int level = 0;
225 226

        QRhiTexture *resolveTexture = nullptr;
227
        int resolveLayer = 0;
228
        int resolveLevel = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    };

    QRhiTextureRenderTargetDescription()
    { }
    QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment)
    { colorAttachments.append(colorAttachment); }
    QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment, QRhiRenderBuffer *depthStencilBuffer_)
        : depthStencilBuffer(depthStencilBuffer_)
    { colorAttachments.append(colorAttachment); }
    QRhiTextureRenderTargetDescription(const ColorAttachment &colorAttachment, QRhiTexture *depthTexture_)
        : depthTexture(depthTexture_)
    { colorAttachments.append(colorAttachment); }

    QVector<ColorAttachment> colorAttachments;
    QRhiRenderBuffer *depthStencilBuffer = nullptr;
    QRhiTexture *depthTexture = nullptr;
};

Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription::ColorAttachment, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription, Q_MOVABLE_TYPE);

struct Q_RHI_EXPORT QRhiTextureUploadDescription
{
    struct Q_RHI_EXPORT Layer {
        struct Q_RHI_EXPORT MipLevel {
            MipLevel() { }
            MipLevel(const QImage &image_) : image(image_) { }
256
            MipLevel(const QByteArray &compressedData_) : compressedData(compressedData_) { }
257

Laszlo Agocs's avatar
Laszlo Agocs committed
258
            QImage image;
259
            QByteArray compressedData;
260

261
            QPoint destinationTopLeft;
262 263
            QSize sourceSize;
            QPoint sourceTopLeft;
Laszlo Agocs's avatar
Laszlo Agocs committed
264
        };
265

Laszlo Agocs's avatar
Laszlo Agocs committed
266 267 268 269
        Layer() { }
        Layer(const QVector<MipLevel> &mipImages_) : mipImages(mipImages_) { }
        QVector<MipLevel> mipImages;
    };
270

Laszlo Agocs's avatar
Laszlo Agocs committed
271 272
    QRhiTextureUploadDescription() { }
    QRhiTextureUploadDescription(const QVector<Layer> &layers_) : layers(layers_) { }
273
    QVector<Layer> layers;
Laszlo Agocs's avatar
Laszlo Agocs committed
274 275 276 277 278 279
};

Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription::Layer::MipLevel, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription::Layer, Q_MOVABLE_TYPE);
Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription, Q_MOVABLE_TYPE);

280 281
struct Q_RHI_EXPORT QRhiTextureCopyDescription
{
282
    QSize pixelSize;
283 284 285

    int sourceLayer = 0;
    int sourceLevel = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
286
    QPoint sourceTopLeft;
287 288 289

    int destinationLayer = 0;
    int destinationLevel = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
290
    QPoint destinationTopLeft;
291 292 293 294
};

Q_DECLARE_TYPEINFO(QRhiTextureCopyDescription, Q_MOVABLE_TYPE);

295
struct Q_RHI_EXPORT QRhiReadbackDescription
Laszlo Agocs's avatar
Laszlo Agocs committed
296
{
297 298
    QRhiReadbackDescription() { }
    QRhiReadbackDescription(QRhiTexture *texture_) : texture(texture_) { }
299

300 301 302
    QRhiTexture *texture = nullptr;
    int layer = 0;
    int level = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
303 304
};

305 306
Q_DECLARE_TYPEINFO(QRhiReadbackDescription, Q_MOVABLE_TYPE);

307 308 309 310
struct Q_RHI_EXPORT QRhiNativeHandles
{
};

311 312 313 314
class Q_RHI_EXPORT QRhiResource
{
public:
    virtual ~QRhiResource();
315

316 317 318
    virtual void release() = 0;
    void releaseAndDestroy();

319 320 321
    QByteArray name() const;
    void setName(const QByteArray &name);

322 323 324
protected:
    QRhiResource(QRhiImplementation *rhi_);
    Q_DISABLE_COPY(QRhiResource)
325 326
    QRhiImplementation *rhi = nullptr;
    QByteArray objectName;
327 328 329 330 331 332
};

class Q_RHI_EXPORT QRhiBuffer : public QRhiResource
{
public:
    enum Type {
333 334 335
        Immutable,
        Static,
        Dynamic
336 337 338 339 340 341 342 343 344
    };

    enum UsageFlag {
        VertexBuffer = 1 << 0,
        IndexBuffer = 1 << 1,
        UniformBuffer = 1 << 2
    };
    Q_DECLARE_FLAGS(UsageFlags, UsageFlag)

Laszlo Agocs's avatar
Laszlo Agocs committed
345 346 347 348 349 350 351 352
    Type type() const { return m_type; }
    void setType(Type t) { m_type = t; }

    UsageFlags usage() const { return m_usage; }
    void setUsage(UsageFlags u) { m_usage = u; }

    int size() const { return m_size; }
    void setSize(int sz) { m_size = sz; }
353 354 355 356 357

    virtual bool build() = 0;

protected:
    QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, int size_);
Laszlo Agocs's avatar
Laszlo Agocs committed
358 359
    Type m_type;
    UsageFlags m_usage;
360
    int m_size;
Laszlo Agocs's avatar
Laszlo Agocs committed
361
    void *m_reserved;
362 363 364 365 366 367 368 369 370
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiBuffer::UsageFlags)

class Q_RHI_EXPORT QRhiTexture : public QRhiResource
{
public:
    enum Flag {
        RenderTarget = 1 << 0,
371
        ChangesFrequently = 1 << 1,
372
        CubeMap = 1 << 2,
373
        MipMapped = 1 << 3,
374
        sRGB = 1 << 4,
375
        UsedAsTransferSource = 1 << 5,
376
        UsedWithGenerateMips = 1 << 6
377 378 379 380
    };
    Q_DECLARE_FLAGS(Flags, Flag)

    enum Format {
381 382
        UnknownFormat,

383 384 385 386 387 388
        RGBA8,
        BGRA8,
        R8,
        R16,

        D16,
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        D32,

        BC1,
        BC2,
        BC3,
        BC4,
        BC5,
        BC6H,
        BC7,

        ETC2_RGB8,
        ETC2_RGB8A1,
        ETC2_RGBA8,

        ASTC_4x4,
        ASTC_5x4,
        ASTC_5x5,
        ASTC_6x5,
        ASTC_6x6,
        ASTC_8x5,
        ASTC_8x6,
        ASTC_8x8,
        ASTC_10x5,
        ASTC_10x6,
        ASTC_10x8,
        ASTC_10x10,
        ASTC_12x10,
        ASTC_12x12
417 418
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
419 420 421 422 423 424 425 426
    Format format() const { return m_format; }
    void setFormat(Format fmt) { m_format = fmt; }

    QSize pixelSize() const { return m_pixelSize; }
    void setPixelSize(const QSize &sz) { m_pixelSize = sz; }

    Flags flags() const { return m_flags; }
    void setFlags(Flags f) { m_flags = f; }
427

Laszlo Agocs's avatar
Laszlo Agocs committed
428 429 430
    int sampleCount() const { return m_sampleCount; }
    void setSampleCount(int s) { m_sampleCount = s; }

431
    virtual bool build() = 0;
432 433
    virtual const QRhiNativeHandles *nativeHandles();
    virtual bool buildFrom(const QRhiNativeHandles *src);
434

435
protected:
Laszlo Agocs's avatar
Laszlo Agocs committed
436 437
    QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_,
                int sampleCount_, Flags flags_);
Laszlo Agocs's avatar
Laszlo Agocs committed
438 439
    Format m_format;
    QSize m_pixelSize;
Laszlo Agocs's avatar
Laszlo Agocs committed
440
    int m_sampleCount;
Laszlo Agocs's avatar
Laszlo Agocs committed
441
    Flags m_flags;
Laszlo Agocs's avatar
Laszlo Agocs committed
442
    void *m_reserved;
443 444 445 446 447 448 449 450
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTexture::Flags)

class Q_RHI_EXPORT QRhiSampler : public QRhiResource
{
public:
    enum Filter {
451
        None,
452 453 454 455 456 457 458 459 460 461 462 463
        Nearest,
        Linear
    };

    enum AddressMode {
        Repeat,
        ClampToEdge,
        Border,
        Mirror,
        MirrorOnce
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    Filter magFilter() const { return m_magFilter; }
    void setMagFilter(Filter f) { m_magFilter = f; }

    Filter minFilter() const { return m_minFilter; }
    void setMinFilter(Filter f) { m_minFilter = f; }

    Filter mipmapMode() const { return m_mipmapMode; }
    void setMipmapMode(Filter f) { m_mipmapMode = f; }

    AddressMode addressU() const { return m_addressU; }
    void setAddressU(AddressMode mode) { m_addressU = mode; }

    AddressMode addressV() const { return m_addressV; }
    void setAddressV(AddressMode mode) { m_addressV = mode; }

    AddressMode addressW() const { return m_addressW; }
    void setAddressW(AddressMode mode) { m_addressW = mode; }
481 482 483 484 485 486 487

    virtual bool build() = 0;

protected:
    QRhiSampler(QRhiImplementation *rhi,
                Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
                AddressMode u_, AddressMode v_, AddressMode w_);
Laszlo Agocs's avatar
Laszlo Agocs committed
488 489 490 491 492 493 494
    Filter m_magFilter;
    Filter m_minFilter;
    Filter m_mipmapMode;
    AddressMode m_addressU;
    AddressMode m_addressV;
    AddressMode m_addressW;
    void *m_reserved;
495 496
};

Laszlo Agocs's avatar
Laszlo Agocs committed
497 498 499 500 501 502 503 504 505
class Q_RHI_EXPORT QRhiRenderBuffer : public QRhiResource
{
public:
    enum Type {
        DepthStencil,
        Color
    };

    enum Flag {
506
        UsedWithSwapChainOnly = 1 << 0
Laszlo Agocs's avatar
Laszlo Agocs committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    };
    Q_DECLARE_FLAGS(Flags, Flag)

    Type type() const { return m_type; }
    void setType(Type t) { m_type = t; }

    QSize pixelSize() const { return m_pixelSize; }
    void setPixelSize(const QSize &sz) { m_pixelSize = sz; }

    int sampleCount() const { return m_sampleCount; }
    void setSampleCount(int s) { m_sampleCount = s; }

    Flags flags() const { return m_flags; }
    void setFlags(Flags h) { m_flags = h; }

    virtual bool build() = 0;

    virtual QRhiTexture::Format backingFormat() const = 0;

protected:
    QRhiRenderBuffer(QRhiImplementation *rhi, Type type_, const QSize &pixelSize_,
                     int sampleCount_, Flags flags_);
    Type m_type;
    QSize m_pixelSize;
    int m_sampleCount;
    Flags m_flags;
    void *m_reserved;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiRenderBuffer::Flags)

538
class Q_RHI_EXPORT QRhiRenderPassDescriptor : public QRhiResource
539 540
{
protected:
541
    QRhiRenderPassDescriptor(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
542
    void *m_reserved;
543 544 545 546 547 548 549 550 551 552 553 554
};

class Q_RHI_EXPORT QRhiRenderTarget : public QRhiResource
{
public:
    enum Type {
        RtRef,
        RtTexture
    };

    virtual Type type() const = 0;
    virtual QSize sizeInPixels() const = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
555
    virtual float devicePixelRatio() const = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
556

557 558
    QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
    void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
559 560 561

protected:
    QRhiRenderTarget(QRhiImplementation *rhi);
562
    QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
Laszlo Agocs's avatar
Laszlo Agocs committed
563
    void *m_reserved;
564 565 566 567 568 569
};

class Q_RHI_EXPORT QRhiTextureRenderTarget : public QRhiRenderTarget
{
public:
    enum Flag {
570 571
        PreserveColorContents = 1 << 0,
        PreserveDepthStencilContents = 1 << 1
572 573 574
    };
    Q_DECLARE_FLAGS(Flags, Flag)

Laszlo Agocs's avatar
Laszlo Agocs committed
575 576
    QRhiTextureRenderTargetDescription description() const { return m_desc; }
    void setDescription(const QRhiTextureRenderTargetDescription &desc) { m_desc = desc; }
577

Laszlo Agocs's avatar
Laszlo Agocs committed
578 579 580
    Flags flags() const { return m_flags; }
    void setFlags(Flags f) { m_flags = f; }

581
    virtual QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
582

583 584 585 586
    virtual bool build() = 0;

protected:
    QRhiTextureRenderTarget(QRhiImplementation *rhi, const QRhiTextureRenderTargetDescription &desc_, Flags flags_);
Laszlo Agocs's avatar
Laszlo Agocs committed
587 588
    QRhiTextureRenderTargetDescription m_desc;
    Flags m_flags;
589 590 591 592 593 594 595
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTextureRenderTarget::Flags)

class Q_RHI_EXPORT QRhiShaderResourceBindings : public QRhiResource
{
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
596 597
    QVector<QRhiShaderResourceBinding> bindings() const { return m_bindings; }
    void setBindings(const QVector<QRhiShaderResourceBinding> &b) { m_bindings = b; }
598 599 600 601 602

    virtual bool build() = 0;

protected:
    QRhiShaderResourceBindings(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
603 604
    QVector<QRhiShaderResourceBinding> m_bindings;
    void *m_reserved;
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
};

class Q_RHI_EXPORT QRhiGraphicsPipeline : public QRhiResource
{
public:
    enum Flag {
        UsesBlendConstants = 1 << 0,
        UsesStencilRef = 1 << 1,
        UsesScissor = 1 << 2
    };
    Q_DECLARE_FLAGS(Flags, Flag)

    enum Topology {
        Triangles,
        TriangleStrip,
        Lines,
        LineStrip,
        Points
    };

625
    enum CullMode {
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
        None,
        Front,
        Back
    };

    enum FrontFace {
        CCW,
        CW
    };

    enum ColorMaskComponent {
        R = 1 << 0,
        G = 1 << 1,
        B = 1 << 2,
        A = 1 << 3
    };
    Q_DECLARE_FLAGS(ColorMask, ColorMaskComponent)

    enum BlendFactor {
        Zero,
        One,
        SrcColor,
        OneMinusSrcColor,
        DstColor,
        OneMinusDstColor,
        SrcAlpha,
        OneMinusSrcAlpha,
        DstAlpha,
        OneMinusDstAlpha,
        ConstantColor,
        OneMinusConstantColor,
        ConstantAlpha,
        OneMinusConstantAlpha,
        SrcAlphaSaturate,
        Src1Color,
        OneMinusSrc1Color,
        Src1Alpha,
        OneMinusSrc1Alpha
    };

    enum BlendOp {
        Add,
        Subtract,
        ReverseSubtract,
        Min,
        Max
    };

    struct TargetBlend {
        ColorMask colorWrite = ColorMask(0xF); // R | G | B | A
        bool enable = false;
        BlendFactor srcColor = One;
        BlendFactor dstColor = OneMinusSrcAlpha;
        BlendOp opColor = Add;
        BlendFactor srcAlpha = One;
        BlendFactor dstAlpha = OneMinusSrcAlpha;
        BlendOp opAlpha = Add;
    };

    enum CompareOp {
        Never,
        Less,
        Equal,
        LessOrEqual,
        Greater,
        NotEqual,
        GreaterOrEqual,
        Always
    };

    enum StencilOp {
        StencilZero,
        Keep,
        Replace,
        IncrementAndClamp,
        DecrementAndClamp,
        Invert,
        IncrementAndWrap,
        DecrementAndWrap
    };

    struct StencilOpState {
        StencilOp failOp = Keep;
        StencilOp depthFailOp = Keep;
        StencilOp passOp = Keep;
        CompareOp compareOp = Always;
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
    Flags flags() const { return m_flags; }
    void setFlags(Flags f) { m_flags = f; }

    Topology topology() const { return m_topology; }
    void setTopology(Topology t) { m_topology = t; }

    CullMode cullMode() const { return m_cullMode; }
    void setCullMode(CullMode mode) { m_cullMode = mode; }

    FrontFace frontFace() const { return m_frontFace; }
    void setFrontFace(FrontFace f) { m_frontFace = f; }

    QVector<TargetBlend> targetBlends() const { return m_targetBlends; }
    void setTargetBlends(const QVector<TargetBlend> &blends) { m_targetBlends = blends; }

    bool hasDepthTest() const { return m_depthTest; }
    void setDepthTest(bool enable) { m_depthTest = enable; }

    bool hasDepthWrite() const { return m_depthWrite; }
    void setDepthWrite(bool enable) { m_depthWrite = enable; }

    CompareOp depthOp() const { return m_depthOp; }
    void setDepthOp(CompareOp op) { m_depthOp = op; }

    bool hasStencilTest() const { return m_stencilTest; }
    void setStencilTest(bool enable) { m_stencilTest = enable; }

    StencilOpState stencilFront() const { return m_stencilFront; }
    void setStencilFront(const StencilOpState &state) { m_stencilFront = state; }

    StencilOpState stencilBack() const { return m_stencilBack; }
    void setStencilBack(const StencilOpState &state) { m_stencilBack = state; }

    quint32 stencilReadMask() const { return m_stencilReadMask; }
    void setStencilReadMask(quint32 mask) { m_stencilReadMask = mask; }

    quint32 stencilWriteMask() const { return m_stencilWriteMask; }
    void setStencilWriteMask(quint32 mask) { m_stencilWriteMask = mask; }

    int sampleCount() const { return m_sampleCount; }
    void setSampleCount(int s) { m_sampleCount = s; }

    QVector<QRhiGraphicsShaderStage> shaderStages() const { return m_shaderStages; }
    void setShaderStages(const QVector<QRhiGraphicsShaderStage> &stages) { m_shaderStages = stages; }

    QRhiVertexInputLayout vertexInputLayout() const { return m_vertexInputLayout; }
    void setVertexInputLayout(const QRhiVertexInputLayout &layout) { m_vertexInputLayout = layout; }

    QRhiShaderResourceBindings *shaderResourceBindings() const { return m_shaderResourceBindings; }
    void setShaderResourceBindings(QRhiShaderResourceBindings *srb) { m_shaderResourceBindings = srb; }

765 766
    QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
    void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
767 768 769 770 771

    virtual bool build() = 0;

protected:
    QRhiGraphicsPipeline(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
772 773 774 775
    Flags m_flags;
    Topology m_topology = Triangles;
    CullMode m_cullMode = None;
    FrontFace m_frontFace = CCW;
776
    QVector<TargetBlend> m_targetBlends;
Laszlo Agocs's avatar
Laszlo Agocs committed
777 778 779 780 781 782
    bool m_depthTest = false;
    bool m_depthWrite = false;
    CompareOp m_depthOp = Less;
    bool m_stencilTest = false;
    StencilOpState m_stencilFront;
    StencilOpState m_stencilBack;
783 784 785
    quint32 m_stencilReadMask = 0xFF;
    quint32 m_stencilWriteMask = 0xFF;
    int m_sampleCount = 1;
Laszlo Agocs's avatar
Laszlo Agocs committed
786 787
    QVector<QRhiGraphicsShaderStage> m_shaderStages;
    QRhiVertexInputLayout m_vertexInputLayout;
788
    QRhiShaderResourceBindings *m_shaderResourceBindings = nullptr;
789
    QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
Laszlo Agocs's avatar
Laszlo Agocs committed
790
    void *m_reserved;
791 792 793 794 795 796 797 798 799
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiGraphicsPipeline::Flags)
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiGraphicsPipeline::ColorMask)
Q_DECLARE_TYPEINFO(QRhiGraphicsPipeline::TargetBlend, Q_MOVABLE_TYPE);

class Q_RHI_EXPORT QRhiSwapChain : public QRhiResource
{
public:
800
    enum Flag {
801
        SurfaceHasPreMulAlpha = 1 << 0,
802
        SurfaceHasNonPreMulAlpha = 1 << 1,
803
        sRGB = 1 << 2,
804 805
        UsedAsTransferSource = 1 << 3,
        NoVSync = 1 << 4
806
    };
807
    Q_DECLARE_FLAGS(Flags, Flag)
808

Laszlo Agocs's avatar
Laszlo Agocs committed
809 810 811
    QWindow *window() const { return m_window; }
    void setWindow(QWindow *window) { m_window = window; }

812 813
    Flags flags() const { return m_flags; }
    void setFlags(Flags f) { m_flags = f; }
Laszlo Agocs's avatar
Laszlo Agocs committed
814 815 816 817 818 819 820

    QRhiRenderBuffer *depthStencil() const { return m_depthStencil; }
    void setDepthStencil(QRhiRenderBuffer *ds) { m_depthStencil = ds; }

    int sampleCount() const { return m_sampleCount; }
    void setSampleCount(int samples) { m_sampleCount = samples; }

821 822
    QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
    void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
Laszlo Agocs's avatar
Laszlo Agocs committed
823

Laszlo Agocs's avatar
Laszlo Agocs committed
824 825 826
    QObject *target() const { return m_target; }
    void setTarget(QObject *obj) { m_target = obj; }

827 828
    QSize currentPixelSize() const { return m_currentPixelSize; }

829 830
    virtual QRhiCommandBuffer *currentFrameCommandBuffer() = 0;
    virtual QRhiRenderTarget *currentFrameRenderTarget() = 0;
831
    virtual QSize surfacePixelSize() = 0;
832
    virtual QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
833
    virtual bool buildOrResize() = 0;
834 835 836

protected:
    QRhiSwapChain(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
837
    QWindow *m_window = nullptr;
838
    Flags m_flags;
Laszlo Agocs's avatar
Laszlo Agocs committed
839 840
    QRhiRenderBuffer *m_depthStencil = nullptr;
    int m_sampleCount = 1;
841
    QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
Laszlo Agocs's avatar
Laszlo Agocs committed
842
    QObject *m_target = nullptr;
843
    QSize m_currentPixelSize;
Laszlo Agocs's avatar
Laszlo Agocs committed
844
    void *m_reserved;
845 846
};

847
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiSwapChain::Flags)
848

849 850 851 852 853 854 855 856
class Q_RHI_EXPORT QRhiCommandBuffer : public QRhiResource
{
public:
    enum IndexFormat {
        IndexUInt16,
        IndexUInt32
    };

857 858
    void resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates);

859
    void beginPass(QRhiRenderTarget *rt,
860 861
                   const QRhiColorClearValue &colorClearValue,
                   const QRhiDepthStencilClearValue &depthStencilClearValue,
862
                   QRhiResourceUpdateBatch *resourceUpdates = nullptr);
863
    void endPass(QRhiResourceUpdateBatch *resourceUpdates = nullptr);
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

    void setGraphicsPipeline(QRhiGraphicsPipeline *ps,
                             QRhiShaderResourceBindings *srb = nullptr);

    using VertexInput = QPair<QRhiBuffer *, quint32>; // buffer, offset
    void setVertexInput(int startBinding, const QVector<VertexInput> &bindings,
                        QRhiBuffer *indexBuf = nullptr, quint32 indexOffset = 0,
                        IndexFormat indexFormat = IndexUInt16);

    void setViewport(const QRhiViewport &viewport);
    void setScissor(const QRhiScissor &scissor);
    void setBlendConstants(const QVector4D &c);
    void setStencilRef(quint32 refValue);

    void draw(quint32 vertexCount,
              quint32 instanceCount = 1,
              quint32 firstVertex = 0,
              quint32 firstInstance = 0);

    void drawIndexed(quint32 indexCount,
                     quint32 instanceCount = 1,
                     quint32 firstIndex = 0,
                     qint32 vertexOffset = 0,
                     quint32 firstInstance = 0);

889 890 891 892
    void debugMarkBegin(const QByteArray &name);
    void debugMarkEnd();
    void debugMarkMsg(const QByteArray &msg);

893 894 895 896 897 898 899 900 901 902 903 904
protected:
    QRhiCommandBuffer(QRhiImplementation *rhi);
    void *m_reserved;
};

struct Q_RHI_EXPORT QRhiReadbackResult
{
    std::function<void()> completed = nullptr;
    QRhiTexture::Format format;
    QSize pixelSize;
    QByteArray data;
}; // non-movable due to the std::function
905

906
class Q_RHI_EXPORT QRhiResourceUpdateBatch
907 908
{
public:
909 910 911 912 913 914
    enum TexturePrepareFlag {
        TextureRead = 1 << 0,
        TextureWrite = 1 << 1
    };
    Q_DECLARE_FLAGS(TexturePrepareFlags, TexturePrepareFlag)

915
    ~QRhiResourceUpdateBatch();
916

917 918
    void release();

919 920
    void merge(QRhiResourceUpdateBatch *other);

921
    void updateDynamicBuffer(QRhiBuffer *buf, int offset, int size, const void *data);
922
    void uploadStaticBuffer(QRhiBuffer *buf, int offset, int size, const void *data);
923
    void uploadStaticBuffer(QRhiBuffer *buf, const void *data);
Laszlo Agocs's avatar
Laszlo Agocs committed
924
    void uploadTexture(QRhiTexture *tex, const QRhiTextureUploadDescription &desc);
925
    void uploadTexture(QRhiTexture *tex, const QImage &image);
926
    void copyTexture(QRhiTexture *dst, QRhiTexture *src, const QRhiTextureCopyDescription &desc = QRhiTextureCopyDescription());
927
    void readBackTexture(const QRhiReadbackDescription &rb, QRhiReadbackResult *result);
928
    void generateMips(QRhiTexture *tex);
929

930 931
    void prepareTextureForUse(QRhiTexture *tex, TexturePrepareFlags flags);

932 933 934 935 936 937 938 939
private:
    QRhiResourceUpdateBatch(QRhiImplementation *rhi);
    Q_DISABLE_COPY(QRhiResourceUpdateBatch)
    QRhiResourceUpdateBatchPrivate *d;
    friend struct QRhiResourceUpdateBatchPrivate;
    friend class QRhi;
};

940 941
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiResourceUpdateBatch::TexturePrepareFlags)

942 943 944 945 946 947 948 949
struct Q_RHI_EXPORT QRhiInitParams
{
};

class Q_RHI_EXPORT QRhi
{
public:
    enum Implementation {
Laszlo Agocs's avatar
Laszlo Agocs committed
950
        Null,
951 952 953 954 955 956
        Vulkan,
        OpenGLES2,
        D3D11,
        Metal
    };

957
    enum Flag {
958 959
        EnableProfiling = 1 << 0,
        EnableDebugMarkers = 1 << 1
960 961 962
    };
    Q_DECLARE_FLAGS(Flags, Flag)

963 964 965 966 967 968 969
    enum FrameOpResult {
        FrameOpSuccess = 0,
        FrameOpError,
        FrameOpSwapChainOutOfDate,
        FrameOpDeviceLost
    };

970 971
    enum Feature {
        MultisampleTexture = 1,
972
        MultisampleRenderBuffer,
973
        DebugMarkers,
974 975
        Timestamps,
        Instancing,
976 977
        CustomInstanceStepRate,
        PrimitiveRestart
978 979
    };

980 981 982 983 984
    enum ResourceSizeLimit {
        TextureSizeMin = 1,
        TextureSizeMax
    };

985 986
    ~QRhi();

987
    static QRhi *create(Implementation impl, QRhiInitParams *params, Flags flags = Flags());
988

989 990
    QRhiGraphicsPipeline *newGraphicsPipeline();
    QRhiShaderResourceBindings *newShaderResourceBindings();
991

992 993 994
    QRhiBuffer *newBuffer(QRhiBuffer::Type type,
                          QRhiBuffer::UsageFlags usage,
                          int size);
995

996 997 998
    QRhiRenderBuffer *newRenderBuffer(QRhiRenderBuffer::Type type,
                                      const QSize &pixelSize,
                                      int sampleCount = 1,
Laszlo Agocs's avatar
Laszlo Agocs committed
999
                                      QRhiRenderBuffer::Flags flags = QRhiRenderBuffer::Flags());
1000

1001 1002
    QRhiTexture *newTexture(QRhiTexture::Format format,
                            const QSize &pixelSize,
Laszlo Agocs's avatar
Laszlo Agocs committed
1003
                            int sampleCount = 1,
1004
                            QRhiTexture::Flags flags = QRhiTexture::Flags());
1005

1006 1007 1008
    QRhiSampler *newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
                            QRhiSampler::Filter mipmapMode,
                            QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w = QRhiSampler::ClampToEdge);
1009

1010 1011
    QRhiTextureRenderTarget *newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
                                                    QRhiTextureRenderTarget::Flags flags = QRhiTextureRenderTarget::Flags());
1012

1013
    QRhiSwapChain *newSwapChain();
1014 1015 1016
    FrameOpResult beginFrame(QRhiSwapChain *swapChain);
    FrameOpResult endFrame(QRhiSwapChain *swapChain);

1017
    FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb);
1018 1019 1020
    FrameOpResult endOffscreenFrame();

    QRhi::FrameOpResult finish();
1021

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
    QRhiResourceUpdateBatch *nextResourceUpdateBatch();

    QVector<int> supportedSampleCounts() const;

    int ubufAlignment() const;
    int ubufAligned(int v) const;

    int mipLevelsForSize(const QSize &size) const;
    QSize sizeForMipLevel(int mipLevel, const QSize &baseLevelSize) const;

    bool isYUpInFramebuffer() const;

    QMatrix4x4 clipSpaceCorrMatrix() const;

1036
    bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags = QRhiTexture::Flags()) const;
1037
    bool isFeatureSupported(QRhi::Feature feature) const;
1038
    int resourceSizeLimit(ResourceSizeLimit limit) const;
1039

1040 1041
    const QRhiNativeHandles *nativeHandles();

1042
    QRhiProfiler *profiler();
1043

1044 1045 1046 1047 1048 1049 1050 1051
protected:
    QRhi();

private:
    Q_DISABLE_COPY(QRhi)
    QRhiImplementation *d = nullptr;
};

1052 1053
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::Flags)

1054 1055 1056
QT_END_NAMESPACE

#endif