Coverage Report

Created: 2026-04-01 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/rhi/qrhi_p.h
Line
Count
Source
1
// Copyright (C) 2023 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#ifndef QRHI_P_H
6
#define QRHI_P_H
7
8
//
9
//  W A R N I N G
10
//  -------------
11
//
12
// This file is not part of the Qt API.  It exists purely as an
13
// implementation detail.  This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#include <rhi/qrhi.h>
20
#include <QBitArray>
21
#include <QAtomicInt>
22
#include <QElapsedTimer>
23
#include <QLoggingCategory>
24
#include <QtCore/qset.h>
25
#include <QtCore/qvarlengtharray.h>
26
#include <QtCore/private/qflatmap_p.h>
27
28
QT_BEGIN_NAMESPACE
29
30
0
#define QRHI_RES(t, x) static_cast<t *>(x)
31
0
#define QRHI_RES_RHI(t) t *rhiD = static_cast<t *>(m_rhi)
32
33
Q_DECLARE_LOGGING_CATEGORY(QRHI_LOG_INFO)
34
Q_DECLARE_LOGGING_CATEGORY(QRHI_LOG_RUB)
35
36
class QRhiImplementation
37
{
38
public:
39
    virtual ~QRhiImplementation();
40
41
    virtual bool create(QRhi::Flags flags) = 0;
42
    virtual void destroy() = 0;
43
    virtual QRhi::AdapterList enumerateAdaptersBeforeCreate(QRhiNativeHandles *nativeHandles) const;
44
45
    virtual QRhiGraphicsPipeline *createGraphicsPipeline() = 0;
46
    virtual QRhiComputePipeline *createComputePipeline() = 0;
47
    virtual QRhiShaderResourceBindings *createShaderResourceBindings() = 0;
48
    virtual QRhiBuffer *createBuffer(QRhiBuffer::Type type,
49
                                     QRhiBuffer::UsageFlags usage,
50
                                     quint32 size) = 0;
51
    virtual QRhiRenderBuffer *createRenderBuffer(QRhiRenderBuffer::Type type,
52
                                                 const QSize &pixelSize,
53
                                                 int sampleCount,
54
                                                 QRhiRenderBuffer::Flags flags,
55
                                                 QRhiTexture::Format backingFormatHint) = 0;
56
    virtual QRhiTexture *createTexture(QRhiTexture::Format format,
57
                                       const QSize &pixelSize,
58
                                       int depth,
59
                                       int arraySize,
60
                                       int sampleCount,
61
                                       QRhiTexture::Flags flags) = 0;
62
    virtual QRhiSampler *createSampler(QRhiSampler::Filter magFilter,
63
                                       QRhiSampler::Filter minFilter,
64
                                       QRhiSampler::Filter mipmapMode,
65
                                       QRhiSampler:: AddressMode u,
66
                                       QRhiSampler::AddressMode v,
67
                                       QRhiSampler::AddressMode w) = 0;
68
69
    virtual QRhiTextureRenderTarget *createTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
70
                                                               QRhiTextureRenderTarget::Flags flags) = 0;
71
72
    virtual QRhiShadingRateMap *createShadingRateMap() = 0;
73
74
    virtual QRhiSwapChain *createSwapChain() = 0;
75
    virtual QRhi::FrameOpResult beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags flags) = 0;
76
    virtual QRhi::FrameOpResult endFrame(QRhiSwapChain *swapChain, QRhi::EndFrameFlags flags) = 0;
77
    virtual QRhi::FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb, QRhi::BeginFrameFlags flags) = 0;
78
    virtual QRhi::FrameOpResult endOffscreenFrame(QRhi::EndFrameFlags flags) = 0;
79
    virtual QRhi::FrameOpResult finish() = 0;
80
81
    virtual void resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) = 0;
82
83
    virtual void beginPass(QRhiCommandBuffer *cb,
84
                           QRhiRenderTarget *rt,
85
                           const QColor &colorClearValue,
86
                           const QRhiDepthStencilClearValue &depthStencilClearValue,
87
                           QRhiResourceUpdateBatch *resourceUpdates,
88
                           QRhiCommandBuffer::BeginPassFlags flags) = 0;
89
    virtual void endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) = 0;
90
91
    virtual void setGraphicsPipeline(QRhiCommandBuffer *cb,
92
                                     QRhiGraphicsPipeline *ps) = 0;
93
94
    virtual void setShaderResources(QRhiCommandBuffer *cb,
95
                                    QRhiShaderResourceBindings *srb,
96
                                    int dynamicOffsetCount,
97
                                    const QRhiCommandBuffer::DynamicOffset *dynamicOffsets) = 0;
98
99
    virtual void setVertexInput(QRhiCommandBuffer *cb,
100
                                int startBinding, int bindingCount, const QRhiCommandBuffer::VertexInput *bindings,
101
                                QRhiBuffer *indexBuf, quint32 indexOffset,
102
                                QRhiCommandBuffer::IndexFormat indexFormat) = 0;
