qrhi.h 39.6 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
class QRhiResourceUpdateBatchPrivate;
61
class QRhiProfiler;
62
class QRhiShaderResourceBindingPrivate;
63
class QRhiResourceSharingHostPrivate;
64

Laszlo Agocs's avatar
Laszlo Agocs committed
65
class Q_RHI_EXPORT QRhiColorClearValue
66
{
Laszlo Agocs's avatar
Laszlo Agocs committed
67
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
68 69 70
    QRhiColorClearValue();
    explicit QRhiColorClearValue(const QVector4D &c);
    QRhiColorClearValue(float r, float g, float b, float a);
Laszlo Agocs's avatar
Laszlo Agocs committed
71 72 73 74 75 76

    QVector4D rgba() const { return m_rgba; }
    void setRgba(const QVector4D &c) { m_rgba = c; }

private:
    QVector4D m_rgba;
77 78 79 80
};

Q_DECLARE_TYPEINFO(QRhiColorClearValue, Q_MOVABLE_TYPE);

81 82 83 84
Q_RHI_EXPORT bool operator==(const QRhiColorClearValue &a, const QRhiColorClearValue &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiColorClearValue &a, const QRhiColorClearValue &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiColorClearValue &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
85
class Q_RHI_EXPORT QRhiDepthStencilClearValue
86
{
Laszlo Agocs's avatar
Laszlo Agocs committed
87
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
88 89
    QRhiDepthStencilClearValue();
    QRhiDepthStencilClearValue(float d, quint32 s);
Laszlo Agocs's avatar
Laszlo Agocs committed
90 91 92 93 94 95 96 97 98 99

    float depthClearValue() const { return m_d; }
    void setDepthClearValue(float d) { m_d = d; }

    quint32 stencilClearValue() const { return m_s; }
    void setStencilClearValue(quint32 s) { m_s = s; }

private:
    float m_d;
    quint32 m_s;
100 101 102 103
};

Q_DECLARE_TYPEINFO(QRhiDepthStencilClearValue, Q_MOVABLE_TYPE);

104 105 106 107
Q_RHI_EXPORT bool operator==(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiDepthStencilClearValue &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
108
class Q_RHI_EXPORT QRhiViewport
109
{
Laszlo Agocs's avatar
Laszlo Agocs committed
110
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
111 112
    QRhiViewport();
    QRhiViewport(float x, float y, float w, float h, float minDepth = 0.0f, float maxDepth = 1.0f);
Laszlo Agocs's avatar
Laszlo Agocs committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126

    QVector4D viewport() const { return m_rect; }
    void setViewport(const QVector4D &v) { m_rect = v; }

    float minDepth() const { return m_minDepth; }
    void setMinDepth(float minDepth) { m_minDepth = minDepth; }

    float maxDepth() const { return m_maxDepth; }
    void setMaxDepth(float maxDepth) { m_maxDepth = maxDepth; }

private:
    QVector4D m_rect;
    float m_minDepth;
    float m_maxDepth;
127 128 129 130
};

Q_DECLARE_TYPEINFO(QRhiViewport, Q_MOVABLE_TYPE);

131 132 133 134
Q_RHI_EXPORT bool operator==(const QRhiViewport &a, const QRhiViewport &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiViewport &a, const QRhiViewport &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiViewport &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
135
class Q_RHI_EXPORT QRhiScissor
136
{
Laszlo Agocs's avatar
Laszlo Agocs committed
137
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
138 139
    QRhiScissor();
    QRhiScissor(int x, int y, int w, int h);
Laszlo Agocs's avatar
Laszlo Agocs committed
140 141 142 143 144 145

    QVector4D scissor() const { return m_rect; }
    void setScissor(const QVector4D &v) { m_rect = v; }

private:
    QVector4D m_rect;
146 147 148 149
};

Q_DECLARE_TYPEINFO(QRhiScissor, Q_MOVABLE_TYPE);

150 151 152 153
Q_RHI_EXPORT bool operator==(const QRhiScissor &a, const QRhiScissor &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiScissor &a, const QRhiScissor &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiScissor &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
154
class Q_RHI_EXPORT QRhiVertexInputBinding
155
{
Laszlo Agocs's avatar
Laszlo Agocs committed
156 157 158 159 160 161
public:
    enum Classification {
        PerVertex,
        PerInstance
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
162 163
    QRhiVertexInputBinding();
    QRhiVertexInputBinding(quint32 stride, Classification cls = PerVertex, int stepRate = 1);
Laszlo Agocs's avatar
Laszlo Agocs committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177

    quint32 stride() const { return m_stride; }
    void setStride(quint32 s) { m_stride = s; }

    Classification classification() const { return m_classification; }
    void setClassification(Classification c) { m_classification = c; }

    int instanceStepRate() const { return m_instanceStepRate; }
    void setInstanceStepRate(int rate) { m_instanceStepRate = rate; }

private:
    quint32 m_stride;
    Classification m_classification;
    int m_instanceStepRate;
178
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
179 180 181 182
};

Q_DECLARE_TYPEINFO(QRhiVertexInputBinding, Q_MOVABLE_TYPE);

183 184 185 186
Q_RHI_EXPORT bool operator==(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiVertexInputBinding &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
187 188 189 190 191 192 193 194 195 196 197 198 199
class Q_RHI_EXPORT QRhiVertexInputAttribute
{
public:
    enum Format {
        Float4,
        Float3,
        Float2,
        Float,
        UNormByte4,
        UNormByte2,
        UNormByte
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
200 201
    QRhiVertexInputAttribute();
    QRhiVertexInputAttribute(int binding, int location, Format format, quint32 offset);
Laszlo Agocs's avatar
Laszlo Agocs committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

    int binding() const { return m_binding; }
    void setBinding(int b) { m_binding = b; }

    int location() const { return m_location; }
    void setLocation(int loc) { m_location = loc; }

    Format format() const { return m_format; }
    void setFormt(Format f) { m_format = f; }

    quint32 offset() const { return m_offset; }
    void setOffset(quint32 ofs) { m_offset = ofs; }

private:
    int m_binding;
    int m_location;
    Format m_format;
    quint32 m_offset;
220
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
221 222 223 224
};

Q_DECLARE_TYPEINFO(QRhiVertexInputAttribute, Q_MOVABLE_TYPE);

225 226 227 228
Q_RHI_EXPORT bool operator==(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiVertexInputAttribute &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
229 230 231
class Q_RHI_EXPORT QRhiVertexInputLayout
{
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
232 233
    QRhiVertexInputLayout();

Laszlo Agocs's avatar
Laszlo Agocs committed
234 235 236 237 238 239 240 241 242
    QVector<QRhiVertexInputBinding> bindings() const { return m_bindings; }
    void setBindings(const QVector<QRhiVertexInputBinding> &v) { m_bindings = v; }

    QVector<QRhiVertexInputAttribute> attributes() const { return m_attributes; }
    void setAttributes(const QVector<QRhiVertexInputAttribute> &v) { m_attributes = v; }

private:
    QVector<QRhiVertexInputBinding> m_bindings;
    QVector<QRhiVertexInputAttribute> m_attributes;
243
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
244 245 246 247
};

Q_DECLARE_TYPEINFO(QRhiVertexInputLayout, Q_MOVABLE_TYPE);

248 249 250 251
Q_RHI_EXPORT bool operator==(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiVertexInputLayout &v, uint seed = 0) Q_DECL_NOTHROW;

Laszlo Agocs's avatar
Laszlo Agocs committed
252
class Q_RHI_EXPORT QRhiGraphicsShaderStage
253
{
Laszlo Agocs's avatar
Laszlo Agocs committed
254
public:
255 256 257
    enum Type {
        Vertex,
        Fragment,
258 259
        TessellationControl,
        TessellationEvaluation
260 261
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
262 263
    QRhiGraphicsShaderStage();
    QRhiGraphicsShaderStage(Type type, const QBakedShader &shader);
264

Laszlo Agocs's avatar
Laszlo Agocs committed
265 266 267 268 269 270 271 272 273
    Type type() const { return m_type; }
    void setType(Type t) { m_type = t; }

    QBakedShader shader() const { return m_shader; }
    void setShader(const QBakedShader &s) { m_shader = s; }

private:
    Type m_type;
    QBakedShader m_shader;
274
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
275 276 277 278
};

Q_DECLARE_TYPEINFO(QRhiGraphicsShaderStage, Q_MOVABLE_TYPE);

279 280 281 282
Q_RHI_EXPORT bool operator==(const QRhiGraphicsShaderStage &a, const QRhiGraphicsShaderStage &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiGraphicsShaderStage &a, const QRhiGraphicsShaderStage &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiGraphicsShaderStage &s, uint seed = 0) Q_DECL_NOTHROW;

283
class Q_RHI_EXPORT QRhiShaderResourceBinding
Laszlo Agocs's avatar
Laszlo Agocs committed
284
{
285
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
286 287 288 289 290 291 292 293
    enum Type {
        UniformBuffer,
        SampledTexture
    };

    enum StageFlag {
        VertexStage = 1 << 0,
        FragmentStage = 1 << 1,
294 295
        TessellationControlStage = 1 << 2,
        TessellationEvaluationStage = 1 << 3
Laszlo Agocs's avatar
Laszlo Agocs committed
296 297 298
    };
    Q_DECLARE_FLAGS(StageFlags, StageFlag)

299 300 301 302 303 304 305 306 307 308 309 310 311
    QRhiShaderResourceBinding();
    QRhiShaderResourceBinding(const QRhiShaderResourceBinding &other);
    QRhiShaderResourceBinding &operator=(const QRhiShaderResourceBinding &other);
    ~QRhiShaderResourceBinding();
    void detach();

    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);

private:
    QRhiShaderResourceBindingPrivate *d;
    friend class QRhiShaderResourceBindingPrivate;
312 313 314
    friend Q_RHI_EXPORT bool operator==(const QRhiShaderResourceBinding &, const QRhiShaderResourceBinding &) Q_DECL_NOTHROW;
    friend Q_RHI_EXPORT bool operator!=(const QRhiShaderResourceBinding &, const QRhiShaderResourceBinding &) Q_DECL_NOTHROW;
    friend Q_RHI_EXPORT uint qHash(const QRhiShaderResourceBinding &, uint) Q_DECL_NOTHROW;
Laszlo Agocs's avatar
Laszlo Agocs committed
315 316 317
#ifndef QT_NO_DEBUG_STREAM
    friend Q_RHI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBinding &);
#endif
Laszlo Agocs's avatar
Laszlo Agocs committed
318 319 320 321
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiShaderResourceBinding::StageFlags)

322 323 324
Q_RHI_EXPORT bool operator==(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT bool operator!=(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) Q_DECL_NOTHROW;
Q_RHI_EXPORT uint qHash(const QRhiShaderResourceBinding &b, uint seed = 0) Q_DECL_NOTHROW;
Laszlo Agocs's avatar
Laszlo Agocs committed
325 326 327
#ifndef QT_NO_DEBUG_STREAM
Q_RHI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBinding &);
#endif
328

Laszlo Agocs's avatar
Laszlo Agocs committed
329
class Q_RHI_EXPORT QRhiColorAttachment
Laszlo Agocs's avatar
Laszlo Agocs committed
330
{
Laszlo Agocs's avatar
Laszlo Agocs committed
331
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
332 333 334
    QRhiColorAttachment();
    QRhiColorAttachment(QRhiTexture *texture);
    QRhiColorAttachment(QRhiRenderBuffer *renderBuffer);
335

Laszlo Agocs's avatar
Laszlo Agocs committed
336 337
    QRhiTexture *texture() const { return m_texture; }
    void setTexture(QRhiTexture *tex) { m_texture = tex; }
338

Laszlo Agocs's avatar
Laszlo Agocs committed
339 340
    QRhiRenderBuffer *renderBuffer() const { return m_renderBuffer; }
    void setRenderBuffer(QRhiRenderBuffer *rb) { m_renderBuffer = rb; }
341

Laszlo Agocs's avatar
Laszlo Agocs committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    int layer() const { return m_layer; }
    void setLayer(int layer) { m_layer = layer; }

    int level() const { return m_level; }
    void setLevel(int level) { m_level = level; }

    QRhiTexture *resolveTexture() const { return m_resolveTexture; }
    void setResolveTexture(QRhiTexture *tex) { m_resolveTexture = tex; }

    int resolveLayer() const { return m_resolveLayer; }
    void setResolveLayer(int layer) { m_resolveLayer = layer; }

    int resolveLevel() const { return m_resolveLevel; }
    void setResolveLevel(int level) { m_resolveLevel = level; }

private:
    QRhiTexture *m_texture = nullptr;
    QRhiRenderBuffer *m_renderBuffer = nullptr;
    int m_layer = 0;
    int m_level = 0;
    QRhiTexture *m_resolveTexture = nullptr;
    int m_resolveLayer = 0;
    int m_resolveLevel = 0;
365
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
366 367 368 369 370 371 372
};

Q_DECLARE_TYPEINFO(QRhiColorAttachment, Q_MOVABLE_TYPE);

class Q_RHI_EXPORT QRhiTextureRenderTargetDescription
{
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
373 374 375 376
    QRhiTextureRenderTargetDescription();
    QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment);
    QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment, QRhiRenderBuffer *depthStencilBuffer);
    QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment, QRhiTexture *depthTexture);
Laszlo Agocs's avatar
Laszlo Agocs committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390

    QVector<QRhiColorAttachment> colorAttachments() const { return m_colorAttachments; }
    void setColorAttachments(const QVector<QRhiColorAttachment> &att) { m_colorAttachments = att; }

    QRhiRenderBuffer *depthStencilBuffer() const { return m_depthStencilBuffer; }
    void setDepthStencilBuffer(QRhiRenderBuffer *renderBuffer) { m_depthStencilBuffer = renderBuffer; }

    QRhiTexture *depthTexture() const { return m_depthTexture; }
    void setDepthTexture(QRhiTexture *texture) { m_depthTexture = texture; }

private:
    QVector<QRhiColorAttachment> m_colorAttachments;
    QRhiRenderBuffer *m_depthStencilBuffer = nullptr;
    QRhiTexture *m_depthTexture = nullptr;
391
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
392 393 394 395
};

Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription, Q_MOVABLE_TYPE);

396
class Q_RHI_EXPORT QRhiTextureMipLevel
Laszlo Agocs's avatar
Laszlo Agocs committed
397
{
398
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
399 400 401
    QRhiTextureMipLevel();
    QRhiTextureMipLevel(const QImage &image);
    QRhiTextureMipLevel(const QByteArray &compressedData);
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

    QImage image() const { return m_image; }
    void setImage(const QImage &image) { m_image = image; }

    QByteArray compressedData() const { return m_compressedData; }
    void setCompressedData(const QByteArray &data) { m_compressedData = data; }

    QPoint destinationTopLeft() const { return m_destinationTopLeft; }
    void setDestinationTopLeft(const QPoint &p) { m_destinationTopLeft = p; }

    QSize sourceSize() const { return m_sourceSize; }
    void setSourceSize(const QSize &size) { m_sourceSize = size; }

    QPoint sourceTopLeft() const { return m_sourceTopLeft; }
    void setSourceTopLeft(const QPoint &p) { m_sourceTopLeft = p; }

private:
    QImage m_image;
    QByteArray m_compressedData;
    QPoint m_destinationTopLeft;
    QSize m_sourceSize;
    QPoint m_sourceTopLeft;
424
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
425 426 427 428 429 430 431
};

Q_DECLARE_TYPEINFO(QRhiTextureMipLevel, Q_MOVABLE_TYPE);

class Q_RHI_EXPORT QRhiTextureLayer
{
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
432 433
    QRhiTextureLayer();
    QRhiTextureLayer(const QVector<QRhiTextureMipLevel> &mipImages);
434 435 436 437 438 439

    QVector<QRhiTextureMipLevel> mipImages() const { return m_mipImages; }
    void setMipImages(const QVector<QRhiTextureMipLevel> &images) { m_mipImages = images; }

private:
    QVector<QRhiTextureMipLevel> m_mipImages;
440
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
441
};
442

443 444 445 446 447
Q_DECLARE_TYPEINFO(QRhiTextureLayer, Q_MOVABLE_TYPE);

class Q_RHI_EXPORT QRhiTextureUploadDescription
{
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
448 449
    QRhiTextureUploadDescription();
    QRhiTextureUploadDescription(const QVector<QRhiTextureLayer> &layers);
450 451 452 453 454 455

    QVector<QRhiTextureLayer> layers() const { return m_layers; }
    void setLayers(const QVector<QRhiTextureLayer> &layers) { m_layers = layers; }

private:
    QVector<QRhiTextureLayer> m_layers;
456
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
457 458 459 460
};

Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription, Q_MOVABLE_TYPE);

461
class Q_RHI_EXPORT QRhiTextureCopyDescription
462
{
463
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
464
    QRhiTextureCopyDescription();
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

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

    int sourceLayer() const { return m_sourceLayer; }
    void setSourceLayer(int layer) { m_sourceLayer = layer; }

    int sourceLevel() const { return m_sourceLevel; }
    void setSourceLevel(int level) { m_sourceLevel = level; }

    QPoint sourceTopLeft() const { return m_sourceTopLeft; }
    void setSourceTopLeft(const QPoint &p) { m_sourceTopLeft = p; }

    int destinationLayer() const { return m_destinationLayer; }
    void setDestinationLayer(int layer) { m_destinationLayer = layer; }
480

481 482 483 484 485 486 487 488 489 490 491 492 493 494
    int destinationLevel() const { return m_destinationLevel; }
    void setDestinationLevel(int level) { m_destinationLevel = level; }

    QPoint destinationTopLeft() const { return m_destinationTopLeft; }
    void setDestinationTopLeft(const QPoint &p) { m_destinationTopLeft = p; }

private:
    QSize m_pixelSize;
    int m_sourceLayer = 0;
    int m_sourceLevel = 0;
    QPoint m_sourceTopLeft;
    int m_destinationLayer = 0;
    int m_destinationLevel = 0;
    QPoint m_destinationTopLeft;
495
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
496 497 498 499
};

Q_DECLARE_TYPEINFO(QRhiTextureCopyDescription, Q_MOVABLE_TYPE);

500
class Q_RHI_EXPORT QRhiReadbackDescription
Laszlo Agocs's avatar
Laszlo Agocs committed
501
{
502
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
503 504
    QRhiReadbackDescription();
    QRhiReadbackDescription(QRhiTexture *texture);
505 506 507 508 509 510

    QRhiTexture *texture() const { return m_texture; }
    void setTexture(QRhiTexture *tex) { m_texture = tex; }

    int layer() const { return m_layer; }
    void setLayer(int layer) { m_layer = layer; }
511

512 513 514 515 516 517 518
    int level() const { return m_level; }
    void setLevel(int level) { m_level = level; }

private:
    QRhiTexture *m_texture = nullptr;
    int m_layer = 0;
    int m_level = 0;
519
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
520 521
};

522 523
Q_DECLARE_TYPEINFO(QRhiReadbackDescription, Q_MOVABLE_TYPE);

524 525 526 527
struct Q_RHI_EXPORT QRhiNativeHandles
{
};

528 529 530 531
class Q_RHI_EXPORT QRhiResource
{
public:
    virtual ~QRhiResource();
532

533 534 535
    virtual void release() = 0;
    void releaseAndDestroy();

536 537 538
    QByteArray name() const;
    void setName(const QByteArray &name);

539
    virtual bool isShareable() const;
Laszlo Agocs's avatar
Laszlo Agocs committed
540

541 542 543
protected:
    QRhiResource(QRhiImplementation *rhi_);
    Q_DISABLE_COPY(QRhiResource)
Laszlo Agocs's avatar
Laszlo Agocs committed
544
    friend class QRhiImplementation;
545 546
    QRhiImplementation *rhi = nullptr;
    QByteArray objectName;
Laszlo Agocs's avatar
Laszlo Agocs committed
547 548
    QRhiResourceSharingHostPrivate *orphanedWithRsh = nullptr;
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
549 550 551 552 553 554
};

class Q_RHI_EXPORT QRhiBuffer : public QRhiResource
{
public:
    enum Type {
555 556 557
        Immutable,
        Static,
        Dynamic
558 559 560 561 562 563 564 565 566
    };

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

Laszlo Agocs's avatar
Laszlo Agocs committed
567 568 569 570 571 572 573 574
    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; }
575 576 577 578 579

    virtual bool build() = 0;

protected:
    QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, int size_);
Laszlo Agocs's avatar
Laszlo Agocs committed
580 581
    Type m_type;
    UsageFlags m_usage;
582
    int m_size;
583
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
584 585 586 587 588 589 590 591 592
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiBuffer::UsageFlags)

