1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Gui module
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#ifndef QRHI_H
38#define QRHI_H
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49//
50
51#include <QtGui/qtguiglobal.h>
52#include <QSize>
53#include <QMatrix4x4>
54#include <QVector>
55#include <QThread>
56#include <QColor>
57#include <QImage>
58#include <functional>
59#include <array>
60#include <private/qshader_p.h>
61
62QT_BEGIN_NAMESPACE
63
64class QWindow;
65class QRhiImplementation;
66class QRhiBuffer;
67class QRhiRenderBuffer;
68class QRhiTexture;
69class QRhiSampler;
70class QRhiCommandBuffer;
71class QRhiResourceUpdateBatch;
72class QRhiResourceUpdateBatchPrivate;
73class QRhiProfiler;
74class QRhiShaderResourceBindingPrivate;
75
76class Q_GUI_EXPORT QRhiDepthStencilClearValue
77{
78public:
79 QRhiDepthStencilClearValue() = default;
80 QRhiDepthStencilClearValue(float d, quint32 s);
81
82 float depthClearValue() const { return m_d; }
83 void setDepthClearValue(float d) { m_d = d; }
84
85 quint32 stencilClearValue() const { return m_s; }
86 void setStencilClearValue(quint32 s) { m_s = s; }
87
88private:
89 float m_d = 1.0f;
90 quint32 m_s = 0;
91};
92
93Q_DECLARE_TYPEINFO(QRhiDepthStencilClearValue, Q_MOVABLE_TYPE);
94
95Q_GUI_EXPORT bool operator==(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) Q_DECL_NOTHROW;
96Q_GUI_EXPORT bool operator!=(const QRhiDepthStencilClearValue &a, const QRhiDepthStencilClearValue &b) Q_DECL_NOTHROW;
97Q_GUI_EXPORT uint qHash(const QRhiDepthStencilClearValue &v, uint seed = 0) Q_DECL_NOTHROW;
98#ifndef QT_NO_DEBUG_STREAM
99Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiDepthStencilClearValue &);
100#endif
101
102class Q_GUI_EXPORT QRhiViewport
103{
104public:
105 QRhiViewport() = default;
106 QRhiViewport(float x, float y, float w, float h, float minDepth = 0.0f, float maxDepth = 1.0f);
107
108 std::array<float, 4> viewport() const { return m_rect; }
109 void setViewport(float x, float y, float w, float h) {
110 m_rect[0] = x; m_rect[1] = y; m_rect[2] = w; m_rect[3] = h;
111 }
112
113 float minDepth() const { return m_minDepth; }
114 void setMinDepth(float minDepth) { m_minDepth = minDepth; }
115
116 float maxDepth() const { return m_maxDepth; }
117 void setMaxDepth(float maxDepth) { m_maxDepth = maxDepth; }
118
119private:
120 std::array<float, 4> m_rect { { 0.0f, 0.0f, 0.0f, 0.0f } };
121 float m_minDepth = 0.0f;
122 float m_maxDepth = 1.0f;
123};
124
125Q_DECLARE_TYPEINFO(QRhiViewport, Q_MOVABLE_TYPE);
126
127Q_GUI_EXPORT bool operator==(const QRhiViewport &a, const QRhiViewport &b) Q_DECL_NOTHROW;
128Q_GUI_EXPORT bool operator!=(const QRhiViewport &a, const QRhiViewport &b) Q_DECL_NOTHROW;
129Q_GUI_EXPORT uint qHash(const QRhiViewport &v, uint seed = 0) Q_DECL_NOTHROW;
130#ifndef QT_NO_DEBUG_STREAM
131Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiViewport &);
132#endif
133
134class Q_GUI_EXPORT QRhiScissor
135{
136public:
137 QRhiScissor() = default;
138 QRhiScissor(int x, int y, int w, int h);
139
140 std::array<int, 4> scissor() const { return m_rect; }
141 void setScissor(int x, int y, int w, int h) {
142 m_rect[0] = x; m_rect[1] = y; m_rect[2] = w; m_rect[3] = h;
143 }
144
145private:
146 std::array<int, 4> m_rect { { 0, 0, 0, 0 } };
147};
148
149Q_DECLARE_TYPEINFO(QRhiScissor, Q_MOVABLE_TYPE);
150
151Q_GUI_EXPORT bool operator==(const QRhiScissor &a, const QRhiScissor &b) Q_DECL_NOTHROW;
152Q_GUI_EXPORT bool operator!=(const QRhiScissor &a, const QRhiScissor &b) Q_DECL_NOTHROW;
153Q_GUI_EXPORT uint qHash(const QRhiScissor &v, uint seed = 0) Q_DECL_NOTHROW;
154#ifndef QT_NO_DEBUG_STREAM
155Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiScissor &);
156#endif
157
158class Q_GUI_EXPORT QRhiVertexInputBinding
159{
160public:
161 enum Classification {
162 PerVertex,
163 PerInstance
164 };
165
166 QRhiVertexInputBinding() = default;
167 QRhiVertexInputBinding(quint32 stride, Classification cls = PerVertex, int stepRate = 1);
168
169 quint32 stride() const { return m_stride; }
170 void setStride(quint32 s) { m_stride = s; }
171
172 Classification classification() const { return m_classification; }
173 void setClassification(Classification c) { m_classification = c; }
174
175 int instanceStepRate() const { return m_instanceStepRate; }
176 void setInstanceStepRate(int rate) { m_instanceStepRate = rate; }
177
178private:
179 quint32 m_stride = 0;
180 Classification m_classification = PerVertex;
181 int m_instanceStepRate = 1;
182};
183
184Q_DECLARE_TYPEINFO(QRhiVertexInputBinding, Q_MOVABLE_TYPE);
185
186Q_GUI_EXPORT bool operator==(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) Q_DECL_NOTHROW;
187Q_GUI_EXPORT bool operator!=(const QRhiVertexInputBinding &a, const QRhiVertexInputBinding &b) Q_DECL_NOTHROW;
188Q_GUI_EXPORT uint qHash(const QRhiVertexInputBinding &v, uint seed = 0) Q_DECL_NOTHROW;
189#ifndef QT_NO_DEBUG_STREAM
190Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiVertexInputBinding &);
191#endif
192
193class Q_GUI_EXPORT QRhiVertexInputAttribute
194{
195public:
196 enum Format {
197 Float4,
198 Float3,
199 Float2,
200 Float,
201 UNormByte4,
202 UNormByte2,
203 UNormByte
204 };
205
206 QRhiVertexInputAttribute() = default;
207 QRhiVertexInputAttribute(int binding, int location, Format format, quint32 offset);
208
209 int binding() const { return m_binding; }
210 void setBinding(int b) { m_binding = b; }
211
212 int location() const { return m_location; }
213 void setLocation(int loc) { m_location = loc; }
214
215 Format format() const { return m_format; }
216 void setFormt(Format f) { m_format = f; }
217
218 quint32 offset() const { return m_offset; }
219 void setOffset(quint32 ofs) { m_offset = ofs; }
220
221private:
222 int m_binding = 0;
223 int m_location = 0;
224 Format m_format = Float4;
225 quint32 m_offset = 0;
226};
227
228Q_DECLARE_TYPEINFO(QRhiVertexInputAttribute, Q_MOVABLE_TYPE);
229
230Q_GUI_EXPORT bool operator==(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) Q_DECL_NOTHROW;
231Q_GUI_EXPORT bool operator!=(const QRhiVertexInputAttribute &a, const QRhiVertexInputAttribute &b) Q_DECL_NOTHROW;
232Q_GUI_EXPORT uint qHash(const QRhiVertexInputAttribute &v, uint seed = 0) Q_DECL_NOTHROW;
233#ifndef QT_NO_DEBUG_STREAM
234Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiVertexInputAttribute &);
235#endif
236
237class Q_GUI_EXPORT QRhiVertexInputLayout
238{
239public:
240 QRhiVertexInputLayout() = default;
241
242 QVector<QRhiVertexInputBinding> bindings() const { return m_bindings; }
243 void setBindings(const QVector<QRhiVertexInputBinding> &v) { m_bindings = v; }
244
245 QVector<QRhiVertexInputAttribute> attributes() const { return m_attributes; }
246 void setAttributes(const QVector<QRhiVertexInputAttribute> &v) { m_attributes = v; }
247
248private:
249 QVector<QRhiVertexInputBinding> m_bindings;
250 QVector<QRhiVertexInputAttribute> m_attributes;
251};
252
253Q_DECLARE_TYPEINFO(QRhiVertexInputLayout, Q_MOVABLE_TYPE);
254
255Q_GUI_EXPORT bool operator==(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) Q_DECL_NOTHROW;
256Q_GUI_EXPORT bool operator!=(const QRhiVertexInputLayout &a, const QRhiVertexInputLayout &b) Q_DECL_NOTHROW;
257Q_GUI_EXPORT uint qHash(const QRhiVertexInputLayout &v, uint seed = 0) Q_DECL_NOTHROW;
258#ifndef QT_NO_DEBUG_STREAM
259Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiVertexInputLayout &);
260#endif
261
262class Q_GUI_EXPORT QRhiShaderStage
263{
264public:
265 enum Type {
266 Vertex,
267 Fragment,
268 Compute
269 };
270
271 QRhiShaderStage() = default;
272 QRhiShaderStage(Type type, const QShader &shader,
273 QShader::Variant v = QShader::StandardShader);
274
275 Type type() const { return m_type; }
276 void setType(Type t) { m_type = t; }
277
278 QShader shader() const { return m_shader; }
279 void setShader(const QShader &s) { m_shader = s; }
280
281 QShader::Variant shaderVariant() const { return m_shaderVariant; }
282 void setShaderVariant(QShader::Variant v) { m_shaderVariant = v; }
283
284private:
285 Type m_type = Vertex;
286 QShader m_shader;
287 QShader::Variant m_shaderVariant = QShader::StandardShader;
288};
289
290Q_DECLARE_TYPEINFO(QRhiShaderStage, Q_MOVABLE_TYPE);
291
292Q_GUI_EXPORT bool operator==(const QRhiShaderStage &a, const QRhiShaderStage &b) Q_DECL_NOTHROW;
293Q_GUI_EXPORT bool operator!=(const QRhiShaderStage &a, const QRhiShaderStage &b) Q_DECL_NOTHROW;
294Q_GUI_EXPORT uint qHash(const QRhiShaderStage &s, uint seed = 0) Q_DECL_NOTHROW;
295#ifndef QT_NO_DEBUG_STREAM
296Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiShaderStage &);
297#endif
298
299using QRhiGraphicsShaderStage = QRhiShaderStage;
300
301class Q_GUI_EXPORT QRhiShaderResourceBinding
302{
303public:
304 enum Type {
305 UniformBuffer,
306 SampledTexture,
307 ImageLoad,
308 ImageStore,
309 ImageLoadStore,
310 BufferLoad,
311 BufferStore,
312 BufferLoadStore
313 };
314
315 enum StageFlag {
316 VertexStage = 1 << 0,
317 FragmentStage = 1 << 1,
318 ComputeStage = 1 << 2
319 };
320 Q_DECLARE_FLAGS(StageFlags, StageFlag)
321
322 QRhiShaderResourceBinding();
323 QRhiShaderResourceBinding(const QRhiShaderResourceBinding &other);
324 QRhiShaderResourceBinding &operator=(const QRhiShaderResourceBinding &other);
325 ~QRhiShaderResourceBinding();
326 void detach();
327
328 bool isLayoutCompatible(const QRhiShaderResourceBinding &other) const;
329
330 static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf);
331 static QRhiShaderResourceBinding uniformBuffer(int binding, StageFlags stage, QRhiBuffer *buf, int offset, int size);
332 static QRhiShaderResourceBinding uniformBufferWithDynamicOffset(int binding, StageFlags stage, QRhiBuffer *buf, int size);
333
334 static QRhiShaderResourceBinding sampledTexture(int binding, StageFlags stage, QRhiTexture *tex, QRhiSampler *sampler);
335
336 static QRhiShaderResourceBinding imageLoad(int binding, StageFlags stage, QRhiTexture *tex, int level);
337 static QRhiShaderResourceBinding imageStore(int binding, StageFlags stage, QRhiTexture *tex, int level);
338 static QRhiShaderResourceBinding imageLoadStore(int binding, StageFlags stage, QRhiTexture *tex, int level);
339
340 static QRhiShaderResourceBinding bufferLoad(int binding, StageFlags stage, QRhiBuffer *buf);
341 static QRhiShaderResourceBinding bufferLoad(int binding, StageFlags stage, QRhiBuffer *buf, int offset, int size);
342 static QRhiShaderResourceBinding bufferStore(int binding, StageFlags stage, QRhiBuffer *buf);
343 static QRhiShaderResourceBinding bufferStore(int binding, StageFlags stage, QRhiBuffer *buf, int offset, int size);
344 static QRhiShaderResourceBinding bufferLoadStore(int binding, StageFlags stage, QRhiBuffer *buf);
345 static QRhiShaderResourceBinding bufferLoadStore(int binding, StageFlags stage, QRhiBuffer *buf, int offset, int size);
346
347private:
348 QRhiShaderResourceBindingPrivate *d;
349 friend class QRhiShaderResourceBindingPrivate;
350 friend Q_GUI_EXPORT bool operator==(const QRhiShaderResourceBinding &, const QRhiShaderResourceBinding &) Q_DECL_NOTHROW;
351 friend Q_GUI_EXPORT bool operator!=(const QRhiShaderResourceBinding &, const QRhiShaderResourceBinding &) Q_DECL_NOTHROW;
352 friend Q_GUI_EXPORT uint qHash(const QRhiShaderResourceBinding &, uint) Q_DECL_NOTHROW;
353#ifndef QT_NO_DEBUG_STREAM
354 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBinding &);
355#endif
356};
357
358Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiShaderResourceBinding::StageFlags)
359
360Q_GUI_EXPORT bool operator==(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) Q_DECL_NOTHROW;
361Q_GUI_EXPORT bool operator!=(const QRhiShaderResourceBinding &a, const QRhiShaderResourceBinding &b) Q_DECL_NOTHROW;
362Q_GUI_EXPORT uint qHash(const QRhiShaderResourceBinding &b, uint seed = 0) Q_DECL_NOTHROW;
363#ifndef QT_NO_DEBUG_STREAM
364Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBinding &);
365#endif
366
367class Q_GUI_EXPORT QRhiColorAttachment
368{
369public:
370 QRhiColorAttachment() = default;
371 QRhiColorAttachment(QRhiTexture *texture);
372 QRhiColorAttachment(QRhiRenderBuffer *renderBuffer);
373
374 QRhiTexture *texture() const { return m_texture; }
375 void setTexture(QRhiTexture *tex) { m_texture = tex; }
376
377 QRhiRenderBuffer *renderBuffer() const { return m_renderBuffer; }
378 void setRenderBuffer(QRhiRenderBuffer *rb) { m_renderBuffer = rb; }
379
380 int layer() const { return m_layer; }
381 void setLayer(int layer) { m_layer = layer; }
382
383 int level() const { return m_level; }
384 void setLevel(int level) { m_level = level; }
385
386 QRhiTexture *resolveTexture() const { return m_resolveTexture; }
387 void setResolveTexture(QRhiTexture *tex) { m_resolveTexture = tex; }
388
389 int resolveLayer() const { return m_resolveLayer; }
390 void setResolveLayer(int layer) { m_resolveLayer = layer; }
391
392 int resolveLevel() const { return m_resolveLevel; }
393 void setResolveLevel(int level) { m_resolveLevel = level; }
394
395private:
396 QRhiTexture *m_texture = nullptr;
397 QRhiRenderBuffer *m_renderBuffer = nullptr;
398 int m_layer = 0;
399 int m_level = 0;
400 QRhiTexture *m_resolveTexture = nullptr;
401 int m_resolveLayer = 0;
402 int m_resolveLevel = 0;
403};
404
405Q_DECLARE_TYPEINFO(QRhiColorAttachment, Q_MOVABLE_TYPE);
406
407class Q_GUI_EXPORT QRhiTextureRenderTargetDescription
408{
409public:
410 QRhiTextureRenderTargetDescription() = default;
411 QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment);
412 QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment, QRhiRenderBuffer *depthStencilBuffer);
413 QRhiTextureRenderTargetDescription(const QRhiColorAttachment &colorAttachment, QRhiTexture *depthTexture);
414
415 QVector<QRhiColorAttachment> colorAttachments() const { return m_colorAttachments; }
416 void setColorAttachments(const QVector<QRhiColorAttachment> &att) { m_colorAttachments = att; }
417
418 QRhiRenderBuffer *depthStencilBuffer() const { return m_depthStencilBuffer; }
419 void setDepthStencilBuffer(QRhiRenderBuffer *renderBuffer) { m_depthStencilBuffer = renderBuffer; }
420
421 QRhiTexture *depthTexture() const { return m_depthTexture; }
422 void setDepthTexture(QRhiTexture *texture) { m_depthTexture = texture; }
423
424private:
425 QVector<QRhiColorAttachment> m_colorAttachments;
426 QRhiRenderBuffer *m_depthStencilBuffer = nullptr;
427 QRhiTexture *m_depthTexture = nullptr;
428};
429
430Q_DECLARE_TYPEINFO(QRhiTextureRenderTargetDescription, Q_MOVABLE_TYPE);
431
432class Q_GUI_EXPORT QRhiTextureSubresourceUploadDescription
433{
434public:
435 QRhiTextureSubresourceUploadDescription() = default;
436 QRhiTextureSubresourceUploadDescription(const QImage &image);
437 QRhiTextureSubresourceUploadDescription(const void *data, int size);
438
439 QImage image() const { return m_image; }
440 void setImage(const QImage &image) { m_image = image; }
441
442 QByteArray data() const { return m_data; }
443 void setData(const QByteArray &data) { m_data = data; }
444
445 QPoint destinationTopLeft() const { return m_destinationTopLeft; }
446 void setDestinationTopLeft(const QPoint &p) { m_destinationTopLeft = p; }
447
448 QSize sourceSize() const { return m_sourceSize; }
449 void setSourceSize(const QSize &size) { m_sourceSize = size; }
450
451 QPoint sourceTopLeft() const { return m_sourceTopLeft; }
452 void setSourceTopLeft(const QPoint &p) { m_sourceTopLeft = p; }
453
454private:
455 QImage m_image;
456 QByteArray m_data;
457 QPoint m_destinationTopLeft;
458 QSize m_sourceSize;
459 QPoint m_sourceTopLeft;
460};
461
462Q_DECLARE_TYPEINFO(QRhiTextureSubresourceUploadDescription, Q_MOVABLE_TYPE);
463
464class Q_GUI_EXPORT QRhiTextureUploadEntry
465{
466public:
467 QRhiTextureUploadEntry() = default;
468 QRhiTextureUploadEntry(int layer, int level, const QRhiTextureSubresourceUploadDescription &desc);
469
470 int layer() const { return m_layer; }
471 void setLayer(int layer) { m_layer = layer; }
472
473 int level() const { return m_level; }
474 void setLevel(int level) { m_level = level; }
475
476 QRhiTextureSubresourceUploadDescription description() const { return m_desc; }
477 void setDescription(const QRhiTextureSubresourceUploadDescription &desc) { m_desc = desc; }
478
479private:
480 int m_layer = 0;
481 int m_level = 0;
482 QRhiTextureSubresourceUploadDescription m_desc;
483};
484
485Q_DECLARE_TYPEINFO(QRhiTextureUploadEntry, Q_MOVABLE_TYPE);
486
487class Q_GUI_EXPORT QRhiTextureUploadDescription
488{
489public:
490 QRhiTextureUploadDescription() = default;
491 QRhiTextureUploadDescription(const QRhiTextureUploadEntry &entry);
492 QRhiTextureUploadDescription(const QVector<QRhiTextureUploadEntry> &entries);
493
494 QVector<QRhiTextureUploadEntry> entries() const { return m_entries; }
495 void setEntries(const QVector<QRhiTextureUploadEntry> &entries) { m_entries = entries; }
496 void append(const QRhiTextureUploadEntry &entry);
497
498private:
499 QVector<QRhiTextureUploadEntry> m_entries;
500};
501
502Q_DECLARE_TYPEINFO(QRhiTextureUploadDescription, Q_MOVABLE_TYPE);
503
504class Q_GUI_EXPORT QRhiTextureCopyDescription
505{
506public:
507 QRhiTextureCopyDescription() = default;
508
509 QSize pixelSize() const { return m_pixelSize; }
510 void setPixelSize(const QSize &sz) { m_pixelSize = sz; }
511
512 int sourceLayer() const { return m_sourceLayer; }
513 void setSourceLayer(int layer) { m_sourceLayer = layer; }
514
515 int sourceLevel() const { return m_sourceLevel; }
516 void setSourceLevel(int level) { m_sourceLevel = level; }
517
518 QPoint sourceTopLeft() const { return m_sourceTopLeft; }
519 void setSourceTopLeft(const QPoint &p) { m_sourceTopLeft = p; }
520
521 int destinationLayer() const { return m_destinationLayer; }
522 void setDestinationLayer(int layer) { m_destinationLayer = layer; }
523
524 int destinationLevel() const { return m_destinationLevel; }
525 void setDestinationLevel(int level) { m_destinationLevel = level; }
526
527 QPoint destinationTopLeft() const { return m_destinationTopLeft; }
528 void setDestinationTopLeft(const QPoint &p) { m_destinationTopLeft = p; }
529
530private:
531 QSize m_pixelSize;
532 int m_sourceLayer = 0;
533 int m_sourceLevel = 0;
534 QPoint m_sourceTopLeft;
535 int m_destinationLayer = 0;
536 int m_destinationLevel = 0;
537 QPoint m_destinationTopLeft;
538};
539
540Q_DECLARE_TYPEINFO(QRhiTextureCopyDescription, Q_MOVABLE_TYPE);
541
542class Q_GUI_EXPORT QRhiReadbackDescription
543{
544public:
545 QRhiReadbackDescription() = default;
546 QRhiReadbackDescription(QRhiTexture *texture);
547
548 QRhiTexture *texture() const { return m_texture; }
549 void setTexture(QRhiTexture *tex) { m_texture = tex; }
550
551 int layer() const { return m_layer; }
552 void setLayer(int layer) { m_layer = layer; }
553
554 int level() const { return m_level; }
555 void setLevel(int level) { m_level = level; }
556
557private:
558 QRhiTexture *m_texture = nullptr;
559 int m_layer = 0;
560 int m_level = 0;
561};
562
563Q_DECLARE_TYPEINFO(QRhiReadbackDescription, Q_MOVABLE_TYPE);
564
565struct Q_GUI_EXPORT QRhiNativeHandles
566{
567};
568
569class Q_GUI_EXPORT QRhiResource
570{
571public:
572 enum Type {
573 Buffer,
574 Texture,
575 Sampler,
576 RenderBuffer,
577 RenderPassDescriptor,
578 RenderTarget,
579 TextureRenderTarget,
580 ShaderResourceBindings,
581 GraphicsPipeline,
582 SwapChain,
583 ComputePipeline,
584 CommandBuffer
585 };
586
587 virtual ~QRhiResource();
588
589 virtual Type resourceType() const = 0;
590
591 virtual void release() = 0;
592 void releaseAndDestroyLater();
593
594 QByteArray name() const;
595 void setName(const QByteArray &name);
596
597 quint64 globalResourceId() const;
598
599protected:
600 QRhiResource(QRhiImplementation *rhi);
601 Q_DISABLE_COPY(QRhiResource)
602 friend class QRhiImplementation;
603 QRhiImplementation *m_rhi = nullptr;
604 quint64 m_id;
605 QByteArray m_objectName;
606};
607
608class Q_GUI_EXPORT QRhiBuffer : public QRhiResource
609{
610public:
611 enum Type {
612 Immutable,
613 Static,
614 Dynamic
615 };
616
617 enum UsageFlag {
618 VertexBuffer = 1 << 0,
619 IndexBuffer = 1 << 1,
620 UniformBuffer = 1 << 2,
621 StorageBuffer = 1 << 3
622 };
623 Q_DECLARE_FLAGS(UsageFlags, UsageFlag)
624
625 QRhiResource::Type resourceType() const override;
626
627 Type type() const { return m_type; }
628 void setType(Type t) { m_type = t; }
629
630 UsageFlags usage() const { return m_usage; }
631 void setUsage(UsageFlags u) { m_usage = u; }
632
633 int size() const { return m_size; }
634 void setSize(int sz) { m_size = sz; }
635
636 virtual bool build() = 0;
637
638protected:
639 QRhiBuffer(QRhiImplementation *rhi, Type type_, UsageFlags usage_, int size_);
640 Type m_type;
641 UsageFlags m_usage;
642 int m_size;
643};
644
645Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiBuffer::UsageFlags)
646
647class Q_GUI_EXPORT QRhiTexture : public QRhiResource
648{
649public:
650 enum Flag {
651 RenderTarget = 1 << 0,
652 CubeMap = 1 << 2,
653 MipMapped = 1 << 3,
654 sRGB = 1 << 4,
655 UsedAsTransferSource = 1 << 5,
656 UsedWithGenerateMips = 1 << 6,
657 UsedWithLoadStore = 1 << 7
658 };
659 Q_DECLARE_FLAGS(Flags, Flag)
660
661 enum Format {
662 UnknownFormat,
663
664 RGBA8,
665 BGRA8,
666 R8,
667 R16,
668 RED_OR_ALPHA8,
669
670 RGBA16F,
671 RGBA32F,
672
673 D16,
674 D32F,
675
676 BC1,
677 BC2,
678 BC3,
679 BC4,
680 BC5,
681 BC6H,
682 BC7,
683
684 ETC2_RGB8,
685 ETC2_RGB8A1,
686 ETC2_RGBA8,
687
688 ASTC_4x4,
689 ASTC_5x4,
690 ASTC_5x5,
691 ASTC_6x5,
692 ASTC_6x6,
693 ASTC_8x5,
694 ASTC_8x6,
695 ASTC_8x8,
696 ASTC_10x5,
697 ASTC_10x6,
698 ASTC_10x8,
699 ASTC_10x10,
700 ASTC_12x10,
701 ASTC_12x12
702 };
703
704 QRhiResource::Type resourceType() const override;
705
706 Format format() const { return m_format; }
707 void setFormat(Format fmt) { m_format = fmt; }
708
709 QSize pixelSize() const { return m_pixelSize; }
710 void setPixelSize(const QSize &sz) { m_pixelSize = sz; }
711
712 Flags flags() const { return m_flags; }
713 void setFlags(Flags f) { m_flags = f; }
714
715 int sampleCount() const { return m_sampleCount; }
716 void setSampleCount(int s) { m_sampleCount = s; }
717
718 virtual bool build() = 0;
719 virtual const QRhiNativeHandles *nativeHandles();
720 virtual bool buildFrom(const QRhiNativeHandles *src);
721
722protected:
723 QRhiTexture(QRhiImplementation *rhi, Format format_, const QSize &pixelSize_,
724 int sampleCount_, Flags flags_);
725 Format m_format;
726 QSize m_pixelSize;
727 int m_sampleCount;
728 Flags m_flags;
729};
730
731Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTexture::Flags)
732
733class Q_GUI_EXPORT QRhiSampler : public QRhiResource
734{
735public:
736 enum Filter {
737 None,
738 Nearest,
739 Linear
740 };
741
742 enum AddressMode {
743 Repeat,
744 ClampToEdge,
745 Border,
746 Mirror,
747 MirrorOnce
748 };
749
750 enum CompareOp {
751 Never,
752 Less,
753 Equal,
754 LessOrEqual,
755 Greater,
756 NotEqual,
757 GreaterOrEqual,
758 Always
759 };
760
761 QRhiResource::Type resourceType() const override;
762
763 Filter magFilter() const { return m_magFilter; }
764 void setMagFilter(Filter f) { m_magFilter = f; }
765
766 Filter minFilter() const { return m_minFilter; }
767 void setMinFilter(Filter f) { m_minFilter = f; }
768
769 Filter mipmapMode() const { return m_mipmapMode; }
770 void setMipmapMode(Filter f) { m_mipmapMode = f; }
771
772 AddressMode addressU() const { return m_addressU; }
773 void setAddressU(AddressMode mode) { m_addressU = mode; }
774
775 AddressMode addressV() const { return m_addressV; }
776 void setAddressV(AddressMode mode) { m_addressV = mode; }
777
778 AddressMode addressW() const { return m_addressW; }
779 void setAddressW(AddressMode mode) { m_addressW = mode; }
780
781 CompareOp textureCompareOp() const { return m_compareOp; }
782 void setTextureCompareOp(CompareOp op) { m_compareOp = op; }
783
784 virtual bool build() = 0;
785
786protected:
787 QRhiSampler(QRhiImplementation *rhi,
788 Filter magFilter_, Filter minFilter_, Filter mipmapMode_,
789 AddressMode u_, AddressMode v_);
790 Filter m_magFilter;
791 Filter m_minFilter;
792 Filter m_mipmapMode;
793 AddressMode m_addressU;
794 AddressMode m_addressV;
795 AddressMode m_addressW;
796 CompareOp m_compareOp;
797};
798
799class Q_GUI_EXPORT QRhiRenderBuffer : public QRhiResource
800{
801public:
802 enum Type {
803 DepthStencil,
804 Color
805 };
806
807 enum Flag {
808 UsedWithSwapChainOnly = 1 << 0
809 };
810 Q_DECLARE_FLAGS(Flags, Flag)
811
812 QRhiResource::Type resourceType() const override;
813
814 Type type() const { return m_type; }
815 void setType(Type t) { m_type = t; }
816
817 QSize pixelSize() const { return m_pixelSize; }
818 void setPixelSize(const QSize &sz) { m_pixelSize = sz; }
819
820 int sampleCount() const { return m_sampleCount; }
821 void setSampleCount(int s) { m_sampleCount = s; }
822
823 Flags flags() const { return m_flags; }
824 void setFlags(Flags h) { m_flags = h; }
825
826 virtual bool build() = 0;
827
828 virtual QRhiTexture::Format backingFormat() const = 0;
829
830protected:
831 QRhiRenderBuffer(QRhiImplementation *rhi, Type type_, const QSize &pixelSize_,
832 int sampleCount_, Flags flags_);
833 Type m_type;
834 QSize m_pixelSize;
835 int m_sampleCount;
836 Flags m_flags;
837};
838
839Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiRenderBuffer::Flags)
840
841class Q_GUI_EXPORT QRhiRenderPassDescriptor : public QRhiResource
842{
843public:
844 QRhiResource::Type resourceType() const override;
845
846protected:
847 QRhiRenderPassDescriptor(QRhiImplementation *rhi);
848};
849
850class Q_GUI_EXPORT QRhiRenderTarget : public QRhiResource
851{
852public:
853 QRhiResource::Type resourceType() const override;
854
855 virtual QSize pixelSize() const = 0;
856 virtual float devicePixelRatio() const = 0;
857 virtual int sampleCount() const = 0;
858
859 QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
860 void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
861
862protected:
863 QRhiRenderTarget(QRhiImplementation *rhi);
864 QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
865};
866
867class Q_GUI_EXPORT QRhiTextureRenderTarget : public QRhiRenderTarget
868{
869public:
870 enum Flag {
871 PreserveColorContents = 1 << 0,
872 PreserveDepthStencilContents = 1 << 1
873 };
874 Q_DECLARE_FLAGS(Flags, Flag)
875
876 QRhiResource::Type resourceType() const override;
877
878 QRhiTextureRenderTargetDescription description() const { return m_desc; }
879 void setDescription(const QRhiTextureRenderTargetDescription &desc) { m_desc = desc; }
880
881 Flags flags() const { return m_flags; }
882 void setFlags(Flags f) { m_flags = f; }
883
884 virtual QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() = 0;
885
886 virtual bool build() = 0;
887
888protected:
889 QRhiTextureRenderTarget(QRhiImplementation *rhi, const QRhiTextureRenderTargetDescription &desc_, Flags flags_);
890 QRhiTextureRenderTargetDescription m_desc;
891 Flags m_flags;
892};
893
894Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiTextureRenderTarget::Flags)
895
896class Q_GUI_EXPORT QRhiShaderResourceBindings : public QRhiResource
897{
898public:
899 QRhiResource::Type resourceType() const override;
900
901 QVector<QRhiShaderResourceBinding> bindings() const { return m_bindings; }
902 void setBindings(const QVector<QRhiShaderResourceBinding> &b) { m_bindings = b; }
903
904 bool isLayoutCompatible(const QRhiShaderResourceBindings *other) const;
905
906 virtual bool build() = 0;
907
908protected:
909 QRhiShaderResourceBindings(QRhiImplementation *rhi);
910 QVector<QRhiShaderResourceBinding> m_bindings;
911#ifndef QT_NO_DEBUG_STREAM
912 friend Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBindings &);
913#endif
914};
915
916#ifndef QT_NO_DEBUG_STREAM
917Q_GUI_EXPORT QDebug operator<<(QDebug, const QRhiShaderResourceBindings &);
918#endif
919
920class Q_GUI_EXPORT QRhiGraphicsPipeline : public QRhiResource
921{
922public:
923 enum Flag {
924 UsesBlendConstants = 1 << 0,
925 UsesStencilRef = 1 << 1,
926 UsesScissor = 1 << 2
927 };
928 Q_DECLARE_FLAGS(Flags, Flag)
929
930 enum Topology {
931 Triangles,
932 TriangleStrip,
933 Lines,
934 LineStrip,
935 Points
936 };
937
938 enum CullMode {
939 None,
940 Front,
941 Back
942 };
943
944 enum FrontFace {
945 CCW,
946 CW
947 };
948
949 enum ColorMaskComponent {
950 R = 1 << 0,
951 G = 1 << 1,
952 B = 1 << 2,
953 A = 1 << 3
954 };
955 Q_DECLARE_FLAGS(ColorMask, ColorMaskComponent)
956
957 enum BlendFactor {
958 Zero,
959 One,
960 SrcColor,
961 OneMinusSrcColor,
962 DstColor,
963 OneMinusDstColor,
964 SrcAlpha,
965 OneMinusSrcAlpha,
966 DstAlpha,
967 OneMinusDstAlpha,
968 ConstantColor,
969 OneMinusConstantColor,
970 ConstantAlpha,
971 OneMinusConstantAlpha,
972 SrcAlphaSaturate,
973 Src1Color,
974 OneMinusSrc1Color,
975 Src1Alpha,
976 OneMinusSrc1Alpha
977 };
978
979 enum BlendOp {
980 Add,
981 Subtract,
982 ReverseSubtract,
983 Min,
984 Max
985 };
986
987 struct TargetBlend {
988 ColorMask colorWrite = ColorMask(0xF); // R | G | B | A
989 bool enable = false;
990 BlendFactor srcColor = One;
991 BlendFactor dstColor = OneMinusSrcAlpha;
992 BlendOp opColor = Add;
993 BlendFactor srcAlpha = One;
994 BlendFactor dstAlpha = OneMinusSrcAlpha;
995 BlendOp opAlpha = Add;
996 };
997
998 enum CompareOp {
999 Never,
1000 Less,
1001 Equal,
1002 LessOrEqual,
1003 Greater,
1004 NotEqual,
1005 GreaterOrEqual,
1006 Always
1007 };
1008
1009 enum StencilOp {
1010 StencilZero,
1011 Keep,
1012 Replace,
1013 IncrementAndClamp,
1014 DecrementAndClamp,
1015 Invert,
1016 IncrementAndWrap,
1017 DecrementAndWrap
1018 };
1019
1020 struct StencilOpState {
1021 StencilOp failOp = Keep;
1022 StencilOp depthFailOp = Keep;
1023 StencilOp passOp = Keep;
1024 CompareOp compareOp = Always;
1025 };
1026
1027 QRhiResource::Type resourceType() const override;
1028
1029 Flags flags() const { return m_flags; }
1030 void setFlags(Flags f) { m_flags = f; }
1031
1032 Topology topology() const { return m_topology; }
1033 void setTopology(Topology t) { m_topology = t; }
1034
1035 CullMode cullMode() const { return m_cullMode; }
1036 void setCullMode(CullMode mode) { m_cullMode = mode; }
1037
1038 FrontFace frontFace() const { return m_frontFace; }
1039 void setFrontFace(FrontFace f) { m_frontFace = f; }
1040
1041 QVector<TargetBlend> targetBlends() const { return m_targetBlends; }
1042 void setTargetBlends(const QVector<TargetBlend> &blends) { m_targetBlends = blends; }
1043
1044 bool hasDepthTest() const { return m_depthTest; }
1045 void setDepthTest(bool enable) { m_depthTest = enable; }
1046
1047 bool hasDepthWrite() const { return m_depthWrite; }
1048 void setDepthWrite(bool enable) { m_depthWrite = enable; }
1049
1050 CompareOp depthOp() const { return m_depthOp; }
1051 void setDepthOp(CompareOp op) { m_depthOp = op; }
1052
1053 bool hasStencilTest() const { return m_stencilTest; }
1054 void setStencilTest(bool enable) { m_stencilTest = enable; }
1055
1056 StencilOpState stencilFront() const { return m_stencilFront; }
1057 void setStencilFront(const StencilOpState &state) { m_stencilFront = state; }
1058
1059 StencilOpState stencilBack() const { return m_stencilBack; }
1060 void setStencilBack(const StencilOpState &state) { m_stencilBack = state; }
1061
1062 quint32 stencilReadMask() const { return m_stencilReadMask; }
1063 void setStencilReadMask(quint32 mask) { m_stencilReadMask = mask; }
1064
1065 quint32 stencilWriteMask() const { return m_stencilWriteMask; }
1066 void setStencilWriteMask(quint32 mask) { m_stencilWriteMask = mask; }
1067
1068 int sampleCount() const { return m_sampleCount; }
1069 void setSampleCount(int s) { m_sampleCount = s; }
1070
1071 float lineWidth() const { return m_lineWidth; }
1072 void setLineWidth(float width) { m_lineWidth = width; }
1073
1074 QVector<QRhiShaderStage> shaderStages() const { return m_shaderStages; }
1075 void setShaderStages(const QVector<QRhiShaderStage> &stages) { m_shaderStages = stages; }
1076
1077 QRhiVertexInputLayout vertexInputLayout() const { return m_vertexInputLayout; }
1078 void setVertexInputLayout(const QRhiVertexInputLayout &layout) { m_vertexInputLayout = layout; }
1079
1080 QRhiShaderResourceBindings *shaderResourceBindings() const { return m_shaderResourceBindings; }
1081 void setShaderResourceBindings(QRhiShaderResourceBindings *srb) { m_shaderResourceBindings = srb; }
1082
1083 QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
1084 void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
1085
1086 virtual bool build() = 0;
1087
1088protected:
1089 QRhiGraphicsPipeline(QRhiImplementation *rhi);
1090 Flags m_flags;
1091 Topology m_topology = Triangles;
1092 CullMode m_cullMode = None;
1093 FrontFace m_frontFace = CCW;
1094 QVector<TargetBlend> m_targetBlends;
1095 bool m_depthTest = false;
1096 bool m_depthWrite = false;
1097 CompareOp m_depthOp = Less;
1098 bool m_stencilTest = false;
1099 StencilOpState m_stencilFront;
1100 StencilOpState m_stencilBack;
1101 quint32 m_stencilReadMask = 0xFF;
1102 quint32 m_stencilWriteMask = 0xFF;
1103 int m_sampleCount = 1;
1104 float m_lineWidth = 1.0f;
1105 QVector<QRhiShaderStage> m_shaderStages;
1106 QRhiVertexInputLayout m_vertexInputLayout;
1107 QRhiShaderResourceBindings *m_shaderResourceBindings = nullptr;
1108 QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
1109};
1110
1111Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiGraphicsPipeline::Flags)
1112Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiGraphicsPipeline::ColorMask)
1113Q_DECLARE_TYPEINFO(QRhiGraphicsPipeline::TargetBlend, Q_MOVABLE_TYPE);
1114
1115class Q_GUI_EXPORT QRhiSwapChain : public QRhiResource
1116{
1117public:
1118 enum Flag {
1119 SurfaceHasPreMulAlpha = 1 << 0,
1120 SurfaceHasNonPreMulAlpha = 1 << 1,
1121 sRGB = 1 << 2,
1122 UsedAsTransferSource = 1 << 3,
1123 NoVSync = 1 << 4,
1124 MinimalBufferCount = 1 << 5
1125 };
1126 Q_DECLARE_FLAGS(Flags, Flag)
1127
1128 QRhiResource::Type resourceType() const override;
1129
1130 QWindow *window() const { return m_window; }
1131 void setWindow(QWindow *window) { m_window = window; }
1132
1133 Flags flags() const { return m_flags; }
1134 void setFlags(Flags f) { m_flags = f; }
1135
1136 QRhiRenderBuffer *depthStencil() const { return m_depthStencil; }
1137 void setDepthStencil(QRhiRenderBuffer *ds) { m_depthStencil = ds; }
1138
1139 int sampleCount() const { return m_sampleCount; }
1140 void setSampleCount(int samples) { m_sampleCount = samples; }
1141
1142 QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_renderPassDesc; }
1143 void setRenderPassDescriptor(QRhiRenderPassDescriptor *desc) { m_renderPassDesc = desc; }
1144
1145 QSize currentPixelSize() const { return m_currentPixelSize; }
1146
1147 virtual QRhiCommandBuffer *currentFrameCommandBuffer() = 0;
1148 virtual QRhiRenderTarget *currentFrameRenderTarget() = 0;
1149 virtual QSize surfacePixelSize() = 0;
1150 virtual QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() = 0;
1151 virtual bool buildOrResize() = 0;
1152
1153protected:
1154 QRhiSwapChain(QRhiImplementation *rhi);
1155 QWindow *m_window = nullptr;
1156 Flags m_flags;
1157 QRhiRenderBuffer *m_depthStencil = nullptr;
1158 int m_sampleCount = 1;
1159 QRhiRenderPassDescriptor *m_renderPassDesc = nullptr;
1160 QSize m_currentPixelSize;
1161};
1162
1163Q_DECLARE_OPERATORS_FOR_FLAGS(QRhiSwapChain::Flags)
1164
1165class Q_GUI_EXPORT QRhiComputePipeline : public QRhiResource
1166{
1167public:
1168 QRhiResource::Type resourceType() const override;
1169 virtual bool build() = 0;
1170
1171 QRhiShaderStage shaderStage() const { return m_shaderStage; }
1172 void setShaderStage(const QRhiShaderStage &stage) { m_shaderStage = stage; }
1173
1174 QRhiShaderResourceBindings *shaderResourceBindings() const { return m_shaderResourceBindings; }
1175 void setShaderResourceBindings(QRhiShaderResourceBindings *srb) { m_shaderResourceBindings = srb; }
1176
1177protected:
1178 QRhiComputePipeline(QRhiImplementation *rhi);
1179 QRhiShaderStage m_shaderStage;
1180 QRhiShaderResourceBindings *m_shaderResourceBindings = nullptr;
1181};
1182
1183class Q_GUI_EXPORT QRhiCommandBuffer : public QRhiResource
1184{
1185public:
1186 enum IndexFormat {
1187 IndexUInt16,
1188 IndexUInt32
1189 };
1190
1191 QRhiResource::Type resourceType() const override;
1192
1193 void resourceUpdate(QRhiResourceUpdateBatch *resourceUpdates);
1194
1195 void beginPass(QRhiRenderTarget *rt,
1196 const QColor &colorClearValue,
1197 const QRhiDepthStencilClearValue &depthStencilClearValue,
1198 QRhiResourceUpdateBatch *resourceUpdates = nullptr);
1199 void endPass(QRhiResourceUpdateBatch *resourceUpdates = nullptr);
1200
1201 void setGraphicsPipeline(QRhiGraphicsPipeline *ps);
1202 using DynamicOffset = QPair<int, quint32>; // binding, offset
1203 void setShaderResources(QRhiShaderResourceBindings *srb = nullptr,
1204 int dynamicOffsetCount = 0,
1205 const DynamicOffset *dynamicOffsets = nullptr);
1206 using VertexInput = QPair<QRhiBuffer *, quint32>; // buffer, offset
1207 void setVertexInput(int startBinding, int bindingCount, const VertexInput *bindings,
1208 QRhiBuffer *indexBuf = nullptr, quint32 indexOffset = 0,
1209 IndexFormat indexFormat = IndexUInt16);
1210
1211 void setViewport(const QRhiViewport &viewport);
1212 void setScissor(const QRhiScissor &scissor);
1213 void setBlendConstants(const QColor &c);
1214 void setStencilRef(quint32 refValue);
1215
1216 void draw(quint32 vertexCount,
1217 quint32 instanceCount = 1,
1218 quint32 firstVertex = 0,
1219 quint32 firstInstance = 0);
1220
1221 void drawIndexed(quint32 indexCount,
1222 quint32 instanceCount = 1,
1223 quint32 firstIndex = 0,
1224 qint32 vertexOffset = 0,
1225 quint32 firstInstance = 0);
1226
1227 void debugMarkBegin(const QByteArray &name);
1228 void debugMarkEnd();
1229 void debugMarkMsg(const QByteArray &msg);
1230
1231 void beginComputePass(QRhiResourceUpdateBatch *resourceUpdates = nullptr);
1232 void endComputePass(QRhiResourceUpdateBatch *resourceUpdates = nullptr);
1233 void setComputePipeline(QRhiComputePipeline *ps);
1234 void dispatch(int x, int y, int z);
1235
1236 const QRhiNativeHandles *nativeHandles();
1237 void beginExternal();
1238 void endExternal();
1239
1240protected:
1241 QRhiCommandBuffer(QRhiImplementation *rhi);
1242};
1243
1244struct Q_GUI_EXPORT QRhiReadbackResult
1245{
1246 std::function<void()> completed = nullptr;
1247 QRhiTexture::Format format;
1248 QSize pixelSize;
1249 QByteArray data;
1250}; // non-movable due to the std::function
1251
1252class Q_GUI_EXPORT QRhiResourceUpdateBatch
1253{
1254public:
1255 ~QRhiResourceUpdateBatch();
1256
1257 void release();
1258
1259 void merge(QRhiResourceUpdateBatch *other);
1260
1261 void updateDynamicBuffer(QRhiBuffer *buf, int offset, int size, const void *data);
1262 void uploadStaticBuffer(QRhiBuffer *buf, int offset, int size, const void *data);
1263 void uploadStaticBuffer(QRhiBuffer *buf, const void *data);
1264 void uploadTexture(QRhiTexture *tex, const QRhiTextureUploadDescription &desc);
1265 void uploadTexture(QRhiTexture *tex, const QImage &image);
1266 void copyTexture(QRhiTexture *dst, QRhiTexture *src, const QRhiTextureCopyDescription &desc = QRhiTextureCopyDescription());
1267 void readBackTexture(const QRhiReadbackDescription &rb, QRhiReadbackResult *result);
1268 void generateMips(QRhiTexture *tex, int layer = 0);
1269
1270private:
1271 QRhiResourceUpdateBatch(QRhiImplementation *rhi);
1272 Q_DISABLE_COPY(QRhiResourceUpdateBatch)
1273 QRhiResourceUpdateBatchPrivate *d;
1274 friend class QRhiResourceUpdateBatchPrivate;
1275 friend class QRhi;
1276};
1277
1278struct Q_GUI_EXPORT QRhiInitParams
1279{
1280};
1281
1282class Q_GUI_EXPORT QRhi
1283{
1284public:
1285 enum Implementation {
1286 Null,
1287 Vulkan,
1288 OpenGLES2,
1289 D3D11,
1290 Metal
1291 };
1292
1293 enum Flag {
1294 EnableProfiling = 1 << 0,
1295 EnableDebugMarkers = 1 << 1
1296 };
1297 Q_DECLARE_FLAGS(Flags, Flag)
1298
1299 enum FrameOpResult {
1300 FrameOpSuccess = 0,
1301 FrameOpError,
1302 FrameOpSwapChainOutOfDate,
1303 FrameOpDeviceLost
1304 };
1305
1306 enum Feature {
1307 MultisampleTexture = 1,
1308 MultisampleRenderBuffer,
1309 DebugMarkers,
1310 Timestamps,
1311 Instancing,
1312 CustomInstanceStepRate,
1313 PrimitiveRestart,
1314 NonDynamicUniformBuffers,
1315 NonFourAlignedEffectiveIndexBufferOffset,
1316 NPOTTextureRepeat,
1317 RedOrAlpha8IsRed,
1318 ElementIndexUint,
1319 Compute,
1320 WideLines,
1321 VertexShaderPointSize,
1322 BaseVertex,
1323 BaseInstance
1324 };
1325
1326 enum BeginFrameFlag {
1327 };
1328 Q_DECLARE_FLAGS(BeginFrameFlags, BeginFrameFlag)
1329
1330 enum EndFrameFlag {
1331 SkipPresent = 1 << 0
1332 };
1333 Q_DECLARE_FLAGS(EndFrameFlags, EndFrameFlag)
1334
1335 enum ResourceLimit {
1336 TextureSizeMin = 1,
1337 TextureSizeMax,
1338 MaxColorAttachments,
1339 FramesInFlight
1340 };
1341
1342 ~QRhi();
1343
1344 static QRhi *create(Implementation impl,
1345 QRhiInitParams *params,
1346 Flags flags = Flags(),
1347 QRhiNativeHandles *importDevice = nullptr);
1348
1349 Implementation backend() const;
1350 QThread *thread() const;
1351
1352 using CleanupCallback = std::function<void(QRhi *)>;
1353 void addCleanupCallback(const CleanupCallback &callback);
1354 void runCleanup();
1355
1356 QRhiGraphicsPipeline *newGraphicsPipeline();
1357 QRhiComputePipeline *newComputePipeline();
1358 QRhiShaderResourceBindings *newShaderResourceBindings();
1359
1360 QRhiBuffer *newBuffer(QRhiBuffer::Type type,
1361 QRhiBuffer::UsageFlags usage,
1362 int size);
1363
1364 QRhiRenderBuffer *newRenderBuffer(QRhiRenderBuffer::Type type,
1365 const QSize &pixelSize,
1366 int sampleCount = 1,
1367 QRhiRenderBuffer::Flags flags = QRhiRenderBuffer::Flags());
1368
1369 QRhiTexture *newTexture(QRhiTexture::Format format,
1370 const QSize &pixelSize,
1371 int sampleCount = 1,
1372 QRhiTexture::Flags flags = QRhiTexture::Flags());
1373
1374 QRhiSampler *newSampler(QRhiSampler::Filter magFilter, QRhiSampler::Filter minFilter,
1375 QRhiSampler::Filter mipmapMode,
1376 QRhiSampler::AddressMode u, QRhiSampler::AddressMode v);
1377
1378 QRhiTextureRenderTarget *newTextureRenderTarget(const QRhiTextureRenderTargetDescription &desc,
1379 QRhiTextureRenderTarget::Flags flags = QRhiTextureRenderTarget::Flags());
1380
1381 QRhiSwapChain *newSwapChain();
1382 FrameOpResult beginFrame(QRhiSwapChain *swapChain, BeginFrameFlags flags = BeginFrameFlags());
1383 FrameOpResult endFrame(QRhiSwapChain *swapChain, EndFrameFlags flags = EndFrameFlags());
1384 bool isRecordingFrame() const;
1385 int currentFrameSlot() const;
1386
1387 FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb);
1388 FrameOpResult endOffscreenFrame();
1389
1390 QRhi::FrameOpResult finish();
1391
1392 QRhiResourceUpdateBatch *nextResourceUpdateBatch();
1393
1394 QVector<int> supportedSampleCounts() const;
1395
1396 int ubufAlignment() const;
1397 int ubufAligned(int v) const;
1398
1399 int mipLevelsForSize(const QSize &size) const;
1400 QSize sizeForMipLevel(int mipLevel, const QSize &baseLevelSize) const;
1401
1402 bool isYUpInFramebuffer() const;
1403 bool isYUpInNDC() const;
1404 bool isClipDepthZeroToOne() const;
1405
1406 QMatrix4x4 clipSpaceCorrMatrix() const;
1407
1408 bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags = QRhiTexture::Flags()) const;
1409 bool isFeatureSupported(QRhi::Feature feature) const;
1410 int resourceLimit(ResourceLimit limit) const;
1411
1412 const QRhiNativeHandles *nativeHandles();
1413 void makeThreadLocalNativeContextCurrent();
1414
1415 QRhiProfiler *profiler();
1416
1417 static const int MAX_LAYERS = 6; // cubemaps only
1418 static const int MAX_LEVELS = 16; // a width and/or height of 65536 should be enough for everyone
1419
1420protected:
1421 QRhi();
1422
1423private:
1424 Q_DISABLE_COPY(QRhi)
1425 QRhiImplementation *d = nullptr;
1426};
1427
1428Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::Flags)
1429Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::BeginFrameFlags)
1430Q_DECLARE_OPERATORS_FOR_FLAGS(QRhi::EndFrameFlags)
1431
1432QT_END_NAMESPACE
1433
1434#endif
1435