103
104
    virtual void setViewport(QRhiCommandBuffer *cb, const QRhiViewport &viewport) = 0;
105
    virtual void setScissor(QRhiCommandBuffer *cb, const QRhiScissor &scissor) = 0;
106
    virtual void setBlendConstants(QRhiCommandBuffer *cb, const QColor &c) = 0;
107
    virtual void setStencilRef(QRhiCommandBuffer *cb, quint32 refValue) = 0;
108
    virtual void setShadingRate(QRhiCommandBuffer *cb, const QSize &coarsePixelSize) = 0;
109
110
    virtual void draw(QRhiCommandBuffer *cb, quint32 vertexCount,
111
                      quint32 instanceCount, quint32 firstVertex, quint32 firstInstance) = 0;
112
    virtual void drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount,
113
                             quint32 instanceCount, quint32 firstIndex,
114
                             qint32 vertexOffset, quint32 firstInstance) = 0;
115
    virtual void drawIndirect(QRhiCommandBuffer *cb, QRhiBuffer *indirectBuffer,
116
                              quint32 offset, quint32 drawCount, quint32 stride) = 0;
117
    virtual void drawIndexedIndirect(QRhiCommandBuffer *cb, QRhiBuffer *indirectBuffer,
118
                                     quint32 offset, quint32 drawCount, quint32 stride) = 0;
119
120
    virtual void debugMarkBegin(QRhiCommandBuffer *cb, const QByteArray &name) = 0;
121
    virtual void debugMarkEnd(QRhiCommandBuffer *cb) = 0;
122
    virtual void debugMarkMsg(QRhiCommandBuffer *cb, const QByteArray &msg) = 0;
123
124
    virtual void beginComputePass(QRhiCommandBuffer *cb,
125
                                  QRhiResourceUpdateBatch *resourceUpdates,
126
                                  QRhiCommandBuffer::BeginPassFlags flags) = 0;
127
    virtual void endComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) = 0;
128
    virtual void setComputePipeline(QRhiCommandBuffer *cb, QRhiComputePipeline *ps) = 0;
129
    virtual void dispatch(QRhiCommandBuffer *cb, int x, int y, int z) = 0;
130
131
    virtual const QRhiNativeHandles *nativeHandles(QRhiCommandBuffer *cb) = 0;
132
    virtual void beginExternal(QRhiCommandBuffer *cb) = 0;
133
    virtual void endExternal(QRhiCommandBuffer *cb) = 0;
134
    virtual double lastCompletedGpuTime(QRhiCommandBuffer *cb) = 0;
135
136
    virtual QList<int> supportedSampleCounts() const = 0;
137
    virtual int ubufAlignment() const = 0;
138
    virtual QList<QSize> supportedShadingRates(int sampleCount) const = 0;
139
    virtual bool isYUpInFramebuffer() const = 0;
140
    virtual bool isYUpInNDC() const = 0;
141
    virtual bool isClipDepthZeroToOne() const = 0;
142
    virtual QMatrix4x4 clipSpaceCorrMatrix() const = 0;
143
    virtual bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags) const = 0;
144
    virtual bool isFeatureSupported(QRhi::Feature feature) const = 0;
145
    virtual int resourceLimit(QRhi::ResourceLimit limit) const = 0;
146
    virtual const QRhiNativeHandles *nativeHandles() = 0;
147
    virtual QRhiDriverInfo driverInfo() const = 0;
148
    virtual QRhiStats statistics() = 0;
149
    virtual bool makeThreadLocalNativeContextCurrent() = 0;
150
    virtual void setQueueSubmitParams(QRhiNativeHandles *params) = 0;
151
    virtual void releaseCachedResources() = 0;
152
    virtual bool isDeviceLost() const = 0;
153
154
    virtual QByteArray pipelineCacheData() = 0;
155
    virtual void setPipelineCacheData(const QByteArray &data) = 0;
156
157
    static QRhiImplementation *newInstance(QRhi::Implementation impl, QRhiInitParams *params, QRhiNativeHandles *importDevice);
158
    void prepareForCreate(QRhi *rhi, QRhi::Implementation impl, QRhi::Flags flags, QRhiAdapter *adapter);
159
160
    bool isCompressedFormat(QRhiTexture::Format format) const;
161
    void compressedFormatInfo(QRhiTexture::Format format, const QSize &size,
162
                              quint32 *bpl, quint32 *byteSize,
163
                              QSize *blockDim) const;
164
    void textureFormatInfo(QRhiTexture::Format format, const QSize &size,
165
                           quint32 *bpl, quint32 *byteSize, quint32 *bytesPerPixel) const;
166
    bool isStencilSupportingFormat(QRhiTexture::Format format) const;