class Q_RHI_EXPORT QRhiTexture : public QRhiResource
{
public:
    enum Flag {
        RenderTarget = 1 << 0,
593
        ChangesFrequently = 1 << 1,
594
        CubeMap = 1 << 2,
595
        MipMapped = 1 << 3,
596
        sRGB = 1 << 4,
597
        UsedAsTransferSource = 1 << 5,
598
        UsedWithGenerateMips = 1 << 6
599 600 601 602
    };
    Q_DECLARE_FLAGS(Flags, Flag)

    enum Format {
603 604
        UnknownFormat,

605 606 607 608 609 610
        RGBA8,
        BGRA8,
        R8,
        R16,

        D16,
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
        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
639 640
    };

Laszlo Agocs's avatar
Laszlo Agocs committed
641 642 643 644 645 646 647 648
    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; }
649

Laszlo Agocs's avatar
Laszlo Agocs committed
650 651 652
    int sampleCount() const { return m_sampleCount; }
    void setSampleCount(int s) { m_sampleCount = s; }

653
    virtual bool build() = 0;
654 655
    virtual const QRhiNativeHandles *nativeHandles();
    virtual bool buildFrom(const QRhiNativeHandles *src);
656

657
protected:
Laszlo Agocs's avatar
Laszlo Agocs committed
658 659
    QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_,
                int sampleCount_, Flags flags_);
Laszlo Agocs's avatar
Laszlo Agocs committed
660 661
    Format m_format;
    QSize m_pixelSize;
Laszlo Agocs's avatar
Laszlo Agocs committed
662
    int m_sampleCount;
Laszlo Agocs's avatar
Laszlo Agocs committed
663
    Flags m_flags;
664
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
665 666 667 668 669 670 671 672
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTexture::Flags)

class Q_RHI_EXPORT QRhiSampler : public QRhiResource
{
public:
    enum Filter {
673
        None,
674 675 676 677 678 679 680 681 682 683 684 685
        Nearest,
        Linear
    };

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

Laszlo Agocs's avatar
Laszlo Agocs committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    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; }
703 704 705 706 707 708 709

    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
710 711 712 713 714 715
    Filter m_magFilter;
    Filter m_minFilter;
    Filter m_mipmapMode;
    AddressMode m_addressU;
    AddressMode m_addressV;
    AddressMode m_addressW;
716
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
717 718
};

Laszlo Agocs's avatar
Laszlo Agocs committed
719 720 721 722 723 724 725 726 727
class Q_RHI_EXPORT QRhiRenderBuffer : public QRhiResource
{
public:
    enum Type {
        DepthStencil,
        Color
    };

    enum Flag {
728
        UsedWithSwapChainOnly = 1 << 0
Laszlo Agocs's avatar
Laszlo Agocs committed
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
    };
    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;
755
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
756 757 758 759
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiRenderBuffer::Flags)