167
168
    void registerResource(QRhiResource *res, bool ownsNativeResources = true)
169
0
    {
170
        // The ownsNativeResources is relevant for the (graphics resource) leak
171
        // check in ~QRhiImplementation; when false, the registration's sole
172
        // purpose is to automatically null out the resource's m_rhi pointer in
173
        // case the rhi goes away first. (which should not happen in
174
        // well-written applications but we try to be graceful)
175
0
        resources.insert(res, ownsNativeResources);
176
0
    }
177
178
    void unregisterResource(QRhiResource *res)
179
0
    {
180
0
        resources.remove(res);
181
0
    }
182
183
    void addDeleteLater(QRhiResource *res)
184
0
    {
185
0
        if (inFrame)
186
0
            pendingDeleteResources.insert(res);
187
0
        else
188
0
            delete res;
189
0
    }
190
191
    void addCleanupCallback(const QRhi::CleanupCallback &callback)
192
0
    {
193
0
        cleanupCallbacks.append(callback);
194
0
    }
195
196
    void addCleanupCallback(const void *key, const QRhi::CleanupCallback &callback)
197
0
    {
198
0
        keyedCleanupCallbacks[key] = callback;
199
0
    }
200
201
    void removeCleanupCallback(const void *key)
202
0
    {
203
0
        keyedCleanupCallbacks.remove(key);
204
0
    }
205
206
    bool sanityCheckGraphicsPipeline(QRhiGraphicsPipeline *ps);
207
    bool sanityCheckShaderResourceBindings(QRhiShaderResourceBindings *srb);
208
    void updateLayoutDesc(QRhiShaderResourceBindings *srb);
209
210
    quint32 pipelineCacheRhiId() const
211
0
    {
212
0
        const quint32 ver = (QT_VERSION_MAJOR << 16) | (QT_VERSION_MINOR << 8) | (QT_VERSION_PATCH);
213
0
        return (quint32(implType) << 24) | ver;
214
0
    }
215
216
    void pipelineCreationStart()
217
0
    {
218
0
        pipelineCreationTimer.start();
219
0
    }
220
221
    void pipelineCreationEnd()
222
0
    {
223
0
        accumulatedPipelineCreationTime += pipelineCreationTimer.elapsed();
224
0
    }
225
226
    qint64 totalPipelineCreationTime() const
227
0
    {
228
0
        return accumulatedPipelineCreationTime;
229
0
    }
230
231
    QRhiVertexInputAttribute::Format shaderDescVariableFormatToVertexInputFormat(QShaderDescription::VariableType type) const;
232
    quint32 byteSizePerVertexForVertexInputFormat(QRhiVertexInputAttribute::Format format) const;
233
234
    static const QRhiShaderResourceBinding::Data *shaderResourceBindingData(const QRhiShaderResourceBinding &binding)
235
0
    {
236
0
        return &binding.d;
237
0
    }
238
239
    static QRhiShaderResourceBinding::Data *shaderResourceBindingData(QRhiShaderResourceBinding &binding)
240
0
    {
241
0
        return &binding.d;
242
0
    }
243
244
    static bool sortedBindingLessThan(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b)
245
0
    {
246
0
        return a.d.binding < b.d.binding;
247
0
    }
248
249
    int effectiveSampleCount(int sampleCount) const;
250
    QSize clampedSubResourceUploadSize(QSize size, QPoint dstPos, int level, QSize textureSizeAtLevelZero, bool warn = true);
251
252
    void runCleanup();
253
254
    QRhi *q;
255
256
    static const int MAX_SHADER_CACHE_ENTRIES = 128;
257
258
    bool debugMarkers = false;
259
    int currentFrameSlot = 0; // for vk, mtl, and similar. unused by gl and d3d11.
260
    bool inFrame = false;
261
262
    QRhiAdapter *requestedRhiAdapter = nullptr;
263
264
private:
265
    QRhi::Implementation implType;
266
    QThread *implThread;
267
    QVarLengthArray<QRhiResourceUpdateBatch *, 4> resUpdPool;
268
    quint64 resUpdPoolMap = 0;
269
    int lastResUpdIdx = -1;
270
    QHash<QRhiResource *, bool> resources;
271
    QSet<QRhiResource *> pendingDeleteResources;
272
    QVarLengthArray<QRhi::CleanupCallback, 4> cleanupCallbacks;
273
    QHash<const void *, QRhi::CleanupCallback> keyedCleanupCallbacks;
274
    QElapsedTimer pipelineCreationTimer;
275
    qint64 accumulatedPipelineCreationTime = 0;
276
277
    friend class QRhi;
278
    friend class QRhiResourceUpdateBatchPrivate;
279
    friend class QRhiBufferData;