760
class Q_RHI_EXPORT QRhiRenderPassDescriptor : public QRhiResource
761 762
{
protected:
763
    QRhiRenderPassDescriptor(QRhiImplementation *rhi);
764
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
765 766 767 768 769 770 771 772 773 774 775 776
};

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
777
    virtual float devicePixelRatio() const = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
778

779 780
    QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
    void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
781 782 783

protected:
    QRhiRenderTarget(QRhiImplementation *rhi);
784
    QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
785
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
786 787 788 789 790 791
};

class Q_RHI_EXPORT QRhiTextureRenderTarget : public QRhiRenderTarget
{
public:
    enum Flag {
792 793
        PreserveColorContents = 1 << 0,
        PreserveDepthStencilContents = 1 << 1
794 795 796
    };
    Q_DECLARE_FLAGS(Flags, Flag)

Laszlo Agocs's avatar
Laszlo Agocs committed
797 798
    QRhiTextureRenderTargetDescription description() const { return m_desc; }
    void setDescription(const QRhiTextureRenderTargetDescription &desc) { m_desc = desc; }
799

Laszlo Agocs's avatar
Laszlo Agocs committed
800 801 802
    Flags flags() const { return m_flags; }
    void setFlags(Flags f) { m_flags = f; }

803
    virtual QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
804

805 806 807 808
    virtual bool build() = 0;

protected:
    QRhiTextureRenderTarget(QRhiImplementation *rhi, const QRhiTextureRenderTargetDescription &desc_, Flags flags_);
Laszlo Agocs's avatar
Laszlo Agocs committed
809 810
    QRhiTextureRenderTargetDescription m_desc;
    Flags m_flags;
811 812 813 814 815 816 817
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTextureRenderTarget::Flags)

class Q_RHI_EXPORT QRhiShaderResourceBindings : public QRhiResource
{
public:
Laszlo Agocs's avatar
Laszlo Agocs committed
818 819
    QVector<QRhiShaderResourceBinding> bindings() const { return m_bindings; }
    void setBindings(const QVector<QRhiShaderResourceBinding> &b) { m_bindings = b; }
820 821 822 823 824

    virtual bool build() = 0;

protected:
    QRhiShaderResourceBindings(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
825
    QVector<QRhiShaderResourceBinding> m_bindings;
826
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
Laszlo Agocs's avatar
Laszlo Agocs committed
827 828 829
#ifndef QT_NO_DEBUG_STREAM
    friend Q_RHI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBindings &);
#endif
830 831
};

Laszlo Agocs's avatar
Laszlo Agocs committed
832 833 834 835
#ifndef QT_NO_DEBUG_STREAM
Q_RHI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBindings &);
#endif

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
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
    };

854
    enum CullMode {
855 856 857 858 859 860 861 862 863 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 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
        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
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    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; }

994 995
    QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
    void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
996 997 998 999 1000

    virtual bool build() = 0;

protected:
    QRhiGraphicsPipeline(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
1001 1002 1003 1004
    Flags m_flags;
    Topology m_topology = Triangles;
    CullMode m_cullMode = None;
    FrontFace m_frontFace = CCW;
1005
    QVector<TargetBlend> m_targetBlends;
Laszlo Agocs's avatar
Laszlo Agocs committed
1006 1007 1008 1009 1010 1011
    bool m_depthTest = false;
    bool m_depthWrite = false;
    CompareOp m_depthOp = Less;
    bool m_stencilTest = false;
    StencilOpState m_stencilFront;
    StencilOpState m_stencilBack;
1012 1013 1014
    quint32 m_stencilReadMask = 0xFF;
    quint32 m_stencilWriteMask = 0xFF;
    int m_sampleCount = 1;
Laszlo Agocs's avatar
Laszlo Agocs committed
1015 1016
    QVector<QRhiGraphicsShaderStage> m_shaderStages;
    QRhiVertexInputLayout m_vertexInputLayout;
1017
    QRhiShaderResourceBindings *m_shaderResourceBindings = nullptr;
1018
    QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
1019
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
1020 1021 1022 1023 1024 1025 1026 1027 1028
};

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:
1029
    enum Flag {
1030
        SurfaceHasPreMulAlpha = 1 << 0,
1031
        SurfaceHasNonPreMulAlpha = 1 << 1,
1032
        sRGB = 1 << 2,
1033 1034
        UsedAsTransferSource = 1 << 3,
        NoVSync = 1 << 4
1035
    };