280
};
281
282
enum QRhiTargetRectBoundMode
283
{
284
    UnBounded,
285
    Bounded
286
};
287
288
template<QRhiTargetRectBoundMode boundingMode, typename T, size_t N>
289
bool qrhi_toTopLeftRenderTargetRect(const QSize &outputSize, const std::array<T, N> &r,
290
                                    T *x, T *y, T *w, T *h)
291
{
292
    // x,y are bottom-left in QRhiScissor and QRhiViewport but top-left in
293
    // Vulkan/Metal/D3D. Our input is an OpenGL-style scissor rect where both
294
    // negative x or y, and partly or completely out of bounds rects are
295
    // allowed. The only thing the input here cannot have is a negative width
296
    // or height. We must handle all other input gracefully, clamping to a zero
297
    // width or height rect in the worst case, and ensuring the resulting rect
298
    // is inside the rendertarget's bounds because some APIs' validation/debug
299
    // layers are allergic to out of bounds scissor rects.
300
301
    const T outputWidth = outputSize.width();
302
    const T outputHeight = outputSize.height();
303
    const T inputWidth = r[2];
304
    const T inputHeight = r[3];
305
306
    if (inputWidth < 0 || inputHeight < 0)
307
        return false;
308
309
    *x = r[0];
310
    *y = outputHeight - (r[1] + inputHeight);
311
    *w = inputWidth;
312
    *h = inputHeight;
313
314
    if (boundingMode == Bounded) {
315
        const T widthOffset = *x < 0 ? -*x : 0;
316
        const T heightOffset = *y < 0 ? -*y : 0;
317
        *w = *x < outputWidth ? qMax<T>(0, inputWidth - widthOffset) : 0;
318
        *h = *y < outputHeight ? qMax<T>(0, inputHeight - heightOffset) : 0;
319
320
        if (outputWidth > 0)
321
            *x = qBound<T>(0, *x, outputWidth - 1);
322
        if (outputHeight > 0)
323
            *y = qBound<T>(0, *y, outputHeight - 1);
324
325
        if (*x + *w > outputWidth)
326
            *w = qMax<T>(0, outputWidth - *x);
327
        if (*y + *h > outputHeight)
328
            *h = qMax<T>(0, outputHeight - *y);
329
    }
330
    return true;
331
}
332
333
struct QRhiBufferDataPrivate
334
{
335
    Q_DISABLE_COPY_MOVE(QRhiBufferDataPrivate)
336
0
    QRhiBufferDataPrivate() { } // don't value-initialize smallData
337
    int ref = 1;
338
    quint32 size = 0;
339
    QByteArray largeData;
340
    static constexpr quint32 SMALL_DATA_SIZE = 1024;
341
    char smallData[SMALL_DATA_SIZE];
342
};
343
344
// no detach-with-contents, no atomic refcount, no shrink
345
class QRhiBufferData
346
{
347
public:
348
0
    QRhiBufferData() = default;
349
    ~QRhiBufferData()
350
0
    {
351
0
        if (d && !--d->ref)
352
0
            delete d;
353
0
    }
354
    QRhiBufferData(const QRhiBufferData &other)
355
0
        : d(other.d)
356
0
    {
357
0
        if (d)
358
0
            d->ref += 1;
359
0
    }
360
    QRhiBufferData &operator=(const QRhiBufferData &other)
361
0
    {
362
0
        if (d == other.d)
363
0
            return *this;
364
0
        if (other.d)
365
0
            other.d->ref += 1;
366
0
        if (d && !--d->ref)
367
0
            delete d;
368
0
        d = other.d;
369
0
        return *this;
370
0
    }
371
    const char *constData() const
372
0
    {
373
0
        return d ? (d->size <= QRhiBufferDataPrivate::SMALL_DATA_SIZE ? d->smallData : d->largeData.constData()) : nullptr;
374
0
    }
375
    quint32 size() const
376
0
    {
377
0
        return d ? d->size : 0;
378
0
    }
379
    quint32 largeAlloc() const
380
0
    {
381
0
        return d ? d->largeData.size() : 0;
382
0
    }
383
    void assign(const char *s, quint32 size)
384
0
    {
385
0
        if (!d) {
386
0
            d = new QRhiBufferDataPrivate;
387
0
        } else if (d->ref != 1) {
388
0
            if (QRHI_LOG_RUB().isDebugEnabled())
389
0
                qDebug("[rub] QRhiBufferData %p/%p new backing due to no-copy detach, ref was %d", this, d, d->ref);
390
0
            d->ref -= 1;
391
0
            d = new QRhiBufferDataPrivate;
392
0
        }
393
0
        d->size = size;
394
0
        if (size <= QRhiBufferDataPrivate::SMALL_DATA_SIZE) {
395
0
            memcpy(d->smallData, s, size);
396
0
        } else {
397
0
            if (QRHI_LOG_RUB().isDebugEnabled() && largeAlloc() < size)
398
0
                qDebug("[rub] QRhiBufferData %p/%p new large data allocation %u -> %u", this, d, largeAlloc(), size);
399
0
            d->largeData.assign(QByteArrayView(s, size)); // keeps capacity
400
0
        }
401
0
    }
402
    void assign(QByteArray data)
403
0
    {
404
0
        if (!d) {
405
0
            d = new QRhiBufferDataPrivate;
406
0
        } else if (d->ref != 1) {
407
0
            if (QRHI_LOG_RUB().isDebugEnabled())
408
0
                qDebug("[rub] QRhiBufferData %p/%p new backing due to no-copy detach, ref was %d", this, d, d->ref);
409
0
            d->ref -= 1;
410
0
            d = new QRhiBufferDataPrivate;
411
0
        }
412
0
        d->size = data.size();
413
0
        if (d->size <= QRhiBufferDataPrivate::SMALL_DATA_SIZE) {
414
0
            memcpy(d->smallData, data.constData(), data.size());
415
0
        } else {
416
0
            d->largeData = std::move(data);
417
0
        }
418
0
    }
419
private:
420
    QRhiBufferDataPrivate *d = nullptr;
421
};
422
423
Q_DECLARE_TYPEINFO(QRhiBufferData, Q_RELOCATABLE_TYPE);
424
425
class QRhiResourceUpdateBatchPrivate
426
{
427
public:
428
    struct BufferOp {
429
        enum Type {
430
            DynamicUpdate,
431
            StaticUpload,
432
            Read
433
        };
434
        Type type;
435
        QRhiBuffer *buf;
436
        quint32 offset;
437
        QRhiBufferData data;
438
        quint32 readSize;
439
        QRhiReadbackResult *result;
440
441
        static BufferOp dynamicUpdate(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
442
0
        {
443
0
            BufferOp op = {};
444
0
            changeToDynamicUpdate(&op, buf, offset, size, data);
445
0
            return op;
446
0
        }
447
448
        static void changeToDynamicUpdate(BufferOp *op, QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
449
0
        {
450
0
            op->type = DynamicUpdate;
451
0
            op->buf = buf;
452
0
            op->offset = offset;
453
0
            const int effectiveSize = size ? size : buf->size();
454
0
            op->data.assign(reinterpret_cast<const char *>(data), effectiveSize);
455
0
        }
456
457
        static BufferOp dynamicUpdate(QRhiBuffer *buf, quint32 offset, QByteArray data)
458
0
        {
459
0
            BufferOp op = {};
460
0
            changeToDynamicUpdate(&op, buf, offset, std::move(data));
461
0
            return op;
462
0
        }
463
464
        static void changeToDynamicUpdate(BufferOp *op, QRhiBuffer *buf, quint32 offset, QByteArray data)
465
0
        {
466
0
            op->type = DynamicUpdate;
467
0
            op->buf = buf;
468
0
            op->offset = offset;
469
0
            op->data.assign(std::move(data));
470
0
        }
471
472
        static BufferOp staticUpload(QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
473
0
        {
474
0
            BufferOp op = {};
475
0
            changeToStaticUpload(&op, buf, offset, size, data);
476
0
            return op;
477
0
        }
478
479
        static void changeToStaticUpload(BufferOp *op, QRhiBuffer *buf, quint32 offset, quint32 size, const void *data)
480
0
        {
481
0
            op->type = StaticUpload;
482
0
            op->buf = buf;
483
0
            op->offset = offset;
484
0
            const int effectiveSize = size ? size : buf->size();
485
0
            op->data.assign(reinterpret_cast<const char *>(data), effectiveSize);
486
0
        }
487
488
        static BufferOp staticUpload(QRhiBuffer *buf, quint32 offset, QByteArray data)
489
0
        {
490
0
            BufferOp op = {};
491
0
            changeToStaticUpload(&op, buf, offset, std::move(data));
492
0
            return op;
493
0
        }
494
495
        static void changeToStaticUpload(BufferOp *op, QRhiBuffer *buf, quint32 offset, QByteArray data)
496
0
        {
497
0
            op->type = StaticUpload;
498
0
            op->buf = buf;
499
0
            op->offset = offset;
500
0
            op->data.assign(std::move(data));
501
0
        }
502
503
        static BufferOp read(QRhiBuffer *buf, quint32 offset, quint32 size, QRhiReadbackResult *result)
504
0
        {
505
0
            BufferOp op = {};
506
0
            op.type = Read;
507
0
            op.buf = buf;
508
0
            op.offset = offset;
509
0
            op.readSize = size;
510
0
            op.result = result;
511
0
            return op;
512
0
        }
513
    };
514
515
    struct TextureOp {
516
        enum Type {
517
            Upload,
518
            Copy,
519
            Read,
520
            GenMips
521
        };
522
        Type type;
523
        QRhiTexture *dst;
524
        // Specifying multiple uploads for a subresource must be supported.
525
        // In the backend this can then end up, where applicable, as a
526
        // single, batched copy operation with only one set of barriers.
527
        // This helps when doing for example glyph cache fills.
528
        using MipLevelUploadList = std::array<QVector<QRhiTextureSubresourceUploadDescription>, QRhi::MAX_MIP_LEVELS>;
529
        QVarLengthArray<MipLevelUploadList, 6> subresDesc;
530
        QRhiTexture *src;
531
        QRhiTextureCopyDescription desc;
532
        QRhiReadbackDescription rb;
533
        QRhiReadbackResult *result;
534
535
        static TextureOp upload(QRhiTexture *tex, const QRhiTextureUploadDescription &desc)
536
0
        {
537
0
            TextureOp op = {};
538
0
            op.type = Upload;
539
0
            op.dst = tex;
540
0
            int maxLayer = -1;
541
0
            for (auto it = desc.cbeginEntries(), itEnd = desc.cendEntries(); it != itEnd; ++it) {
542
0
                if (it->layer() > maxLayer)
543
0
                    maxLayer = it->layer();
544
0
            }
545
0
            op.subresDesc.resize(maxLayer + 1);
546
0
            for (auto it = desc.cbeginEntries(), itEnd = desc.cendEntries(); it != itEnd; ++it)
547
0
                op.subresDesc[it->layer()][it->level()].append(it->description());
548
0
            return op;
549
0
        }
550
551
        static TextureOp copy(QRhiTexture *dst, QRhiTexture *src, const QRhiTextureCopyDescription &desc)
552
0
        {
553
0
            TextureOp op = {};
554
0
            op.type = Copy;
555
0
            op.dst = dst;
556
0
            op.src = src;
557
0
            op.desc = desc;
558
0
            return op;
559
0
        }
560
561
        static TextureOp read(const QRhiReadbackDescription &rb, QRhiReadbackResult *result)
562
0
        {
563
0
            TextureOp op = {};
564
0
            op.type = Read;
565
0
            op.rb = rb;
566
0
            op.result = result;
567
0
            return op;
568
0
        }
569
570
        static TextureOp genMips(QRhiTexture *tex)
571
0
        {
572
0
            TextureOp op = {};
573
0
            op.type = GenMips;
574
0
            op.dst = tex;
575
0
            return op;
576
0
        }
577
    };
578
579
    int activeBufferOpCount = 0; // this is the real number of used elements in bufferOps, not bufferOps.count()
580
    static const int BUFFER_OPS_STATIC_ALLOC = 64;
581
    QVarLengthArray<BufferOp, BUFFER_OPS_STATIC_ALLOC> bufferOps;
582
583
    int activeTextureOpCount = 0; // this is the real number of used elements in textureOps, not textureOps.count()
584
    static const int TEXTURE_OPS_STATIC_ALLOC = 32;
585
    QVarLengthArray<TextureOp, TEXTURE_OPS_STATIC_ALLOC> textureOps;
586
587
    QRhiResourceUpdateBatch *q = nullptr;
588
    QRhiImplementation *rhi = nullptr;
589
    int poolIndex = -1;
590
591
    void free();
592
    void merge(QRhiResourceUpdateBatchPrivate *other);
593
    bool hasOptimalCapacity() const;
594
    void trimOpLists();
595
596
0
    static QRhiResourceUpdateBatchPrivate *get(QRhiResourceUpdateBatch *b) { return b->d; }
597
};
598
599
template<typename T>
600
struct QRhiBatchedBindings
601
{
602
    void feed(int binding, T resource) { // binding must be strictly increasing
603
        if (curBinding == -1 || binding > curBinding + 1) {
604
            finish();
605
            curBatch.startBinding = binding;
606
            curBatch.resources.clear();
607
            curBatch.resources.append(resource);
608
        } else {
609
            Q_ASSERT(binding == curBinding + 1);
610
            curBatch.resources.append(resource);
611
        }
612
        curBinding = binding;
613
    }
614
615
    bool finish() {
616
        if (!curBatch.resources.isEmpty())
617
            batches.append(curBatch);
618
        return !batches.isEmpty();
619
    }
620
621
    void clear() {
622
        batches.clear();
623
        curBatch.resources.clear();
624
        curBinding = -1;
625
    }
626
627
    struct Batch {
628
        uint startBinding;
629
        QVector<T> resources;
630
631
        bool operator==(const Batch &other) const
632
        {
633
            return startBinding == other.startBinding && resources == other.resources;
634
        }
635
636
        bool operator!=(const Batch &other) const
637
        {
638
            return !operator==(other);
639
        }
640
    };
641
642
    // some backends make copies of QRhiBatchedBindings -> implicit sharing and
643
    // not having a (possibly wasted) prealloc are beneficial -> use QVector
644
    // instead of QVLA
645
    QVector<Batch> batches; // sorted by startBinding
646
647
    bool operator==(const QRhiBatchedBindings<T> &other) const
648
    {
649
        return batches == other.batches;
650
    }
651
652
    bool operator!=(const QRhiBatchedBindings<T> &other) const
653
    {
654
        return !operator==(other);
655
    }
656
657
private:
658
    Batch curBatch;
659
    int curBinding = -1;
660
};
661
662
class QRhiGlobalObjectIdGenerator
663
{
664
public:
665
#ifdef Q_ATOMIC_INT64_IS_SUPPORTED
666
    using Type = quint64;
667
#else
668
    using Type = quint32;
669
#endif
670
    static Type newId();
671
};
672
673
class QRhiPassResourceTracker
674
{
675
public:
676
    bool isEmpty() const;
677
    void reset();
678
679
    struct UsageState {
680
        int layout;
681
        int access;
682
        int stage;
683
    };
684
685
    enum BufferStage {
686
        BufIndirectDrawStage,
687
        BufVertexInputStage,
688
        BufVertexStage,
689
        BufTCStage,
690
        BufTEStage,
691
        BufFragmentStage,
692
        BufComputeStage,
693
        BufGeometryStage
694
    };
695
696
    enum BufferAccess {
697
        BufIndirectDraw,
698
        BufVertexInput,
699
        BufIndexRead,
700
        BufUniformRead,
701
        BufStorageLoad,
702
        BufStorageStore,
703
        BufStorageLoadStore
704
    };
705
706
    void registerBuffer(QRhiBuffer *buf, int slot, BufferAccess *access, BufferStage *stage,
707
                        const UsageState &state);
708
709
    enum TextureStage {
710
        TexVertexStage,
711
        TexTCStage,
712
        TexTEStage,
713
        TexFragmentStage,
714
        TexColorOutputStage,
715
        TexDepthOutputStage,
716
        TexComputeStage,
717
        TexGeometryStage
718
    };
719
720
    enum TextureAccess {
721
        TexSample,
722
        TexColorOutput,
723
        TexDepthOutput,
724
        TexStorageLoad,
725
        TexStorageStore,
726
        TexStorageLoadStore,
727
        TexShadingRate
728
    };
729
730
    void registerTexture(QRhiTexture *tex, TextureAccess *access, TextureStage *stage,
731
                         const UsageState &state);
732
733
    struct Buffer {
734
        int slot;
735
        BufferAccess access;
736
        BufferStage stage;
737
        UsageState stateAtPassBegin;
738
    };
739
740
0
    const QVarLengthFlatMap<QRhiBuffer *, Buffer, 12> &buffers() const { return m_buffers; }
741
742
    struct Texture {
743
        TextureAccess access;
744
        TextureStage stage;
745
        UsageState stateAtPassBegin;
746
    };
747
748
0
    const QVarLengthFlatMap<QRhiTexture *, Texture, 12> &textures() const { return m_textures; }
749
750
    static BufferStage toPassTrackerBufferStage(QRhiShaderResourceBinding::StageFlags stages);
751
    static TextureStage toPassTrackerTextureStage(QRhiShaderResourceBinding::StageFlags stages);
752
753
private:
754
    QVarLengthFlatMap<QRhiBuffer *, Buffer, 12> m_buffers;
755
    QVarLengthFlatMap<QRhiTexture *, Texture, 12> m_textures;
756
};
757
758
Q_DECLARE_TYPEINFO(QRhiPassResourceTracker::Buffer, Q_RELOCATABLE_TYPE);
759
Q_DECLARE_TYPEINFO(QRhiPassResourceTracker::Texture, Q_RELOCATABLE_TYPE);
760
761
template<typename T, int GROW = 1024>
762
class QRhiBackendCommandList
763
{
764
public:
765
    QRhiBackendCommandList() = default;
766
    ~QRhiBackendCommandList() { delete[] v; }
767
    inline void reset() { p = 0; }
768
    inline bool isEmpty() const { return p == 0; }
769
    inline T &get() {
770
        if (p == a) {
771
            a += GROW;
772
            T *nv = new T[a];
773
            if (v) {
774
                memcpy(nv, v, p * sizeof(T));
775
                delete[] v;
776
            }
777
            v = nv;
778
        }
779
        return v[p++];
780
    }
781
    inline void unget() { --p; }
782
    inline T *cbegin() const { return v; }
783
    inline T *cend() const { return v + p; }
784
    inline T *begin() { return v; }
785
    inline T *end() { return v + p; }
786
private:
787
    Q_DISABLE_COPY(QRhiBackendCommandList)
788
    T *v = nullptr;
789
    int a = 0;
790
    int p = 0;
791
};
792
793
struct QRhiRenderTargetAttachmentTracker
794
{
795
    struct ResId { quint64 id; uint generation; };
796
    using ResIdList = QVarLengthArray<ResId, 8 * 2 + 1>; // color, resolve, ds
797
798
    template<typename TexType, typename RenderBufferType>
799
    static void updateResIdList(const QRhiTextureRenderTargetDescription &desc, ResIdList *dst);
800
801
    template<typename TexType, typename RenderBufferType>
802
    static bool isUpToDate(const QRhiTextureRenderTargetDescription &desc, const ResIdList &currentResIdList);
803
};
804
805
inline bool operator==(const QRhiRenderTargetAttachmentTracker::ResId &a, const QRhiRenderTargetAttachmentTracker::ResId &b)
806
0
{
807
0
    return a.id == b.id && a.generation == b.generation;
808
0
}
809
810
inline bool operator!=(const QRhiRenderTargetAttachmentTracker::ResId &a, const QRhiRenderTargetAttachmentTracker::ResId &b)
811
0
{
812
0
    return !(a == b);
813
0
}
814
815
template<typename TexType, typename RenderBufferType>
816
void QRhiRenderTargetAttachmentTracker::updateResIdList(const QRhiTextureRenderTargetDescription &desc, ResIdList *dst)
817
0
{
818
0
    const bool hasDepthStencil = desc.depthStencilBuffer() || desc.depthTexture();
819
0
    dst->resize(desc.colorAttachmentCount() * 2 + (hasDepthStencil ? 1 : 0));
820
0
    int n = 0;
821
0
    for (auto it = desc.cbeginColorAttachments(), itEnd = desc.cendColorAttachments(); it != itEnd; ++it, ++n) {
822
0
        const QRhiColorAttachment &colorAtt(*it);
823
0
        if (colorAtt.texture()) {
824
0
            TexType *texD = QRHI_RES(TexType, colorAtt.texture());
825
0
            (*dst)[n] = { texD->globalResourceId(), texD->generation };
826
0
        } else if (colorAtt.renderBuffer()) {
827
0
            RenderBufferType *rbD = QRHI_RES(RenderBufferType, colorAtt.renderBuffer());
828
0
            (*dst)[n] = { rbD->globalResourceId(), rbD->generation };
829
0
        } else {
830
0
            (*dst)[n] = { 0, 0 };
831
0
        }
832
0
        ++n;
833
0
        if (colorAtt.resolveTexture()) {
834
0
            TexType *texD = QRHI_RES(TexType, colorAtt.resolveTexture());
835
0
            (*dst)[n] = { texD->globalResourceId(), texD->generation };
836
0
        } else {
837
0
            (*dst)[n] = { 0, 0 };
838
0
        }
839
0
    }
840
0
    if (hasDepthStencil) {
841
0
        if (desc.depthTexture()) {
842
0
            TexType *depthTexD = QRHI_RES(TexType, desc.depthTexture());
843
0
            (*dst)[n] = { depthTexD->globalResourceId(), depthTexD->generation };
844
0
        } else if (desc.depthStencilBuffer()) {
845
0
            RenderBufferType *depthRbD = QRHI_RES(RenderBufferType, desc.depthStencilBuffer());
846
0
            (*dst)[n] = { depthRbD->globalResourceId(), depthRbD->generation };
847
0
        } else {
848
0
            (*dst)[n] = { 0, 0 };
849
0
        }
850
0
    }
851
0
}
852
853
template<typename TexType, typename RenderBufferType>
854
bool QRhiRenderTargetAttachmentTracker::isUpToDate(const QRhiTextureRenderTargetDescription &desc, const ResIdList &currentResIdList)
855
0
{
856
    // Just as setShaderResources() recognizes if an srb's referenced
857
    // resources have been rebuilt (got a create() since the srb's
858
    // create()), we should do the same for the textures and renderbuffers
859
    // referenced from the rendertarget. It is not uncommon that a texture
860
    // or ds buffer gets resized due to following a window size in some
861
    // form, which involves a create() on them. It is then nice if the
862
    // render target auto-rebuilds in beginPass().
863
864
0
    ResIdList resIdList;
865
0
    updateResIdList<TexType, RenderBufferType>(desc, &resIdList);
866
0
    return resIdList == currentResIdList;
867
0
}
868
869
template<typename T>
870
inline T *qrhi_objectFromProxyData(QRhiSwapChainProxyData *pd, QWindow *window, QRhi::Implementation impl, uint objectIndex)
871
{
872
    Q_ASSERT(objectIndex < std::size(pd->reserved));
873
    if (!pd->reserved[objectIndex]) // // was not set, no other choice, do it here, whatever thread this is
874
        *pd = QRhi::updateSwapChainProxyData(impl, window);
875
    return static_cast<T *>(pd->reserved[objectIndex]);
876
}
877
878
QT_END_NAMESPACE
879
880
#endif