1036
    Q_DECLARE_FLAGS(Flags, Flag)
1037

Laszlo Agocs's avatar
Laszlo Agocs committed
1038 1039 1040
    QWindow *window() const { return m_window; }
    void setWindow(QWindow *window) { m_window = window; }

1041 1042
    Flags flags() const { return m_flags; }
    void setFlags(Flags f) { m_flags = f; }
Laszlo Agocs's avatar
Laszlo Agocs committed
1043 1044 1045 1046 1047 1048 1049

    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; }

1050 1051
    QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
    void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
Laszlo Agocs's avatar
Laszlo Agocs committed
1052

Laszlo Agocs's avatar
Laszlo Agocs committed
1053 1054 1055
    QObject *target() const { return m_target; }
    void setTarget(QObject *obj) { m_target = obj; }

1056 1057
    QSize currentPixelSize() const { return m_currentPixelSize; }

1058 1059
    virtual QRhiCommandBuffer *currentFrameCommandBuffer() = 0;
    virtual QRhiRenderTarget *currentFrameRenderTarget() = 0;
1060
    virtual QSize surfacePixelSize() = 0;
1061
    virtual QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() = 0;
Laszlo Agocs's avatar
Laszlo Agocs committed
1062
    virtual bool buildOrResize() = 0;
1063 1064 1065

protected:
    QRhiSwapChain(QRhiImplementation *rhi);
Laszlo Agocs's avatar
Laszlo Agocs committed
1066
    QWindow *m_window = nullptr;
1067
    Flags m_flags;
Laszlo Agocs's avatar
Laszlo Agocs committed
1068 1069
    QRhiRenderBuffer *m_depthStencil = nullptr;
    int m_sampleCount = 1;
1070
    QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
Laszlo Agocs's avatar
Laszlo Agocs committed
1071
    QObject *m_target = nullptr;
1072
    QSize m_currentPixelSize;
1073
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
1074 1075
};

1076
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiSwapChain::Flags)
1077

1078 1079 1080 1081 1082 1083 1084 1085
class Q_RHI_EXPORT QRhiCommandBuffer : public QRhiResource
{
public:
    enum IndexFormat {
        IndexUInt16,
        IndexUInt32
    };

1086 1087
    void resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates);

1088
    void beginPass(QRhiRenderTarget *rt,
1089 1090
                   const QRhiColorClearValue &colorClearValue,
                   const QRhiDepthStencilClearValue &depthStencilClearValue,
1091
                   QRhiResourceUpdateBatch *resourceUpdates = nullptr);
1092
    void endPass(QRhiResourceUpdateBatch *resourceUpdates = nullptr);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

    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);

1118 1119 1120 1121
    void debugMarkBegin(const QByteArray &name);
    void debugMarkEnd();
    void debugMarkMsg(const QByteArray &msg);

1122 1123
protected:
    QRhiCommandBuffer(QRhiImplementation *rhi);
1124
    Q_DECL_UNUSED_MEMBER quint64 m_reserved;
1125 1126 1127 1128 1129 1130 1131 1132 1133
};

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

1135
class Q_RHI_EXPORT QRhiResourceUpdateBatch
1136 1137 1138
{
public:
    ~QRhiResourceUpdateBatch();
1139

1140 1141
    void release();

1142 1143
    void merge(QRhiResourceUpdateBatch *other);

1144
    void updateDynamicBuffer(QRhiBuffer *buf, int offset, int size, const void *data);
1145
    void uploadStaticBuffer(QRhiBuffer *buf, int offset, int size, const void *data);
1146
    void uploadStaticBuffer(QRhiBuffer *buf, const void *data);
Laszlo Agocs's avatar
Laszlo Agocs committed
1147
    void uploadTexture(QRhiTexture *tex, const QRhiTextureUploadDescription &desc);
1148
    void uploadTexture(QRhiTexture *tex, const QImage &image);
1149
    void copyTexture(QRhiTexture *dst, QRhiTexture *src, const QRhiTextureCopyDescription &desc = QRhiTextureCopyDescription());
1150
    void readBackTexture(const QRhiReadbackDescription &rb, QRhiReadbackResult *result);
1151
    void generateMips(QRhiTexture *tex);
1152 1153 1154 1155 1156

private:
    QRhiResourceUpdateBatch(QRhiImplementation *rhi);
    Q_DISABLE_COPY(QRhiResourceUpdateBatch)
    QRhiResourceUpdateBatchPrivate *d;
Laszlo Agocs's avatar
Laszlo Agocs committed
1157
    friend class QRhiResourceUpdateBatchPrivate;
1158 1159 1160
    friend class QRhi;
};

1161 1162 1163 1164 1165 1166 1167 1168 1169
class Q_RHI_EXPORT QRhiResourceSharingHost
{
public:
    QRhiResourceSharingHost();
    ~QRhiResourceSharingHost();

private:
    Q_DISABLE_COPY(QRhiResourceSharingHost)
    QRhiResourceSharingHostPrivate *d;
Laszlo Agocs's avatar
Laszlo Agocs committed
1170
    friend class QRhiResourceSharingHostPrivate;
1171 1172
};

1173 1174
struct Q_RHI_EXPORT QRhiInitParams
{
1175
    QRhiResourceSharingHost *resourceSharingHost = nullptr;
1176 1177 1178 1179 1180 1181
};

class Q_RHI_EXPORT QRhi
{
public:
    enum Implementation {
Laszlo Agocs's avatar
Laszlo Agocs committed
1182
        Null,
1183 1184 1185 1186 1187 1188
        Vulkan,
        OpenGLES2,
        D3D11,
        Metal
    };

1189
    enum Flag {
1190 1191
        EnableProfiling = 1 << 0,
        EnableDebugMarkers = 1 << 1
1192 1193 1194
    };
    Q_DECLARE_FLAGS(Flags, Flag)

1195 1196 1197 1198 1199 1200 1201
    enum FrameOpResult {
        FrameOpSuccess = 0,
        FrameOpError,
        FrameOpSwapChainOutOfDate,
        FrameOpDeviceLost
    };

1202 1203
    enum Feature {
        MultisampleTexture = 1,
1204
        MultisampleRenderBuffer,
1205
        DebugMarkers,
1206 1207
        Timestamps,
        Instancing,
1208
        CustomInstanceStepRate,
1209
        PrimitiveRestart
1210 1211
    };

1212 1213 1214 1215 1216 1217 1218 1219 1220
    enum BeginFrameFlag {
    };
    Q_DECLARE_FLAGS(BeginFrameFlags, BeginFrameFlag)

    enum EndFrameFlag {
        SkipPresent = 1 << 0
    };
    Q_DECLARE_FLAGS(EndFrameFlags, EndFrameFlag)

1221 1222 1223 1224 1225
    enum ResourceSizeLimit {
        TextureSizeMin = 1,
        TextureSizeMax
    };

1226 1227
    ~QRhi();

1228 1229 1230 1231
    static QRhi *create(Implementation impl,
                        QRhiInitParams *params,
                        Flags flags = Flags(),
                        QRhiNativeHandles *importDevice = nullptr);
1232

1233 1234
    QRhiGraphicsPipeline *newGraphicsPipeline();
    QRhiShaderResourceBindings *newShaderResourceBindings();
1235

1236 1237 1238
    QRhiBuffer *newBuffer(QRhiBuffer::Type type,
                          QRhiBuffer::UsageFlags usage,
                          int size);
1239

1240 1241 1242
    QRhiRenderBuffer *newRenderBuffer(QRhiRenderBuffer::Type type,
                                      const QSize &pixelSize,
                                      int sampleCount = 1,
Laszlo Agocs's avatar
Laszlo Agocs committed
1243
                                      QRhiRenderBuffer::Flags flags = QRhiRenderBuffer::Flags());
1244

1245 1246
    QRhiTexture *newTexture(QRhiTexture::Format format,
                            const QSize &pixelSize,
Laszlo Agocs's avatar
Laszlo Agocs committed
1247
                            int sampleCount = 1,
1248
                            QRhiTexture::Flags flags = QRhiTexture::Flags());
1249

1250 1251 1252
    QRhiSampler *newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
                            QRhiSampler::Filter mipmapMode,
                            QRhiSampler::AddressMode u, QRhiSampler::AddressMode v, QRhiSampler::AddressMode w = QRhiSampler::ClampToEdge);
1253

1254 1255
    QRhiTextureRenderTarget *newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
                                                    QRhiTextureRenderTarget::Flags flags = QRhiTextureRenderTarget::Flags());
1256

1257
    QRhiSwapChain *newSwapChain();
1258 1259
    FrameOpResult beginFrame(QRhiSwapChain *swapChain, BeginFrameFlags flags = BeginFrameFlags());
    FrameOpResult endFrame(QRhiSwapChain *swapChain, EndFrameFlags flags = EndFrameFlags());
1260

1261
    FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb);
1262 1263 1264
    FrameOpResult endOffscreenFrame();

    QRhi::FrameOpResult finish();
1265

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
    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;

1280
    bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags = QRhiTexture::Flags()) const;
1281
    bool isFeatureSupported(QRhi::Feature feature) const;
1282
    int resourceSizeLimit(ResourceSizeLimit limit) const;
1283

1284 1285
    const QRhiNativeHandles *nativeHandles();

1286
    QRhiProfiler *profiler();
1287

1288 1289 1290 1291 1292 1293 1294 1295
protected:
    QRhi();

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

1296
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::Flags)
1297 1298
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::BeginFrameFlags)
Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::EndFrameFlags)
1299

1300 1301 1302
QT_END_NAMESPACE

#endif